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

cleanup old blocks from cache in pruning routine #145

Merged
merged 1 commit into from
Oct 15, 2024
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
17 changes: 17 additions & 0 deletions indexer/beacon/blockcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,23 @@ func (cache *blockCache) getPruningBlocks(minInMemorySlot phase0.Slot) []*Block
return blocks
}

// getCleanupBlocks returns the blocks that can be cleaned up based on the given finalized slot.
func (cache *blockCache) getCleanupBlocks(finalizedSlot phase0.Slot) []*Block {
cache.cacheMutex.RLock()
defer cache.cacheMutex.RUnlock()

blocks := []*Block{}
for slot, slotBlocks := range cache.slotMap {
if slot >= finalizedSlot {
continue
}

blocks = append(blocks, slotBlocks...)
}

return blocks
}

// getForkBlocks returns a slice of blocks that belong to the specified forkId.
func (cache *blockCache) getForkBlocks(forkId ForkKey) []*Block {
cache.cacheMutex.RLock()
Expand Down
12 changes: 12 additions & 0 deletions indexer/beacon/pruning.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,13 @@ func (indexer *Indexer) processCachePruning(prunedEpochStats, prunedEpochStates
pruneBlock.block.block = nil
}

// remove all blocks in the finalized block range from the cache
finalizedSlot := chainState.EpochToSlot(indexer.lastFinalizedEpoch)
cleanupBlocks := indexer.blockCache.getCleanupBlocks(finalizedSlot)
for _, block := range cleanupBlocks {
indexer.blockCache.removeBlock(block)
}

// clean up epoch stats cache
for _, epochStats := range indexer.epochCache.getEpochStatsBeforeEpoch(minInMemoryEpoch) {
if epochStats.dependentState != nil {
Expand All @@ -379,6 +386,11 @@ func (indexer *Indexer) processCachePruning(prunedEpochStats, prunedEpochStates
epochStats.pruneValues()
prunedEpochStats++
}

if epochStats.epoch < indexer.lastFinalizedEpoch {
// remove from epoch stats cache
indexer.epochCache.removeEpochStats(epochStats)
}
}

prunedEpochStates += indexer.epochCache.removeUnreferencedEpochStates()
Expand Down
Loading