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

Problem: no command to fix corrupted data in versiondb #1685

Merged
merged 30 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from 17 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Bug Fixes

* [#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.

### Improvements

Expand Down
4 changes: 4 additions & 0 deletions app/versiondb.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ func (app *App) setupVersionDB(
for _, key := range keys {
exposedKeys = append(exposedKeys, key)
}

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

app.CommitMultiStore().AddListeners(exposedKeys)

// register in app streaming manager
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 @@
ChangeSetToVersionDBCmd(),
RestoreAppDBCmd(opts),
RestoreVersionDBCmd(),
FixDataCmd(opts.DefaultStores),

Check warning on line 31 in versiondb/client/cmd.go

View check run for this annotation

Codecov / codecov/patch

versiondb/client/cmd.go#L31

Added line #L31 was not covered by tests
)
return cmd
}
28 changes: 28 additions & 0 deletions versiondb/client/fixdata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package client

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

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)
if err != nil {
return err
}

Check warning on line 18 in versiondb/client/fixdata.go

View check run for this annotation

Codecov / codecov/patch

versiondb/client/fixdata.go#L8-L18

Added lines #L8 - L18 were not covered by tests

if err := versionDB.FixData(stores); err != nil {
return err
}

Check warning on line 22 in versiondb/client/fixdata.go

View check run for this annotation

Codecov / codecov/patch

versiondb/client/fixdata.go#L20-L22

Added lines #L20 - L22 were not covered by tests

return nil

Check warning on line 24 in versiondb/client/fixdata.go

View check run for this annotation

Codecov / codecov/patch

versiondb/client/fixdata.go#L24

Added line #L24 was not covered by tests
},
}
return cmd

Check warning on line 27 in versiondb/client/fixdata.go

View check run for this annotation

Codecov / codecov/patch

versiondb/client/fixdata.go#L27

Added line #L27 was not covered by tests
}
yihuang marked this conversation as resolved.
Show resolved Hide resolved
44 changes: 36 additions & 8 deletions versiondb/tsrocksdb/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tsrocksdb

import (
"bytes"
"encoding/binary"

"cosmossdk.io/store/types"
"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)

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,22 @@ func (itr rocksDBIterator) Next() {
} else {
itr.source.Next()
}

itr.trySkipZeroVersion()
}
yihuang marked this conversation as resolved.
Show resolved Hide resolved

func (itr rocksDBIterator) timestamp() uint64 {
ts := itr.source.Timestamp()
defer ts.Free()
return binary.LittleEndian.Uint64(ts.Data())
}

func (itr rocksDBIterator) trySkipZeroVersion() {
if itr.skipVersionZero {
for itr.Valid() && itr.timestamp() == 0 {
itr.Next()
}
}
yihuang marked this conversation as resolved.
Show resolved Hide resolved
}

