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(store/v2): build the migration manager in the root store factory #22336

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
51 changes: 46 additions & 5 deletions store/v2/root/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import (
"cosmossdk.io/store/v2/commitment/mem"
"cosmossdk.io/store/v2/db"
"cosmossdk.io/store/v2/internal"
"cosmossdk.io/store/v2/migration"
"cosmossdk.io/store/v2/pruning"
"cosmossdk.io/store/v2/snapshots"
"cosmossdk.io/store/v2/storage"
"cosmossdk.io/store/v2/storage/pebbledb"
"cosmossdk.io/store/v2/storage/rocksdb"
Expand Down Expand Up @@ -139,11 +141,11 @@ func CreateRootStore(opts *FactoryOptions) (store.RootStore, error) {
return nil, err
}

newTreeFn := func(key string) (commitment.Tree, error) {
newTreeFn := func(key string, scType SCType) (commitment.Tree, error) {
if internal.IsMemoryStoreKey(key) {
return mem.New(), nil
} else {
switch storeOpts.SCType {
switch scType {
case SCTypeIavl:
return iavl.NewIavlTree(db.NewPrefixDB(opts.SCRawDB, []byte(key)), opts.Logger, storeOpts.IavlConfig), nil
case SCTypeIavlV2:
Expand All @@ -154,17 +156,31 @@ func CreateRootStore(opts *FactoryOptions) (store.RootStore, error) {
}
}

// check if we need to migrate the store
isMigrating := false
scType := storeOpts.SCType
ssLatestVersion, err := ss.GetLatestVersion()
if err != nil {
return nil, err
}
if ssLatestVersion != latestVersion {
isMigrating = true // need to migrate
if scType != SCTypeIavl {
scType = SCTypeIavl // only support iavl v1 for migration
}
}
Comment on lines +161 to +173
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Simplify isMigrating assignment

You can assign the result of the condition directly to isMigrating to make the code more concise.

Apply this diff to streamline the variable initialization:

-isMigrating := false
...
-if ssLatestVersion != latestVersion {
-    isMigrating = true // need to migrate
+isMigrating := ssLatestVersion != latestVersion
 if isMigrating {
     if scType != SCTypeIavl {
         scType = SCTypeIavl // only support iavl v1 for migration
     }
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// check if we need to migrate the store
isMigrating := false
scType := storeOpts.SCType
ssLatestVersion, err := ss.GetLatestVersion()
if err != nil {
return nil, err
}
if ssLatestVersion != latestVersion {
isMigrating = true // need to migrate
if scType != SCTypeIavl {
scType = SCTypeIavl // only support iavl v1 for migration
}
}
// check if we need to migrate the store
scType := storeOpts.SCType
ssLatestVersion, err := ss.GetLatestVersion()
if err != nil {
return nil, err
}
isMigrating := ssLatestVersion != latestVersion
if isMigrating {
if scType != SCTypeIavl {
scType = SCTypeIavl // only support iavl v1 for migration
}
}


trees := make(map[string]commitment.Tree, len(opts.StoreKeys))
for _, key := range opts.StoreKeys {
tree, err := newTreeFn(key)
tree, err := newTreeFn(key, scType)
if err != nil {
return nil, err
}
trees[key] = tree
}
oldTrees := make(map[string]commitment.Tree, len(opts.StoreKeys))
for _, key := range removedStoreKeys {
tree, err := newTreeFn(string(key))
tree, err := newTreeFn(string(key), scType)
if err != nil {
return nil, err
}
Expand All @@ -176,6 +192,31 @@ func CreateRootStore(opts *FactoryOptions) (store.RootStore, error) {
return nil, err
}

var mm *migration.Manager
if isMigrating {
snapshotDB, err := snapshots.NewStore(fmt.Sprintf("%s/data/snapshots/store.db", opts.RootDir))
if err != nil {
return nil, err
}
Comment on lines +210 to +213
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Add cleanup for snapshot DB on error.

The snapshot DB creation should be cleaned up if subsequent operations fail to prevent orphaned files.

 snapshotDB, err := snapshots.NewStore(fmt.Sprintf("%s/data/snapshots/store.db", opts.RootDir))
 if err != nil {
+    // Clean up any partially created files
+    _ = os.RemoveAll(fmt.Sprintf("%s/data/snapshots", opts.RootDir))
     return nil, err
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
snapshotDB, err := snapshots.NewStore(fmt.Sprintf("%s/data/snapshots/store.db", opts.RootDir))
if err != nil {
return nil, err
}
snapshotDB, err := snapshots.NewStore(fmt.Sprintf("%s/data/snapshots/store.db", opts.RootDir))
if err != nil {
// Clean up any partially created files
_ = os.RemoveAll(fmt.Sprintf("%s/data/snapshots", opts.RootDir))
return nil, err
}

snapshotMgr := snapshots.NewManager(snapshotDB, snapshots.SnapshotOptions{}, sc, nil, nil, opts.Logger)
var newSC *commitment.CommitStore
if scType != storeOpts.SCType {
newTrees := make(map[string]commitment.Tree, len(opts.StoreKeys))
for _, key := range opts.StoreKeys {
tree, err := newTreeFn(key, storeOpts.SCType)
if err != nil {
return nil, err
}
newTrees[key] = tree
}
newSC, err = commitment.NewCommitStore(newTrees, nil, opts.SCRawDB, opts.Logger)
if err != nil {
return nil, err
}
}
mm = migration.NewManager(opts.SCRawDB, snapshotMgr, ss, newSC, opts.Logger)
}
Comment on lines +208 to +231
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consider refactoring migration setup for better maintainability.

The migration setup logic is complex and handles multiple concerns. Consider extracting it into a separate function for better maintainability and testing.

func setupMigrationManager(opts *FactoryOptions, ss *storage.StorageStore, sc *commitment.CommitStore, scType SCType) (*migration.Manager, error) {
    snapshotDB, err := snapshots.NewStore(fmt.Sprintf("%s/data/snapshots/store.db", opts.RootDir))
    if err != nil {
        return nil, err
    }
    
    snapshotMgr := snapshots.NewManager(snapshotDB, snapshots.SnapshotOptions{}, sc, nil, nil, opts.Logger)
    
    var newSC *commitment.CommitStore
    if scType != opts.Options.SCType {
        newSC, err = createNewCommitStore(opts, scType)
        if err != nil {
            snapshotDB.Close() // Cleanup on error
            return nil, err
        }
    }
    
    return migration.NewManager(opts.SCRawDB, snapshotMgr, ss, newSC, opts.Logger), nil
}


pm := pruning.NewManager(sc, ss, storeOpts.SCPruningOption, storeOpts.SSPruningOption)
return New(opts.SCRawDB, opts.Logger, ss, sc, pm, nil, nil)
return New(opts.SCRawDB, opts.Logger, ss, sc, pm, mm, nil)
}
Loading