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: iavl live migration of orphan nodes #866

Closed
wants to merge 26 commits into from

Conversation

czarcas7ic
Copy link
Contributor

@czarcas7ic czarcas7ic commented Dec 26, 2023

This is cherry picks of work from both John Reynolds and Dev to ensure main stays up to date with the iavl v1.0.0 branch we will be using in v21 of Osmosis.

Previously, we would block chain progress on pruning orphan nodes. For a chain like Osmosis, this took upwards of 2 hours. This change allows chain progress to continue, while pruning orphan nodes synchronously.

As a drive by change, this also fixes the golang linter in this repo.

@czarcas7ic czarcas7ic closed this Dec 26, 2023
@czarcas7ic czarcas7ic reopened this Dec 26, 2023
Comment on lines -15 to -22
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
- name: Check out repository code
uses: actions/checkout@v4
- name: 🐿 Setup Golang
uses: actions/setup-go@v4
with:
go-version: '^1.20.0'
go-version: 1.21
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.51.2
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a drive by change, the linter was not working on any open PR

@czarcas7ic czarcas7ic changed the title fix: iavl live migration fix: iavl live migration of orphan nodes Dec 26, 2023
@czarcas7ic czarcas7ic marked this pull request as ready for review December 26, 2023 23:46
@czarcas7ic czarcas7ic requested a review from a team as a code owner December 26, 2023 23:46
Copy link

coderabbitai bot commented Dec 26, 2023

Walkthrough

The project has undergone a significant update focused on improving performance, code organization, and maintainability. The Golang version was upgraded, and a custom linting command was introduced. Thread safety in BatchWithFlusher was enhanced with mutex locks. Comments were added to clarify the immutability of certain function inputs. The encoding layer received optimizations and new functionalities. A new FastPrefixFormatter type was introduced for efficient key manipulation. Node structure was refined for better child node handling, and the database management code was restructured with new mutexes, deletion methods, and more efficient key handling.

Changes

File(s) Change Summary
.github/workflows/lint.yml Updated Golang to 1.21, switched to custom lint command.
batch.go Added mutex for thread safety in BatchWithFlusher.
fastnode/fast_node.go, internal/encoding/encoding.go Added immutability comments; encoding.go also includes new functions and simplifications.
keyformat/.../prefix_formatter.go Introduced FastPrefixFormatter for key operations.
mutable_tree.go, node.go Refactored methods for leaf and child node handling.
nodedb.go Overhauled key formatting, added mutexes and new deletion methods, optimized key iteration.

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 with CodeRabbit Bot (@coderabbitai)

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

  • You can tag CodeRabbit on specific lines of code or entire files in the PR by tagging @coderabbitai in a comment. Examples:
    • @coderabbitai generate unit tests for this file
    • @coderabbitai modularize this function
  • You can tag @coderabbitai in a PR comment and ask questions about the PR and the codebase. Examples:
    • @coderabbitai gather interesting statistics about this repository and render them in a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai generate unit tests for the src/utils.ts file.

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.

Comment on lines +326 to +348
// cloneNoChildFetch clones a node without fetching its children.
func (node *Node) cloneNoChildFetch() (*Node, error) {
if node.isLeaf() {
return nil, ErrCloneLeafNode
}
// match compatibility with old by clearing original node's pointer ref's. I don't really get why this is needed.
var leftNode, rightNode *Node
if node.nodeKey != nil {
leftNode, rightNode = node.leftNode, node.rightNode
node.leftNode, node.rightNode = nil, nil
}
return &Node{
key: node.key,
subtreeHeight: node.subtreeHeight,
size: node.size,
hash: nil,
nodeKey: nil,
leftNode: leftNode,
rightNode: rightNode,
leftNodeKey: node.leftNodeKey,
rightNodeKey: node.rightNodeKey,
}, nil
}
Copy link

Choose a reason for hiding this comment

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

The cloneNoChildFetch method correctly clones a node without fetching its children, which can be useful for performance optimization. However, the comment on line 331 about clearing the original node's pointer references is unclear and should be explained or removed if it's not relevant.

