Skip to content

Commit d97e006

Browse files
authored
Merge pull request #21491 from karalabe/state-sync-leak-fix
core/state, eth, trie: stabilize memory use, fix memory leak
2 parents 856307d + d8da0b3 commit d97e006

File tree

7 files changed

+58
-29
lines changed

7 files changed

+58
-29
lines changed

core/state/statedb.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,7 @@ func (s *StateDB) Commit(deleteEmptyObjects bool) (common.Hash, error) {
847847
// The onleaf func is called _serially_, so we can reuse the same account
848848
// for unmarshalling every time.
849849
var account Account
850-
root, err := s.trie.Commit(func(leaf []byte, parent common.Hash) error {
850+
root, err := s.trie.Commit(func(path []byte, leaf []byte, parent common.Hash) error {
851851
if err := rlp.DecodeBytes(leaf, &account); err != nil {
852852
return nil
853853
}

core/state/sync.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ import (
2828
// NewStateSync create a new state trie download scheduler.
2929
func NewStateSync(root common.Hash, database ethdb.KeyValueReader, bloom *trie.SyncBloom) *trie.Sync {
3030
var syncer *trie.Sync
31-
callback := func(leaf []byte, parent common.Hash) error {
31+
callback := func(path []byte, leaf []byte, parent common.Hash) error {
3232
var obj Account
3333
if err := rlp.Decode(bytes.NewReader(leaf), &obj); err != nil {
3434
return err
3535
}
36-
syncer.AddSubTrie(obj.Root, 64, parent, nil)
37-
syncer.AddCodeEntry(common.BytesToHash(obj.CodeHash), 64, parent)
36+
syncer.AddSubTrie(obj.Root, path, parent, nil)
37+
syncer.AddCodeEntry(common.BytesToHash(obj.CodeHash), path, parent)
3838
return nil
3939
}
4040
syncer = trie.NewSync(root, database, callback, bloom)

eth/downloader/downloader.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1611,7 +1611,13 @@ func (d *Downloader) processFastSyncContent(latest *types.Header) error {
16111611
// Start syncing state of the reported head block. This should get us most of
16121612
// the state of the pivot block.
16131613
sync := d.syncState(latest.Root)
1614-
defer sync.Cancel()
1614+
defer func() {
1615+
// The `sync` object is replaced every time the pivot moves. We need to
1616+
// defer close the very last active one, hence the lazy evaluation vs.
1617+
// calling defer sync.Cancel() !!!
1618+
sync.Cancel()
1619+
}()
1620+
16151621
closeOnErr := func(s *stateSync) {
16161622
if err := s.Wait(); err != nil && err != errCancelStateFetch && err != errCanceled {
16171623
d.queue.Close() // wake up Results
@@ -1674,9 +1680,8 @@ func (d *Downloader) processFastSyncContent(latest *types.Header) error {
16741680
// If new pivot block found, cancel old state retrieval and restart
16751681
if oldPivot != P {
16761682
sync.Cancel()
1677-
16781683
sync = d.syncState(P.Header.Root)
1679-
defer sync.Cancel()
1684+
16801685
go closeOnErr(sync)
16811686
oldPivot = P
16821687
}

trie/committer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,12 +226,12 @@ func (c *committer) commitLoop(db *Database) {
226226
switch n := n.(type) {
227227
case *shortNode:
228228
if child, ok := n.Val.(valueNode); ok {
229-
c.onleaf(child, hash)
229+
c.onleaf(nil, child, hash)
230230
}
231231
case *fullNode:
232232
for i := 0; i < 16; i++ {
233233
if child, ok := n.Children[i].(valueNode); ok {
234-
c.onleaf(child, hash)
234+
c.onleaf(nil, child, hash)
235235
}
236236
}
237237
}

trie/sync.go

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,19 @@ var ErrNotRequested = errors.New("not requested")
3434
// node it already processed previously.
3535
var ErrAlreadyProcessed = errors.New("already processed")
3636

37+
// maxFetchesPerDepth is the maximum number of pending trie nodes per depth. The
38+
// role of this value is to limit the number of trie nodes that get expanded in
39+
// memory if the node was configured with a significant number of peers.
40+
const maxFetchesPerDepth = 16384
41+
3742
// request represents a scheduled or already in-flight state retrieval request.
3843
type request struct {
44+
path []byte // Merkle path leading to this node for prioritization
3945
hash common.Hash // Hash of the node data content to retrieve
4046
data []byte // Data content of the node, cached until all subtrees complete
4147
code bool // Whether this is a code entry
4248

4349
parents []*request // Parent state nodes referencing this entry (notify all upon completion)
44-
depth int // Depth level within the trie the node is located to prioritise DFS
4550
deps int // Number of dependencies before allowed to commit this node
4651

4752
callback LeafCallback // Callback to invoke if a leaf node it reached on this branch
@@ -89,6 +94,7 @@ type Sync struct {
8994
nodeReqs map[common.Hash]*request // Pending requests pertaining to a trie node hash
9095
codeReqs map[common.Hash]*request // Pending requests pertaining to a code hash
9196
queue *prque.Prque // Priority queue with the pending requests
97+
fetches map[int]int // Number of active fetches per trie node depth
9298
bloom *SyncBloom // Bloom filter for fast state existence checks
9399
}
94100

@@ -100,14 +106,15 @@ func NewSync(root common.Hash, database ethdb.KeyValueReader, callback LeafCallb
100106
nodeReqs: make(map[common.Hash]*request),
101107
codeReqs: make(map[common.Hash]*request),
102108
queue: prque.New(nil),
109+
fetches: make(map[int]int),
103110
bloom: bloom,
104111
}
105-
ts.AddSubTrie(root, 0, common.Hash{}, callback)
112+
ts.AddSubTrie(root, nil, common.Hash{}, callback)
106113
return ts
107114
}
108115

109116
// AddSubTrie registers a new trie to the sync code, rooted at the designated parent.
110-
func (s *Sync) AddSubTrie(root common.Hash, depth int, parent common.Hash, callback LeafCallback) {
117+
func (s *Sync) AddSubTrie(root common.Hash, path []byte, parent common.Hash, callback LeafCallback) {
111118
// Short circuit if the trie is empty or already known
112119
if root == emptyRoot {
113120
return
@@ -128,8 +135,8 @@ func (s *Sync) AddSubTrie(root common.Hash, depth int, parent common.Hash, callb
128135
}
129136
// Assemble the new sub-trie sync request
130137
req := &request{
138+
path: path,
131139
hash: root,
132-
depth: depth,
133140
callback: callback,
134141
}
135142
// If this sub-trie has a designated parent, link them together
@@ -147,7 +154,7 @@ func (s *Sync) AddSubTrie(root common.Hash, depth int, parent common.Hash, callb
147154
// AddCodeEntry schedules the direct retrieval of a contract code that should not
148155
// be interpreted as a trie node, but rather accepted and stored into the database
149156
// as is.
150-
func (s *Sync) AddCodeEntry(hash common.Hash, depth int, parent common.Hash) {
157+
func (s *Sync) AddCodeEntry(hash common.Hash, path []byte, parent common.Hash) {
151158
// Short circuit if the entry is empty or already known
152159
if hash == emptyState {
153160
return
@@ -170,9 +177,9 @@ func (s *Sync) AddCodeEntry(hash common.Hash, depth int, parent common.Hash) {
170177
}
171178
// Assemble the new sub-trie sync request
172179
req := &request{
173-
hash: hash,
174-
code: true,
175-
depth: depth,
180+
path: path,
181+
hash: hash,
182+
code: true,
176183
}
177184
// If this sub-trie has a designated parent, link them together
178185
if parent != (common.Hash{}) {
@@ -190,7 +197,18 @@ func (s *Sync) AddCodeEntry(hash common.Hash, depth int, parent common.Hash) {
190197
func (s *Sync) Missing(max int) []common.Hash {
191198
var requests []common.Hash
192199
for !s.queue.Empty() && (max == 0 || len(requests) < max) {
193-
requests = append(requests, s.queue.PopItem().(common.Hash))
200+
// Retrieve th enext item in line
201+
item, prio := s.queue.Peek()
202+
203+
// If we have too many already-pending tasks for this depth, throttle
204+
depth := int(prio >> 56)
205+
if s.fetches[depth] > maxFetchesPerDepth {
206+
break
207+
}
208+
// Item is allowed to be scheduled, add it to the task list
209+
s.queue.Pop()
210+
s.fetches[depth]++
211+
requests = append(requests, item.(common.Hash))
194212
}
195213
return requests
196214
}
@@ -285,31 +303,35 @@ func (s *Sync) schedule(req *request) {
285303
// is a trie node and code has same hash. In this case two elements
286304
// with same hash and same or different depth will be pushed. But it's
287305
// ok the worst case is the second response will be treated as duplicated.
288-
s.queue.Push(req.hash, int64(req.depth))
306+
prio := int64(len(req.path)) << 56 // depth >= 128 will never happen, storage leaves will be included in their parents
307+
for i := 0; i < 14 && i < len(req.path); i++ {
308+
prio |= int64(15-req.path[i]) << (52 - i*4) // 15-nibble => lexicographic order
309+
}
310+
s.queue.Push(req.hash, prio)
289311
}
290312

291313
// children retrieves all the missing children of a state trie entry for future
292314
// retrieval scheduling.
293315
func (s *Sync) children(req *request, object node) ([]*request, error) {
294316
// Gather all the children of the node, irrelevant whether known or not
295317
type child struct {
296-
node node
297-
depth int
318+
path []byte
319+
node node
298320
}
299321
var children []child
300322

301323
switch node := (object).(type) {
302324
case *shortNode:
303325
children = []child{{
304-
node: node.Val,
305-
depth: req.depth + len(node.Key),
326+
node: node.Val,
327+
path: append(append([]byte(nil), req.path...), node.Key...),
306328
}}
307329
case *fullNode:
308330
for i := 0; i < 17; i++ {
309331
if node.Children[i] != nil {
310332
children = append(children, child{
311-
node: node.Children[i],
312-
depth: req.depth + 1,
333+
node: node.Children[i],
334+
path: append(append([]byte(nil), req.path...), byte(i)),
313335
})
314336
}
315337
}
@@ -322,7 +344,7 @@ func (s *Sync) children(req *request, object node) ([]*request, error) {
322344
// Notify any external watcher of a new key/value node
323345
if req.callback != nil {
324346
if node, ok := (child.node).(valueNode); ok {
325-
if err := req.callback(node, req.hash); err != nil {
347+
if err := req.callback(req.path, node, req.hash); err != nil {
326348
return nil, err
327349
}
328350
}
@@ -346,9 +368,9 @@ func (s *Sync) children(req *request, object node) ([]*request, error) {
346368
}
347369
// Locally unknown node, schedule for retrieval
348370
requests = append(requests, &request{
371+
path: child.path,
349372
hash: hash,
350373
parents: []*request{req},
351-
depth: child.depth,
352374
callback: req.callback,
353375
})
354376
}
@@ -364,9 +386,11 @@ func (s *Sync) commit(req *request) (err error) {
364386
if req.code {
365387
s.membatch.codes[req.hash] = req.data
366388
delete(s.codeReqs, req.hash)
389+
s.fetches[len(req.path)]--
367390
} else {
368391
s.membatch.nodes[req.hash] = req.data
369392
delete(s.nodeReqs, req.hash)
393+
s.fetches[len(req.path)]--
370394
}
371395
// Check all parents for completion
372396
for _, parent := range req.parents {

trie/trie.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ var (
3838
// LeafCallback is a callback type invoked when a trie operation reaches a leaf
3939
// node. It's used by state sync and commit to allow handling external references
4040
// between account and storage tries.
41-
type LeafCallback func(leaf []byte, parent common.Hash) error
41+
type LeafCallback func(path []byte, leaf []byte, parent common.Hash) error
4242

4343
// Trie is a Merkle Patricia Trie.
4444
// The zero value is an empty trie with no database.

trie/trie_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ func BenchmarkCommitAfterHash(b *testing.B) {
565565
benchmarkCommitAfterHash(b, nil)
566566
})
567567
var a account
568-
onleaf := func(leaf []byte, parent common.Hash) error {
568+
onleaf := func(path []byte, leaf []byte, parent common.Hash) error {
569569
rlp.DecodeBytes(leaf, &a)
570570
return nil
571571
}

0 commit comments

Comments
 (0)