Repository navigation
indexer: Implement pruning mechanism - #1176
Conversation
Proper ticker used instead of sleep for pruning service job
Fixed failing test due to wrong check on blockstore base height
Moved pruner initialization into blockExecutor again.
| @@ -1,23 +1,96 @@ | |||
| package kv_test | |||
| package kv | |||
There was a problem hiding this comment.
| package kv | |
| package kv_test |
we cannot change package names.
| @@ -0,0 +1,5 @@ | |||
| package kv | |||
|
|
|||
| func GetKeys(indexer BlockerIndexer) [][]byte { | |||
There was a problem hiding this comment.
Is this a function only used for unit tests? If that is the case, it is currently also being included when building the cometbft binary. If you want it to only be present when building UT binaries (i.e., when running go test) so should, either rename this file to have suffix "_test.go", or move this code to one of such files.
There was a problem hiding this comment.
Ahh, this is a tough one. It is used outside kv and kv_test packages, namely in package state_test in state/pruner_test.go.
So renaming the file to "*_test.go" would make it invisible there, and it's not possible to move it outside kv package since it accesses private function getKeys.
There was a problem hiding this comment.
I see. Quite unfortunate, but I guess I can't think of any option better than what you've done.
| defer func(batch dbm.Batch) { | ||
| err := batch.Close() | ||
| if err != nil { | ||
| idx.log.Error(fmt.Sprintf("Error when closing pruning batch: %v", err)) |
There was a problem hiding this comment.
If you replaced the batch (at least once) in line 160, won't you be trying to close the same batch twice? If that is the case, I don't think you should be producing an error here... it'll be misleading to the operator.
Same comment in the other deferred function below
There was a problem hiding this comment.
From what I understand, batch.Close() is idempotent, meaning one can call it multiple times. So closing a closed batch would not lead to an error, though I agree that this is far from being clear.
I didn't come up with this approach myself, but rather I took from here
There was a problem hiding this comment.
I see, in that case, if you're sure that closing a closed batch returns nil, I'm OK with this approach.
| return 0, lastRetainHeight, err | ||
| } | ||
| batch = idx.store.NewBatch() | ||
| defer func(batch dbm.Batch) { |
There was a problem hiding this comment.
Minor: would it be worth defining another function, in the same way flush is defined, to contain code in lines 161-166, and 121-125? (this would remove code duplication)
| if len(remaining) == 0 && | ||
| blockHeightKeyPrefix == types.BlockHeightKey && | ||
| left <= possibleHeight && | ||
| possibleHeight < right { | ||
| return true | ||
| } | ||
| return false |
There was a problem hiding this comment.
| if len(remaining) == 0 && | |
| blockHeightKeyPrefix == types.BlockHeightKey && | |
| left <= possibleHeight && | |
| possibleHeight < right { | |
| return true | |
| } | |
| return false | |
| return len(remaining) == 0 && | |
| blockHeightKeyPrefix == types.BlockHeightKey && | |
| left <= possibleHeight && | |
| possibleHeight < right |
Just a suggestion, up to you
| lastRetainHeight = 1 | ||
| } | ||
|
|
||
| ctx := context.Background() |
There was a problem hiding this comment.
It would be good to add an issue to github to remember to thread this context up in the call chain.
There was a problem hiding this comment.
I am not sure that I understood what you mean well enough to make it myself
There was a problem hiding this comment.
Not a big deal... this will anyway have to be done as part of the "big" context threading task.
| defer func(batch dbm.Batch) { | ||
| err := batch.Close() | ||
| if err != nil { | ||
| txi.log.Error(fmt.Sprintf("Error when closing pruning batch: %v", err)) |
There was a problem hiding this comment.
Same comment on getting this error, even when everything went fine but we had more than 1000 pruned
| ctx := context.Background() | ||
| results, err := txi.Search(ctx, query.MustCompile( | ||
| fmt.Sprintf("tx.height < %d AND tx.height >= %d", retainHeight, lastRetainHeight))) | ||
| if err != nil { |
There was a problem hiding this comment.
Question to @jmalicevic. Are we sure we're not gonna hit any error in line 66? (it's a "search" query so I imagine a lot of code is gonna be hit there)
There was a problem hiding this comment.
If all goes well there should not be an error, just an empty result returned. An error should happen only in exceptional cases... The only thing I am not 100% sure of is whether we should panic here as this is not critical consensus code , so maybe it is better to log an error?
There was a problem hiding this comment.
IMO, better return an error, if possible
| numHeightsPersistentlyPruned = numHeightsBatchPruned | ||
| currentPersistentlyRetainedHeight = currentBatchRetainedHeight | ||
| batch = txi.store.NewBatch() | ||
| defer func(batch dbm.Batch) { |
There was a problem hiding this comment.
Same comment on unifying both (identical) defer functions into one
| flush := func(batch dbm.Batch) error { | ||
| err := batch.WriteSync() | ||
| if err != nil { | ||
| return fmt.Errorf("failed to flush pruning batch %w", err) |
There was a problem hiding this comment.
could we differentiate the text of logs done by block indexer and those done by tx indexer?
E.g. "failed to flush pruning batch %w" --> "failed to flush tx pruning batch %w"
| @@ -0,0 +1,2 @@ | |||
| - `[state/indexer/block]` BlockIndexer now has additional method `Prune(retainHeight int64) (int64, int64, error)` ([\#1176](https://github.com/cometbft/cometbft/pull/1176)) | |||
| - `[state/txindex]` TxIndexer now has additional method `Prune(retainHeight int64) (int64, int64, error)` ([\#1176](https://github.com/cometbft/cometbft/pull/1176)) No newline at end of file | |||
There was a problem hiding this comment.
Can you also mention the other to added methods? No need to mention their entire signature
We addressed this and the PR was reviewed afterwards again.
Closes #169
PR checklist
.changelog(we use unclog to manage our changelog)docs/orspec/) and code comments