From 0e9f9e99f974e2834b37ea9c34821250de38aaeb Mon Sep 17 00:00:00 2001 From: Simon Dudley Date: Wed, 15 Nov 2023 17:14:51 +1000 Subject: [PATCH] Rename cache to queue Signed-off-by: Simon Dudley --- .../bonsai/trielog/TrieLogManager.java | 5 +- .../bonsai/trielog/TrieLogPruner.java | 16 +++---- .../bonsai/trielog/TrieLogPrunerTest.java | 48 +++++++++---------- 3 files changed, 34 insertions(+), 35 deletions(-) diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/bonsai/trielog/TrieLogManager.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/bonsai/trielog/TrieLogManager.java index c642882733e..3a874de6b67 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/bonsai/trielog/TrieLogManager.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/bonsai/trielog/TrieLogManager.java @@ -85,9 +85,8 @@ public synchronized void saveTrieLog( } finally { if (success) { stateUpdater.commit(); - trieLogPruner.cacheForLaterPruning( - forBlockHeader.getNumber(), forBlockHeader.getBlockHash()); - trieLogPruner.pruneFromCache(); + trieLogPruner.addToPruneQueue(forBlockHeader.getNumber(), forBlockHeader.getBlockHash()); + trieLogPruner.pruneFromQueue(); } else { stateUpdater.rollback(); } diff --git a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/bonsai/trielog/TrieLogPruner.java b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/bonsai/trielog/TrieLogPruner.java index 33fd73476ca..d41fca11968 100644 --- a/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/bonsai/trielog/TrieLogPruner.java +++ b/ethereum/core/src/main/java/org/hyperledger/besu/ethereum/bonsai/trielog/TrieLogPruner.java @@ -62,10 +62,10 @@ public TrieLogPruner( } public void initialize() { - preloadCache(); + preloadQueue(); } - private void preloadCache() { + private void preloadQueue() { LOG.atInfo() .setMessage("Loading first {} trie logs from database...") .addArgument(loadingLimit) @@ -86,22 +86,22 @@ private void preloadCache() { } }); LOG.atInfo().log("Loaded {} trie logs from database", count); - pruneFromCache(); + pruneFromQueue(); } catch (Exception e) { LOG.error("Error loading trie logs from database, nothing pruned", e); } } - void cacheForLaterPruning(final long blockNumber, final Hash blockHash) { + void addToPruneQueue(final long blockNumber, final Hash blockHash) { LOG.atTrace() - .setMessage("caching trie log for later pruning blockNumber {}; blockHash {}") + .setMessage("adding trie log to queue for later pruning blockNumber {}; blockHash {}") .addArgument(blockNumber) .addArgument(blockHash) .log(); trieLogBlocksAndForksByDescendingBlockNumber.put(blockNumber, blockHash); } - int pruneFromCache() { + int pruneFromQueue() { final long retainAboveThisBlock = blockchain.getChainHeadBlockNumber() - numBlocksToRetain; final Optional finalized = blockchain.getFinalized(); if (requireFinalizedBlock && finalized.isEmpty()) { @@ -182,12 +182,12 @@ public void initialize() { } @Override - void cacheForLaterPruning(final long blockNumber, final Hash blockHash) { + void addToPruneQueue(final long blockNumber, final Hash blockHash) { // no-op } @Override - int pruneFromCache() { + int pruneFromQueue() { // no-op return -1; } diff --git a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/bonsai/trielog/TrieLogPrunerTest.java b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/bonsai/trielog/TrieLogPrunerTest.java index ca5a7bba429..8538edda378 100644 --- a/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/bonsai/trielog/TrieLogPrunerTest.java +++ b/ethereum/core/src/test/java/org/hyperledger/besu/ethereum/bonsai/trielog/TrieLogPrunerTest.java @@ -55,7 +55,7 @@ public void setup() { } @Test - public void initialize_preloads_cache_and_prunes_orphaned_blocks() { + public void initialize_preloads_queue_and_prunes_orphaned_blocks() { // Given int loadingLimit = 2; final BlockDataGenerator generator = new BlockDataGenerator(); @@ -88,16 +88,16 @@ public void trieLogs_pruned_in_reverse_order_within_pruning_window() { TrieLogPruner trieLogPruner = new TrieLogPruner(worldState, blockchain, blocksToRetain, pruningWindowSize, false); - trieLogPruner.cacheForLaterPruning(0, key(0)); // older block outside prune window - trieLogPruner.cacheForLaterPruning(1, key(1)); // block inside the prune window - trieLogPruner.cacheForLaterPruning(1, key(2)); // same block number (fork) - trieLogPruner.cacheForLaterPruning(2, key(3)); // different block inside prune window - trieLogPruner.cacheForLaterPruning(3, key(4)); // retained block - trieLogPruner.cacheForLaterPruning(4, key(5)); // different retained block - trieLogPruner.cacheForLaterPruning(5, key(6)); // another retained block + trieLogPruner.addToPruneQueue(0, key(0)); // older block outside prune window + trieLogPruner.addToPruneQueue(1, key(1)); // block inside the prune window + trieLogPruner.addToPruneQueue(1, key(2)); // same block number (fork) + trieLogPruner.addToPruneQueue(2, key(3)); // different block inside prune window + trieLogPruner.addToPruneQueue(3, key(4)); // retained block + trieLogPruner.addToPruneQueue(4, key(5)); // different retained block + trieLogPruner.addToPruneQueue(5, key(6)); // another retained block // When - int wasPruned = trieLogPruner.pruneFromCache(); + int wasPruned = trieLogPruner.pruneFromQueue(); // Then assertThat(wasPruned).isEqualTo(3); @@ -107,10 +107,10 @@ public void trieLogs_pruned_in_reverse_order_within_pruning_window() { inOrder.verify(worldState, times(1)).pruneTrieLog(key(2)); // Subsequent run should add one more block, then prune two oldest remaining keys - trieLogPruner.cacheForLaterPruning(6, key(6)); + trieLogPruner.addToPruneQueue(6, key(6)); when(blockchain.getChainHeadBlockNumber()).thenReturn(6L); - wasPruned = trieLogPruner.pruneFromCache(); + wasPruned = trieLogPruner.pruneFromQueue(); assertThat(wasPruned).isEqualTo(2); inOrder.verify(worldState, times(1)).pruneTrieLog(key(4)); @@ -127,7 +127,7 @@ public void retain_non_finalized_blocks() { setupPrunerAndFinalizedBlock(configuredRetainHeight, finalizedBlockHeight); // When - final int wasPruned = trieLogPruner.pruneFromCache(); + final int wasPruned = trieLogPruner.pruneFromQueue(); // Then assertThat(wasPruned).isEqualTo(1); @@ -148,7 +148,7 @@ public void boundary_test_when_configured_retain_equals_finalized_block() { setupPrunerAndFinalizedBlock(configuredRetainHeight, finalizedBlockHeight); // When - final int wasPruned = trieLogPruner.pruneFromCache(); + final int wasPruned = trieLogPruner.pruneFromQueue(); // Then assertThat(wasPruned).isEqualTo(1); @@ -169,7 +169,7 @@ public void use_configured_retain_when_finalized_block_is_higher() { setupPrunerAndFinalizedBlock(configuredRetainHeight, finalizedBlockHeight); // When - final int wasPruned = trieLogPruner.pruneFromCache(); + final int wasPruned = trieLogPruner.pruneFromQueue(); // Then assertThat(wasPruned).isEqualTo(2); @@ -196,11 +196,11 @@ public void skip_pruning_when_finalized_block_required_but_not_present() { TrieLogPruner trieLogPruner = new TrieLogPruner(worldState, blockchain, blocksToRetain, pruningWindowSize, true); - trieLogPruner.cacheForLaterPruning(1, key(1)); - trieLogPruner.cacheForLaterPruning(2, key(2)); + trieLogPruner.addToPruneQueue(1, key(1)); + trieLogPruner.addToPruneQueue(2, key(2)); // When - final int wasPruned = trieLogPruner.pruneFromCache(); + final int wasPruned = trieLogPruner.pruneFromQueue(); // Then assertThat(wasPruned).isEqualTo(0); @@ -218,14 +218,14 @@ public void do_not_count_trieLog_when_prune_fails_first_attempt() { setupPrunerAndFinalizedBlock(configuredRetainHeight, finalizedBlockHeight); // When - final int wasPruned = trieLogPruner.pruneFromCache(); + final int wasPruned = trieLogPruner.pruneFromQueue(); // Then assertThat(wasPruned).isEqualTo(2); // Subsequent run should prune previously skipped trieLog when(worldState.pruneTrieLog(key(2))).thenReturn(true); - assertThat(trieLogPruner.pruneFromCache()).isEqualTo(1); + assertThat(trieLogPruner.pruneFromQueue()).isEqualTo(1); } private TrieLogPruner setupPrunerAndFinalizedBlock( @@ -243,11 +243,11 @@ private TrieLogPruner setupPrunerAndFinalizedBlock( TrieLogPruner trieLogPruner = new TrieLogPruner(worldState, blockchain, blocksToRetain, pruningWindowSize, true); - trieLogPruner.cacheForLaterPruning(1, key(1)); - trieLogPruner.cacheForLaterPruning(2, key(2)); - trieLogPruner.cacheForLaterPruning(3, key(3)); - trieLogPruner.cacheForLaterPruning(4, key(4)); - trieLogPruner.cacheForLaterPruning(5, key(5)); + trieLogPruner.addToPruneQueue(1, key(1)); + trieLogPruner.addToPruneQueue(2, key(2)); + trieLogPruner.addToPruneQueue(3, key(3)); + trieLogPruner.addToPruneQueue(4, key(4)); + trieLogPruner.addToPruneQueue(5, key(5)); return trieLogPruner; }