Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PIE-2303] Automatic log bloom caching - Remove usage of pending file. #407

Merged
merged 2 commits into from
Feb 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.util.Optional;
import java.util.OptionalLong;

import org.apache.logging.log4j.LogManager;
Expand Down Expand Up @@ -51,13 +52,15 @@ public void start() {
(event, __) -> {
if (event.isNewCanonicalHead()) {
transactionLogBloomCacher.cacheLogsBloomForBlockHeader(
event.getBlock().getHeader());
event.getBlock().getHeader(), Optional.empty(), true);
}
}));
chainReorgSubscriptionId =
OptionalLong.of(
blockchain.observeChainReorg(
(header, __) -> transactionLogBloomCacher.cacheLogsBloomForBlockHeader(header)));
(header, __) ->
transactionLogBloomCacher.cacheLogsBloomForBlockHeader(
header, Optional.empty(), true)));

transactionLogBloomCacher
.getScheduler()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import java.io.RandomAccessFile;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.time.Duration;
import java.util.Map;
import java.util.Optional;
Expand All @@ -49,7 +48,6 @@ public class TransactionLogBloomCacher {

public static final int BLOCKS_PER_BLOOM_CACHE = 100_000;
private static final int BLOOM_BITS_LENGTH = 256;
public static final String PENDING = "pending";
private final Map<Long, Boolean> cachedSegments;

private final Lock submissionLock = new ReentrantLock();
Expand Down Expand Up @@ -95,22 +93,16 @@ public CachingStatus generateLogBloomCache(final long start, final long stop) {
LOG.error("Cache directory '{}' does not exist and could not be made.", cacheDir);
return cachingStatus;
}

final File pendingFile = calculateCacheFileName(PENDING, cacheDir);
for (long blockNum = start; blockNum < stop; blockNum += BLOCKS_PER_BLOOM_CACHE) {
LOG.info("Caching segment at {}", blockNum);
try (final FileOutputStream fos = new FileOutputStream(pendingFile)) {
final long blockCount = fillCacheFile(blockNum, blockNum + BLOCKS_PER_BLOOM_CACHE, fos);
if (blockCount == BLOCKS_PER_BLOOM_CACHE) {
Files.move(
pendingFile.toPath(),
calculateCacheFileName(blockNum, cacheDir).toPath(),
StandardCopyOption.REPLACE_EXISTING,
StandardCopyOption.ATOMIC_MOVE);
} else {
LOG.info("Partial segment at {}, only {} blocks cached", blockNum, blockCount);
break;
}
final File cacheFile = calculateCacheFileName(blockNum, cacheDir);
blockchain
.getBlockHeader(blockNum)
.ifPresent(
blockHeader ->
cacheLogsBloomForBlockHeader(blockHeader, Optional.of(cacheFile), false));
try (final FileOutputStream fos = new FileOutputStream(cacheFile)) {
fillCacheFile(blockNum, blockNum + BLOCKS_PER_BLOOM_CACHE, fos);
}
}
} catch (final Exception e) {
Expand All @@ -122,7 +114,7 @@ public CachingStatus generateLogBloomCache(final long start, final long stop) {
return cachingStatus;
}

private long fillCacheFile(
private void fillCacheFile(
final long startBlock, final long stopBlock, final FileOutputStream fos) throws IOException {
long blockNum = startBlock;
while (blockNum < stopBlock) {
Expand All @@ -134,15 +126,19 @@ private long fillCacheFile(
cachingStatus.currentBlock = blockNum;
blockNum++;
}
return blockNum - startBlock;
}

public void cacheLogsBloomForBlockHeader(final BlockHeader blockHeader) {
public void cacheLogsBloomForBlockHeader(
final BlockHeader blockHeader,
final Optional<File> reusedCacheFile,
final boolean ensureChecks) {
try {
final long blockNumber = blockHeader.getNumber();
LOG.debug("Caching logs bloom for block {}.", "0x" + Long.toHexString(blockNumber));
ensurePreviousSegmentsArePresent(blockNumber);
final File cacheFile = calculateCacheFileName(blockNumber, cacheDir);
if (ensureChecks) {
RatanRSur marked this conversation as resolved.
Show resolved Hide resolved
ensurePreviousSegmentsArePresent(blockNumber);
}
final File cacheFile = reusedCacheFile.orElse(calculateCacheFileName(blockNumber, cacheDir));
if (!cacheFile.exists()) {
Files.createFile(cacheFile.toPath());
}
Expand Down