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

feat: speed up rollback command #620

Closed
wants to merge 15 commits into from
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

- [#586](https://github.com/cosmos/iavl/pull/586) Remove the `RangeProof` and refactor the ics23_proof to use the internal methods.
- [#620](https://github.com/cosmos/iavl/pull/620) Add fast mode to rollback chain state in quick and dirty way, may leave some orphan nodes in db, not a big deal.

## 0.19.4 (October 28, 2022)

Expand Down
16 changes: 14 additions & 2 deletions mutable_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -616,19 +616,31 @@ func (tree *MutableTree) LoadVersion(targetVersion int64) (int64, error) {
// LoadVersionForOverwriting attempts to load a tree at a previously committed
// version, or the latest version below it. Any versions greater than targetVersion will be deleted.
func (tree *MutableTree) LoadVersionForOverwriting(targetVersion int64) (int64, error) {
latestVersion, err := tree.LoadVersion(targetVersion)
return tree.LoadVersionForOverwritingWithMode(targetVersion, false)
}

func (tree *MutableTree) LoadVersionForOverwritingWithMode(targetVersion int64, fastMode bool) (int64, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's add a doc string explaining the new parameter.

var latestVersion int64
var err error
if !fastMode {
latestVersion, err = tree.LoadVersion(targetVersion)
} else {
latestVersion, err = tree.LazyLoadVersion(targetVersion)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a param name clash:

  • seams that we want to use fastMode when we don't want to use fastCache
  • so let's rename fastMode parameter to something different, eg lazy bool or noFastCache bool.

Copy link
Contributor Author

@mmsqe mmsqe Nov 17, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually this flag is mainly related with rollback, should we rename to FastRollback or OfflineRollback.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, but in the DeleteVersionsFrom it's related to the fast cache

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sense, then OfflineRollback might better since assumption is based on offline and re-index on restart.

}
if err != nil {
return latestVersion, err
}

if err = tree.ndb.DeleteVersionsFrom(targetVersion + 1); err != nil {
if err = tree.ndb.DeleteVersionsFrom(targetVersion+1, fastMode); err != nil {
return latestVersion, err
}

if !tree.skipFastStorageUpgrade {
if err := tree.enableFastStorageAndCommitLocked(); err != nil {
return latestVersion, err
}
} else if err = tree.ndb.Commit(); err != nil {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yihuang seems we still need commit if skipFastStorageUpgrade?

Copy link
Collaborator

@yihuang yihuang Dec 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, so it don't commit previously?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yup, I wonder if need fix as a separate bug

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, the current rollback cmd maybe don't work at all if the fast node is disabled 😂

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Closing this PR, let's limit the options.

return latestVersion, err
}

tree.ndb.resetLatestVersion(latestVersion)
Expand Down
108 changes: 66 additions & 42 deletions nodedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ func (ndb *nodeDB) DeleteVersion(version int64, checkLatestVersion bool) error {
}

// DeleteVersionsFrom permanently deletes all tree versions from the given version upwards.
func (ndb *nodeDB) DeleteVersionsFrom(version int64) error {
func (ndb *nodeDB) DeleteVersionsFrom(version int64, fastMode bool) error {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto, also update doc string

latest, err := ndb.getLatestVersion()
if err != nil {
return err
Expand All @@ -444,34 +444,53 @@ func (ndb *nodeDB) DeleteVersionsFrom(version int64) error {

// First, delete all active nodes in the current (latest) version whose node version is after
// the given version.
err = ndb.deleteNodesFrom(version, root)
if err != nil {
return err
}

// 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 {
var fromVersion, toVersion int64
orphanKeyFormat.Scan(key, &toVersion, &fromVersion)

if fromVersion >= version {
if err = ndb.batch.Delete(key); err != nil {
return err
}
if err = ndb.batch.Delete(ndb.nodeKey(hash)); err != nil {
return err
if !fastMode {
err = ndb.deleteNodesFrom(version, root)
if err != nil {
return err
}
// 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 {
var fromVersion, toVersion int64
orphanKeyFormat.Scan(key, &toVersion, &fromVersion)

if fromVersion >= version {
if err = ndb.batch.Delete(key); err != nil {
return err
}
if err = ndb.batch.Delete(ndb.nodeKey(hash)); err != nil {
return err
}
ndb.nodeCache.Remove(hash)
} else if toVersion >= version-1 {
if err = ndb.batch.Delete(key); err != nil {
return err
}
}
ndb.nodeCache.Remove(hash)
} else if toVersion >= version-1 {
if err = ndb.batch.Delete(key); err != nil {
return err
return nil
})
} else {
err = ndb.traverseOrphansVersion(version-1, func(key, hash []byte) error {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we change a version here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since toVersion in orphan records is current version-1

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, let's add a comment in the code.

var fromVersion, toVersion int64
orphanKeyFormat.Scan(key, &toVersion, &fromVersion)
if fromVersion >= version {
if err = ndb.batch.Delete(key); err != nil {
return err
}
if err = ndb.batch.Delete(ndb.nodeKey(hash)); err != nil {
return err
}
ndb.nodeCache.Remove(hash)
} else if toVersion >= version-1 {
if err = ndb.batch.Delete(key); err != nil {
return err
}
}
}
return nil
})

return nil
})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The inner function is the same in both cases. Let's avoid copy-paste.

}
if err != nil {
return err
}
Expand All @@ -489,24 +508,29 @@ func (ndb *nodeDB) DeleteVersionsFrom(version int64) error {
}

// Delete fast node entries
err = ndb.traverseFastNodes(func(keyWithPrefix, v []byte) error {
key := keyWithPrefix[1:]
fastNode, err := fastnode.DeserializeNode(key, v)
if err != nil {
return err
}

if version <= fastNode.GetVersionLastUpdatedAt() {
if err = ndb.batch.Delete(keyWithPrefix); err != nil {
// Delete step will be skipped with enable fastMode
// with the assumption that the rollback happens offline
// since fast nodes will be reinforced when next start up
if !fastMode {
err = ndb.traverseFastNodes(func(keyWithPrefix, v []byte) error {
key := keyWithPrefix[1:]
fastNode, err := fastnode.DeserializeNode(key, v)
if err != nil {
return err
}
ndb.fastNodeCache.Remove(key)
}
return nil
})

if err != nil {
return err
if version <= fastNode.GetVersionLastUpdatedAt() {
if err = ndb.batch.Delete(keyWithPrefix); err != nil {
return err
}
ndb.fastNodeCache.Remove(key)
}
return nil
})

if err != nil {
return err
}
}

return nil
Expand Down
63 changes: 63 additions & 0 deletions nodedb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,66 @@ func makeAndPopulateMutableTree(tb testing.TB) *MutableTree {
require.Nil(tb, err, "Expected .SaveVersion to succeed")
return tree
}

func TestDeleteVersion(t *testing.T) {
v := []byte("value")

var version int64 = 100
testCases := []struct {
name string
v int64
fastMode bool
}{
{
"delete from version without fast mode",
version,
false,
},
{
"delete from version -1 without fast mode",
version - 1,
false,
},
{
"enable fast mode",
version,
true,
},
}
for _, tc := range testCases {
db := db.NewMemDB()
ndb := newNodeDB(db, 0, nil)
leftNode := NewNode([]byte("left_key"), v, version-1)
rightNode := NewNode([]byte("right_key"), v, version-1)
node := NewNode([]byte("key"), v, version)
node.leftNode = leftNode
node.rightNode = rightNode
node.subtreeHeight = 1
node.size = 2
hash, err := ndb.SaveBranch(node)
require.NoError(t, err)
err = ndb.Commit()
require.NoError(t, err)
key := ndb.rootKey(version)
err = ndb.db.Set(key, hash)
require.NoError(t, err)
err = ndb.DeleteVersionsFrom(tc.v, tc.fastMode)
require.NoError(t, err)
err = ndb.Commit()
require.NoError(t, err)
bz, err := ndb.db.Get(ndb.nodeKey(hash))
require.NoError(t, err)
leftBz, err := ndb.db.Get(ndb.nodeKey(leftNode.hash))
require.NoError(t, err)
if !tc.fastMode {
if tc.v <= version {
require.Empty(t, bz)
}
if tc.v < version {
require.Empty(t, leftBz)
} else {
require.NotEmpty(t, leftBz)
}
}
}
}