Skip to content

Commit

Permalink
feat: speed up rollback command (#636)
Browse files Browse the repository at this point in the history
(cherry picked from commit d4086fe)

# Conflicts:
#	nodedb.go
  • Loading branch information
yihuang authored and mergify[bot] committed Dec 12, 2022
1 parent 2af006a commit 76afdea
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
## Unreleased

- [#640](https://github.com/cosmos/iavl/pull/640) commit `NodeDB` batch in `LoadVersionForOverwriting`.
- [#636](https://github.com/cosmos/iavl/pull/636) Speed up rollback method: `LoadVersionForOverwriting`.

## 0.19.4 (October 28, 2022)

Expand Down
19 changes: 7 additions & 12 deletions mutable_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -630,17 +630,18 @@ func (tree *MutableTree) LoadVersionForOverwriting(targetVersion int64) (int64,
return latestVersion, err
}

tree.mtx.Lock()
defer tree.mtx.Unlock()

tree.ndb.resetLatestVersion(latestVersion)

if !tree.skipFastStorageUpgrade {
if err := tree.enableFastStorageAndCommitLocked(); err != nil {
// it'll repopulates the fast node index because of version mismatch.
if _, err := tree.enableFastStorageAndCommitIfNotEnabled(); err != nil {
return latestVersion, err
}
}

tree.ndb.resetLatestVersion(latestVersion)

tree.mtx.Lock()
defer tree.mtx.Unlock()

for v := range tree.versions {
if v > targetVersion {
delete(tree.versions, v)
Expand Down Expand Up @@ -706,12 +707,6 @@ func (tree *MutableTree) enableFastStorageAndCommitIfNotEnabled() (bool, error)
return true, nil
}

func (tree *MutableTree) enableFastStorageAndCommitLocked() error {
tree.mtx.Lock()
defer tree.mtx.Unlock()
return tree.enableFastStorageAndCommit()
}

func (tree *MutableTree) enableFastStorageAndCommit() error {
var err error

Expand Down
17 changes: 14 additions & 3 deletions nodedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const (
defaultStorageVersionValue = "1.0.0"
fastStorageVersionValue = "1.1.0"
fastNodeCacheSize = 100000
maxVersion = int64(math.MaxInt64)
)

var (
Expand Down Expand Up @@ -451,7 +452,7 @@ func (ndb *nodeDB) DeleteVersionsFrom(version int64) error {
// Next, delete orphans:
// - Delete orphan entries *and referred nodes* with fromVersion >= version
// - Delete orphan entries with toVersion >= version-1 (since orphans at latest are not orphans)
err = ndb.traverseOrphans(func(key, hash []byte) error {
err = ndb.traverseRange(orphanKeyFormat.Key(version-1), orphanKeyFormat.Key(maxVersion), func(key, hash []byte) error {
var fromVersion, toVersion int64
orphanKeyFormat.Scan(key, &toVersion, &fromVersion)

Expand All @@ -476,7 +477,7 @@ func (ndb *nodeDB) DeleteVersionsFrom(version int64) error {
}

// Delete the version root entries
err = ndb.traverseRange(rootKeyFormat.Key(version), rootKeyFormat.Key(int64(math.MaxInt64)), func(k, v []byte) error {
err = ndb.traverseRange(rootKeyFormat.Key(version), rootKeyFormat.Key(maxVersion), func(k, v []byte) error {
if err = ndb.batch.Delete(k); err != nil {
return err
}
Expand All @@ -487,6 +488,7 @@ func (ndb *nodeDB) DeleteVersionsFrom(version int64) error {
return err
}

<<<<<<< HEAD
// Delete fast node entries
err = ndb.traverseFastNodes(func(keyWithPrefix, v []byte) error {
key := keyWithPrefix[1:]
Expand All @@ -508,6 +510,9 @@ func (ndb *nodeDB) DeleteVersionsFrom(version int64) error {
if err != nil {
return err
}
=======
// NOTICE: we don't touch fast node indexes here, because it'll be rebuilt later because of version mismatch.
>>>>>>> d4086fe (feat: speed up rollback command (#636))

return nil
}
Expand Down Expand Up @@ -605,6 +610,11 @@ func (ndb *nodeDB) deleteNodesFrom(version int64, hash []byte) error {
return err
}

if node.version < version {
// We can skip the whole sub-tree since children.version <= parent.version.
return nil
}

if node.leftHash != nil {
if err := ndb.deleteNodesFrom(version, node.leftHash); err != nil {
return err
Expand Down Expand Up @@ -722,7 +732,7 @@ func (ndb *nodeDB) rootKey(version int64) []byte {
func (ndb *nodeDB) getLatestVersion() (int64, error) {
if ndb.latestVersion == 0 {
var err error
ndb.latestVersion, err = ndb.getPreviousVersion(1<<63 - 1)
ndb.latestVersion, err = ndb.getPreviousVersion(maxVersion)
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -786,6 +796,7 @@ func (ndb *nodeDB) traverseOrphans(fn func(keyWithPrefix, v []byte) error) error
}

// Traverse fast nodes and return error if any, nil otherwise
// nolint: unused
func (ndb *nodeDB) traverseFastNodes(fn func(k, v []byte) error) error {
return ndb.traversePrefix(fastKeyFormat.Key(), fn)
}
Expand Down

0 comments on commit 76afdea

Please sign in to comment.