Skip to content

Commit 816ea2a

Browse files
committed
triedb: force flush
1 parent 1099c56 commit 816ea2a

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed

core/blockchain.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,7 @@ func NewBlockChain(db ethdb.Database, genesis *Genesis, engine consensus.Engine,
533533
stateSizer, err := state.NewSizeTracker(bc.db, bc.triedb)
534534
if err == nil {
535535
bc.stateSizer = stateSizer
536+
triedb.EnableForceFlush()
536537
} else {
537538
log.Info("Failed to setup size tracker", "err", err)
538539
}

triedb/database.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,3 +384,12 @@ func (db *Database) SnapshotCompleted() bool {
384384
}
385385
return pdb.SnapshotCompleted()
386386
}
387+
388+
// EnableForceFlush enables the pathdb to flush any pending changes to disk immediately,
389+
// regardless of the buffer size threshold. This can be used to accelerate
390+
// state sizer initialization by making buffered state changes visible on disk.
391+
func (db *Database) EnableForceFlush() {
392+
if pdb, ok := db.backend.(*pathdb.Database); ok {
393+
pdb.EnableForceFlush()
394+
}
395+
}

triedb/pathdb/database.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,8 @@ type Database struct {
226226
stateFreezer ethdb.ResettableAncientStore // Freezer for storing state histories, nil possible in tests
227227
stateIndexer *historyIndexer // History indexer historical state data, nil possible
228228

229-
lock sync.RWMutex // Lock to prevent mutations from happening at the same time
229+
lock sync.RWMutex // Lock to prevent mutations from happening at the same time
230+
forceFlush bool // Flag to force buffer flush regardless of size
230231
}
231232

232233
// New attempts to load an already existing layer from a persistent key-value
@@ -448,6 +449,19 @@ func (db *Database) Commit(root common.Hash, report bool) error {
448449
return db.tree.cap(root, 0)
449450
}
450451

452+
// EnableForceFlush enables force flushing for the next state update.
453+
// This will cause the next Update() call to flush the disk buffer immediately,
454+
// regardless of the buffer threshold, while preserving the 128 diff layers in memory.
455+
func (db *Database) EnableForceFlush() {
456+
db.lock.Lock()
457+
defer db.lock.Unlock()
458+
459+
if !db.forceFlush {
460+
log.Info("Enabling force flush for next pathdb update")
461+
db.forceFlush = true
462+
}
463+
}
464+
451465
// Disable deactivates the database and invalidates all available state layers
452466
// as stale to prevent access to the persistent state, which is in the syncing
453467
// stage.

triedb/pathdb/disklayer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ func (dl *diskLayer) commit(bottom *diffLayer, force bool) (*diskLayer, error) {
417417

418418
// Terminate the background state snapshot generation before mutating the
419419
// persistent state.
420-
if combined.full() || force || flush {
420+
if combined.full() || force || flush || dl.db.forceFlush {
421421
// Wait until the previous frozen buffer is fully flushed
422422
if dl.frozen != nil {
423423
if err := dl.frozen.waitFlush(); err != nil {

0 commit comments

Comments
 (0)