-
Notifications
You must be signed in to change notification settings - Fork 269
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 #636
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,6 +35,7 @@ const ( | |
defaultStorageVersionValue = "1.0.0" | ||
fastStorageVersionValue = "1.1.0" | ||
fastNodeCacheSize = 100000 | ||
maxVersion = int64(math.MaxInt64) | ||
) | ||
|
||
var ( | ||
|
@@ -452,7 +453,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) | ||
|
||
|
@@ -477,7 +478,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 | ||
} | ||
|
@@ -488,26 +489,7 @@ func (ndb *nodeDB) DeleteVersionsFrom(version int64) error { | |
return err | ||
} | ||
|
||
// 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 { | ||
return err | ||
} | ||
ndb.fastNodeCache.Remove(key) | ||
} | ||
return nil | ||
}) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
// NOTICE: we don't touch fast node indexes here, because it'll be rebuilt later because of version mismatch. | ||
|
||
return nil | ||
} | ||
|
@@ -605,6 +587,11 @@ func (ndb *nodeDB) deleteNodesFrom(version int64, hash []byte) error { | |
return err | ||
} | ||
|
||
if node.version < version { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Like |
||
// 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 | ||
|
@@ -725,7 +712,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 | ||
} | ||
|
@@ -789,6 +776,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) | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the trade off here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe the previous logic to just delete some fastnodes is simply wrong, because fast node is assumed to be represent the complete live state, it's wrong to leave a partial fast node index.
In new version, fast node index will be fully rebuilt because of version mismatch, and if user don't want to slow down rollback, they can disable fast node index before rollback, after recovered the node operation, user can find another good time to rebuild the fast node index by enable it again.