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: add tool to migrate stores for fastnode #321

Merged
merged 3 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
94 changes: 94 additions & 0 deletions server/migrate_store.go
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
}
6 changes: 6 additions & 0 deletions server/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ func AddCommands(rootCmd *cobra.Command, defaultNodeHome string, appCreator type
ExportCmd(appExport, defaultNodeHome),
version.NewVersionCommand(),
NewRollbackCmd(appCreator, defaultNodeHome),
NewMigrateStoreCmd(appCreator, defaultNodeHome),
)
}

Expand Down Expand Up @@ -432,6 +433,11 @@ func openDB(rootDir string, backendType dbm.BackendType, opts ...*dbm.NewDatabas
return dbm.NewDB("application", backendType, dataDir, opts...)
}

func openDBWithDataDir(rootDir, subDir string, backendType dbm.BackendType, opts ...*dbm.NewDatabaseOption) (dbm.DB, error) {
dataDir := filepath.Join(rootDir, subDir)
return dbm.NewDB("application", backendType, dataDir, opts...)
}

func openTraceWriter(traceWriterFile string) (w io.WriteCloser, err error) {
if traceWriterFile == "" {
return
Expand Down
62 changes: 62 additions & 0 deletions store/rootmulti/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,37 @@ func (rs *Store) GetStoreType() types.StoreType {
return types.StoreTypeMulti
}

// MigrateStores will migrate stores to another type in another db.
func (rs *Store) MigrateStores(targetType types.StoreType, newDb dbm.DB) error {
if targetType != types.StoreTypeDB {
return errors.New("only StoreTypeDB is supported")
}

batch := newDb.NewBatch()
defer batch.Close()

for key, store := range rs.stores {
switch store.GetStoreType() {
case types.StoreTypeIAVL:
rs.logger.Info("Migrating IAVL store", "store name", key.Name())
iterator := store.Iterator(nil, nil)
for ; iterator.Valid(); iterator.Next() {
prefixKey := append([]byte("s/k:"+key.Name()+"/"), iterator.Key()...)
if err := batch.Set(prefixKey, iterator.Value()); err != nil {
return err
}
}
_ = iterator.Close()
default:

}
}
if err := batch.WriteSync(); err != nil {
Copy link
Collaborator

@j75689 j75689 Sep 27, 2023

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 do WriteSync in the loop

Copy link
Collaborator Author

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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

// WriteSync implements Batch.
func (b *goLevelDBBatch) WriteSync() error {
	return b.write(true)
}

func (b *goLevelDBBatch) write(sync bool) error {
	if b.batch == nil {
		return errBatchClosed
	}
	err := b.db.db.Write(b.batch, &opt.WriteOptions{Sync: sync})
	if err != nil {
		return err
	}
	// Make sure batch cannot be used afterwards. Callers should still call Close(), for errors.
	return b.Close()
}

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.

return err
}
return nil
}

// MountStoreWithDB implements CommitMultiStore.
func (rs *Store) MountStoreWithDB(key types.StoreKey, typ types.StoreType, db dbm.DB) {
if key == nil {
Expand Down Expand Up @@ -1249,3 +1280,34 @@ func flushLatestVersion(batch dbm.Batch, version int64) {

batch.Set([]byte(latestVersionKey), bz)
}

// MigrateCommitInfos will migrate commit infos to another db.
func MigrateCommitInfos(oldDb, newDb dbm.DB) (int64, error) {
bz, err := oldDb.Get([]byte(latestVersionKey))
if err != nil {
return 0, errors.New("fail to read the latest version")
}
if err = newDb.SetSync([]byte(latestVersionKey), bz); err != nil {
return 0, err
}

version := GetLatestVersion(oldDb)
bz, err = oldDb.Get([]byte(fmt.Sprintf(commitInfoKeyFmt, version)))
if bz == nil {
return 0, errors.New("fail to read the latest commit info")
}
if err != nil {
return 0, err
}
if err = newDb.SetSync([]byte(fmt.Sprintf(commitInfoKeyFmt, version)), bz); err != nil {
return 0, err
}

// ignore the errors for saving old commit info
bz, _ = oldDb.Get([]byte(fmt.Sprintf(commitInfoKeyFmt, version-1)))
if bz != nil {
_ = newDb.SetSync([]byte(fmt.Sprintf(commitInfoKeyFmt, version-1)), bz)
}

return version, nil
}
Loading