-
Notifications
You must be signed in to change notification settings - Fork 24
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: add tool to migrate stores for fastnode #321
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package server | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/cometbft/cometbft/node" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/cosmos/cosmos-sdk/client/flags" | ||
serverconfig "github.com/cosmos/cosmos-sdk/server/config" | ||
"github.com/cosmos/cosmos-sdk/server/types" | ||
"github.com/cosmos/cosmos-sdk/store/rootmulti" | ||
storetypes "github.com/cosmos/cosmos-sdk/store/types" | ||
) | ||
|
||
// tmpMigratingDir is a temporary directory to facilitate the migration. | ||
const tmpMigratingDir = "data-migrating" | ||
|
||
// NewMigrateStoreCmd creates a command to migrate multistore from IAVL stores to plain DB stores to enable fast node. | ||
func NewMigrateStoreCmd(appCreator types.AppCreator, defaultNodeHome string) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "migrate-store", | ||
Short: "migrate application db to use plain db stores instead of IAVL stores", | ||
Long: ` | ||
To run a fast node, plain DB store type is needed. To convert a normal full node to a fast full node, | ||
we need to migrate the underlying stores. With this command, the old application db will be backed up, | ||
the new application db will use plain DB store types. | ||
`, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
ctx := GetServerContextFromCmd(cmd) | ||
cfg := ctx.Config | ||
home := cfg.RootDir | ||
db, err := openDB(home, GetAppDBBackend(ctx.Viper)) | ||
if err != nil { | ||
return err | ||
} | ||
newDb, err := openDBWithDataDir(home, tmpMigratingDir, GetAppDBBackend(ctx.Viper)) | ||
if err != nil { | ||
return err | ||
} | ||
config, err := serverconfig.GetConfig(ctx.Viper) | ||
if err != nil { | ||
return err | ||
} | ||
genDocProvider := node.DefaultGenesisDocProviderFunc(ctx.Config) | ||
genDoc, err := genDocProvider() | ||
if err != nil { | ||
return err | ||
} | ||
app := appCreator(ctx.Logger, db, nil, genDoc.ChainID, &config, ctx.Viper) | ||
|
||
if err = app.CommitMultiStore().LoadLatestVersion(); err != nil { | ||
return err | ||
} | ||
rs, ok := app.CommitMultiStore().(*rootmulti.Store) | ||
if !ok { | ||
return errors.New("cannot convert store to root multi store") | ||
} | ||
|
||
if err = rs.MigrateStores(storetypes.StoreTypeDB, newDb); err != nil { | ||
return err | ||
} | ||
version, err := rootmulti.MigrateCommitInfos(db, newDb) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Printf("Multi root store is dumped at version %d \n", version) | ||
|
||
_ = db.Close() | ||
_ = newDb.Close() | ||
|
||
applicationPath := fmt.Sprintf("%s%c%s%c%s", home, os.PathSeparator, "data", os.PathSeparator, "application.db") | ||
applicationBackupPath := fmt.Sprintf("%s%c%s%c%s", home, os.PathSeparator, "data", os.PathSeparator, "application.db-backup") | ||
applicationMigratePath := fmt.Sprintf("%s%c%s%c%s", home, os.PathSeparator, tmpMigratingDir, os.PathSeparator, "application.db") | ||
if err = os.Rename(applicationPath, applicationBackupPath); err != nil { | ||
return err | ||
} | ||
if err = os.Rename(applicationMigratePath, applicationPath); err != nil { | ||
return err | ||
} | ||
fmt.Printf("Database is replaced and the old one is backup %s\n", applicationBackupPath) | ||
|
||
_ = os.Remove(applicationMigratePath) | ||
fmt.Printf("Migrate database done, please update app.toml and config.toml to use fastnode feature") | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
cmd.Flags().String(flags.FlagHome, defaultNodeHome, "The application home directory") | ||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are risks with OOM I think, maybe can consider adding a
BatchLimit
and doWriteSync
in the loopThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree. I had tried to call
WriteSync
for batches. However, there are some issues, I will figure out it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The batch implementation is wired. After
write
it will be closed.At the same time,
Batch
is rarely used in the project. I will remove the use of batch as well.