Skip to content

Commit

Permalink
add interval when getting ErrSnapshotStale
Browse files Browse the repository at this point in the history
  • Loading branch information
j75689 committed Apr 27, 2022
1 parent b56c19c commit 5a254c4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
8 changes: 8 additions & 0 deletions core/state/state_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,14 @@ func (s *StateObject) GetCommittedState(db Database, key common.Hash) common.Has
}
// ErrSnapshotStale may occur due to diff layers in the update, so we should try again in noTrie mode.
if s.db.NoTrie() && err != nil && errors.Is(err, snapshot.ErrSnapshotStale) {
// This error occurs when statedb.snaps.Cap changes the state of the merged difflayer
// to stale during the refresh of the difflayer, indicating that it is about to be discarded.
// Since the difflayer is refreshed in parallel,
// there is a small chance that the difflayer of the stale will be read while reading,
// resulting in an empty array being returned here.
// Therefore, noTrie mode must retry here,
// and add a time interval when retrying to avoid stacking too much and causing OOM.
time.Sleep(snapshotStaleRetryInterval)
return s.GetCommittedState(db, key)
}
// If snapshot unavailable or reading from it failed, load from the database
Expand Down
13 changes: 12 additions & 1 deletion core/state/statedb.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ import (
"github.com/ethereum/go-ethereum/trie"
)

const defaultNumOfSlots = 100
const (
defaultNumOfSlots = 100
snapshotStaleRetryInterval = time.Millisecond * 100
)

type revision struct {
id int
Expand Down Expand Up @@ -677,6 +680,14 @@ func (s *StateDB) getDeletedStateObject(addr common.Address) *StateObject {

// ErrSnapshotStale may occur due to diff layers in the update, so we should try again in noTrie mode.
if s.NoTrie() && err != nil && errors.Is(err, snapshot.ErrSnapshotStale) {
// This error occurs when statedb.snaps.Cap changes the state of the merged difflayer
// to stale during the refresh of the difflayer, indicating that it is about to be discarded.
// Since the difflayer is refreshed in parallel,
// there is a small chance that the difflayer of the stale will be read while reading,
// resulting in an empty array being returned here.
// Therefore, noTrie mode must retry here,
// and add a time interval when retrying to avoid stacking too much and causing OOM.
time.Sleep(snapshotStaleRetryInterval)
return s.getDeletedStateObject(addr)
}

Expand Down

0 comments on commit 5a254c4

Please sign in to comment.