mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-17 18:30:45 +00:00
core/rawdb: change the mechanism to schedule freezer sync (#32135)
This pull request slightly improves the freezer fsync mechanism by scheduling the Sync operation based on the number of uncommitted items and original time interval. Originally, freezer.Sync was triggered every 30 seconds, which worked well during active chain synchronization. However, once the initial state sync is complete, the fixed interval causes Sync to be scheduled too frequently. To address this, the scheduling logic has been improved to consider both the time interval and the number of uncommitted items. This additional condition helps avoid unnecessary Sync operations when the chain is idle.
This commit is contained in:
parent
17903fedf0
commit
7364e63ef9
2 changed files with 17 additions and 6 deletions
|
|
@ -25,9 +25,16 @@ import (
|
||||||
"github.com/golang/snappy"
|
"github.com/golang/snappy"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
// This is the maximum amount of data that will be buffered in memory
|
// This is the maximum amount of data that will be buffered in memory
|
||||||
// for a single freezer table batch.
|
// for a single freezer table batch.
|
||||||
const freezerBatchBufferLimit = 2 * 1024 * 1024
|
freezerBatchBufferLimit = 2 * 1024 * 1024
|
||||||
|
|
||||||
|
// freezerTableFlushThreshold defines the threshold for triggering a freezer
|
||||||
|
// table sync operation. If the number of accumulated uncommitted items exceeds
|
||||||
|
// this value, a sync will be scheduled.
|
||||||
|
freezerTableFlushThreshold = 512
|
||||||
|
)
|
||||||
|
|
||||||
// freezerBatch is a write operation of multiple items on a freezer.
|
// freezerBatch is a write operation of multiple items on a freezer.
|
||||||
type freezerBatch struct {
|
type freezerBatch struct {
|
||||||
|
|
@ -201,6 +208,7 @@ func (batch *freezerTableBatch) commit() error {
|
||||||
|
|
||||||
// Update headBytes of table.
|
// Update headBytes of table.
|
||||||
batch.t.headBytes += dataSize
|
batch.t.headBytes += dataSize
|
||||||
|
items := batch.curItem - batch.t.items.Load()
|
||||||
batch.t.items.Store(batch.curItem)
|
batch.t.items.Store(batch.curItem)
|
||||||
|
|
||||||
// Update metrics.
|
// Update metrics.
|
||||||
|
|
@ -208,7 +216,9 @@ func (batch *freezerTableBatch) commit() error {
|
||||||
batch.t.writeMeter.Mark(dataSize + indexSize)
|
batch.t.writeMeter.Mark(dataSize + indexSize)
|
||||||
|
|
||||||
// Periodically sync the table, todo (rjl493456442) make it configurable?
|
// Periodically sync the table, todo (rjl493456442) make it configurable?
|
||||||
if time.Since(batch.t.lastSync) > 30*time.Second {
|
batch.t.uncommitted += items
|
||||||
|
if batch.t.uncommitted > freezerTableFlushThreshold && time.Since(batch.t.lastSync) > 30*time.Second {
|
||||||
|
batch.t.uncommitted = 0
|
||||||
batch.t.lastSync = time.Now()
|
batch.t.lastSync = time.Now()
|
||||||
return batch.t.Sync()
|
return batch.t.Sync()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -113,6 +113,7 @@ type freezerTable struct {
|
||||||
tailId uint32 // number of the earliest file
|
tailId uint32 // number of the earliest file
|
||||||
|
|
||||||
metadata *freezerTableMeta // metadata of the table
|
metadata *freezerTableMeta // metadata of the table
|
||||||
|
uncommitted uint64 // Count of items written without flushing to file
|
||||||
lastSync time.Time // Timestamp when the last sync was performed
|
lastSync time.Time // Timestamp when the last sync was performed
|
||||||
|
|
||||||
headBytes int64 // Number of bytes written to the head file
|
headBytes int64 // Number of bytes written to the head file
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue