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

feat: allow saving version 0 #1002

Merged
merged 9 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions mutable_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type MutableTree struct {
unsavedFastNodeRemovals *sync.Map // 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
initialVersionSet bool

mtx sync.Mutex
}
Expand All @@ -62,6 +63,7 @@ func NewMutableTree(db corestore.KVStoreWithBatch, cacheSize int, skipFastStorag
unsavedFastNodeRemovals: &sync.Map{},
ndb: ndb,
skipFastStorageUpgrade: skipFastStorageUpgrade,
initialVersionSet: opts.initialVersionSet,
}
}

Expand Down Expand Up @@ -146,8 +148,9 @@ func (tree *MutableTree) WorkingHash() []byte {

func (tree *MutableTree) WorkingVersion() int64 {
version := tree.version + 1
if version == 1 && tree.ndb.opts.InitialVersion > 0 {
if version == 1 && tree.initialVersionSet {
version = int64(tree.ndb.opts.InitialVersion)
tree.initialVersionSet = false
}
return version
kocubinski marked this conversation as resolved.
Show resolved Hide resolved
}
Expand Down Expand Up @@ -459,11 +462,11 @@ func (tree *MutableTree) LoadVersion(targetVersion int64) (int64, error) {
tree.ndb.opts.InitialVersion, firstVersion)
}

if latestVersion < targetVersion {
if latestVersion >= 0 && latestVersion < targetVersion {
return latestVersion, fmt.Errorf("wanted to load target %d but only found up to %d", targetVersion, latestVersion)
}

if firstVersion == 0 {
if firstVersion <= 0 {
if targetVersion <= 0 {
if !tree.skipFastStorageUpgrade {
tree.mtx.Lock()
Expand Down Expand Up @@ -871,6 +874,7 @@ func (tree *MutableTree) saveFastNodeRemovals() error {
// and is otherwise ignored.
func (tree *MutableTree) SetInitialVersion(version uint64) {
tree.ndb.opts.InitialVersion = version
tree.initialVersionSet = true
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Consider adding mutex protection for thread safety

The SetInitialVersion method modifies shared state (initialVersionSet) without synchronization. Consider protecting this operation with the existing mtx mutex to prevent potential race conditions in concurrent usage scenarios.

Apply this diff to add mutex protection:

 func (tree *MutableTree) SetInitialVersion(version uint64) {
+    tree.mtx.Lock()
+    defer tree.mtx.Unlock()
     tree.ndb.opts.InitialVersion = version
     tree.initialVersionSet = true
 }
📝 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
tree.initialVersionSet = true
func (tree *MutableTree) SetInitialVersion(version uint64) {
tree.mtx.Lock()
defer tree.mtx.Unlock()
tree.ndb.opts.InitialVersion = version
tree.initialVersionSet = true
}

}

// DeleteVersionsTo removes versions upto the given version from the MutableTree.
Expand Down
2 changes: 1 addition & 1 deletion nodedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,7 @@ func (ndb *nodeDB) getLatestVersion() (int64, error) {
return latestVersion, nil
}

return 0, nil
return -1, nil
}

func (ndb *nodeDB) resetLatestVersion(version int64) {
Expand Down
3 changes: 3 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ type Options struct {

// AsyncPruning is a flag to enable async pruning
AsyncPruning bool

initialVersionSet bool
}

// DefaultOptions returns the default options for IAVL.
Expand All @@ -105,6 +107,7 @@ func SyncOption(sync bool) Option {
func InitialVersionOption(iv uint64) Option {
return func(opts *Options) {
opts.InitialVersion = iv
opts.initialVersionSet = true
}
}

Expand Down
Loading