// Error implements Iterator.
Expand All @@ -137,6 +161,10 @@ func (itr *rocksDBIterator) assertIsValid() {
// This function can be applied on *Slice returned from Key() and Value()
// of an Iterator, because they are marked as freed.
func moveSliceToBytes(s *grocksdb.Slice) []byte {
if s == nil {
return nil
}

defer s.Free()
if !s.Exists() {
return nil
Expand Down
99 changes: 86 additions & 13 deletions versiondb/tsrocksdb/store.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tsrocksdb

import (
"bytes"
"encoding/binary"
"errors"
"fmt"
Expand Down Expand Up @@ -38,6 +39,9 @@
type Store struct {
db *grocksdb.DB
cfHandle *grocksdb.ColumnFamilyHandle

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

func NewStore(dir string) (Store, error) {
Expand All @@ -58,6 +62,10 @@
}
}

func (s *Store) SetSkipVersionZero(skip bool) {
s.skipVersionZero = skip
}

func (s Store) SetLatestVersion(version int64) error {
var ts [TimestampSize]byte
binary.LittleEndian.PutUint64(ts[:], uint64(version))
Expand Down Expand Up @@ -86,11 +94,23 @@
}

func (s Store) GetAtVersionSlice(storeKey string, key []byte, version *int64) (*grocksdb.Slice, error) {
return s.db.GetCF(
value, ts, err := s.db.GetCFWithTS(
newTSReadOptions(version),
s.cfHandle,
prependStoreKey(storeKey, key),
)
if err != nil {
return nil, err
}

Check warning on line 104 in versiondb/tsrocksdb/store.go

View check run for this annotation

Codecov / codecov/patch

versiondb/tsrocksdb/store.go#L103-L104

Added lines #L103 - L104 were not covered by tests
defer ts.Free()

yihuang marked this conversation as resolved.
Show resolved Hide resolved
if value.Exists() && s.skipVersionZero {
if binary.LittleEndian.Uint64(ts.Data()) == 0 {
return nil, nil
}
}

return value, err
yihuang marked this conversation as resolved.
Show resolved Hide resolved
}

// GetAtVersion implements VersionStore interface
Expand All @@ -105,7 +125,7 @@
// HasAtVersion implements VersionStore interface
func (s Store) HasAtVersion(storeKey string, key []byte, version *int64) (bool, error) {
slice, err := s.GetAtVersionSlice(storeKey, key, version)
if err != nil {
if err != nil || slice == nil {
yihuang marked this conversation as resolved.
Show resolved Hide resolved
return false, err
}
defer slice.Free()
Expand All @@ -128,28 +148,24 @@

// IteratorAtVersion implements VersionStore interface
func (s Store) IteratorAtVersion(storeKey string, start, end []byte, version *int64) (types.Iterator, error) {
if (start != nil && len(start) == 0) || (end != nil && len(end) == 0) {
return nil, errKeyEmpty
}

prefix := storePrefix(storeKey)
start, end = iterateWithPrefix(prefix, start, end)

itr := s.db.NewIteratorCF(newTSReadOptions(version), s.cfHandle)
return newRocksDBIterator(itr, prefix, start, end, false), nil
return s.iteratorAtVersion(storeKey, start, end, version, false)
yihuang marked this conversation as resolved.
Show resolved Hide resolved
}

// ReverseIteratorAtVersion implements VersionStore interface
func (s Store) ReverseIteratorAtVersion(storeKey string, start, end []byte, version *int64) (types.Iterator, error) {
return s.iteratorAtVersion(storeKey, start, end, version, true)
}

func (s Store) iteratorAtVersion(storeKey string, start, end []byte, version *int64, reverse bool) (types.Iterator, error) {
if (start != nil && len(start) == 0) || (end != nil && len(end) == 0) {
return nil, errKeyEmpty
}

prefix := storePrefix(storeKey)
start, end = iterateWithPrefix(storePrefix(storeKey), start, end)
start, end = iterateWithPrefix(prefix, start, end)

itr := s.db.NewIteratorCF(newTSReadOptions(version), s.cfHandle)
return newRocksDBIterator(itr, prefix, start, end, true), nil
return newRocksDBIterator(itr, prefix, start, end, reverse, s.skipVersionZero), nil
yihuang marked this conversation as resolved.
Show resolved Hide resolved
}
yihuang marked this conversation as resolved.
Show resolved Hide resolved

// FeedChangeSet is used to migrate legacy change sets into versiondb
Expand Down Expand Up @@ -216,6 +232,63 @@
)
}

// 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 {
for _, storeName := range storeNames {
if err := s.fixDataStore(storeName); err != nil {
return err
}

Check warning on line 242 in versiondb/tsrocksdb/store.go

View check run for this annotation

Codecov / codecov/patch

versiondb/tsrocksdb/store.go#L241-L242

Added lines #L241 - L242 were not covered by tests
}

return nil
}
yihuang marked this conversation as resolved.
Show resolved Hide resolved

// 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 {
var version int64
iter, err := s.IteratorAtVersion(storeName, nil, nil, &version)
if err != nil {
return err
}

Check warning on line 254 in versiondb/tsrocksdb/store.go

View check run for this annotation

Codecov / codecov/patch

versiondb/tsrocksdb/store.go#L253-L254

Added lines #L253 - L254 were not covered by tests
defer iter.Close()

batch := grocksdb.NewWriteBatch()
defer batch.Destroy()

prefix := storePrefix(storeName)
for ; iter.Valid(); iter.Next() {
key := iter.Key()
if len(key) < TimestampSize {
return fmt.Errorf("invalid key length: %X, store: %s", key, storeName)
}

Check warning on line 265 in versiondb/tsrocksdb/store.go

View check run for this annotation

Codecov / codecov/patch

versiondb/tsrocksdb/store.go#L264-L265

Added lines #L264 - L265 were not covered by tests

ts := key[len(key)-TimestampSize:]
key = key[:len(key)-TimestampSize]
realKey := cloneAppend(prefix, key)

readOpts := grocksdb.NewDefaultReadOptions()
readOpts.SetTimestamp(ts)
oldValue, err := s.db.GetCF(readOpts, s.cfHandle, realKey)
mmsqe marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return err
}

Check warning on line 276 in versiondb/tsrocksdb/store.go

View check run for this annotation

Codecov / codecov/patch

versiondb/tsrocksdb/store.go#L275-L276

Added lines #L275 - L276 were not covered by tests
yihuang marked this conversation as resolved.
Show resolved Hide resolved
readOpts.Destroy()

clean := bytes.Equal(oldValue.Data(), iter.Value())
oldValue.Free()

if clean {
continue

Check warning on line 283 in versiondb/tsrocksdb/store.go

View check run for this annotation

Codecov / codecov/patch

versiondb/tsrocksdb/store.go#L283

Added line #L283 was not covered by tests
}

batch.PutCFWithTS(s.cfHandle, realKey, ts, iter.Value())
}

return s.db.Write(defaultSyncWriteOpts, batch)
}
yihuang marked this conversation as resolved.
Show resolved Hide resolved

func newTSReadOptions(version *int64) *grocksdb.ReadOptions {
var ver uint64
if version == nil {
Expand Down
Loading
Loading