Skip to content

Commit

Permalink
Problem: memiavl restore fail to reopen the db (#1130)
Browse files Browse the repository at this point in the history
* Problem: memiavl restore fail to reopen twice

Solution:
- close the db before open again

* fix

* add unlock err

---------

Co-authored-by: mmsqe <mavis@crypto.com>
  • Loading branch information
yihuang and mmsqe authored Aug 10, 2023
1 parent ee517d8 commit 0fb08b2
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
- [#1102](https://github.com/crypto-org-chain/cronos/pull/1102) avoid duplicate cache events emitted from ibc and gravity hook.
- [#1123](https://github.com/crypto-org-chain/cronos/pull/1123) Fix memiavl snapshot switching
- [#1125](https://github.com/crypto-org-chain/cronos/pull/1125) Fix genesis migrate for feeibc, evm, feemarket and gravity.
- [#1130](https://github.com/crypto-org-chain/cronos/pull/1130) Fix lock issues when state-sync with memiavl.

### Features

Expand Down
7 changes: 6 additions & 1 deletion integration_tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,12 @@ def test_statesync(cronos):
"json-rpc": {
"address": "127.0.0.1:{EVMRPC_PORT}",
"ws-address": "127.0.0.1:{EVMRPC_PORT_WS}",
}
},
"memiavl": {
"enable": True,
"zero-copy": True,
"snapshot-interval": 5,
},
},
)
clustercli.supervisor.startProcess(f"{clustercli.chain_id}-node{i}")
Expand Down
11 changes: 10 additions & 1 deletion memiavl/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,22 @@ import (
// Import restore memiavl db from state-sync snapshot stream
func Import(
dir string, height uint64, format uint32, protoReader protoio.Reader,
) (snapshottypes.SnapshotItem, error) {
) (item snapshottypes.SnapshotItem, err error) {
if height > math.MaxUint32 {
return snapshottypes.SnapshotItem{}, fmt.Errorf("version overflows uint32: %d", height)
}
snapshotDir := snapshotName(int64(height))
tmpDir := snapshotDir + "-tmp"

var fileLock FileLock
fileLock, err = LockFile(filepath.Join(dir, LockFileName))
if err != nil {
return snapshottypes.SnapshotItem{}, fmt.Errorf("fail to lock db: %w", err)
}
defer func() {
err = stderrors.Join(err, fileLock.Unlock())
}()

// Import nodes into stores. The first item is expected to be a SnapshotItem containing
// a SnapshotStoreItem, telling us which store to import into. The following items will contain
// SnapshotNodeItem (i.e. ExportNode) until we reach the next SnapshotStoreItem or EOF.
Expand Down
7 changes: 7 additions & 0 deletions store/rootmulti/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,13 @@ func (rs *Store) Snapshot(height uint64, protoWriter protoio.Writer) error {

// Implements interface Snapshotter
func (rs *Store) Restore(height uint64, format uint32, protoReader protoio.Reader) (snapshottypes.SnapshotItem, error) {
if rs.db != nil {
if err := rs.db.Close(); err != nil {
return snapshottypes.SnapshotItem{}, fmt.Errorf("failed to close db: %w", err)
}
rs.db = nil
}

item, err := memiavl.Import(rs.dir, height, format, protoReader)
if err != nil {
return item, err
Expand Down

0 comments on commit 0fb08b2

Please sign in to comment.