From c6af48100d214d635d10f0ad47ddc11b4a153db6 Mon Sep 17 00:00:00 2001 From: galaio <12880651+galaio@users.noreply.github.com> Date: Tue, 16 Jul 2024 21:44:39 +0800 Subject: [PATCH] freezer: Opt freezer env checking (#2580) --- cmd/geth/pruneblock_test.go | 6 ++++++ core/blockchain_repair_test.go | 9 ++++++++ core/blockchain_sethead_test.go | 9 ++++++++ core/rawdb/chain_freezer.go | 38 ++++++++++++++------------------- 4 files changed, 40 insertions(+), 22 deletions(-) diff --git a/cmd/geth/pruneblock_test.go b/cmd/geth/pruneblock_test.go index b2a93f65aa..ac8a6a70d4 100644 --- a/cmd/geth/pruneblock_test.go +++ b/cmd/geth/pruneblock_test.go @@ -155,6 +155,12 @@ func BlockchainCreator(t *testing.T, chaindbPath, AncientPath string, blockRemai triedb := triedb.NewDatabase(db, nil) defer triedb.Close() + if err = db.SetupFreezerEnv(ðdb.FreezerEnv{ + ChainCfg: gspec.Config, + BlobExtraReserve: params.DefaultExtraReserveForBlobRequests, + }); err != nil { + t.Fatalf("Failed to create chain: %v", err) + } genesis := gspec.MustCommit(db, triedb) // Initialize a fresh chain with only a genesis block blockchain, err := core.NewBlockChain(db, config, gspec, nil, engine, vm.Config{}, nil, nil) diff --git a/core/blockchain_repair_test.go b/core/blockchain_repair_test.go index 3cfcdafe4a..170fbf3689 100644 --- a/core/blockchain_repair_test.go +++ b/core/blockchain_repair_test.go @@ -26,6 +26,8 @@ import ( "testing" "time" + "github.com/ethereum/go-ethereum/ethdb" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/consensus/ethash" "github.com/ethereum/go-ethereum/core/rawdb" @@ -1795,6 +1797,13 @@ func testRepairWithScheme(t *testing.T, tt *rewindTest, snapshots bool, scheme s config.SnapshotWait = true } config.TriesInMemory = 128 + + if err = db.SetupFreezerEnv(ðdb.FreezerEnv{ + ChainCfg: gspec.Config, + BlobExtraReserve: params.DefaultExtraReserveForBlobRequests, + }); err != nil { + t.Fatalf("Failed to create chain: %v", err) + } chain, err := NewBlockChain(db, config, gspec, nil, engine, vm.Config{}, nil, nil) if err != nil { t.Fatalf("Failed to create chain: %v", err) diff --git a/core/blockchain_sethead_test.go b/core/blockchain_sethead_test.go index 217610c33a..3faa19226c 100644 --- a/core/blockchain_sethead_test.go +++ b/core/blockchain_sethead_test.go @@ -27,6 +27,8 @@ import ( "testing" "time" + "github.com/ethereum/go-ethereum/ethdb" + "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/consensus/ethash" "github.com/ethereum/go-ethereum/core/rawdb" @@ -1998,6 +2000,13 @@ func testSetHeadWithScheme(t *testing.T, tt *rewindTest, snapshots bool, scheme config.SnapshotWait = true } config.TriesInMemory = 128 + + if err = db.SetupFreezerEnv(ðdb.FreezerEnv{ + ChainCfg: gspec.Config, + BlobExtraReserve: params.DefaultExtraReserveForBlobRequests, + }); err != nil { + t.Fatalf("Failed to create chain: %v", err) + } chain, err := NewBlockChain(db, config, gspec, nil, engine, vm.Config{}, nil, nil) if err != nil { t.Fatalf("Failed to create chain: %v", err) diff --git a/core/rawdb/chain_freezer.go b/core/rawdb/chain_freezer.go index 31dd80c320..ac7a80905d 100644 --- a/core/rawdb/chain_freezer.go +++ b/core/rawdb/chain_freezer.go @@ -58,7 +58,8 @@ type chainFreezer struct { wg sync.WaitGroup trigger chan chan struct{} // Manual blocking freeze trigger, test determinism - freezeEnv atomic.Value + freezeEnv atomic.Value + waitEnvTimes int multiDatabase bool } @@ -178,19 +179,6 @@ func (f *chainFreezer) freeze(db ethdb.KeyValueStore) { } } - // check freezer env first, it must wait a while when the env is necessary - err := f.checkFreezerEnv() - if err == missFreezerEnvErr { - log.Warn("Freezer need related env, may wait for a while", "err", err) - backoff = true - continue - } - if err != nil { - log.Error("Freezer check FreezerEnv err", "err", err) - backoff = true - continue - } - var ( frozen uint64 threshold uint64 @@ -200,6 +188,7 @@ func (f *chainFreezer) freeze(db ethdb.KeyValueStore) { hash common.Hash number *uint64 head *types.Header + err error ) // use finalized block as the chain freeze indicator was used for multiDatabase feature, if multiDatabase is false, keep 9W blocks in db @@ -282,6 +271,18 @@ func (f *chainFreezer) freeze(db ethdb.KeyValueStore) { last = first + freezerBatchLimit } } + + // check env first before chain freeze, it must wait when the env is necessary + if err := f.checkFreezerEnv(); err != nil { + f.waitEnvTimes++ + if f.waitEnvTimes%30 == 0 { + log.Warn("Freezer need related env, may wait for a while, and it's not a issue when non-import block", "err", err) + return + } + backoff = true + continue + } + // Seems we have data ready to be frozen, process in usable batches var ( start = time.Now() @@ -544,14 +545,7 @@ func (f *chainFreezer) checkFreezerEnv() error { if exist { return nil } - blobFrozen, err := f.TableAncients(ChainFreezerBlobSidecarTable) - if err != nil { - return err - } - if blobFrozen > 0 { - return missFreezerEnvErr - } - return nil + return missFreezerEnvErr } func isCancun(env *ethdb.FreezerEnv, num *big.Int, time uint64) bool {