Missing syncDir on value log rotation and L0 table flush breaks crash consistency
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 45/100
Research direction
Start with createVlogFile in value.go (~L526-553) and the comment above valueLog.sync (~L749-751), then compare addLevel0Table in levels.go (~L1656-1670) with the directory sync in compactBuildTables (~L1009-1014). Read handleMemTableFlush in db.go (~L1138-1146) to see the MANIFEST write path. Done means the new .vlog and .sst directory entries are durable before publication, with cleanup on sync failure, plus a crash-simulation test that drops the directory entry and reopens.
Written by the indexing model from the issue text.
Description
Describe the bug
Two file creation paths in Badger omit syncDir before referencing or publishing the new file, breaking crash consistency even with WithSyncWrites(true):
-
Missing
syncDiron value log rotation (silent corruption and cross key value substitution):createVlogFilecallslf.open(...)and publishes the new.vlogfile without syncing the file or callingsyncDir(vlog.dirPath). The directory sync added in #1434 was dropped during #1555, even though the comment abovevalueLog.syncstill assumescreateVlogFilesyncs the value log directory on every rotation. If power is lost after a rotated.vlog(such as000002.vlog) receives acknowledged writes and its LSM value pointer is synced in the memtable WAL, while the unsynced000002.vlogdirectory entry is lost,badger.Opensucceeds and recreates an empty000002.vlog. Subsequent reads of previously acknowledged keys return empty byte slices witherr == nil(even withWithVerifyValueChecksum(true)), and once new large values are written to the recreated000002.vlog, reads of the old keys silently return the new keys' values witherr == niland a valid checksum. -
Missing
syncDiron L0 table flush (permanentbadger.Openfailure):handleMemTableFlushcallstable.CreateTableandaddLevel0Table, which appends the new L0.sstfile toMANIFESTand fsyncsMANIFESTwithout first callingsyncDir(db.opt.Dir)(unlike compaction incompactBuildTables). If power is lost afterMANIFESTis durable while the unsynced.sstdirectory entry is lost, reopening fails withfile does not exist for table 1.
To Reproduce
- Value log rotation: Open a DB with
WithSyncWrites(true),WithVerifyValueChecksum(true),WithValueThreshold(256), andWithValueLogFileSize(1 << 20). Write large values until000002.vlogis created and receives a committed keyk_old. Simulate losing the unsynced000002.vlogdirectory entry while retaining the synced memtable WAL and earlier files. Reopen the DB:badger.Opensucceeds, readingk_oldreturns an empty value witherr == nil, and writing a new large valuek_newcauses a subsequent read ofk_oldto returnk_new's value witherr == nil. - L0 table flush: Open a DB with
WithSyncWrites(true)and flush a memtable into000001.sst. Observe thatMANIFESTis synced without any interveningsyncDir(opt.Dir). Simulate losing the unsynced000001.sstdirectory entry while retaining the syncedMANIFEST. Reopening withbadger.Openfails withfile does not exist for table 1.
Expected behavior
createVlogFileshould sync the newly created.vlogfile and callvlog.db.syncDir(vlog.dirPath)before publishing the file infilesMap, cleaning up the file if syncing fails.addLevel0Tableshould calls.kv.syncDir(s.kv.opt.Dir)before recording the new.ssttable inMANIFEST(when!t.IsInmemory).
Environment
- OS: Linux / macOS
- Language: Go
- Version: v4 (
main)
- Dominant language
- Go
- Stars
- 15.8k
- Forks
- 1.4k
- PR merge metrics
- No merged PRs in 30d
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from dgraph-io/badger
-
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
-
Iterator with SinceTs reads the tables whose max version equals SinceTsPossibly taken @BenSouchet claimed this 25 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
kind/bug Stale
Difficulty 2/5 Under an hour Newbie friendliness 75/100
-
Missing value log fsync on MemTableFlush and vlog rotation results in dangling value pointers after a crashPossibly taken @sli67 claimed this today. Open
Difficulty 3/5 1-2 days Newbie friendliness 20/100
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
All issues in dgraph-io/badger
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
hivecommons/hive#19912 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
siyuan-note/siyuan#21692 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
rudderlabs/rudder-server#7641 · 1 comment ·
Maintainers usually reply within 2 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
ossf/package-analysis#1222 ·
Maintainers usually reply within 1 day