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

revert #23 (sync access to fast node cache), fix bug related to old height export #33

Merged
merged 2 commits into from
Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 6 additions & 1 deletion mutable_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,10 @@ func (t *MutableTree) Iterate(fn func(key []byte, value []byte) bool) (stopped b
// Iterator returns an iterator over the mutable tree.
// CONTRACT: no updates are made to the tree while an iterator is active.
func (t *MutableTree) Iterator(start, end []byte, ascending bool) dbm.Iterator {
return NewUnsavedFastIterator(start, end, ascending, t.ndb, t.unsavedFastNodeAdditions, t.unsavedFastNodeRemovals)
if t.IsFastCacheEnabled() {
return NewUnsavedFastIterator(start, end, ascending, t.ndb, t.unsavedFastNodeAdditions, t.unsavedFastNodeRemovals)
}
return t.ImmutableTree.Iterator(start, end, ascending)
}

func (tree *MutableTree) set(key []byte, value []byte) (orphans []*Node, updated bool) {
Expand Down Expand Up @@ -789,6 +792,7 @@ func (tree *MutableTree) getUnsavedFastNodeRemovals() map[string]interface{} {
func (tree *MutableTree) addUnsavedAddition(key []byte, node *FastNode) {
delete(tree.unsavedFastNodeRemovals, string(key))
tree.unsavedFastNodeAdditions[string(key)] = node
tree.ndb.cacheFastNode(node)
Copy link
Member

Choose a reason for hiding this comment

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

Why are we caching immediately?

Copy link
Member Author

Choose a reason for hiding this comment

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

This is just a revert of the faulty merged in commit. I will come back to this and address it

}

func (tree *MutableTree) saveFastNodeAdditions() error {
Expand All @@ -809,6 +813,7 @@ func (tree *MutableTree) saveFastNodeAdditions() error {
func (tree *MutableTree) addUnsavedRemoval(key []byte) {
delete(tree.unsavedFastNodeAdditions, string(key))
tree.unsavedFastNodeRemovals[string(key)] = true
tree.ndb.uncacheFastNode(key)
}

func (tree *MutableTree) saveFastNodeRemovals() error {
Expand Down
19 changes: 6 additions & 13 deletions nodedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,13 @@ func (ndb *nodeDB) GetNode(hash []byte) *Node {
}

func (ndb *nodeDB) GetFastNode(key []byte) (*FastNode, error) {
ndb.mtx.Lock()
defer ndb.mtx.Unlock()
if !ndb.hasUpgradedToFastStorage() {
return nil, errors.New("storage version is not fast")
}

ndb.mtx.Lock()
defer ndb.mtx.Unlock()

if len(key) == 0 {
return nil, fmt.Errorf("nodeDB.GetFastNode() requires key, len(key) equals 0")
}
Expand Down Expand Up @@ -232,9 +233,6 @@ func (ndb *nodeDB) SaveFastNodeNoCache(node *FastNode) error {
// 1.1.0-<version of the current live state>. Returns error if storage version is incorrect or on
// db error, nil otherwise. Requires changes to be comitted after to be persisted.
func (ndb *nodeDB) setFastStorageVersionToBatch() error {
ndb.mtx.Lock()
defer ndb.mtx.Unlock()

var newVersion string
if ndb.storageVersion >= fastStorageVersionValue {
// Storage version should be at index 0 and latest fast cache version at index 1
Expand Down Expand Up @@ -272,8 +270,6 @@ func (ndb *nodeDB) hasUpgradedToFastStorage() bool {
// We determine this by checking the version of the live state and the version of the live state when
// latest storage was updated on disk the last time.
func (ndb *nodeDB) shouldForceFastStorageUpgrade() bool {
ndb.mtx.Lock()
defer ndb.mtx.Unlock()
versions := strings.Split(ndb.storageVersion, fastStorageVersionDelimiter)

if len(versions) == 2 {
Expand All @@ -285,7 +281,6 @@ func (ndb *nodeDB) shouldForceFastStorageUpgrade() bool {
}

// SaveNode saves a FastNode to disk.
// CONTRACT: the caller must serizlize access to this method through ndb.mtx.
func (ndb *nodeDB) saveFastNodeUnlocked(node *FastNode, shouldAddToCache bool) error {
if node.key == nil {
return fmt.Errorf("FastNode cannot have a nil value for key")
Expand Down Expand Up @@ -440,6 +435,7 @@ func (ndb *nodeDB) DeleteVersionsFrom(version int64) error {
if err = ndb.batch.Delete(ndb.nodeKey(hash)); err != nil {
return err
}
ndb.uncacheNode(hash)
} else if toVersion >= version-1 {
if err := ndb.batch.Delete(key); err != nil {
return err
Expand Down Expand Up @@ -474,9 +470,10 @@ func (ndb *nodeDB) DeleteVersionsFrom(version int64) error {
}

if version <= fastNode.versionLastUpdatedAt {
if err := ndb.DeleteFastNode(fastNode.key); err != nil {
if err = ndb.batch.Delete(keyWithPrefix); err != nil {
return err
}
ndb.uncacheFastNode(key)
}
return nil
})
Expand Down Expand Up @@ -562,8 +559,6 @@ func (ndb *nodeDB) DeleteVersionsRange(fromVersion, toVersion int64) error {
}

func (ndb *nodeDB) DeleteFastNode(key []byte) error {
ndb.mtx.Lock()
defer ndb.mtx.Unlock()
if err := ndb.batch.Delete(ndb.fastNodeKey(key)); err != nil {
return err
}
Expand Down Expand Up @@ -835,7 +830,6 @@ func (ndb *nodeDB) cacheNode(node *Node) {
}
}

// CONTRACT: the caller must serizlize access to this method through ndb.mtx.
func (ndb *nodeDB) uncacheFastNode(key []byte) {
if elem, ok := ndb.fastNodeCache[string(key)]; ok {
ndb.fastNodeCacheQueue.Remove(elem)
Expand All @@ -845,7 +839,6 @@ func (ndb *nodeDB) uncacheFastNode(key []byte) {

// Add a node to the cache and pop the least recently used node if we've
// reached the cache size limit.
// CONTRACT: the caller must serizlize access to this method through ndb.mtx.
func (ndb *nodeDB) cacheFastNode(node *FastNode) {
elem := ndb.fastNodeCacheQueue.PushBack(node)
ndb.fastNodeCache[string(node.key)] = elem
Expand Down