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 bug fatal error: concurrent map read and map write #23

Merged
merged 1 commit into from
Feb 21, 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
2 changes: 0 additions & 2 deletions mutable_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,6 @@ 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)
}

func (tree *MutableTree) saveFastNodeAdditions() error {
Expand All @@ -810,7 +809,6 @@ 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: 13 additions & 6 deletions nodedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,12 @@ func (ndb *nodeDB) GetNode(hash []byte) *Node {
}

func (ndb *nodeDB) GetFastNode(key []byte) (*FastNode, error) {
ndb.mtx.Lock()
defer ndb.mtx.Unlock()
Comment on lines +154 to +155
Copy link
Member

Choose a reason for hiding this comment

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

Can this be a RLock?

Copy link
Member

Choose a reason for hiding this comment

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

wait why did moving this before hasUpgradedToFastStorage have any impact?

Copy link
Member Author

Choose a reason for hiding this comment

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

storageVersion is still a common resource that might be written and read at the same time

In addition, for readability. Easier to see locks at the top of a method

Copy link
Member Author

Choose a reason for hiding this comment

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

looking into RLock...

Copy link
Member Author

Choose a reason for hiding this comment

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

here we are modifying a common resource. As a result, I don't think RLock could work

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 @@ -233,6 +232,9 @@ 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 @@ -270,6 +272,8 @@ 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 @@ -281,6 +285,7 @@ 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 @@ -435,7 +440,6 @@ 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 @@ -470,10 +474,9 @@ func (ndb *nodeDB) DeleteVersionsFrom(version int64) error {
}

if version <= fastNode.versionLastUpdatedAt {
if err = ndb.batch.Delete(keyWithPrefix); err != nil {
if err := ndb.DeleteFastNode(fastNode.key); err != nil {
return err
}
ndb.uncacheFastNode(key)
}
return nil
})
Expand Down Expand Up @@ -559,6 +562,8 @@ 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 @@ -830,6 +835,7 @@ 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 @@ -839,6 +845,7 @@ 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