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

Snapshot looup lock lookup #2828

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
feat: improve lock
  • Loading branch information
jingjunLi committed Dec 26, 2024
commit a6f8d3f74d98fc08724857a7c0f4e4739b546f25
6 changes: 4 additions & 2 deletions core/state/snapshot/difflayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -528,12 +528,14 @@ func (dl *diffLayer) flatten() snapshot {

// Before actually writing all our data to the parent, first ensure that the
// parent hasn't been 'corrupted' by someone else already flattening into it
if parent.stale.Swap(true) {
if parent.canLookup.Swap(true) {
panic("parent diff layer is stale") // we've flattened into the same parent from two children, boo
}
if parent.canLookup.Swap(true) {

if parent.stale.Swap(true) {
panic("parent diff layer is stale") // we've flattened into the same parent from two children, boo
}

//log.Info("Layer flattening stale", "layer", parent.Root(), "destructs", len(dl.destructSet))
for hash, data := range dl.accountData {
parent.accountData[hash] = data
Expand Down
24 changes: 13 additions & 11 deletions core/state/snapshot/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,6 @@ func (l *Lookup) addLayer(diff *diffLayer) {
lookupAddLayerTimer.UpdateSince(now)
}(time.Now())
layerIDCounter++
l.lock.Lock()
defer l.lock.Unlock()

for accountHash, _ := range diff.accountData {
l.state2LayerRoots[accountHash.String()] = append(l.state2LayerRoots[accountHash.String()], diff)
Expand All @@ -120,8 +118,6 @@ func (l *Lookup) addLayer(diff *diffLayer) {
l.state2LayerRoots[accountHash.String()+storageHash.String()] = append(l.state2LayerRoots[accountHash.String()+storageHash.String()], diff)
}
}

//l.addDescendant(diff)
}

// removeLayer traverses all the dirty state within the given diff layer and
Expand All @@ -131,8 +127,6 @@ func (l *Lookup) removeLayer(diff *diffLayer) error {
lookupRemoveLayerTimer.UpdateSince(now)
}(time.Now())
layerIDRemoveCounter++
l.lock.Lock()
defer l.lock.Unlock()

diffRoot := diff.Root()
for accountHash, _ := range diff.accountData {
Expand Down Expand Up @@ -195,8 +189,6 @@ func (l *Lookup) removeLayer(diff *diffLayer) error {
}
}

//l.removeDescendant(diff)

return nil
}

Expand All @@ -222,8 +214,6 @@ func (l *Lookup) addDescendant(topDiffLayer Snapshot) {
defer func(now time.Time) {
lookupAddDescendantTimer.UpdateSince(now)
}(time.Now())
l.lock.Lock()
defer l.lock.Unlock()

// Link the new layer into the descendents set
for h := range diffAncestors(topDiffLayer) {
Expand All @@ -240,10 +230,22 @@ func (l *Lookup) removeDescendant(bottomDiffLayer Snapshot) {
defer func(now time.Time) {
lookupRemoveDescendantTimer.UpdateSince(now)
}(time.Now())

delete(l.descendants, bottomDiffLayer.Root())
}

func (l *Lookup) AddSnapshot(diff *diffLayer) {
l.lock.Lock()
defer l.lock.Unlock()
l.addLayer(diff)
l.addDescendant(diff)
}

delete(l.descendants, bottomDiffLayer.Root())
func (l *Lookup) RemoveSnapshot(diff *diffLayer) {
l.lock.Lock()
defer l.lock.Unlock()
l.removeLayer(diff)
l.removeDescendant(diff)
}

func (l *Lookup) LookupAccount(accountAddrHash common.Hash, head common.Hash) Snapshot {
Expand Down
9 changes: 3 additions & 6 deletions core/state/snapshot/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,7 @@ func (t *Tree) Update(blockRoot common.Hash, parentRoot common.Hash, accounts ma

t.layers[snap.root] = snap
// update lookup, which in the tree lock guard.
t.lookup.addLayer(snap)
t.lookup.addDescendant(snap)
t.lookup.AddSnapshot(snap)
if t.baseDiff == nil || reflect.ValueOf(t.baseDiff).IsNil() {
t.baseDiff = snap
}
Expand Down Expand Up @@ -486,8 +485,7 @@ func (t *Tree) Cap(root common.Hash, layers int) error {
if !ok {
return
}
t.lookup.removeLayer(diff)
t.lookup.removeDescendant(diff)
t.lookup.RemoveSnapshot(diff)
}
var remove func(root common.Hash, snap snapshot)
remove = func(root common.Hash, snap snapshot) {
Expand Down Expand Up @@ -585,8 +583,7 @@ func (t *Tree) cap(diff *diffLayer, layers int) *diskLayer {
if !ok {
return
}
t.lookup.removeLayer(diff)
t.lookup.removeDescendant(diff)
t.lookup.RemoveSnapshot(diff)
}

//TODO:check it?
Expand Down