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

fix: the reference root issue #909

Merged
merged 32 commits into from
Mar 26, 2024
Merged

fix: the reference root issue #909

merged 32 commits into from
Mar 26, 2024

Conversation

cool-develope
Copy link
Collaborator

Implement async pruning of legacy nodes

Fix

When the root refers to the legacy root, it will reformat the legacy node as a new node key format. This node is removed while pruning but it is still being referred by the forward version.
Another issue with the reference node, it can refer to the internal node, not only the root. For example, if there are no adds, just removes, then the leaf or inner node can be the root.

  • Store the nodeKey instead of only the version as a reference node value
  • When reformat the legacy root, use (version, 0) as a nodeKey to distinguish with the root. It will also be applied for the case of when pruning the version, reformat the given root nodeKey to (version, 0).
  • Fix the legacy node pruning

@cool-develope cool-develope requested a review from a team as a code owner March 12, 2024 20:47
Copy link

coderabbitai bot commented Mar 12, 2024

Walkthrough

The recent updates focus on enhancing consistency, clarity, and efficiency in handling node storage and version management. Key modifications include method renaming for uniformity, improved commentary for easier understanding, and a refined approach to dealing with legacy nodes. Additionally, the logic for deleting versions has been clarified and optimized, with particular attention to batch deletions and mutex handling for safer concurrency operations. These changes aim to streamline operations and maintenance of the data structure.

Changes

File(s) Summary of Changes
mutable_tree.go - Standardized method naming
- Enhanced comments for clarity
- Adjusted legacy node handling
- Refined DeleteVersionsTo method documentation
nodedb.go - Introduced constants for batch deletion
- Renamed method for setting storage version
- Reworked deletion logic for orphans and roots
- Implemented batch deletion pauses
- Improved mutex handling
- Updated version, root, node, and orphan management methods

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share

Tips

Chat

There are 3 ways to chat with CodeRabbit:

Note: Auto-reply has been disabled for this repository by the repository owner. The CodeRabbit bot will not respond to your replies unless it is explicitly tagged.

  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit tests for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

@cool-develope cool-develope mentioned this pull request Mar 12, 2024
Comment on lines +1825 to +1854
func TestEmptyVersionDelete(t *testing.T) {
db, err := dbm.NewDB("test", "memdb", "")
require.NoError(t, err)
defer db.Close()

tree := NewMutableTree(db, 0, false, log.NewNopLogger())

_, err = tree.Set([]byte("key1"), []byte("value1"))
require.NoError(t, err)

toVersion := 10
for i := 0; i < toVersion; i++ {
_, _, err = tree.SaveVersion()
require.NoError(t, err)
}

require.NoError(t, tree.DeleteVersionsTo(5))

// Load the tree from disk.
tree = NewMutableTree(db, 0, false, log.NewNopLogger())
v, err := tree.Load()
require.NoError(t, err)
require.Equal(t, int64(toVersion), v)
// Version 1 is only meaningful, so it should not be deleted.
require.Equal(t, tree.root.GetKey(), (&NodeKey{version: 1, nonce: 0}).GetKey())
// it is expected that the version 1 is deleted.
versions := tree.AvailableVersions()
require.Equal(t, 6, versions[0])
require.Len(t, versions, 5)
}
Copy link

Choose a reason for hiding this comment

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

The test TestEmptyVersionDelete aims to verify the behavior of deleting versions up to a certain point and ensuring that the tree loads correctly afterward. However, there's a discrepancy in the comments and the expected behavior:

  • Line 1849 suggests that version 1 should not be deleted, which aligns with the assertion checking the root key for version 1.
  • Line 1850, however, comments that it is expected version 1 is deleted, which contradicts the previous assertion and the actual test logic.

To clarify the test's intention and ensure it accurately reflects the expected behavior, please revise the comments to consistently describe the expected outcome of the test.

