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

feat/parallel pruning #447

Merged
merged 5 commits into from
Jun 12, 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
7 changes: 7 additions & 0 deletions baseapp/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -444,8 +444,15 @@ func (app *BaseApp) Commit() abci.ResponseCommit {
// Write the DeliverTx state into branched storage and commit the MultiStore.
// The write to the DeliverTx state writes all state transitions to the root
// MultiStore (app.cms) so when Commit() is called is persists those values.
startWriteTime := time.Now()
app.deliverState.ms.Write()
app.logger.Debug("ms.Write", "height", header.Height, "cost of time",
fmt.Sprintf(" %.2fms", float64(time.Since(startWriteTime).Microseconds())/1000))

startCommitTime := time.Now()
commitID := app.cms.Commit()
app.logger.Debug("cms.Commit", "height", header.Height, "cost of time",
fmt.Sprintf(" %.2fms", float64(time.Since(startCommitTime).Microseconds())/1000))

res := abci.ResponseCommit{
Data: commitID.Hash,
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,5 @@ retract (
// do not use
v0.43.0
)

replace github.com/cosmos/iavl => ../iavl
2 changes: 1 addition & 1 deletion store/iavl/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"github.com/cometbft/cometbft/libs/log"
tmcrypto "github.com/cometbft/cometbft/proto/tendermint/crypto"
ics23 "github.com/confio/ics23/go"
"github.com/cosmos/iavl"

Check failure on line 14 in store/iavl/store.go

View workflow job for this annotation

GitHub Actions / tests (00)

github.com/cosmos/[email protected]: replacement directory ../iavl does not exist

Check failure on line 14 in store/iavl/store.go

View workflow job for this annotation

GitHub Actions / tests (01)

github.com/cosmos/[email protected]: replacement directory ../iavl does not exist

Check failure on line 14 in store/iavl/store.go

View workflow job for this annotation

GitHub Actions / tests (03)

github.com/cosmos/[email protected]: replacement directory ../iavl does not exist

Check failure on line 14 in store/iavl/store.go

View workflow job for this annotation

GitHub Actions / tests (02)

github.com/cosmos/[email protected]: replacement directory ../iavl does not exist

Check failure on line 14 in store/iavl/store.go

View workflow job for this annotation

GitHub Actions / tests (00)

github.com/cosmos/[email protected]: replacement directory ../iavl does not exist

Check failure on line 14 in store/iavl/store.go

View workflow job for this annotation

GitHub Actions / tests (01)

github.com/cosmos/[email protected]: replacement directory ../iavl does not exist

Check failure on line 14 in store/iavl/store.go

View workflow job for this annotation

GitHub Actions / tests (02)

github.com/cosmos/[email protected]: replacement directory ../iavl does not exist

Check failure on line 14 in store/iavl/store.go

View workflow job for this annotation

GitHub Actions / tests (03)

github.com/cosmos/[email protected]: replacement directory ../iavl does not exist

"github.com/cosmos/cosmos-sdk/store/cachekv"
pruningtypes "github.com/cosmos/cosmos-sdk/store/pruning/types"
Expand Down Expand Up @@ -52,7 +52,7 @@
// provided DB. An error is returned if the version fails to load, or if called with a positive
// version on an empty tree.
func LoadStoreWithInitialVersion(db dbm.DB, logger log.Logger, key types.StoreKey, id types.CommitID, lazyLoading bool, initialVersion uint64, cacheSize int, disableFastNode bool) (types.CommitKVStore, error) {
tree, err := iavl.NewMutableTreeWithOpts(db, cacheSize, &iavl.Options{InitialVersion: initialVersion}, disableFastNode)
tree, err := iavl.NewMutableTreeWithOpts(db, cacheSize, &iavl.Options{InitialVersion: initialVersion}, disableFastNode, key.Name())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -120,7 +120,7 @@
}

return &Store{
tree: &immutableTree{iTree},

Check failure on line 123 in store/iavl/store.go

View workflow job for this annotation

GitHub Actions / golangci-lint

cannot use &(immutableTree literal) (value of type *immutableTree) as Tree value in struct literal: *immutableTree does not implement Tree (missing method Get) (typecheck)

Check failure on line 123 in store/iavl/store.go

View workflow job for this annotation

GitHub Actions / golangci-lint

cannot use &(immutableTree literal) (value of type *immutableTree) as Tree value in struct literal: *immutableTree does not implement Tree (missing method Get) (typecheck)
}, nil
}

Expand Down Expand Up @@ -274,11 +274,11 @@
if err != nil {
return nil, fmt.Errorf("iavl export failed for version %v: %w", version, err)
}
tree, ok := istore.tree.(*immutableTree)

Check failure on line 277 in store/iavl/store.go

View workflow job for this annotation

GitHub Actions / golangci-lint

impossible type assertion: istore.tree.(*immutableTree)

Check failure on line 277 in store/iavl/store.go

View workflow job for this annotation

GitHub Actions / golangci-lint

impossible type assertion: istore.tree.(*immutableTree)
if !ok || tree == nil {
return nil, fmt.Errorf("iavl export failed: unable to fetch tree for version %v", version)
}
return tree.Export()

Check failure on line 281 in store/iavl/store.go

View workflow job for this annotation

GitHub Actions / golangci-lint

tree.Export undefined (type *immutableTree has no field or method Export) (typecheck)

Check failure on line 281 in store/iavl/store.go

View workflow job for this annotation

GitHub Actions / golangci-lint

tree.Export undefined (type *immutableTree has no field or method Export) (typecheck)
}

