From 04d80daaa662ccd9fbd0ce8b6b150034fd2887c9 Mon Sep 17 00:00:00 2001 From: Karim TAAM Date: Mon, 2 Mar 2020 17:48:32 +0100 Subject: [PATCH 1/3] create file if logBloom-current.cache is missing Signed-off-by: Karim TAAM --- .../besu/ethereum/api/query/TransactionLogBloomCacher.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/query/TransactionLogBloomCacher.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/query/TransactionLogBloomCacher.java index bafbf05fdf4..11fd90e7cdb 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/query/TransactionLogBloomCacher.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/query/TransactionLogBloomCacher.java @@ -172,6 +172,9 @@ private boolean populateLatestSegment() { try { long blockNumber = blockchain.getChainHeadBlockNumber(); final File currentFile = calculateCacheFileName(CURRENT, cacheDir); + if (!currentFile.exists()) { + currentFile.createNewFile(); + } final long segmentNumber = blockNumber / BLOCKS_PER_BLOOM_CACHE; try (final OutputStream out = new FileOutputStream(currentFile)) { fillCacheFile(segmentNumber * BLOCKS_PER_BLOOM_CACHE, blockNumber, out); From 2554227503926929595526508e55302d701a695d Mon Sep 17 00:00:00 2001 From: Karim TAAM Date: Mon, 2 Mar 2020 18:52:42 +0100 Subject: [PATCH 2/3] update populateLatestSegment in order to resolve the missing logs issue Signed-off-by: Karim TAAM --- .../ethereum/api/query/TransactionLogBloomCacher.java | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/query/TransactionLogBloomCacher.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/query/TransactionLogBloomCacher.java index 11fd90e7cdb..a5b472de3ab 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/query/TransactionLogBloomCacher.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/query/TransactionLogBloomCacher.java @@ -170,12 +170,10 @@ private void cacheSingleBlock(final BlockHeader blockHeader, final File cacheFil private boolean populateLatestSegment() { try { - long blockNumber = blockchain.getChainHeadBlockNumber(); final File currentFile = calculateCacheFileName(CURRENT, cacheDir); - if (!currentFile.exists()) { - currentFile.createNewFile(); - } - final long segmentNumber = blockNumber / BLOCKS_PER_BLOOM_CACHE; + + 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); } @@ -184,6 +182,9 @@ private boolean populateLatestSegment() { cacheSingleBlock(blockchain.getBlockHeader(blockNumber).orElseThrow(), currentFile); blockNumber++; } + if (!currentFile.exists()) { + currentFile.createNewFile(); + } Files.move( currentFile.toPath(), calculateCacheFileName(blockNumber, cacheDir).toPath(), From 9f72271cbe2bcde701252d2ffea8465dc43fa0d3 Mon Sep 17 00:00:00 2001 From: Karim TAAM Date: Tue, 3 Mar 2020 13:17:47 +0100 Subject: [PATCH 3/3] add lock for populateLatestSegment Signed-off-by: Karim TAAM --- .../api/query/TransactionLogBloomCacher.java | 50 +++++++++++-------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/query/TransactionLogBloomCacher.java b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/query/TransactionLogBloomCacher.java index a5b472de3ab..94317544c72 100644 --- a/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/query/TransactionLogBloomCacher.java +++ b/ethereum/api/src/main/java/org/hyperledger/besu/ethereum/api/query/TransactionLogBloomCacher.java @@ -56,6 +56,7 @@ public class TransactionLogBloomCacher { private final Map cachedSegments; private final Lock submissionLock = new ReentrantLock(); + private final Lock populateLastFragmentLock = new ReentrantLock(); private final EthScheduler scheduler; private final Blockchain blockchain; @@ -170,31 +171,36 @@ private void cacheSingleBlock(final BlockHeader blockHeader, final File cacheFil private boolean populateLatestSegment() { try { - final File currentFile = calculateCacheFileName(CURRENT, cacheDir); + 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++; - } - if (!currentFile.exists()) { - currentFile.createNewFile(); + 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) {