From e9e463e9a8dcecc078f93711abeb8f3c77974015 Mon Sep 17 00:00:00 2001 From: Maciej Kulawik Date: Mon, 23 Oct 2023 19:19:12 +0000 Subject: [PATCH 1/3] add cleans cache finalizer, trigger the cleaning early when recreating state --- eth/state_accessor.go | 2 ++ trie/database_wrap.go | 8 ++++++++ 2 files changed, 10 insertions(+) diff --git a/eth/state_accessor.go b/eth/state_accessor.go index 1b0ab0c545..4eab76ba69 100644 --- a/eth/state_accessor.go +++ b/eth/state_accessor.go @@ -85,6 +85,7 @@ func (eth *Ethereum) StateAtBlock(ctx context.Context, block *types.Block, reexe // Create an ephemeral trie.Database for isolating the live one. Otherwise // the internal junks created by tracing will be persisted into the disk. database = state.NewDatabaseWithConfig(eth.chainDb, &trie.Config{Cache: 16}) + defer database.TrieDB().ResetCleans() if statedb, err = state.New(block.Root(), database, nil); err == nil { log.Info("Found disk backend for state trie", "root", block.Root(), "number", block.Number()) return statedb, noopReleaser, nil @@ -100,6 +101,7 @@ func (eth *Ethereum) StateAtBlock(ctx context.Context, block *types.Block, reexe // Create an ephemeral trie.Database for isolating the live one. Otherwise // the internal junks created by tracing will be persisted into the disk. database = state.NewDatabaseWithConfig(eth.chainDb, &trie.Config{Cache: 16}) + defer database.TrieDB().ResetCleans() // If we didn't check the live database, do check state over ephemeral database, // otherwise we would rewind past a persisted block (specific corner case is diff --git a/trie/database_wrap.go b/trie/database_wrap.go index b05f56847a..2ca83d8f68 100644 --- a/trie/database_wrap.go +++ b/trie/database_wrap.go @@ -84,6 +84,7 @@ func prepare(diskdb ethdb.Database, config *Config) *Database { } else { cleans = fastcache.LoadFromFileOrNew(config.Journal, config.Cache*1024*1024) } + runtime.SetFinalizer(cleans, func(c *fastcache.Cache) { c.Reset() }) } var preimages *preimageStore if config != nil && config.Preimages { @@ -97,6 +98,13 @@ func prepare(diskdb ethdb.Database, config *Config) *Database { } } +// resets fastcache to return memory chunks to pool of free chunks +func (db *Database) ResetCleans() { + if db.cleans != nil { + db.cleans.Reset() + } +} + // NewDatabase initializes the trie database with default settings, namely // the legacy hash-based scheme is used by default. func NewDatabase(diskdb ethdb.Database) *Database { From bafe56a513882b417ea760e9fe31cf11cb606ed3 Mon Sep 17 00:00:00 2001 From: Maciej Kulawik Date: Wed, 1 Nov 2023 18:47:16 +0000 Subject: [PATCH 2/3] reset fastcache in Database.Close --- trie/triedb/hashdb/database.go | 8 +++++++- trie/triedb/pathdb/database.go | 7 +++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/trie/triedb/hashdb/database.go b/trie/triedb/hashdb/database.go index f03ef3d5f7..7e9e5c02c8 100644 --- a/trie/triedb/hashdb/database.go +++ b/trie/triedb/hashdb/database.go @@ -635,7 +635,13 @@ func (db *Database) Size() common.StorageSize { } // Close closes the trie database and releases all held resources. -func (db *Database) Close() error { return nil } +func (db *Database) Close() error { + if db.cleans != nil { + // Reset cleans cache to return mmaped memory chunks to fastcache pool + db.cleans.Reset() + } + return nil +} // Scheme returns the node scheme used in the database. func (db *Database) Scheme() string { diff --git a/trie/triedb/pathdb/database.go b/trie/triedb/pathdb/database.go index 29f6b5e103..6e89247fad 100644 --- a/trie/triedb/pathdb/database.go +++ b/trie/triedb/pathdb/database.go @@ -344,6 +344,13 @@ func (db *Database) Close() error { db.lock.Lock() defer db.lock.Unlock() + dl := db.tree.bottom() + if dl.cleans != nil { + // Reset cleans cache to return mmaped memory chunks to fastcache pool + // We need to do that only for one diskLayer as all diskLayers share the same cleans cache + dl.cleans.Reset() + } + db.readOnly = true if db.freezer == nil { return nil From 614c728d6ccef3db0d2796efcf4482edd269c2a9 Mon Sep 17 00:00:00 2001 From: Maciej Kulawik Date: Wed, 1 Nov 2023 18:54:22 +0000 Subject: [PATCH 3/3] update comment --- trie/triedb/pathdb/database.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/trie/triedb/pathdb/database.go b/trie/triedb/pathdb/database.go index 6e89247fad..a9cdb01e9d 100644 --- a/trie/triedb/pathdb/database.go +++ b/trie/triedb/pathdb/database.go @@ -347,7 +347,7 @@ func (db *Database) Close() error { dl := db.tree.bottom() if dl.cleans != nil { // Reset cleans cache to return mmaped memory chunks to fastcache pool - // We need to do that only for one diskLayer as all diskLayers share the same cleans cache + // All diskLayers share the same cleans cache dl.cleans.Reset() }