From 6031e793ee957deea276de6edec8891f35081655 Mon Sep 17 00:00:00 2001 From: yihuang Date: Fri, 29 Nov 2024 20:29:02 +0800 Subject: [PATCH] Problem: prune cmd don't wait for async pruning to finish (#986) * Problem: prune cmd should disable async pruning * fix build * cleanup * fix mock * close db --- CHANGELOG.md | 1 + baseapp/options.go | 5 +++++ client/pruning/main.go | 4 ++++ server/mock/store.go | 4 ++++ server/start.go | 1 + server/util.go | 1 + store/iavl/store.go | 7 ++++++- store/rootmulti/store.go | 15 ++++++--------- store/types/store.go | 3 +++ 9 files changed, 31 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 15046e02b0b7..90136568636a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -73,6 +73,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ * (sims) [21906](https://github.com/cosmos/cosmos-sdk/pull/21906) Skip sims test when running dry on validators * (cli) [#21919](https://github.com/cosmos/cosmos-sdk/pull/21919) Query address-by-acc-num by account_id instead of id. +* (cli) [#22656](https://github.com/cosmos/cosmos-sdk/pull/22656) Prune cmd should disable async pruning. ## [v0.50.10](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.50.10) - 2024-09-20 diff --git a/baseapp/options.go b/baseapp/options.go index 9e066c94c1c4..d4d6052addd1 100644 --- a/baseapp/options.go +++ b/baseapp/options.go @@ -84,6 +84,11 @@ func SetIAVLDisableFastNode(disable bool) func(*BaseApp) { return func(bapp *BaseApp) { bapp.cms.SetIAVLDisableFastNode(disable) } } +// SetIAVLSyncPruning set sync/async pruning in the IAVL store. +func SetIAVLSyncPruning(syncPruning bool) func(*BaseApp) { + return func(bapp *BaseApp) { bapp.cms.SetIAVLSyncPruning(syncPruning) } +} + // SetInterBlockCache provides a BaseApp option function that sets the // inter-block cache. func SetInterBlockCache(cache storetypes.MultiStorePersistentCache) func(*BaseApp) { diff --git a/client/pruning/main.go b/client/pruning/main.go index 51dc5f9c2162..324deb674e82 100644 --- a/client/pruning/main.go +++ b/client/pruning/main.go @@ -44,6 +44,9 @@ Supported app-db-backend types include 'goleveldb', 'rocksdb', 'pebbledb'.`, return err } + // must disable async pruning + vp.Set(server.FlagIAVLSyncPruning, true) + // use the first argument if present to set the pruning method if len(args) > 0 { vp.Set(server.FlagPruning, args[0]) @@ -69,6 +72,7 @@ Supported app-db-backend types include 'goleveldb', 'rocksdb', 'pebbledb'.`, if err != nil { return err } + defer db.Close() logger := log.NewLogger(cmd.OutOrStdout()) app := appCreator(logger, db, nil, vp) diff --git a/server/mock/store.go b/server/mock/store.go index a73c57272f42..45ff433193a2 100644 --- a/server/mock/store.go +++ b/server/mock/store.go @@ -138,6 +138,10 @@ func (ms multiStore) SetIAVLDisableFastNode(disable bool) { panic("not implemented") } +func (ms multiStore) SetIAVLSyncPruning(syncPruning bool) { + panic("not implemented") +} + func (ms multiStore) SetInitialVersion(version int64) error { panic("not implemented") } diff --git a/server/start.go b/server/start.go index dbc575464669..57a25576ac67 100644 --- a/server/start.go +++ b/server/start.go @@ -74,6 +74,7 @@ const ( FlagMinRetainBlocks = "min-retain-blocks" FlagIAVLCacheSize = "iavl-cache-size" FlagDisableIAVLFastNode = "iavl-disable-fastnode" + FlagIAVLSyncPruning = "iavl-sync-pruning" FlagShutdownGrace = "shutdown-grace" // state sync-related flags diff --git a/server/util.go b/server/util.go index 521d13d8a508..09bb5c4a6c07 100644 --- a/server/util.go +++ b/server/util.go @@ -556,6 +556,7 @@ func DefaultBaseappOptions(appOpts types.AppOptions) []func(*baseapp.BaseApp) { baseapp.SetSnapshot(snapshotStore, snapshotOptions), baseapp.SetIAVLCacheSize(cast.ToInt(appOpts.Get(FlagIAVLCacheSize))), baseapp.SetIAVLDisableFastNode(cast.ToBool(appOpts.Get(FlagDisableIAVLFastNode))), + baseapp.SetIAVLSyncPruning(cast.ToBool(appOpts.Get(FlagIAVLSyncPruning))), defaultMempool, baseapp.SetChainID(chainID), baseapp.SetQueryGasLimit(cast.ToUint64(appOpts.Get(FlagQueryGasLimit))), diff --git a/store/iavl/store.go b/store/iavl/store.go index 7066891cda19..a37117026fa4 100644 --- a/store/iavl/store.go +++ b/store/iavl/store.go @@ -50,7 +50,12 @@ func LoadStore(db dbm.DB, logger log.Logger, key types.StoreKey, id types.Commit // 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, initialVersion uint64, cacheSize int, disableFastNode bool, metrics metrics.StoreMetrics) (types.CommitKVStore, error) { - tree := iavl.NewMutableTree(wrapper.NewDBWrapper(db), cacheSize, disableFastNode, logger, iavl.InitialVersionOption(initialVersion), iavl.AsyncPruningOption(true)) + return LoadStoreWithOpts(db, logger, key, id, initialVersion, cacheSize, disableFastNode, metrics, iavl.AsyncPruningOption(true)) +} + +func LoadStoreWithOpts(db dbm.DB, logger log.Logger, key types.StoreKey, id types.CommitID, initialVersion uint64, cacheSize int, disableFastNode bool, metrics metrics.StoreMetrics, opts ...iavl.Option) (types.CommitKVStore, error) { + opts = append(opts, iavl.InitialVersionOption(initialVersion)) + tree := iavl.NewMutableTree(wrapper.NewDBWrapper(db), cacheSize, disableFastNode, logger, opts...) isUpgradeable, err := tree.IsUpgradeable() if err != nil { diff --git a/store/rootmulti/store.go b/store/rootmulti/store.go index 540814b51415..64bf52b9b358 100644 --- a/store/rootmulti/store.go +++ b/store/rootmulti/store.go @@ -62,6 +62,7 @@ type Store struct { pruningManager *pruning.Manager iavlCacheSize int iavlDisableFastNode bool + iavlSyncPruning bool storesParams map[types.StoreKey]storeParams // CommitStore is a common interface to unify generic CommitKVStore of different value types stores map[types.StoreKey]types.CommitStore @@ -133,6 +134,10 @@ func (rs *Store) SetIAVLDisableFastNode(disableFastNode bool) { rs.iavlDisableFastNode = disableFastNode } +func (rs *Store) SetIAVLSyncPruning(syncPruning bool) { + rs.iavlSyncPruning = syncPruning +} + // GetStoreType implements Store. func (rs *Store) GetStoreType() types.StoreType { return types.StoreTypeMulti @@ -1047,15 +1052,7 @@ func (rs *Store) loadCommitStoreFromParams(key types.StoreKey, id types.CommitID panic("recursive MultiStores not yet supported") case types.StoreTypeIAVL: - var store types.CommitKVStore - var err error - - if params.initialVersion == 0 { - store, err = iavl.LoadStore(db, rs.logger, key, id, rs.iavlCacheSize, rs.iavlDisableFastNode, rs.metrics) - } else { - store, err = iavl.LoadStoreWithInitialVersion(db, rs.logger, key, id, params.initialVersion, rs.iavlCacheSize, rs.iavlDisableFastNode, rs.metrics) - } - + store, err := iavl.LoadStoreWithOpts(db, rs.logger, key, id, params.initialVersion, rs.iavlCacheSize, rs.iavlDisableFastNode, rs.metrics, iavltree.AsyncPruningOption(!rs.iavlSyncPruning)) if err != nil { return nil, err } diff --git a/store/types/store.go b/store/types/store.go index cd92fbd0ca07..23e54f253b3c 100644 --- a/store/types/store.go +++ b/store/types/store.go @@ -225,6 +225,9 @@ type CommitMultiStore interface { // SetIAVLDisableFastNode enables/disables fastnode feature on iavl. SetIAVLDisableFastNode(disable bool) + // SetIAVLSyncPruning set sync/async pruning on iavl. + SetIAVLSyncPruning(sync bool) + // RollbackToVersion rollback the db to specific version(height). RollbackToVersion(version int64) error