From 04f16489ee0b21ac3aa1f48d19b2f524496314a6 Mon Sep 17 00:00:00 2001 From: Gary Rong Date: Thu, 12 Mar 2026 10:58:08 +0800 Subject: [PATCH] triedb/pathdb: fix test stall --- triedb/pathdb/config.go | 7 ++++--- triedb/pathdb/database.go | 4 ++-- triedb/pathdb/database_test.go | 1 + triedb/pathdb/history_indexer.go | 17 +++++++++++------ triedb/pathdb/history_indexer_state.go | 9 ++++++--- triedb/pathdb/history_indexer_test.go | 2 +- 6 files changed, 25 insertions(+), 15 deletions(-) diff --git a/triedb/pathdb/config.go b/triedb/pathdb/config.go index c236b34333..97ee1c2315 100644 --- a/triedb/pathdb/config.go +++ b/triedb/pathdb/config.go @@ -105,9 +105,10 @@ type Config struct { FullValueCheckpoint uint32 // The rate at which trie nodes are encoded in full-value format // Testing configurations - SnapshotNoBuild bool // Flag Whether the state generation is disabled - NoAsyncFlush bool // Flag whether the background buffer flushing is disabled - NoAsyncGeneration bool // Flag whether the background generation is disabled + SnapshotNoBuild bool // Flag Whether the state generation is disabled + NoAsyncFlush bool // Flag whether the background buffer flushing is disabled + NoAsyncGeneration bool // Flag whether the background generation is disabled + NoHistoryIndexDelay bool // Flag whether the history index delay is disabled } // sanitize checks the provided user configurations and changes anything that's diff --git a/triedb/pathdb/database.go b/triedb/pathdb/database.go index 5255602a4e..86a42c69f4 100644 --- a/triedb/pathdb/database.go +++ b/triedb/pathdb/database.go @@ -215,14 +215,14 @@ func (db *Database) setHistoryIndexer() { if db.stateIndexer != nil { db.stateIndexer.close() } - db.stateIndexer = newHistoryIndexer(db.diskdb, db.stateFreezer, db.tree.bottom().stateID(), typeStateHistory) + db.stateIndexer = newHistoryIndexer(db.diskdb, db.stateFreezer, db.tree.bottom().stateID(), typeStateHistory, db.config.NoHistoryIndexDelay) log.Info("Enabled state history indexing") } if db.trienodeFreezer != nil { if db.trienodeIndexer != nil { db.trienodeIndexer.close() } - db.trienodeIndexer = newHistoryIndexer(db.diskdb, db.trienodeFreezer, db.tree.bottom().stateID(), typeTrienodeHistory) + db.trienodeIndexer = newHistoryIndexer(db.diskdb, db.trienodeFreezer, db.tree.bottom().stateID(), typeTrienodeHistory, db.config.NoHistoryIndexDelay) log.Info("Enabled trienode history indexing") } } diff --git a/triedb/pathdb/database_test.go b/triedb/pathdb/database_test.go index 2d1819d08f..8ece83cad7 100644 --- a/triedb/pathdb/database_test.go +++ b/triedb/pathdb/database_test.go @@ -182,6 +182,7 @@ func newTester(t *testing.T, config *testerConfig) *tester { WriteBufferSize: config.writeBufferSize(), NoAsyncFlush: true, JournalDirectory: config.journalDir, + NoHistoryIndexDelay: true, }, config.isVerkle) obj = &tester{ diff --git a/triedb/pathdb/history_indexer.go b/triedb/pathdb/history_indexer.go index ec7233110c..c9bf3e87f1 100644 --- a/triedb/pathdb/history_indexer.go +++ b/triedb/pathdb/history_indexer.go @@ -367,9 +367,9 @@ type indexIniter struct { wg sync.WaitGroup } -func newIndexIniter(disk ethdb.Database, freezer ethdb.AncientStore, typ historyType, lastID uint64) *indexIniter { +func newIndexIniter(disk ethdb.Database, freezer ethdb.AncientStore, typ historyType, lastID uint64, noWait bool) *indexIniter { initer := &indexIniter{ - state: newIniterState(disk), + state: newIniterState(disk, noWait), disk: disk, freezer: freezer, interrupt: make(chan *interruptSignal), @@ -441,14 +441,17 @@ func (i *indexIniter) run(recover bool) { // checkDone reports whether indexing has completed for all histories. checkDone = func() bool { - return i.indexed.Load() == i.last.Load() + metadata := loadIndexMetadata(i.disk, i.typ) + return metadata != nil && metadata.Last == i.last.Load() } // canExit reports whether the initial indexing phase has completed. canExit = func() bool { return !i.state.is(stateSyncing) && checkDone() } - heartBeat = time.NewTicker(15 * time.Second) + heartBeat = time.NewTimer(0) ) + defer heartBeat.Stop() + if recover { if aborted := i.recover(); aborted { return @@ -507,6 +510,8 @@ func (i *indexIniter) run(recover bool) { } case <-heartBeat.C: + heartBeat.Reset(time.Second * 15) + // Short circuit if the indexer is still busy if done != nil { continue @@ -765,10 +770,10 @@ func checkVersion(disk ethdb.KeyValueStore, typ historyType) { // newHistoryIndexer constructs the history indexer and launches the background // initer to complete the indexing of any remaining state histories. -func newHistoryIndexer(disk ethdb.Database, freezer ethdb.AncientStore, lastHistoryID uint64, typ historyType) *historyIndexer { +func newHistoryIndexer(disk ethdb.Database, freezer ethdb.AncientStore, lastHistoryID uint64, typ historyType, noWait bool) *historyIndexer { checkVersion(disk, typ) return &historyIndexer{ - initer: newIndexIniter(disk, freezer, typ, lastHistoryID), + initer: newIndexIniter(disk, freezer, typ, lastHistoryID, noWait), typ: typ, disk: disk, freezer: freezer, diff --git a/triedb/pathdb/history_indexer_state.go b/triedb/pathdb/history_indexer_state.go index 67f1df3d6b..645b4391ad 100644 --- a/triedb/pathdb/history_indexer_state.go +++ b/triedb/pathdb/history_indexer_state.go @@ -67,12 +67,12 @@ type initerState struct { term chan struct{} } -func newIniterState(disk ethdb.Database) *initerState { +func newIniterState(disk ethdb.Database, noWait bool) *initerState { s := &initerState{ disk: disk, term: make(chan struct{}), } - go s.update() + go s.update(noWait) return s } @@ -94,7 +94,7 @@ func (s *initerState) set(state state) { s.state = state } -func (s *initerState) update() { +func (s *initerState) update(noWait bool) { ticker := time.NewTicker(time.Minute) defer ticker.Stop() @@ -102,6 +102,9 @@ func (s *initerState) update() { if headBlock != nil && time.Since(time.Unix(int64(headBlock.Time), 0)) < syncStateTimeWindow { s.set(stateSynced) log.Info("Marked indexing initer as synced") + } else if noWait { + s.set(stateSynced) + log.Info("Marked indexing initer as synced forcibly") } else { s.set(stateSyncing) } diff --git a/triedb/pathdb/history_indexer_test.go b/triedb/pathdb/history_indexer_test.go index 71296de3c7..8bb1db42da 100644 --- a/triedb/pathdb/history_indexer_test.go +++ b/triedb/pathdb/history_indexer_test.go @@ -38,7 +38,7 @@ func TestHistoryIndexerShortenDeadlock(t *testing.T) { rawdb.WriteStateHistory(freezer, uint64(i+1), h.meta.encode(), accountIndex, storageIndex, accountData, storageData) } // As a workaround, assign a future block to keep the initer running indefinitely - indexer := newHistoryIndexer(db, freezer, 200, typeStateHistory) + indexer := newHistoryIndexer(db, freezer, 200, typeStateHistory, true) defer indexer.close() done := make(chan error, 1)