// Import imports an IAVL tree at the given version, returning an iavl.Importer for importing.
Expand Down
48 changes: 40 additions & 8 deletions store/rootmulti/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"sort"
"strings"
"sync"
"time"

dbm "github.com/cometbft/cometbft-db"
abci "github.com/cometbft/cometbft/abci/types"
Expand Down Expand Up @@ -627,6 +628,9 @@ func (rs *Store) PruneStores(clearPruningManager bool, pruningHeights []int64) (

rs.logger.Debug("pruning store", "heights", pruningHeights)

startPruneTime := time.Now()
var wg sync.WaitGroup
errCh := make(chan error)
for key, store := range rs.stores {
rs.logger.Debug("pruning store", "key", key) // Also log store.name (a private variable)?

Expand All @@ -638,16 +642,44 @@ func (rs *Store) PruneStores(clearPruningManager bool, pruningHeights []int64) (

store = rs.GetCommitKVStore(key)

err := store.(*iavl.Store).DeleteVersions(pruningHeights...)
if err == nil {
continue
}
wg.Add(1)
go func(key types.StoreKey, store types.CommitStore) {
rs.logger.Debug("DeleteVersions", "store", key.Name())
startDeleteTime := time.Now()
err := store.(*iavl.Store).DeleteVersions(pruningHeights...)
if err == nil {
rs.logger.Debug("DeleteVersions", "store", key.Name(), "cost of time",
fmt.Sprintf("%.2fms", float64(time.Since(startDeleteTime).Microseconds())/1000))
wg.Done()
return
}

if errCause := errors.Cause(err); errCause != nil && errCause != iavltree.ErrVersionDoesNotExist {
return err
}
if errCause := errors.Cause(err); errCause != nil && errCause != iavltree.ErrVersionDoesNotExist {
errCh <- err
return
}
wg.Done()
}(key, store)
}

done := make(chan struct{})
go func() {
wg.Wait()
done <- struct{}{}
}()

select {
case <-done:
rs.logger.Debug(
"PruneStores",
"from", pruningHeights[0],
"to", pruningHeights[len(pruningHeights)-1],
"cost of time", fmt.Sprintf("%.2fms", float64(time.Since(startPruneTime).Microseconds())/1000),
)
return nil
case err := <-errCh:
return err
}
return nil
}

// getStoreByName performs a lookup of a StoreKey given a store name typically
Expand Down
Loading