-
Notifications
You must be signed in to change notification settings - Fork 275
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: Support concurrency for IAVL and fix Racing conditions #805
Merged
Merged
Changes from all commits
Commits
Show all changes
6 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 |
---|---|---|
|
@@ -4,9 +4,12 @@ import ( | |
"bytes" | ||
"errors" | ||
"sort" | ||
"sync" | ||
|
||
dbm "github.com/cosmos/cosmos-db" | ||
|
||
"github.com/cosmos/iavl/fastnode" | ||
ibytes "github.com/cosmos/iavl/internal/bytes" | ||
) | ||
|
||
var ( | ||
|
@@ -29,14 +32,14 @@ type UnsavedFastIterator struct { | |
fastIterator dbm.Iterator | ||
|
||
nextUnsavedNodeIdx int | ||
unsavedFastNodeAdditions map[string]*fastnode.Node | ||
unsavedFastNodeRemovals map[string]interface{} | ||
unsavedFastNodesToSort [][]byte | ||
unsavedFastNodeAdditions *sync.Map // map[string]*FastNode | ||
unsavedFastNodeRemovals *sync.Map // map[string]interface{} | ||
unsavedFastNodesToSort []string | ||
} | ||
|
||
var _ dbm.Iterator = (*UnsavedFastIterator)(nil) | ||
|
||
func NewUnsavedFastIterator(start, end []byte, ascending bool, ndb *nodeDB, unsavedFastNodeAdditions map[string]*fastnode.Node, unsavedFastNodeRemovals map[string]interface{}) *UnsavedFastIterator { | ||
func NewUnsavedFastIterator(start, end []byte, ascending bool, ndb *nodeDB, unsavedFastNodeAdditions, unsavedFastNodeRemovals *sync.Map) *UnsavedFastIterator { | ||
iter := &UnsavedFastIterator{ | ||
start: start, | ||
end: end, | ||
|
@@ -50,29 +53,6 @@ func NewUnsavedFastIterator(start, end []byte, ascending bool, ndb *nodeDB, unsa | |
fastIterator: NewFastIterator(start, end, ascending, ndb), | ||
} | ||
|
||
// We need to ensure that we iterate over saved and unsaved state in order. | ||
// The strategy is to sort unsaved nodes, the fast node on disk are already sorted. | ||
// Then, we keep a pointer to both the unsaved and saved nodes, and iterate over them in order efficiently. | ||
for _, fastNode := range unsavedFastNodeAdditions { | ||
if start != nil && bytes.Compare(fastNode.GetKey(), start) < 0 { | ||
continue | ||
} | ||
|
||
if end != nil && bytes.Compare(fastNode.GetKey(), end) >= 0 { | ||
continue | ||
} | ||
|
||
iter.unsavedFastNodesToSort = append(iter.unsavedFastNodesToSort, fastNode.GetKey()) | ||
} | ||
|
||
sort.Slice(iter.unsavedFastNodesToSort, func(i, j int) bool { | ||
cmp := bytes.Compare(iter.unsavedFastNodesToSort[i], iter.unsavedFastNodesToSort[j]) | ||
if ascending { | ||
return cmp < 0 | ||
} | ||
return cmp > 0 | ||
}) | ||
|
||
if iter.ndb == nil { | ||
iter.err = errFastIteratorNilNdbGiven | ||
iter.valid = false | ||
|
@@ -90,8 +70,34 @@ func NewUnsavedFastIterator(start, end []byte, ascending bool, ndb *nodeDB, unsa | |
iter.valid = false | ||
return iter | ||
} | ||
// We need to ensure that we iterate over saved and unsaved state in order. | ||
// The strategy is to sort unsaved nodes, the fast node on disk are already sorted. | ||
// Then, we keep a pointer to both the unsaved and saved nodes, and iterate over them in order efficiently. | ||
unsavedFastNodeAdditions.Range(func(k, v interface{}) bool { | ||
fastNode := v.(*fastnode.Node) | ||
|
||
if start != nil && bytes.Compare(fastNode.GetKey(), start) < 0 { | ||
return true | ||
} | ||
|
||
if end != nil && bytes.Compare(fastNode.GetKey(), end) >= 0 { | ||
return true | ||
} | ||
|
||
// convert key to bytes. Type conversion failure should not happen in practice | ||
iter.unsavedFastNodesToSort = append(iter.unsavedFastNodesToSort, k.(string)) | ||
|
||
// Move to the first elemenet | ||
return true | ||
}) | ||
|
||
sort.Slice(iter.unsavedFastNodesToSort, func(i, j int) bool { | ||
if ascending { | ||
return iter.unsavedFastNodesToSort[i] < iter.unsavedFastNodesToSort[j] | ||
} | ||
return iter.unsavedFastNodesToSort[i] > iter.unsavedFastNodesToSort[j] | ||
}) | ||
|
||
// Move to the first element | ||
iter.Next() | ||
|
||
return iter | ||
|
@@ -134,31 +140,31 @@ func (iter *UnsavedFastIterator) Next() { | |
return | ||
} | ||
|
||
diskKeyStr := iter.fastIterator.Key() | ||
diskKey := iter.fastIterator.Key() | ||
diskKeyStr := ibytes.UnsafeBytesToStr(diskKey) | ||
if iter.fastIterator.Valid() && iter.nextUnsavedNodeIdx < len(iter.unsavedFastNodesToSort) { | ||
|
||
if iter.unsavedFastNodeRemovals[string(diskKeyStr)] != nil { | ||
value, ok := iter.unsavedFastNodeRemovals.Load(diskKeyStr) | ||
if ok && value != nil { | ||
// If next fast node from disk is to be removed, skip it. | ||
iter.fastIterator.Next() | ||
iter.Next() | ||
return | ||
} | ||
|
||
nextUnsavedKey := iter.unsavedFastNodesToSort[iter.nextUnsavedNodeIdx] | ||
nextUnsavedNode := iter.unsavedFastNodeAdditions[string(nextUnsavedKey)] | ||
nextUnsavedNodeVal, _ := iter.unsavedFastNodeAdditions.Load(nextUnsavedKey) | ||
nextUnsavedNode := nextUnsavedNodeVal.(*fastnode.Node) | ||
|
||
var isUnsavedNext bool | ||
cmp := bytes.Compare(diskKeyStr, nextUnsavedKey) | ||
if iter.ascending { | ||
isUnsavedNext = cmp >= 0 | ||
isUnsavedNext = diskKeyStr >= nextUnsavedKey | ||
} else { | ||
isUnsavedNext = cmp <= 0 | ||
isUnsavedNext = diskKeyStr <= nextUnsavedKey | ||
} | ||
|
||
if isUnsavedNext { | ||
// Unsaved node is next | ||
|
||
if cmp == 0 { | ||
if diskKeyStr == nextUnsavedKey { | ||
// Unsaved update prevails over saved copy so we skip the copy from disk | ||
iter.fastIterator.Next() | ||
} | ||
|
@@ -179,7 +185,8 @@ func (iter *UnsavedFastIterator) Next() { | |
|
||
// if only nodes on disk are left, we return them | ||
if iter.fastIterator.Valid() { | ||
if iter.unsavedFastNodeRemovals[string(diskKeyStr)] != nil { | ||
value, ok := iter.unsavedFastNodeRemovals.Load(diskKeyStr) | ||
if ok && value != nil { | ||
// If next fast node from disk is to be removed, skip it. | ||
iter.fastIterator.Next() | ||
iter.Next() | ||
|
@@ -196,7 +203,8 @@ func (iter *UnsavedFastIterator) Next() { | |
// if only unsaved nodes are left, we can just iterate | ||
if iter.nextUnsavedNodeIdx < len(iter.unsavedFastNodesToSort) { | ||
nextUnsavedKey := iter.unsavedFastNodesToSort[iter.nextUnsavedNodeIdx] | ||
nextUnsavedNode := iter.unsavedFastNodeAdditions[string(nextUnsavedKey)] | ||
nextUnsavedNodeVal, _ := iter.unsavedFastNodeAdditions.Load(nextUnsavedKey) | ||
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. Might be worthwhile checking second return here as well |
||
nextUnsavedNode := nextUnsavedNodeVal.(*fastnode.Node) | ||
|
||
iter.nextKey = nextUnsavedNode.GetKey() | ||
iter.nextVal = nextUnsavedNode.GetValue() | ||
|
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.
We should probably check whether the second return was set to true before proceeding