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-2322] Create file if logBloom-current.cache is missing #438

Merged
Merged
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 @@ -56,6 +56,7 @@ public class TransactionLogBloomCacher {
private final Map<Long, Boolean> cachedSegments;

private final Lock submissionLock = new ReentrantLock();
private final Lock populateLastFragmentLock = new ReentrantLock();

private final EthScheduler scheduler;
private final Blockchain blockchain;
Expand Down Expand Up @@ -170,27 +171,36 @@ private void cacheSingleBlock(final BlockHeader blockHeader, final File cacheFil

private boolean populateLatestSegment() {
try {
long blockNumber = blockchain.getChainHeadBlockNumber();
final File currentFile = calculateCacheFileName(CURRENT, cacheDir);
final long segmentNumber = blockNumber / BLOCKS_PER_BLOOM_CACHE;
try (final OutputStream out = new FileOutputStream(currentFile)) {
fillCacheFile(segmentNumber * BLOCKS_PER_BLOOM_CACHE, blockNumber, out);
}
while (blockNumber <= blockchain.getChainHeadBlockNumber()
&& (blockNumber % BLOCKS_PER_BLOOM_CACHE != 0)) {
cacheSingleBlock(blockchain.getBlockHeader(blockNumber).orElseThrow(), currentFile);
blockNumber++;
if (populateLastFragmentLock.tryLock(100, TimeUnit.MILLISECONDS)) {
try {
final File currentFile = calculateCacheFileName(CURRENT, cacheDir);

final long segmentNumber = blockchain.getChainHeadBlockNumber() / BLOCKS_PER_BLOOM_CACHE;
long blockNumber = segmentNumber / BLOCKS_PER_BLOOM_CACHE;
try (final OutputStream out = new FileOutputStream(currentFile)) {
fillCacheFile(segmentNumber * BLOCKS_PER_BLOOM_CACHE, blockNumber, out);
}
while (blockNumber <= blockchain.getChainHeadBlockNumber()
&& (blockNumber % BLOCKS_PER_BLOOM_CACHE != 0)) {
cacheSingleBlock(blockchain.getBlockHeader(blockNumber).orElseThrow(), currentFile);
blockNumber++;
}
Files.move(
currentFile.toPath(),
calculateCacheFileName(blockNumber, cacheDir).toPath(),
StandardCopyOption.REPLACE_EXISTING,
StandardCopyOption.ATOMIC_MOVE);
return true;
} catch (final IOException e) {
LOG.error("Unhandled caching exception.", e);
} finally {
populateLastFragmentLock.unlock();
}
}
Files.move(
currentFile.toPath(),
calculateCacheFileName(blockNumber, cacheDir).toPath(),
StandardCopyOption.REPLACE_EXISTING,
StandardCopyOption.ATOMIC_MOVE);
return true;
} catch (final IOException e) {
LOG.error("Unhandled caching exception.", e);
return false;
} catch (final InterruptedException e) {
// ignore
}
return false;
}

private void ensurePreviousSegmentsArePresent(final long blockNumber) {
Expand Down