Skip to content

Commit 7364e63

Browse files
authored
core/rawdb: change the mechanism to schedule freezer sync (ethereum#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.
1 parent 17903fe commit 7364e63

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

core/rawdb/freezer_batch.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,16 @@ import (
2525
"github.com/golang/snappy"
2626
)
2727

28-
// This is the maximum amount of data that will be buffered in memory
29-
// for a single freezer table batch.
30-
const freezerBatchBufferLimit = 2 * 1024 * 1024
28+
const (
29+
// This is the maximum amount of data that will be buffered in memory
30+
// for a single freezer table batch.
31+
freezerBatchBufferLimit = 2 * 1024 * 1024
32+
33+
// freezerTableFlushThreshold defines the threshold for triggering a freezer
34+
// table sync operation. If the number of accumulated uncommitted items exceeds
35+
// this value, a sync will be scheduled.
36+
freezerTableFlushThreshold = 512
37+
)
3138

3239
// freezerBatch is a write operation of multiple items on a freezer.
3340
type freezerBatch struct {
@@ -201,14 +208,17 @@ func (batch *freezerTableBatch) commit() error {
201208

202209
// Update headBytes of table.
203210
batch.t.headBytes += dataSize
211+
items := batch.curItem - batch.t.items.Load()
204212
batch.t.items.Store(batch.curItem)
205213

206214
// Update metrics.
207215
batch.t.sizeGauge.Inc(dataSize + indexSize)
208216
batch.t.writeMeter.Mark(dataSize + indexSize)
209217

210218
// Periodically sync the table, todo (rjl493456442) make it configurable?
211-
if time.Since(batch.t.lastSync) > 30*time.Second {
219+
batch.t.uncommitted += items
220+
if batch.t.uncommitted > freezerTableFlushThreshold && time.Since(batch.t.lastSync) > 30*time.Second {
221+
batch.t.uncommitted = 0
212222
batch.t.lastSync = time.Now()
213223
return batch.t.Sync()
214224
}

core/rawdb/freezer_table.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,9 @@ type freezerTable struct {
112112
headId uint32 // number of the currently active head file
113113
tailId uint32 // number of the earliest file
114114

115-
metadata *freezerTableMeta // metadata of the table
116-
lastSync time.Time // Timestamp when the last sync was performed
115+
metadata *freezerTableMeta // metadata of the table
116+
uncommitted uint64 // Count of items written without flushing to file
117+
lastSync time.Time // Timestamp when the last sync was performed
117118

118119
headBytes int64 // Number of bytes written to the head file
119120
readMeter *metrics.Meter // Meter for measuring the effective amount of data read

0 commit comments

Comments
 (0)