- // it is expected that the version 1 is deleted.
+ // it is expected that versions up to 5 are deleted, leaving versions 6 and above.

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
func TestEmptyVersionDelete(t *testing.T) {
db, err := dbm.NewDB("test", "memdb", "")
require.NoError(t, err)
defer db.Close()
tree := NewMutableTree(db, 0, false, log.NewNopLogger())
_, err = tree.Set([]byte("key1"), []byte("value1"))
require.NoError(t, err)
toVersion := 10
for i := 0; i < toVersion; i++ {
_, _, err = tree.SaveVersion()
require.NoError(t, err)
}
require.NoError(t, tree.DeleteVersionsTo(5))
// Load the tree from disk.
tree = NewMutableTree(db, 0, false, log.NewNopLogger())
v, err := tree.Load()
require.NoError(t, err)
require.Equal(t, int64(toVersion), v)
// Version 1 is only meaningful, so it should not be deleted.
require.Equal(t, tree.root.GetKey(), (&NodeKey{version: 1, nonce: 0}).GetKey())
// it is expected that the version 1 is deleted.
versions := tree.AvailableVersions()
require.Equal(t, 6, versions[0])
require.Len(t, versions, 5)
}
func TestEmptyVersionDelete(t *testing.T) {
db, err := dbm.NewDB("test", "memdb", "")
require.NoError(t, err)
defer db.Close()
tree := NewMutableTree(db, 0, false, log.NewNopLogger())
_, err = tree.Set([]byte("key1"), []byte("value1"))
require.NoError(t, err)
toVersion := 10
for i := 0; i < toVersion; i++ {
_, _, err = tree.SaveVersion()
require.NoError(t, err)
}
require.NoError(t, tree.DeleteVersionsTo(5))
// Load the tree from disk.
tree = NewMutableTree(db, 0, false, log.NewNopLogger())
v, err := tree.Load()
require.NoError(t, err)
require.Equal(t, int64(toVersion), v)
// Version 1 is only meaningful, so it should not be deleted.
require.Equal(t, tree.root.GetKey(), (&NodeKey{version: 1, nonce: 0}).GetKey())
// it is expected that versions up to 5 are deleted, leaving versions 6 and above.
versions := tree.AvailableVersions()
require.Equal(t, 6, versions[0])
require.Len(t, versions, 5)
}

tac0turtle pushed a commit that referenced this pull request Mar 13, 2024
Comment on lines +246 to +252
// SetFastStorageVersionToBatch sets storage version to fast where the version is
// 1.1.0-<version of the current live state>. Returns error if storage version is incorrect or on
// db error, nil otherwise. Requires changes to be committed after to be persisted.
func (ndb *nodeDB) setFastStorageVersionToBatch(latestVersion int64) error {
func (ndb *nodeDB) SetFastStorageVersionToBatch(latestVersion int64) error {
ndb.mtx.Lock()
defer ndb.mtx.Unlock()

Copy link

Choose a reason for hiding this comment

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

The method SetFastStorageVersionToBatch now includes a locking mechanism to ensure thread safety. However, the comparison ndb.storageVersion >= fastStorageVersionValue uses string comparison for version checking, which might not work as intended for semantic versioning. Consider implementing or using a library for semantic version comparison to avoid potential issues.

- if ndb.storageVersion >= fastStorageVersionValue {
+ if compareSemanticVersions(ndb.storageVersion, fastStorageVersionValue) >= 0 {

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
// SetFastStorageVersionToBatch sets storage version to fast where the version is
// 1.1.0-<version of the current live state>. Returns error if storage version is incorrect or on
// db error, nil otherwise. Requires changes to be committed after to be persisted.
func (ndb *nodeDB) setFastStorageVersionToBatch(latestVersion int64) error {
func (ndb *nodeDB) SetFastStorageVersionToBatch(latestVersion int64) error {
ndb.mtx.Lock()
defer ndb.mtx.Unlock()
// SetFastStorageVersionToBatch sets storage version to fast where the version is
// 1.1.0-<version of the current live state>. Returns error if storage version is incorrect or on
// db error, nil otherwise. Requires changes to be committed after to be persisted.
func (ndb *nodeDB) SetFastStorageVersionToBatch(latestVersion int64) error {
ndb.mtx.Lock()
defer ndb.mtx.Unlock()

@tac0turtle tac0turtle enabled auto-merge (squash) March 26, 2024 19:59
@tac0turtle tac0turtle merged commit 2894221 into master Mar 26, 2024
7 checks passed
@tac0turtle tac0turtle deleted the feat/pruning_ref branch March 26, 2024 20:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants