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: Add Extra Check for Reformatted Root Node in GetNode #1007

Merged
merged 3 commits into from
Nov 26, 2024

Conversation

cool-develope
Copy link
Collaborator

@cool-develope cool-develope commented Nov 20, 2024

Problem Description

When the root node is reformatted due to pruning, its nodeKey changes from (version, 1) to (version, 0). As a result:

1.	This version becomes invisible during a root search.
2.	If a node in the next version references the reformatted node, it becomes inaccessible.

Proposed Solution

Add an extra check in the GetNode function to handle reformatted root nodes. This ensures that:

•	Reformatted nodes with nodeKey (version, 0) remain accessible.
•	References to these nodes from subsequent versions are correctly resolved.

Summary by CodeRabbit

  • New Features

    • Introduced a new test to validate the behavior of the MutableTree after version deletions.
    • Added new APIs: DeleteVersionsFrom(int64) and GetLatestVersion.
  • Bug Fixes

    • Enhanced the GetNode method to improve retrieval logic for nodes, ensuring better handling of cases with nil buffers and legacy nodes.
  • Documentation

    • Updated the changelog to reflect recent improvements and fixes, including a new section for bug fixes and detailed descriptions of changes.

@cool-develope cool-develope requested a review from a team as a code owner November 20, 2024 14:53
Copy link

coderabbitai bot commented Nov 20, 2024

Walkthrough

The pull request introduces a new test function, TestReferenceRootPruning, in the mutable_tree_test.go file to validate the behavior of the MutableTree after version deletions. It also modifies the GetNode method in nodedb.go to enhance node retrieval logic, particularly when handling nodes with a nil buffer. The changes ensure that the system can correctly access node data even after pruning operations.

Changes

File Change Summary
mutable_tree_test.go Added a new test function TestReferenceRootPruning to validate tree behavior after deletions.
nodedb.go Modified GetNode method to handle nil buffers and improve error messages for node retrieval.
CHANGELOG.md Updated to include new APIs DeleteVersionsFrom(int64) and GetLatestVersion, and documented improvements and bug fixes.

Possibly related PRs

  • feat: expose nodeDB's DeleteVersionsFrom method #952: The addition of the DeleteVersionsFrom(int64) method in mutable_tree.go is directly related to the new test function TestReferenceRootPruning in mutable_tree_test.go, as both involve managing version deletions in the MutableTree.
  • chore: sync missing features from v1.2.x to the default branch #969: This PR also documents the addition of the DeleteVersionsFrom(int64) API, which is relevant to the changes in the main PR that test the behavior of the MutableTree after version deletions.
  • fix: close the pruning process properly #970: The changes in this PR regarding the proper closure of the pruning process in nodeDB are relevant as they enhance the overall functionality and reliability of the MutableTree, which is being tested in the main PR.
  • feat: allow saving version 0 #1002: The modifications to allow saving version 0 in the MutableTree are related to the overall version management improvements being tested in the main PR, particularly in how the tree handles version states.

Suggested reviewers

  • tac0turtle

Poem

In a tree where versions fade,
A rabbit hops, unafraid.
With tests to check each little change,
The nodes align, and all feels strange.
Pruning paths, a dance so neat,
New values bloom where old ones meet! 🐇🌳


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:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • 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 testing code 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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 using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • 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/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Outside diff range and nitpick comments (2)
nodedb.go (2)

162-175: Performance impact is minimal and acceptable.

The additional database lookup for reformatted nodes is justified:

  • It only occurs in specific edge cases
  • The nodeCache will help mitigate the performance impact
  • The correctness guarantee outweighs the minimal performance cost

Consider monitoring the cache hit rates for these reformatted nodes to ensure optimal performance.


172-172: Consider enhancing the error message for better debugging.

The error message could be more specific about the reformatting attempt.

-				return nil, fmt.Errorf("can't get the reformatted node %v: %v", nk, err)
+				return nil, fmt.Errorf("can't get the reformatted node (version=%d, nonce=0) from original (version=%d, nonce=1): %v", nKey.version, nKey.version, err)
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between e64d157 and 9ea8b0b.

📒 Files selected for processing (2)
  • mutable_tree_test.go (1 hunks)
  • nodedb.go (1 hunks)
🔇 Additional comments (2)
nodedb.go (2)

162-175: LGTM! The implementation correctly handles reformatted root nodes.

The added code successfully addresses the issue with reformatted root nodes by:

  1. Checking for nil buffer and non-legacy nodes
  2. Attempting to retrieve nodes with nonce 0 when nonce 1 isn't found
  3. Providing clear error messages for debugging

This change aligns well with the PR objectives of fixing access to reformatted root nodes.


162-175: Verify edge cases in the implementation.

Let's verify the following aspects:

  1. Legacy node handling remains unaffected
  2. Error handling for reformatted nodes
  3. Version consistency across node references
✅ Verification successful

Let me gather more information to verify the implementation thoroughly.


Based on the gathered information, I can now provide the final response:

Implementation handles edge cases appropriately

The verification confirms:

  1. Legacy nodes are handled separately through the isLegcyNode flag, with no interference from the reformatting logic
  2. Error handling is consistent across node retrievals, with specific error messages for reformatted nodes
  3. NodeKey structure properly maintains version and nonce fields, ensuring version consistency

The implementation correctly handles the edge case of reformatted nodes (nonce=1) by falling back to the original version (nonce=0) while maintaining proper error handling and version consistency.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the implementation handles edge cases correctly

# Check if there are any legacy nodes that might be affected
echo "Checking for legacy nodes..."
ast-grep --pattern 'isLegacy == true && nonce == 1'

# Check error handling patterns
echo "Checking error handling consistency..."
rg "can't get (the reformatted )?node" -A 2

# Check version handling in node references
echo "Checking version handling..."
ast-grep --pattern 'nodeKey.version = $version'

Length of output: 617


Script:

#!/bin/bash
# Let's check the complete context around node handling and versioning

# Check how NodeKey is used in the codebase
echo "Checking NodeKey usage patterns..."
ast-grep --pattern 'type NodeKey struct {
  $$$
}'

# Look for nonce handling patterns
echo "Checking nonce handling..."
ast-grep --pattern 'nonce: $expr'

# Check for version handling in GetNodeKey
echo "Checking GetNodeKey implementation..."
ast-grep --pattern 'func GetNodeKey($$$) {
  $$$
}'

# Look for other error cases in node retrieval
echo "Checking node retrieval patterns..."
rg "ndb\.db\.Get\(.*\)" -A 2

Length of output: 1725

Comment on lines +1485 to +1507
func TestReferenceRootPruning(t *testing.T) {
memDB := dbm.NewMemDB()
tree := NewMutableTree(memDB, 0, true, NewNopLogger())

_, err := tree.Set([]byte("foo"), []byte("bar"))
require.NoError(t, err)
_, _, err = tree.SaveVersion()
require.NoError(t, err)

_, _, err = tree.SaveVersion()
require.NoError(t, err)

_, err = tree.Set([]byte("foo1"), []byte("bar"))
require.NoError(t, err)
_, _, err = tree.SaveVersion()
require.NoError(t, err)

err = tree.DeleteVersionsTo(1)
require.NoError(t, err)

_, err = tree.Set([]byte("foo"), []byte("bar*"))
require.NoError(t, err)
}
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Enhance test coverage and documentation for root node reformatting.

The test needs improvements in the following areas:

  1. Add assertions to verify:

    • The final state after setting the new value
    • The root node reformatting (nodeKey change from (version, 1) to (version, 0))
    • References from subsequent versions to the reformatted node
  2. Add documentation to explain the test's purpose and steps

Here's the suggested implementation:

 func TestReferenceRootPruning(t *testing.T) {
+    // This test verifies that when a root node is reformatted during pruning
+    // (changing its nodeKey from (version, 1) to (version, 0)):
+    // 1. The reformatted node remains accessible
+    // 2. References from subsequent versions are correctly resolved
+
     memDB := dbm.NewMemDB()
     tree := NewMutableTree(memDB, 0, true, NewNopLogger())

     // Set initial key-value pair
     _, err := tree.Set([]byte("foo"), []byte("bar"))
     require.NoError(t, err)
     _, ver1, err := tree.SaveVersion()
     require.NoError(t, err)

     // Save empty version to create a reference to the root
     _, ver2, err := tree.SaveVersion()
     require.NoError(t, err)

     // Add another key-value pair
     _, err = tree.Set([]byte("foo1"), []byte("bar"))
     require.NoError(t, err)
     _, ver3, err := tree.SaveVersion()
     require.NoError(t, err)

     // Delete versions up to ver1, triggering root node reformatting
     err = tree.DeleteVersionsTo(ver1)
     require.NoError(t, err)

     // Verify the root node was reformatted
+    rootKey := GetRootKey(ver1)
+    node, err := tree.ndb.GetNode(rootKey)
+    require.NoError(t, err)
+    require.Equal(t, int64(0), node.nodeKey.index, "root node should be reformatted with index 0")

     // Set new value and verify it's accessible
     _, err = tree.Set([]byte("foo"), []byte("bar*"))
     require.NoError(t, err)
+    _, ver4, err := tree.SaveVersion()
+    require.NoError(t, err)
+
+    // Verify the value is correctly set
+    val, err := tree.Get([]byte("foo"))
+    require.NoError(t, err)
+    require.Equal(t, []byte("bar*"), val, "new value should be accessible after root reformatting")
+
+    // Verify references from subsequent versions
+    for _, version := range []int64{ver2, ver3, ver4} {
+        _, err := tree.GetVersioned([]byte("foo"), version)
+        require.NoError(t, err, "value should be accessible in version %d", version)
+    }
 }
📝 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. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
func TestReferenceRootPruning(t *testing.T) {
memDB := dbm.NewMemDB()
tree := NewMutableTree(memDB, 0, true, NewNopLogger())
_, err := tree.Set([]byte("foo"), []byte("bar"))
require.NoError(t, err)
_, _, err = tree.SaveVersion()
require.NoError(t, err)
_, _, err = tree.SaveVersion()
require.NoError(t, err)
_, err = tree.Set([]byte("foo1"), []byte("bar"))
require.NoError(t, err)
_, _, err = tree.SaveVersion()
require.NoError(t, err)
err = tree.DeleteVersionsTo(1)
require.NoError(t, err)
_, err = tree.Set([]byte("foo"), []byte("bar*"))
require.NoError(t, err)
}
func TestReferenceRootPruning(t *testing.T) {
// This test verifies that when a root node is reformatted during pruning
// (changing its nodeKey from (version, 1) to (version, 0)):
// 1. The reformatted node remains accessible
// 2. References from subsequent versions are correctly resolved
memDB := dbm.NewMemDB()
tree := NewMutableTree(memDB, 0, true, NewNopLogger())
// Set initial key-value pair
_, err := tree.Set([]byte("foo"), []byte("bar"))
require.NoError(t, err)
_, ver1, err := tree.SaveVersion()
require.NoError(t, err)
// Save empty version to create a reference to the root
_, ver2, err := tree.SaveVersion()
require.NoError(t, err)
// Add another key-value pair
_, err = tree.Set([]byte("foo1"), []byte("bar"))
require.NoError(t, err)
_, ver3, err := tree.SaveVersion()
require.NoError(t, err)
// Delete versions up to ver1, triggering root node reformatting
err = tree.DeleteVersionsTo(ver1)
require.NoError(t, err)
// Verify the root node was reformatted
rootKey := GetRootKey(ver1)
node, err := tree.ndb.GetNode(rootKey)
require.NoError(t, err)
require.Equal(t, int64(0), node.nodeKey.index, "root node should be reformatted with index 0")
// Set new value and verify it's accessible
_, err = tree.Set([]byte("foo"), []byte("bar*"))
require.NoError(t, err)
_, ver4, err := tree.SaveVersion()
require.NoError(t, err)
// Verify the value is correctly set
val, err := tree.Get([]byte("foo"))
require.NoError(t, err)
require.Equal(t, []byte("bar*"), val, "new value should be accessible after root reformatting")
// Verify references from subsequent versions
for _, version := range []int64{ver2, ver3, ver4} {
_, err := tree.GetVersioned([]byte("foo"), version)
require.NoError(t, err, "value should be accessible in version %d", version)
}
}

@yihuang
Copy link
Collaborator

yihuang commented Nov 21, 2024

do you need to add a changelog?

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (1)
CHANGELOG.md (1)

7-7: Enhance the changelog entry with more details about the bug and its impact.

While the entry follows the correct format, it could be more descriptive about the actual problem being fixed. Consider expanding it to:

- - [#1007](https://github.com/cosmos/iavl/pull/1007) Add the extra check for the reformatted root node in `GetNode`
+ - [#1007](https://github.com/cosmos/iavl/pull/1007) Fix root node accessibility after reformatting by adding an extra check in `GetNode`. Previously, reformatted root nodes with changed nodeKey from (version, 1) to (version, 0) became inaccessible, affecting both the reformatted version and subsequent versions referencing it.
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 9ea8b0b and da029aa.

📒 Files selected for processing (1)
  • CHANGELOG.md (1 hunks)
🔇 Additional comments (1)
CHANGELOG.md (1)

Line range hint 1-8: LGTM! The changelog structure is consistent.

The entry is correctly placed in the "Bug Fixes" section under "Unreleased", following the established changelog structure.

@yihuang
Copy link
Collaborator

yihuang commented Nov 25, 2024

shall we merge?

@tac0turtle tac0turtle merged commit cf74234 into master Nov 26, 2024
7 of 8 checks passed
@tac0turtle tac0turtle deleted the pruning/reformatted_node_get branch November 26, 2024 09:12
@tac0turtle
Copy link
Member

tac0turtle commented Nov 26, 2024

@Mergifyio backport release/v1.2.x

@tac0turtle
Copy link
Member

@Mergifyio backport release/v1.3.x

Copy link
Contributor

mergify bot commented Nov 26, 2024

backport release/v1.2.x

✅ Backports have been created

Copy link
Contributor

mergify bot commented Nov 26, 2024

backport release/v1.3.x

✅ Backports have been created

mergify bot pushed a commit that referenced this pull request Nov 26, 2024
(cherry picked from commit cf74234)

# Conflicts:
#	mutable_tree_test.go
mergify bot pushed a commit that referenced this pull request Nov 26, 2024
tac0turtle added a commit that referenced this pull request Nov 26, 2024
…1007) (#1009)

Co-authored-by: cool-developer <51834436+cool-develope@users.noreply.github.com>
Co-authored-by: Marko Baricevic <marbar3778@yahoo.com>
tac0turtle pushed a commit that referenced this pull request Nov 26, 2024
…1007) (#1010)

Co-authored-by: cool-developer <51834436+cool-develope@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants