Skip to content

indexer: Implement pruning mechanism - #1176

Merged
werty144 merged 150 commits into
feature/adr101-pull-companionfrom
anton/tx_index-pruning
Sep 8, 2023
Merged

werty144 merged 150 commits into
feature/adr101-pull-companionfrom
anton/tx_index-pruning

Conversation

@werty144

@werty144 werty144 commented Jul 28, 2023 •

Copy link
Copy Markdown
Contributor

Closes #169

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

Comment thread state/indexer/block/kv/kv_test.go Outdated
@@ -1,23 +1,96 @@
package kv_test
package kv

ghost Sep 6, 2023

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
package kv
package kv_test

we cannot change package names.

Comment thread state/indexer/block/kv/util.go Resolved
Comment thread state/indexer/block/kv/kv_test.go Resolved

ghost left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thank you Anton, great job!

Comment thread state/indexer/block.go Resolved
@@ -0,0 +1,5 @@
package kv

func GetKeys(indexer BlockerIndexer) [][]byte {

ghost Sep 7, 2023

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.

ghost Sep 7, 2023

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

ghost Sep 7, 2023 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I see. Quite unfortunate, but I guess I can't think of any option better than what you've done.

Comment thread state/indexer/block/kv/kv.go Outdated
defer func(batch dbm.Batch) {
err := batch.Close()
if err != nil {
idx.log.Error(fmt.Sprintf("Error when closing pruning batch: %v", err))

ghost Sep 7, 2023

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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

ghost Sep 7, 2023

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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

ghost Sep 7, 2023

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I see, in that case, if you're sure that closing a closed batch returns nil, I'm OK with this approach.

Comment thread state/indexer/block/kv/kv.go Outdated
return 0, lastRetainHeight, err
}
batch = idx.store.NewBatch()
defer func(batch dbm.Batch) {

ghost Sep 7, 2023

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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)

ghost Sep 7, 2023

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

Comment thread state/indexer/block/kv/util.go Outdated
Comment on lines +69 to +75
if len(remaining) == 0 &&
blockHeightKeyPrefix == types.BlockHeightKey &&
left <= possibleHeight &&
possibleHeight < right {
return true
}
return false

ghost Sep 7, 2023

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
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

ghost Sep 7, 2023

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

Comment thread state/txindex/kv/kv.go
lastRetainHeight = 1
}

ctx := context.Background()

ghost Sep 7, 2023

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

It would be good to add an issue to github to remember to thread this context up in the call chain.

ghost Sep 7, 2023

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I am not sure that I understood what you mean well enough to make it myself

ghost Sep 7, 2023

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Not a big deal... this will anyway have to be done as part of the "big" context threading task.

Comment thread state/txindex/kv/kv.go Outdated
defer func(batch dbm.Batch) {
err := batch.Close()
if err != nil {
txi.log.Error(fmt.Sprintf("Error when closing pruning batch: %v", err))

ghost Sep 7, 2023

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Same comment on getting this error, even when everything went fine but we had more than 1000 pruned

ghost Sep 7, 2023

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

Comment thread state/txindex/kv/kv.go
ctx := context.Background()
results, err := txi.Search(ctx, query.MustCompile(
fmt.Sprintf("tx.height < %d AND tx.height >= %d", retainHeight, lastRetainHeight)))
if err != nil {

ghost Sep 7, 2023

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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)

ghost Sep 8, 2023

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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?

ghost Sep 8, 2023

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

IMO, better return an error, if possible

ghost Sep 8, 2023

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Yup, its fixed now.

Comment thread state/txindex/kv/kv.go Outdated
numHeightsPersistentlyPruned = numHeightsBatchPruned
currentPersistentlyRetainedHeight = currentBatchRetainedHeight
batch = txi.store.NewBatch()
defer func(batch dbm.Batch) {

ghost Sep 7, 2023

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Same comment on unifying both (identical) defer functions into one

ghost Sep 7, 2023

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

Comment thread state/txindex/kv/kv.go Outdated
flush := func(batch dbm.Batch) error {
err := batch.WriteSync()
if err != nil {
return fmt.Errorf("failed to flush pruning batch %w", err)

ghost Sep 7, 2023 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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"

ghost Sep 7, 2023

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

@@ -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

ghost Sep 7, 2023

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can you also mention the other to added methods? No need to mention their entire signature

ghost Sep 7, 2023

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done

ghost left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🙏

@jmalicevic
jmalicevic dismissed mzabaluev’s stale review September 8, 2023 12:37

We addressed this and the PR was reviewed afterwards again.

@werty144
werty144 merged commit 96f4ab8 into feature/adr101-pull-companion Sep 8, 2023
@werty144
werty144 deleted the anton/tx_index-pruning branch September 8, 2023 12:43
@jmalicevic jmalicevic mentioned this pull request Sep 19, 2023
2 of 5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P:storage-optimization Priority: Give operators greater control over storage and storage optimization storage

Projects

No open projects
Status: Done

Development

Successfully merging this pull request may close these issues.

7 participants