triedb: force flush

This commit is contained in:
jsvisa 2025-08-26 06:30:06 +08:00 committed by Gary Rong
parent 706efc597e
commit f1cb76d0f2
4 changed files with 26 additions and 2 deletions

View file

@ -536,6 +536,7 @@ func NewBlockChain(db ethdb.Database, genesis *Genesis, engine consensus.Engine,
stateSizer, err := state.NewSizeTracker(bc.db, bc.triedb)
if err == nil {
bc.stateSizer = stateSizer
triedb.EnableForceFlush()
} else {
log.Info("Failed to setup size tracker", "err", err)
}

View file

@ -384,3 +384,12 @@ func (db *Database) SnapshotCompleted() bool {
}
return pdb.SnapshotCompleted()
}
// EnableForceFlush enables the pathdb to flush any pending changes to disk immediately,
// regardless of the buffer size threshold. This can be used to accelerate
// state sizer initialization by making buffered state changes visible on disk.
func (db *Database) EnableForceFlush() {
if pdb, ok := db.backend.(*pathdb.Database); ok {
pdb.EnableForceFlush()
}
}

View file

@ -138,6 +138,7 @@ type Database struct {
stateIndexer *historyIndexer // History indexer historical state data, nil possible
lock sync.RWMutex // Lock to prevent mutations from happening at the same time
forceFlush bool // Flag to force buffer flush regardless of size
}
// New attempts to load an already existing layer from a persistent key-value
@ -360,6 +361,19 @@ func (db *Database) Commit(root common.Hash, report bool) error {
return db.tree.cap(root, 0)
}
// EnableForceFlush enables force flushing for the next state update.
// This will cause the next Update() call to flush the disk buffer immediately,
// regardless of the buffer threshold, while preserving the 128 diff layers in memory.
func (db *Database) EnableForceFlush() {
db.lock.Lock()
defer db.lock.Unlock()
if !db.forceFlush {
log.Info("Enabling force flush for next pathdb update")
db.forceFlush = true
}
}
// Disable deactivates the database and invalidates all available state layers
// as stale to prevent access to the persistent state, which is in the syncing
// stage.

View file

@ -417,7 +417,7 @@ func (dl *diskLayer) commit(bottom *diffLayer, force bool) (*diskLayer, error) {
// Terminate the background state snapshot generation before mutating the
// persistent state.
if combined.full() || force || flush {
if combined.full() || force || flush || dl.db.forceFlush {
// Wait until the previous frozen buffer is fully flushed
if dl.frozen != nil {
if err := dl.frozen.waitFlush(); err != nil {