- // match compatibility with old by clearing original node's pointer ref's. I don't really get why this is needed.
+ // Clearing the original node's pointer references to avoid unintended side effects during cloning.

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
// cloneNoChildFetch clones a node without fetching its children.
func (node *Node) cloneNoChildFetch() (*Node, error) {
if node.isLeaf() {
return nil, ErrCloneLeafNode
}
// match compatibility with old by clearing original node's pointer ref's. I don't really get why this is needed.
var leftNode, rightNode *Node
if node.nodeKey != nil {
leftNode, rightNode = node.leftNode, node.rightNode
node.leftNode, node.rightNode = nil, nil
}
return &Node{
key: node.key,
subtreeHeight: node.subtreeHeight,
size: node.size,
hash: nil,
nodeKey: nil,
leftNode: leftNode,
rightNode: rightNode,
leftNodeKey: node.leftNodeKey,
rightNodeKey: node.rightNodeKey,
}, nil
}
// cloneNoChildFetch clones a node without fetching its children.
func (node *Node) cloneNoChildFetch() (*Node, error) {
if node.isLeaf() {
return nil, ErrCloneLeafNode
}
// Clearing the original node's pointer references to avoid unintended side effects during cloning.
var leftNode, rightNode *Node
if node.nodeKey != nil {
leftNode, rightNode = node.leftNode, node.rightNode
node.leftNode, node.rightNode = nil, nil
}
return &Node{
key: node.key,
subtreeHeight: node.subtreeHeight,
size: node.size,
hash: nil,
nodeKey: nil,
leftNode: leftNode,
rightNode: rightNode,
leftNodeKey: node.leftNodeKey,
rightNodeKey: node.rightNodeKey,
}, nil
}

Comment on lines 673 to 679
return fmt.Errorf("writing mode, %w", err)
}
if mode&ModeLegacyLeftNode != 0 { // legacy leftNodeKey
err = encoding.EncodeBytes(w, node.leftNodeKey)
err = encoding.Encode32BytesHash(w, node.leftNodeKey)
if err != nil {
return fmt.Errorf("writing the legacy left node key, %w", err)
}
Copy link

Choose a reason for hiding this comment

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

The update to the writeBytes method to encode the leftNodeKey when in legacy mode is correct. However, the error handling should be consistent with the rest of the codebase. Ensure that the error messages are informative and consistent with the project's error handling conventions.

- return fmt.Errorf("writing the legacy left node key, %w", err)
+ return fmt.Errorf("error writing the legacy left node key: %w", err)

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
return fmt.Errorf("writing mode, %w", err)
}
if mode&ModeLegacyLeftNode != 0 { // legacy leftNodeKey
err = encoding.EncodeBytes(w, node.leftNodeKey)
err = encoding.Encode32BytesHash(w, node.leftNodeKey)
if err != nil {
return fmt.Errorf("writing the legacy left node key, %w", err)
}
return fmt.Errorf("writing mode, %w", err)
}
if mode&ModeLegacyLeftNode != 0 { // legacy leftNodeKey
err = encoding.Encode32BytesHash(w, node.leftNodeKey)
if err != nil {
return fmt.Errorf("error writing the legacy left node key: %w", err)
}

Comment on lines 692 to 698
return ErrRightNodeKeyEmpty
}
if mode&ModeLegacyRightNode != 0 { // legacy rightNodeKey
err = encoding.EncodeBytes(w, node.rightNodeKey)
err = encoding.Encode32BytesHash(w, node.rightNodeKey)
if err != nil {
return fmt.Errorf("writing the legacy right node key, %w", err)
}
Copy link

Choose a reason for hiding this comment

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

Similarly, the update to encode the rightNodeKey when in legacy mode is correct. The same considerations for error handling and message consistency apply here as well.

- return fmt.Errorf("writing the legacy right node key, %w", err)
+ return fmt.Errorf("error writing the legacy right node key: %w", err)

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
return ErrRightNodeKeyEmpty
}
if mode&ModeLegacyRightNode != 0 { // legacy rightNodeKey
err = encoding.EncodeBytes(w, node.rightNodeKey)
err = encoding.Encode32BytesHash(w, node.rightNodeKey)
if err != nil {
return fmt.Errorf("writing the legacy right node key, %w", err)
}
return ErrRightNodeKeyEmpty
}
if mode&ModeLegacyRightNode != 0 { // legacy rightNodeKey
err = encoding.Encode32BytesHash(w, node.rightNodeKey)
if err != nil {
return fmt.Errorf("error writing the legacy right node key: %w", err)
}

@czarcas7ic czarcas7ic marked this pull request as draft December 27, 2023 04:37
@cool-develope
Copy link
Collaborator

Does it include all of #863, #864, #865 changes?

@czarcas7ic
Copy link
Contributor Author

This needs to be reopened with the proper changes just haven't had a moment to do so

@czarcas7ic
Copy link
Contributor Author

Closing in favor of #877 and #876

@czarcas7ic czarcas7ic closed this Jan 31, 2024
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.

3 participants