Skip to content

Commit 143c434

Browse files
authored
core, eth, trie: streaming GC for the trie cache (#16810)
* core, eth, trie: streaming GC for the trie cache * trie: track memcache statistics
1 parent 3f33a7c commit 143c434

4 files changed

Lines changed: 202 additions & 38 deletions

File tree

‎core/blockchain.go‎

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ func (bc *BlockChain) Stop() {
674674
for !bc.triegc.Empty() {
675675
triedb.Dereference(bc.triegc.PopItem().(common.Hash), common.Hash{})
676676
}
677-
if size := triedb.Size(); size != 0 {
677+
if size, _ := triedb.Size(); size != 0 {
678678
log.Error("Dangling trie nodes after full cleanup")
679679
}
680680
}
@@ -916,33 +916,29 @@ func (bc *BlockChain) WriteBlockWithState(block *types.Block, receipts []*types.
916916
bc.triegc.Push(root, -float32(block.NumberU64()))
917917

918918
if current := block.NumberU64(); current > triesInMemory {
919+
// If we exceeded our memory allowance, flush matured singleton nodes to disk
920+
var (
921+
nodes, imgs = triedb.Size()
922+
limit = common.StorageSize(bc.cacheConfig.TrieNodeLimit) * 1024 * 1024
923+
)
924+
if nodes > limit || imgs > 4*1024*1024 {
925+
triedb.Cap(limit - ethdb.IdealBatchSize)
926+
}
919927
// Find the next state trie we need to commit
920928
header := bc.GetHeaderByNumber(current - triesInMemory)
921929
chosen := header.Number.Uint64()
922930

923-
// Only write to disk if we exceeded our memory allowance *and* also have at
924-
// least a given number of tries gapped.
925-
var (
926-
size = triedb.Size()
927-
limit = common.StorageSize(bc.cacheConfig.TrieNodeLimit) * 1024 * 1024
928-
)
929-
if size > limit || bc.gcproc > bc.cacheConfig.TrieTimeLimit {
931+
// If we exceeded out time allowance, flush an entire trie to disk
932+
if bc.gcproc > bc.cacheConfig.TrieTimeLimit {
930933
// If we're exceeding limits but haven't reached a large enough memory gap,
931934
// warn the user that the system is becoming unstable.
932-
if chosen < lastWrite+triesInMemory {
933-
switch {
934-
case size >= 2*limit:
935-
log.Warn("State memory usage too high, committing", "size", size, "limit", limit, "optimum", float64(chosen-lastWrite)/triesInMemory)
936-
case bc.gcproc >= 2*bc.cacheConfig.TrieTimeLimit:
937-
log.Info("State in memory for too long, committing", "time", bc.gcproc, "allowance", bc.cacheConfig.TrieTimeLimit, "optimum", float64(chosen-lastWrite)/triesInMemory)
938-
}
939-
}
940-
// If optimum or critical limits reached, write to disk
941-
if chosen >= lastWrite+triesInMemory || size >= 2*limit || bc.gcproc >= 2*bc.cacheConfig.TrieTimeLimit {
942-
triedb.Commit(header.Root, true)
943-
lastWrite = chosen
944-
bc.gcproc = 0
935+
if chosen < lastWrite+triesInMemory && bc.gcproc >= 2*bc.cacheConfig.TrieTimeLimit {
936+
log.Info("State in memory for too long, committing", "time", bc.gcproc, "allowance", bc.cacheConfig.TrieTimeLimit, "optimum", float64(chosen-lastWrite)/triesInMemory)
945937
}
938+
// Flush an entire trie and restart the counters
939+
triedb.Commit(header.Root, true)
940+
lastWrite = chosen
941+
bc.gcproc = 0
946942
}
947943
// Garbage collect anything below our required write retention
948944
for !bc.triegc.Empty() {
@@ -1181,7 +1177,9 @@ func (bc *BlockChain) insertChain(chain types.Blocks) (int, []interface{}, []*ty
11811177
}
11821178
stats.processed++
11831179
stats.usedGas += usedGas
1184-
stats.report(chain, i, bc.stateCache.TrieDB().Size())
1180+
1181+
cache, _ := bc.stateCache.TrieDB().Size()
1182+
stats.report(chain, i, cache)
11851183
}
11861184
// Append a single chain head event if we've progressed the chain
11871185
if lastCanon != nil && bc.CurrentBlock().Hash() == lastCanon.Hash() {

‎eth/api_tracer.go‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,8 @@ func (api *PrivateDebugAPI) traceChain(ctx context.Context, start, end *types.Bl
251251
// Print progress logs if long enough time elapsed
252252
if time.Since(logged) > 8*time.Second {
253253
if number > origin {
254-
log.Info("Tracing chain segment", "start", origin, "end", end.NumberU64(), "current", number, "transactions", traced, "elapsed", time.Since(begin), "memory", database.TrieDB().Size())
254+
nodes, imgs := database.TrieDB().Size()
255+
log.Info("Tracing chain segment", "start", origin, "end", end.NumberU64(), "current", number, "transactions", traced, "elapsed", time.Since(begin), "memory", nodes+imgs)
255256
} else {
256257
log.Info("Preparing state for chain trace", "block", number, "start", origin, "elapsed", time.Since(begin))
257258
}
@@ -298,6 +299,8 @@ func (api *PrivateDebugAPI) traceChain(ctx context.Context, start, end *types.Bl
298299
// Dereference all past tries we ourselves are done working with
299300
database.TrieDB().Dereference(proot, common.Hash{})
300301
proot = root
302+
303+
// TODO(karalabe): Do we need the preimages? Won't they accumulate too much?
301304
}
302305
}()
303306

@@ -526,7 +529,8 @@ func (api *PrivateDebugAPI) computeStateDB(block *types.Block, reexec uint64) (*
526529
database.TrieDB().Dereference(proot, common.Hash{})
527530
proot = root
528531
}
529-
log.Info("Historical state regenerated", "block", block.NumberU64(), "elapsed", time.Since(start), "size", database.TrieDB().Size())
532+
nodes, imgs := database.TrieDB().Size()
533+
log.Info("Historical state regenerated", "block", block.NumberU64(), "elapsed", time.Since(start), "nodes", nodes, "preimages", imgs)
530534
return statedb, nil
531535
}
532536

‎eth/config.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ var DefaultConfig = Config{
4747
LightPeers: 100,
4848
DatabaseCache: 768,
4949
TrieCache: 256,
50-
TrieTimeout: 5 * time.Minute,
50+
TrieTimeout: 60 * time.Minute,
5151
GasPrice: big.NewInt(18 * params.Shannon),
5252

5353
TxPool: core.DefaultTxPoolConfig,

0 commit comments

Comments
 (0)