Skip to content

Commit

Permalink
Rename cache to queue
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Dudley <[email protected]>
  • Loading branch information
siladu committed Nov 15, 2023
1 parent 9ba8bed commit 0e9f9e9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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<Hash> finalized = blockchain.getFinalized();
if (requireFinalizedBlock && finalized.isEmpty()) {
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand All @@ -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));
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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(
Expand All @@ -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;
}
Expand Down

0 comments on commit 0e9f9e9

Please sign in to comment.