Skip to content

Commit

Permalink
Problem: no command to fix corrupted data in versiondb (backport: #1685
Browse files Browse the repository at this point in the history
…) (#1686)

* Problem: nixpkgs-fmt is deprecated (backport: #1677)

Solution:
- switch to nixfmt-rfc-style

* Problem: no command to fix corrupted data in versiondb (backport: #1685)

Closes: #1683

Solution:
- add fix command to fix corrupted data in versiondb

rename

support SkipVersionZero

support SkipVersionZero

cleanup

cleanup

cleanup

fix test

cleanup

destroy

log store name

fix data manually

cli

Update versiondb/client/fixdata.go

Signed-off-by: yihuang <[email protected]>

Update versiondb/client/fixdata.go

Signed-off-by: yihuang <[email protected]>

rnemae

Update CHANGELOG.md

Signed-off-by: yihuang <[email protected]>

fix test

don't return nil as empty slice

add dryrun mode

separete read from iteration

add stores flag

debug

add timestamp api

* update rocksdb

* changelog

* fix flag name conflict

* validate timestamp

* Update CHANGELOG.md

Signed-off-by: yihuang <[email protected]>

* skip non-zero version

* flush after fix

* only flush if not dry-run

---------

Signed-off-by: yihuang <[email protected]>
  • Loading branch information
yihuang authored Nov 12, 2024
1 parent 1f20b38 commit 93af817
Show file tree
Hide file tree
Showing 15 changed files with 346 additions and 36 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

* [#1670](https://github.com/crypto-org-chain/cronos/pull/1670) Fix state overwrite in debug trace APIs.
* [#1679](https://github.com/crypto-org-chain/cronos/pull/1679) Include no trace detail on insufficient balance fix.
* [#1685](https://github.com/crypto-org-chain/cronos/pull/1685) Add command to fix versiondb corrupted data.
* [#1688](https://github.com/crypto-org-chain/cronos/pull/1688) Add timestamp api to versiondb iterator.
* [#1686](https://github.com/crypto-org-chain/cronos/pull/1686) Update rocksdb to 9.7.4.

*Oct 14, 2024*

Expand Down
4 changes: 4 additions & 0 deletions app/versiondb.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func (app *App) setupVersionDB(
if err := os.MkdirAll(dataDir, os.ModePerm); err != nil {
return nil, err
}

versionDB, err := tsrocksdb.NewStore(dataDir)
if err != nil {
return nil, err
Expand All @@ -34,6 +35,9 @@ func (app *App) setupVersionDB(
exposeStoreKeys = append(exposeStoreKeys, storeKey)
}

// see: https://github.com/crypto-org-chain/cronos/issues/1683
versionDB.SetSkipVersionZero(true)

service := versiondb.NewStreamingService(versionDB, exposeStoreKeys)
app.SetStreamingService(service)

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ require (
github.com/golang/protobuf v1.5.4
github.com/gorilla/mux v1.8.0
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/linxGnu/grocksdb v1.9.2
github.com/linxGnu/grocksdb v1.9.7
github.com/peggyjv/gravity-bridge/module/v2 v2.0.0-20220420162017-838c0d25e974
github.com/spf13/cast v1.6.0
github.com/spf13/cobra v1.8.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1085,8 +1085,8 @@ github.com/libp2p/go-buffer-pool v0.1.0 h1:oK4mSFcQz7cTQIfqbe4MIj9gLW+mnanjyFtc6
github.com/libp2p/go-buffer-pool v0.1.0/go.mod h1:N+vh8gMqimBzdKkSMVuydVDq+UV5QTWy5HSiZacSbPg=
github.com/lightstep/lightstep-tracer-common/golang/gogo v0.0.0-20190605223551-bc2310a04743/go.mod h1:qklhhLq1aX+mtWk9cPHPzaBjWImj5ULL6C7HFJtXQMM=
github.com/lightstep/lightstep-tracer-go v0.18.1/go.mod h1:jlF1pusYV4pidLvZ+XD0UBX0ZE6WURAspgAczcDHrL4=
github.com/linxGnu/grocksdb v1.9.2 h1:O3mzvO0wuzQ9mtlHbDrShixyVjVbmuqTjFrzlf43wZ8=
github.com/linxGnu/grocksdb v1.9.2/go.mod h1:QYiYypR2d4v63Wj1adOOfzglnoII0gLj3PNh4fZkcFA=
github.com/linxGnu/grocksdb v1.9.7 h1:Bp2r1Yti/IXxEobZZnDooXAui/Q+5gVqgQMenLWyDUw=
github.com/linxGnu/grocksdb v1.9.7/go.mod h1:QYiYypR2d4v63Wj1adOOfzglnoII0gLj3PNh4fZkcFA=
github.com/lufeee/execinquery v1.2.1/go.mod h1:EC7DrEKView09ocscGHC+apXMIaorh4xqSxS/dy8SbM=
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I=
github.com/lyft/protoc-gen-star v0.5.3/go.mod h1:V0xaHgaf5oCCqmcxYcWiDfTiKsZsRc87/1qhoTACD8w=
Expand Down
4 changes: 2 additions & 2 deletions gomod2nix.toml
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,8 @@ schema = 3
version = "v0.1.0"
hash = "sha256-wQqGTtRWsfR9n0O/SXHVgECebbnNmHddxJIbG63OJBQ="
[mod."github.com/linxGnu/grocksdb"]
version = "v1.9.2"
hash = "sha256-ThXtaXx6LvRIFW4xLHsMrVWdsN2qobLPA0InLmlADOM="
version = "v1.9.7"
hash = "sha256-ZSomnYZRo7gHB9/FW55MebNkNzn0DuR96RfsVpAwjIQ="
[mod."github.com/magiconair/properties"]
version = "v1.8.7"
hash = "sha256-XQ2bnc2s7/IH3WxEO4GishZurMyKwEclZy1DXg+2xXc="
Expand Down
4 changes: 2 additions & 2 deletions nix/rocksdb.nix
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@

stdenv.mkDerivation (finalAttrs: {
pname = "rocksdb";
version = "9.2.1";
version = "9.7.4";

src = fetchFromGitHub {
owner = "facebook";
repo = finalAttrs.pname;
rev = "v${finalAttrs.version}";
hash = "sha256-Zifn5Gu/4h6TaEqSaWQ2mFdryeAarqbHWW3fKUGGFac=";
hash = "sha256-u5uuShM2SxHc9/zL4UU56IhCcR/ZQbzde0LgOYS44bM=";
};

nativeBuildInputs = [
Expand Down
1 change: 1 addition & 0 deletions versiondb/client/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func ChangeSetGroupCmd(opts Options) *cobra.Command {
ChangeSetToVersionDBCmd(),
RestoreAppDBCmd(opts),
RestoreVersionDBCmd(),
FixDataCmd(opts.DefaultStores),
)
return cmd
}
59 changes: 59 additions & 0 deletions versiondb/client/fixdata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package client

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

const (
FlagDryRun = "dry-run"
FlagStore = "store-name"
)

func FixDataCmd(defaultStores []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]
dryRun, err := cmd.Flags().GetBool(FlagDryRun)
if err != nil {
return err
}
stores, err := cmd.Flags().GetStringArray(FlagStore)
if err != nil {
return err
}
if len(stores) == 0 {
stores = defaultStores
}

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
}

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.")
cmd.Flags().StringArray(FlagStore, []string{}, "Store names to fix, if not specified, all stores will be fixed.")
return cmd
}
2 changes: 1 addition & 1 deletion versiondb/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require (
github.com/cosmos/iavl v0.21.0-alpha.1.0.20230904092046-df3db2d96583
github.com/crypto-org-chain/cronos/memiavl v0.0.3
github.com/golang/snappy v0.0.4
github.com/linxGnu/grocksdb v1.9.2
github.com/linxGnu/grocksdb v1.9.7
github.com/spf13/cast v1.5.0
github.com/spf13/cobra v1.6.1
github.com/stretchr/testify v1.9.0
Expand Down
4 changes: 2 additions & 2 deletions versiondb/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -515,8 +515,8 @@ github.com/libp2p/go-buffer-pool v0.1.0 h1:oK4mSFcQz7cTQIfqbe4MIj9gLW+mnanjyFtc6
github.com/libp2p/go-buffer-pool v0.1.0/go.mod h1:N+vh8gMqimBzdKkSMVuydVDq+UV5QTWy5HSiZacSbPg=
github.com/lightstep/lightstep-tracer-common/golang/gogo v0.0.0-20190605223551-bc2310a04743/go.mod h1:qklhhLq1aX+mtWk9cPHPzaBjWImj5ULL6C7HFJtXQMM=
github.com/lightstep/lightstep-tracer-go v0.18.1/go.mod h1:jlF1pusYV4pidLvZ+XD0UBX0ZE6WURAspgAczcDHrL4=
github.com/linxGnu/grocksdb v1.9.2 h1:O3mzvO0wuzQ9mtlHbDrShixyVjVbmuqTjFrzlf43wZ8=
github.com/linxGnu/grocksdb v1.9.2/go.mod h1:QYiYypR2d4v63Wj1adOOfzglnoII0gLj3PNh4fZkcFA=
github.com/linxGnu/grocksdb v1.9.7 h1:Bp2r1Yti/IXxEobZZnDooXAui/Q+5gVqgQMenLWyDUw=
github.com/linxGnu/grocksdb v1.9.7/go.mod h1:QYiYypR2d4v63Wj1adOOfzglnoII0gLj3PNh4fZkcFA=
github.com/lyft/protoc-gen-validate v0.0.13/go.mod h1:XbGvPuh87YZc5TdIa2/I4pLk0QoUACkjt2znoq26NVQ=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/magiconair/properties v1.8.6 h1:5ibWZ6iY0NctNGWo87LalDlEZ6R41TqbbDamhfG/Qzo=
Expand Down
43 changes: 33 additions & 10 deletions versiondb/tsrocksdb/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package tsrocksdb

import (
"bytes"
"encoding/binary"

"github.com/cosmos/cosmos-sdk/store/types"
"github.com/crypto-org-chain/cronos/versiondb"
"github.com/linxGnu/grocksdb"
)

Expand All @@ -12,11 +13,14 @@ type rocksDBIterator struct {
prefix, start, end []byte
isReverse bool
isInvalid bool

// see: https://github.com/crypto-org-chain/cronos/issues/1683
skipVersionZero bool
}

var _ types.Iterator = (*rocksDBIterator)(nil)
var _ versiondb.Iterator = (*rocksDBIterator)(nil)

func newRocksDBIterator(source *grocksdb.Iterator, prefix, start, end []byte, isReverse bool) *rocksDBIterator {
func newRocksDBIterator(source *grocksdb.Iterator, prefix, start, end []byte, isReverse bool, skipVersionZero bool) *rocksDBIterator {
if isReverse {
if end == nil {
source.SeekToLast()
Expand All @@ -39,14 +43,18 @@ func newRocksDBIterator(source *grocksdb.Iterator, prefix, start, end []byte, is
source.Seek(start)
}
}
return &rocksDBIterator{
source: source,
prefix: prefix,
start: start,
end: end,
isReverse: isReverse,
isInvalid: false,
it := &rocksDBIterator{
source: source,
prefix: prefix,
start: start,
end: end,
isReverse: isReverse,
isInvalid: false,
skipVersionZero: skipVersionZero,
}

it.trySkipZeroVersion()
return it
}

// Domain implements Iterator.
Expand Down Expand Up @@ -114,6 +122,21 @@ func (itr rocksDBIterator) Next() {
} else {
itr.source.Next()
}

itr.trySkipZeroVersion()
}

func (itr rocksDBIterator) Timestamp() []byte {
itr.assertIsValid()
return moveSliceToBytes(itr.source.Timestamp())
}

func (itr rocksDBIterator) trySkipZeroVersion() {
if itr.skipVersionZero {
for itr.Valid() && binary.LittleEndian.Uint64(itr.Timestamp()) == 0 {
itr.Next()
}
}
}

// Error implements Iterator.
Expand Down
14 changes: 14 additions & 0 deletions versiondb/tsrocksdb/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,20 @@ 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()
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
Loading

0 comments on commit 93af817

Please sign in to comment.