Skip to content

Commit b9dad97

Browse files
rjl493456442shekhirin
authored andcommitted
core/rawdb: fsync head data file before closing it (ethereum#26490)
This PR fixes an issue which might result in data lost in freezer. Whenever mutation happens in freezer, all data will be written into head data file and it will be rotated with a new one in case the size of file reaches the threshold. Theoretically, the rotated old data file should be fsync'd to prevent data loss. In freezer.Sync function, we only fsync: (1) index file (2) meta file and (3) head data file. So this PR forcibly fsync the head data file if mutation happens in the boundary of data file.
1 parent 1d6bd4f commit b9dad97

File tree

3 files changed

+8
-5
lines changed

3 files changed

+8
-5
lines changed

core/rawdb/chain_freezer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,14 @@ func (f *chainFreezer) Close() error {
8686
// This functionality is deliberately broken off from block importing to avoid
8787
// incurring additional data shuffling delays on block propagation.
8888
func (f *chainFreezer) freeze(db ethdb.KeyValueStore) {
89-
nfdb := &nofreezedb{KeyValueStore: db}
90-
9189
var (
9290
backoff bool
9391
triggered chan struct{} // Used in tests
92+
nfdb = &nofreezedb{KeyValueStore: db}
9493
)
9594
timer := time.NewTimer(freezerRecheckInterval)
9695
defer timer.Stop()
96+
9797
for {
9898
select {
9999
case <-f.quit:

core/rawdb/freezer_table.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -852,8 +852,11 @@ func (t *freezerTable) advanceHead() error {
852852
if err != nil {
853853
return err
854854
}
855-
856-
// Close old file, and reopen in RDONLY mode.
855+
// Commit the contents of the old file to stable storage and
856+
// tear it down. It will be re-opened in read-only mode.
857+
if err := t.head.Sync(); err != nil {
858+
return err
859+
}
857860
t.releaseFile(t.headId)
858861
t.openFile(t.headId, openFreezerFileForReadOnly)
859862

core/rawdb/freezer_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ func TestFreezerConcurrentModifyTruncate(t *testing.T) {
190190

191191
var item = make([]byte, 256)
192192

193-
for i := 0; i < 1000; i++ {
193+
for i := 0; i < 10; i++ {
194194
// First reset and write 100 items.
195195
if err := f.TruncateHead(0); err != nil {
196196
t.Fatal("truncate failed:", err)

0 commit comments

Comments
 (0)