-
Notifications
You must be signed in to change notification settings - Fork 325
feat: add adr-001 for node key refactoring #608
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
Changes from 27 commits
4f9a004
8646a9a
6e5b081
4ee7804
9468ee2
0c2d610
1459a18
d8d0bf9
88885f1
5272de4
006d76c
7cc7280
4e6044f
0bf7486
a0dcc0e
ee11dff
d9f7d2e
10184ac
5f94844
85a90e7
8fb87b6
6bbf7f9
204d881
f743311
bf85d92
b064ede
6095bc4
7873267
fb0f182
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| # ADR ADR-001: Node Key Refactoring | ||
|
|
||
| ## Changelog | ||
|
|
||
| - 2022-10-31: First draft | ||
|
|
||
| ## Status | ||
|
|
||
| Proposed | ||
|
|
||
| ## Context | ||
|
|
||
| The original key format of IAVL nodes is a hash of the node. It does not take advantage of data locality on LSM-Tree. Nodes are stored with the random hash value, so it increases the number of compactions and makes it difficult to find the node. The new key format will take advantage of data locality in the LSM tree and reduce the number of compactions. | ||
|
|
||
| The `orphans` are used to manage node removal in the current design and allow the deletion of removed nodes for the specific version from the disk through the `DeleteVersion` API. It needs to track every time when updating the tree and also requires extra storage to store `orphans`. But there are only 2 use cases for `DeleteVersion`: | ||
|
|
||
| 1. Rollback of the tree to a previous version | ||
| 2. Remove unnecessary old nodes | ||
|
|
||
| ## Decision | ||
|
|
||
| - Use the version and the local nonce as a node key like `bigendian(version) | bigendian(nonce)` format. Here the `nonce` is a local sequence id for the same version. | ||
| - Store the children node keys (`leftNodeKey` and `rightNodeKey`) in the node body. | ||
| - Remove the `version` field from node body writes. | ||
| - Remove the `leftHash` and `rightHash` fields, and instead store `hash` field in the node body. | ||
| - Remove the `orphans` completely from both tree and storage. | ||
|
|
||
| New node structure | ||
|
|
||
| ```go | ||
| type NodeKey struct { | ||
| version int64 | ||
| nonce int32 | ||
| } | ||
|
|
||
| type Node struct { | ||
| key []byte | ||
| value []byte | ||
| hash []byte // keep it in the storage instead of leftHash and rightHash | ||
| nodeKey *NodeKey // new field, the key in the storage | ||
| leftNodeKey *NodeKey // new field, need to store in the storage | ||
| rightNodeKey *NodeKey // new field, need to store in the storage | ||
| leftNode *Node | ||
| rightNode *Node | ||
| size int64 | ||
| leftNode *Node | ||
| rightNode *Node | ||
| subtreeHeight int8 | ||
| } | ||
| ``` | ||
|
|
||
| New tree structure | ||
|
|
||
| ```go | ||
| type MutableTree struct { | ||
| *ImmutableTree // The current, working tree. | ||
| lastSaved *ImmutableTree // The most recently saved tree. | ||
| unsavedFastNodeAdditions map[string]*fastnode.Node // FastNodes that have not yet been saved to disk | ||
| unsavedFastNodeRemovals map[string]interface{} // FastNodes that have not yet been removed from disk | ||
| ndb *nodeDB | ||
| skipFastStorageUpgrade bool // If true, the tree will work like no fast storage and always not upgrade fast storage | ||
|
|
||
| mtx sync.Mutex | ||
| } | ||
| ``` | ||
|
|
||
| We will assign the `nodeKey` when saving the current version in `SaveVersion`. It will reduce unnecessary checks in CRUD operations of the tree and keep sorted the order of insertion in the LSM tree. | ||
|
|
||
| ### Migration | ||
|
|
||
| We can migrate nodes through the following steps: | ||
|
|
||
|
Contributor
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.
Contributor
Author
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.
|
||
| - Export the snapshot of the tree from the original version. | ||
| - Import the snapshot to the new version. | ||
| - Track the nonce for the same version using int32 array of the version length. | ||
| - Assign the `nodeKey` when saving the node. | ||
|
|
||
| ### Pruning | ||
|
Contributor
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. this section needs more details. |
||
|
|
||
| We assume keeping only the range versions of `fromVersion` to `toVersion`. Refer to [this issue](https://github.com/cosmos/cosmos-sdk/issues/12989). | ||
|
cool-develope marked this conversation as resolved.
Outdated
|
||
|
|
||
| Here we are introducing a new way how to get orphaned nodes which remove in the `n+1`th version updates. | ||
|
tac0turtle marked this conversation as resolved.
Outdated
|
||
|
|
||
| - Traverse the tree in-order way based on the root of `n+1`th version. | ||
| - If we visit the lower version node, pick the node and don't visit further deeply. Pay attention to the order of these nodes. | ||
| - Traverse the tree in-order way based on the root of `n`th version. | ||
|
tac0turtle marked this conversation as resolved.
|
||
| - Iterate the tree until meet the first node among the above nodes(stack) and delete all visited nodes so far from `n`th tree. | ||
| - Pop the first node from the stack and iterate again. | ||
|
|
||
| If we assume `1 to (n-1)` versions already been removed, when we want to remove the `n`th version, we can just remove the above orphaned nodes. | ||
|
|
||
| ### Rollback | ||
|
|
||
| When we want to rollback to the specific version `n` | ||
|
|
||
| - Iterate the version from `n+1`. | ||
| - Traverse key-value through `traversePrefix` with `prefix=bigendian(version)`. | ||
| - Remove all iterated nodes. | ||
|
|
||
| ## Consequences | ||
|
cool-develope marked this conversation as resolved.
|
||
|
|
||
| ### Positive | ||
|
|
||
| Using the version and the path, we take advantage of data locality in the LSM tree. Since we commit the sorted data, it can reduce compactions and makes it easy to find the key. Also, it can reduce the key and node size in the storage. | ||
|
cool-develope marked this conversation as resolved.
Outdated
cool-develope marked this conversation as resolved.
Outdated
|
||
|
|
||
| ``` | ||
| # node body | ||
|
|
||
| add `hash`: +32 byte | ||
| add `leftNodeKey`, `rightNodeKey`: max (8 + 4) * 2 = +24 byte | ||
| remove `leftHash`, `rightHash`: -64 byte | ||
| remove `version`: max -8 byte | ||
| ------------------------------------------------------------ | ||
| total save 16 byte | ||
|
|
||
| # node key | ||
|
|
||
| remove `hash`: -32 byte | ||
| add `version|nonce`: +12 byte | ||
| ------------------------------------ | ||
| total save 20 byte | ||
| ``` | ||
|
|
||
| Removing orphans also provides performance improvements including memory and storage saving. | ||
|
cool-develope marked this conversation as resolved.
Outdated
|
||
|
|
||
| ### Negative | ||
|
|
||
| The `Update` operation will require extra DB access because we need to take children to calculate the hash of updated nodes. | ||
|
Contributor
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. Do you mean to say that it requires extra DB reads? Why is this not needed in Set and Remove?
Contributor
Author
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. because
Contributor
Author
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. so we are getting
Contributor
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. the trade off here is storage saving on per node basis, right? maybe we mention this as a tradeoff
cool-develope marked this conversation as resolved.
Outdated
|
||
| It doesn't require more access in other cases including `Set`, `Remove`, and `Proof`. | ||
|
cool-develope marked this conversation as resolved.
Outdated
|
||
|
|
||
| It is impossible to remove the individual version. The new design requires more restrict pruning strategies. | ||
|
Contributor
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. can you elaborate on this?
Contributor
Author
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. I think it is enough, it will remove 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. Even if removing
Contributor
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. lets edit to include this.
cool-develope marked this conversation as resolved.
Outdated
|
||
|
|
||
| When importing the tree, it may require more memory because of int32 array of the version length. We will introduce the new importing strategy to reduce the memory usage. | ||
|
|
||
| ## References | ||
|
|
||
| - https://github.com/cosmos/iavl/issues/548 | ||
| - https://github.com/cosmos/iavl/issues/137 | ||
| - https://github.com/cosmos/iavl/issues/571 | ||
| - https://github.com/cosmos/cosmos-sdk/issues/12989 | ||
Uh oh!
There was an error while loading. Please reload this page.