Skip to content

Commit 93da0cf

Browse files
cmd, core, eth, light, trie: dump clean cache periodically (#20391)
* cmd, core, eth, light, trie: dump clean cache periodically * eth: update config * trie: minor fix * core, trie: address comments * eth: remove useless * trie: print clean cache dump start too Co-authored-by: Péter Szilágyi <peterke@gmail.com>
1 parent 79ce553 commit 93da0cf

12 files changed

Lines changed: 128 additions & 24 deletions

File tree

‎cmd/geth/main.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ var (
108108
utils.CacheFlag,
109109
utils.CacheDatabaseFlag,
110110
utils.CacheTrieFlag,
111+
utils.CacheTrieJournalFlag,
112+
utils.CacheTrieRejournalFlag,
111113
utils.CacheGCFlag,
112114
utils.CacheSnapshotFlag,
113115
utils.CacheNoPrefetchFlag,

‎cmd/geth/usage.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ var AppHelpFlagGroups = []flags.FlagGroup{
109109
utils.CacheFlag,
110110
utils.CacheDatabaseFlag,
111111
utils.CacheTrieFlag,
112+
utils.CacheTrieJournalFlag,
113+
utils.CacheTrieRejournalFlag,
112114
utils.CacheGCFlag,
113115
utils.CacheSnapshotFlag,
114116
utils.CacheNoPrefetchFlag,

‎cmd/utils/flags.go‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,16 @@ var (
361361
Usage: "Percentage of cache memory allowance to use for trie caching (default = 15% full mode, 30% archive mode)",
362362
Value: 15,
363363
}
364+
CacheTrieJournalFlag = cli.StringFlag{
365+
Name: "cache.trie.journal",
366+
Usage: "Disk journal directory for trie cache to survive node restarts",
367+
Value: eth.DefaultConfig.TrieCleanCacheJournal,
368+
}
369+
CacheTrieRejournalFlag = cli.DurationFlag{
370+
Name: "cache.trie.rejournal",
371+
Usage: "Time interval to regenerate the trie cache journal",
372+
Value: eth.DefaultConfig.TrieCleanCacheRejournal,
373+
}
364374
CacheGCFlag = cli.IntFlag{
365375
Name: "cache.gc",
366376
Usage: "Percentage of cache memory allowance to use for trie pruning (default = 25% full mode, 0% archive mode)",
@@ -1537,6 +1547,12 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *eth.Config) {
15371547
if ctx.GlobalIsSet(CacheFlag.Name) || ctx.GlobalIsSet(CacheTrieFlag.Name) {
15381548
cfg.TrieCleanCache = ctx.GlobalInt(CacheFlag.Name) * ctx.GlobalInt(CacheTrieFlag.Name) / 100
15391549
}
1550+
if ctx.GlobalIsSet(CacheTrieJournalFlag.Name) {
1551+
cfg.TrieCleanCacheJournal = ctx.GlobalString(CacheTrieJournalFlag.Name)
1552+
}
1553+
if ctx.GlobalIsSet(CacheTrieRejournalFlag.Name) {
1554+
cfg.TrieCleanCacheRejournal = ctx.GlobalDuration(CacheTrieRejournalFlag.Name)
1555+
}
15401556
if ctx.GlobalIsSet(CacheFlag.Name) || ctx.GlobalIsSet(CacheGCFlag.Name) {
15411557
cfg.TrieDirtyCache = ctx.GlobalInt(CacheFlag.Name) * ctx.GlobalInt(CacheGCFlag.Name) / 100
15421558
}

‎core/blockchain.go‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ const (
116116
// that's resident in a blockchain.
117117
type CacheConfig struct {
118118
TrieCleanLimit int // Memory allowance (MB) to use for caching trie nodes in memory
119+
TrieCleanJournal string // Disk journal for saving clean cache entries.
120+
TrieCleanRejournal time.Duration // Time interval to dump clean cache to disk periodically
119121
TrieCleanNoPrefetch bool // Whether to disable heuristic state prefetching for followup blocks
120122
TrieDirtyLimit int // Memory limit (MB) at which to start flushing dirty trie nodes to disk
121123
TrieDirtyDisabled bool // Whether to disable trie write caching and GC altogether (archive node)
@@ -220,7 +222,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par
220222
cacheConfig: cacheConfig,
221223
db: db,
222224
triegc: prque.New(nil),
223-
stateCache: state.NewDatabaseWithCache(db, cacheConfig.TrieCleanLimit),
225+
stateCache: state.NewDatabaseWithCache(db, cacheConfig.TrieCleanLimit, cacheConfig.TrieCleanJournal),
224226
quit: make(chan struct{}),
225227
shouldPreserve: shouldPreserve,
226228
bodyCache: bodyCache,
@@ -328,6 +330,19 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par
328330
bc.txLookupLimit = *txLookupLimit
329331
go bc.maintainTxIndex(txIndexBlock)
330332
}
333+
// If periodic cache journal is required, spin it up.
334+
if bc.cacheConfig.TrieCleanRejournal > 0 {
335+
if bc.cacheConfig.TrieCleanRejournal < time.Minute {
336+
log.Warn("Sanitizing invalid trie cache journal time", "provided", bc.cacheConfig.TrieCleanRejournal, "updated", time.Minute)
337+
bc.cacheConfig.TrieCleanRejournal = time.Minute
338+
}
339+
triedb := bc.stateCache.TrieDB()
340+
bc.wg.Add(1)
341+
go func() {
342+
defer bc.wg.Done()
343+
triedb.SaveCachePeriodically(bc.cacheConfig.TrieCleanJournal, bc.cacheConfig.TrieCleanRejournal, bc.quit)
344+
}()
345+
}
331346
return bc, nil
332347
}
333348

@@ -919,6 +934,12 @@ func (bc *BlockChain) Stop() {
919934
log.Error("Dangling trie nodes after full cleanup")
920935
}
921936
}
937+
// Ensure all live cached entries be saved into disk, so that we can skip
938+
// cache warmup when node restarts.
939+
if bc.cacheConfig.TrieCleanJournal != "" {
940+
triedb := bc.stateCache.TrieDB()
941+
triedb.SaveCache(bc.cacheConfig.TrieCleanJournal)
942+
}
922943
log.Info("Blockchain stopped")
923944
}
924945

‎core/genesis.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ func SetupGenesisBlock(db ethdb.Database, genesis *Genesis) (*params.ChainConfig
174174
// We have the genesis block in database(perhaps in ancient database)
175175
// but the corresponding state is missing.
176176
header := rawdb.ReadHeader(db, stored, 0)
177-
if _, err := state.New(header.Root, state.NewDatabaseWithCache(db, 0), nil); err != nil {
177+
if _, err := state.New(header.Root, state.NewDatabaseWithCache(db, 0, ""), nil); err != nil {
178178
if genesis == nil {
179179
genesis = DefaultGenesisBlock()
180180
}

‎core/state/database.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,16 @@ type Trie interface {
100100
// concurrent use, but does not retain any recent trie nodes in memory. To keep some
101101
// historical state in memory, use the NewDatabaseWithCache constructor.
102102
func NewDatabase(db ethdb.Database) Database {
103-
return NewDatabaseWithCache(db, 0)
103+
return NewDatabaseWithCache(db, 0, "")
104104
}
105105

106106
// NewDatabaseWithCache creates a backing store for state. The returned database
107107
// is safe for concurrent use and retains a lot of collapsed RLP trie nodes in a
108108
// large memory cache.
109-
func NewDatabaseWithCache(db ethdb.Database, cache int) Database {
109+
func NewDatabaseWithCache(db ethdb.Database, cache int, journal string) Database {
110110
csc, _ := lru.New(codeSizeCacheSize)
111111
return &cachingDB{
112-
db: trie.NewDatabaseWithCache(db, cache),
112+
db: trie.NewDatabaseWithCache(db, cache, journal),
113113
codeSizeCache: csc,
114114
}
115115
}

‎eth/api_tracer.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ func (api *PrivateDebugAPI) traceChain(ctx context.Context, start, end *types.Bl
147147

148148
// Ensure we have a valid starting state before doing any work
149149
origin := start.NumberU64()
150-
database := state.NewDatabaseWithCache(api.eth.ChainDb(), 16) // Chain tracing will probably start at genesis
150+
database := state.NewDatabaseWithCache(api.eth.ChainDb(), 16, "") // Chain tracing will probably start at genesis
151151

152152
if number := start.NumberU64(); number > 0 {
153153
start = api.eth.blockchain.GetBlock(start.ParentHash(), start.NumberU64()-1)
@@ -641,7 +641,7 @@ func (api *PrivateDebugAPI) computeStateDB(block *types.Block, reexec uint64) (*
641641
}
642642
// Otherwise try to reexec blocks until we find a state or reach our limit
643643
origin := block.NumberU64()
644-
database := state.NewDatabaseWithCache(api.eth.ChainDb(), 16)
644+
database := state.NewDatabaseWithCache(api.eth.ChainDb(), 16, "")
645645

646646
for i := uint64(0); i < reexec; i++ {
647647
block = api.eth.blockchain.GetBlock(block.ParentHash(), block.NumberU64()-1)

‎eth/backend.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,8 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) {
183183
}
184184
cacheConfig = &core.CacheConfig{
185185
TrieCleanLimit: config.TrieCleanCache,
186+
TrieCleanJournal: ctx.ResolvePath(config.TrieCleanCacheJournal),
187+
TrieCleanRejournal: config.TrieCleanCacheRejournal,
186188
TrieCleanNoPrefetch: config.NoPrefetch,
187189
TrieDirtyLimit: config.TrieDirtyCache,
188190
TrieDirtyDisabled: config.NoPruning,

‎eth/config.go‎

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,16 @@ var DefaultConfig = Config{
5757
DatasetsOnDisk: 2,
5858
DatasetsLockMmap: false,
5959
},
60-
NetworkId: 1,
61-
LightPeers: 100,
62-
UltraLightFraction: 75,
63-
DatabaseCache: 512,
64-
TrieCleanCache: 256,
65-
TrieDirtyCache: 256,
66-
TrieTimeout: 60 * time.Minute,
67-
SnapshotCache: 256,
60+
NetworkId: 1,
61+
LightPeers: 100,
62+
UltraLightFraction: 75,
63+
DatabaseCache: 512,
64+
TrieCleanCache: 256,
65+
TrieCleanCacheJournal: "triecache",
66+
TrieCleanCacheRejournal: 60 * time.Minute,
67+
TrieDirtyCache: 256,
68+
TrieTimeout: 60 * time.Minute,
69+
SnapshotCache: 256,
6870
Miner: miner.Config{
6971
GasFloor: 8000000,
7072
GasCeil: 8000000,
@@ -139,10 +141,12 @@ type Config struct {
139141
DatabaseCache int
140142
DatabaseFreezer string
141143

142-
TrieCleanCache int
143-
TrieDirtyCache int
144-
TrieTimeout time.Duration
145-
SnapshotCache int
144+
TrieCleanCache int
145+
TrieCleanCacheJournal string `toml:",omitempty"` // Disk journal directory for trie cache to survive node restarts
146+
TrieCleanCacheRejournal time.Duration `toml:",omitempty"` // Time interval to regenerate the journal for clean cache
147+
TrieDirtyCache int
148+
TrieTimeout time.Duration
149+
SnapshotCache int
146150

147151
// Mining options
148152
Miner miner.Config

‎eth/gen_config.go‎

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)