Repository navigation
Conversation
This was referenced Dec 5, 2026
✅ clang-tidy: No findings on changed linesCompleted in 236.8s. |
Summary: With `paranoid_checks=false`, a failed WAL write does not stop the DB. `WALIOStatusCheck()` clears the WAL writer's error so that later writes can go on, and meta#15193 kept that on purpose: its comment says `WALIOStatusCheck()` "preserves the legacy behavior of resetting the writer so a later operation can retry". Later writes go to the same WAL, and they are lost on the next `DB::Open()`. `log::Writer` splits records into 32 KiB blocks and tracks its offset in the current block. When a write to the file fails, `WritableFileWriter` drops the buffered bytes it could not write, but `log::Writer` has already counted them, and a partial `write()`, as when the file system fills up part-way through a request, leaves some of them in the file. From then on the writer's block boundaries do not match the file's. On `DB::Open()`, `log::Reader` expects a record header at the start of each 32 KiB block of the file, finds the middle of a record there instead, and drops the block as corruption. Writes acknowledged after the error are lost, including `sync=true` writes that were written in full and synced. On a real `ENOSPC` (a 32 MiB tmpfs with 512 KiB left, default options except `paranoid_checks=false`), 31 of 300 synced writes made after space was freed are recovered; with 516 KiB left, none are. With this change, with `paranoid_checks=false` on the default write path: - After a write to the current WAL fails, no record is added to that WAL again. The next write switches to a new WAL, under a new file number, without a memtable switch. The old WAL stays like any older WAL: it is synced, tracked in the MANIFEST when `track_and_verify_wals_in_manifest` is set, and replayed by `DB::Open()`, which reads it up to the failed write and then reads the new WAL from its start. The reopen is with `paranoid_checks=false`, as in the tests; with `paranoid_checks=true` the torn end of the old WAL is handled as any torn WAL that is not the last one is in the chosen `wal_recovery_mode`. - If the old WAL is empty, or no write to it was ever acknowledged, it is deleted instead of kept, unless file deletions are disabled, a sync of it is in progress, or `SwitchWAL()` is flushing memtables to free it. So repeated failures leave at most two WAL files, not one per failed write. With `manual_wal_flush=true` a write is acknowledged when it enters the WAL buffer, so an old WAL that holds any byte is kept: if every `FlushWAL()` writes part of the buffer and then fails, one WAL per failure is kept until the memtables are flushed. - If creating the new WAL fails, the write returns that error and the next write tries again. An `IOFenced` error also stops writes, as it does in `WALIOStatusCheck()`. - A failed sync, of the current WAL, an older WAL or the WAL directory, leaves the WAL's contents intact, so writes continue in the same WAL as before. A `FlushWAL()` or a write that meets the error of a failed sync before it is reported returns that error without writing anything, and the WAL is kept. If the sync fails while a write is already being added, the write can still be refused part-way and switch WALs. The switch then first writes what the old WAL's buffer holds, as a memtable switch does; if that fails, that write returns the error, as a failed `FlushWAL()` would, and the next write switches. The error is now cleared on every WAL writer, not only the current one: after a switch, a sync also syncs the old WAL, and a failure there would otherwise fail every later sync. In that setup, with 512 or 516 KiB left and `manual_wal_flush` off or on, all 300 synced writes made after the error are recovered. With values of 0x05 bytes, where main's `DB::Open()` does not return (meta#13333), the same holds. With `manual_wal_flush=true`, writes that were acknowledged into the WAL buffer and dropped when writing it failed remain lost, with or without this change. On POSIX this change should land with or after the change that counts the bytes of a partial write in `PosixWritableFile`'s size. With `manual_wal_flush=true` and 512 KiB left, the failed `FlushWAL()` writes 61,854 of 66,062 bytes, which hold 59 complete records of acknowledged writes. With this change those bytes end the old WAL, and `PosixWritableFile::Close()` truncates the file to the size it counted, which leaves out a partial write. Of the 700 writes acknowledged before space was freed, main recovers 507 (511 with 516 KiB left), this change alone 448 in both cases, and this change with that one and the WAL reader fix for meta#13333, 507 and 511. `WinWritableFile` does not truncate on close. Two other approaches were considered. Realigning the writer to the file's size needs the true size after a failed write, and `FSWritableFile::GetFileSize()` is not specified to be exact after a failed append; on POSIX it misses the bytes of a partial write today. The switch needs no size; only the deletion of an old WAL that holds nothing reads one, and it keeps the WAL unless nothing written to it succeeded and the file system reports a size of 0. Persisting the memtables before taking more writes, as `DB::Resume()` does for file-scoped WAL errors, needs SST and MANIFEST space during the condition being handled and stops writes until it succeeds, and `ResumeImpl()` does not support `manual_wal_flush` or `track_and_verify_wals_in_manifest`. With `paranoid_checks=false` writes are meant to continue. A WAL switch without a memtable switch leaves the existing WAL retention unchanged: memtables already span several WALs whenever another column family triggered the switch, and each column family's log number still decides when a WAL becomes obsolete. The switch is done only when all of these hold: `paranoid_checks=false`, one write queue, no pipelined or unordered writes, `allow_2pc=false`, `recycle_log_file_num=0` and `track_and_verify_wals=false`. Every other configuration keeps writing to the same WAL, so the loss of later records remains there. With 2PC, a commit can follow a prepare that was in the dropped buffer. A new recycled WAL can hold a stale tail after the torn record, which stops replay under `kPointInTimeRecovery` even with `paranoid_checks=false`. `track_and_verify_wals` records the predecessor WAL's exact size and last sequence number, which are not known after a failed write. Two write queues, pipelined and unordered writes have their own leaders and sequence allocation, and the second queue does not go through `PreprocessWrite()`; a switch there needs its own design. Three changes reach those configurations: the error reset and the bonus fix below, and the early return in `WriteToWAL()` when the WAL file already has an error, which fails the write with the same error as before, but before anything else is done for it. This change does not stop a failed write's sequence numbers from being reused. When its record reached the WAL in full and only the file's `Flush()` or `Sync()` failed, the next write gets the same numbers and `DB::Open()` fails with `Corruption: Sequence number is being set backwards during recovery`, as before. meta#15819 fixes the same reuse after a failed MANIFEST commit and leaves this case to a separate change. Skipping the numbers here would not be enough: after a failed write that did not land, the skipped range is a gap between the old WAL and the new one, and `kPointInTimeRecovery` with `paranoid_checks=true` resumes after a torn WAL only when the next WAL's sequence numbers follow on, so it would drop the new WAL. Other effects: - With `manual_wal_flush=true`, a write whose sync found that a concurrent `FlushWAL()` had dropped its record now returns an error instead of OK. - Clearing the error on every WAL writer is not limited to the configurations above. It applies wherever `WALIOStatusCheck()` resets instead of setting a background error, which is every configuration with `paranoid_checks=false`, and a `Busy` or `Incomplete` status with `paranoid_checks=true`. - Not changed: a checkpoint copies an open WAL up to the bytes `WritableFileWriter` counted as written, and an append that reached the file but returned an error is not counted, so the copy can miss those bytes, with or without this change. Before, the current WAL was copied short by that amount at its end; now it is the old WAL that is. - meta#15820 changes `WALIOStatusCheck()` and `FlushWAL()` for WAL errors that become background errors. It sets no mark for a failed WAL write with `paranoid_checks=false`, because RocksDB keeps using that WAL; with this change RocksDB stops using it, so that reasoning would need a second look if both land. Bonus fix: with `manual_wal_flush=true`, `WriteToWAL()` returned without unlocking `wal_write_mutex_` when writing the user-defined timestamp size record failed, so the next write waited forever. Test Plan: New tests in `db/db_wal_test.cc`, using a `FileSystem` wrapper that fails a WAL file's `Append()`, `Flush()` or `Sync()`, or its creation, on request. Each of the first eight fails or hangs without the change: - `DBWALWriteErrorTest.WritesAfterFailedWalWriteAreRecovered`: a failed write that lands 0 or 5 bytes, with `manual_wal_flush` and `track_and_verify_wals_in_manifest` on and off, then 10 synced writes crossing several blocks. A new WAL is used, the old one does not grow, and all 10 are recovered in all four `wal_recovery_mode` values. Without the change, 3 of 10 are recovered. - `DBWALWriteErrorTest.WalFilesBoundedWhileWritesFail`: 20 failing writes that land 0 or 5 bytes each (with `manual_wal_flush`, 0 bytes only) leave at most two WAL files, and synced writes after the failures are recovered. Without the change, those writes are lost. - `DBWALWriteErrorTest.FailedWalWriteThatLandedIsKept`: with `manual_wal_flush`, the first `FlushWAL()` of a new WAL reaches the file in full and returns an error. The old WAL is kept and its writes are recovered. Without the change no new WAL is used. - `DBWALWriteErrorTest.NewWalCreationFails`: a failed WAL creation fails the write and leaves no file, and the next write succeeds. Without the change the write goes to the same WAL. - `DBWALWriteErrorTest.FlushWalFailsWhileWriteIsInProgress`: with `manual_wal_flush`, a `FlushWAL()` fails after a write chose the WAL (sync point). The write fails and the old WAL does not grow. Without the change the write is accepted. - `DBWALWriteErrorTest.WalSwitchBeforeFlushWalReportsError`: with `manual_wal_flush`, a synced write switches WALs while a failed `FlushWAL()` has not yet reported its error (new sync point `DBImpl::FlushWAL:WriteBufferFailed`). The write and later syncs succeed. Without the change, with the new sync point added, the write fails with `IO error: Seen injected error. Skip writing buffer.` (a debug build's wording). - `DBWALWriteErrorTest.FailedSyncOfOldWalIsRetried`: after a switch, a sync of the old WAL fails once, and later synced writes and `SyncWAL()` succeed. Without the change no new WAL is used. - `DBWALWriteErrorTest.DamagedWalBufferIsWrittenBeforeSwitch`: with `manual_wal_flush`, the WAL is marked damaged by an error that dropped nothing (injected at a sync point) while an acknowledged write is only in its buffer. The switch writes the buffer first and keeps the WAL, and the write is recovered; if writing the buffer fails, that write returns the error and the next write switches. Without the change the next write hangs, because the failed write leaves `wal_write_mutex_` locked (the bonus fix). - `DBWALWriteErrorTest.FailedWalSyncKeepsWal`: a failed sync keeps the WAL, and data accepted before it is recovered. Unchanged behavior; passes without the change. - `DBWALWriteErrorTest.FailedSyncSeenBeforeItIsReportedKeepsWal`: with `manual_wal_flush`, a `FlushWAL()` or a write runs after a failed `SyncWAL()` and before its error is reported (sync point). It fails, the WAL is kept, and an acknowledged write that was only in the WAL buffer is recovered. Unchanged behavior; passes without the change. - `DBWALWriteErrorTest.WalNotSwitchedInOtherConfigurations`: pipelined, two queues, unordered, recycled WALs, `allow_2pc` and `track_and_verify_wals` each keep writing to the same WAL. Unchanged behavior; passes without the change. `db_crashtest.py` does not run with `paranoid_checks=false`, so the crash test does not cover this path; the coverage is the tests above and the real-`ENOSPC` runs. Undoing any one of these rules fails a test. Writing the old WAL's buffer before the switch, or returning the error when that fails: `DamagedWalBufferIsWrittenBeforeSwitch` loses the acknowledged write, or reports no error. Clearing the error on every writer: `FailedSyncOfOldWalIsRetried`. Deleting an old WAL that no acknowledged write reached, not only an empty one: three files are left in `WalFilesBoundedWhileWritesFail`. Returning early in `WriteToWAL()`, or marking the WAL in `FlushWAL()` only when writing the buffer dropped it: `FailedSyncSeenBeforeItIsReportedKeepsWal` switches WALs although nothing was lost; these two keep a failed sync from costing a WAL. The same rule in `WriteGroupToWAL()` has no test of its own.
jevolk
force-pushed
the
wal-writer-after-error
branch
from
December 5, 2026 06:06
43342d1 to
b929a2e
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary:
Background
With
paranoid_checks=false, a failed WAL write does not stop the DB.WALIOStatusCheck()clears the WAL writer's error so that later writes can go on (db_impl_write.cc L2146-L2149), and #15193 kept that on purpose: its comment saysWALIOStatusCheck()"preserves the legacy behavior of resetting the writer so a later operation can retry" (db_impl.cc L2730-L2738). Later writes go to the same WAL.log::Writersplits records into 32 KiB blocks and tracks its offset in the current block (block_offset_). When a write to the file fails,WritableFileWriterdrops the buffered bytes it could not write, butlog::Writerhas already counted them. A partialwrite(), as when the file system fills up part-way through a request, leaves some of them in the file. From then on the writer's block boundaries do not match the file's.The problem
The writes that follow the failed one are lost on the next
DB::Open(). Each record is framed for block boundaries that are not where the file has them. OnDB::Open(),log::Readerexpects a record header at the start of each 32 KiB block of the file, finds the middle of a record there instead, and drops the block as corruption. Writes acknowledged after the error are lost, includingsync=truewrites that were written in full and synced.A real
ENOSPC, on a 32 MiB tmpfs with 512 KiB left, default options exceptparanoid_checks=false:The writer now places its block boundaries 3,176 bytes before the file's. Space is then freed and 300
Put()s withsync=truefollow, everywrite()complete and everyfdatasync()returning 0. On reopen, 31 of the 300 synced writes are recovered; with 516 KiB left the tear falls inside a block and none are. #15930 (the report of this bug) has the reproducer and the field cases that led here.New behavior
With
paranoid_checks=false, on the default write path:track_and_verify_wals_in_manifestis set, and replayed byDB::Open(), which reads it up to the failed write and then reads the new WAL from its start. The reopen is withparanoid_checks=false, as in the tests; withparanoid_checks=truethe torn end of the old WAL is handled as any torn WAL that is not the last one is in the chosenwal_recovery_mode.SwitchWAL()is flushing memtables to free it. So repeated failures leave at most two WAL files, not one per failed write. Withmanual_wal_flush=truea write is acknowledged when it enters the WAL buffer, so an old WAL that holds any byte is kept: if everyFlushWAL()writes part of the buffer and then fails, one WAL per failure is kept until the memtables are flushed.IOFencederror also stops writes, as it does inWALIOStatusCheck().FlushWAL()or a write that meets the error of a failed sync before it is reported returns that error without writing anything, and the WAL is kept. If the sync fails while a write is already being added, the write can still be refused part-way and switch WALs. The switch then first writes what the old WAL's buffer holds, as a memtable switch does; if that fails, that write returns the error, as a failedFlushWAL()would, and the next write switches. The error is now cleared on every WAL writer, not only the current one: after a switch, a sync also syncs the old WAL, and a failure there would otherwise fail every later sync.On the reproducer above, with only this change:
manual_wal_flushbbbb0x05DB::Open()does not return (#13333)With
manual_wal_flush=true, writes that were acknowledged into the WAL buffer and dropped when writing it failed remain lost, with or without this change.On POSIX, merge with or after #15931: with
manual_wal_flush=trueand 512 KiB left, the failedFlushWAL()writes 61,854 of 66,062 bytes, which hold 59 complete records of acknowledged writes. With this change those bytes end the old WAL, andPosixWritableFile::Close()truncates the file to the size it counted, which leaves out a partial write, so those records are cut off. Of the 700 writes acknowledged before space was freed, with 512 / 516 KiB left:#15931 counts the partial write. In every row with this change, all 300 synced writes after the error are recovered. The truncation is
PosixWritableFile::Close()'s; the Windows file does not truncate on close.Why a new WAL
Two other approaches were considered:
FSWritableFile::GetFileSize()is not specified to be exact after a failed append; on POSIX it misses the bytes of a partial write today (Count the bytes of a partial write in PosixWritableFile's size #15931). The switch needs no size. Only the deletion of an old WAL that holds nothing reads one, and it keeps the WAL unless nothing written to it succeeded and the file system reports a size of 0.DB::Resume()does for file-scoped WAL errors. That needs SST and MANIFEST space during the condition being handled, stops writes until it succeeds, andResumeImpl()does not supportmanual_wal_flushortrack_and_verify_wals_in_manifest(db_impl.cc L489-L524). Withparanoid_checks=falsewrites are meant to continue.A WAL switch without a memtable switch leaves the existing WAL retention unchanged: memtables already span several WALs whenever another column family triggered the switch, and each column family's log number still decides when a WAL becomes obsolete.
Scope
The switch is done only when all of these hold:
paranoid_checks=false, one write queue, no pipelined or unordered writes,allow_2pc=false,recycle_log_file_num=0andtrack_and_verify_wals=false. Every other configuration keeps writing to the same WAL, so the loss of later records described above remains there:kPointInTimeRecoveryeven withparanoid_checks=false.track_and_verify_walsrecords the predecessor WAL's exact size and last sequence number, which are not known after a failed write.PreprocessWrite(). A switch there needs its own design and is left for a follow-up (possible, not planned).Three changes reach those configurations: the error reset and the bonus fix under "Other effects", and the early return in
WriteToWAL()when the WAL file already has an error, which fails the write with the same error as before, but before anything else is done for it.This change does not stop a failed write's sequence numbers from being reused. When its record reached the WAL in full and only the file's
Flush()orSync()failed, the next write gets the same numbers andDB::Open()fails withCorruption: Sequence number is being set backwards during recovery, as on main. #15819 fixes the same reuse after a failed MANIFEST commit and leaves this case to a separate change. Skipping the numbers here would not be enough: after a failed write that did not land, the skipped range is a gap between the old WAL and the new one, andkPointInTimeRecoverywithparanoid_checks=trueresumes after a torn WAL only when the next WAL's sequence numbers follow on (MaybeReviseStopReplayForCorruption()), so it would drop the new WAL.Other effects
manual_wal_flush=true, a write whose sync found that a concurrentFlushWAL()had dropped its record now returns an error instead of OK.manual_wal_flush=true,WriteToWAL()returned without unlockingwal_write_mutex_when writing the user-defined timestamp size record failed, so the next write waited forever (covered byDamagedWalBufferIsWrittenBeforeSwitch).WALIOStatusCheck()resets instead of setting a background error, which is every configuration withparanoid_checks=false, and aBusyorIncompletestatus withparanoid_checks=true.WritableFileWritercounted as written, and an append that reached the file but returned an error is not counted. The copy can then miss those bytes, with or without this change. Before, the current WAL was copied short by that amount at its end; now it is the old WAL that is.WALIOStatusCheck()andFlushWAL()for WAL errors that become background errors. It sets no mark for a failed WAL write withparanoid_checks=false, because RocksDB keeps using that WAL; with this change RocksDB stops using it, so that reasoning would need a second look if both land.Test Plan:
New tests in
db/db_wal_test.cc, using aFileSystemwrapper that fails a WAL file'sAppend(),Flush()orSync(), or its creation, on request:DBWALWriteErrorTest.WritesAfterFailedWalWriteAreRecoveredmanual_wal_flushandtrack_and_verify_wals_in_manifeston and off, then 10 synced writes crossing several blocks: a new WAL is used, the old one does not grow, and all 10 are recovered in all fourwal_recovery_modevaluesDBWALWriteErrorTest.WalFilesBoundedWhileWritesFailmanual_wal_flush, 0 bytes only) leave at most two WAL files, and synced writes after the failures are recoveredDBWALWriteErrorTest.FailedWalWriteThatLandedIsKeptmanual_wal_flush, the firstFlushWAL()of a new WAL that reaches the file in full and returns an error: the old WAL is kept and its writes are recoveredDBWALWriteErrorTest.NewWalCreationFailsDBWALWriteErrorTest.FlushWalFailsWhileWriteIsInProgressmanual_wal_flush, aFlushWAL()that fails after a write chose the WAL (sync point): the write fails and the old WAL does not growDBWALWriteErrorTest.WalSwitchBeforeFlushWalReportsErrormanual_wal_flush, a synced write that switches WALs while a failedFlushWAL()has not yet reported its error (new sync pointDBImpl::FlushWAL:WriteBufferFailed): the write and later syncs succeedIO error: Seen injected error. Skip writing buffer.(a debug build's wording)DBWALWriteErrorTest.FailedSyncOfOldWalIsRetriedSyncWAL()succeedDBWALWriteErrorTest.DamagedWalBufferIsWrittenBeforeSwitchmanual_wal_flush, the WAL is marked damaged by an error that dropped nothing (injected at a sync point) while an acknowledged write is only in its buffer: the switch writes the buffer first and keeps the WAL, and the write is recovered; if writing the buffer fails, that write returns the error and the next write switcheswal_write_mutex_locked (the bonus fix)DBWALWriteErrorTest.FailedWalSyncKeepsWalDBWALWriteErrorTest.FailedSyncSeenBeforeItIsReportedKeepsWalmanual_wal_flush, aFlushWAL()or a write that runs after a failedSyncWAL()and before its error is reported (sync point): it fails, the WAL is kept, and an acknowledged write that was only in the WAL buffer is recoveredDBWALWriteErrorTest.WalNotSwitchedInOtherConfigurationsallow_2pcandtrack_and_verify_wals: each keeps writing to the same WALUndoing any one of these rules fails a test. Writing the old WAL's buffer before the switch, or returning the error when that fails:
DamagedWalBufferIsWrittenBeforeSwitchloses the acknowledged write, or reports no error. Clearing the error on every writer:FailedSyncOfOldWalIsRetried. Deleting an old WAL that no acknowledged write reached, not only an empty one: three files are left inWalFilesBoundedWhileWritesFail. Returning early inWriteToWAL(), or marking the WAL inFlushWAL()only when writing the buffer dropped it:FailedSyncSeenBeforeItIsReportedKeepsWalswitches WALs although nothing was lost; these two keep a failed sync from costing a WAL. The same rule inWriteGroupToWAL()has no test of its own.db_crashtest.pydoes not run withparanoid_checks=false, so the crash test does not cover this path; the coverage is the tests above and the real-ENOSPCruns.The real-
ENOSPCreproducer and its runs are in #15930.