Skip to content

feat: Pruning and compaction port to 0.38#2

Merged
jmalicevic merged 5 commits intojasmina/pruning0.38from
jasmina/polygon/compaction_0.38
Aug 8, 2025
Merged

feat: Pruning and compaction port to 0.38#2
jmalicevic merged 5 commits intojasmina/pruning0.38from
jasmina/polygon/compaction_0.38

Conversation

@jmalicevic
Copy link

This is a manual back port of https://github.com/cometbft/cometbft/pull/2356/files#diff-fe3f53c9e61cbc76a2639c55769478a908fe350e272b241250b50489ebc38568 . I copied the changes to not include indexer related things as the indexer pruning is not existing yet in the target branch.


PR checklist

  • Tests written/updated
  • Changelog entry added in .changelog (we use unclog to manage our changelog)
  • Updated relevant documentation (docs/ or spec/) and code comments

@jmalicevic jmalicevic self-assigned this Aug 6, 2025
@jmalicevic jmalicevic requested a review from sergio-mena August 6, 2025 13:49
metrics *Metrics

// Preserve the number of state entries pruned.
// Used to calculated correctly when to trigger compactions

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Used to calculated correctly when to trigger compactions
// Used to calculate correctly when to trigger compactions

state/store.go Outdated

return nil
// We do not want to panic or interrupt consensus on compaction failure
if store.StoreOptions.Compact && previosulyPrunedStates+pruned >= uint64(store.StoreOptions.CompactionInterval) {
Copy link

@sergio-mena sergio-mena Aug 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there's something here that doesn't add up.

  • previosulyPrunedStates is passed as a parameter
  • There's only one callsite, and previosulyPrunedStates is assigned field prunedStates of the pruner
  • So, successive calls to this method (PruneStates) will correctly accumulate the number of pruned states over time
  • HOWEVER, when we reach CompactionInterval, and this if is hit, we will call compact, but field prunedStates of the pruner is not reset
  • So... once CompactionInterval is reached this IF will be hit every time... calling store.db.Compact(nil, nil) in every call to this method

... or am I missing something?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this has to be reset for this condition to work. And the condition for ABCI results should follow the logic of this. Will update this and ping you

state/store.go Outdated
}

if forceCompact && store.Compact {
if pruned+batchPruned >= store.CompactionInterval || targetRetainHeight-lastRetainHeight >= store.CompactionInterval {
Copy link

@sergio-mena sergio-mena Aug 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also have a problem with this condition. If I understood the code well

  • if condition pruned+batchPruned >= store.CompactionInterval is true,
  • then condition targetRetainHeight-lastRetainHeight is also true

... because pruned+batchPruned will increase by 1 at every iteration of the loop.
Since both conditions are combined with a logical OR, then the second condition is redundant.

Thinking more generally, why do we care about targetRetainHeight and lastRetainHeight here? Shouldn't we just care about the first condition, i.e., the actual number of ABCI responses pruned?

Copy link

@sergio-mena sergio-mena Aug 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another comment here, if we pruned some ABCIResponses but less than store.CompactionInterval, we won't be compacting, but:

  • How do we keep track of the cumulative number of ABCIResponses pruned but not compacted?
  • Imagine a node that calls PruneABCIResponses 1000 times, but each time it prunes less than store.CompactionInterval. After the 1000 calls, no compaction will be done IIUC -- is that what we want?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think targetRetainHeight-lastRetainHeight was there to guard against your second comment. But thinking about it, it is not actually doing that. It is simply indicating there was A change (that is also why the compaction will work because it will compact every time), but what we want is a cumulative diff. So this needs to be fixed.

state/store.go Outdated
Comment on lines +492 to +493
store.StoreOptions.ResultsToCompact += uint64(pruned + batchPruned)
if forceCompact && store.Compact && store.StoreOptions.ResultsToCompact >= (uint64)(store.StoreOptions.CompactionInterval) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would structure this differently. If store.Compact is false, we don't want store.StoreOptions.ResultsToCompact to be growing forever and eventually wrapping.
I would add an outer IF with checking store.Compact only. And, if true, then we update store.StoreOptions.ResultsToCompact and do the inner IF with the rest of the conditions.

state/pruner.go Outdated
prunedStates, err := p.stateStore.PruneStates(base, height, evRetainHeight, p.prunedStates)
p.prunedStates += prunedStates

p.prunedStates, err = p.stateStore.PruneStates(base, height, evRetainHeight, p.prunedStates)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is p.prunedStates now used for anything?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, the last parameter in PruneStates is unused within the function

@jmalicevic jmalicevic changed the title Jasmina/polygon/compaction 0.38 feat(store): Compaction for store pruning Aug 8, 2025
Copy link

@sergio-mena sergio-mena left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀🚀🚀

@jmalicevic jmalicevic changed the title feat(store): Compaction for store pruning feat: Pruning and compaction port to 0.38 Aug 8, 2025
@jmalicevic jmalicevic merged commit 5566c49 into jasmina/pruning0.38 Aug 8, 2025
12 of 15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants