Skip to content

Commit 5566c49

Browse files
authored
Merge pull request #2 from informalsystems/jasmina/polygon/compaction_0.38
feat: Pruning and compaction port to 0.38
2 parents d407167 + ce73c51 commit 5566c49

9 files changed

Lines changed: 229 additions & 91 deletions

File tree

config/config.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,6 +1151,22 @@ type StorageConfig struct {
11511151
DiscardABCIResponses bool `mapstructure:"discard_abci_responses"`
11521152
// Configuration related to storage pruning.
11531153
Pruning *PruningConfig `mapstructure:"pruning"`
1154+
1155+
// Compaction on pruning - enable or disable in-process compaction.
1156+
// If the DB backend supports it, this will force the DB to compact
1157+
// the database levels and save on storage space. Setting this to true
1158+
// is most beneficial when used in combination with pruning as it will
1159+
// phyisically delete the entries marked for deletion.
1160+
// false by default (forcing compaction is disabled).
1161+
Compact bool `mapstructure:"compact"`
1162+
// Compaction interval - number of blocks to try explicit compaction on.
1163+
// This parameter should be tuned depending on the number of items
1164+
// you expect to delete between two calls to forced compaction.
1165+
// If your retain height is 1 block, it is too much of an overhead
1166+
// to try compaction every block. But it should also not be a very
1167+
// large multiple of your retain height as it might occur bigger overheads.
1168+
// 1000 by default.
1169+
CompactionInterval int64 `mapstructure:"compaction_interval"`
11541170
}
11551171

11561172
// DefaultStorageConfig returns the default configuration options relating to
@@ -1159,6 +1175,8 @@ func DefaultStorageConfig() *StorageConfig {
11591175
return &StorageConfig{
11601176
DiscardABCIResponses: false,
11611177
Pruning: DefaultPruningConfig(),
1178+
Compact: false,
1179+
CompactionInterval: 1000,
11621180
}
11631181
}
11641182

config/toml.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,18 @@ peer_query_maj23_sleep_duration = "{{ .Consensus.PeerQueryMaj23SleepDuration }}"
521521
# reindex events in the command-line tool.
522522
discard_abci_responses = {{ .Storage.DiscardABCIResponses}}
523523
524+
# If set to true, CometBFT will force compaction to happen for databases that support this feature.
525+
# and save on storage space. Setting this to true is most benefits when used in combination
526+
# with pruning as it will phyisically delete the entries marked for deletion.
527+
# false by default (forcing compaction is disabled).
528+
compact = false
529+
# To avoid forcing compaction every time, this parameter instructs CometBFT to wait
530+
# the given amount of blocks to be pruned before triggering compaction.
531+
# It should be tuned depending on the number of items. If your retain height is 1 block,
532+
# it is too much of an overhead to try compaction every block. But it should also not be a very
533+
# large multiple of your retain height as it might occur bigger overheads.
534+
compaction_interval = 1000
535+
524536
[storage.pruning]
525537
526538
# The time period between automated background pruning operations.

node/setup.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func initDBs(config *cfg.Config, dbProvider cfg.DBProvider) (blockStore *store.B
110110
if err != nil {
111111
return
112112
}
113-
blockStore = store.NewBlockStore(blockStoreDB)
113+
blockStore = store.NewBlockStore(blockStoreDB, store.WithCompaction(config.Storage.Compact, config.Storage.CompactionInterval))
114114

115115
stateDB, err = dbProvider(&cfg.DBContext{ID: "state", Config: config})
116116
if err != nil {

state/export_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func ValidateValidatorUpdates(abciUpdates []abci.ValidatorUpdate, params types.V
4141
// SaveValidatorsInfo is an alias for the private saveValidatorsInfo method in
4242
// store.go, exported exclusively and explicitly for testing.
4343
func SaveValidatorsInfo(db dbm.DB, height, lastHeightChanged int64, valSet *types.ValidatorSet) error {
44-
stateStore := dbStore{db, StoreOptions{DiscardABCIResponses: false}}
44+
stateStore := dbStore{db, StoreOptions{DiscardABCIResponses: false}, StoreStateKeeper{}}
4545
batch := stateStore.db.NewBatch()
4646
err := stateStore.saveValidatorsInfo(height, lastHeightChanged, valSet, batch)
4747
if err != nil {

state/mocks/store.go

Lines changed: 26 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

state/pruner.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ type Pruner struct {
3737
observer PrunerObserver
3838

3939
metrics *Metrics
40+
41+
// Preserve the number of state entries pruned.
42+
// Used to calculated correctly when to trigger compactions
43+
// TODO This is unused and should be removed from V1 and main as well.
44+
prunedStates uint64
4045
}
4146

4247
type prunerConfig struct {
@@ -303,10 +308,16 @@ func (p *Pruner) pruneABCIResToRetainHeight(lastRetainHeight int64) int64 {
303308
return lastRetainHeight
304309
}
305310

311+
// If the block retain height is 0, pruning of the block and state stores might be disabled
312+
// This should not prevent Comet from pruning ABCI results if needed.
313+
// We could by default always compact when pruning the responses, but in case the state store
314+
// is being compacted we introduce an overhead that might cause performance penalties.
315+
forceCompact := p.findMinBlockRetainHeight() == 0
316+
306317
// newRetainHeight is the height just after that which we have successfully
307318
// pruned. In case of an error it will be 0, but then it will also be
308319
// ignored.
309-
numPruned, newRetainHeight, err := p.stateStore.PruneABCIResponses(targetRetainHeight)
320+
numPruned, newRetainHeight, err := p.stateStore.PruneABCIResponses(targetRetainHeight, forceCompact)
310321
if err != nil {
311322
p.logger.Error("Failed to prune ABCI responses", "err", err, "targetRetainHeight", targetRetainHeight)
312323
return lastRetainHeight
@@ -357,8 +368,13 @@ func (p *Pruner) pruneBlocksToHeight(height int64) (uint64, int64, error) {
357368
if err != nil {
358369
return 0, 0, ErrFailedToPruneBlocks{Height: height, Err: err}
359370
}
360-
if err := p.stateStore.PruneStates(base, height, evRetainHeight); err != nil {
361-
return 0, 0, ErrFailedToPruneStates{Height: height, Err: err}
371+
372+
if pruned > 0 {
373+
374+
_, err = p.stateStore.PruneStates(base, height, evRetainHeight)
375+
if err != nil {
376+
return 0, 0, ErrFailedToPruneStates{Height: height, Err: err}
377+
}
362378
}
363379
return pruned, evRetainHeight, err
364380
}

0 commit comments

Comments
 (0)