Skip to content

Commit

Permalink
add dryrun mode
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuang committed Nov 12, 2024
1 parent cd306a4 commit 95d74ce
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 7 deletions.
24 changes: 22 additions & 2 deletions versiondb/client/fixdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,47 @@ package client

import (
"github.com/crypto-org-chain/cronos/versiondb/tsrocksdb"
"github.com/linxGnu/grocksdb"
"github.com/spf13/cobra"
)

const FlagDryRun = "dry-run"

func FixDataCmd(stores []string) *cobra.Command {
cmd := &cobra.Command{
Use: "fixdata <dir>",
Args: cobra.ExactArgs(1),
Short: "Fix wrong data in versiondb, see: https://github.com/crypto-org-chain/cronos/issues/1683",
RunE: func(cmd *cobra.Command, args []string) error {
dir := args[0]
versionDB, err := tsrocksdb.NewStore(dir)
dryRun, err := cmd.Flags().GetBool(FlagDryRun)
if err != nil {
return err
}

var (
db *grocksdb.DB
cfHandle *grocksdb.ColumnFamilyHandle
)

if dryRun {
db, cfHandle, err = tsrocksdb.OpenVersionDBForReadOnly(dir, false)
} else {
db, cfHandle, err = tsrocksdb.OpenVersionDB(dir)
}
if err != nil {
return err
}

if err := versionDB.FixData(stores); err != nil {
versionDB := tsrocksdb.NewStoreWithDB(db, cfHandle)
if err := versionDB.FixData(stores, dryRun); err != nil {
return err
}

return nil
},
}

cmd.Flags().Bool(FlagDryRun, false, "Dry run, do not write to the database, open the database in read-only mode.")
return cmd
}
16 changes: 16 additions & 0 deletions versiondb/tsrocksdb/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,22 @@ func OpenVersionDB(dir string) (*grocksdb.DB, *grocksdb.ColumnFamilyHandle, erro
return db, cfHandles[1], nil
}

// OpenVersionDBForReadOnly open versiondb in readonly mode
func OpenVersionDBForReadOnly(dir string, errorIfWalFileExists bool) (*grocksdb.DB, *grocksdb.ColumnFamilyHandle, error) {
opts := grocksdb.NewDefaultOptions()
opts.SetCreateIfMissing(true)
opts.SetCreateIfMissingColumnFamilies(true)
db, cfHandles, err := grocksdb.OpenDbForReadOnlyColumnFamilies(
opts, dir, []string{"default", VersionDBCFName},
[]*grocksdb.Options{opts, NewVersionDBOpts(false)},
errorIfWalFileExists,
)
if err != nil {
return nil, nil, err
}
return db, cfHandles[1], nil
}

// OpenVersionDBAndTrimHistory opens versiondb similar to `OpenVersionDB`,
// but it also trim the versions newer than target one, can be used for rollback.
func OpenVersionDBAndTrimHistory(dir string, version int64) (*grocksdb.DB, *grocksdb.ColumnFamilyHandle, error) {
Expand Down
18 changes: 13 additions & 5 deletions versiondb/tsrocksdb/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,9 @@ func (s Store) Flush() error {
// FixData fixes wrong data written in versiondb due to rocksdb upgrade, the operation is idempotent.
// see: https://github.com/crypto-org-chain/cronos/issues/1683
// call this before `SetSkipVersionZero(true)`.
func (s Store) FixData(storeNames []string) error {
func (s Store) FixData(storeNames []string, dryRun bool) error {
for _, storeName := range storeNames {
if err := s.fixDataStore(storeName); err != nil {
if err := s.fixDataStore(storeName, dryRun); err != nil {
return err
}
}
Expand All @@ -246,7 +246,7 @@ func (s Store) FixData(storeNames []string) error {
}

// fixDataStore iterate the wrong data at version 0, parse the timestamp from the key and write it again.
func (s Store) fixDataStore(storeName string) error {
func (s Store) fixDataStore(storeName string, dryRun bool) error {
var version int64
iter, err := s.IteratorAtVersion(storeName, nil, nil, &version)
if err != nil {
Expand Down Expand Up @@ -283,10 +283,18 @@ func (s Store) fixDataStore(storeName string) error {
continue
}

batch.PutCFWithTS(s.cfHandle, realKey, ts, iter.Value())
if dryRun {
fmt.Printf("fix data: %s, key: %X, ts: %X\n", storeName, key, ts)
} else {
batch.PutCFWithTS(s.cfHandle, realKey, ts, iter.Value())
}
}

return s.db.Write(defaultSyncWriteOpts, batch)
if !dryRun {
return s.db.Write(defaultSyncWriteOpts, batch)
}

return nil
}

func newTSReadOptions(version *int64) *grocksdb.ReadOptions {
Expand Down

0 comments on commit 95d74ce

Please sign in to comment.