Repository navigation
Fix data race to VersionSet::io_status_ - #7034
riversand963 wants to merge 2 commits into
Conversation
facebook-github-bot
left a comment
There was a problem hiding this comment.
@riversand963 has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
|
Just double check, if the version->io_status_ is not ok, resume will be called. During this process, if ProcessManifestWrites is successful, io_status_ will be change back to IOStatus::OK() (the else case test io_status_.IsIOError()). Therefore, we do not need the reset anymore. |
That's correct. |
fb3faee to
0f9daf1
Compare
|
@riversand963 has updated the pull request. Re-import the pull request |
facebook-github-bot
left a comment
There was a problem hiding this comment.
@riversand963 has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
zhichao-cao
left a comment
There was a problem hiding this comment.
LGTM. Thanks for the fix!
|
hmm, I think there are more data races to |
Summary: Currently, VersionSet::io_status_ can be concurrently accessed by multiple threads without lock, causing tsan test to fail. For example, a bg flush thread resets io_status_ before calling LogAndApply(), while another thread already in the process of LogAndApply() reads io_status_. We do not have to reset io_status_ each time we call LogAndApply(). io_status_ is part of the state of VersionSet, and it indicates the outcome of preceding MANIFEST/CURRENT files IO operations. Its value should be updated only when: 1. MANIFEST/CURRENT files IO fail for the first time. 2. MANIFEST/CURRENT files IO succeed as part of recovering from a prior failure without process restart, e.g. calling Resume(). Test Plan (devserver): COMPILE_WITH_TSAN=1 make check COMPILE_WITH_TSAN=1 make db_test2 ./db_test2 --gtest_filter=DBTest2.CompactionStall
0f9daf1 to
66d08fc
Compare
|
@riversand963 has updated the pull request. Re-import the pull request |
|
@zhichao-cao Thanks for the previous review. Could you take another look? |
facebook-github-bot
left a comment
There was a problem hiding this comment.
@riversand963 has imported this pull request. If you are a Facebook employee, you can view this diff on Phabricator.
| mu->Lock(); | ||
| } | ||
|
|
||
| if (!io_s.ok()) { |
There was a problem hiding this comment.
So the general fix logic is, io_status_ is only updated here. In other places, we use local variable to keep the status in e.g., io_s.
There was a problem hiding this comment.
Yes, access to io_status_ should be protected by the db_mutex.
There was a problem hiding this comment.
Yeah, I notice that WriteCurrentStateToManifest is not protected by the db_mutex
|
Thanks for the fix. I think it is good to go now. |
|
@riversand963 merged this pull request in d47c871. |
Summary: After #6949 , VersionSet::io_status_ can be concurrently accessed by multiple threads without lock, causing tsan test to fail. For example, a bg flush thread resets io_status_ before calling LogAndApply(), while another thread already in the process of LogAndApply() reads io_status_. This is a bug. We do not have to reset io_status_ each time we call LogAndApply(). io_status_ is part of the state of VersionSet, and it indicates the outcome of preceding MANIFEST/CURRENT files IO operations. Its value should be updated only when: 1. MANIFEST/CURRENT files IO fail for the first time. 2. MANIFEST/CURRENT files IO succeed as part of recovering from a prior failure without process restart, e.g. calling Resume(). Test Plan (devserver): COMPILE_WITH_TSAN=1 make check COMPILE_WITH_TSAN=1 make db_test2 ./db_test2 --gtest_filter=DBTest2.CompactionStall Pull Request resolved: #7034 Reviewed By: zhichao-cao Differential Revision: D22247137 Pulled By: riversand963 fbshipit-source-id: 77b83e05390f3ee3cd2d96d3fdd6fe4f225e3216
Summary: After meta#6949 , VersionSet::io_status_ can be concurrently accessed by multiple threads without lock, causing tsan test to fail. For example, a bg flush thread resets io_status_ before calling LogAndApply(), while another thread already in the process of LogAndApply() reads io_status_. This is a bug. We do not have to reset io_status_ each time we call LogAndApply(). io_status_ is part of the state of VersionSet, and it indicates the outcome of preceding MANIFEST/CURRENT files IO operations. Its value should be updated only when: 1. MANIFEST/CURRENT files IO fail for the first time. 2. MANIFEST/CURRENT files IO succeed as part of recovering from a prior failure without process restart, e.g. calling Resume(). Test Plan (devserver): COMPILE_WITH_TSAN=1 make check COMPILE_WITH_TSAN=1 make db_test2 ./db_test2 --gtest_filter=DBTest2.CompactionStall Pull Request resolved: meta#7034 Reviewed By: zhichao-cao Differential Revision: D22247137 Pulled By: riversand963 fbshipit-source-id: 77b83e05390f3ee3cd2d96d3fdd6fe4f225e3216
Summary: Background: when a MANIFEST write fails, RocksDB cannot know whether the failed edit is in the MANIFEST that CURRENT names. For example, the record can be written and synced before the error is reported. Or a batch can create a new MANIFEST, rename CURRENT to it, and then get an error from the directory sync. So `VersionSet::ProcessManifestWrites()` puts the new files of the failed batch (the edits of one `ProcessManifestWrites()` call) in the error handler quarantine, and the purge of obsolete files does not delete them. When a MANIFEST write fails, `ProcessManifestWrites()` also resets `descriptor_log_`, and a batch without a `descriptor_log_` sets `new_descriptor_log`. So the next batch that writes the MANIFEST always starts a new MANIFEST. Once that new MANIFEST is installed, the old one is no longer used, and the quarantine is cleared. `VersionSet::io_status_` keeps the MANIFEST write error until then. How the code got into this state: - Since meta#7034, `ProcessManifestWrites()` resets `io_status_` after any call whose MANIFEST I/O status is OK. It records only the first error. - meta#12030 added the quarantine. It added the files of a failed batch only inside the "first error" block, and it cleared the quarantine together with the reset of `io_status_`. At that time `SetOptions()` still wrote to the MANIFEST. - meta#13384 made `SetOptions()` skip the MANIFEST write. So every `SetOptions()` became a call with an OK MANIFEST I/O status that writes no new MANIFEST. The problems: 1. Two kinds of calls have an OK MANIFEST I/O status but write no new MANIFEST: - `DBImpl::SetOptions()` installs a new Version through `LogAndApply()` with an edit that writes nothing to the MANIFEST (`skip_manifest_write`). - A batch can fail before any MANIFEST I/O, for example when `LoadTableHandlers()` fails with `paranoid_checks`. A compaction that is already running when a flush's MANIFEST write fails still commits, because the background error is checked only when the compaction starts. After such a call, the old MANIFEST can still hold the failed edit, but its files are no longer quarantined. 2. A second failed MANIFEST write, while `io_status_` already holds an earlier error, does not quarantine its own files. This is a problem when the second batch creates a new MANIFEST and renames CURRENT to it, and then reports an error. CURRENT names the new MANIFEST, and it lists the new files. In both cases, the purge after the failed job, or the purge in a clean `Close()`, deletes the files. When CURRENT names a MANIFEST that lists a deleted file, every `DB::Open()` fails with "Corruption: IO error: No such file or directory ... .sst The file MANIFEST-... may be corrupted". A db_stress crash test hit problem 1: a flush's MANIFEST write got an injected write error, a `SetOptions()` ran about 1.6 ms later and cleared the quarantine, the flush job deleted its SST, and the harness killed the process. This change cannot repair a DB that already lost such an SST. Example of problem 2, reproduced by the new unit test: ``` 1. DropColumnFamily(): the MANIFEST record is synced, then the write reports an error. io_status_ = that error. No background error is set. 2. (optional) SetOptions() succeeds. 3. Flush: starts a new MANIFEST that lists the flushed SST, renames CURRENT to it, then SetCurrentFile() reports an error. io_status_ is already bad, so the SST is not quarantined. 4. The flush job's purge deletes the SST. The next DB::Open() fails. ``` Without step 2, upstream fails this way. With step 2, upstream passes, because `SetOptions()` resets `io_status_` before the flush, so the flush failure is the "first" error. Fixing problem 1 alone would make the run with step 2 fail too, because `SetOptions()` would no longer reset `io_status_`. So this change fixes both problems. New behavior: - Every MANIFEST write failure adds the files of its batch to the quarantine. `io_status_` keeps the first error. A file number is added only once, because automatic recovery can retry many times, each retry adds the old MANIFEST again, and each `FindObsoleteFiles()` copies the list. - `io_status_` is reset and the quarantine is cleared only when the call wrote and installed a new MANIFEST (s is OK and `new_descriptor_log` is set). A batch that skips the MANIFEST write never sets `new_descriptor_log`. Until then, the files stay quarantined. This is enough for recovery: because `descriptor_log_` was reset, the next real MANIFEST write (for example, a non-atomic flush, a compaction, or the forced write in `DB::Resume()`) starts a new MANIFEST, and its success clears the whole quarantine. - `DBImpl::SetOptions()` sets no background error after its `LogAndApply()`. This part is required, not a cleanup. Since meta#13384, that call does no MANIFEST I/O and is never grouped with other writers, so a non-OK `io_status()` there always belongs to another, earlier batch. The removed block was `if (!versions_->io_status().ok()) { assert(!s.ok()); SetBGError(...); }`. With only the `VersionSet` change, `io_status()` stays non-OK after a successful `SetOptions()`. Then a debug build fails the assert, and a release build sets the old error as a fatal background error, so the next write fails. A backport must include this part. - Two callers do not take the old error in `versions_->io_status()` as their own when they did not fail on MANIFEST I/O: - An atomic flush checks `versions_->io_status()` while it waits for its turn to install. The check stops the wait when the flush that it waits for failed its MANIFEST write. A flush that does not wait for another flush, while no background error is set, ignores the error and installs its result. That install writes a new MANIFEST, and its success clears the old error and the quarantine. While a background error is set, the flush returns the error and does not install, as before this change while io_status() holds the error. The check must stay in that case: when the DB is stopped, the code after the wait checks only the recovery error, which is OK when no recovery runs. This also fixes an upstream case. Upstream, a `SetOptions()` after a failed MANIFEST write resets io_status(). A later atomic flush could then install its result after a fatal background error stopped the DB. With this change, io_status() keeps the error until a new MANIFEST is written, so that flush returns the error instead. Without any old MANIFEST error, an atomic flush that is already running can still install after a fatal background error. That is older than this change and not about the MANIFEST, so this change leaves it. - `CompactionJob::Install()` takes `versions_->io_status()` as the job's I/O status only when the commit itself failed. A compaction that failed before its commit, for example when it could not write an output file, keeps its own I/O status. This change closes both problems above: the `SetOptions()` trigger, the "failure before MANIFEST I/O" trigger, and the second failed MANIFEST write, with or without a `SetOptions()` in between. The same rule also covers a running compaction whose commit fails at the CURRENT step after a flush's MANIFEST write failed (derived from the code, not tested separately). Consequences: - Quarantined files can stay longer, until the next new MANIFEST or a reopen. This only delays their deletion. - `DB::Resume()` writes a new MANIFEST when `versions_->io_status()` is an `IOError`. Without this change, a `SetOptions()` before the `Resume()` made it skip that write. With this change the write happens, and it clears the quarantine. - `versions_->io_status()` keeps an old MANIFEST write error until a new MANIFEST is written, and a `SetOptions()` call does not reset it. Several callers read it after their own failure and report it as their own error. This misattribution is older than this change: it already happened between a failed MANIFEST write and the next call with an OK MANIFEST I/O status. The worst case is an old non-retryable error that some call left without setting a background error. `DropColumnFamily()`, `CreateColumnFamily()`, `DeleteFilesInRanges()`, `PromoteL0()`, `IncreaseFullHistoryTsLow()`, the level refit of `CompactRange()`, and the file number reservation before an ingestion are such calls. A caller that takes that old error can set it as a fatal background error, and only a reopen recovers from that. This change fixes the two cases above (an atomic flush that does not wait, and a compaction that fails before its commit). Known cases that stay, all older than this change: - An atomic flush that waits for another flush still returns the old error. This needs two atomic flushes at the same time. - A compaction whose commit fails before MANIFEST I/O, for example with `Corruption` from `LoadTableHandlers()`, reports the old `IOError` with reason `kManifestWrite` instead of its own `Corruption`. The outputs of that compaction were never committed. - A compaction or flush that fails for its own reason picks the reason `kManifestWrite` or `kManifestWriteNoWAL` when the old error exists. For a retryable error of a compaction, this makes it a hard error instead of a soft error. Automatic recovery or `DB::Resume()` then writes a new MANIFEST and recovers. The worse case is with `paranoid_checks=false`. By the severity table, a non-retryable `IOError` is no error for `kCompaction` and `kFlushNoWAL`, but a fatal error for `kManifestWrite` and `kManifestWriteNoWAL`. A `NoSpace` error is no error for `kCompaction`, but a hard error for `kManifestWrite`. - Other calls that write the MANIFEST, such as `ApplyWALToManifest()` and the record of the OPTIONS file number in `WriteOptionsFile()`, set `versions_->io_status()` as a background error when their `LogAndApply()` fails, also when it failed before MANIFEST I/O. A complete fix tracks the MANIFEST I/O status of each batch, and lets a caller report `versions_->io_status()` only when its own commit failed on MANIFEST I/O. That touches `LogAndApply()`, flush, compaction and the DB callers above, and is planned as a follow-up. - An ingestion whose MANIFEST write fails deletes its ingested file in the rollback, even when the file is quarantined. This gap is older than this change, and it is on a different code path. A follow-up will fix it. - No API, option or format change. Differential Revision: D134907305
Summary: Background: when a MANIFEST write fails, RocksDB cannot know whether the failed edit is in the MANIFEST that CURRENT names. For example, the record can be written and synced before the error is reported. Or a batch can create a new MANIFEST, rename CURRENT to it, and then get an error from the directory sync. So `VersionSet::ProcessManifestWrites()` puts the new files of the failed batch (the edits of one `ProcessManifestWrites()` call) in the error handler quarantine, and the purge of obsolete files does not delete them. When a MANIFEST write fails, `ProcessManifestWrites()` also resets `descriptor_log_`, and a batch without a `descriptor_log_` sets `new_descriptor_log`. So the next batch that writes the MANIFEST always starts a new MANIFEST. Once that new MANIFEST is installed, the old one is no longer used, and the quarantine is cleared. `VersionSet::io_status_` keeps the MANIFEST write error until then. How the code got into this state: - Since meta#7034, `ProcessManifestWrites()` resets `io_status_` after any call whose MANIFEST I/O status is OK. It records only the first error. - meta#12030 added the quarantine. It added the files of a failed batch only inside the "first error" block, and it cleared the quarantine together with the reset of `io_status_`. At that time `SetOptions()` still wrote to the MANIFEST. - meta#13384 made `SetOptions()` skip the MANIFEST write. So every `SetOptions()` became a call with an OK MANIFEST I/O status that writes no new MANIFEST. The problems: 1. Two kinds of calls have an OK MANIFEST I/O status but write no new MANIFEST: - `DBImpl::SetOptions()` installs a new Version through `LogAndApply()` with an edit that writes nothing to the MANIFEST (`skip_manifest_write`). - A batch can fail before any MANIFEST I/O, for example when `LoadTableHandlers()` fails with `paranoid_checks`. A compaction that is already running when a flush's MANIFEST write fails still commits, because the background error is checked only when the compaction starts. After such a call, the old MANIFEST can still hold the failed edit, but its files are no longer quarantined. 2. A second failed MANIFEST write, while `io_status_` already holds an earlier error, does not quarantine its own files. This is a problem when the second batch creates a new MANIFEST and renames CURRENT to it, and then reports an error. CURRENT names the new MANIFEST, and it lists the new files. In both cases, the purge after the failed job, or the purge in a clean `Close()`, deletes the files. When CURRENT names a MANIFEST that lists a deleted file, every `DB::Open()` fails with "Corruption: IO error: No such file or directory ... .sst The file MANIFEST-... may be corrupted". A db_stress crash test hit problem 1: a flush's MANIFEST write got an injected write error, a `SetOptions()` ran about 1.6 ms later and cleared the quarantine, the flush job deleted its SST, and the harness killed the process. This change cannot repair a DB that already lost such an SST. Example of problem 2, reproduced by the new unit test: ``` 1. DropColumnFamily(): the MANIFEST record is synced, then the write reports an error. io_status_ = that error. No background error is set. 2. (optional) SetOptions() succeeds. 3. Flush: starts a new MANIFEST that lists the flushed SST, renames CURRENT to it, then SetCurrentFile() reports an error. io_status_ is already bad, so the SST is not quarantined. 4. The flush job's purge deletes the SST. The next DB::Open() fails. ``` Without step 2, upstream fails this way. With step 2, upstream passes, because `SetOptions()` resets `io_status_` before the flush, so the flush failure is the "first" error. Fixing problem 1 alone would make the run with step 2 fail too, because `SetOptions()` would no longer reset `io_status_`. So this change fixes both problems. New behavior: - Every MANIFEST write failure adds the files of its batch to the quarantine. `io_status_` keeps the first error. A file number is added only once, because automatic recovery can retry many times, each retry adds the old MANIFEST again, and each `FindObsoleteFiles()` copies the list. - `io_status_` is reset and the quarantine is cleared only when the call wrote and installed a new MANIFEST (s is OK and `new_descriptor_log` is set). A batch that skips the MANIFEST write never sets `new_descriptor_log`. Until then, the files stay quarantined. This is enough for recovery: because `descriptor_log_` was reset, the next real MANIFEST write (for example, a non-atomic flush, a compaction, or the forced write in `DB::Resume()`) starts a new MANIFEST, and its success clears the whole quarantine. - `DBImpl::SetOptions()` sets no background error after its `LogAndApply()`. This part is required, not a cleanup. Since meta#13384, that call does no MANIFEST I/O and is never grouped with other writers, so a non-OK `io_status()` there always belongs to another, earlier batch. The removed block was `if (!versions_->io_status().ok()) { assert(!s.ok()); SetBGError(...); }`. With only the `VersionSet` change, `io_status()` stays non-OK after a successful `SetOptions()`. Then a debug build fails the assert, and a release build sets the old error as a fatal background error, so the next write fails. A backport must include this part. - Two callers do not take the old error in `versions_->io_status()` as their own when they did not fail on MANIFEST I/O: - An atomic flush checks `versions_->io_status()` while it waits for its turn to install. The check stops the wait when the flush that it waits for failed its MANIFEST write. A flush that does not wait for another flush, while no background error is set, ignores the error and installs its result. That install writes a new MANIFEST, and its success clears the old error and the quarantine. While a background error is set, the flush returns the error and does not install, as before this change while io_status() holds the error. The check must stay in that case: when the DB is stopped, the code after the wait checks only the recovery error, which is OK when no recovery runs. This also fixes an upstream case. Upstream, a `SetOptions()` after a failed MANIFEST write resets io_status(). A later atomic flush could then install its result after a fatal background error stopped the DB. With this change, io_status() keeps the error until a new MANIFEST is written, so that flush returns the error instead. Without any old MANIFEST error, an atomic flush that is already running can still install after a fatal background error. That is older than this change and not about the MANIFEST, so this change leaves it. - `CompactionJob::Install()` takes `versions_->io_status()` as the job's I/O status only when the commit itself failed. A compaction that failed before its commit, for example when it could not write an output file, keeps its own I/O status. This change closes both problems above: the `SetOptions()` trigger, the "failure before MANIFEST I/O" trigger, and the second failed MANIFEST write, with or without a `SetOptions()` in between. The same rule also covers a running compaction whose commit fails at the CURRENT step after a flush's MANIFEST write failed (derived from the code, not tested separately). Consequences: - Quarantined files can stay longer, until the next new MANIFEST or a reopen. This only delays their deletion. - `DB::Resume()` writes a new MANIFEST when `versions_->io_status()` is an `IOError`. Without this change, a `SetOptions()` before the `Resume()` made it skip that write. With this change the write happens, and it clears the quarantine. - `versions_->io_status()` keeps an old MANIFEST write error until a new MANIFEST is written, and a `SetOptions()` call does not reset it. Several callers read it after their own failure and report it as their own error. This misattribution is older than this change: it already happened between a failed MANIFEST write and the next call with an OK MANIFEST I/O status. The worst case is an old non-retryable error that some call left without setting a background error. `DropColumnFamily()`, `CreateColumnFamily()`, `DeleteFilesInRanges()`, `PromoteL0()`, `IncreaseFullHistoryTsLow()`, the level refit of `CompactRange()`, and the file number reservation before an ingestion are such calls. A caller that takes that old error can set it as a fatal background error, and only a reopen recovers from that. This change fixes the two cases above (an atomic flush that does not wait, and a compaction that fails before its commit). Known cases that stay, all older than this change: - An atomic flush that waits for another flush still returns the old error. This needs two atomic flushes at the same time. - A compaction whose commit fails before MANIFEST I/O, for example with `Corruption` from `LoadTableHandlers()`, reports the old `IOError` with reason `kManifestWrite` instead of its own `Corruption`. The outputs of that compaction were never committed. - A compaction or flush that fails for its own reason picks the reason `kManifestWrite` or `kManifestWriteNoWAL` when the old error exists. For a retryable error of a compaction, this makes it a hard error instead of a soft error. Automatic recovery or `DB::Resume()` then writes a new MANIFEST and recovers. The worse case is with `paranoid_checks=false`. By the severity table, a non-retryable `IOError` is no error for `kCompaction` and `kFlushNoWAL`, but a fatal error for `kManifestWrite` and `kManifestWriteNoWAL`. A `NoSpace` error is no error for `kCompaction`, but a hard error for `kManifestWrite`. - Other calls that write the MANIFEST, such as `ApplyWALToManifest()` and the record of the OPTIONS file number in `WriteOptionsFile()`, set `versions_->io_status()` as a background error when their `LogAndApply()` fails, also when it failed before MANIFEST I/O. A complete fix tracks the MANIFEST I/O status of each batch, and lets a caller report `versions_->io_status()` only when its own commit failed on MANIFEST I/O. That touches `LogAndApply()`, flush, compaction and the DB callers above, and is planned as a follow-up. - An ingestion whose MANIFEST write fails deletes its ingested file in the rollback, even when the file is quarantined. This gap is older than this change, and it is on a different code path. A follow-up will fix it. - No API, option or format change. Differential Revision: D134907305
Summary: Background: when a MANIFEST write fails, RocksDB cannot know whether the failed edit is in the MANIFEST that CURRENT names. For example, the record can be written and synced before the error is reported. Or a batch can create a new MANIFEST, rename CURRENT to it, and then get an error from the directory sync. So `VersionSet::ProcessManifestWrites()` puts the new files of the failed batch (the edits of one `ProcessManifestWrites()` call) in the error handler quarantine, and the purge of obsolete files does not delete them. When a MANIFEST write fails, `ProcessManifestWrites()` also resets `descriptor_log_`, and a batch without a `descriptor_log_` sets `new_descriptor_log`. So the next batch that writes the MANIFEST always starts a new MANIFEST. Once that new MANIFEST is installed, the old one is no longer used, and the quarantine is cleared. `VersionSet::io_status_` keeps the MANIFEST write error until then. How the code got into this state: - Since meta#7034, `ProcessManifestWrites()` resets `io_status_` after any call whose MANIFEST I/O status is OK. It records only the first error. - meta#12030 added the quarantine. It added the files of a failed batch only inside the "first error" block, and it cleared the quarantine together with the reset of `io_status_`. At that time `SetOptions()` still wrote to the MANIFEST. - meta#13384 made `SetOptions()` skip the MANIFEST write. So every `SetOptions()` became a call with an OK MANIFEST I/O status that writes no new MANIFEST. The problems: 1. Two kinds of calls have an OK MANIFEST I/O status but write no new MANIFEST: - `DBImpl::SetOptions()` installs a new Version through `LogAndApply()` with an edit that writes nothing to the MANIFEST (`skip_manifest_write`). - A batch can fail before any MANIFEST I/O, for example when `LoadTableHandlers()` fails with `paranoid_checks`. A compaction that is already running when a flush's MANIFEST write fails still commits, because the background error is checked only when the compaction starts. After such a call, the old MANIFEST can still hold the failed edit, but its files are no longer quarantined. 2. A second failed MANIFEST write, while `io_status_` already holds an earlier error, does not quarantine its own files. This is a problem when the second batch creates a new MANIFEST and renames CURRENT to it, and then reports an error. CURRENT names the new MANIFEST, and it lists the new files. In both cases, the purge after the failed job, or the purge in a clean `Close()`, deletes the files. When CURRENT names a MANIFEST that lists a deleted file, every `DB::Open()` fails with "Corruption: IO error: No such file or directory ... .sst The file MANIFEST-... may be corrupted". A db_stress crash test hit problem 1: a flush's MANIFEST write got an injected write error, a `SetOptions()` ran about 1.6 ms later and cleared the quarantine, the flush job deleted its SST, and the harness killed the process. This change cannot repair a DB that already lost such an SST. Example of problem 2, reproduced by the new unit test: ``` 1. DropColumnFamily(): the MANIFEST record is synced, then the write reports an error. io_status_ = that error. No background error is set. 2. (optional) SetOptions() succeeds. 3. Flush: starts a new MANIFEST that lists the flushed SST, renames CURRENT to it, then SetCurrentFile() reports an error. io_status_ is already bad, so the SST is not quarantined. 4. The flush job's purge deletes the SST. The next DB::Open() fails. ``` Without step 2, upstream fails this way. With step 2, upstream passes, because `SetOptions()` resets `io_status_` before the flush, so the flush failure is the "first" error. Fixing problem 1 alone would make the run with step 2 fail too, because `SetOptions()` would no longer reset `io_status_`. So this change fixes both problems. New behavior: - Every MANIFEST write failure adds the files of its batch to the quarantine. `io_status_` keeps the first error. A file number is added only once, because automatic recovery can retry many times, each retry adds the old MANIFEST again, and each `FindObsoleteFiles()` copies the list. - `io_status_` is reset and the quarantine is cleared only when the call wrote and installed a new MANIFEST (s is OK and `new_descriptor_log` is set). A batch that skips the MANIFEST write never sets `new_descriptor_log`. Until then, the files stay quarantined. This is enough for recovery: because `descriptor_log_` was reset, the next real MANIFEST write (for example, a non-atomic flush, a compaction, or the forced write in `DB::Resume()`) starts a new MANIFEST, and its success clears the whole quarantine. - `DBImpl::SetOptions()` sets no background error after its `LogAndApply()`. This part is required, not a cleanup. Since meta#13384, that call does no MANIFEST I/O and is never grouped with other writers, so a non-OK `io_status()` there always belongs to another, earlier batch. The removed block was `if (!versions_->io_status().ok()) { assert(!s.ok()); SetBGError(...); }`. With only the `VersionSet` change, `io_status()` stays non-OK after a successful `SetOptions()`. Then a debug build fails the assert, and a release build sets the old error as a fatal background error, so the next write fails. A backport must include this part. - Two callers do not take the old error in `versions_->io_status()` as their own when they did not fail on MANIFEST I/O: - An atomic flush checks `versions_->io_status()` while it waits for its turn to install. The check stops the wait when the flush that it waits for failed its MANIFEST write. A flush that does not wait for another flush, while no background error is set, ignores the error and installs its result. That install writes a new MANIFEST, and its success clears the old error and the quarantine. While a background error is set, the flush returns the error and does not install, as before this change while io_status() holds the error. The check must stay in that case: when the DB is stopped, the code after the wait checks only the recovery error, which is OK when no recovery runs. This also fixes an upstream case. Upstream, a `SetOptions()` after a failed MANIFEST write resets io_status(). A later atomic flush could then install its result after a fatal background error stopped the DB. With this change, io_status() keeps the error until a new MANIFEST is written, so that flush returns the error instead. Without any old MANIFEST error, an atomic flush that is already running can still install after a fatal background error. That is older than this change and not about the MANIFEST, so this change leaves it. - `CompactionJob::Install()` takes `versions_->io_status()` as the job's I/O status only when the commit itself failed. A compaction that failed before its commit, for example when it could not write an output file, keeps its own I/O status. This change closes both problems above: the `SetOptions()` trigger, the "failure before MANIFEST I/O" trigger, and the second failed MANIFEST write, with or without a `SetOptions()` in between. The same rule also covers a running compaction whose commit fails at the CURRENT step after a flush's MANIFEST write failed (derived from the code, not tested separately). Consequences: - Quarantined files can stay longer, until the next new MANIFEST or a reopen. This only delays their deletion. - `DB::Resume()` writes a new MANIFEST when `versions_->io_status()` is an `IOError`. Without this change, a `SetOptions()` before the `Resume()` made it skip that write. With this change the write happens, and it clears the quarantine. - `versions_->io_status()` keeps an old MANIFEST write error until a new MANIFEST is written, and a `SetOptions()` call does not reset it. Several callers read it after their own failure and report it as their own error. This misattribution is older than this change: it already happened between a failed MANIFEST write and the next call with an OK MANIFEST I/O status. The worst case is an old non-retryable error that some call left without setting a background error. `DropColumnFamily()`, `CreateColumnFamily()`, `DeleteFilesInRanges()`, `PromoteL0()`, `IncreaseFullHistoryTsLow()`, the level refit of `CompactRange()`, and the file number reservation before an ingestion are such calls. A caller that takes that old error can set it as a fatal background error, and only a reopen recovers from that. This change fixes the two cases above (an atomic flush that does not wait, and a compaction that fails before its commit). Known cases that stay, all older than this change: - An atomic flush that waits for another flush still returns the old error. This needs two atomic flushes at the same time. - A compaction whose commit fails before MANIFEST I/O, for example with `Corruption` from `LoadTableHandlers()`, reports the old `IOError` with reason `kManifestWrite` instead of its own `Corruption`. The outputs of that compaction were never committed. - A compaction or flush that fails for its own reason picks the reason `kManifestWrite` or `kManifestWriteNoWAL` when the old error exists. For a retryable error of a compaction, this makes it a hard error instead of a soft error. Automatic recovery or `DB::Resume()` then writes a new MANIFEST and recovers. The worse case is with `paranoid_checks=false`. By the severity table, a non-retryable `IOError` is no error for `kCompaction` and `kFlushNoWAL`, but a fatal error for `kManifestWrite` and `kManifestWriteNoWAL`. A `NoSpace` error is no error for `kCompaction`, but a hard error for `kManifestWrite`. - Other calls that write the MANIFEST, such as `ApplyWALToManifest()` and the record of the OPTIONS file number in `WriteOptionsFile()`, set `versions_->io_status()` as a background error when their `LogAndApply()` fails, also when it failed before MANIFEST I/O. A complete fix tracks the MANIFEST I/O status of each batch, and lets a caller report `versions_->io_status()` only when its own commit failed on MANIFEST I/O. That touches `LogAndApply()`, flush, compaction and the DB callers above, and is planned as a follow-up. - An ingestion whose MANIFEST write fails deletes its ingested file in the rollback, even when the file is quarantined. This gap is older than this change, and it is on a different code path. A follow-up will fix it. - No API, option or format change. Differential Revision: D134907305
Summary: Background: when a MANIFEST write fails, RocksDB cannot know whether the failed edit is in the MANIFEST that CURRENT names. For example, the record can be written and synced before the error is reported. Or a batch can create a new MANIFEST, rename CURRENT to it, and then get an error from the directory sync. So `VersionSet::ProcessManifestWrites()` puts the new files of the failed batch (the edits of one `ProcessManifestWrites()` call) in the error handler quarantine, and the purge of obsolete files does not delete them. When a MANIFEST write fails, `ProcessManifestWrites()` also resets `descriptor_log_`, and a batch without a `descriptor_log_` sets `new_descriptor_log`. So the next batch that writes the MANIFEST always starts a new MANIFEST. Once that new MANIFEST is installed, the old one is no longer used, and the quarantine is cleared. `VersionSet::io_status_` keeps the MANIFEST write error until then. How the code got into this state: - Since meta#7034, `ProcessManifestWrites()` resets `io_status_` after any call whose MANIFEST I/O status is OK. It records only the first error. - meta#12030 added the quarantine. It added the files of a failed batch only inside the "first error" block, and it cleared the quarantine together with the reset of `io_status_`. At that time `SetOptions()` still wrote to the MANIFEST. - meta#13384 made `SetOptions()` skip the MANIFEST write. So every `SetOptions()` became a call with an OK MANIFEST I/O status that writes no new MANIFEST. The problems: 1. Two kinds of calls have an OK MANIFEST I/O status but write no new MANIFEST: - `DBImpl::SetOptions()` installs a new Version through `LogAndApply()` with an edit that writes nothing to the MANIFEST (`skip_manifest_write`). - A batch can fail before any MANIFEST I/O, for example when `LoadTableHandlers()` fails with `paranoid_checks`. A compaction that is already running when a flush's MANIFEST write fails still commits, because the background error is checked only when the compaction starts. After such a call, the old MANIFEST can still hold the failed edit, but its files are no longer quarantined. 2. A second failed MANIFEST write, while `io_status_` already holds an earlier error, does not quarantine its own files. This is a problem when the second batch creates a new MANIFEST and renames CURRENT to it, and then reports an error. CURRENT names the new MANIFEST, and it lists the new files. In both cases, the purge after the failed job, or the purge in a clean `Close()`, deletes the files. When CURRENT names a MANIFEST that lists a deleted file, every `DB::Open()` fails with "Corruption: IO error: No such file or directory ... .sst The file MANIFEST-... may be corrupted". A db_stress crash test hit problem 1: a flush's MANIFEST write got an injected write error, a `SetOptions()` ran about 1.6 ms later and cleared the quarantine, the flush job deleted its SST, and the harness killed the process. This change cannot repair a DB that already lost such an SST. Example of problem 2, reproduced by the new unit test: ``` 1. DropColumnFamily(): the MANIFEST record is synced, then the write reports an error. io_status_ = that error. No background error is set. 2. (optional) SetOptions() succeeds. 3. Flush: starts a new MANIFEST that lists the flushed SST, renames CURRENT to it, then SetCurrentFile() reports an error. io_status_ is already bad, so the SST is not quarantined. 4. The flush job's purge deletes the SST. The next DB::Open() fails. ``` Without step 2, upstream fails this way. With step 2, upstream passes, because `SetOptions()` resets `io_status_` before the flush, so the flush failure is the "first" error. Fixing problem 1 alone would make the run with step 2 fail too, because `SetOptions()` would no longer reset `io_status_`. So this change fixes both problems. New behavior: - Every MANIFEST write failure adds the files of its batch to the quarantine. `io_status_` keeps the first error. A file number is added only once, because automatic recovery can retry many times, each retry adds the old MANIFEST again, and each `FindObsoleteFiles()` copies the list. - `io_status_` is reset and the quarantine is cleared only when the call wrote and installed a new MANIFEST (s is OK and `new_descriptor_log` is set). A batch that skips the MANIFEST write never sets `new_descriptor_log`. Until then, the files stay quarantined. This is enough for recovery: because `descriptor_log_` was reset, the next real MANIFEST write (for example, a non-atomic flush, a compaction, or the forced write in `DB::Resume()`) starts a new MANIFEST, and its success clears the whole quarantine. - `DBImpl::SetOptions()` sets no background error after its `LogAndApply()`. This part is required, not a cleanup. Since meta#13384, that call does no MANIFEST I/O and is never grouped with other writers, so a non-OK `io_status()` there always belongs to another, earlier batch. The removed block was `if (!versions_->io_status().ok()) { assert(!s.ok()); SetBGError(...); }`. With only the `VersionSet` change, `io_status()` stays non-OK after a successful `SetOptions()`. Then a debug build fails the assert, and a release build sets the old error as a fatal background error, so the next write fails. A backport must include this part. - Two callers do not take the old error in `versions_->io_status()` as their own when they did not fail on MANIFEST I/O: - An atomic flush checks `versions_->io_status()` while it waits for its turn to install. The check stops the wait when the flush that it waits for failed its MANIFEST write. A flush that does not wait for another flush, while no background error is set, ignores the error and installs its result. That install writes a new MANIFEST, and its success clears the old error and the quarantine. While a background error is set, the flush returns the error and does not install, as before this change while io_status() holds the error. The check must stay in that case: when the DB is stopped, the code after the wait checks only the recovery error, which is OK when no recovery runs. This also fixes an upstream case. Upstream, a `SetOptions()` after a failed MANIFEST write resets io_status(). A later atomic flush could then install its result after a fatal background error stopped the DB. With this change, io_status() keeps the error until a new MANIFEST is written, so that flush returns the error instead. Without any old MANIFEST error, an atomic flush that is already running can still install after a fatal background error. That is older than this change and not about the MANIFEST, so this change leaves it. - `CompactionJob::Install()` takes `versions_->io_status()` as the job's I/O status only when the commit itself failed. A compaction that failed before its commit, for example when it could not write an output file, keeps its own I/O status. This change closes both problems above: the `SetOptions()` trigger, the "failure before MANIFEST I/O" trigger, and the second failed MANIFEST write, with or without a `SetOptions()` in between. The same rule also covers a running compaction whose commit fails at the CURRENT step after a flush's MANIFEST write failed (derived from the code, not tested separately). Consequences: - Quarantined files can stay longer, until the next new MANIFEST or a reopen. This only delays their deletion. - `DB::Resume()` writes a new MANIFEST when `versions_->io_status()` is an `IOError`. Without this change, a `SetOptions()` before the `Resume()` made it skip that write. With this change the write happens, and it clears the quarantine. - `versions_->io_status()` keeps an old MANIFEST write error until a new MANIFEST is written, and a `SetOptions()` call does not reset it. Several callers read it after their own failure and report it as their own error. This misattribution is older than this change: it already happened between a failed MANIFEST write and the next call with an OK MANIFEST I/O status. The worst case is an old non-retryable error that some call left without setting a background error. `DropColumnFamily()`, `CreateColumnFamily()`, `DeleteFilesInRanges()`, `PromoteL0()`, `IncreaseFullHistoryTsLow()`, the level refit of `CompactRange()`, and the file number reservation before an ingestion are such calls. A caller that takes that old error can set it as a fatal background error, and only a reopen recovers from that. This change fixes the two cases above (an atomic flush that does not wait, and a compaction that fails before its commit). Known cases that stay, all older than this change: - An atomic flush that waits for another flush still returns the old error. This needs two atomic flushes at the same time. - A compaction whose commit fails before MANIFEST I/O, for example with `Corruption` from `LoadTableHandlers()`, reports the old `IOError` with reason `kManifestWrite` instead of its own `Corruption`. The outputs of that compaction were never committed. - A compaction or flush that fails for its own reason picks the reason `kManifestWrite` or `kManifestWriteNoWAL` when the old error exists. For a retryable error of a compaction, this makes it a hard error instead of a soft error. Automatic recovery or `DB::Resume()` then writes a new MANIFEST and recovers. The worse case is with `paranoid_checks=false`. By the severity table, a non-retryable `IOError` is no error for `kCompaction` and `kFlushNoWAL`, but a fatal error for `kManifestWrite` and `kManifestWriteNoWAL`. A `NoSpace` error is no error for `kCompaction`, but a hard error for `kManifestWrite`. - Other calls that write the MANIFEST, such as `ApplyWALToManifest()` and the record of the OPTIONS file number in `WriteOptionsFile()`, set `versions_->io_status()` as a background error when their `LogAndApply()` fails, also when it failed before MANIFEST I/O. A complete fix tracks the MANIFEST I/O status of each batch, and lets a caller report `versions_->io_status()` only when its own commit failed on MANIFEST I/O. That touches `LogAndApply()`, flush, compaction and the DB callers above, and is planned as a follow-up. - An ingestion whose MANIFEST write fails deletes its ingested file in the rollback, even when the file is quarantined. This gap is older than this change, and it is on a different code path. A follow-up will fix it. - No API, option or format change. Differential Revision: D134907305
Summary: Background: when a MANIFEST write fails, RocksDB cannot know whether the failed edit is in the MANIFEST that CURRENT names. For example, the record can be written and synced before the error is reported. Or a batch can create a new MANIFEST, rename CURRENT to it, and then get an error from the directory sync. So `VersionSet::ProcessManifestWrites()` puts the new files of the failed batch (the edits of one `ProcessManifestWrites()` call) in the error handler quarantine, and the purge of obsolete files does not delete them. When a MANIFEST write fails, `ProcessManifestWrites()` also resets `descriptor_log_`, and a batch without a `descriptor_log_` sets `new_descriptor_log`. So the next batch that writes the MANIFEST always starts a new MANIFEST. Once that new MANIFEST is installed, the old one is no longer used, and the quarantine is cleared. `VersionSet::io_status_` keeps the MANIFEST write error until then. How the code got into this state: - Since meta#7034, `ProcessManifestWrites()` resets `io_status_` after any call whose MANIFEST I/O status is OK. It records only the first error. - meta#12030 added the quarantine. It added the files of a failed batch only inside the "first error" block, and it cleared the quarantine together with the reset of `io_status_`. At that time `SetOptions()` still wrote to the MANIFEST. - meta#13384 made `SetOptions()` skip the MANIFEST write. So every `SetOptions()` became a call with an OK MANIFEST I/O status that writes no new MANIFEST. The problems: 1. Two kinds of calls have an OK MANIFEST I/O status but write no new MANIFEST: - `DBImpl::SetOptions()` installs a new Version through `LogAndApply()` with an edit that writes nothing to the MANIFEST (`skip_manifest_write`). - A batch can fail before any MANIFEST I/O, for example when `LoadTableHandlers()` fails with `paranoid_checks`. A compaction that is already running when a flush's MANIFEST write fails still commits, because the background error is checked only when the compaction starts. After such a call, the old MANIFEST can still hold the failed edit, but its files are no longer quarantined. 2. A second failed MANIFEST write, while `io_status_` already holds an earlier error, does not quarantine its own files. This is a problem when the second batch creates a new MANIFEST and renames CURRENT to it, and then reports an error. CURRENT names the new MANIFEST, and it lists the new files. In both cases, the purge after the failed job, or the purge in a clean `Close()`, deletes the files. When CURRENT names a MANIFEST that lists a deleted file, every `DB::Open()` fails with "Corruption: IO error: No such file or directory ... .sst The file MANIFEST-... may be corrupted". A db_stress crash test hit problem 1: a flush's MANIFEST write got an injected write error, a `SetOptions()` ran about 1.6 ms later and cleared the quarantine, the flush job deleted its SST, and the harness killed the process. This change cannot repair a DB that already lost such an SST. Example of problem 2, reproduced by the new unit test: ``` 1. DropColumnFamily(): the MANIFEST record is synced, then the write reports an error. io_status_ = that error. No background error is set. 2. (optional) SetOptions() succeeds. 3. Flush: starts a new MANIFEST that lists the flushed SST, renames CURRENT to it, then SetCurrentFile() reports an error. io_status_ is already bad, so the SST is not quarantined. 4. The flush job's purge deletes the SST. The next DB::Open() fails. ``` Without step 2, upstream fails this way. With step 2, upstream passes, because `SetOptions()` resets `io_status_` before the flush, so the flush failure is the "first" error. Fixing problem 1 alone would make the run with step 2 fail too, because `SetOptions()` would no longer reset `io_status_`. So this change fixes both problems. New behavior: - Every MANIFEST write failure adds the files of its batch to the quarantine. `io_status_` keeps the first error. A file number is added only once, because automatic recovery can retry many times, each retry adds the old MANIFEST again, and each `FindObsoleteFiles()` copies the list. - `io_status_` is reset and the quarantine is cleared only when the call wrote and installed a new MANIFEST (s is OK and `new_descriptor_log` is set). A batch that skips the MANIFEST write never sets `new_descriptor_log`. Until then, the files stay quarantined. This is enough for recovery: because `descriptor_log_` was reset, the next real MANIFEST write (for example, a non-atomic flush, a compaction, or the forced write in `DB::Resume()`) starts a new MANIFEST, and its success clears the whole quarantine. - `DBImpl::SetOptions()` sets no background error after its `LogAndApply()`. This part is required, not a cleanup. Since meta#13384, that call does no MANIFEST I/O and is never grouped with other writers, so a non-OK `io_status()` there always belongs to another, earlier batch. The removed block was `if (!versions_->io_status().ok()) { assert(!s.ok()); SetBGError(...); }`. With only the `VersionSet` change, `io_status()` stays non-OK after a successful `SetOptions()`. Then a debug build fails the assert, and a release build sets the old error as a fatal background error, so the next write fails. A backport must include this part. - Two callers do not take the old error in `versions_->io_status()` as their own when they did not fail on MANIFEST I/O: - An atomic flush checks `versions_->io_status()` while it waits for its turn to install. The check stops the wait when the flush that it waits for failed its MANIFEST write. A flush that does not wait for another flush, while no background error is set, ignores the error and installs its result. That install writes a new MANIFEST, and its success clears the old error and the quarantine. While a background error is set, the flush returns the error and does not install, as before this change while io_status() holds the error. The check must stay in that case: when the DB is stopped, the code after the wait checks only the recovery error, which is OK when no recovery runs. This also fixes an upstream case. Upstream, a `SetOptions()` after a failed MANIFEST write resets io_status(). A later atomic flush could then install its result after a fatal background error stopped the DB. With this change, io_status() keeps the error until a new MANIFEST is written, so that flush returns the error instead. Without any old MANIFEST error, an atomic flush that is already running can still install after a fatal background error. That is older than this change and not about the MANIFEST, so this change leaves it. - `CompactionJob::Install()` takes `versions_->io_status()` as the job's I/O status only when the commit itself failed. A compaction that failed before its commit, for example when it could not write an output file, keeps its own I/O status. This change closes both problems above: the `SetOptions()` trigger, the "failure before MANIFEST I/O" trigger, and the second failed MANIFEST write, with or without a `SetOptions()` in between. The same rule also covers a running compaction whose commit fails at the CURRENT step after a flush's MANIFEST write failed (derived from the code, not tested separately). Consequences: - Quarantined files can stay longer, until the next new MANIFEST or a reopen. This only delays their deletion. - `DB::Resume()` writes a new MANIFEST when `versions_->io_status()` is an `IOError`. Without this change, a `SetOptions()` before the `Resume()` made it skip that write. With this change the write happens, and it clears the quarantine. - `versions_->io_status()` keeps an old MANIFEST write error until a new MANIFEST is written, and a `SetOptions()` call does not reset it. Several callers read it after their own failure and report it as their own error. This misattribution is older than this change: it already happened between a failed MANIFEST write and the next call with an OK MANIFEST I/O status. The worst case is an old non-retryable error that some call left without setting a background error. `DropColumnFamily()`, `CreateColumnFamily()`, `DeleteFilesInRanges()`, `PromoteL0()`, `IncreaseFullHistoryTsLow()`, the level refit of `CompactRange()`, and the file number reservation before an ingestion are such calls. A caller that takes that old error can set it as a fatal background error, and only a reopen recovers from that. This change fixes the two cases above (an atomic flush that does not wait, and a compaction that fails before its commit). Known cases that stay, all older than this change: - An atomic flush that waits for another flush still returns the old error. This needs two atomic flushes at the same time. - A compaction whose commit fails before MANIFEST I/O, for example with `Corruption` from `LoadTableHandlers()`, reports the old `IOError` with reason `kManifestWrite` instead of its own `Corruption`. The outputs of that compaction were never committed. - A compaction or flush that fails for its own reason picks the reason `kManifestWrite` or `kManifestWriteNoWAL` when the old error exists. For a retryable error of a compaction, this makes it a hard error instead of a soft error. Automatic recovery or `DB::Resume()` then writes a new MANIFEST and recovers. The worse case is with `paranoid_checks=false`. By the severity table, a non-retryable `IOError` is no error for `kCompaction` and `kFlushNoWAL`, but a fatal error for `kManifestWrite` and `kManifestWriteNoWAL`. A `NoSpace` error is no error for `kCompaction`, but a hard error for `kManifestWrite`. - Other calls that write the MANIFEST, such as `ApplyWALToManifest()` and the record of the OPTIONS file number in `WriteOptionsFile()`, set `versions_->io_status()` as a background error when their `LogAndApply()` fails, also when it failed before MANIFEST I/O. A complete fix tracks the MANIFEST I/O status of each batch, and lets a caller report `versions_->io_status()` only when its own commit failed on MANIFEST I/O. That touches `LogAndApply()`, flush, compaction and the DB callers above, and is planned as a follow-up. - An ingestion whose MANIFEST write fails deletes its ingested file in the rollback, even when the file is quarantined. This gap is older than this change, and it is on a different code path. A follow-up will fix it. - No API, option or format change. Differential Revision: D134907305
Summary: Pull Request resolved: meta#15818 Background: when a MANIFEST write fails, RocksDB cannot know whether the failed edit is in the MANIFEST that CURRENT names. For example, the record can be written and synced before the error is reported. Or a batch can create a new MANIFEST, rename CURRENT to it, and then get an error from the directory sync. So `VersionSet::ProcessManifestWrites()` puts the new files of the failed batch (the edits of one `ProcessManifestWrites()` call) in the error handler quarantine, and the purge of obsolete files does not delete them. When a MANIFEST write fails, `ProcessManifestWrites()` also resets `descriptor_log_`, and a batch without a `descriptor_log_` sets `new_descriptor_log`. So the next batch that writes the MANIFEST always starts a new MANIFEST. Once that new MANIFEST is installed, the old one is no longer used, and the quarantine is cleared. `VersionSet::io_status_` keeps the MANIFEST write error until then. How the code got into this state: - Since meta#7034, `ProcessManifestWrites()` resets `io_status_` after any call whose MANIFEST I/O status is OK. It records only the first error. - meta#12030 added the quarantine. It added the files of a failed batch only inside the "first error" block, and it cleared the quarantine together with the reset of `io_status_`. At that time `SetOptions()` still wrote to the MANIFEST. - meta#13384 made `SetOptions()` skip the MANIFEST write. So every `SetOptions()` became a call with an OK MANIFEST I/O status that writes no new MANIFEST. The problems: 1. Two kinds of calls have an OK MANIFEST I/O status but write no new MANIFEST: - `DBImpl::SetOptions()` installs a new Version through `LogAndApply()` with an edit that writes nothing to the MANIFEST (`skip_manifest_write`). - A batch can fail before any MANIFEST I/O, for example when `LoadTableHandlers()` fails with `paranoid_checks`. A compaction that is already running when a flush's MANIFEST write fails still commits, because the background error is checked only when the compaction starts. After such a call, the old MANIFEST can still hold the failed edit, but its files are no longer quarantined. 2. A second failed MANIFEST write, while `io_status_` already holds an earlier error, does not quarantine its own files. This is a problem when the second batch creates a new MANIFEST and renames CURRENT to it, and then reports an error. CURRENT names the new MANIFEST, and it lists the new files. In both cases, the purge after the failed job, or the purge in a clean `Close()`, deletes the files. When CURRENT names a MANIFEST that lists a deleted file, every `DB::Open()` fails with "Corruption: IO error: No such file or directory ... .sst The file MANIFEST-... may be corrupted". A db_stress crash test hit problem 1: a flush's MANIFEST write got an injected write error, a `SetOptions()` ran about 1.6 ms later and cleared the quarantine, the flush job deleted its SST, and the harness killed the process. This change cannot repair a DB that already lost such an SST. Example of problem 2, reproduced by the new unit test: ``` 1. DropColumnFamily(): the MANIFEST record is synced, then the write reports an error. io_status_ = that error. No background error is set. 2. (optional) SetOptions() succeeds. 3. Flush: starts a new MANIFEST that lists the flushed SST, renames CURRENT to it, then SetCurrentFile() reports an error. io_status_ is already bad, so the SST is not quarantined. 4. The flush job's purge deletes the SST. The next DB::Open() fails. ``` Without step 2, upstream fails this way. With step 2, upstream passes, because `SetOptions()` resets `io_status_` before the flush, so the flush failure is the "first" error. Fixing problem 1 alone would make the run with step 2 fail too, because `SetOptions()` would no longer reset `io_status_`. So this change fixes both problems. New behavior: - Every MANIFEST write failure adds the files of its batch to the quarantine. `io_status_` keeps the first error. A file number is added only once, because automatic recovery can retry many times, each retry adds the old MANIFEST again, and each `FindObsoleteFiles()` copies the list. - `io_status_` is reset and the quarantine is cleared only when the call wrote and installed a new MANIFEST (s is OK and `new_descriptor_log` is set). A batch that skips the MANIFEST write never sets `new_descriptor_log`. Until then, the files stay quarantined. This is enough for recovery: because `descriptor_log_` was reset, the next real MANIFEST write (for example, a non-atomic flush, a compaction, or the forced write in `DB::Resume()`) starts a new MANIFEST, and its success clears the whole quarantine. - `DBImpl::SetOptions()` sets no background error after its `LogAndApply()`. This part is required, not a cleanup. Since meta#13384, that call does no MANIFEST I/O and is never grouped with other writers, so a non-OK `io_status()` there always belongs to another, earlier batch. The removed block was `if (!versions_->io_status().ok()) { assert(!s.ok()); SetBGError(...); }`. With only the `VersionSet` change, `io_status()` stays non-OK after a successful `SetOptions()`. Then a debug build fails the assert, and a release build sets the old error as a fatal background error, so the next write fails. A backport must include this part. - Two callers do not take the old error in `versions_->io_status()` as their own when they did not fail on MANIFEST I/O: - An atomic flush checks `versions_->io_status()` while it waits for its turn to install. The check stops the wait when the flush that it waits for failed its MANIFEST write. A flush that does not wait for another flush, while no background error is set, ignores the error and installs its result. That install writes a new MANIFEST, and its success clears the old error and the quarantine. While a background error is set, the flush returns the error and does not install, as before this change while io_status() holds the error. The check must stay in that case: when the DB is stopped, the code after the wait checks only the recovery error, which is OK when no recovery runs. This also fixes an upstream case. Upstream, a `SetOptions()` after a failed MANIFEST write resets io_status(). A later atomic flush could then install its result after a fatal background error stopped the DB. With this change, io_status() keeps the error until a new MANIFEST is written, so that flush returns the error instead. Without any old MANIFEST error, an atomic flush that is already running can still install after a fatal background error. That is older than this change and not about the MANIFEST, so this change leaves it. - `CompactionJob::Install()` takes `versions_->io_status()` as the job's I/O status only when the commit itself failed. A compaction that failed before its commit, for example when it could not write an output file, keeps its own I/O status. This change closes both problems above: the `SetOptions()` trigger, the "failure before MANIFEST I/O" trigger, and the second failed MANIFEST write, with or without a `SetOptions()` in between. The same rule also covers a running compaction whose commit fails at the CURRENT step after a flush's MANIFEST write failed (derived from the code, not tested separately). Consequences: - Quarantined files can stay longer, until the next new MANIFEST or a reopen. This only delays their deletion. - `DB::Resume()` writes a new MANIFEST when `versions_->io_status()` is an `IOError`. Without this change, a `SetOptions()` before the `Resume()` made it skip that write. With this change the write happens, and it clears the quarantine. - `versions_->io_status()` keeps an old MANIFEST write error until a new MANIFEST is written, and a `SetOptions()` call does not reset it. Several callers read it after their own failure and report it as their own error. This misattribution is older than this change: it already happened between a failed MANIFEST write and the next call with an OK MANIFEST I/O status. The worst case is an old non-retryable error that some call left without setting a background error. `DropColumnFamily()`, `CreateColumnFamily()`, `DeleteFilesInRanges()`, `PromoteL0()`, `IncreaseFullHistoryTsLow()`, the level refit of `CompactRange()`, and the file number reservation before an ingestion are such calls. A caller that takes that old error can set it as a fatal background error, and only a reopen recovers from that. This change fixes the two cases above (an atomic flush that does not wait, and a compaction that fails before its commit). Known cases that stay, all older than this change: - An atomic flush that waits for another flush still returns the old error. This needs two atomic flushes at the same time. - A compaction whose commit fails before MANIFEST I/O, for example with `Corruption` from `LoadTableHandlers()`, reports the old `IOError` with reason `kManifestWrite` instead of its own `Corruption`. The outputs of that compaction were never committed. - A compaction or flush that fails for its own reason picks the reason `kManifestWrite` or `kManifestWriteNoWAL` when the old error exists. For a retryable error of a compaction, this makes it a hard error instead of a soft error. Automatic recovery or `DB::Resume()` then writes a new MANIFEST and recovers. The worse case is with `paranoid_checks=false`. By the severity table, a non-retryable `IOError` is no error for `kCompaction` and `kFlushNoWAL`, but a fatal error for `kManifestWrite` and `kManifestWriteNoWAL`. A `NoSpace` error is no error for `kCompaction`, but a hard error for `kManifestWrite`. - Other calls that write the MANIFEST, such as `ApplyWALToManifest()` and the record of the OPTIONS file number in `WriteOptionsFile()`, set `versions_->io_status()` as a background error when their `LogAndApply()` fails, also when it failed before MANIFEST I/O. A complete fix tracks the MANIFEST I/O status of each batch, and lets a caller report `versions_->io_status()` only when its own commit failed on MANIFEST I/O. That touches `LogAndApply()`, flush, compaction and the DB callers above, and is planned as a follow-up. - An ingestion whose MANIFEST write fails deletes its ingested file in the rollback, even when the file is quarantined. This gap is older than this change, and it is on a different code path. A follow-up will fix it. - No API, option or format change. Differential Revision: D134907305
Summary: Pull Request resolved: meta#15818 Background: when a MANIFEST write fails, RocksDB cannot know whether the failed edit is in the MANIFEST that CURRENT names. For example, the record can be written and synced before the error is reported. Or a batch can create a new MANIFEST, rename CURRENT to it, and then get an error from the directory sync. So `VersionSet::ProcessManifestWrites()` puts the new files of the failed batch (the edits of one `ProcessManifestWrites()` call) in the error handler quarantine, and the purge of obsolete files does not delete them. When a MANIFEST write fails, `ProcessManifestWrites()` also resets `descriptor_log_`, and a batch without a `descriptor_log_` sets `new_descriptor_log`. So the next batch that writes the MANIFEST always starts a new MANIFEST. Once that new MANIFEST is installed, the old one is no longer used, and the quarantine is cleared. `VersionSet::io_status_` keeps the MANIFEST write error until then. How the code got into this state: - Since meta#7034, `ProcessManifestWrites()` resets `io_status_` after any call whose MANIFEST I/O status is OK. It records only the first error. - meta#12030 added the quarantine. It added the files of a failed batch only inside the "first error" block, and it cleared the quarantine together with the reset of `io_status_`. At that time `SetOptions()` still wrote to the MANIFEST. - meta#13384 made `SetOptions()` skip the MANIFEST write. So every `SetOptions()` became a call with an OK MANIFEST I/O status that writes no new MANIFEST. The problems: 1. Two kinds of calls have an OK MANIFEST I/O status but write no new MANIFEST: - `DBImpl::SetOptions()` installs a new Version through `LogAndApply()` with an edit that writes nothing to the MANIFEST (`skip_manifest_write`). - A batch can fail before any MANIFEST I/O, for example when `LoadTableHandlers()` fails with `paranoid_checks`. A compaction that is already running when a flush's MANIFEST write fails still commits, because the background error is checked only when the compaction starts. After such a call, the old MANIFEST can still hold the failed edit, but its files are no longer quarantined. 2. A second failed MANIFEST write, while `io_status_` already holds an earlier error, does not quarantine its own files. This is a problem when the second batch creates a new MANIFEST and renames CURRENT to it, and then reports an error. CURRENT names the new MANIFEST, and it lists the new files. In both cases, the purge after the failed job, or the purge in a clean `Close()`, deletes the files. When CURRENT names a MANIFEST that lists a deleted file, every `DB::Open()` fails with "Corruption: IO error: No such file or directory ... .sst The file MANIFEST-... may be corrupted". A db_stress crash test hit problem 1: a flush's MANIFEST write got an injected write error, a `SetOptions()` ran about 1.6 ms later and cleared the quarantine, the flush job deleted its SST, and the harness killed the process. This change cannot repair a DB that already lost such an SST. Example of problem 2, reproduced by the new unit test: ``` 1. DropColumnFamily(): the MANIFEST record is synced, then the write reports an error. io_status_ = that error. No background error is set. 2. (optional) SetOptions() succeeds. 3. Flush: starts a new MANIFEST that lists the flushed SST, renames CURRENT to it, then SetCurrentFile() reports an error. io_status_ is already bad, so the SST is not quarantined. 4. The flush job's purge deletes the SST. The next DB::Open() fails. ``` Without step 2, upstream fails this way. With step 2, upstream passes, because `SetOptions()` resets `io_status_` before the flush, so the flush failure is the "first" error. Fixing problem 1 alone would make the run with step 2 fail too, because `SetOptions()` would no longer reset `io_status_`. So this change fixes both problems. New behavior: - Every MANIFEST write failure adds the files of its batch to the quarantine. `io_status_` keeps the first error. A file number is added only once, because automatic recovery can retry many times, each retry adds the old MANIFEST again, and each `FindObsoleteFiles()` copies the list. - `io_status_` is reset and the quarantine is cleared only when the call wrote and installed a new MANIFEST (s is OK and `new_descriptor_log` is set). A batch that skips the MANIFEST write never sets `new_descriptor_log`. Until then, the files stay quarantined. This is enough for recovery: because `descriptor_log_` was reset, the next real MANIFEST write (for example, a non-atomic flush, a compaction, or the forced write in `DB::Resume()`) starts a new MANIFEST, and its success clears the whole quarantine. - `DBImpl::SetOptions()` sets no background error after its `LogAndApply()`. This part is required, not a cleanup. Since meta#13384, that call does no MANIFEST I/O and is never grouped with other writers, so a non-OK `io_status()` there always belongs to another, earlier batch. The removed block was `if (!versions_->io_status().ok()) { assert(!s.ok()); SetBGError(...); }`. With only the `VersionSet` change, `io_status()` stays non-OK after a successful `SetOptions()`. Then a debug build fails the assert, and a release build sets the old error as a fatal background error, so the next write fails. A backport must include this part. - Two callers do not take the old error in `versions_->io_status()` as their own when they did not fail on MANIFEST I/O: - An atomic flush checks `versions_->io_status()` while it waits for its turn to install. The check stops the wait when the flush that it waits for failed its MANIFEST write. A flush that does not wait for another flush, while no background error is set, ignores the error and installs its result. That install writes a new MANIFEST, and its success clears the old error and the quarantine. While a background error is set, the flush returns the error and does not install, as before this change while io_status() holds the error. The check must stay in that case: when the DB is stopped, the code after the wait checks only the recovery error, which is OK when no recovery runs. This also fixes an upstream case. Upstream, a `SetOptions()` after a failed MANIFEST write resets io_status(). A later atomic flush could then install its result after a fatal background error stopped the DB. With this change, io_status() keeps the error until a new MANIFEST is written, so that flush returns the error instead. Without any old MANIFEST error, an atomic flush that is already running can still install after a fatal background error. That is older than this change and not about the MANIFEST, so this change leaves it. - `CompactionJob::Install()` takes `versions_->io_status()` as the job's I/O status only when the commit itself failed. A compaction that failed before its commit, for example when it could not write an output file, keeps its own I/O status. This change closes both problems above: the `SetOptions()` trigger, the "failure before MANIFEST I/O" trigger, and the second failed MANIFEST write, with or without a `SetOptions()` in between. The same rule also covers a running compaction whose commit fails at the CURRENT step after a flush's MANIFEST write failed (derived from the code, not tested separately). Consequences: - Quarantined files can stay longer, until the next new MANIFEST or a reopen. This only delays their deletion. - `DB::Resume()` writes a new MANIFEST when `versions_->io_status()` is an `IOError`. Without this change, a `SetOptions()` before the `Resume()` made it skip that write. With this change the write happens, and it clears the quarantine. - `versions_->io_status()` keeps an old MANIFEST write error until a new MANIFEST is written, and a `SetOptions()` call does not reset it. Several callers read it after their own failure and report it as their own error. This misattribution is older than this change: it already happened between a failed MANIFEST write and the next call with an OK MANIFEST I/O status. The worst case is an old non-retryable error that some call left without setting a background error. `DropColumnFamily()`, `CreateColumnFamily()`, `DeleteFilesInRanges()`, `PromoteL0()`, `IncreaseFullHistoryTsLow()`, the level refit of `CompactRange()`, and the file number reservation before an ingestion are such calls. A caller that takes that old error can set it as a fatal background error, and only a reopen recovers from that. This change fixes the two cases above (an atomic flush that does not wait, and a compaction that fails before its commit). Known cases that stay, all older than this change: - An atomic flush that waits for another flush still returns the old error. This needs two atomic flushes at the same time. - A compaction whose commit fails before MANIFEST I/O, for example with `Corruption` from `LoadTableHandlers()`, reports the old `IOError` with reason `kManifestWrite` instead of its own `Corruption`. The outputs of that compaction were never committed. - A compaction or flush that fails for its own reason picks the reason `kManifestWrite` or `kManifestWriteNoWAL` when the old error exists. For a retryable error of a compaction, this makes it a hard error instead of a soft error. Automatic recovery or `DB::Resume()` then writes a new MANIFEST and recovers. The worse case is with `paranoid_checks=false`. By the severity table, a non-retryable `IOError` is no error for `kCompaction` and `kFlushNoWAL`, but a fatal error for `kManifestWrite` and `kManifestWriteNoWAL`. A `NoSpace` error is no error for `kCompaction`, but a hard error for `kManifestWrite`. - Other calls that write the MANIFEST, such as `ApplyWALToManifest()` and the record of the OPTIONS file number in `WriteOptionsFile()`, set `versions_->io_status()` as a background error when their `LogAndApply()` fails, also when it failed before MANIFEST I/O. A complete fix tracks the MANIFEST I/O status of each batch, and lets a caller report `versions_->io_status()` only when its own commit failed on MANIFEST I/O. That touches `LogAndApply()`, flush, compaction and the DB callers above, and is planned as a follow-up. - An ingestion whose MANIFEST write fails deletes its ingested file in the rollback, even when the file is quarantined. This gap is older than this change, and it is on a different code path. A follow-up will fix it. - No API, option or format change. Differential Revision: D134907305
Summary: Pull Request resolved: meta#15818 Background: when a MANIFEST write fails, RocksDB cannot know whether the failed edit is in the MANIFEST that CURRENT names. For example, the record can be written and synced before the error is reported. Or a batch can create a new MANIFEST, rename CURRENT to it, and then get an error from the directory sync. So `VersionSet::ProcessManifestWrites()` puts the new files of the failed batch (the edits of one `ProcessManifestWrites()` call) in the error handler quarantine, and the purge of obsolete files does not delete them. When a MANIFEST write fails, `ProcessManifestWrites()` also resets `descriptor_log_`, and a batch without a `descriptor_log_` sets `new_descriptor_log`. So the next batch that writes the MANIFEST always starts a new MANIFEST. Once that new MANIFEST is installed, the old one is no longer used, and the quarantine is cleared. `VersionSet::io_status_` keeps the MANIFEST write error until then. How the code got into this state: - Since meta#7034, `ProcessManifestWrites()` resets `io_status_` after any call whose MANIFEST I/O status is OK. It records only the first error. - meta#12030 added the quarantine. It added the files of a failed batch only inside the "first error" block, and it cleared the quarantine together with the reset of `io_status_`. At that time `SetOptions()` still wrote to the MANIFEST. - meta#13384 made `SetOptions()` skip the MANIFEST write. So every `SetOptions()` became a call with an OK MANIFEST I/O status that writes no new MANIFEST. The problems: 1. Two kinds of calls have an OK MANIFEST I/O status but write no new MANIFEST: - `DBImpl::SetOptions()` installs a new Version through `LogAndApply()` with an edit that writes nothing to the MANIFEST (`skip_manifest_write`). - A batch can fail before any MANIFEST I/O, for example when `LoadTableHandlers()` fails with `paranoid_checks`. A compaction that is already running when a flush's MANIFEST write fails still commits, because the background error is checked only when the compaction starts. After such a call, the old MANIFEST can still hold the failed edit, but its files are no longer quarantined. 2. A second failed MANIFEST write, while `io_status_` already holds an earlier error, does not quarantine its own files. This is a problem when the second batch creates a new MANIFEST and renames CURRENT to it, and then reports an error. CURRENT names the new MANIFEST, and it lists the new files. In both cases, the purge after the failed job, or the purge in a clean `Close()`, deletes the files. When CURRENT names a MANIFEST that lists a deleted file, every `DB::Open()` fails with "Corruption: IO error: No such file or directory ... .sst The file MANIFEST-... may be corrupted". A db_stress crash test hit problem 1: a flush's MANIFEST write got an injected write error, a `SetOptions()` ran about 1.6 ms later and cleared the quarantine, the flush job deleted its SST, and the harness killed the process. This change cannot repair a DB that already lost such an SST. Example of problem 2, reproduced by the new unit test: ``` 1. DropColumnFamily(): the MANIFEST record is synced, then the write reports an error. io_status_ = that error. No background error is set. 2. (optional) SetOptions() succeeds. 3. Flush: starts a new MANIFEST that lists the flushed SST, renames CURRENT to it, then SetCurrentFile() reports an error. io_status_ is already bad, so the SST is not quarantined. 4. The flush job's purge deletes the SST. The next DB::Open() fails. ``` Without step 2, upstream fails this way. With step 2, upstream passes, because `SetOptions()` resets `io_status_` before the flush, so the flush failure is the "first" error. Fixing problem 1 alone would make the run with step 2 fail too, because `SetOptions()` would no longer reset `io_status_`. So this change fixes both problems. New behavior: - Every MANIFEST write failure adds the files of its batch to the quarantine. `io_status_` keeps the first error. A file number is added only once, because automatic recovery can retry many times, each retry adds the old MANIFEST again, and each `FindObsoleteFiles()` copies the list. - `io_status_` is reset and the quarantine is cleared only when the call wrote and installed a new MANIFEST (s is OK and `new_descriptor_log` is set). A batch that skips the MANIFEST write never sets `new_descriptor_log`. Until then, the files stay quarantined. This is enough for recovery: because `descriptor_log_` was reset, the next real MANIFEST write (for example, a non-atomic flush, a compaction, or the forced write in `DB::Resume()`) starts a new MANIFEST, and its success clears the whole quarantine. - `DBImpl::SetOptions()` sets no background error after its `LogAndApply()`. This part is required, not a cleanup. Since meta#13384, that call does no MANIFEST I/O and is never grouped with other writers, so a non-OK `io_status()` there always belongs to another, earlier batch. The removed block was `if (!versions_->io_status().ok()) { assert(!s.ok()); SetBGError(...); }`. With only the `VersionSet` change, `io_status()` stays non-OK after a successful `SetOptions()`. Then a debug build fails the assert, and a release build sets the old error as a fatal background error, so the next write fails. A backport must include this part. - Two callers do not take the old error in `versions_->io_status()` as their own when they did not fail on MANIFEST I/O: - An atomic flush checks `versions_->io_status()` while it waits for its turn to install. The check stops the wait when the flush that it waits for failed its MANIFEST write. A flush that does not wait for another flush, while no background error is set, ignores the error and installs its result. That install writes a new MANIFEST, and its success clears the old error and the quarantine. While a background error is set, the flush returns the error and does not install, as before this change while io_status() holds the error. The check must stay in that case: when the DB is stopped, the code after the wait checks only the recovery error, which is OK when no recovery runs. This also fixes an upstream case. Upstream, a `SetOptions()` after a failed MANIFEST write resets io_status(). A later atomic flush could then install its result after a fatal background error stopped the DB. With this change, io_status() keeps the error until a new MANIFEST is written, so that flush returns the error instead. Without any old MANIFEST error, an atomic flush that is already running can still install after a fatal background error. That is older than this change and not about the MANIFEST, so this change leaves it. - `CompactionJob::Install()` takes `versions_->io_status()` as the job's I/O status only when the commit itself failed. A compaction that failed before its commit, for example when it could not write an output file, keeps its own I/O status. This change closes both problems above: the `SetOptions()` trigger, the "failure before MANIFEST I/O" trigger, and the second failed MANIFEST write, with or without a `SetOptions()` in between. The same rule also covers a running compaction whose commit fails at the CURRENT step after a flush's MANIFEST write failed (derived from the code, not tested separately). Consequences: - Quarantined files can stay longer, until the next new MANIFEST or a reopen. This only delays their deletion. - `DB::Resume()` writes a new MANIFEST when `versions_->io_status()` is an `IOError`. Without this change, a `SetOptions()` before the `Resume()` made it skip that write. With this change the write happens, and it clears the quarantine. - `versions_->io_status()` keeps an old MANIFEST write error until a new MANIFEST is written, and a `SetOptions()` call does not reset it. Several callers read it after their own failure and report it as their own error. This misattribution is older than this change: it already happened between a failed MANIFEST write and the next call with an OK MANIFEST I/O status. The worst case is an old non-retryable error that some call left without setting a background error. `DropColumnFamily()`, `CreateColumnFamily()`, `DeleteFilesInRanges()`, `PromoteL0()`, `IncreaseFullHistoryTsLow()`, the level refit of `CompactRange()`, and the file number reservation before an ingestion are such calls. A caller that takes that old error can set it as a fatal background error, and only a reopen recovers from that. This change fixes the two cases above (an atomic flush that does not wait, and a compaction that fails before its commit). Known cases that stay, all older than this change: - An atomic flush that waits for another flush still returns the old error. This needs two atomic flushes at the same time. - A compaction whose commit fails before MANIFEST I/O, for example with `Corruption` from `LoadTableHandlers()`, reports the old `IOError` with reason `kManifestWrite` instead of its own `Corruption`. The outputs of that compaction were never committed. - A compaction or flush that fails for its own reason picks the reason `kManifestWrite` or `kManifestWriteNoWAL` when the old error exists. For a retryable error of a compaction, this makes it a hard error instead of a soft error. Automatic recovery or `DB::Resume()` then writes a new MANIFEST and recovers. The worse case is with `paranoid_checks=false`. By the severity table, a non-retryable `IOError` is no error for `kCompaction` and `kFlushNoWAL`, but a fatal error for `kManifestWrite` and `kManifestWriteNoWAL`. A `NoSpace` error is no error for `kCompaction`, but a hard error for `kManifestWrite`. - Other calls that write the MANIFEST, such as `ApplyWALToManifest()` and the record of the OPTIONS file number in `WriteOptionsFile()`, set `versions_->io_status()` as a background error when their `LogAndApply()` fails, also when it failed before MANIFEST I/O. A complete fix tracks the MANIFEST I/O status of each batch, and lets a caller report `versions_->io_status()` only when its own commit failed on MANIFEST I/O. That touches `LogAndApply()`, flush, compaction and the DB callers above, and is planned as a follow-up. - An ingestion whose MANIFEST write fails deletes its ingested file in the rollback, even when the file is quarantined. This gap is older than this change, and it is on a different code path. A follow-up will fix it. - No API, option or format change. Differential Revision: D134907305
Summary: Pull Request resolved: meta#15818 Background: when a MANIFEST write fails, RocksDB cannot know whether the failed edit is in the MANIFEST that CURRENT names. For example, the record can be written and synced before the error is reported. Or a batch can create a new MANIFEST, rename CURRENT to it, and then get an error from the directory sync. So `VersionSet::ProcessManifestWrites()` puts the new files of the failed batch (the edits of one `ProcessManifestWrites()` call) in the error handler quarantine, and the purge of obsolete files does not delete them. When a MANIFEST write fails, `ProcessManifestWrites()` also resets `descriptor_log_`, and a batch without a `descriptor_log_` sets `new_descriptor_log`. So the next batch that writes the MANIFEST always starts a new MANIFEST. Once that new MANIFEST is installed, the old one is no longer used, and the quarantine is cleared. `VersionSet::io_status_` keeps the MANIFEST write error until then. How the code got into this state: - Since meta#7034, `ProcessManifestWrites()` resets `io_status_` after any call whose MANIFEST I/O status is OK. It records only the first error. - meta#12030 added the quarantine. It added the files of a failed batch only inside the "first error" block, and it cleared the quarantine together with the reset of `io_status_`. At that time `SetOptions()` still wrote to the MANIFEST. - meta#13384 made `SetOptions()` skip the MANIFEST write. So every `SetOptions()` became a call with an OK MANIFEST I/O status that writes no new MANIFEST. The problems: 1. Two kinds of calls have an OK MANIFEST I/O status but write no new MANIFEST: - `DBImpl::SetOptions()` installs a new Version through `LogAndApply()` with an edit that writes nothing to the MANIFEST (`skip_manifest_write`). - A batch can fail before any MANIFEST I/O, for example when `LoadTableHandlers()` fails with `paranoid_checks`. A compaction that is already running when a flush's MANIFEST write fails still commits, because the background error is checked only when the compaction starts. After such a call, the old MANIFEST can still hold the failed edit, but its files are no longer quarantined. 2. A second failed MANIFEST write, while `io_status_` already holds an earlier error, does not quarantine its own files. This is a problem when the second batch creates a new MANIFEST and renames CURRENT to it, and then reports an error. CURRENT names the new MANIFEST, and it lists the new files. In both cases, the purge after the failed job, or the purge in a clean `Close()`, deletes the files. When CURRENT names a MANIFEST that lists a deleted file, every `DB::Open()` fails with "Corruption: IO error: No such file or directory ... .sst The file MANIFEST-... may be corrupted". A db_stress crash test hit problem 1: a flush's MANIFEST write got an injected write error, a `SetOptions()` ran about 1.6 ms later and cleared the quarantine, the flush job deleted its SST, and the harness killed the process. This change cannot repair a DB that already lost such an SST. Example of problem 2, reproduced by the new unit test: ``` 1. DropColumnFamily(): the MANIFEST record is synced, then the write reports an error. io_status_ = that error. No background error is set. 2. (optional) SetOptions() succeeds. 3. Flush: starts a new MANIFEST that lists the flushed SST, renames CURRENT to it, then SetCurrentFile() reports an error. io_status_ is already bad, so the SST is not quarantined. 4. The flush job's purge deletes the SST. The next DB::Open() fails. ``` Without step 2, upstream fails this way. With step 2, upstream passes, because `SetOptions()` resets `io_status_` before the flush, so the flush failure is the "first" error. Fixing problem 1 alone would make the run with step 2 fail too, because `SetOptions()` would no longer reset `io_status_`. So this change fixes both problems. New behavior: - Every MANIFEST write failure adds the files of its batch to the quarantine. `io_status_` keeps the first error. A file number is added only once, because automatic recovery can retry many times, each retry adds the old MANIFEST again, and each `FindObsoleteFiles()` copies the list. - `io_status_` is reset and the quarantine is cleared only when the call wrote and installed a new MANIFEST (s is OK and `new_descriptor_log` is set). A batch that skips the MANIFEST write never sets `new_descriptor_log`. Until then, the files stay quarantined. This is enough for recovery: because `descriptor_log_` was reset, the next real MANIFEST write (for example, a non-atomic flush, a compaction, or the forced write in `DB::Resume()`) starts a new MANIFEST, and its success clears the whole quarantine. - `DBImpl::SetOptions()` sets no background error after its `LogAndApply()`. This part is required, not a cleanup. Since meta#13384, that call does no MANIFEST I/O and is never grouped with other writers, so a non-OK `io_status()` there always belongs to another, earlier batch. The removed block was `if (!versions_->io_status().ok()) { assert(!s.ok()); SetBGError(...); }`. With only the `VersionSet` change, `io_status()` stays non-OK after a successful `SetOptions()`. Then a debug build fails the assert, and a release build sets the old error as a fatal background error, so the next write fails. A backport must include this part. - Two callers do not take the old error in `versions_->io_status()` as their own when they did not fail on MANIFEST I/O: - An atomic flush checks `versions_->io_status()` while it waits for its turn to install. The check stops the wait when the flush that it waits for failed its MANIFEST write. A flush that does not wait for another flush, while no background error is set, ignores the error and installs its result. That install writes a new MANIFEST, and its success clears the old error and the quarantine. While a background error is set, the flush returns the error and does not install, as before this change while io_status() holds the error. The check must stay in that case: when the DB is stopped, the code after the wait checks only the recovery error, which is OK when no recovery runs. This also fixes an upstream case. Upstream, a `SetOptions()` after a failed MANIFEST write resets io_status(). A later atomic flush could then install its result after a fatal background error stopped the DB. With this change, io_status() keeps the error until a new MANIFEST is written, so that flush returns the error instead. Without any old MANIFEST error, an atomic flush that is already running can still install after a fatal background error. That is older than this change and not about the MANIFEST, so this change leaves it. - `CompactionJob::Install()` takes `versions_->io_status()` as the job's I/O status only when the commit itself failed. A compaction that failed before its commit, for example when it could not write an output file, keeps its own I/O status. This change closes both problems above: the `SetOptions()` trigger, the "failure before MANIFEST I/O" trigger, and the second failed MANIFEST write, with or without a `SetOptions()` in between. The same rule also covers a running compaction whose commit fails at the CURRENT step after a flush's MANIFEST write failed (derived from the code, not tested separately). Consequences: - Quarantined files can stay longer, until the next new MANIFEST or a reopen. This only delays their deletion. - `DB::Resume()` writes a new MANIFEST when `versions_->io_status()` is an `IOError`. Without this change, a `SetOptions()` before the `Resume()` made it skip that write. With this change the write happens, and it clears the quarantine. - `versions_->io_status()` keeps an old MANIFEST write error until a new MANIFEST is written, and a `SetOptions()` call does not reset it. Several callers read it after their own failure and report it as their own error. This misattribution is older than this change: it already happened between a failed MANIFEST write and the next call with an OK MANIFEST I/O status. The worst case is an old non-retryable error that some call left without setting a background error. `DropColumnFamily()`, `CreateColumnFamily()`, `DeleteFilesInRanges()`, `PromoteL0()`, `IncreaseFullHistoryTsLow()`, the level refit of `CompactRange()`, and the file number reservation before an ingestion are such calls. A caller that takes that old error can set it as a fatal background error, and only a reopen recovers from that. This change fixes the two cases above (an atomic flush that does not wait, and a compaction that fails before its commit). Known cases that stay, all older than this change: - An atomic flush that waits for another flush still returns the old error. This needs two atomic flushes at the same time. - A compaction whose commit fails before MANIFEST I/O, for example with `Corruption` from `LoadTableHandlers()`, reports the old `IOError` with reason `kManifestWrite` instead of its own `Corruption`. The outputs of that compaction were never committed. - A compaction or flush that fails for its own reason picks the reason `kManifestWrite` or `kManifestWriteNoWAL` when the old error exists. For a retryable error of a compaction, this makes it a hard error instead of a soft error. Automatic recovery or `DB::Resume()` then writes a new MANIFEST and recovers. The worse case is with `paranoid_checks=false`. By the severity table, a non-retryable `IOError` is no error for `kCompaction` and `kFlushNoWAL`, but a fatal error for `kManifestWrite` and `kManifestWriteNoWAL`. A `NoSpace` error is no error for `kCompaction`, but a hard error for `kManifestWrite`. - Other calls that write the MANIFEST, such as `ApplyWALToManifest()` and the record of the OPTIONS file number in `WriteOptionsFile()`, set `versions_->io_status()` as a background error when their `LogAndApply()` fails, also when it failed before MANIFEST I/O. A complete fix tracks the MANIFEST I/O status of each batch, and lets a caller report `versions_->io_status()` only when its own commit failed on MANIFEST I/O. That touches `LogAndApply()`, flush, compaction and the DB callers above, and is planned as a follow-up. - An ingestion whose MANIFEST write fails deletes its ingested file in the rollback, even when the file is quarantined. This gap is older than this change, and it is on a different code path. A follow-up will fix it. - No API, option or format change. Differential Revision: D134907305
Summary: Pull Request resolved: meta#15818 Background: when a MANIFEST write fails, RocksDB cannot know whether the failed edit is in the MANIFEST that CURRENT names. For example, the record can be written and synced before the error is reported. Or a batch can create a new MANIFEST, rename CURRENT to it, and then get an error from the directory sync. So `VersionSet::ProcessManifestWrites()` puts the new files of the failed batch (the edits of one `ProcessManifestWrites()` call) in the error handler quarantine, and the purge of obsolete files does not delete them. When a MANIFEST write fails, `ProcessManifestWrites()` also resets `descriptor_log_`, and a batch without a `descriptor_log_` sets `new_descriptor_log`. So the next batch that writes the MANIFEST always starts a new MANIFEST. Once that new MANIFEST is installed, the old one is no longer used, and the quarantine is cleared. `VersionSet::io_status_` keeps the MANIFEST write error until then. How the code got into this state: - Since meta#7034, `ProcessManifestWrites()` resets `io_status_` after any call whose MANIFEST I/O status is OK. It records only the first error. - meta#12030 added the quarantine. It added the files of a failed batch only inside the "first error" block, and it cleared the quarantine together with the reset of `io_status_`. At that time `SetOptions()` still wrote to the MANIFEST. - meta#13384 made `SetOptions()` skip the MANIFEST write. So every `SetOptions()` became a call with an OK MANIFEST I/O status that writes no new MANIFEST. The problems: 1. Two kinds of calls have an OK MANIFEST I/O status but write no new MANIFEST: - `DBImpl::SetOptions()` installs a new Version through `LogAndApply()` with an edit that writes nothing to the MANIFEST (`skip_manifest_write`). - A batch can fail before any MANIFEST I/O, for example when `LoadTableHandlers()` fails with `paranoid_checks`. A compaction that is already running when a flush's MANIFEST write fails still commits, because the background error is checked only when the compaction starts. After such a call, the old MANIFEST can still hold the failed edit, but its files are no longer quarantined. 2. A second failed MANIFEST write, while `io_status_` already holds an earlier error, does not quarantine its own files. This is a problem when the second batch creates a new MANIFEST and renames CURRENT to it, and then reports an error. CURRENT names the new MANIFEST, and it lists the new files. In both cases, the purge after the failed job, or the purge in a clean `Close()`, deletes the files. When CURRENT names a MANIFEST that lists a deleted file, every `DB::Open()` fails with "Corruption: IO error: No such file or directory ... .sst The file MANIFEST-... may be corrupted". A db_stress crash test hit problem 1: a flush's MANIFEST write got an injected write error, a `SetOptions()` ran about 1.6 ms later and cleared the quarantine, the flush job deleted its SST, and the harness killed the process. This change cannot repair a DB that already lost such an SST. Example of problem 2, reproduced by the new unit test: ``` 1. DropColumnFamily(): the MANIFEST record is synced, then the write reports an error. io_status_ = that error. No background error is set. 2. (optional) SetOptions() succeeds. 3. Flush: starts a new MANIFEST that lists the flushed SST, renames CURRENT to it, then SetCurrentFile() reports an error. io_status_ is already bad, so the SST is not quarantined. 4. The flush job's purge deletes the SST. The next DB::Open() fails. ``` Without step 2, upstream fails this way. With step 2, upstream passes, because `SetOptions()` resets `io_status_` before the flush, so the flush failure is the "first" error. Fixing problem 1 alone would make the run with step 2 fail too, because `SetOptions()` would no longer reset `io_status_`. So this change fixes both problems. New behavior: - Every MANIFEST write failure adds the files of its batch to the quarantine. `io_status_` keeps the first error. A file number is added only once, because automatic recovery can retry many times, each retry adds the old MANIFEST again, and each `FindObsoleteFiles()` copies the list. - `io_status_` is reset and the quarantine is cleared only when the call wrote and installed a new MANIFEST (s is OK and `new_descriptor_log` is set). A batch that skips the MANIFEST write never sets `new_descriptor_log`. Until then, the files stay quarantined. This is enough for recovery: because `descriptor_log_` was reset, the next real MANIFEST write (for example, a non-atomic flush, a compaction, or the forced write in `DB::Resume()`) starts a new MANIFEST, and its success clears the whole quarantine. - `DBImpl::SetOptions()` sets no background error after its `LogAndApply()`. This part is required, not a cleanup. Since meta#13384, that call does no MANIFEST I/O and is never grouped with other writers, so a non-OK `io_status()` there always belongs to another, earlier batch. The removed block was `if (!versions_->io_status().ok()) { assert(!s.ok()); SetBGError(...); }`. With only the `VersionSet` change, `io_status()` stays non-OK after a successful `SetOptions()`. Then a debug build fails the assert, and a release build sets the old error as a fatal background error, so the next write fails. A backport must include this part. - Two callers do not take the old error in `versions_->io_status()` as their own when they did not fail on MANIFEST I/O: - An atomic flush checks `versions_->io_status()` while it waits for its turn to install. The check stops the wait when the flush that it waits for failed its MANIFEST write. A flush that does not wait for another flush, while no background error is set, ignores the error and installs its result. That install writes a new MANIFEST, and its success clears the old error and the quarantine. While a background error is set, the flush returns the error and does not install, as before this change while io_status() holds the error. The check must stay in that case: when the DB is stopped, the code after the wait checks only the recovery error, which is OK when no recovery runs. This also fixes an upstream case. Upstream, a `SetOptions()` after a failed MANIFEST write resets io_status(). A later atomic flush could then install its result after a fatal background error stopped the DB. With this change, io_status() keeps the error until a new MANIFEST is written, so that flush returns the error instead. Without any old MANIFEST error, an atomic flush that is already running can still install after a fatal background error. That is older than this change and not about the MANIFEST, so this change leaves it. - `CompactionJob::Install()` takes `versions_->io_status()` as the job's I/O status only when the commit itself failed. A compaction that failed before its commit, for example when it could not write an output file, keeps its own I/O status. This change closes both problems above: the `SetOptions()` trigger, the "failure before MANIFEST I/O" trigger, and the second failed MANIFEST write, with or without a `SetOptions()` in between. The same rule also covers a running compaction whose commit fails at the CURRENT step after a flush's MANIFEST write failed (derived from the code, not tested separately). Consequences: - Quarantined files can stay longer, until the next new MANIFEST or a reopen. This only delays their deletion. - `DB::Resume()` writes a new MANIFEST when `versions_->io_status()` is an `IOError`. Without this change, a `SetOptions()` before the `Resume()` made it skip that write. With this change the write happens, and it clears the quarantine. - `versions_->io_status()` keeps an old MANIFEST write error until a new MANIFEST is written, and a `SetOptions()` call does not reset it. Several callers read it after their own failure and report it as their own error. This misattribution is older than this change: it already happened between a failed MANIFEST write and the next call with an OK MANIFEST I/O status. The worst case is an old non-retryable error that some call left without setting a background error. `DropColumnFamily()`, `CreateColumnFamily()`, `DeleteFilesInRanges()`, `PromoteL0()`, `IncreaseFullHistoryTsLow()`, the level refit of `CompactRange()`, and the file number reservation before an ingestion are such calls. A caller that takes that old error can set it as a fatal background error, and only a reopen recovers from that. This change fixes the two cases above (an atomic flush that does not wait, and a compaction that fails before its commit). Known cases that stay, all older than this change: - An atomic flush that waits for another flush still returns the old error. This needs two atomic flushes at the same time. - A compaction whose commit fails before MANIFEST I/O, for example with `Corruption` from `LoadTableHandlers()`, reports the old `IOError` with reason `kManifestWrite` instead of its own `Corruption`. The outputs of that compaction were never committed. - A compaction or flush that fails for its own reason picks the reason `kManifestWrite` or `kManifestWriteNoWAL` when the old error exists. For a retryable error of a compaction, this makes it a hard error instead of a soft error. Automatic recovery or `DB::Resume()` then writes a new MANIFEST and recovers. The worse case is with `paranoid_checks=false`. By the severity table, a non-retryable `IOError` is no error for `kCompaction` and `kFlushNoWAL`, but a fatal error for `kManifestWrite` and `kManifestWriteNoWAL`. A `NoSpace` error is no error for `kCompaction`, but a hard error for `kManifestWrite`. - Other calls that write the MANIFEST, such as `ApplyWALToManifest()` and the record of the OPTIONS file number in `WriteOptionsFile()`, set `versions_->io_status()` as a background error when their `LogAndApply()` fails, also when it failed before MANIFEST I/O. A complete fix tracks the MANIFEST I/O status of each batch, and lets a caller report `versions_->io_status()` only when its own commit failed on MANIFEST I/O. That touches `LogAndApply()`, flush, compaction and the DB callers above, and is planned as a follow-up. - An ingestion whose MANIFEST write fails deletes its ingested file in the rollback, even when the file is quarantined. This gap is older than this change, and it is on a different code path. A follow-up will fix it. - No API, option or format change. Differential Revision: D134907305
Summary: Pull Request resolved: meta#15818 Background: when a MANIFEST write fails, RocksDB cannot know whether the failed edit is in the MANIFEST that CURRENT names. For example, the record can be written and synced before the error is reported. Or a batch can create a new MANIFEST, rename CURRENT to it, and then get an error from the directory sync. So `VersionSet::ProcessManifestWrites()` puts the new files of the failed batch (the edits of one `ProcessManifestWrites()` call) in the error handler quarantine, and the purge of obsolete files does not delete them. When a MANIFEST write fails, `ProcessManifestWrites()` also resets `descriptor_log_`, and a batch without a `descriptor_log_` sets `new_descriptor_log`. So the next batch that writes the MANIFEST always starts a new MANIFEST. Once that new MANIFEST is installed, the old one is no longer used, and the quarantine is cleared. `VersionSet::io_status_` keeps the MANIFEST write error until then. How the code got into this state: - Since meta#7034, `ProcessManifestWrites()` resets `io_status_` after any call whose MANIFEST I/O status is OK. It records only the first error. - meta#12030 added the quarantine. It added the files of a failed batch only inside the "first error" block, and it cleared the quarantine together with the reset of `io_status_`. At that time `SetOptions()` still wrote to the MANIFEST. - meta#13384 made `SetOptions()` skip the MANIFEST write. So every `SetOptions()` became a call with an OK MANIFEST I/O status that writes no new MANIFEST. The problems: 1. Two kinds of calls have an OK MANIFEST I/O status but write no new MANIFEST: - `DBImpl::SetOptions()` installs a new Version through `LogAndApply()` with an edit that writes nothing to the MANIFEST (`skip_manifest_write`). - A batch can fail before any MANIFEST I/O, for example when `LoadTableHandlers()` fails with `paranoid_checks`. A compaction that is already running when a flush's MANIFEST write fails still commits, because the background error is checked only when the compaction starts. After such a call, the old MANIFEST can still hold the failed edit, but its files are no longer quarantined. 2. A second failed MANIFEST write, while `io_status_` already holds an earlier error, does not quarantine its own files. This is a problem when the second batch creates a new MANIFEST and renames CURRENT to it, and then reports an error. CURRENT names the new MANIFEST, and it lists the new files. In both cases, the purge after the failed job, or the purge in a clean `Close()`, deletes the files. When CURRENT names a MANIFEST that lists a deleted file, every `DB::Open()` fails with "Corruption: IO error: No such file or directory ... .sst The file MANIFEST-... may be corrupted". A db_stress crash test hit problem 1: a flush's MANIFEST write got an injected write error, a `SetOptions()` ran about 1.6 ms later and cleared the quarantine, the flush job deleted its SST, and the harness killed the process. This change cannot repair a DB that already lost such an SST. Example of problem 2, reproduced by the new unit test: ``` 1. DropColumnFamily(): the MANIFEST record is synced, then the write reports an error. io_status_ = that error. No background error is set. 2. (optional) SetOptions() succeeds. 3. Flush: starts a new MANIFEST that lists the flushed SST, renames CURRENT to it, then SetCurrentFile() reports an error. io_status_ is already bad, so the SST is not quarantined. 4. The flush job's purge deletes the SST. The next DB::Open() fails. ``` Without step 2, upstream fails this way. With step 2, upstream passes, because `SetOptions()` resets `io_status_` before the flush, so the flush failure is the "first" error. Fixing problem 1 alone would make the run with step 2 fail too, because `SetOptions()` would no longer reset `io_status_`. So this change fixes both problems. New behavior: - Every MANIFEST write failure adds the files of its batch to the quarantine. `io_status_` keeps the first error. A file number is added only once, because automatic recovery can retry many times, each retry adds the old MANIFEST again, and each `FindObsoleteFiles()` copies the list. - `io_status_` is reset and the quarantine is cleared only when the call wrote and installed a new MANIFEST (s is OK and `new_descriptor_log` is set). A batch that skips the MANIFEST write never sets `new_descriptor_log`. Until then, the files stay quarantined. This is enough for recovery: because `descriptor_log_` was reset, the next real MANIFEST write (for example, a non-atomic flush, a compaction, or the forced write in `DB::Resume()`) starts a new MANIFEST, and its success clears the whole quarantine. - `DBImpl::SetOptions()` sets no background error after its `LogAndApply()`. This part is required, not a cleanup. Since meta#13384, that call does no MANIFEST I/O and is never grouped with other writers, so a non-OK `io_status()` there always belongs to another, earlier batch. The removed block was `if (!versions_->io_status().ok()) { assert(!s.ok()); SetBGError(...); }`. With only the `VersionSet` change, `io_status()` stays non-OK after a successful `SetOptions()`. Then a debug build fails the assert, and a release build sets the old error as a fatal background error, so the next write fails. A backport must include this part. - Two callers do not take the old error in `versions_->io_status()` as their own when they did not fail on MANIFEST I/O: - An atomic flush checks `versions_->io_status()` while it waits for its turn to install. The check stops the wait when the flush that it waits for failed its MANIFEST write. A flush that does not wait for another flush, while no background error is set, ignores the error and installs its result. That install writes a new MANIFEST, and its success clears the old error and the quarantine. While a background error is set, the flush returns the error and does not install, as before this change while io_status() holds the error. The check must stay in that case: when the DB is stopped, the code after the wait checks only the recovery error, which is OK when no recovery runs. This also fixes an upstream case. Upstream, a `SetOptions()` after a failed MANIFEST write resets io_status(). A later atomic flush could then install its result after a fatal background error stopped the DB. With this change, io_status() keeps the error until a new MANIFEST is written, so that flush returns the error instead. Without any old MANIFEST error, an atomic flush that is already running can still install after a fatal background error. That is older than this change and not about the MANIFEST, so this change leaves it. - `CompactionJob::Install()` takes `versions_->io_status()` as the job's I/O status only when the commit itself failed. A compaction that failed before its commit, for example when it could not write an output file, keeps its own I/O status. This change closes both problems above: the `SetOptions()` trigger, the "failure before MANIFEST I/O" trigger, and the second failed MANIFEST write, with or without a `SetOptions()` in between. The same rule also covers a running compaction whose commit fails at the CURRENT step after a flush's MANIFEST write failed (derived from the code, not tested separately). Consequences: - Quarantined files can stay longer, until the next new MANIFEST or a reopen. This only delays their deletion. - `DB::Resume()` writes a new MANIFEST when `versions_->io_status()` is an `IOError`. Without this change, a `SetOptions()` before the `Resume()` made it skip that write. With this change the write happens, and it clears the quarantine. - `versions_->io_status()` keeps an old MANIFEST write error until a new MANIFEST is written, and a `SetOptions()` call does not reset it. Several callers read it after their own failure and report it as their own error. This misattribution is older than this change: it already happened between a failed MANIFEST write and the next call with an OK MANIFEST I/O status. The worst case is an old non-retryable error that some call left without setting a background error. `DropColumnFamily()`, `CreateColumnFamily()`, `DeleteFilesInRanges()`, `PromoteL0()`, `IncreaseFullHistoryTsLow()`, the level refit of `CompactRange()`, and the file number reservation before an ingestion are such calls. A caller that takes that old error can set it as a fatal background error, and only a reopen recovers from that. This change fixes the two cases above (an atomic flush that does not wait, and a compaction that fails before its commit). Known cases that stay, all older than this change: - An atomic flush that waits for another flush still returns the old error. This needs two atomic flushes at the same time. - A compaction whose commit fails before MANIFEST I/O, for example with `Corruption` from `LoadTableHandlers()`, reports the old `IOError` with reason `kManifestWrite` instead of its own `Corruption`. The outputs of that compaction were never committed. - A compaction or flush that fails for its own reason picks the reason `kManifestWrite` or `kManifestWriteNoWAL` when the old error exists. For a retryable error of a compaction, this makes it a hard error instead of a soft error. Automatic recovery or `DB::Resume()` then writes a new MANIFEST and recovers. The worse case is with `paranoid_checks=false`. By the severity table, a non-retryable `IOError` is no error for `kCompaction` and `kFlushNoWAL`, but a fatal error for `kManifestWrite` and `kManifestWriteNoWAL`. A `NoSpace` error is no error for `kCompaction`, but a hard error for `kManifestWrite`. - Other calls that write the MANIFEST, such as `ApplyWALToManifest()` and the record of the OPTIONS file number in `WriteOptionsFile()`, set `versions_->io_status()` as a background error when their `LogAndApply()` fails, also when it failed before MANIFEST I/O. A complete fix tracks the MANIFEST I/O status of each batch, and lets a caller report `versions_->io_status()` only when its own commit failed on MANIFEST I/O. That touches `LogAndApply()`, flush, compaction and the DB callers above, and is planned as a follow-up. - An ingestion whose MANIFEST write fails deletes its ingested file in the rollback, even when the file is quarantined. This gap is older than this change, and it is on a different code path. A follow-up will fix it. - No API, option or format change. Differential Revision: D134907305
Summary: Pull Request resolved: meta#15818 Background: when a MANIFEST write fails, RocksDB cannot know whether the failed edit is in the MANIFEST that CURRENT names. For example, the record can be written and synced before the error is reported. Or a batch can create a new MANIFEST, rename CURRENT to it, and then get an error from the directory sync. So `VersionSet::ProcessManifestWrites()` puts the new files of the failed batch (the edits of one `ProcessManifestWrites()` call) in the error handler quarantine, and the purge of obsolete files does not delete them. When a MANIFEST write fails, `ProcessManifestWrites()` also resets `descriptor_log_`, and a batch without a `descriptor_log_` sets `new_descriptor_log`. So the next batch that writes the MANIFEST always starts a new MANIFEST. Once that new MANIFEST is installed, the old one is no longer used, and the quarantine is cleared. `VersionSet::io_status_` keeps the MANIFEST write error until then. How the code got into this state: - Since meta#7034, `ProcessManifestWrites()` resets `io_status_` after any call whose MANIFEST I/O status is OK. It records only the first error. - meta#12030 added the quarantine. It added the files of a failed batch only inside the "first error" block, and it cleared the quarantine together with the reset of `io_status_`. At that time `SetOptions()` still wrote to the MANIFEST. - meta#13384 made `SetOptions()` skip the MANIFEST write. So every `SetOptions()` became a call with an OK MANIFEST I/O status that writes no new MANIFEST. The problems: 1. Two kinds of calls have an OK MANIFEST I/O status but write no new MANIFEST: - `DBImpl::SetOptions()` installs a new Version through `LogAndApply()` with an edit that writes nothing to the MANIFEST (`skip_manifest_write`). - A batch can fail before any MANIFEST I/O, for example when `LoadTableHandlers()` fails with `paranoid_checks`. A compaction that is already running when a flush's MANIFEST write fails still commits, because the background error is checked only when the compaction starts. After such a call, the old MANIFEST can still hold the failed edit, but its files are no longer quarantined. 2. A second failed MANIFEST write, while `io_status_` already holds an earlier error, does not quarantine its own files. This is a problem when the second batch creates a new MANIFEST and renames CURRENT to it, and then reports an error. CURRENT names the new MANIFEST, and it lists the new files. In both cases, the purge after the failed job, or the purge in a clean `Close()`, deletes the files. When CURRENT names a MANIFEST that lists a deleted file, every `DB::Open()` fails with "Corruption: IO error: No such file or directory ... .sst The file MANIFEST-... may be corrupted". A db_stress crash test hit problem 1: a flush's MANIFEST write got an injected write error, a `SetOptions()` ran about 1.6 ms later and cleared the quarantine, the flush job deleted its SST, and the harness killed the process. This change cannot repair a DB that already lost such an SST. Example of problem 2, reproduced by the new unit test: ``` 1. DropColumnFamily(): the MANIFEST record is synced, then the write reports an error. io_status_ = that error. No background error is set. 2. (optional) SetOptions() succeeds. 3. Flush: starts a new MANIFEST that lists the flushed SST, renames CURRENT to it, then SetCurrentFile() reports an error. io_status_ is already bad, so the SST is not quarantined. 4. The flush job's purge deletes the SST. The next DB::Open() fails. ``` Without step 2, upstream fails this way. With step 2, upstream passes, because `SetOptions()` resets `io_status_` before the flush, so the flush failure is the "first" error. Fixing problem 1 alone would make the run with step 2 fail too, because `SetOptions()` would no longer reset `io_status_`. So this change fixes both problems. New behavior: - Every MANIFEST write failure adds the files of its batch to the quarantine. `io_status_` keeps the first error. A file number is added only once, because automatic recovery can retry many times, each retry adds the old MANIFEST again, and each `FindObsoleteFiles()` copies the list. - `io_status_` is reset and the quarantine is cleared only when the call wrote and installed a new MANIFEST (s is OK and `new_descriptor_log` is set). A batch that skips the MANIFEST write never sets `new_descriptor_log`. Until then, the files stay quarantined. This is enough for recovery: because `descriptor_log_` was reset, the next real MANIFEST write (for example, a non-atomic flush, a compaction, or the forced write in `DB::Resume()`) starts a new MANIFEST, and its success clears the whole quarantine. - `DBImpl::SetOptions()` sets no background error after its `LogAndApply()`. This part is required, not a cleanup. Since meta#13384, that call does no MANIFEST I/O and is never grouped with other writers, so a non-OK `io_status()` there always belongs to another, earlier batch. The removed block was `if (!versions_->io_status().ok()) { assert(!s.ok()); SetBGError(...); }`. With only the `VersionSet` change, `io_status()` stays non-OK after a successful `SetOptions()`. Then a debug build fails the assert, and a release build sets the old error as a fatal background error, so the next write fails. A backport must include this part. - Two callers do not take the old error in `versions_->io_status()` as their own when they did not fail on MANIFEST I/O: - An atomic flush checks `versions_->io_status()` while it waits for its turn to install. The check stops the wait when the flush that it waits for failed its MANIFEST write. A flush that does not wait for another flush, while no background error is set, ignores the error and installs its result. That install writes a new MANIFEST, and its success clears the old error and the quarantine. While a background error is set, the flush returns the error and does not install, as before this change while io_status() holds the error. The check must stay in that case: when the DB is stopped, the code after the wait checks only the recovery error, which is OK when no recovery runs. This also fixes an upstream case. Upstream, a `SetOptions()` after a failed MANIFEST write resets io_status(). A later atomic flush could then install its result after a fatal background error stopped the DB. With this change, io_status() keeps the error until a new MANIFEST is written, so that flush returns the error instead. Without any old MANIFEST error, an atomic flush that is already running can still install after a fatal background error. That is older than this change and not about the MANIFEST, so this change leaves it. - `CompactionJob::Install()` takes `versions_->io_status()` as the job's I/O status only when the commit itself failed. A compaction that failed before its commit, for example when it could not write an output file, keeps its own I/O status. This change closes both problems above: the `SetOptions()` trigger, the "failure before MANIFEST I/O" trigger, and the second failed MANIFEST write, with or without a `SetOptions()` in between. The same rule also covers a running compaction whose commit fails at the CURRENT step after a flush's MANIFEST write failed (derived from the code, not tested separately). Consequences: - Quarantined files can stay longer, until the next new MANIFEST or a reopen. This only delays their deletion. - `DB::Resume()` writes a new MANIFEST when `versions_->io_status()` is an `IOError`. Without this change, a `SetOptions()` before the `Resume()` made it skip that write. With this change the write happens, and it clears the quarantine. - `versions_->io_status()` keeps an old MANIFEST write error until a new MANIFEST is written, and a `SetOptions()` call does not reset it. Several callers read it after their own failure and report it as their own error. This misattribution is older than this change: it already happened between a failed MANIFEST write and the next call with an OK MANIFEST I/O status. The worst case is an old non-retryable error that some call left without setting a background error. `DropColumnFamily()`, `CreateColumnFamily()`, `DeleteFilesInRanges()`, `PromoteL0()`, `IncreaseFullHistoryTsLow()`, the level refit of `CompactRange()`, and the file number reservation before an ingestion are such calls. A caller that takes that old error can set it as a fatal background error, and only a reopen recovers from that. This change fixes the two cases above (an atomic flush that does not wait, and a compaction that fails before its commit). Known cases that stay, all older than this change: - An atomic flush that waits for another flush still returns the old error. This needs two atomic flushes at the same time. - A compaction whose commit fails before MANIFEST I/O, for example with `Corruption` from `LoadTableHandlers()`, reports the old `IOError` with reason `kManifestWrite` instead of its own `Corruption`. The outputs of that compaction were never committed. - A compaction or flush that fails for its own reason picks the reason `kManifestWrite` or `kManifestWriteNoWAL` when the old error exists. For a retryable error of a compaction, this makes it a hard error instead of a soft error. Automatic recovery or `DB::Resume()` then writes a new MANIFEST and recovers. The worse case is with `paranoid_checks=false`. By the severity table, a non-retryable `IOError` is no error for `kCompaction` and `kFlushNoWAL`, but a fatal error for `kManifestWrite` and `kManifestWriteNoWAL`. A `NoSpace` error is no error for `kCompaction`, but a hard error for `kManifestWrite`. - Other calls that write the MANIFEST, such as `ApplyWALToManifest()` and the record of the OPTIONS file number in `WriteOptionsFile()`, set `versions_->io_status()` as a background error when their `LogAndApply()` fails, also when it failed before MANIFEST I/O. A complete fix tracks the MANIFEST I/O status of each batch, and lets a caller report `versions_->io_status()` only when its own commit failed on MANIFEST I/O. That touches `LogAndApply()`, flush, compaction and the DB callers above, and is planned as a follow-up. - An ingestion whose MANIFEST write fails deletes its ingested file in the rollback, even when the file is quarantined. This gap is older than this change, and it is on a different code path. A follow-up will fix it. - No API, option or format change. Differential Revision: D134907305
Summary: Pull Request resolved: meta#15818 Background: when a MANIFEST write fails, RocksDB cannot know whether the failed edit is in the MANIFEST that CURRENT names. For example, the record can be written and synced before the error is reported. Or a batch can create a new MANIFEST, rename CURRENT to it, and then get an error from the directory sync. So `VersionSet::ProcessManifestWrites()` puts the new files of the failed batch (the edits of one `ProcessManifestWrites()` call) in the error handler quarantine, and the purge of obsolete files does not delete them. When a MANIFEST write fails, `ProcessManifestWrites()` also resets `descriptor_log_`, and a batch without a `descriptor_log_` sets `new_descriptor_log`. So the next batch that writes the MANIFEST always starts a new MANIFEST. Once that new MANIFEST is installed, the old one is no longer used, and the quarantine is cleared. `VersionSet::io_status_` keeps the MANIFEST write error until then. How the code got into this state: - Since meta#7034, `ProcessManifestWrites()` resets `io_status_` after any call whose MANIFEST I/O status is OK. It records only the first error. - meta#12030 added the quarantine. It added the files of a failed batch only inside the "first error" block, and it cleared the quarantine together with the reset of `io_status_`. At that time `SetOptions()` still wrote to the MANIFEST. - meta#13384 made `SetOptions()` skip the MANIFEST write. So every `SetOptions()` became a call with an OK MANIFEST I/O status that writes no new MANIFEST. The problems: 1. Two kinds of calls have an OK MANIFEST I/O status but write no new MANIFEST: - `DBImpl::SetOptions()` installs a new Version through `LogAndApply()` with an edit that writes nothing to the MANIFEST (`skip_manifest_write`). - A batch can fail before any MANIFEST I/O, for example when `LoadTableHandlers()` fails with `paranoid_checks`. A compaction that is already running when a flush's MANIFEST write fails still commits, because the background error is checked only when the compaction starts. After such a call, the old MANIFEST can still hold the failed edit, but its files are no longer quarantined. 2. A second failed MANIFEST write, while `io_status_` already holds an earlier error, does not quarantine its own files. This is a problem when the second batch creates a new MANIFEST and renames CURRENT to it, and then reports an error. CURRENT names the new MANIFEST, and it lists the new files. In both cases, the purge after the failed job, or the purge in a clean `Close()`, deletes the files. When CURRENT names a MANIFEST that lists a deleted file, every `DB::Open()` fails with "Corruption: IO error: No such file or directory ... .sst The file MANIFEST-... may be corrupted". A db_stress crash test hit problem 1: a flush's MANIFEST write got an injected write error, a `SetOptions()` ran about 1.6 ms later and cleared the quarantine, the flush job deleted its SST, and the harness killed the process. This change cannot repair a DB that already lost such an SST. Example of problem 2, reproduced by the new unit test: ``` 1. DropColumnFamily(): the MANIFEST record is synced, then the write reports an error. io_status_ = that error. No background error is set. 2. (optional) SetOptions() succeeds. 3. Flush: starts a new MANIFEST that lists the flushed SST, renames CURRENT to it, then SetCurrentFile() reports an error. io_status_ is already bad, so the SST is not quarantined. 4. The flush job's purge deletes the SST. The next DB::Open() fails. ``` Without step 2, upstream fails this way. With step 2, upstream passes, because `SetOptions()` resets `io_status_` before the flush, so the flush failure is the "first" error. Fixing problem 1 alone would make the run with step 2 fail too, because `SetOptions()` would no longer reset `io_status_`. So this change fixes both problems. New behavior: - Every MANIFEST write failure adds the files of its batch to the quarantine. `io_status_` keeps the first error. A file number is added only once, because automatic recovery can retry many times, each retry adds the old MANIFEST again, and each `FindObsoleteFiles()` copies the list. - `io_status_` is reset and the quarantine is cleared only when the call wrote and installed a new MANIFEST (s is OK and `new_descriptor_log` is set). A batch that skips the MANIFEST write never sets `new_descriptor_log`. Until then, the files stay quarantined. This is enough for recovery: because `descriptor_log_` was reset, the next real MANIFEST write (for example, a non-atomic flush, a compaction, or the forced write in `DB::Resume()`) starts a new MANIFEST, and its success clears the whole quarantine. - `DBImpl::SetOptions()` sets no background error after its `LogAndApply()`. This part is required, not a cleanup. Since meta#13384, that call does no MANIFEST I/O and is never grouped with other writers, so a non-OK `io_status()` there always belongs to another, earlier batch. The removed block was `if (!versions_->io_status().ok()) { assert(!s.ok()); SetBGError(...); }`. With only the `VersionSet` change, `io_status()` stays non-OK after a successful `SetOptions()`. Then a debug build fails the assert, and a release build sets the old error as a fatal background error, so the next write fails. A backport must include this part. - Two callers do not take the old error in `versions_->io_status()` as their own when they did not fail on MANIFEST I/O: - An atomic flush checks `versions_->io_status()` while it waits for its turn to install. The check stops the wait when the flush that it waits for failed its MANIFEST write. A flush that does not wait for another flush, while no background error is set, ignores the error and installs its result. That install writes a new MANIFEST, and its success clears the old error and the quarantine. While a background error is set, the flush returns the error and does not install, as before this change while io_status() holds the error. The check must stay in that case: when the DB is stopped, the code after the wait checks only the recovery error, which is OK when no recovery runs. This also fixes an upstream case. Upstream, a `SetOptions()` after a failed MANIFEST write resets io_status(). A later atomic flush could then install its result after a fatal background error stopped the DB. With this change, io_status() keeps the error until a new MANIFEST is written, so that flush returns the error instead. Without any old MANIFEST error, an atomic flush that is already running can still install after a fatal background error. That is older than this change and not about the MANIFEST, so this change leaves it. - `CompactionJob::Install()` takes `versions_->io_status()` as the job's I/O status only when the commit itself failed. A compaction that failed before its commit, for example when it could not write an output file, keeps its own I/O status. This change closes both problems above: the `SetOptions()` trigger, the "failure before MANIFEST I/O" trigger, and the second failed MANIFEST write, with or without a `SetOptions()` in between. The same rule also covers a running compaction whose commit fails at the CURRENT step after a flush's MANIFEST write failed (derived from the code, not tested separately). Consequences: - Quarantined files can stay longer, until the next new MANIFEST or a reopen. This only delays their deletion. - `DB::Resume()` writes a new MANIFEST when `versions_->io_status()` is an `IOError`. Without this change, a `SetOptions()` before the `Resume()` made it skip that write. With this change the write happens, and it clears the quarantine. - `versions_->io_status()` keeps an old MANIFEST write error until a new MANIFEST is written, and a `SetOptions()` call does not reset it. Several callers read it after their own failure and report it as their own error. This misattribution is older than this change: it already happened between a failed MANIFEST write and the next call with an OK MANIFEST I/O status. The worst case is an old non-retryable error that some call left without setting a background error. `DropColumnFamily()`, `CreateColumnFamily()`, `DeleteFilesInRanges()`, `PromoteL0()`, `IncreaseFullHistoryTsLow()`, the level refit of `CompactRange()`, and the file number reservation before an ingestion are such calls. A caller that takes that old error can set it as a fatal background error, and only a reopen recovers from that. This change fixes the two cases above (an atomic flush that does not wait, and a compaction that fails before its commit). Known cases that stay, all older than this change: - An atomic flush that waits for another flush still returns the old error. This needs two atomic flushes at the same time. - A compaction whose commit fails before MANIFEST I/O, for example with `Corruption` from `LoadTableHandlers()`, reports the old `IOError` with reason `kManifestWrite` instead of its own `Corruption`. The outputs of that compaction were never committed. - A compaction or flush that fails for its own reason picks the reason `kManifestWrite` or `kManifestWriteNoWAL` when the old error exists. For a retryable error of a compaction, this makes it a hard error instead of a soft error. Automatic recovery or `DB::Resume()` then writes a new MANIFEST and recovers. The worse case is with `paranoid_checks=false`. By the severity table, a non-retryable `IOError` is no error for `kCompaction` and `kFlushNoWAL`, but a fatal error for `kManifestWrite` and `kManifestWriteNoWAL`. A `NoSpace` error is no error for `kCompaction`, but a hard error for `kManifestWrite`. - Other calls that write the MANIFEST, such as `ApplyWALToManifest()` and the record of the OPTIONS file number in `WriteOptionsFile()`, set `versions_->io_status()` as a background error when their `LogAndApply()` fails, also when it failed before MANIFEST I/O. A complete fix tracks the MANIFEST I/O status of each batch, and lets a caller report `versions_->io_status()` only when its own commit failed on MANIFEST I/O. That touches `LogAndApply()`, flush, compaction and the DB callers above, and is planned as a follow-up. - An ingestion whose MANIFEST write fails deletes its ingested file in the rollback, even when the file is quarantined. This gap is older than this change, and it is on a different code path. A follow-up will fix it. - No API, option or format change. Differential Revision: D134907305
Summary: Pull Request resolved: meta#15818 Background: when a MANIFEST write fails, RocksDB cannot know whether the failed edit is in the MANIFEST that CURRENT names. For example, the record can be written and synced before the error is reported. Or a batch can create a new MANIFEST, rename CURRENT to it, and then get an error from the directory sync. So `VersionSet::ProcessManifestWrites()` puts the new files of the failed batch (the edits of one `ProcessManifestWrites()` call) in the error handler quarantine, and the purge of obsolete files does not delete them. When a MANIFEST write fails, `ProcessManifestWrites()` also resets `descriptor_log_`, and a batch without a `descriptor_log_` sets `new_descriptor_log`. So the next batch that writes the MANIFEST always starts a new MANIFEST. Once that new MANIFEST is installed, the old one is no longer used, and the quarantine is cleared. `VersionSet::io_status_` keeps the MANIFEST write error until then. How the code got into this state: - Since meta#7034, `ProcessManifestWrites()` resets `io_status_` after any call whose MANIFEST I/O status is OK. It records only the first error. - meta#12030 added the quarantine. It added the files of a failed batch only inside the "first error" block, and it cleared the quarantine together with the reset of `io_status_`. At that time `SetOptions()` still wrote to the MANIFEST. - meta#13384 made `SetOptions()` skip the MANIFEST write. So every `SetOptions()` became a call with an OK MANIFEST I/O status that writes no new MANIFEST. The problems: 1. Two kinds of calls have an OK MANIFEST I/O status but write no new MANIFEST: - `DBImpl::SetOptions()` installs a new Version through `LogAndApply()` with an edit that writes nothing to the MANIFEST (`skip_manifest_write`). - A batch can fail before any MANIFEST I/O, for example when `LoadTableHandlers()` fails with `paranoid_checks`. A compaction that is already running when a flush's MANIFEST write fails still commits, because the background error is checked only when the compaction starts. After such a call, the old MANIFEST can still hold the failed edit, but its files are no longer quarantined. 2. A second failed MANIFEST write, while `io_status_` already holds an earlier error, does not quarantine its own files. This is a problem when the second batch creates a new MANIFEST and renames CURRENT to it, and then reports an error. CURRENT names the new MANIFEST, and it lists the new files. In both cases, the purge after the failed job, or the purge in a clean `Close()`, deletes the files. When CURRENT names a MANIFEST that lists a deleted file, every `DB::Open()` fails with "Corruption: IO error: No such file or directory ... .sst The file MANIFEST-... may be corrupted". A db_stress crash test hit problem 1: a flush's MANIFEST write got an injected write error, a `SetOptions()` ran about 1.6 ms later and cleared the quarantine, the flush job deleted its SST, and the harness killed the process. This change cannot repair a DB that already lost such an SST. Example of problem 2, reproduced by the new unit test: ``` 1. DropColumnFamily(): the MANIFEST record is synced, then the write reports an error. io_status_ = that error. No background error is set. 2. (optional) SetOptions() succeeds. 3. Flush: starts a new MANIFEST that lists the flushed SST, renames CURRENT to it, then SetCurrentFile() reports an error. io_status_ is already bad, so the SST is not quarantined. 4. The flush job's purge deletes the SST. The next DB::Open() fails. ``` Without step 2, upstream fails this way. With step 2, upstream passes, because `SetOptions()` resets `io_status_` before the flush, so the flush failure is the "first" error. Fixing problem 1 alone would make the run with step 2 fail too, because `SetOptions()` would no longer reset `io_status_`. So this change fixes both problems. New behavior: - Every MANIFEST write failure adds the files of its batch to the quarantine. `io_status_` keeps the first error. A file number is added only once, because automatic recovery can retry many times, each retry adds the old MANIFEST again, and each `FindObsoleteFiles()` copies the list. - `io_status_` is reset and the quarantine is cleared only when the call wrote and installed a new MANIFEST (s is OK and `new_descriptor_log` is set). A batch that skips the MANIFEST write never sets `new_descriptor_log`. Until then, the files stay quarantined. This is enough for recovery: because `descriptor_log_` was reset, the next real MANIFEST write (for example, a non-atomic flush, a compaction, or the forced write in `DB::Resume()`) starts a new MANIFEST, and its success clears the whole quarantine. - `DBImpl::SetOptions()` sets no background error after its `LogAndApply()`. This part is required, not a cleanup. Since meta#13384, that call does no MANIFEST I/O and is never grouped with other writers, so a non-OK `io_status()` there always belongs to another, earlier batch. The removed block was `if (!versions_->io_status().ok()) { assert(!s.ok()); SetBGError(...); }`. With only the `VersionSet` change, `io_status()` stays non-OK after a successful `SetOptions()`. Then a debug build fails the assert, and a release build sets the old error as a fatal background error, so the next write fails. A backport must include this part. - Two callers do not take the old error in `versions_->io_status()` as their own when they did not fail on MANIFEST I/O: - An atomic flush checks `versions_->io_status()` while it waits for its turn to install. The check stops the wait when the flush that it waits for failed its MANIFEST write. A flush that does not wait for another flush, while no background error is set, ignores the error and installs its result. That install writes a new MANIFEST, and its success clears the old error and the quarantine. While a background error is set, the flush returns the error and does not install, as before this change while io_status() holds the error. The check must stay in that case: when the DB is stopped, the code after the wait checks only the recovery error, which is OK when no recovery runs. This also fixes an upstream case. Upstream, a `SetOptions()` after a failed MANIFEST write resets io_status(). A later atomic flush could then install its result after a fatal background error stopped the DB. With this change, io_status() keeps the error until a new MANIFEST is written, so that flush returns the error instead. Without any old MANIFEST error, an atomic flush that is already running can still install after a fatal background error. That is older than this change and not about the MANIFEST, so this change leaves it. - `CompactionJob::Install()` takes `versions_->io_status()` as the job's I/O status only when the commit itself failed. A compaction that failed before its commit, for example when it could not write an output file, keeps its own I/O status. This change closes both problems above: the `SetOptions()` trigger, the "failure before MANIFEST I/O" trigger, and the second failed MANIFEST write, with or without a `SetOptions()` in between. The same rule also covers a running compaction whose commit fails at the CURRENT step after a flush's MANIFEST write failed (derived from the code, not tested separately). Consequences: - Quarantined files can stay longer, until the next new MANIFEST or a reopen. This only delays their deletion. - `DB::Resume()` writes a new MANIFEST when `versions_->io_status()` is an `IOError`. Without this change, a `SetOptions()` before the `Resume()` made it skip that write. With this change the write happens, and it clears the quarantine. - `versions_->io_status()` keeps an old MANIFEST write error until a new MANIFEST is written, and a `SetOptions()` call does not reset it. Several callers read it after their own failure and report it as their own error. This misattribution is older than this change: it already happened between a failed MANIFEST write and the next call with an OK MANIFEST I/O status. The worst case is an old non-retryable error that some call left without setting a background error. `DropColumnFamily()`, `CreateColumnFamily()`, `DeleteFilesInRanges()`, `PromoteL0()`, `IncreaseFullHistoryTsLow()`, the level refit of `CompactRange()`, and the file number reservation before an ingestion are such calls. A caller that takes that old error can set it as a fatal background error, and only a reopen recovers from that. This change fixes the two cases above (an atomic flush that does not wait, and a compaction that fails before its commit). Known cases that stay, all older than this change: - An atomic flush that waits for another flush still returns the old error. This needs two atomic flushes at the same time. - A compaction whose commit fails before MANIFEST I/O, for example with `Corruption` from `LoadTableHandlers()`, reports the old `IOError` with reason `kManifestWrite` instead of its own `Corruption`. The outputs of that compaction were never committed. - A compaction or flush that fails for its own reason picks the reason `kManifestWrite` or `kManifestWriteNoWAL` when the old error exists. For a retryable error of a compaction, this makes it a hard error instead of a soft error. Automatic recovery or `DB::Resume()` then writes a new MANIFEST and recovers. The worse case is with `paranoid_checks=false`. By the severity table, a non-retryable `IOError` is no error for `kCompaction` and `kFlushNoWAL`, but a fatal error for `kManifestWrite` and `kManifestWriteNoWAL`. A `NoSpace` error is no error for `kCompaction`, but a hard error for `kManifestWrite`. - Other calls that write the MANIFEST, such as `ApplyWALToManifest()` and the record of the OPTIONS file number in `WriteOptionsFile()`, set `versions_->io_status()` as a background error when their `LogAndApply()` fails, also when it failed before MANIFEST I/O. A complete fix tracks the MANIFEST I/O status of each batch, and lets a caller report `versions_->io_status()` only when its own commit failed on MANIFEST I/O. That touches `LogAndApply()`, flush, compaction and the DB callers above, and is planned as a follow-up. - An ingestion whose MANIFEST write fails deletes its ingested file in the rollback, even when the file is quarantined. This gap is older than this change, and it is on a different code path. A follow-up will fix it. - No API, option or format change. Differential Revision: D134907305
Summary: Background: when a MANIFEST write fails, RocksDB cannot know whether the failed edit is in the MANIFEST that CURRENT names. For example, the record can be written and synced before the error is reported. Or a batch can create a new MANIFEST, rename CURRENT to it, and then get an error from the directory sync. So `VersionSet::ProcessManifestWrites()` puts the new files of the failed batch (the edits of one `ProcessManifestWrites()` call) in the error handler quarantine, and the purge of obsolete files does not delete them. When a MANIFEST write fails, `ProcessManifestWrites()` also resets `descriptor_log_`, and a batch without a `descriptor_log_` sets `new_descriptor_log`. So the next batch that writes the MANIFEST always starts a new MANIFEST. Once that new MANIFEST is installed, the old one is no longer used, and the quarantine is cleared. `VersionSet::io_status_` keeps the MANIFEST write error until then. How the code got into this state: - Since meta#7034, `ProcessManifestWrites()` resets `io_status_` after any call whose MANIFEST I/O status is OK. It records only the first error. - meta#12030 added the quarantine. It added the files of a failed batch only inside the "first error" block, and it cleared the quarantine together with the reset of `io_status_`. At that time `SetOptions()` still wrote to the MANIFEST. - meta#13384 made `SetOptions()` skip the MANIFEST write. So every `SetOptions()` became a call with an OK MANIFEST I/O status that writes no new MANIFEST. The problems: 1. Two kinds of calls have an OK MANIFEST I/O status but write no new MANIFEST: - `DBImpl::SetOptions()` installs a new Version through `LogAndApply()` with an edit that writes nothing to the MANIFEST (`skip_manifest_write`). - A batch can fail before any MANIFEST I/O, for example when `LoadTableHandlers()` fails with `paranoid_checks`. A compaction that is already running when a flush's MANIFEST write fails still commits, because the background error is checked only when the compaction starts. After such a call, the old MANIFEST can still hold the failed edit, but its files are no longer quarantined. 2. A second failed MANIFEST write, while `io_status_` already holds an earlier error, does not quarantine its own files. This is a problem when the second batch creates a new MANIFEST and renames CURRENT to it, and then reports an error. CURRENT names the new MANIFEST, and it lists the new files. In both cases, the purge after the failed job, or the purge in a clean `Close()`, deletes the files. When CURRENT names a MANIFEST that lists a deleted file, every `DB::Open()` fails with "Corruption: IO error: No such file or directory ... .sst The file MANIFEST-... may be corrupted". A db_stress crash test hit problem 1: a flush's MANIFEST write got an injected write error, a `SetOptions()` ran about 1.6 ms later and cleared the quarantine, the flush job deleted its SST, and the harness killed the process. This change cannot repair a DB that already lost such an SST. Example of problem 2, reproduced by the new unit test: ``` 1. DropColumnFamily(): the MANIFEST record is synced, then the write reports an error. io_status_ = that error. No background error is set. 2. (optional) SetOptions() succeeds. 3. Flush: starts a new MANIFEST that lists the flushed SST, renames CURRENT to it, then SetCurrentFile() reports an error. io_status_ is already bad, so the SST is not quarantined. 4. The flush job's purge deletes the SST. The next DB::Open() fails. ``` Without step 2, upstream fails this way. With step 2, upstream passes, because `SetOptions()` resets `io_status_` before the flush, so the flush failure is the "first" error. Fixing problem 1 alone would make the run with step 2 fail too, because `SetOptions()` would no longer reset `io_status_`. So this change fixes both problems. New behavior: - Every MANIFEST write failure adds the files of its batch to the quarantine. `io_status_` keeps the first error. A file number is added only once, because automatic recovery can retry many times, each retry adds the old MANIFEST again, and each `FindObsoleteFiles()` copies the list. - `io_status_` is reset and the quarantine is cleared only when the call wrote and installed a new MANIFEST (s is OK and `new_descriptor_log` is set). A batch that skips the MANIFEST write never sets `new_descriptor_log`. Until then, the files stay quarantined. This is enough for recovery: because `descriptor_log_` was reset, the next real MANIFEST write (for example, a non-atomic flush, a compaction, or the forced write in `DB::Resume()`) starts a new MANIFEST, and its success clears the whole quarantine. - `DBImpl::SetOptions()` sets no background error after its `LogAndApply()`. This part is required, not a cleanup. Since meta#13384, that call does no MANIFEST I/O and is never grouped with other writers, so a non-OK `io_status()` there always belongs to another, earlier batch. The removed block was `if (!versions_->io_status().ok()) { assert(!s.ok()); SetBGError(...); }`. With only the `VersionSet` change, `io_status()` stays non-OK after a successful `SetOptions()`. Then a debug build fails the assert, and a release build sets the old error as a fatal background error, so the next write fails. A backport must include this part. - Two callers do not take the old error in `versions_->io_status()` as their own when they did not fail on MANIFEST I/O: - An atomic flush checks `versions_->io_status()` while it waits for its turn to install. The check stops the wait when the flush that it waits for failed its MANIFEST write. A flush that does not wait for another flush, while no background error is set, ignores the error and installs its result. That install writes a new MANIFEST, and its success clears the old error and the quarantine. While a background error is set, the flush returns the error and does not install, as before this change while io_status() holds the error. The check must stay in that case: when the DB is stopped, the code after the wait checks only the recovery error, which is OK when no recovery runs. This also fixes an upstream case. Upstream, a `SetOptions()` after a failed MANIFEST write resets io_status(). A later atomic flush could then install its result after a fatal background error stopped the DB. With this change, io_status() keeps the error until a new MANIFEST is written, so that flush returns the error instead. Without any old MANIFEST error, an atomic flush that is already running can still install after a fatal background error. That is older than this change and not about the MANIFEST, so this change leaves it. - `CompactionJob::Install()` takes `versions_->io_status()` as the job's I/O status only when the commit itself failed. A compaction that failed before its commit, for example when it could not write an output file, keeps its own I/O status. This change closes both problems above: the `SetOptions()` trigger, the "failure before MANIFEST I/O" trigger, and the second failed MANIFEST write, with or without a `SetOptions()` in between. The same rule also covers a running compaction whose commit fails at the CURRENT step after a flush's MANIFEST write failed (derived from the code, not tested separately). Consequences: - Quarantined files can stay longer, until the next new MANIFEST or a reopen. This only delays their deletion. - `DB::Resume()` writes a new MANIFEST when `versions_->io_status()` is an `IOError`. Without this change, a `SetOptions()` before the `Resume()` made it skip that write. With this change the write happens, and it clears the quarantine. - `versions_->io_status()` keeps an old MANIFEST write error until a new MANIFEST is written, and a `SetOptions()` call does not reset it. Several callers read it after their own failure and report it as their own error. This misattribution is older than this change: it already happened between a failed MANIFEST write and the next call with an OK MANIFEST I/O status. The worst case is an old non-retryable error that some call left without setting a background error. `DropColumnFamily()`, `CreateColumnFamily()`, `DeleteFilesInRanges()`, `PromoteL0()`, `IncreaseFullHistoryTsLow()`, the level refit of `CompactRange()`, and the file number reservation before an ingestion are such calls. A caller that takes that old error can set it as a fatal background error, and only a reopen recovers from that. This change fixes the two cases above (an atomic flush that does not wait, and a compaction that fails before its commit). Known cases that stay, all older than this change: - An atomic flush that waits for another flush still returns the old error. This needs two atomic flushes at the same time. - A compaction whose commit fails before MANIFEST I/O, for example with `Corruption` from `LoadTableHandlers()`, reports the old `IOError` with reason `kManifestWrite` instead of its own `Corruption`. The outputs of that compaction were never committed. - A compaction or flush that fails for its own reason picks the reason `kManifestWrite` or `kManifestWriteNoWAL` when the old error exists. For a retryable error of a compaction, this makes it a hard error instead of a soft error. Automatic recovery or `DB::Resume()` then writes a new MANIFEST and recovers. The worse case is with `paranoid_checks=false`. By the severity table, a non-retryable `IOError` is no error for `kCompaction` and `kFlushNoWAL`, but a fatal error for `kManifestWrite` and `kManifestWriteNoWAL`. A `NoSpace` error is no error for `kCompaction`, but a hard error for `kManifestWrite`. - Other calls that write the MANIFEST, such as `ApplyWALToManifest()` and the record of the OPTIONS file number in `WriteOptionsFile()`, set `versions_->io_status()` as a background error when their `LogAndApply()` fails, also when it failed before MANIFEST I/O. A complete fix tracks the MANIFEST I/O status of each batch, and lets a caller report `versions_->io_status()` only when its own commit failed on MANIFEST I/O. That touches `LogAndApply()`, flush, compaction and the DB callers above, and is planned as a follow-up. - An ingestion whose MANIFEST write fails deletes its ingested file in the rollback, even when the file is quarantined. This gap is older than this change, and it is on a different code path. A follow-up will fix it. - No API, option or format change. Differential Revision: D134907305
Summary: Background: when a MANIFEST write fails, RocksDB cannot know whether the failed edit is in the MANIFEST that CURRENT names. For example, the record can be written and synced before the error is reported. Or a batch can create a new MANIFEST, rename CURRENT to it, and then get an error from the directory sync. So `VersionSet::ProcessManifestWrites()` puts the new files of the failed batch (the edits of one `ProcessManifestWrites()` call) in the error handler quarantine, and the purge of obsolete files does not delete them. When a MANIFEST write fails, `ProcessManifestWrites()` also resets `descriptor_log_`, and a batch without a `descriptor_log_` sets `new_descriptor_log`. So the next batch that writes the MANIFEST always starts a new MANIFEST. Once that new MANIFEST is installed, the old one is no longer used, and the quarantine is cleared. `VersionSet::io_status_` keeps the MANIFEST write error until then. How the code got into this state: - Since meta#7034, `ProcessManifestWrites()` resets `io_status_` after any call whose MANIFEST I/O status is OK. It records only the first error. - meta#12030 added the quarantine. It added the files of a failed batch only inside the "first error" block, and it cleared the quarantine together with the reset of `io_status_`. At that time `SetOptions()` still wrote to the MANIFEST. - meta#13384 made `SetOptions()` skip the MANIFEST write. So every `SetOptions()` became a call with an OK MANIFEST I/O status that writes no new MANIFEST. The problems: 1. Two kinds of calls have an OK MANIFEST I/O status but write no new MANIFEST: - `DBImpl::SetOptions()` installs a new Version through `LogAndApply()` with an edit that writes nothing to the MANIFEST (`skip_manifest_write`). - A batch can fail before any MANIFEST I/O, for example when `LoadTableHandlers()` fails with `paranoid_checks`. A compaction that is already running when a flush's MANIFEST write fails still commits, because the background error is checked only when the compaction starts. After such a call, the old MANIFEST can still hold the failed edit, but its files are no longer quarantined. 2. A second failed MANIFEST write, while `io_status_` already holds an earlier error, does not quarantine its own files. This is a problem when the second batch creates a new MANIFEST and renames CURRENT to it, and then reports an error. CURRENT names the new MANIFEST, and it lists the new files. In both cases, the purge after the failed job, or the purge in a clean `Close()`, deletes the files. When CURRENT names a MANIFEST that lists a deleted file, every `DB::Open()` fails with "Corruption: IO error: No such file or directory ... .sst The file MANIFEST-... may be corrupted". A db_stress crash test hit problem 1: a flush's MANIFEST write got an injected write error, a `SetOptions()` ran about 1.6 ms later and cleared the quarantine, the flush job deleted its SST, and the harness killed the process. This change cannot repair a DB that already lost such an SST. Example of problem 2, reproduced by the new unit test: ``` 1. DropColumnFamily(): the MANIFEST record is synced, then the write reports an error. io_status_ = that error. No background error is set. 2. (optional) SetOptions() succeeds. 3. Flush: starts a new MANIFEST that lists the flushed SST, renames CURRENT to it, then SetCurrentFile() reports an error. io_status_ is already bad, so the SST is not quarantined. 4. The flush job's purge deletes the SST. The next DB::Open() fails. ``` Without step 2, upstream fails this way. With step 2, upstream passes, because `SetOptions()` resets `io_status_` before the flush, so the flush failure is the "first" error. Fixing problem 1 alone would make the run with step 2 fail too, because `SetOptions()` would no longer reset `io_status_`. So this change fixes both problems. New behavior: - Every MANIFEST write failure adds the files of its batch to the quarantine. `io_status_` keeps the first error. A file number is added only once, because automatic recovery can retry many times, each retry adds the old MANIFEST again, and each `FindObsoleteFiles()` copies the list. The check uses a hash set of the numbers already in the list, so its cost stays linear in the number of files. A failed batch can have many files, for example a large ingestion or a refit of a whole level, and the check runs under the DB mutex. - `io_status_` is reset and the quarantine is cleared only when the call wrote and installed a new MANIFEST (s is OK and `new_descriptor_log` is set). A batch that skips the MANIFEST write never sets `new_descriptor_log`. Until then, the files stay quarantined. This is enough for recovery: because `descriptor_log_` was reset, the next real MANIFEST write (for example, a non-atomic flush, a compaction, or the forced write in `DB::Resume()`) starts a new MANIFEST, and its success clears the whole quarantine. - `DBImpl::SetOptions()` sets no background error after its `LogAndApply()`. This part is required, not a cleanup. Since meta#13384, that call does no MANIFEST I/O and is never grouped with other writers, so a non-OK `io_status()` there always belongs to another, earlier batch. The removed block was `if (!versions_->io_status().ok()) { assert(!s.ok()); SetBGError(...); }`. With only the `VersionSet` change, `io_status()` stays non-OK after a successful `SetOptions()`. Then a debug build fails the assert, and a release build sets the old error as a fatal background error, so the next write fails. A backport must include this part. - Two callers do not take the old error in `versions_->io_status()` as their own when they did not fail on MANIFEST I/O: - An atomic flush checks `versions_->io_status()` while it waits for its turn to install. The check stops the wait when the flush that it waits for failed its MANIFEST write. A flush that does not wait for another flush, while the DB is not stopped, ignores the error. With no background error, it installs its result. That install writes a new MANIFEST, and its success clears the old error and the quarantine. With a soft background error, a flush that is not for recovery gets that soft error from the code after the wait and does not install, so it reports its own error. A recovery flush installs, which writes the new MANIFEST. While the DB is stopped, the flush returns the error and does not install, as before this change while io_status() holds the error. The check must stay in that case: when the DB is stopped, the code after the wait checks only the recovery error, which is OK when no recovery runs. This also fixes an upstream case. Upstream, a `SetOptions()` after a failed MANIFEST write resets io_status(). A later atomic flush could then install its result after a fatal background error stopped the DB. With this change, io_status() keeps the error until a new MANIFEST is written, so that flush returns the error instead. Without any old MANIFEST error, an atomic flush that is already running can still install after a fatal background error. That is older than this change and not about the MANIFEST, so this change leaves it. - `CompactionJob::Install()` takes `versions_->io_status()` as the job's I/O status only when the commit itself failed. A compaction that failed before its commit, for example when it could not write an output file, keeps its own I/O status. - The SstFileManager recovers from a soft `NoSpace` error with a direct clear in `ErrorHandler::RecoverFromBGError()`, which does not go through `DBImpl::ResumeImpl()` and writes no MANIFEST. When the quarantine is not empty or `versions_->io_status()` is not OK, the direct clear first writes a new MANIFEST with an empty edit, as `DBImpl::ResumeImpl()` does. If that write fails, the direct clear sets the error of that write as a background error with reason `kManifestWrite`, does not clear, and returns. The SstFileManager then retries, unless that error is fatal. If shutdown has started, the direct clear does not write and returns `ShutdownInProgress`, as `DBImpl::ResumeImpl()` does. Some calls set no background error when their MANIFEST write fails, for example a failed `PromoteL0()` or `DropColumnFamily()`, so a later soft error can find files in the quarantine. Without this part, a debug build then fails the assert in `ErrorHandler::ClearBGError()` that the quarantine is empty, and a release build clears the error while the old MANIFEST may still hold the failed edit. Upstream already fails the assert after a failed `PromoteL0()` followed by a soft error, and this change fixes that case too. With the rest of this change, a `SetOptions()` between them keeps the quarantine, so without this part the assert would fire in that case as well. This change closes both problems above: the `SetOptions()` trigger, the "failure before MANIFEST I/O" trigger, and the second failed MANIFEST write, with or without a `SetOptions()` in between. The same rule also covers a running compaction whose commit fails at the CURRENT step after a flush's MANIFEST write failed (derived from the code, not tested separately). Consequences: - Quarantined files can stay longer, until the next new MANIFEST or a reopen. This only delays their deletion. - The automatic clear of a soft `NoSpace` error by the SstFileManager can take longer after an older failed MANIFEST write. It writes a new MANIFEST first, and it keeps the error if that write fails. This is needed because the older failed write may still be in the old MANIFEST, and clearing the error without a new MANIFEST would forget that. If that MANIFEST write fails, its error becomes a hard error, so writes stop, and the later retry runs the full recovery of `DB::Resume()`, which flushes all column families. If the failed write has a non-retryable `IOError`, the error is fatal, and only a reopen recovers. - `DB::Resume()` writes a new MANIFEST when `versions_->io_status()` is an `IOError`. Without this change, a `SetOptions()` before the `Resume()` made it skip that write. With this change the write happens, and it clears the quarantine. - `versions_->io_status()` keeps an old MANIFEST write error until a new MANIFEST is written, and a `SetOptions()` call does not reset it. Several callers read it after their own failure and report it as their own error. This misattribution is older than this change: it already happened between a failed MANIFEST write and the next call with an OK MANIFEST I/O status. The worst case is an old non-retryable error that some call left without setting a background error. `DropColumnFamily()`, `CreateColumnFamily()`, `DeleteFilesInRanges()`, `PromoteL0()`, `IncreaseFullHistoryTsLow()`, the level refit of `CompactRange()`, and the file number reservation before an ingestion are such calls. A caller that takes that old error can set it as a fatal background error, and only a reopen recovers from that. This change fixes the two cases above (an atomic flush that does not wait, and a compaction that fails before its commit). Known cases that stay, all older than this change: - An atomic flush that waits for another flush still returns the old error. This needs two atomic flushes at the same time. - A compaction whose commit fails before MANIFEST I/O, for example with `Corruption` from `LoadTableHandlers()`, reports the old `IOError` with reason `kManifestWrite` instead of its own `Corruption`. The outputs of that compaction were never committed. - A compaction or flush that fails for its own reason picks the reason `kManifestWrite` or `kManifestWriteNoWAL` when the old error exists. For a retryable error of a compaction, this makes it a hard error instead of a soft error. Automatic recovery or `DB::Resume()` then writes a new MANIFEST and recovers, unless an atomic flush is running at that time. The hard error stops the DB, so that atomic flush returns the old error. When the old error is not retryable, this makes it a fatal error, and neither automatic recovery nor `DB::Resume()` recovers. Upstream recovers in this case when a `SetOptions()` ran after the old error, and goes fatal already at the compaction when none ran. The worse case is with `paranoid_checks=false`. By the severity table, a non-retryable `IOError` is no error for `kCompaction` and `kFlushNoWAL`, but a fatal error for `kManifestWrite` and `kManifestWriteNoWAL`. A `NoSpace` error is no error for `kCompaction`, but a hard error for `kManifestWrite`. - Other calls that write the MANIFEST, such as `ApplyWALToManifest()` and the record of the OPTIONS file number in `WriteOptionsFile()`, set `versions_->io_status()` as a background error when their `LogAndApply()` fails, also when it failed before MANIFEST I/O. Three compaction paths in `DBImpl::BackgroundCompaction()` read `versions_->io_status()` after their `LogAndApply()` also when it succeeded: the FIFO deletion compaction, the FIFO trivial copy (a temperature change), and the trivial move. A complete fix tracks the MANIFEST I/O status of each batch, and lets a caller report `versions_->io_status()` only when its own commit failed on MANIFEST I/O. That touches `LogAndApply()`, flush, compaction and the DB callers above, and is planned as a follow-up. - An ingestion whose MANIFEST write fails deletes its ingested file in the rollback, even when the file is quarantined. This gap is older than this change, and it is on a different code path. A follow-up will fix it. - No API, option or format change. Differential Revision: D134907305
Summary: Background: when a MANIFEST write fails, RocksDB cannot know whether the failed edit is in the MANIFEST that CURRENT names. For example, the record can be written and synced before the error is reported. Or a batch can create a new MANIFEST, rename CURRENT to it, and then get an error from the directory sync. So `VersionSet::ProcessManifestWrites()` puts the new files of the failed batch (the edits of one `ProcessManifestWrites()` call) in the error handler quarantine, and the purge of obsolete files does not delete them. When a MANIFEST write fails, `ProcessManifestWrites()` also resets `descriptor_log_`, and a batch without a `descriptor_log_` sets `new_descriptor_log`. So the next batch that writes the MANIFEST always starts a new MANIFEST. Once that new MANIFEST is installed, the old one is no longer used, and the quarantine is cleared. `VersionSet::io_status_` keeps the MANIFEST write error until then. How the code got into this state: - Since meta#7034, `ProcessManifestWrites()` resets `io_status_` after any call whose MANIFEST I/O status is OK. It records only the first error. - meta#12030 added the quarantine. It added the files of a failed batch only inside the "first error" block, and it cleared the quarantine together with the reset of `io_status_`. At that time `SetOptions()` still wrote to the MANIFEST. - meta#13384 made `SetOptions()` skip the MANIFEST write. So every `SetOptions()` became a call with an OK MANIFEST I/O status that writes no new MANIFEST. The problems: 1. Two kinds of calls have an OK MANIFEST I/O status but write no new MANIFEST: - `DBImpl::SetOptions()` installs a new Version through `LogAndApply()` with an edit that writes nothing to the MANIFEST (`skip_manifest_write`). - A batch can fail before any MANIFEST I/O, for example when `LoadTableHandlers()` fails with `paranoid_checks`. A compaction that is already running when a flush's MANIFEST write fails still commits, because the background error is checked only when the compaction starts. After such a call, the old MANIFEST can still hold the failed edit, but its files are no longer quarantined. 2. A second failed MANIFEST write, while `io_status_` already holds an earlier error, does not quarantine its own files. This is a problem when the second batch creates a new MANIFEST and renames CURRENT to it, and then reports an error. CURRENT names the new MANIFEST, and it lists the new files. In both cases, the purge after the failed job, or the purge in a clean `Close()`, deletes the files. When CURRENT names a MANIFEST that lists a deleted file, every `DB::Open()` fails with "Corruption: IO error: No such file or directory ... .sst The file MANIFEST-... may be corrupted". A db_stress crash test hit problem 1: a flush's MANIFEST write got an injected write error, a `SetOptions()` ran about 1.6 ms later and cleared the quarantine, the flush job deleted its SST, and the harness killed the process. This change cannot repair a DB that already lost such an SST. Example of problem 2, reproduced by the new unit test: ``` 1. DropColumnFamily(): the MANIFEST record is synced, then the write reports an error. io_status_ = that error. No background error is set. 2. (optional) SetOptions() succeeds. 3. Flush: starts a new MANIFEST that lists the flushed SST, renames CURRENT to it, then SetCurrentFile() reports an error. io_status_ is already bad, so the SST is not quarantined. 4. The flush job's purge deletes the SST. The next DB::Open() fails. ``` Without step 2, upstream fails this way. With step 2, upstream passes, because `SetOptions()` resets `io_status_` before the flush, so the flush failure is the "first" error. Fixing problem 1 alone would make the run with step 2 fail too, because `SetOptions()` would no longer reset `io_status_`. So this change fixes both problems. New behavior: - Every MANIFEST write failure adds the files of its batch to the quarantine. `io_status_` keeps the first error. A file number is added only once, because automatic recovery can retry many times, each retry adds the old MANIFEST again, and each `FindObsoleteFiles()` copies the list. The check uses a hash set of the numbers already in the list, so its cost stays linear in the number of files. A failed batch can have many files, for example a large ingestion or a refit of a whole level, and the check runs under the DB mutex. - `io_status_` is reset and the quarantine is cleared only when the call wrote and installed a new MANIFEST (s is OK and `new_descriptor_log` is set). A batch that skips the MANIFEST write never sets `new_descriptor_log`. Until then, the files stay quarantined. This is enough for recovery: because `descriptor_log_` was reset, the next real MANIFEST write (for example, a non-atomic flush, a compaction, or the forced write in `DB::Resume()`) starts a new MANIFEST, and its success clears the whole quarantine. - `DBImpl::SetOptions()` sets no background error after its `LogAndApply()`. This part is required, not a cleanup. Since meta#13384, that call does no MANIFEST I/O and is never grouped with other writers, so a non-OK `io_status()` there always belongs to another, earlier batch. The removed block was `if (!versions_->io_status().ok()) { assert(!s.ok()); SetBGError(...); }`. With only the `VersionSet` change, `io_status()` stays non-OK after a successful `SetOptions()`. Then a debug build fails the assert, and a release build sets the old error as a fatal background error, so the next write fails. A backport must include this part. - Two callers do not take the old error in `versions_->io_status()` as their own when they did not fail on MANIFEST I/O: - An atomic flush checks `versions_->io_status()` while it waits for its turn to install. The check stops the wait when the flush that it waits for failed its MANIFEST write. A flush that does not wait for another flush, while the DB is not stopped, ignores the error. With no background error, it installs its result. That install writes a new MANIFEST, and its success clears the old error and the quarantine. With a soft background error, a flush that is not for recovery gets that soft error from the code after the wait and does not install, so it reports its own error. A recovery flush installs, which writes the new MANIFEST. While the DB is stopped, the flush returns the error and does not install, as before this change while io_status() holds the error. The check must stay in that case: when the DB is stopped, the code after the wait checks only the recovery error, which is OK when no recovery runs. This also fixes an upstream case. Upstream, a `SetOptions()` after a failed MANIFEST write resets io_status(). A later atomic flush could then install its result after a fatal background error stopped the DB. With this change, io_status() keeps the error until a new MANIFEST is written, so that flush returns the error instead. Without any old MANIFEST error, an atomic flush that is already running can still install after a fatal background error. That is older than this change and not about the MANIFEST, so this change leaves it. - `CompactionJob::Install()` takes `versions_->io_status()` as the job's I/O status only when the commit itself failed. A compaction that failed before its commit, for example when it could not write an output file, keeps its own I/O status. - The SstFileManager recovers from a soft `NoSpace` error with a direct clear in `ErrorHandler::RecoverFromBGError()`, which does not go through `DBImpl::ResumeImpl()` and writes no MANIFEST. When the quarantine is not empty or `versions_->io_status()` is not OK, the direct clear first writes a new MANIFEST with an empty edit, as `DBImpl::ResumeImpl()` does. If that write fails, the direct clear sets the error of that write as a background error with reason `kManifestWrite`, does not clear, and returns. The SstFileManager then retries, unless that error is fatal. If shutdown has started, the direct clear does not write and returns `ShutdownInProgress`, as `DBImpl::ResumeImpl()` does. Some calls set no background error when their MANIFEST write fails, for example a failed `PromoteL0()` or `DropColumnFamily()`, so a later soft error can find files in the quarantine. Without this part, a debug build then fails the assert in `ErrorHandler::ClearBGError()` that the quarantine is empty, and a release build clears the error while the old MANIFEST may still hold the failed edit. Upstream already fails the assert after a failed `PromoteL0()` followed by a soft error, and this change fixes that case too. With the rest of this change, a `SetOptions()` between them keeps the quarantine, so without this part the assert would fire in that case as well. This change closes both problems above: the `SetOptions()` trigger, the "failure before MANIFEST I/O" trigger, and the second failed MANIFEST write, with or without a `SetOptions()` in between. The same rule also covers a running compaction whose commit fails at the CURRENT step after a flush's MANIFEST write failed (derived from the code, not tested separately). Consequences: - Quarantined files can stay longer, until the next new MANIFEST or a reopen. This only delays their deletion. - The automatic clear of a soft `NoSpace` error by the SstFileManager can take longer after an older failed MANIFEST write. It writes a new MANIFEST first, and it keeps the error if that write fails. This is needed because the older failed write may still be in the old MANIFEST, and clearing the error without a new MANIFEST would forget that. If that MANIFEST write fails, its error becomes a hard error, so writes stop, and the later retry runs the full recovery of `DB::Resume()`, which flushes all column families. If the failed write has a non-retryable `IOError`, the error is fatal, and only a reopen recovers. - `DB::Resume()` writes a new MANIFEST when `versions_->io_status()` is an `IOError`. Without this change, a `SetOptions()` before the `Resume()` made it skip that write. With this change the write happens, and it clears the quarantine. - `versions_->io_status()` keeps an old MANIFEST write error until a new MANIFEST is written, and a `SetOptions()` call does not reset it. Several callers read it after their own failure and report it as their own error. This misattribution is older than this change: it already happened between a failed MANIFEST write and the next call with an OK MANIFEST I/O status. The worst case is an old non-retryable error that some call left without setting a background error. `DropColumnFamily()`, `CreateColumnFamily()`, `DeleteFilesInRanges()`, `PromoteL0()`, `IncreaseFullHistoryTsLow()`, the level refit of `CompactRange()`, and the file number reservation before an ingestion are such calls. A caller that takes that old error can set it as a fatal background error, and only a reopen recovers from that. This change fixes the two cases above (an atomic flush that does not wait, and a compaction that fails before its commit). Known cases that stay, all older than this change: - An atomic flush that waits for another flush still returns the old error. This needs two atomic flushes at the same time. - A compaction whose commit fails before MANIFEST I/O, for example with `Corruption` from `LoadTableHandlers()`, reports the old `IOError` with reason `kManifestWrite` instead of its own `Corruption`. The outputs of that compaction were never committed. - A compaction or flush that fails for its own reason picks the reason `kManifestWrite` or `kManifestWriteNoWAL` when the old error exists. For a retryable error of a compaction, this makes it a hard error instead of a soft error. Automatic recovery or `DB::Resume()` then writes a new MANIFEST and recovers, unless an atomic flush is running at that time. The hard error stops the DB, so that atomic flush returns the old error. When the old error is not retryable, this makes it a fatal error, and neither automatic recovery nor `DB::Resume()` recovers. Upstream recovers in this case when a `SetOptions()` ran after the old error, and goes fatal already at the compaction when none ran. The worse case is with `paranoid_checks=false`. By the severity table, a non-retryable `IOError` is no error for `kCompaction` and `kFlushNoWAL`, but a fatal error for `kManifestWrite` and `kManifestWriteNoWAL`. A `NoSpace` error is no error for `kCompaction`, but a hard error for `kManifestWrite`. - Other calls that write the MANIFEST, such as `ApplyWALToManifest()` and the record of the OPTIONS file number in `WriteOptionsFile()`, set `versions_->io_status()` as a background error when their `LogAndApply()` fails, also when it failed before MANIFEST I/O. Three compaction paths in `DBImpl::BackgroundCompaction()` read `versions_->io_status()` after their `LogAndApply()` also when it succeeded: the FIFO deletion compaction, the FIFO trivial copy (a temperature change), and the trivial move. A complete fix tracks the MANIFEST I/O status of each batch, and lets a caller report `versions_->io_status()` only when its own commit failed on MANIFEST I/O. That touches `LogAndApply()`, flush, compaction and the DB callers above, and is planned as a follow-up. - An ingestion whose MANIFEST write fails deletes its ingested file in the rollback, even when the file is quarantined. This gap is older than this change, and it is on a different code path. A follow-up will fix it. - No API, option or format change. Differential Revision: D134907305
Summary: Background: when a MANIFEST write fails, RocksDB cannot know whether the failed edit is in the MANIFEST that CURRENT names. For example, the record can be written and synced before the error is reported. Or a batch can create a new MANIFEST, rename CURRENT to it, and then get an error from the directory sync. So `VersionSet::ProcessManifestWrites()` puts the new files of the failed batch (the edits of one `ProcessManifestWrites()` call) in the error handler quarantine, and the purge of obsolete files does not delete them. When a MANIFEST write fails, `ProcessManifestWrites()` also resets `descriptor_log_`, and a batch without a `descriptor_log_` sets `new_descriptor_log`. So the next batch that writes the MANIFEST always starts a new MANIFEST. Once that new MANIFEST is installed, the old one is no longer used, and the quarantine is cleared. `VersionSet::io_status_` keeps the MANIFEST write error until then. How the code got into this state: - Since meta#7034, `ProcessManifestWrites()` resets `io_status_` after any call whose MANIFEST I/O status is OK. It records only the first error. - meta#12030 added the quarantine. It added the files of a failed batch only inside the "first error" block, and it cleared the quarantine together with the reset of `io_status_`. At that time `SetOptions()` still wrote to the MANIFEST. - meta#13384 made `SetOptions()` skip the MANIFEST write. So every `SetOptions()` became a call with an OK MANIFEST I/O status that writes no new MANIFEST. The problems: 1. Two kinds of calls have an OK MANIFEST I/O status but write no new MANIFEST: - `DBImpl::SetOptions()` installs a new Version through `LogAndApply()` with an edit that writes nothing to the MANIFEST (`skip_manifest_write`). - A batch can fail before any MANIFEST I/O, for example when `LoadTableHandlers()` fails with `paranoid_checks`. A compaction that is already running when a flush's MANIFEST write fails still commits, because the background error is checked only when the compaction starts. After such a call, the old MANIFEST can still hold the failed edit, but its files are no longer quarantined. 2. A second failed MANIFEST write, while `io_status_` already holds an earlier error, does not quarantine its own files. This is a problem when the second batch creates a new MANIFEST and renames CURRENT to it, and then reports an error. CURRENT names the new MANIFEST, and it lists the new files. In both cases, the purge after the failed job, or the purge in a clean `Close()`, deletes the files. When CURRENT names a MANIFEST that lists a deleted file, every `DB::Open()` fails with "Corruption: IO error: No such file or directory ... .sst The file MANIFEST-... may be corrupted". A db_stress crash test hit problem 1: a flush's MANIFEST write got an injected write error, a `SetOptions()` ran about 1.6 ms later and cleared the quarantine, the flush job deleted its SST, and the harness killed the process. This change cannot repair a DB that already lost such an SST. Example of problem 2, reproduced by the new unit test: ``` 1. DropColumnFamily(): the MANIFEST record is synced, then the write reports an error. io_status_ = that error. No background error is set. 2. (optional) SetOptions() succeeds. 3. Flush: starts a new MANIFEST that lists the flushed SST, renames CURRENT to it, then SetCurrentFile() reports an error. io_status_ is already bad, so the SST is not quarantined. 4. The flush job's purge deletes the SST. The next DB::Open() fails. ``` Without step 2, upstream fails this way. With step 2, upstream passes, because `SetOptions()` resets `io_status_` before the flush, so the flush failure is the "first" error. Fixing problem 1 alone would make the run with step 2 fail too, because `SetOptions()` would no longer reset `io_status_`. So this change fixes both problems. New behavior: - Every MANIFEST write failure adds the files of its batch to the quarantine. `io_status_` keeps the first error. A file number is added only once, because automatic recovery can retry many times, each retry adds the old MANIFEST again, and each `FindObsoleteFiles()` copies the list. The check uses a hash set of the numbers already in the list, so its cost stays linear in the number of files. A failed batch can have many files, for example a large ingestion or a refit of a whole level, and the check runs under the DB mutex. - `io_status_` is reset and the quarantine is cleared only when the call wrote and installed a new MANIFEST (s is OK and `new_descriptor_log` is set). A batch that skips the MANIFEST write never sets `new_descriptor_log`. Until then, the files stay quarantined. This is enough for recovery: because `descriptor_log_` was reset, the next real MANIFEST write (for example, a non-atomic flush, a compaction, or the forced write in `DB::Resume()`) starts a new MANIFEST, and its success clears the whole quarantine. - `DBImpl::SetOptions()` sets no background error after its `LogAndApply()`. This part is required, not a cleanup. Since meta#13384, that call does no MANIFEST I/O and is never grouped with other writers, so a non-OK `io_status()` there always belongs to another, earlier batch. The removed block was `if (!versions_->io_status().ok()) { assert(!s.ok()); SetBGError(...); }`. With only the `VersionSet` change, `io_status()` stays non-OK after a successful `SetOptions()`. Then a debug build fails the assert, and a release build sets the old error as a fatal background error, so the next write fails. A backport must include this part. - Two callers do not take the old error in `versions_->io_status()` as their own when they did not fail on MANIFEST I/O: - An atomic flush checks `versions_->io_status()` while it waits for its turn to install. The check stops the wait when the flush that it waits for failed its MANIFEST write. A flush that does not wait for another flush, while the DB is not stopped, ignores the error. With no background error, it installs its result. That install writes a new MANIFEST, and its success clears the old error and the quarantine. With a soft background error, a flush that is not for recovery gets that soft error from the code after the wait and does not install, so it reports its own error. A recovery flush installs, which writes the new MANIFEST. While the DB is stopped, the flush returns the error and does not install, as before this change while io_status() holds the error. The check must stay in that case: when the DB is stopped, the code after the wait checks only the recovery error, which is OK when no recovery runs. This also fixes an upstream case. Upstream, a `SetOptions()` after a failed MANIFEST write resets io_status(). A later atomic flush could then install its result after a fatal background error stopped the DB. With this change, io_status() keeps the error until a new MANIFEST is written, so that flush returns the error instead. Without any old MANIFEST error, an atomic flush that is already running can still install after a fatal background error. That is older than this change and not about the MANIFEST, so this change leaves it. - `CompactionJob::Install()` takes `versions_->io_status()` as the job's I/O status only when the commit itself failed. A compaction that failed before its commit, for example when it could not write an output file, keeps its own I/O status. - The SstFileManager recovers from a soft `NoSpace` error with a direct clear in `ErrorHandler::RecoverFromBGError()`, which does not go through `DBImpl::ResumeImpl()` and writes no MANIFEST. When the quarantine is not empty or `versions_->io_status()` is not OK, the direct clear first writes a new MANIFEST with an empty edit, as `DBImpl::ResumeImpl()` does. If that write fails, the direct clear sets the error of that write as a background error with reason `kManifestWrite`, does not clear, and returns. The SstFileManager then retries, unless that error is fatal. If shutdown has started, the direct clear does not write and returns `ShutdownInProgress`, as `DBImpl::ResumeImpl()` does. Some calls set no background error when their MANIFEST write fails, for example a failed `PromoteL0()` or `DropColumnFamily()`, so a later soft error can find files in the quarantine. Without this part, a debug build then fails the assert in `ErrorHandler::ClearBGError()` that the quarantine is empty, and a release build clears the error while the old MANIFEST may still hold the failed edit. Upstream already fails the assert after a failed `PromoteL0()` followed by a soft error, and this change fixes that case too. With the rest of this change, a `SetOptions()` between them keeps the quarantine, so without this part the assert would fire in that case as well. This change closes both problems above: the `SetOptions()` trigger, the "failure before MANIFEST I/O" trigger, and the second failed MANIFEST write, with or without a `SetOptions()` in between. The same rule also covers a running compaction whose commit fails at the CURRENT step after a flush's MANIFEST write failed (derived from the code, not tested separately). Consequences: - Quarantined files can stay longer, until the next new MANIFEST or a reopen. This only delays their deletion. - The automatic clear of a soft `NoSpace` error by the SstFileManager can take longer after an older failed MANIFEST write. It writes a new MANIFEST first, and it keeps the error if that write fails. This is needed because the older failed write may still be in the old MANIFEST, and clearing the error without a new MANIFEST would forget that. If that MANIFEST write fails, its error becomes a hard error, so writes stop, and the later retry runs the full recovery of `DB::Resume()`, which flushes all column families. If the failed write has a non-retryable `IOError`, the error is fatal, and only a reopen recovers. - `DB::Resume()` writes a new MANIFEST when `versions_->io_status()` is an `IOError`. Without this change, a `SetOptions()` before the `Resume()` made it skip that write. With this change the write happens, and it clears the quarantine. - `versions_->io_status()` keeps an old MANIFEST write error until a new MANIFEST is written, and a `SetOptions()` call does not reset it. Several callers read it after their own failure and report it as their own error. This misattribution is older than this change: it already happened between a failed MANIFEST write and the next call with an OK MANIFEST I/O status. The worst case is an old non-retryable error that some call left without setting a background error. `DropColumnFamily()`, `CreateColumnFamily()`, `DeleteFilesInRanges()`, `PromoteL0()`, `IncreaseFullHistoryTsLow()`, the level refit of `CompactRange()`, and the file number reservation before an ingestion are such calls. A caller that takes that old error can set it as a fatal background error, and only a reopen recovers from that. This change fixes the two cases above (an atomic flush that does not wait, and a compaction that fails before its commit). Known cases that stay, all older than this change: - An atomic flush that waits for another flush still returns the old error. This needs two atomic flushes at the same time. - A compaction whose commit fails before MANIFEST I/O, for example with `Corruption` from `LoadTableHandlers()`, reports the old `IOError` with reason `kManifestWrite` instead of its own `Corruption`. The outputs of that compaction were never committed. - A compaction or flush that fails for its own reason picks the reason `kManifestWrite` or `kManifestWriteNoWAL` when the old error exists. For a retryable error of a compaction, this makes it a hard error instead of a soft error. Automatic recovery or `DB::Resume()` then writes a new MANIFEST and recovers, unless an atomic flush is running at that time. The hard error stops the DB, so that atomic flush returns the old error. When the old error is not retryable, this makes it a fatal error, and neither automatic recovery nor `DB::Resume()` recovers. Upstream recovers in this case when a `SetOptions()` ran after the old error, and goes fatal already at the compaction when none ran. The worse case is with `paranoid_checks=false`. By the severity table, a non-retryable `IOError` is no error for `kCompaction` and `kFlushNoWAL`, but a fatal error for `kManifestWrite` and `kManifestWriteNoWAL`. A `NoSpace` error is no error for `kCompaction`, but a hard error for `kManifestWrite`. - Other calls that write the MANIFEST, such as `ApplyWALToManifest()` and the record of the OPTIONS file number in `WriteOptionsFile()`, set `versions_->io_status()` as a background error when their `LogAndApply()` fails, also when it failed before MANIFEST I/O. Three compaction paths in `DBImpl::BackgroundCompaction()` read `versions_->io_status()` after their `LogAndApply()` also when it succeeded: the FIFO deletion compaction, the FIFO trivial copy (a temperature change), and the trivial move. A complete fix tracks the MANIFEST I/O status of each batch, and lets a caller report `versions_->io_status()` only when its own commit failed on MANIFEST I/O. That touches `LogAndApply()`, flush, compaction and the DB callers above, and is planned as a follow-up. - An ingestion whose MANIFEST write fails deletes its ingested file in the rollback, even when the file is quarantined. This gap is older than this change, and it is on a different code path. A follow-up will fix it. - No API, option or format change. Differential Revision: D134907305
Summary: Pull Request resolved: meta#15818 Background: when a MANIFEST write fails, RocksDB cannot know whether the failed edit is in the MANIFEST that CURRENT names. For example, the record can be written and synced before the error is reported. Or a batch can create a new MANIFEST, rename CURRENT to it, and then get an error from the directory sync. So `VersionSet::ProcessManifestWrites()` puts the new files of the failed batch (the edits of one `ProcessManifestWrites()` call) in the error handler quarantine, and the purge of obsolete files does not delete them. When a MANIFEST write fails, `ProcessManifestWrites()` also resets `descriptor_log_`, and a batch without a `descriptor_log_` sets `new_descriptor_log`. So the next batch that writes the MANIFEST always starts a new MANIFEST. Once that new MANIFEST is installed, the old one is no longer used, and the quarantine is cleared. `VersionSet::io_status_` keeps the MANIFEST write error until then. How the code got into this state: - Since meta#7034, `ProcessManifestWrites()` resets `io_status_` after any call whose MANIFEST I/O status is OK. It records only the first error. - meta#12030 added the quarantine. It added the files of a failed batch only inside the "first error" block, and it cleared the quarantine together with the reset of `io_status_`. At that time `SetOptions()` still wrote to the MANIFEST. - meta#13384 made `SetOptions()` skip the MANIFEST write. So every `SetOptions()` became a call with an OK MANIFEST I/O status that writes no new MANIFEST. The problems: 1. Two kinds of calls have an OK MANIFEST I/O status but write no new MANIFEST: - `DBImpl::SetOptions()` installs a new Version through `LogAndApply()` with an edit that writes nothing to the MANIFEST (`skip_manifest_write`). - A batch can fail before any MANIFEST I/O, for example when `LoadTableHandlers()` fails with `paranoid_checks`. A compaction that is already running when a flush's MANIFEST write fails still commits, because the background error is checked only when the compaction starts. After such a call, the old MANIFEST can still hold the failed edit, but its files are no longer quarantined. 2. A second failed MANIFEST write, while `io_status_` already holds an earlier error, does not quarantine its own files. This is a problem when the second batch creates a new MANIFEST and renames CURRENT to it, and then reports an error. CURRENT names the new MANIFEST, and it lists the new files. In both cases, the purge after the failed job, or the purge in a clean `Close()`, deletes the files. When CURRENT names a MANIFEST that lists a deleted file, every `DB::Open()` fails with "Corruption: IO error: No such file or directory ... .sst The file MANIFEST-... may be corrupted". A db_stress crash test hit problem 1: a flush's MANIFEST write got an injected write error, a `SetOptions()` ran about 1.6 ms later and cleared the quarantine, the flush job deleted its SST, and the harness killed the process. This change cannot repair a DB that already lost such an SST. Example of problem 2, reproduced by the new unit test: ``` 1. DropColumnFamily(): the MANIFEST record is synced, then the write reports an error. io_status_ = that error. No background error is set. 2. (optional) SetOptions() succeeds. 3. Flush: starts a new MANIFEST that lists the flushed SST, renames CURRENT to it, then SetCurrentFile() reports an error. io_status_ is already bad, so the SST is not quarantined. 4. The flush job's purge deletes the SST. The next DB::Open() fails. ``` Without step 2, upstream fails this way. With step 2, upstream passes, because `SetOptions()` resets `io_status_` before the flush, so the flush failure is the "first" error. Fixing problem 1 alone would make the run with step 2 fail too, because `SetOptions()` would no longer reset `io_status_`. So this change fixes both problems. New behavior: - Every MANIFEST write failure adds the files of its batch to the quarantine. `io_status_` keeps the first error. A file number is added only once, because automatic recovery can retry many times, each retry adds the old MANIFEST again, and each `FindObsoleteFiles()` copies the list. The check uses a hash set of the numbers already in the list, so its cost stays linear in the number of files. A failed batch can have many files, for example a large ingestion or a refit of a whole level, and the check runs under the DB mutex. - `io_status_` is reset and the quarantine is cleared only when the call wrote and installed a new MANIFEST (s is OK and `new_descriptor_log` is set). A batch that skips the MANIFEST write never sets `new_descriptor_log`. Until then, the files stay quarantined. This is enough for recovery: because `descriptor_log_` was reset, the next real MANIFEST write (for example, a non-atomic flush, a compaction, or the forced write in `DB::Resume()`) starts a new MANIFEST, and its success clears the whole quarantine. - `DBImpl::SetOptions()` sets no background error after its `LogAndApply()`. This part is required, not a cleanup. Since meta#13384, that call does no MANIFEST I/O and is never grouped with other writers, so a non-OK `io_status()` there always belongs to another, earlier batch. The removed block was `if (!versions_->io_status().ok()) { assert(!s.ok()); SetBGError(...); }`. With only the `VersionSet` change, `io_status()` stays non-OK after a successful `SetOptions()`. Then a debug build fails the assert, and a release build sets the old error as a fatal background error, so the next write fails. A backport must include this part. - Two callers do not take the old error in `versions_->io_status()` as their own when they did not fail on MANIFEST I/O: - An atomic flush checks `versions_->io_status()` while it waits for its turn to install. The check stops the wait when the flush that it waits for failed its MANIFEST write. A flush that does not wait for another flush, while the DB is not stopped, ignores the error. With no background error, it installs its result. That install writes a new MANIFEST, and its success clears the old error and the quarantine. With a soft background error, a flush that is not for recovery gets that soft error from the code after the wait and does not install, so it reports its own error. A recovery flush installs, which writes the new MANIFEST. While the DB is stopped, the flush returns the error and does not install, as before this change while io_status() holds the error. The check must stay in that case: when the DB is stopped, the code after the wait checks only the recovery error, which is OK when no recovery runs. This also fixes an upstream case. Upstream, a `SetOptions()` after a failed MANIFEST write resets io_status(). A later atomic flush could then install its result after a fatal background error stopped the DB. With this change, io_status() keeps the error until a new MANIFEST is written, so that flush returns the error instead. Without any old MANIFEST error, an atomic flush that is already running can still install after a fatal background error. That is older than this change and not about the MANIFEST, so this change leaves it. - `CompactionJob::Install()` takes `versions_->io_status()` as the job's I/O status only when the commit itself failed. A compaction that failed before its commit, for example when it could not write an output file, keeps its own I/O status. - The SstFileManager recovers from a soft `NoSpace` error with a direct clear in `ErrorHandler::RecoverFromBGError()`, which does not go through `DBImpl::ResumeImpl()` and writes no MANIFEST. When the quarantine is not empty or `versions_->io_status()` is not OK, the direct clear first writes a new MANIFEST with an empty edit, as `DBImpl::ResumeImpl()` does. If that write fails, the direct clear sets the error of that write as a background error with reason `kManifestWrite`, does not clear, and returns. The SstFileManager then retries, unless that error is fatal. If shutdown has started, the direct clear does not write and returns `ShutdownInProgress`, as `DBImpl::ResumeImpl()` does. Some calls set no background error when their MANIFEST write fails, for example a failed `PromoteL0()` or `DropColumnFamily()`, so a later soft error can find files in the quarantine. Without this part, a debug build then fails the assert in `ErrorHandler::ClearBGError()` that the quarantine is empty, and a release build clears the error while the old MANIFEST may still hold the failed edit. Upstream already fails the assert after a failed `PromoteL0()` followed by a soft error, and this change fixes that case too. With the rest of this change, a `SetOptions()` between them keeps the quarantine, so without this part the assert would fire in that case as well. This change closes both problems above: the `SetOptions()` trigger, the "failure before MANIFEST I/O" trigger, and the second failed MANIFEST write, with or without a `SetOptions()` in between. The same rule also covers a running compaction whose commit fails at the CURRENT step after a flush's MANIFEST write failed (derived from the code, not tested separately). Consequences: - Quarantined files can stay longer, until the next new MANIFEST or a reopen. This only delays their deletion. - The automatic clear of a soft `NoSpace` error by the SstFileManager can take longer after an older failed MANIFEST write. It writes a new MANIFEST first, and it keeps the error if that write fails. This is needed because the older failed write may still be in the old MANIFEST, and clearing the error without a new MANIFEST would forget that. If that MANIFEST write fails, its error becomes a hard error, so writes stop, and the later retry runs the full recovery of `DB::Resume()`, which flushes all column families. If the failed write has a non-retryable `IOError`, the error is fatal, and only a reopen recovers. - `DB::Resume()` writes a new MANIFEST when `versions_->io_status()` is an `IOError`. Without this change, a `SetOptions()` before the `Resume()` made it skip that write. With this change the write happens, and it clears the quarantine. - `versions_->io_status()` keeps an old MANIFEST write error until a new MANIFEST is written, and a `SetOptions()` call does not reset it. Several callers read it after their own failure and report it as their own error. This misattribution is older than this change: it already happened between a failed MANIFEST write and the next call with an OK MANIFEST I/O status. The worst case is an old non-retryable error that some call left without setting a background error. `DropColumnFamily()`, `CreateColumnFamily()`, `DeleteFilesInRanges()`, `PromoteL0()`, `IncreaseFullHistoryTsLow()`, the level refit of `CompactRange()`, and the file number reservation before an ingestion are such calls. A caller that takes that old error can set it as a fatal background error, and only a reopen recovers from that. This change fixes the two cases above (an atomic flush that does not wait, and a compaction that fails before its commit). Known cases that stay, all older than this change: - An atomic flush that waits for another flush still returns the old error. This needs two atomic flushes at the same time. - A compaction whose commit fails before MANIFEST I/O, for example with `Corruption` from `LoadTableHandlers()`, reports the old `IOError` with reason `kManifestWrite` instead of its own `Corruption`. The outputs of that compaction were never committed. - A compaction or flush that fails for its own reason picks the reason `kManifestWrite` or `kManifestWriteNoWAL` when the old error exists. For a retryable error of a compaction, this makes it a hard error instead of a soft error. Automatic recovery or `DB::Resume()` then writes a new MANIFEST and recovers, unless an atomic flush is running at that time. The hard error stops the DB, so that atomic flush returns the old error. When the old error is not retryable, this makes it a fatal error, and neither automatic recovery nor `DB::Resume()` recovers. Upstream recovers in this case when a `SetOptions()` ran after the old error, and goes fatal already at the compaction when none ran. The worse case is with `paranoid_checks=false`. By the severity table, a non-retryable `IOError` is no error for `kCompaction` and `kFlushNoWAL`, but a fatal error for `kManifestWrite` and `kManifestWriteNoWAL`. A `NoSpace` error is no error for `kCompaction`, but a hard error for `kManifestWrite`. - Other calls that write the MANIFEST, such as `ApplyWALToManifest()` and the record of the OPTIONS file number in `WriteOptionsFile()`, set `versions_->io_status()` as a background error when their `LogAndApply()` fails, also when it failed before MANIFEST I/O. Three compaction paths in `DBImpl::BackgroundCompaction()` read `versions_->io_status()` after their `LogAndApply()` also when it succeeded: the FIFO deletion compaction, the FIFO trivial copy (a temperature change), and the trivial move. A complete fix tracks the MANIFEST I/O status of each batch, and lets a caller report `versions_->io_status()` only when its own commit failed on MANIFEST I/O. That touches `LogAndApply()`, flush, compaction and the DB callers above, and is planned as a follow-up. - An ingestion whose MANIFEST write fails deletes its ingested file in the rollback, even when the file is quarantined. This gap is older than this change, and it is on a different code path. A follow-up will fix it. - No API, option or format change. Differential Revision: D134907305
Summary: Pull Request resolved: meta#15818 Background: when a MANIFEST write fails, RocksDB cannot know whether the failed edit is in the MANIFEST that CURRENT names. For example, the record can be written and synced before the error is reported. Or a batch can create a new MANIFEST, rename CURRENT to it, and then get an error from the directory sync. So `VersionSet::ProcessManifestWrites()` puts the new files of the failed batch (the edits of one `ProcessManifestWrites()` call) in the error handler quarantine, and the purge of obsolete files does not delete them. When a MANIFEST write fails, `ProcessManifestWrites()` also resets `descriptor_log_`, and a batch without a `descriptor_log_` sets `new_descriptor_log`. So the next batch that writes the MANIFEST always starts a new MANIFEST. Once that new MANIFEST is installed, the old one is no longer used, and the quarantine is cleared. `VersionSet::io_status_` keeps the MANIFEST write error until then. How the code got into this state: - Since meta#7034, `ProcessManifestWrites()` resets `io_status_` after any call whose MANIFEST I/O status is OK. It records only the first error. - meta#12030 added the quarantine. It added the files of a failed batch only inside the "first error" block, and it cleared the quarantine together with the reset of `io_status_`. At that time `SetOptions()` still wrote to the MANIFEST. - meta#13384 made `SetOptions()` skip the MANIFEST write. So every `SetOptions()` became a call with an OK MANIFEST I/O status that writes no new MANIFEST. The problems: 1. Two kinds of calls have an OK MANIFEST I/O status but write no new MANIFEST: - `DBImpl::SetOptions()` installs a new Version through `LogAndApply()` with an edit that writes nothing to the MANIFEST (`skip_manifest_write`). - A batch can fail before any MANIFEST I/O, for example when `LoadTableHandlers()` fails with `paranoid_checks`. A compaction that is already running when a flush's MANIFEST write fails still commits, because the background error is checked only when the compaction starts. After such a call, the old MANIFEST can still hold the failed edit, but its files are no longer quarantined. 2. A second failed MANIFEST write, while `io_status_` already holds an earlier error, does not quarantine its own files. This is a problem when the second batch creates a new MANIFEST and renames CURRENT to it, and then reports an error. CURRENT names the new MANIFEST, and it lists the new files. In both cases, the purge after the failed job, or the purge in a clean `Close()`, deletes the files. When CURRENT names a MANIFEST that lists a deleted file, every `DB::Open()` fails with "Corruption: IO error: No such file or directory ... .sst The file MANIFEST-... may be corrupted". A db_stress crash test hit problem 1: a flush's MANIFEST write got an injected write error, a `SetOptions()` ran about 1.6 ms later and cleared the quarantine, the flush job deleted its SST, and the harness killed the process. This change cannot repair a DB that already lost such an SST. Example of problem 2, reproduced by the new unit test: ``` 1. DropColumnFamily(): the MANIFEST record is synced, then the write reports an error. io_status_ = that error. No background error is set. 2. (optional) SetOptions() succeeds. 3. Flush: starts a new MANIFEST that lists the flushed SST, renames CURRENT to it, then SetCurrentFile() reports an error. io_status_ is already bad, so the SST is not quarantined. 4. The flush job's purge deletes the SST. The next DB::Open() fails. ``` Without step 2, upstream fails this way. With step 2, upstream passes, because `SetOptions()` resets `io_status_` before the flush, so the flush failure is the "first" error. Fixing problem 1 alone would make the run with step 2 fail too, because `SetOptions()` would no longer reset `io_status_`. So this change fixes both problems. New behavior: - Every MANIFEST write failure adds the files of its batch to the quarantine. `io_status_` keeps the first error. A file number is added only once, because automatic recovery can retry many times, each retry adds the old MANIFEST again, and each `FindObsoleteFiles()` copies the list. The check uses a hash set of the numbers already in the list, so its cost stays linear in the number of files. A failed batch can have many files, for example a large ingestion or a refit of a whole level, and the check runs under the DB mutex. - `io_status_` is reset and the quarantine is cleared only when the call wrote and installed a new MANIFEST (s is OK and `new_descriptor_log` is set). A batch that skips the MANIFEST write never sets `new_descriptor_log`. Until then, the files stay quarantined. This is enough for recovery: because `descriptor_log_` was reset, the next real MANIFEST write (for example, a non-atomic flush, a compaction, or the forced write in `DB::Resume()`) starts a new MANIFEST, and its success clears the whole quarantine. - `DBImpl::SetOptions()` sets no background error after its `LogAndApply()`. This part is required, not a cleanup. Since meta#13384, that call does no MANIFEST I/O and is never grouped with other writers, so a non-OK `io_status()` there always belongs to another, earlier batch. The removed block was `if (!versions_->io_status().ok()) { assert(!s.ok()); SetBGError(...); }`. With only the `VersionSet` change, `io_status()` stays non-OK after a successful `SetOptions()`. Then a debug build fails the assert, and a release build sets the old error as a fatal background error, so the next write fails. A backport must include this part. - Two callers do not take the old error in `versions_->io_status()` as their own when they did not fail on MANIFEST I/O: - An atomic flush checks `versions_->io_status()` while it waits for its turn to install. The check stops the wait when the flush that it waits for failed its MANIFEST write. A flush that does not wait for another flush, while the DB is not stopped, ignores the error. With no background error, it installs its result. That install writes a new MANIFEST, and its success clears the old error and the quarantine. With a soft background error, a flush that is not for recovery gets that soft error from the code after the wait and does not install, so it reports its own error. A recovery flush installs, which writes the new MANIFEST. While the DB is stopped, the flush returns the error and does not install, as before this change while io_status() holds the error. The check must stay in that case: when the DB is stopped, the code after the wait checks only the recovery error, which is OK when no recovery runs. This also fixes an upstream case. Upstream, a `SetOptions()` after a failed MANIFEST write resets io_status(). A later atomic flush could then install its result after a fatal background error stopped the DB. With this change, io_status() keeps the error until a new MANIFEST is written, so that flush returns the error instead. Without any old MANIFEST error, an atomic flush that is already running can still install after a fatal background error. That is older than this change and not about the MANIFEST, so this change leaves it. - `CompactionJob::Install()` takes `versions_->io_status()` as the job's I/O status only when the commit itself failed. A compaction that failed before its commit, for example when it could not write an output file, keeps its own I/O status. - The SstFileManager recovers from a soft `NoSpace` error with a direct clear in `ErrorHandler::RecoverFromBGError()`, which does not go through `DBImpl::ResumeImpl()` and writes no MANIFEST. When the quarantine is not empty or `versions_->io_status()` is not OK, the direct clear first writes a new MANIFEST with an empty edit, as `DBImpl::ResumeImpl()` does. If that write fails, the direct clear sets the error of that write as a background error with reason `kManifestWrite`, does not clear, and returns. The SstFileManager then retries, unless that error is fatal. If shutdown has started, the direct clear does not write and returns `ShutdownInProgress`, as `DBImpl::ResumeImpl()` does. Some calls set no background error when their MANIFEST write fails, for example a failed `PromoteL0()` or `DropColumnFamily()`, so a later soft error can find files in the quarantine. Without this part, a debug build then fails the assert in `ErrorHandler::ClearBGError()` that the quarantine is empty, and a release build clears the error while the old MANIFEST may still hold the failed edit. Upstream already fails the assert after a failed `PromoteL0()` followed by a soft error, and this change fixes that case too. With the rest of this change, a `SetOptions()` between them keeps the quarantine, so without this part the assert would fire in that case as well. This change closes both problems above: the `SetOptions()` trigger, the "failure before MANIFEST I/O" trigger, and the second failed MANIFEST write, with or without a `SetOptions()` in between. The same rule also covers a running compaction whose commit fails at the CURRENT step after a flush's MANIFEST write failed (derived from the code, not tested separately). Consequences: - Quarantined files can stay longer, until the next new MANIFEST or a reopen. This only delays their deletion. - The automatic clear of a soft `NoSpace` error by the SstFileManager can take longer after an older failed MANIFEST write. It writes a new MANIFEST first, and it keeps the error if that write fails. This is needed because the older failed write may still be in the old MANIFEST, and clearing the error without a new MANIFEST would forget that. If that MANIFEST write fails, its error becomes a hard error, so writes stop, and the later retry runs the full recovery of `DB::Resume()`, which flushes all column families. If the failed write has a non-retryable `IOError`, the error is fatal, and only a reopen recovers. - `DB::Resume()` writes a new MANIFEST when `versions_->io_status()` is an `IOError`. Without this change, a `SetOptions()` before the `Resume()` made it skip that write. With this change the write happens, and it clears the quarantine. - `versions_->io_status()` keeps an old MANIFEST write error until a new MANIFEST is written, and a `SetOptions()` call does not reset it. Several callers read it after their own failure and report it as their own error. This misattribution is older than this change: it already happened between a failed MANIFEST write and the next call with an OK MANIFEST I/O status. The worst case is an old non-retryable error that some call left without setting a background error. `DropColumnFamily()`, `CreateColumnFamily()`, `DeleteFilesInRanges()`, `PromoteL0()`, `IncreaseFullHistoryTsLow()`, the level refit of `CompactRange()`, and the file number reservation before an ingestion are such calls. A caller that takes that old error can set it as a fatal background error, and only a reopen recovers from that. This change fixes the two cases above (an atomic flush that does not wait, and a compaction that fails before its commit). Known cases that stay, all older than this change: - An atomic flush that waits for another flush still returns the old error. This needs two atomic flushes at the same time. - A compaction whose commit fails before MANIFEST I/O, for example with `Corruption` from `LoadTableHandlers()`, reports the old `IOError` with reason `kManifestWrite` instead of its own `Corruption`. The outputs of that compaction were never committed. - A compaction or flush that fails for its own reason picks the reason `kManifestWrite` or `kManifestWriteNoWAL` when the old error exists. For a retryable error of a compaction, this makes it a hard error instead of a soft error. Automatic recovery or `DB::Resume()` then writes a new MANIFEST and recovers, unless an atomic flush is running at that time. The hard error stops the DB, so that atomic flush returns the old error. When the old error is not retryable, this makes it a fatal error, and neither automatic recovery nor `DB::Resume()` recovers. Upstream recovers in this case when a `SetOptions()` ran after the old error, and goes fatal already at the compaction when none ran. The worse case is with `paranoid_checks=false`. By the severity table, a non-retryable `IOError` is no error for `kCompaction` and `kFlushNoWAL`, but a fatal error for `kManifestWrite` and `kManifestWriteNoWAL`. A `NoSpace` error is no error for `kCompaction`, but a hard error for `kManifestWrite`. - Other calls that write the MANIFEST, such as `ApplyWALToManifest()` and the record of the OPTIONS file number in `WriteOptionsFile()`, set `versions_->io_status()` as a background error when their `LogAndApply()` fails, also when it failed before MANIFEST I/O. Three compaction paths in `DBImpl::BackgroundCompaction()` read `versions_->io_status()` after their `LogAndApply()` also when it succeeded: the FIFO deletion compaction, the FIFO trivial copy (a temperature change), and the trivial move. A complete fix tracks the MANIFEST I/O status of each batch, and lets a caller report `versions_->io_status()` only when its own commit failed on MANIFEST I/O. That touches `LogAndApply()`, flush, compaction and the DB callers above, and is planned as a follow-up. - An ingestion whose MANIFEST write fails deletes its ingested file in the rollback, even when the file is quarantined. This gap is older than this change, and it is on a different code path. A follow-up will fix it. - No API, option or format change. Differential Revision: D134907305
Summary: Pull Request resolved: meta#15818 Background: when a MANIFEST write fails, RocksDB cannot know whether the failed edit is in the MANIFEST that CURRENT names. For example, the record can be written and synced before the error is reported. Or a batch can create a new MANIFEST, rename CURRENT to it, and then get an error from the directory sync. So `VersionSet::ProcessManifestWrites()` puts the new files of the failed batch (the edits of one `ProcessManifestWrites()` call) in the error handler quarantine, and the purge of obsolete files does not delete them. When a MANIFEST write fails, `ProcessManifestWrites()` also resets `descriptor_log_`, and a batch without a `descriptor_log_` sets `new_descriptor_log`. So the next batch that writes the MANIFEST always starts a new MANIFEST. Once that new MANIFEST is installed, the old one is no longer used, and the quarantine is cleared. `VersionSet::io_status_` keeps the MANIFEST write error until then. How the code got into this state: - Since meta#7034, `ProcessManifestWrites()` resets `io_status_` after any call whose MANIFEST I/O status is OK. It records only the first error. - meta#12030 added the quarantine. It added the files of a failed batch only inside the "first error" block, and it cleared the quarantine together with the reset of `io_status_`. At that time `SetOptions()` still wrote to the MANIFEST. - meta#13384 made `SetOptions()` skip the MANIFEST write. So every `SetOptions()` became a call with an OK MANIFEST I/O status that writes no new MANIFEST. The problems: 1. Two kinds of calls have an OK MANIFEST I/O status but write no new MANIFEST: - `DBImpl::SetOptions()` installs a new Version through `LogAndApply()` with an edit that writes nothing to the MANIFEST (`skip_manifest_write`). - A batch can fail before any MANIFEST I/O, for example when `LoadTableHandlers()` fails with `paranoid_checks`. A compaction that is already running when a flush's MANIFEST write fails still commits, because the background error is checked only when the compaction starts. After such a call, the old MANIFEST can still hold the failed edit, but its files are no longer quarantined. 2. A second failed MANIFEST write, while `io_status_` already holds an earlier error, does not quarantine its own files. This is a problem when the second batch creates a new MANIFEST and renames CURRENT to it, and then reports an error. CURRENT names the new MANIFEST, and it lists the new files. In both cases, the purge after the failed job, or the purge in a clean `Close()`, deletes the files. When CURRENT names a MANIFEST that lists a deleted file, every `DB::Open()` fails with "Corruption: IO error: No such file or directory ... .sst The file MANIFEST-... may be corrupted". A db_stress crash test hit problem 1: a flush's MANIFEST write got an injected write error, a `SetOptions()` ran about 1.6 ms later and cleared the quarantine, the flush job deleted its SST, and the harness killed the process. This change cannot repair a DB that already lost such an SST. Example of problem 2, reproduced by the new unit test: ``` 1. DropColumnFamily(): the MANIFEST record is synced, then the write reports an error. io_status_ = that error. No background error is set. 2. (optional) SetOptions() succeeds. 3. Flush: starts a new MANIFEST that lists the flushed SST, renames CURRENT to it, then SetCurrentFile() reports an error. io_status_ is already bad, so the SST is not quarantined. 4. The flush job's purge deletes the SST. The next DB::Open() fails. ``` Without step 2, upstream fails this way. With step 2, upstream passes, because `SetOptions()` resets `io_status_` before the flush, so the flush failure is the "first" error. Fixing problem 1 alone would make the run with step 2 fail too, because `SetOptions()` would no longer reset `io_status_`. So this change fixes both problems. New behavior: - Every MANIFEST write failure adds the files of its batch to the quarantine. `io_status_` keeps the first error. A file number is added only once, because automatic recovery can retry many times, each retry adds the old MANIFEST again, and each `FindObsoleteFiles()` copies the list. The check uses a hash set of the numbers already in the list, so its cost stays linear in the number of files. A failed batch can have many files, for example a large ingestion or a refit of a whole level, and the check runs under the DB mutex. - `io_status_` is reset and the quarantine is cleared only when the call wrote and installed a new MANIFEST (s is OK and `new_descriptor_log` is set). A batch that skips the MANIFEST write never sets `new_descriptor_log`. Until then, the files stay quarantined. This is enough for recovery: because `descriptor_log_` was reset, the next real MANIFEST write (for example, a non-atomic flush, a compaction, or the forced write in `DB::Resume()`) starts a new MANIFEST, and its success clears the whole quarantine. - `DBImpl::SetOptions()` sets no background error after its `LogAndApply()`. This part is required, not a cleanup. Since meta#13384, that call does no MANIFEST I/O and is never grouped with other writers, so a non-OK `io_status()` there always belongs to another, earlier batch. The removed block was `if (!versions_->io_status().ok()) { assert(!s.ok()); SetBGError(...); }`. With only the `VersionSet` change, `io_status()` stays non-OK after a successful `SetOptions()`. Then a debug build fails the assert, and a release build sets the old error as a fatal background error, so the next write fails. A backport must include this part. - Two callers do not take the old error in `versions_->io_status()` as their own when they did not fail on MANIFEST I/O: - An atomic flush checks `versions_->io_status()` while it waits for its turn to install. The check stops the wait when the flush that it waits for failed its MANIFEST write. A flush that does not wait for another flush, while the DB is not stopped, ignores the error. With no background error, it installs its result. That install writes a new MANIFEST, and its success clears the old error and the quarantine. With a soft background error, a flush that is not for recovery gets that soft error from the code after the wait and does not install, so it reports its own error. A recovery flush installs, which writes the new MANIFEST. While the DB is stopped, the flush returns the error and does not install, as before this change while io_status() holds the error. The check must stay in that case: when the DB is stopped, the code after the wait checks only the recovery error, which is OK when no recovery runs. This also fixes an upstream case. Upstream, a `SetOptions()` after a failed MANIFEST write resets io_status(). A later atomic flush could then install its result after a fatal background error stopped the DB. With this change, io_status() keeps the error until a new MANIFEST is written, so that flush returns the error instead. Without any old MANIFEST error, an atomic flush that is already running can still install after a fatal background error. That is older than this change and not about the MANIFEST, so this change leaves it. - `CompactionJob::Install()` takes `versions_->io_status()` as the job's I/O status only when the commit itself failed. A compaction that failed before its commit, for example when it could not write an output file, keeps its own I/O status. - The SstFileManager recovers from a soft `NoSpace` error with a direct clear in `ErrorHandler::RecoverFromBGError()`, which does not go through `DBImpl::ResumeImpl()` and writes no MANIFEST. When the quarantine is not empty or `versions_->io_status()` is not OK, the direct clear first writes a new MANIFEST with an empty edit, as `DBImpl::ResumeImpl()` does. If that write fails, the direct clear sets the error of that write as a background error with reason `kManifestWrite`, does not clear, and returns. The SstFileManager then retries, unless that error is fatal. If shutdown has started, the direct clear does not write and returns `ShutdownInProgress`, as `DBImpl::ResumeImpl()` does. Some calls set no background error when their MANIFEST write fails, for example a failed `PromoteL0()` or `DropColumnFamily()`, so a later soft error can find files in the quarantine. Without this part, a debug build then fails the assert in `ErrorHandler::ClearBGError()` that the quarantine is empty, and a release build clears the error while the old MANIFEST may still hold the failed edit. Upstream already fails the assert after a failed `PromoteL0()` followed by a soft error, and this change fixes that case too. With the rest of this change, a `SetOptions()` between them keeps the quarantine, so without this part the assert would fire in that case as well. This change closes both problems above: the `SetOptions()` trigger, the "failure before MANIFEST I/O" trigger, and the second failed MANIFEST write, with or without a `SetOptions()` in between. The same rule also covers a running compaction whose commit fails at the CURRENT step after a flush's MANIFEST write failed (derived from the code, not tested separately). Consequences: - Quarantined files can stay longer, until the next new MANIFEST or a reopen. This only delays their deletion. - The automatic clear of a soft `NoSpace` error by the SstFileManager can take longer after an older failed MANIFEST write. It writes a new MANIFEST first, and it keeps the error if that write fails. This is needed because the older failed write may still be in the old MANIFEST, and clearing the error without a new MANIFEST would forget that. If that MANIFEST write fails, its error becomes a hard error, so writes stop, and the later retry runs the full recovery of `DB::Resume()`, which flushes all column families. If the failed write has a non-retryable `IOError`, the error is fatal, and only a reopen recovers. - `DB::Resume()` writes a new MANIFEST when `versions_->io_status()` is an `IOError`. Without this change, a `SetOptions()` before the `Resume()` made it skip that write. With this change the write happens, and it clears the quarantine. - `versions_->io_status()` keeps an old MANIFEST write error until a new MANIFEST is written, and a `SetOptions()` call does not reset it. Several callers read it after their own failure and report it as their own error. This misattribution is older than this change: it already happened between a failed MANIFEST write and the next call with an OK MANIFEST I/O status. The worst case is an old non-retryable error that some call left without setting a background error. `DropColumnFamily()`, `CreateColumnFamily()`, `DeleteFilesInRanges()`, `PromoteL0()`, `IncreaseFullHistoryTsLow()`, the level refit of `CompactRange()`, and the file number reservation before an ingestion are such calls. A caller that takes that old error can set it as a fatal background error, and only a reopen recovers from that. This change fixes the two cases above (an atomic flush that does not wait, and a compaction that fails before its commit). Known cases that stay, all older than this change: - An atomic flush that waits for another flush still returns the old error. This needs two atomic flushes at the same time. - A compaction whose commit fails before MANIFEST I/O, for example with `Corruption` from `LoadTableHandlers()`, reports the old `IOError` with reason `kManifestWrite` instead of its own `Corruption`. The outputs of that compaction were never committed. - A compaction or flush that fails for its own reason picks the reason `kManifestWrite` or `kManifestWriteNoWAL` when the old error exists. For a retryable error of a compaction, this makes it a hard error instead of a soft error. Automatic recovery or `DB::Resume()` then writes a new MANIFEST and recovers, unless an atomic flush is running at that time. The hard error stops the DB, so that atomic flush returns the old error. When the old error is not retryable, this makes it a fatal error, and neither automatic recovery nor `DB::Resume()` recovers. Upstream recovers in this case when a `SetOptions()` ran after the old error, and goes fatal already at the compaction when none ran. The worse case is with `paranoid_checks=false`. By the severity table, a non-retryable `IOError` is no error for `kCompaction` and `kFlushNoWAL`, but a fatal error for `kManifestWrite` and `kManifestWriteNoWAL`. A `NoSpace` error is no error for `kCompaction`, but a hard error for `kManifestWrite`. - Other calls that write the MANIFEST, such as `ApplyWALToManifest()` and the record of the OPTIONS file number in `WriteOptionsFile()`, set `versions_->io_status()` as a background error when their `LogAndApply()` fails, also when it failed before MANIFEST I/O. Three compaction paths in `DBImpl::BackgroundCompaction()` read `versions_->io_status()` after their `LogAndApply()` also when it succeeded: the FIFO deletion compaction, the FIFO trivial copy (a temperature change), and the trivial move. A complete fix tracks the MANIFEST I/O status of each batch, and lets a caller report `versions_->io_status()` only when its own commit failed on MANIFEST I/O. That touches `LogAndApply()`, flush, compaction and the DB callers above, and is planned as a follow-up. - An ingestion whose MANIFEST write fails deletes its ingested file in the rollback, even when the file is quarantined. This gap is older than this change, and it is on a different code path. A follow-up will fix it. - No API, option or format change. Differential Revision: D134907305
Summary: Pull Request resolved: meta#15818 Background: when a MANIFEST write fails, RocksDB cannot know whether the failed edit is in the MANIFEST that CURRENT names. For example, the record can be written and synced before the error is reported. Or a batch can create a new MANIFEST, rename CURRENT to it, and then get an error from the directory sync. So `VersionSet::ProcessManifestWrites()` puts the new files of the failed batch (the edits of one `ProcessManifestWrites()` call) in the error handler quarantine, and the purge of obsolete files does not delete them. When a MANIFEST write fails, `ProcessManifestWrites()` also resets `descriptor_log_`, and a batch without a `descriptor_log_` sets `new_descriptor_log`. So the next batch that writes the MANIFEST always starts a new MANIFEST. Once that new MANIFEST is installed, the old one is no longer used, and the quarantine is cleared. `VersionSet::io_status_` keeps the MANIFEST write error until then. How the code got into this state: - Since meta#7034, `ProcessManifestWrites()` resets `io_status_` after any call whose MANIFEST I/O status is OK. It records only the first error. - meta#12030 added the quarantine. It added the files of a failed batch only inside the "first error" block, and it cleared the quarantine together with the reset of `io_status_`. At that time `SetOptions()` still wrote to the MANIFEST. - meta#13384 made `SetOptions()` skip the MANIFEST write. So every `SetOptions()` became a call with an OK MANIFEST I/O status that writes no new MANIFEST. The problems: 1. Two kinds of calls have an OK MANIFEST I/O status but write no new MANIFEST: - `DBImpl::SetOptions()` installs a new Version through `LogAndApply()` with an edit that writes nothing to the MANIFEST (`skip_manifest_write`). - A batch can fail before any MANIFEST I/O, for example when `LoadTableHandlers()` fails with `paranoid_checks`. A compaction that is already running when a flush's MANIFEST write fails still commits, because the background error is checked only when the compaction starts. After such a call, the old MANIFEST can still hold the failed edit, but its files are no longer quarantined. 2. A second failed MANIFEST write, while `io_status_` already holds an earlier error, does not quarantine its own files. This is a problem when the second batch creates a new MANIFEST and renames CURRENT to it, and then reports an error. CURRENT names the new MANIFEST, and it lists the new files. In both cases, the purge after the failed job, or the purge in a clean `Close()`, deletes the files. When CURRENT names a MANIFEST that lists a deleted file, every `DB::Open()` fails with "Corruption: IO error: No such file or directory ... .sst The file MANIFEST-... may be corrupted". A db_stress crash test hit problem 1: a flush's MANIFEST write got an injected write error, a `SetOptions()` ran about 1.6 ms later and cleared the quarantine, the flush job deleted its SST, and the harness killed the process. This change cannot repair a DB that already lost such an SST. Example of problem 2, reproduced by the new unit test: ``` 1. DropColumnFamily(): the MANIFEST record is synced, then the write reports an error. io_status_ = that error. No background error is set. 2. (optional) SetOptions() succeeds. 3. Flush: starts a new MANIFEST that lists the flushed SST, renames CURRENT to it, then SetCurrentFile() reports an error. io_status_ is already bad, so the SST is not quarantined. 4. The flush job's purge deletes the SST. The next DB::Open() fails. ``` Without step 2, upstream fails this way. With step 2, upstream passes, because `SetOptions()` resets `io_status_` before the flush, so the flush failure is the "first" error. Fixing problem 1 alone would make the run with step 2 fail too, because `SetOptions()` would no longer reset `io_status_`. So this change fixes both problems. New behavior: - Every MANIFEST write failure adds the files of its batch to the quarantine. `io_status_` keeps the first error. A file number is added only once, because automatic recovery can retry many times, each retry adds the old MANIFEST again, and each `FindObsoleteFiles()` copies the list. The check uses a hash set of the numbers already in the list, so its cost stays linear in the number of files. A failed batch can have many files, for example a large ingestion or a refit of a whole level, and the check runs under the DB mutex. - `io_status_` is reset and the quarantine is cleared only when the call wrote and installed a new MANIFEST (s is OK and `new_descriptor_log` is set). A batch that skips the MANIFEST write never sets `new_descriptor_log`. Until then, the files stay quarantined. This is enough for recovery: because `descriptor_log_` was reset, the next real MANIFEST write (for example, a non-atomic flush, a compaction, or the forced write in `DB::Resume()`) starts a new MANIFEST, and its success clears the whole quarantine. - `DBImpl::SetOptions()` sets no background error after its `LogAndApply()`. This part is required, not a cleanup. Since meta#13384, that call does no MANIFEST I/O and is never grouped with other writers, so a non-OK `io_status()` there always belongs to another, earlier batch. The removed block was `if (!versions_->io_status().ok()) { assert(!s.ok()); SetBGError(...); }`. With only the `VersionSet` change, `io_status()` stays non-OK after a successful `SetOptions()`. Then a debug build fails the assert, and a release build sets the old error as a fatal background error, so the next write fails. A backport must include this part. - Two callers do not take the old error in `versions_->io_status()` as their own when they did not fail on MANIFEST I/O: - An atomic flush checks `versions_->io_status()` while it waits for its turn to install. The check stops the wait when the flush that it waits for failed its MANIFEST write. A flush that does not wait for another flush, while the DB is not stopped, ignores the error. With no background error, it installs its result. That install writes a new MANIFEST, and its success clears the old error and the quarantine. With a soft background error, a flush that is not for recovery gets that soft error from the code after the wait and does not install, so it reports its own error. A recovery flush installs, which writes the new MANIFEST. While the DB is stopped, the flush returns the error and does not install, as before this change while io_status() holds the error. The check must stay in that case: when the DB is stopped, the code after the wait checks only the recovery error, which is OK when no recovery runs. This also fixes an upstream case. Upstream, a `SetOptions()` after a failed MANIFEST write resets io_status(). A later atomic flush could then install its result after a fatal background error stopped the DB. With this change, io_status() keeps the error until a new MANIFEST is written, so that flush returns the error instead. Without any old MANIFEST error, an atomic flush that is already running can still install after a fatal background error. That is older than this change and not about the MANIFEST, so this change leaves it. - `CompactionJob::Install()` takes `versions_->io_status()` as the job's I/O status only when the commit itself failed. A compaction that failed before its commit, for example when it could not write an output file, keeps its own I/O status. - The SstFileManager recovers from a soft `NoSpace` error with a direct clear in `ErrorHandler::RecoverFromBGError()`, which does not go through `DBImpl::ResumeImpl()` and writes no MANIFEST. When the quarantine is not empty or `versions_->io_status()` is not OK, the direct clear first writes a new MANIFEST with an empty edit, as `DBImpl::ResumeImpl()` does. If that write fails, the direct clear sets the error of that write as a background error with reason `kManifestWrite`, does not clear, and returns. The SstFileManager then retries, unless that error is fatal. If shutdown has started, the direct clear does not write and returns `ShutdownInProgress`, as `DBImpl::ResumeImpl()` does. Some calls set no background error when their MANIFEST write fails, for example a failed `PromoteL0()` or `DropColumnFamily()`, so a later soft error can find files in the quarantine. Without this part, a debug build then fails the assert in `ErrorHandler::ClearBGError()` that the quarantine is empty, and a release build clears the error while the old MANIFEST may still hold the failed edit. Upstream already fails the assert after a failed `PromoteL0()` followed by a soft error, and this change fixes that case too. With the rest of this change, a `SetOptions()` between them keeps the quarantine, so without this part the assert would fire in that case as well. This change closes both problems above: the `SetOptions()` trigger, the "failure before MANIFEST I/O" trigger, and the second failed MANIFEST write, with or without a `SetOptions()` in between. The same rule also covers a running compaction whose commit fails at the CURRENT step after a flush's MANIFEST write failed (derived from the code, not tested separately). Consequences: - Quarantined files can stay longer, until the next new MANIFEST or a reopen. This only delays their deletion. - The automatic clear of a soft `NoSpace` error by the SstFileManager can take longer after an older failed MANIFEST write. It writes a new MANIFEST first, and it keeps the error if that write fails. This is needed because the older failed write may still be in the old MANIFEST, and clearing the error without a new MANIFEST would forget that. If that MANIFEST write fails, its error becomes a hard error, so writes stop, and the later retry runs the full recovery of `DB::Resume()`, which flushes all column families. If the failed write has a non-retryable `IOError`, the error is fatal, and only a reopen recovers. - `DB::Resume()` writes a new MANIFEST when `versions_->io_status()` is an `IOError`. Without this change, a `SetOptions()` before the `Resume()` made it skip that write. With this change the write happens, and it clears the quarantine. - `versions_->io_status()` keeps an old MANIFEST write error until a new MANIFEST is written, and a `SetOptions()` call does not reset it. Several callers read it after their own failure and report it as their own error. This misattribution is older than this change: it already happened between a failed MANIFEST write and the next call with an OK MANIFEST I/O status. The worst case is an old non-retryable error that some call left without setting a background error. `DropColumnFamily()`, `CreateColumnFamily()`, `DeleteFilesInRanges()`, `PromoteL0()`, `IncreaseFullHistoryTsLow()`, the level refit of `CompactRange()`, and the file number reservation before an ingestion are such calls. A caller that takes that old error can set it as a fatal background error, and only a reopen recovers from that. This change fixes the two cases above (an atomic flush that does not wait, and a compaction that fails before its commit). Known cases that stay, all older than this change: - An atomic flush that waits for another flush still returns the old error. This needs two atomic flushes at the same time. - A compaction whose commit fails before MANIFEST I/O, for example with `Corruption` from `LoadTableHandlers()`, reports the old `IOError` with reason `kManifestWrite` instead of its own `Corruption`. The outputs of that compaction were never committed. - A compaction or flush that fails for its own reason picks the reason `kManifestWrite` or `kManifestWriteNoWAL` when the old error exists. For a retryable error of a compaction, this makes it a hard error instead of a soft error. Automatic recovery or `DB::Resume()` then writes a new MANIFEST and recovers, unless an atomic flush is running at that time. The hard error stops the DB, so that atomic flush returns the old error. When the old error is not retryable, this makes it a fatal error, and neither automatic recovery nor `DB::Resume()` recovers. Upstream recovers in this case when a `SetOptions()` ran after the old error, and goes fatal already at the compaction when none ran. The worse case is with `paranoid_checks=false`. By the severity table, a non-retryable `IOError` is no error for `kCompaction` and `kFlushNoWAL`, but a fatal error for `kManifestWrite` and `kManifestWriteNoWAL`. A `NoSpace` error is no error for `kCompaction`, but a hard error for `kManifestWrite`. - Other calls that write the MANIFEST, such as `ApplyWALToManifest()` and the record of the OPTIONS file number in `WriteOptionsFile()`, set `versions_->io_status()` as a background error when their `LogAndApply()` fails, also when it failed before MANIFEST I/O. Three compaction paths in `DBImpl::BackgroundCompaction()` read `versions_->io_status()` after their `LogAndApply()` also when it succeeded: the FIFO deletion compaction, the FIFO trivial copy (a temperature change), and the trivial move. A complete fix tracks the MANIFEST I/O status of each batch, and lets a caller report `versions_->io_status()` only when its own commit failed on MANIFEST I/O. That touches `LogAndApply()`, flush, compaction and the DB callers above, and is planned as a follow-up. - An ingestion whose MANIFEST write fails deletes its ingested file in the rollback, even when the file is quarantined. This gap is older than this change, and it is on a different code path. A follow-up will fix it. - No API, option or format change. Differential Revision: D134907305
Summary: Pull Request resolved: meta#15818 Background: when a MANIFEST write fails, RocksDB cannot know whether the failed edit is in the MANIFEST that CURRENT names. For example, the record can be written and synced before the error is reported. Or a batch can create a new MANIFEST, rename CURRENT to it, and then get an error from the directory sync. So `VersionSet::ProcessManifestWrites()` puts the new files of the failed batch (the edits of one `ProcessManifestWrites()` call) in the error handler quarantine, and the purge of obsolete files does not delete them. When a MANIFEST write fails, `ProcessManifestWrites()` also resets `descriptor_log_`, and a batch without a `descriptor_log_` sets `new_descriptor_log`. So the next batch that writes the MANIFEST always starts a new MANIFEST. Once that new MANIFEST is installed, the old one is no longer used, and the quarantine is cleared. `VersionSet::io_status_` keeps the MANIFEST write error until then. How the code got into this state: - Since meta#7034, `ProcessManifestWrites()` resets `io_status_` after any call whose MANIFEST I/O status is OK. It records only the first error. - meta#12030 added the quarantine. It added the files of a failed batch only inside the "first error" block, and it cleared the quarantine together with the reset of `io_status_`. At that time `SetOptions()` still wrote to the MANIFEST. - meta#13384 made `SetOptions()` skip the MANIFEST write. So every `SetOptions()` became a call with an OK MANIFEST I/O status that writes no new MANIFEST. The problems: 1. Two kinds of calls have an OK MANIFEST I/O status but write no new MANIFEST: - `DBImpl::SetOptions()` installs a new Version through `LogAndApply()` with an edit that writes nothing to the MANIFEST (`skip_manifest_write`). - A batch can fail before any MANIFEST I/O, for example when `LoadTableHandlers()` fails with `paranoid_checks`. A compaction that is already running when a flush's MANIFEST write fails still commits, because the background error is checked only when the compaction starts. After such a call, the old MANIFEST can still hold the failed edit, but its files are no longer quarantined. 2. A second failed MANIFEST write, while `io_status_` already holds an earlier error, does not quarantine its own files. This is a problem when the second batch creates a new MANIFEST and renames CURRENT to it, and then reports an error. CURRENT names the new MANIFEST, and it lists the new files. In both cases, the purge after the failed job, or the purge in a clean `Close()`, deletes the files. When CURRENT names a MANIFEST that lists a deleted file, every `DB::Open()` fails with "Corruption: IO error: No such file or directory ... .sst The file MANIFEST-... may be corrupted". A db_stress crash test hit problem 1: a flush's MANIFEST write got an injected write error, a `SetOptions()` ran about 1.6 ms later and cleared the quarantine, the flush job deleted its SST, and the harness killed the process. This change cannot repair a DB that already lost such an SST. Example of problem 2, reproduced by the new unit test: ``` 1. DropColumnFamily(): the MANIFEST record is synced, then the write reports an error. io_status_ = that error. No background error is set. 2. (optional) SetOptions() succeeds. 3. Flush: starts a new MANIFEST that lists the flushed SST, renames CURRENT to it, then SetCurrentFile() reports an error. io_status_ is already bad, so the SST is not quarantined. 4. The flush job's purge deletes the SST. The next DB::Open() fails. ``` Without step 2, upstream fails this way. With step 2, upstream passes, because `SetOptions()` resets `io_status_` before the flush, so the flush failure is the "first" error. Fixing problem 1 alone would make the run with step 2 fail too, because `SetOptions()` would no longer reset `io_status_`. So this change fixes both problems. New behavior: - Every MANIFEST write failure adds the files of its batch to the quarantine. `io_status_` keeps the first error. A file number is added only once, because automatic recovery can retry many times, each retry adds the old MANIFEST again, and each `FindObsoleteFiles()` copies the list. The check uses a hash set of the numbers already in the list, so its cost stays linear in the number of files. A failed batch can have many files, for example a large ingestion or a refit of a whole level, and the check runs under the DB mutex. - `io_status_` is reset and the quarantine is cleared only when the call wrote and installed a new MANIFEST (s is OK and `new_descriptor_log` is set). A batch that skips the MANIFEST write never sets `new_descriptor_log`. Until then, the files stay quarantined. This is enough for recovery: because `descriptor_log_` was reset, the next real MANIFEST write (for example, a non-atomic flush, a compaction, or the forced write in `DB::Resume()`) starts a new MANIFEST, and its success clears the whole quarantine. - `DBImpl::SetOptions()` sets no background error after its `LogAndApply()`. This part is required, not a cleanup. Since meta#13384, that call does no MANIFEST I/O and is never grouped with other writers, so a non-OK `io_status()` there always belongs to another, earlier batch. The removed block was `if (!versions_->io_status().ok()) { assert(!s.ok()); SetBGError(...); }`. With only the `VersionSet` change, `io_status()` stays non-OK after a successful `SetOptions()`. Then a debug build fails the assert, and a release build sets the old error as a fatal background error, so the next write fails. A backport must include this part. - Two callers do not take the old error in `versions_->io_status()` as their own when they did not fail on MANIFEST I/O: - An atomic flush checks `versions_->io_status()` while it waits for its turn to install. The check stops the wait when the flush that it waits for failed its MANIFEST write. A flush that does not wait for another flush, while the DB is not stopped, ignores the error. With no background error, it installs its result. That install writes a new MANIFEST, and its success clears the old error and the quarantine. With a soft background error, a flush that is not for recovery gets that soft error from the code after the wait and does not install, so it reports its own error. A recovery flush installs, which writes the new MANIFEST. While the DB is stopped, the flush returns the error and does not install, as before this change while io_status() holds the error. The check must stay in that case: when the DB is stopped, the code after the wait checks only the recovery error, which is OK when no recovery runs. This also fixes an upstream case. Upstream, a `SetOptions()` after a failed MANIFEST write resets io_status(). A later atomic flush could then install its result after a fatal background error stopped the DB. With this change, io_status() keeps the error until a new MANIFEST is written, so that flush returns the error instead. Without any old MANIFEST error, an atomic flush that is already running can still install after a fatal background error. That is older than this change and not about the MANIFEST, so this change leaves it. - `CompactionJob::Install()` takes `versions_->io_status()` as the job's I/O status only when the commit itself failed. A compaction that failed before its commit, for example when it could not write an output file, keeps its own I/O status. - The SstFileManager recovers from a soft `NoSpace` error with a direct clear in `ErrorHandler::RecoverFromBGError()`, which does not go through `DBImpl::ResumeImpl()` and writes no MANIFEST. When the quarantine is not empty or `versions_->io_status()` is not OK, the direct clear first writes a new MANIFEST with an empty edit, as `DBImpl::ResumeImpl()` does. If that write fails, the direct clear sets the error of that write as a background error with reason `kManifestWrite`, does not clear, and returns. The SstFileManager then retries, unless that error is fatal. If shutdown has started, the direct clear does not write and returns `ShutdownInProgress`, as `DBImpl::ResumeImpl()` does. Some calls set no background error when their MANIFEST write fails, for example a failed `PromoteL0()` or `DropColumnFamily()`, so a later soft error can find files in the quarantine. Without this part, a debug build then fails the assert in `ErrorHandler::ClearBGError()` that the quarantine is empty, and a release build clears the error while the old MANIFEST may still hold the failed edit. Upstream already fails the assert after a failed `PromoteL0()` followed by a soft error, and this change fixes that case too. With the rest of this change, a `SetOptions()` between them keeps the quarantine, so without this part the assert would fire in that case as well. This change closes both problems above: the `SetOptions()` trigger, the "failure before MANIFEST I/O" trigger, and the second failed MANIFEST write, with or without a `SetOptions()` in between. The same rule also covers a running compaction whose commit fails at the CURRENT step after a flush's MANIFEST write failed (derived from the code, not tested separately). Consequences: - Quarantined files can stay longer, until the next new MANIFEST or a reopen. This only delays their deletion. - The automatic clear of a soft `NoSpace` error by the SstFileManager can take longer after an older failed MANIFEST write. It writes a new MANIFEST first, and it keeps the error if that write fails. This is needed because the older failed write may still be in the old MANIFEST, and clearing the error without a new MANIFEST would forget that. If that MANIFEST write fails, its error becomes a hard error, so writes stop, and the later retry runs the full recovery of `DB::Resume()`, which flushes all column families. If the failed write has a non-retryable `IOError`, the error is fatal, and only a reopen recovers. - `DB::Resume()` writes a new MANIFEST when `versions_->io_status()` is an `IOError`. Without this change, a `SetOptions()` before the `Resume()` made it skip that write. With this change the write happens, and it clears the quarantine. - `versions_->io_status()` keeps an old MANIFEST write error until a new MANIFEST is written, and a `SetOptions()` call does not reset it. Several callers read it after their own failure and report it as their own error. This misattribution is older than this change: it already happened between a failed MANIFEST write and the next call with an OK MANIFEST I/O status. The worst case is an old non-retryable error that some call left without setting a background error. `DropColumnFamily()`, `CreateColumnFamily()`, `DeleteFilesInRanges()`, `PromoteL0()`, `IncreaseFullHistoryTsLow()`, the level refit of `CompactRange()`, and the file number reservation before an ingestion are such calls. A caller that takes that old error can set it as a fatal background error, and only a reopen recovers from that. This change fixes the two cases above (an atomic flush that does not wait, and a compaction that fails before its commit). Known cases that stay, all older than this change: - An atomic flush that waits for another flush still returns the old error. This needs two atomic flushes at the same time. - A compaction whose commit fails before MANIFEST I/O, for example with `Corruption` from `LoadTableHandlers()`, reports the old `IOError` with reason `kManifestWrite` instead of its own `Corruption`. The outputs of that compaction were never committed. - A compaction or flush that fails for its own reason picks the reason `kManifestWrite` or `kManifestWriteNoWAL` when the old error exists. For a retryable error of a compaction, this makes it a hard error instead of a soft error. Automatic recovery or `DB::Resume()` then writes a new MANIFEST and recovers, unless an atomic flush is running at that time. The hard error stops the DB, so that atomic flush returns the old error. When the old error is not retryable, this makes it a fatal error, and neither automatic recovery nor `DB::Resume()` recovers. Upstream recovers in this case when a `SetOptions()` ran after the old error, and goes fatal already at the compaction when none ran. The worse case is with `paranoid_checks=false`. By the severity table, a non-retryable `IOError` is no error for `kCompaction` and `kFlushNoWAL`, but a fatal error for `kManifestWrite` and `kManifestWriteNoWAL`. A `NoSpace` error is no error for `kCompaction`, but a hard error for `kManifestWrite`. - Other calls that write the MANIFEST, such as `ApplyWALToManifest()` and the record of the OPTIONS file number in `WriteOptionsFile()`, set `versions_->io_status()` as a background error when their `LogAndApply()` fails, also when it failed before MANIFEST I/O. Three compaction paths in `DBImpl::BackgroundCompaction()` read `versions_->io_status()` after their `LogAndApply()` also when it succeeded: the FIFO deletion compaction, the FIFO trivial copy (a temperature change), and the trivial move. A complete fix tracks the MANIFEST I/O status of each batch, and lets a caller report `versions_->io_status()` only when its own commit failed on MANIFEST I/O. That touches `LogAndApply()`, flush, compaction and the DB callers above, and is planned as a follow-up. - An ingestion whose MANIFEST write fails deletes its ingested file in the rollback, even when the file is quarantined. This gap is older than this change, and it is on a different code path. A follow-up will fix it. - No API, option or format change. Differential Revision: D134907305
Summary: Pull Request resolved: meta#15818 Background: when a MANIFEST write fails, RocksDB cannot know whether the failed edit is in the MANIFEST that CURRENT names. For example, the record can be written and synced before the error is reported. Or a batch can create a new MANIFEST, rename CURRENT to it, and then get an error from the directory sync. So `VersionSet::ProcessManifestWrites()` puts the new files of the failed batch (the edits of one `ProcessManifestWrites()` call) in the error handler quarantine, and the purge of obsolete files does not delete them. When a MANIFEST write fails, `ProcessManifestWrites()` also resets `descriptor_log_`, and a batch without a `descriptor_log_` sets `new_descriptor_log`. So the next batch that writes the MANIFEST always starts a new MANIFEST. Once that new MANIFEST is installed, the old one is no longer used, and the quarantine is cleared. `VersionSet::io_status_` keeps the MANIFEST write error until then. How the code got into this state: - Since meta#7034, `ProcessManifestWrites()` resets `io_status_` after any call whose MANIFEST I/O status is OK. It records only the first error. - meta#12030 added the quarantine. It added the files of a failed batch only inside the "first error" block, and it cleared the quarantine together with the reset of `io_status_`. At that time `SetOptions()` still wrote to the MANIFEST. - meta#13384 made `SetOptions()` skip the MANIFEST write. So every `SetOptions()` became a call with an OK MANIFEST I/O status that writes no new MANIFEST. The problems: 1. Two kinds of calls have an OK MANIFEST I/O status but write no new MANIFEST: - `DBImpl::SetOptions()` installs a new Version through `LogAndApply()` with an edit that writes nothing to the MANIFEST (`skip_manifest_write`). - A batch can fail before any MANIFEST I/O, for example when `LoadTableHandlers()` fails with `paranoid_checks`. A compaction that is already running when a flush's MANIFEST write fails still commits, because the background error is checked only when the compaction starts. After such a call, the old MANIFEST can still hold the failed edit, but its files are no longer quarantined. 2. A second failed MANIFEST write, while `io_status_` already holds an earlier error, does not quarantine its own files. This is a problem when the second batch creates a new MANIFEST and renames CURRENT to it, and then reports an error. CURRENT names the new MANIFEST, and it lists the new files. In both cases, the purge after the failed job, or the purge in a clean `Close()`, deletes the files. When CURRENT names a MANIFEST that lists a deleted file, every `DB::Open()` fails with "Corruption: IO error: No such file or directory ... .sst The file MANIFEST-... may be corrupted". A db_stress crash test hit problem 1: a flush's MANIFEST write got an injected write error, a `SetOptions()` ran about 1.6 ms later and cleared the quarantine, the flush job deleted its SST, and the harness killed the process. This change cannot repair a DB that already lost such an SST. Example of problem 2, reproduced by the new unit test: ``` 1. DropColumnFamily(): the MANIFEST record is synced, then the write reports an error. io_status_ = that error. No background error is set. 2. (optional) SetOptions() succeeds. 3. Flush: starts a new MANIFEST that lists the flushed SST, renames CURRENT to it, then SetCurrentFile() reports an error. io_status_ is already bad, so the SST is not quarantined. 4. The flush job's purge deletes the SST. The next DB::Open() fails. ``` Without step 2, upstream fails this way. With step 2, upstream passes, because `SetOptions()` resets `io_status_` before the flush, so the flush failure is the "first" error. Fixing problem 1 alone would make the run with step 2 fail too, because `SetOptions()` would no longer reset `io_status_`. So this change fixes both problems. New behavior: - Every MANIFEST write failure adds the files of its batch to the quarantine. `io_status_` keeps the first error. A file number is added only once, because automatic recovery can retry many times, each retry adds the old MANIFEST again, and each `FindObsoleteFiles()` copies the list. The check uses a hash set of the numbers already in the list, so its cost stays linear in the number of files. A failed batch can have many files, for example a large ingestion or a refit of a whole level, and the check runs under the DB mutex. - `io_status_` is reset and the quarantine is cleared only when the call wrote and installed a new MANIFEST (s is OK and `new_descriptor_log` is set). A batch that skips the MANIFEST write never sets `new_descriptor_log`. Until then, the files stay quarantined. This is enough for recovery: because `descriptor_log_` was reset, the next real MANIFEST write (for example, a non-atomic flush, a compaction, or the forced write in `DB::Resume()`) starts a new MANIFEST, and its success clears the whole quarantine. - `DBImpl::SetOptions()` sets no background error after its `LogAndApply()`. This part is required, not a cleanup. Since meta#13384, that call does no MANIFEST I/O and is never grouped with other writers, so a non-OK `io_status()` there always belongs to another, earlier batch. The removed block was `if (!versions_->io_status().ok()) { assert(!s.ok()); SetBGError(...); }`. With only the `VersionSet` change, `io_status()` stays non-OK after a successful `SetOptions()`. Then a debug build fails the assert, and a release build sets the old error as a fatal background error, so the next write fails. A backport must include this part. - Two callers do not take the old error in `versions_->io_status()` as their own when they did not fail on MANIFEST I/O: - An atomic flush checks `versions_->io_status()` while it waits for its turn to install. The check stops the wait when the flush that it waits for failed its MANIFEST write. A flush that does not wait for another flush, while the DB is not stopped, ignores the error. With no background error, it installs its result. That install writes a new MANIFEST, and its success clears the old error and the quarantine. With a soft background error, a flush that is not for recovery gets that soft error from the code after the wait and does not install, so it reports its own error. A recovery flush installs, which writes the new MANIFEST. While the DB is stopped, the flush returns the error and does not install, as before this change while io_status() holds the error. The check must stay in that case: when the DB is stopped, the code after the wait checks only the recovery error, which is OK when no recovery runs. This also fixes an upstream case. Upstream, a `SetOptions()` after a failed MANIFEST write resets io_status(). A later atomic flush could then install its result after a fatal background error stopped the DB. With this change, io_status() keeps the error until a new MANIFEST is written, so that flush returns the error instead. Without any old MANIFEST error, an atomic flush that is already running can still install after a fatal background error. That is older than this change and not about the MANIFEST, so this change leaves it. - `CompactionJob::Install()` takes `versions_->io_status()` as the job's I/O status only when the commit itself failed. A compaction that failed before its commit, for example when it could not write an output file, keeps its own I/O status. - The SstFileManager recovers from a soft `NoSpace` error with a direct clear in `ErrorHandler::RecoverFromBGError()`, which does not go through `DBImpl::ResumeImpl()` and writes no MANIFEST. When the quarantine is not empty or `versions_->io_status()` is not OK, the direct clear first writes a new MANIFEST with an empty edit, as `DBImpl::ResumeImpl()` does. If that write fails, the direct clear sets the error of that write as a background error with reason `kManifestWrite`, does not clear, and returns. The SstFileManager then retries, unless that error is fatal. If shutdown has started, the direct clear does not write and returns `ShutdownInProgress`, as `DBImpl::ResumeImpl()` does. Some calls set no background error when their MANIFEST write fails, for example a failed `PromoteL0()` or `DropColumnFamily()`, so a later soft error can find files in the quarantine. Without this part, a debug build then fails the assert in `ErrorHandler::ClearBGError()` that the quarantine is empty, and a release build clears the error while the old MANIFEST may still hold the failed edit. Upstream already fails the assert after a failed `PromoteL0()` followed by a soft error, and this change fixes that case too. With the rest of this change, a `SetOptions()` between them keeps the quarantine, so without this part the assert would fire in that case as well. This change closes both problems above: the `SetOptions()` trigger, the "failure before MANIFEST I/O" trigger, and the second failed MANIFEST write, with or without a `SetOptions()` in between. The same rule also covers a running compaction whose commit fails at the CURRENT step after a flush's MANIFEST write failed (derived from the code, not tested separately). Consequences: - Quarantined files can stay longer, until the next new MANIFEST or a reopen. This only delays their deletion. - The automatic clear of a soft `NoSpace` error by the SstFileManager can take longer after an older failed MANIFEST write. It writes a new MANIFEST first, and it keeps the error if that write fails. This is needed because the older failed write may still be in the old MANIFEST, and clearing the error without a new MANIFEST would forget that. If that MANIFEST write fails, its error becomes a hard error, so writes stop, and the later retry runs the full recovery of `DB::Resume()`, which flushes all column families. If the failed write has a non-retryable `IOError`, the error is fatal, and only a reopen recovers. - `DB::Resume()` writes a new MANIFEST when `versions_->io_status()` is an `IOError`. Without this change, a `SetOptions()` before the `Resume()` made it skip that write. With this change the write happens, and it clears the quarantine. - `versions_->io_status()` keeps an old MANIFEST write error until a new MANIFEST is written, and a `SetOptions()` call does not reset it. Several callers read it after their own failure and report it as their own error. This misattribution is older than this change: it already happened between a failed MANIFEST write and the next call with an OK MANIFEST I/O status. The worst case is an old non-retryable error that some call left without setting a background error. `DropColumnFamily()`, `CreateColumnFamily()`, `DeleteFilesInRanges()`, `PromoteL0()`, `IncreaseFullHistoryTsLow()`, the level refit of `CompactRange()`, and the file number reservation before an ingestion are such calls. A caller that takes that old error can set it as a fatal background error, and only a reopen recovers from that. This change fixes the two cases above (an atomic flush that does not wait, and a compaction that fails before its commit). Known cases that stay, all older than this change: - An atomic flush that waits for another flush still returns the old error. This needs two atomic flushes at the same time. - A compaction whose commit fails before MANIFEST I/O, for example with `Corruption` from `LoadTableHandlers()`, reports the old `IOError` with reason `kManifestWrite` instead of its own `Corruption`. The outputs of that compaction were never committed. - A compaction or flush that fails for its own reason picks the reason `kManifestWrite` or `kManifestWriteNoWAL` when the old error exists. For a retryable error of a compaction, this makes it a hard error instead of a soft error. Automatic recovery or `DB::Resume()` then writes a new MANIFEST and recovers, unless an atomic flush is running at that time. The hard error stops the DB, so that atomic flush returns the old error. When the old error is not retryable, this makes it a fatal error, and neither automatic recovery nor `DB::Resume()` recovers. Upstream recovers in this case when a `SetOptions()` ran after the old error, and goes fatal already at the compaction when none ran. The worse case is with `paranoid_checks=false`. By the severity table, a non-retryable `IOError` is no error for `kCompaction` and `kFlushNoWAL`, but a fatal error for `kManifestWrite` and `kManifestWriteNoWAL`. A `NoSpace` error is no error for `kCompaction`, but a hard error for `kManifestWrite`. - Other calls that write the MANIFEST, such as `ApplyWALToManifest()` and the record of the OPTIONS file number in `WriteOptionsFile()`, set `versions_->io_status()` as a background error when their `LogAndApply()` fails, also when it failed before MANIFEST I/O. Three compaction paths in `DBImpl::BackgroundCompaction()` read `versions_->io_status()` after their `LogAndApply()` also when it succeeded: the FIFO deletion compaction, the FIFO trivial copy (a temperature change), and the trivial move. A complete fix tracks the MANIFEST I/O status of each batch, and lets a caller report `versions_->io_status()` only when its own commit failed on MANIFEST I/O. That touches `LogAndApply()`, flush, compaction and the DB callers above, and is planned as a follow-up. - An ingestion whose MANIFEST write fails deletes its ingested file in the rollback, even when the file is quarantined. This gap is older than this change, and it is on a different code path. A follow-up will fix it. - No API, option or format change. Differential Revision: D134907305
Summary: Pull Request resolved: meta#15818 Background: when a MANIFEST write fails, RocksDB cannot know whether the failed edit is in the MANIFEST that CURRENT names. For example, the record can be written and synced before the error is reported. Or a batch can create a new MANIFEST, rename CURRENT to it, and then get an error from the directory sync. So `VersionSet::ProcessManifestWrites()` puts the new files of the failed batch (the edits of one `ProcessManifestWrites()` call) in the error handler quarantine, and the purge of obsolete files does not delete them. When a MANIFEST write fails, `ProcessManifestWrites()` also resets `descriptor_log_`, and a batch without a `descriptor_log_` sets `new_descriptor_log`. So the next batch that writes the MANIFEST always starts a new MANIFEST. Once that new MANIFEST is installed, the old one is no longer used, and the quarantine is cleared. `VersionSet::io_status_` keeps the MANIFEST write error until then. How the code got into this state: - Since meta#7034, `ProcessManifestWrites()` resets `io_status_` after any call whose MANIFEST I/O status is OK. It records only the first error. - meta#12030 added the quarantine. It added the files of a failed batch only inside the "first error" block, and it cleared the quarantine together with the reset of `io_status_`. At that time `SetOptions()` still wrote to the MANIFEST. - meta#13384 made `SetOptions()` skip the MANIFEST write. So every `SetOptions()` became a call with an OK MANIFEST I/O status that writes no new MANIFEST. The problems: 1. Two kinds of calls have an OK MANIFEST I/O status but write no new MANIFEST: - `DBImpl::SetOptions()` installs a new Version through `LogAndApply()` with an edit that writes nothing to the MANIFEST (`skip_manifest_write`). - A batch can fail before any MANIFEST I/O, for example when `LoadTableHandlers()` fails with `paranoid_checks`. A compaction that is already running when a flush's MANIFEST write fails still commits, because the background error is checked only when the compaction starts. After such a call, the old MANIFEST can still hold the failed edit, but its files are no longer quarantined. 2. A second failed MANIFEST write, while `io_status_` already holds an earlier error, does not quarantine its own files. This is a problem when the second batch creates a new MANIFEST and renames CURRENT to it, and then reports an error. CURRENT names the new MANIFEST, and it lists the new files. In both cases, the purge after the failed job, or the purge in a clean `Close()`, deletes the files. When CURRENT names a MANIFEST that lists a deleted file, every `DB::Open()` fails with "Corruption: IO error: No such file or directory ... .sst The file MANIFEST-... may be corrupted". A db_stress crash test hit problem 1: a flush's MANIFEST write got an injected write error, a `SetOptions()` ran about 1.6 ms later and cleared the quarantine, the flush job deleted its SST, and the harness killed the process. This change cannot repair a DB that already lost such an SST. Example of problem 2, reproduced by the new unit test: ``` 1. DropColumnFamily(): the MANIFEST record is synced, then the write reports an error. io_status_ = that error. No background error is set. 2. (optional) SetOptions() succeeds. 3. Flush: starts a new MANIFEST that lists the flushed SST, renames CURRENT to it, then SetCurrentFile() reports an error. io_status_ is already bad, so the SST is not quarantined. 4. The flush job's purge deletes the SST. The next DB::Open() fails. ``` Without step 2, upstream fails this way. With step 2, upstream passes, because `SetOptions()` resets `io_status_` before the flush, so the flush failure is the "first" error. Fixing problem 1 alone would make the run with step 2 fail too, because `SetOptions()` would no longer reset `io_status_`. So this change fixes both problems. New behavior: - Every MANIFEST write failure adds the files of its batch to the quarantine. `io_status_` keeps the first error. A file number is added only once, because automatic recovery can retry many times, each retry adds the old MANIFEST again, and each `FindObsoleteFiles()` copies the list. The check uses a hash set of the numbers already in the list, so its cost stays linear in the number of files. A failed batch can have many files, for example a large ingestion or a refit of a whole level, and the check runs under the DB mutex. - `io_status_` is reset and the quarantine is cleared only when the call wrote and installed a new MANIFEST (s is OK and `new_descriptor_log` is set). A batch that skips the MANIFEST write never sets `new_descriptor_log`. Until then, the files stay quarantined. This is enough for recovery: because `descriptor_log_` was reset, the next real MANIFEST write (for example, a non-atomic flush, a compaction, or the forced write in `DB::Resume()`) starts a new MANIFEST, and its success clears the whole quarantine. - `DBImpl::SetOptions()` sets no background error after its `LogAndApply()`. This part is required, not a cleanup. Since meta#13384, that call does no MANIFEST I/O and is never grouped with other writers, so a non-OK `io_status()` there always belongs to another, earlier batch. The removed block was `if (!versions_->io_status().ok()) { assert(!s.ok()); SetBGError(...); }`. With only the `VersionSet` change, `io_status()` stays non-OK after a successful `SetOptions()`. Then a debug build fails the assert, and a release build sets the old error as a fatal background error, so the next write fails. A backport must include this part. - Two callers do not take the old error in `versions_->io_status()` as their own when they did not fail on MANIFEST I/O: - An atomic flush checks `versions_->io_status()` while it waits for its turn to install. The check stops the wait when the flush that it waits for failed its MANIFEST write. A flush that does not wait for another flush, while the DB is not stopped, ignores the error. With no background error, it installs its result. That install writes a new MANIFEST, and its success clears the old error and the quarantine. With a soft background error, a flush that is not for recovery gets that soft error from the code after the wait and does not install, so it reports its own error. A recovery flush installs, which writes the new MANIFEST. While the DB is stopped, the flush returns the error and does not install, as before this change while io_status() holds the error. The check must stay in that case: when the DB is stopped, the code after the wait checks only the recovery error, which is OK when no recovery runs. This also fixes an upstream case. Upstream, a `SetOptions()` after a failed MANIFEST write resets io_status(). A later atomic flush could then install its result after a fatal background error stopped the DB. With this change, io_status() keeps the error until a new MANIFEST is written, so that flush returns the error instead. Without any old MANIFEST error, an atomic flush that is already running can still install after a fatal background error. That is older than this change and not about the MANIFEST, so this change leaves it. - `CompactionJob::Install()` takes `versions_->io_status()` as the job's I/O status only when the commit itself failed. A compaction that failed before its commit, for example when it could not write an output file, keeps its own I/O status. - The SstFileManager recovers from a soft `NoSpace` error with a direct clear in `ErrorHandler::RecoverFromBGError()`, which does not go through `DBImpl::ResumeImpl()` and writes no MANIFEST. When the quarantine is not empty or `versions_->io_status()` is not OK, the direct clear first writes a new MANIFEST with an empty edit, as `DBImpl::ResumeImpl()` does. If that write fails, the direct clear sets the error of that write as a background error with reason `kManifestWrite`, does not clear, and returns. The SstFileManager then retries, unless that error is fatal. If shutdown has started, the direct clear does not write and returns `ShutdownInProgress`, as `DBImpl::ResumeImpl()` does. Some calls set no background error when their MANIFEST write fails, for example a failed `PromoteL0()` or `DropColumnFamily()`, so a later soft error can find files in the quarantine. Without this part, a debug build then fails the assert in `ErrorHandler::ClearBGError()` that the quarantine is empty, and a release build clears the error while the old MANIFEST may still hold the failed edit. Upstream already fails the assert after a failed `PromoteL0()` followed by a soft error, and this change fixes that case too. With the rest of this change, a `SetOptions()` between them keeps the quarantine, so without this part the assert would fire in that case as well. This change closes both problems above: the `SetOptions()` trigger, the "failure before MANIFEST I/O" trigger, and the second failed MANIFEST write, with or without a `SetOptions()` in between. The same rule also covers a running compaction whose commit fails at the CURRENT step after a flush's MANIFEST write failed (derived from the code, not tested separately). Consequences: - Quarantined files can stay longer, until the next new MANIFEST or a reopen. This only delays their deletion. - The automatic clear of a soft `NoSpace` error by the SstFileManager can take longer after an older failed MANIFEST write. It writes a new MANIFEST first, and it keeps the error if that write fails. This is needed because the older failed write may still be in the old MANIFEST, and clearing the error without a new MANIFEST would forget that. If that MANIFEST write fails, its error becomes a hard error, so writes stop, and the later retry runs the full recovery of `DB::Resume()`, which flushes all column families. If the failed write has a non-retryable `IOError`, the error is fatal, and only a reopen recovers. - `DB::Resume()` writes a new MANIFEST when `versions_->io_status()` is an `IOError`. Without this change, a `SetOptions()` before the `Resume()` made it skip that write. With this change the write happens, and it clears the quarantine. - `versions_->io_status()` keeps an old MANIFEST write error until a new MANIFEST is written, and a `SetOptions()` call does not reset it. Several callers read it after their own failure and report it as their own error. This misattribution is older than this change: it already happened between a failed MANIFEST write and the next call with an OK MANIFEST I/O status. The worst case is an old non-retryable error that some call left without setting a background error. `DropColumnFamily()`, `CreateColumnFamily()`, `DeleteFilesInRanges()`, `PromoteL0()`, `IncreaseFullHistoryTsLow()`, the level refit of `CompactRange()`, and the file number reservation before an ingestion are such calls. A caller that takes that old error can set it as a fatal background error, and only a reopen recovers from that. This change fixes the two cases above (an atomic flush that does not wait, and a compaction that fails before its commit). Known cases that stay, all older than this change: - An atomic flush that waits for another flush still returns the old error. This needs two atomic flushes at the same time. - A compaction whose commit fails before MANIFEST I/O, for example with `Corruption` from `LoadTableHandlers()`, reports the old `IOError` with reason `kManifestWrite` instead of its own `Corruption`. The outputs of that compaction were never committed. - A compaction or flush that fails for its own reason picks the reason `kManifestWrite` or `kManifestWriteNoWAL` when the old error exists. For a retryable error of a compaction, this makes it a hard error instead of a soft error. Automatic recovery or `DB::Resume()` then writes a new MANIFEST and recovers, unless an atomic flush is running at that time. The hard error stops the DB, so that atomic flush returns the old error. When the old error is not retryable, this makes it a fatal error, and neither automatic recovery nor `DB::Resume()` recovers. Upstream recovers in this case when a `SetOptions()` ran after the old error, and goes fatal already at the compaction when none ran. The worse case is with `paranoid_checks=false`. By the severity table, a non-retryable `IOError` is no error for `kCompaction` and `kFlushNoWAL`, but a fatal error for `kManifestWrite` and `kManifestWriteNoWAL`. A `NoSpace` error is no error for `kCompaction`, but a hard error for `kManifestWrite`. - Other calls that write the MANIFEST, such as `ApplyWALToManifest()` and the record of the OPTIONS file number in `WriteOptionsFile()`, set `versions_->io_status()` as a background error when their `LogAndApply()` fails, also when it failed before MANIFEST I/O. Three compaction paths in `DBImpl::BackgroundCompaction()` read `versions_->io_status()` after their `LogAndApply()` also when it succeeded: the FIFO deletion compaction, the FIFO trivial copy (a temperature change), and the trivial move. A complete fix tracks the MANIFEST I/O status of each batch, and lets a caller report `versions_->io_status()` only when its own commit failed on MANIFEST I/O. That touches `LogAndApply()`, flush, compaction and the DB callers above, and is planned as a follow-up. - An ingestion whose MANIFEST write fails deletes its ingested file in the rollback, even when the file is quarantined. This gap is older than this change, and it is on a different code path. A follow-up will fix it. - No API, option or format change. Differential Revision: D134907305
Summary: Pull Request resolved: meta#15818 Background: when a MANIFEST write fails, RocksDB cannot know whether the failed edit is in the MANIFEST that CURRENT names. For example, the record can be written and synced before the error is reported. Or a batch can create a new MANIFEST, rename CURRENT to it, and then get an error from the directory sync. So `VersionSet::ProcessManifestWrites()` puts the new files of the failed batch (the edits of one `ProcessManifestWrites()` call) in the error handler quarantine, and the purge of obsolete files does not delete them. When a MANIFEST write fails, `ProcessManifestWrites()` also resets `descriptor_log_`, and a batch without a `descriptor_log_` sets `new_descriptor_log`. So the next batch that writes the MANIFEST always starts a new MANIFEST. Once that new MANIFEST is installed, the old one is no longer used, and the quarantine is cleared. `VersionSet::io_status_` keeps the MANIFEST write error until then. How the code got into this state: - Since meta#7034, `ProcessManifestWrites()` resets `io_status_` after any call whose MANIFEST I/O status is OK. It records only the first error. - meta#12030 added the quarantine. It added the files of a failed batch only inside the "first error" block, and it cleared the quarantine together with the reset of `io_status_`. At that time `SetOptions()` still wrote to the MANIFEST. - meta#13384 made `SetOptions()` skip the MANIFEST write. So every `SetOptions()` became a call with an OK MANIFEST I/O status that writes no new MANIFEST. The problems: 1. Two kinds of calls have an OK MANIFEST I/O status but write no new MANIFEST: - `DBImpl::SetOptions()` installs a new Version through `LogAndApply()` with an edit that writes nothing to the MANIFEST (`skip_manifest_write`). - A batch can fail before any MANIFEST I/O, for example when `LoadTableHandlers()` fails with `paranoid_checks`. A compaction that is already running when a flush's MANIFEST write fails still commits, because the background error is checked only when the compaction starts. After such a call, the old MANIFEST can still hold the failed edit, but its files are no longer quarantined. 2. A second failed MANIFEST write, while `io_status_` already holds an earlier error, does not quarantine its own files. This is a problem when the second batch creates a new MANIFEST and renames CURRENT to it, and then reports an error. CURRENT names the new MANIFEST, and it lists the new files. In both cases, the purge after the failed job, or the purge in a clean `Close()`, deletes the files. When CURRENT names a MANIFEST that lists a deleted file, every `DB::Open()` fails with "Corruption: IO error: No such file or directory ... .sst The file MANIFEST-... may be corrupted". A db_stress crash test hit problem 1: a flush's MANIFEST write got an injected write error, a `SetOptions()` ran about 1.6 ms later and cleared the quarantine, the flush job deleted its SST, and the harness killed the process. This change cannot repair a DB that already lost such an SST. Example of problem 2, reproduced by the new unit test: ``` 1. DropColumnFamily(): the MANIFEST record is synced, then the write reports an error. io_status_ = that error. No background error is set. 2. (optional) SetOptions() succeeds. 3. Flush: starts a new MANIFEST that lists the flushed SST, renames CURRENT to it, then SetCurrentFile() reports an error. io_status_ is already bad, so the SST is not quarantined. 4. The flush job's purge deletes the SST. The next DB::Open() fails. ``` Without step 2, upstream fails this way. With step 2, upstream passes, because `SetOptions()` resets `io_status_` before the flush, so the flush failure is the "first" error. Fixing problem 1 alone would make the run with step 2 fail too, because `SetOptions()` would no longer reset `io_status_`. So this change fixes both problems. New behavior: - Every MANIFEST write failure adds the files of its batch to the quarantine. `io_status_` keeps the first error. A file number is added only once, because automatic recovery can retry many times, each retry adds the old MANIFEST again, and each `FindObsoleteFiles()` copies the list. The check uses a hash set of the numbers already in the list, so its cost stays linear in the number of files. A failed batch can have many files, for example a large ingestion or a refit of a whole level, and the check runs under the DB mutex. - `io_status_` is reset and the quarantine is cleared only when the call wrote and installed a new MANIFEST (s is OK and `new_descriptor_log` is set). A batch that skips the MANIFEST write never sets `new_descriptor_log`. Until then, the files stay quarantined. This is enough for recovery: because `descriptor_log_` was reset, the next real MANIFEST write (for example, a non-atomic flush, a compaction, or the forced write in `DB::Resume()`) starts a new MANIFEST, and its success clears the whole quarantine. - `DBImpl::SetOptions()` sets no background error after its `LogAndApply()`. This part is required, not a cleanup. Since meta#13384, that call does no MANIFEST I/O and is never grouped with other writers, so a non-OK `io_status()` there always belongs to another, earlier batch. The removed block was `if (!versions_->io_status().ok()) { assert(!s.ok()); SetBGError(...); }`. With only the `VersionSet` change, `io_status()` stays non-OK after a successful `SetOptions()`. Then a debug build fails the assert, and a release build sets the old error as a fatal background error, so the next write fails. A backport must include this part. - Two callers do not take the old error in `versions_->io_status()` as their own when they did not fail on MANIFEST I/O: - An atomic flush checks `versions_->io_status()` while it waits for its turn to install. The check stops the wait when the flush that it waits for failed its MANIFEST write. A flush that does not wait for another flush, while the DB is not stopped, ignores the error. With no background error, it installs its result. That install writes a new MANIFEST, and its success clears the old error and the quarantine. With a soft background error, a flush that is not for recovery gets that soft error from the code after the wait and does not install, so it reports its own error. A recovery flush installs, which writes the new MANIFEST. While the DB is stopped, the flush returns the error and does not install, as before this change while io_status() holds the error. The check must stay in that case: when the DB is stopped, the code after the wait checks only the recovery error, which is OK when no recovery runs. This also fixes an upstream case. Upstream, a `SetOptions()` after a failed MANIFEST write resets io_status(). A later atomic flush could then install its result after a fatal background error stopped the DB. With this change, io_status() keeps the error until a new MANIFEST is written, so that flush returns the error instead. Without any old MANIFEST error, an atomic flush that is already running can still install after a fatal background error. That is older than this change and not about the MANIFEST, so this change leaves it. - `CompactionJob::Install()` takes `versions_->io_status()` as the job's I/O status only when the commit itself failed. A compaction that failed before its commit, for example when it could not write an output file, keeps its own I/O status. - The SstFileManager recovers from a soft `NoSpace` error with a direct clear in `ErrorHandler::RecoverFromBGError()`, which does not go through `DBImpl::ResumeImpl()` and writes no MANIFEST. When the quarantine is not empty or `versions_->io_status()` is not OK, the direct clear first writes a new MANIFEST with an empty edit, as `DBImpl::ResumeImpl()` does. If that write fails, the direct clear sets the error of that write as a background error with reason `kManifestWrite`, does not clear, and returns. The SstFileManager then retries, unless that error is fatal. If shutdown has started, the direct clear does not write and returns `ShutdownInProgress`, as `DBImpl::ResumeImpl()` does. Some calls set no background error when their MANIFEST write fails, for example a failed `PromoteL0()` or `DropColumnFamily()`, so a later soft error can find files in the quarantine. Without this part, a debug build then fails the assert in `ErrorHandler::ClearBGError()` that the quarantine is empty, and a release build clears the error while the old MANIFEST may still hold the failed edit. Upstream already fails the assert after a failed `PromoteL0()` followed by a soft error, and this change fixes that case too. With the rest of this change, a `SetOptions()` between them keeps the quarantine, so without this part the assert would fire in that case as well. This change closes both problems above: the `SetOptions()` trigger, the "failure before MANIFEST I/O" trigger, and the second failed MANIFEST write, with or without a `SetOptions()` in between. The same rule also covers a running compaction whose commit fails at the CURRENT step after a flush's MANIFEST write failed (derived from the code, not tested separately). Consequences: - Quarantined files can stay longer, until the next new MANIFEST or a reopen. This only delays their deletion. - The automatic clear of a soft `NoSpace` error by the SstFileManager can take longer after an older failed MANIFEST write. It writes a new MANIFEST first, and it keeps the error if that write fails. This is needed because the older failed write may still be in the old MANIFEST, and clearing the error without a new MANIFEST would forget that. If that MANIFEST write fails, its error becomes a hard error, so writes stop, and the later retry runs the full recovery of `DB::Resume()`, which flushes all column families. If the failed write has a non-retryable `IOError`, the error is fatal, and only a reopen recovers. - `DB::Resume()` writes a new MANIFEST when `versions_->io_status()` is an `IOError`. Without this change, a `SetOptions()` before the `Resume()` made it skip that write. With this change the write happens, and it clears the quarantine. - `versions_->io_status()` keeps an old MANIFEST write error until a new MANIFEST is written, and a `SetOptions()` call does not reset it. Several callers read it after their own failure and report it as their own error. This misattribution is older than this change: it already happened between a failed MANIFEST write and the next call with an OK MANIFEST I/O status. The worst case is an old non-retryable error that some call left without setting a background error. `DropColumnFamily()`, `CreateColumnFamily()`, `DeleteFilesInRanges()`, `PromoteL0()`, `IncreaseFullHistoryTsLow()`, the level refit of `CompactRange()`, and the file number reservation before an ingestion are such calls. A caller that takes that old error can set it as a fatal background error, and only a reopen recovers from that. This change fixes the two cases above (an atomic flush that does not wait, and a compaction that fails before its commit). Known cases that stay, all older than this change: - An atomic flush that waits for another flush still returns the old error. This needs two atomic flushes at the same time. - A compaction whose commit fails before MANIFEST I/O, for example with `Corruption` from `LoadTableHandlers()`, reports the old `IOError` with reason `kManifestWrite` instead of its own `Corruption`. The outputs of that compaction were never committed. - A compaction or flush that fails for its own reason picks the reason `kManifestWrite` or `kManifestWriteNoWAL` when the old error exists. For a retryable error of a compaction, this makes it a hard error instead of a soft error. Automatic recovery or `DB::Resume()` then writes a new MANIFEST and recovers, unless an atomic flush is running at that time. The hard error stops the DB, so that atomic flush returns the old error. When the old error is not retryable, this makes it a fatal error, and neither automatic recovery nor `DB::Resume()` recovers. Upstream recovers in this case when a `SetOptions()` ran after the old error, and goes fatal already at the compaction when none ran. The worse case is with `paranoid_checks=false`. By the severity table, a non-retryable `IOError` is no error for `kCompaction` and `kFlushNoWAL`, but a fatal error for `kManifestWrite` and `kManifestWriteNoWAL`. A `NoSpace` error is no error for `kCompaction`, but a hard error for `kManifestWrite`. - Other calls that write the MANIFEST, such as `ApplyWALToManifest()` and the record of the OPTIONS file number in `WriteOptionsFile()`, set `versions_->io_status()` as a background error when their `LogAndApply()` fails, also when it failed before MANIFEST I/O. Three compaction paths in `DBImpl::BackgroundCompaction()` read `versions_->io_status()` after their `LogAndApply()` also when it succeeded: the FIFO deletion compaction, the FIFO trivial copy (a temperature change), and the trivial move. A complete fix tracks the MANIFEST I/O status of each batch, and lets a caller report `versions_->io_status()` only when its own commit failed on MANIFEST I/O. That touches `LogAndApply()`, flush, compaction and the DB callers above, and is planned as a follow-up. - An ingestion whose MANIFEST write fails deletes its ingested file in the rollback, even when the file is quarantined. This gap is older than this change, and it is on a different code path. A follow-up will fix it. - No API, option or format change. Differential Revision: D134907305
Summary: Pull Request resolved: meta#15818 Background: when a MANIFEST write fails, RocksDB cannot know whether the failed edit is in the MANIFEST that CURRENT names. For example, the record can be written and synced before the error is reported. Or a batch can create a new MANIFEST, rename CURRENT to it, and then get an error from the directory sync. So `VersionSet::ProcessManifestWrites()` puts the new files of the failed batch (the edits of one `ProcessManifestWrites()` call) in the error handler quarantine, and the purge of obsolete files does not delete them. When a MANIFEST write fails, `ProcessManifestWrites()` also resets `descriptor_log_`, and a batch without a `descriptor_log_` sets `new_descriptor_log`. So the next batch that writes the MANIFEST always starts a new MANIFEST. Once that new MANIFEST is installed, the old one is no longer used, and the quarantine is cleared. `VersionSet::io_status_` keeps the MANIFEST write error until then. How the code got into this state: - Since meta#7034, `ProcessManifestWrites()` resets `io_status_` after any call whose MANIFEST I/O status is OK. It records only the first error. - meta#12030 added the quarantine. It added the files of a failed batch only inside the "first error" block, and it cleared the quarantine together with the reset of `io_status_`. At that time `SetOptions()` still wrote to the MANIFEST. - meta#13384 made `SetOptions()` skip the MANIFEST write. So every `SetOptions()` became a call with an OK MANIFEST I/O status that writes no new MANIFEST. The problems: 1. Two kinds of calls have an OK MANIFEST I/O status but write no new MANIFEST: - `DBImpl::SetOptions()` installs a new Version through `LogAndApply()` with an edit that writes nothing to the MANIFEST (`skip_manifest_write`). - A batch can fail before any MANIFEST I/O, for example when `LoadTableHandlers()` fails with `paranoid_checks`. A compaction that is already running when a flush's MANIFEST write fails still commits, because the background error is checked only when the compaction starts. After such a call, the old MANIFEST can still hold the failed edit, but its files are no longer quarantined. 2. A second failed MANIFEST write, while `io_status_` already holds an earlier error, does not quarantine its own files. This is a problem when the second batch creates a new MANIFEST and renames CURRENT to it, and then reports an error. CURRENT names the new MANIFEST, and it lists the new files. In both cases, the purge after the failed job, or the purge in a clean `Close()`, deletes the files. When CURRENT names a MANIFEST that lists a deleted file, every `DB::Open()` fails with "Corruption: IO error: No such file or directory ... .sst The file MANIFEST-... may be corrupted". A db_stress crash test hit problem 1: a flush's MANIFEST write got an injected write error, a `SetOptions()` ran about 1.6 ms later and cleared the quarantine, the flush job deleted its SST, and the harness killed the process. This change cannot repair a DB that already lost such an SST. Example of problem 2, reproduced by the new unit test: ``` 1. DropColumnFamily(): the MANIFEST record is synced, then the write reports an error. io_status_ = that error. No background error is set. 2. (optional) SetOptions() succeeds. 3. Flush: starts a new MANIFEST that lists the flushed SST, renames CURRENT to it, then SetCurrentFile() reports an error. io_status_ is already bad, so the SST is not quarantined. 4. The flush job's purge deletes the SST. The next DB::Open() fails. ``` Without step 2, upstream fails this way. With step 2, upstream passes, because `SetOptions()` resets `io_status_` before the flush, so the flush failure is the "first" error. Fixing problem 1 alone would make the run with step 2 fail too, because `SetOptions()` would no longer reset `io_status_`. So this change fixes both problems. New behavior: - Every MANIFEST write failure adds the files of its batch to the quarantine. `io_status_` keeps the first error. A file number is added only once, because automatic recovery can retry many times, each retry adds the old MANIFEST again, and each `FindObsoleteFiles()` copies the list. The check uses a hash set of the numbers already in the list, so its cost stays linear in the number of files. A failed batch can have many files, for example a large ingestion or a refit of a whole level, and the check runs under the DB mutex. - `io_status_` is reset and the quarantine is cleared only when the call wrote and installed a new MANIFEST (s is OK and `new_descriptor_log` is set). A batch that skips the MANIFEST write never sets `new_descriptor_log`. Until then, the files stay quarantined. This is enough for recovery: because `descriptor_log_` was reset, the next real MANIFEST write (for example, a non-atomic flush, a compaction, or the forced write in `DB::Resume()`) starts a new MANIFEST, and its success clears the whole quarantine. - `DBImpl::SetOptions()` sets no background error after its `LogAndApply()`. This part is required, not a cleanup. Since meta#13384, that call does no MANIFEST I/O and is never grouped with other writers, so a non-OK `io_status()` there always belongs to another, earlier batch. The removed block was `if (!versions_->io_status().ok()) { assert(!s.ok()); SetBGError(...); }`. With only the `VersionSet` change, `io_status()` stays non-OK after a successful `SetOptions()`. Then a debug build fails the assert, and a release build sets the old error as a fatal background error, so the next write fails. A backport must include this part. - Two callers do not take the old error in `versions_->io_status()` as their own when they did not fail on MANIFEST I/O: - An atomic flush checks `versions_->io_status()` while it waits for its turn to install. The check stops the wait when the flush that it waits for failed its MANIFEST write. A flush that does not wait for another flush, while the DB is not stopped, ignores the error. With no background error, it installs its result. That install writes a new MANIFEST, and its success clears the old error and the quarantine. With a soft background error, a flush that is not for recovery gets that soft error from the code after the wait and does not install, so it reports its own error. A recovery flush installs, which writes the new MANIFEST. While the DB is stopped, the flush returns the error and does not install, as before this change while io_status() holds the error. The check must stay in that case: when the DB is stopped, the code after the wait checks only the recovery error, which is OK when no recovery runs. This also fixes an upstream case. Upstream, a `SetOptions()` after a failed MANIFEST write resets io_status(). A later atomic flush could then install its result after a fatal background error stopped the DB. With this change, io_status() keeps the error until a new MANIFEST is written, so that flush returns the error instead. Without any old MANIFEST error, an atomic flush that is already running can still install after a fatal background error. That is older than this change and not about the MANIFEST, so this change leaves it. - `CompactionJob::Install()` takes `versions_->io_status()` as the job's I/O status only when the commit itself failed. A compaction that failed before its commit, for example when it could not write an output file, keeps its own I/O status. - The SstFileManager recovers from a soft `NoSpace` error with a direct clear in `ErrorHandler::RecoverFromBGError()`, which does not go through `DBImpl::ResumeImpl()` and writes no MANIFEST. When the quarantine is not empty or `versions_->io_status()` is not OK, the direct clear first writes a new MANIFEST with an empty edit, as `DBImpl::ResumeImpl()` does. If that write fails, the direct clear sets the error of that write as a background error with reason `kManifestWrite`, does not clear, and returns. The SstFileManager then retries, unless that error is fatal. If shutdown has started, the direct clear does not write and returns `ShutdownInProgress`, as `DBImpl::ResumeImpl()` does. Some calls set no background error when their MANIFEST write fails, for example a failed `PromoteL0()` or `DropColumnFamily()`, so a later soft error can find files in the quarantine. Without this part, a debug build then fails the assert in `ErrorHandler::ClearBGError()` that the quarantine is empty, and a release build clears the error while the old MANIFEST may still hold the failed edit. Upstream already fails the assert after a failed `PromoteL0()` followed by a soft error, and this change fixes that case too. With the rest of this change, a `SetOptions()` between them keeps the quarantine, so without this part the assert would fire in that case as well. This change closes both problems above: the `SetOptions()` trigger, the "failure before MANIFEST I/O" trigger, and the second failed MANIFEST write, with or without a `SetOptions()` in between. The same rule also covers a running compaction whose commit fails at the CURRENT step after a flush's MANIFEST write failed (derived from the code, not tested separately). Consequences: - Quarantined files can stay longer, until the next new MANIFEST or a reopen. This only delays their deletion. - The automatic clear of a soft `NoSpace` error by the SstFileManager can take longer after an older failed MANIFEST write. It writes a new MANIFEST first, and it keeps the error if that write fails. This is needed because the older failed write may still be in the old MANIFEST, and clearing the error without a new MANIFEST would forget that. If that MANIFEST write fails, its error becomes a hard error, so writes stop, and the later retry runs the full recovery of `DB::Resume()`, which flushes all column families. If the failed write has a non-retryable `IOError`, the error is fatal, and only a reopen recovers. - `DB::Resume()` writes a new MANIFEST when `versions_->io_status()` is an `IOError`. Without this change, a `SetOptions()` before the `Resume()` made it skip that write. With this change the write happens, and it clears the quarantine. - `versions_->io_status()` keeps an old MANIFEST write error until a new MANIFEST is written, and a `SetOptions()` call does not reset it. Several callers read it after their own failure and report it as their own error. This misattribution is older than this change: it already happened between a failed MANIFEST write and the next call with an OK MANIFEST I/O status. The worst case is an old non-retryable error that some call left without setting a background error. `DropColumnFamily()`, `CreateColumnFamily()`, `DeleteFilesInRanges()`, `PromoteL0()`, `IncreaseFullHistoryTsLow()`, the level refit of `CompactRange()`, and the file number reservation before an ingestion are such calls. A caller that takes that old error can set it as a fatal background error, and only a reopen recovers from that. This change fixes the two cases above (an atomic flush that does not wait, and a compaction that fails before its commit). Known cases that stay, all older than this change: - An atomic flush that waits for another flush still returns the old error. This needs two atomic flushes at the same time. - A compaction whose commit fails before MANIFEST I/O, for example with `Corruption` from `LoadTableHandlers()`, reports the old `IOError` with reason `kManifestWrite` instead of its own `Corruption`. The outputs of that compaction were never committed. - A compaction or flush that fails for its own reason picks the reason `kManifestWrite` or `kManifestWriteNoWAL` when the old error exists. For a retryable error of a compaction, this makes it a hard error instead of a soft error. Automatic recovery or `DB::Resume()` then writes a new MANIFEST and recovers, unless an atomic flush is running at that time. The hard error stops the DB, so that atomic flush returns the old error. When the old error is not retryable, this makes it a fatal error, and neither automatic recovery nor `DB::Resume()` recovers. Upstream recovers in this case when a `SetOptions()` ran after the old error, and goes fatal already at the compaction when none ran. The worse case is with `paranoid_checks=false`. By the severity table, a non-retryable `IOError` is no error for `kCompaction` and `kFlushNoWAL`, but a fatal error for `kManifestWrite` and `kManifestWriteNoWAL`. A `NoSpace` error is no error for `kCompaction`, but a hard error for `kManifestWrite`. - Other calls that write the MANIFEST, such as `ApplyWALToManifest()` and the record of the OPTIONS file number in `WriteOptionsFile()`, set `versions_->io_status()` as a background error when their `LogAndApply()` fails, also when it failed before MANIFEST I/O. Three compaction paths in `DBImpl::BackgroundCompaction()` read `versions_->io_status()` after their `LogAndApply()` also when it succeeded: the FIFO deletion compaction, the FIFO trivial copy (a temperature change), and the trivial move. A complete fix tracks the MANIFEST I/O status of each batch, and lets a caller report `versions_->io_status()` only when its own commit failed on MANIFEST I/O. That touches `LogAndApply()`, flush, compaction and the DB callers above, and is planned as a follow-up. - An ingestion whose MANIFEST write fails deletes its ingested file in the rollback, even when the file is quarantined. This gap is older than this change, and it is on a different code path. A follow-up will fix it. - No API, option or format change. Differential Revision: D134907305
Summary: Background: when a MANIFEST write fails, RocksDB cannot know whether the failed edit is in the MANIFEST that CURRENT names. For example, the record can be written and synced before the error is reported. Or a batch can create a new MANIFEST, rename CURRENT to it, and then get an error from the directory sync. So `VersionSet::ProcessManifestWrites()` puts the new files of the failed batch (the edits of one `ProcessManifestWrites()` call) in the error handler quarantine, and the purge of obsolete files does not delete them. When a MANIFEST write fails, `ProcessManifestWrites()` also resets `descriptor_log_`, and a batch without a `descriptor_log_` sets `new_descriptor_log`. So the next batch that writes the MANIFEST always starts a new MANIFEST. Once that new MANIFEST is installed, the old one is no longer used, and the quarantine is cleared. `VersionSet::io_status_` keeps the MANIFEST write error until then. How the code got into this state: - Since meta#7034, `ProcessManifestWrites()` resets `io_status_` after any call whose MANIFEST I/O status is OK. It records only the first error. - meta#12030 added the quarantine. It added the files of a failed batch only inside the "first error" block, and it cleared the quarantine together with the reset of `io_status_`. At that time `SetOptions()` still wrote to the MANIFEST. - meta#13384 made `SetOptions()` skip the MANIFEST write. So every `SetOptions()` became a call with an OK MANIFEST I/O status that writes no new MANIFEST. The problems: 1. Two kinds of calls have an OK MANIFEST I/O status but write no new MANIFEST: - `DBImpl::SetOptions()` installs a new Version through `LogAndApply()` with an edit that writes nothing to the MANIFEST (`skip_manifest_write`). - A batch can fail before any MANIFEST I/O, for example when `LoadTableHandlers()` fails with `paranoid_checks`. A compaction that is already running when a flush's MANIFEST write fails still commits, because the background error is checked only when the compaction starts. After such a call, the old MANIFEST can still hold the failed edit, but its files are no longer quarantined. 2. A second failed MANIFEST write, while `io_status_` already holds an earlier error, does not quarantine its own files. This is a problem when the second batch creates a new MANIFEST and renames CURRENT to it, and then reports an error. CURRENT names the new MANIFEST, and it lists the new files. In both cases, the purge after the failed job, or the purge in a clean `Close()`, deletes the files. When CURRENT names a MANIFEST that lists a deleted file, every `DB::Open()` fails with "Corruption: IO error: No such file or directory ... .sst The file MANIFEST-... may be corrupted". A db_stress crash test hit problem 1: a flush's MANIFEST write got an injected write error, a `SetOptions()` ran about 1.6 ms later and cleared the quarantine, the flush job deleted its SST, and the harness killed the process. This change cannot repair a DB that already lost such an SST. Example of problem 2, reproduced by the new unit test: ``` 1. DropColumnFamily(): the MANIFEST record is synced, then the write reports an error. io_status_ = that error. No background error is set. 2. (optional) SetOptions() succeeds. 3. Flush: starts a new MANIFEST that lists the flushed SST, renames CURRENT to it, then SetCurrentFile() reports an error. io_status_ is already bad, so the SST is not quarantined. 4. The flush job's purge deletes the SST. The next DB::Open() fails. ``` Without step 2, upstream fails this way. With step 2, upstream passes, because `SetOptions()` resets `io_status_` before the flush, so the flush failure is the "first" error. Fixing problem 1 alone would make the run with step 2 fail too, because `SetOptions()` would no longer reset `io_status_`. So this change fixes both problems. New behavior: - Every MANIFEST write failure adds the files of its batch to the quarantine. `io_status_` keeps the first error. A file number is added only once, because automatic recovery can retry many times, each retry adds the old MANIFEST again, and each `FindObsoleteFiles()` copies the list. The check uses a hash set of the numbers already in the list, so its cost stays linear in the number of files. A failed batch can have many files, for example a large ingestion or a refit of a whole level, and the check runs under the DB mutex. - `io_status_` is reset and the quarantine is cleared only when the call wrote and installed a new MANIFEST (s is OK and `new_descriptor_log` is set). A batch that skips the MANIFEST write never sets `new_descriptor_log`. Until then, the files stay quarantined. This is enough for recovery: because `descriptor_log_` was reset, the next real MANIFEST write (for example, a non-atomic flush, a compaction, or the forced write in `DB::Resume()`) starts a new MANIFEST, and its success clears the whole quarantine. - `DBImpl::SetOptions()` sets no background error after its `LogAndApply()`. This part is required, not a cleanup. Since meta#13384, that call does no MANIFEST I/O and is never grouped with other writers, so a non-OK `io_status()` there always belongs to another, earlier batch. The removed block was `if (!versions_->io_status().ok()) { assert(!s.ok()); SetBGError(...); }`. With only the `VersionSet` change, `io_status()` stays non-OK after a successful `SetOptions()`. Then a debug build fails the assert, and a release build sets the old error as a fatal background error, so the next write fails. A backport must include this part. - Two callers do not take the old error in `versions_->io_status()` as their own when they did not fail on MANIFEST I/O: - An atomic flush checks `versions_->io_status()` while it waits for its turn to install. The check stops the wait when the flush that it waits for failed its MANIFEST write. A flush that does not wait for another flush, while the DB is not stopped, ignores the error. With no background error, it installs its result. That install writes a new MANIFEST, and its success clears the old error and the quarantine. With a soft background error, a flush that is not for recovery gets that soft error from the code after the wait and does not install, so it reports its own error. A recovery flush installs, which writes the new MANIFEST. While the DB is stopped, the flush returns the error and does not install, as before this change while io_status() holds the error. The check must stay in that case: when the DB is stopped, the code after the wait checks only the recovery error, which is OK when no recovery runs. This also fixes an upstream case. Upstream, a `SetOptions()` after a failed MANIFEST write resets io_status(). A later atomic flush could then install its result after a fatal background error stopped the DB. With this change, io_status() keeps the error until a new MANIFEST is written, so that flush returns the error instead. Without any old MANIFEST error, an atomic flush that is already running can still install after a fatal background error. That is older than this change and not about the MANIFEST, so this change leaves it. - `CompactionJob::Install()` takes `versions_->io_status()` as the job's I/O status only when the commit itself failed. A compaction that failed before its commit, for example when it could not write an output file, keeps its own I/O status. - The SstFileManager recovers from a soft `NoSpace` error with a direct clear in `ErrorHandler::RecoverFromBGError()`, which does not go through `DBImpl::ResumeImpl()` and writes no MANIFEST. When the quarantine is not empty or `versions_->io_status()` is not OK, the direct clear first writes a new MANIFEST with an empty edit, as `DBImpl::ResumeImpl()` does. If that write fails, the direct clear sets the error of that write as a background error with reason `kManifestWrite`, does not clear, and returns. The SstFileManager then retries, unless that error is fatal. If shutdown has started, the direct clear does not write and returns `ShutdownInProgress`, as `DBImpl::ResumeImpl()` does. Some calls set no background error when their MANIFEST write fails, for example a failed `PromoteL0()` or `DropColumnFamily()`, so a later soft error can find files in the quarantine. Without this part, a debug build then fails the assert in `ErrorHandler::ClearBGError()` that the quarantine is empty, and a release build clears the error while the old MANIFEST may still hold the failed edit. Upstream already fails the assert after a failed `PromoteL0()` followed by a soft error, and this change fixes that case too. With the rest of this change, a `SetOptions()` between them keeps the quarantine, so without this part the assert would fire in that case as well. This change closes both problems above: the `SetOptions()` trigger, the "failure before MANIFEST I/O" trigger, and the second failed MANIFEST write, with or without a `SetOptions()` in between. The same rule also covers a running compaction whose commit fails at the CURRENT step after a flush's MANIFEST write failed (derived from the code, not tested separately). Consequences: - Quarantined files can stay longer, until the next new MANIFEST or a reopen. This only delays their deletion. - The automatic clear of a soft `NoSpace` error by the SstFileManager can take longer after an older failed MANIFEST write. It writes a new MANIFEST first, and it keeps the error if that write fails. This is needed because the older failed write may still be in the old MANIFEST, and clearing the error without a new MANIFEST would forget that. If that MANIFEST write fails, its error becomes a hard error, so writes stop, and the later retry runs the full recovery of `DB::Resume()`, which flushes all column families. If the failed write has a non-retryable `IOError`, the error is fatal, and only a reopen recovers. - `DB::Resume()` writes a new MANIFEST when `versions_->io_status()` is an `IOError`. Without this change, a `SetOptions()` before the `Resume()` made it skip that write. With this change the write happens, and it clears the quarantine. - `versions_->io_status()` keeps an old MANIFEST write error until a new MANIFEST is written, and a `SetOptions()` call does not reset it. Several callers read it after their own failure and report it as their own error. This misattribution is older than this change: it already happened between a failed MANIFEST write and the next call with an OK MANIFEST I/O status. The worst case is an old non-retryable error that some call left without setting a background error. `DropColumnFamily()`, `CreateColumnFamily()`, `DeleteFilesInRanges()`, `PromoteL0()`, `IncreaseFullHistoryTsLow()`, the level refit of `CompactRange()`, and the file number reservation before an ingestion are such calls. A caller that takes that old error can set it as a fatal background error, and only a reopen recovers from that. This change fixes the two cases above (an atomic flush that does not wait, and a compaction that fails before its commit). Known cases that stay, all older than this change: - An atomic flush that waits for another flush still returns the old error. This needs two atomic flushes at the same time. - A compaction whose commit fails before MANIFEST I/O, for example with `Corruption` from `LoadTableHandlers()`, reports the old `IOError` with reason `kManifestWrite` instead of its own `Corruption`. The outputs of that compaction were never committed. - A compaction or flush that fails for its own reason picks the reason `kManifestWrite` or `kManifestWriteNoWAL` when the old error exists. For a retryable error of a compaction, this makes it a hard error instead of a soft error. Automatic recovery or `DB::Resume()` then writes a new MANIFEST and recovers, unless an atomic flush is running at that time. The hard error stops the DB, so that atomic flush returns the old error. When the old error is not retryable, this makes it a fatal error, and neither automatic recovery nor `DB::Resume()` recovers. Upstream recovers in this case when a `SetOptions()` ran after the old error, and goes fatal already at the compaction when none ran. The worse case is with `paranoid_checks=false`. By the severity table, a non-retryable `IOError` is no error for `kCompaction` and `kFlushNoWAL`, but a fatal error for `kManifestWrite` and `kManifestWriteNoWAL`. A `NoSpace` error is no error for `kCompaction`, but a hard error for `kManifestWrite`. - Other calls that write the MANIFEST, such as `ApplyWALToManifest()` and the record of the OPTIONS file number in `WriteOptionsFile()`, set `versions_->io_status()` as a background error when their `LogAndApply()` fails, also when it failed before MANIFEST I/O. Three compaction paths in `DBImpl::BackgroundCompaction()` read `versions_->io_status()` after their `LogAndApply()` also when it succeeded: the FIFO deletion compaction, the FIFO trivial copy (a temperature change), and the trivial move. A complete fix tracks the MANIFEST I/O status of each batch, and lets a caller report `versions_->io_status()` only when its own commit failed on MANIFEST I/O. That touches `LogAndApply()`, flush, compaction and the DB callers above, and is planned as a follow-up. - An ingestion whose MANIFEST write fails deletes its ingested file in the rollback, even when the file is quarantined. This gap is older than this change, and it is on a different code path. A follow-up will fix it. - No API, option or format change. Differential Revision: D134907305
Summary: Background: when a MANIFEST write fails, RocksDB cannot know whether the failed edit is in the MANIFEST that CURRENT names. For example, the record can be written and synced before the error is reported. Or a batch can create a new MANIFEST, rename CURRENT to it, and then get an error from the directory sync. So `VersionSet::ProcessManifestWrites()` puts the new files of the failed batch (the edits of one `ProcessManifestWrites()` call) in the error handler quarantine, and the purge of obsolete files does not delete them. When a MANIFEST write fails, `ProcessManifestWrites()` also resets `descriptor_log_`, and a batch without a `descriptor_log_` sets `new_descriptor_log`. So the next batch that writes the MANIFEST always starts a new MANIFEST. Once that new MANIFEST is installed, the old one is no longer used, and the quarantine is cleared. `VersionSet::io_status_` keeps the MANIFEST write error until then. How the code got into this state: - Since meta#7034, `ProcessManifestWrites()` resets `io_status_` after any call whose MANIFEST I/O status is OK. It records only the first error. - meta#12030 added the quarantine. It added the files of a failed batch only inside the "first error" block, and it cleared the quarantine together with the reset of `io_status_`. At that time `SetOptions()` still wrote to the MANIFEST. - meta#13384 made `SetOptions()` skip the MANIFEST write. So every `SetOptions()` became a call with an OK MANIFEST I/O status that writes no new MANIFEST. The problems: 1. Two kinds of calls have an OK MANIFEST I/O status but write no new MANIFEST: - `DBImpl::SetOptions()` installs a new Version through `LogAndApply()` with an edit that writes nothing to the MANIFEST (`skip_manifest_write`). - A batch can fail before any MANIFEST I/O, for example when `LoadTableHandlers()` fails with `paranoid_checks`. A compaction that is already running when a flush's MANIFEST write fails still commits, because the background error is checked only when the compaction starts. After such a call, the old MANIFEST can still hold the failed edit, but its files are no longer quarantined. 2. A second failed MANIFEST write, while `io_status_` already holds an earlier error, does not quarantine its own files. This is a problem when the second batch creates a new MANIFEST and renames CURRENT to it, and then reports an error. CURRENT names the new MANIFEST, and it lists the new files. In both cases, the purge after the failed job, or the purge in a clean `Close()`, deletes the files. When CURRENT names a MANIFEST that lists a deleted file, every `DB::Open()` fails with "Corruption: IO error: No such file or directory ... .sst The file MANIFEST-... may be corrupted". A db_stress crash test hit problem 1: a flush's MANIFEST write got an injected write error, a `SetOptions()` ran about 1.6 ms later and cleared the quarantine, the flush job deleted its SST, and the harness killed the process. This change cannot repair a DB that already lost such an SST. Example of problem 2, reproduced by the new unit test: ``` 1. DropColumnFamily(): the MANIFEST record is synced, then the write reports an error. io_status_ = that error. No background error is set. 2. (optional) SetOptions() succeeds. 3. Flush: starts a new MANIFEST that lists the flushed SST, renames CURRENT to it, then SetCurrentFile() reports an error. io_status_ is already bad, so the SST is not quarantined. 4. The flush job's purge deletes the SST. The next DB::Open() fails. ``` Without step 2, upstream fails this way. With step 2, upstream passes, because `SetOptions()` resets `io_status_` before the flush, so the flush failure is the "first" error. Fixing problem 1 alone would make the run with step 2 fail too, because `SetOptions()` would no longer reset `io_status_`. So this change fixes both problems. New behavior: - Every MANIFEST write failure adds the files of its batch to the quarantine. `io_status_` keeps the first error. A file number is added only once, because automatic recovery can retry many times, each retry adds the old MANIFEST again, and each `FindObsoleteFiles()` copies the list. The check uses a hash set of the numbers already in the list, so its cost stays linear in the number of files. A failed batch can have many files, for example a large ingestion or a refit of a whole level, and the check runs under the DB mutex. - `io_status_` is reset and the quarantine is cleared only when the call wrote and installed a new MANIFEST (s is OK and `new_descriptor_log` is set). A batch that skips the MANIFEST write never sets `new_descriptor_log`. Until then, the files stay quarantined. This is enough for recovery: because `descriptor_log_` was reset, the next real MANIFEST write (for example, a non-atomic flush, a compaction, or the forced write in `DB::Resume()`) starts a new MANIFEST, and its success clears the whole quarantine. - `DBImpl::SetOptions()` sets no background error after its `LogAndApply()`. This part is required, not a cleanup. Since meta#13384, that call does no MANIFEST I/O and is never grouped with other writers, so a non-OK `io_status()` there always belongs to another, earlier batch. The removed block was `if (!versions_->io_status().ok()) { assert(!s.ok()); SetBGError(...); }`. With only the `VersionSet` change, `io_status()` stays non-OK after a successful `SetOptions()`. Then a debug build fails the assert, and a release build sets the old error as a fatal background error, so the next write fails. A backport must include this part. - Two callers do not take the old error in `versions_->io_status()` as their own when they did not fail on MANIFEST I/O: - An atomic flush checks `versions_->io_status()` while it waits for its turn to install. The check stops the wait when the flush that it waits for failed its MANIFEST write. A flush that does not wait for another flush, while the DB is not stopped, ignores the error. With no background error, it installs its result. That install writes a new MANIFEST, and its success clears the old error and the quarantine. With a soft background error, a flush that is not for recovery gets that soft error from the code after the wait and does not install, so it reports its own error. A recovery flush installs, which writes the new MANIFEST. While the DB is stopped, the flush returns the error and does not install, as before this change while io_status() holds the error. The check must stay in that case: when the DB is stopped, the code after the wait checks only the recovery error, which is OK when no recovery runs. This also fixes an upstream case. Upstream, a `SetOptions()` after a failed MANIFEST write resets io_status(). A later atomic flush could then install its result after a fatal background error stopped the DB. With this change, io_status() keeps the error until a new MANIFEST is written, so that flush returns the error instead. Without any old MANIFEST error, an atomic flush that is already running can still install after a fatal background error. That is older than this change and not about the MANIFEST, so this change leaves it. - `CompactionJob::Install()` takes `versions_->io_status()` as the job's I/O status only when the commit itself failed. A compaction that failed before its commit, for example when it could not write an output file, keeps its own I/O status. - The SstFileManager recovers from a soft `NoSpace` error with a direct clear in `ErrorHandler::RecoverFromBGError()`, which does not go through `DBImpl::ResumeImpl()` and writes no MANIFEST. When the quarantine is not empty or `versions_->io_status()` is not OK, the direct clear first writes a new MANIFEST with an empty edit, as `DBImpl::ResumeImpl()` does. If that write fails, the direct clear sets the error of that write as a background error with reason `kManifestWrite`, does not clear, and returns. The SstFileManager then retries, unless that error is fatal. If shutdown has started, the direct clear does not write and returns `ShutdownInProgress`, as `DBImpl::ResumeImpl()` does. Some calls set no background error when their MANIFEST write fails, for example a failed `PromoteL0()` or `DropColumnFamily()`, so a later soft error can find files in the quarantine. Without this part, a debug build then fails the assert in `ErrorHandler::ClearBGError()` that the quarantine is empty, and a release build clears the error while the old MANIFEST may still hold the failed edit. Upstream already fails the assert after a failed `PromoteL0()` followed by a soft error, and this change fixes that case too. With the rest of this change, a `SetOptions()` between them keeps the quarantine, so without this part the assert would fire in that case as well. This change closes both problems above: the `SetOptions()` trigger, the "failure before MANIFEST I/O" trigger, and the second failed MANIFEST write, with or without a `SetOptions()` in between. The same rule also covers a running compaction whose commit fails at the CURRENT step after a flush's MANIFEST write failed (derived from the code, not tested separately). Consequences: - Quarantined files can stay longer, until the next new MANIFEST or a reopen. This only delays their deletion. - The automatic clear of a soft `NoSpace` error by the SstFileManager can take longer after an older failed MANIFEST write. It writes a new MANIFEST first, and it keeps the error if that write fails. This is needed because the older failed write may still be in the old MANIFEST, and clearing the error without a new MANIFEST would forget that. If that MANIFEST write fails, its error becomes a hard error, so writes stop, and the later retry runs the full recovery of `DB::Resume()`, which flushes all column families. If the failed write has a non-retryable `IOError`, the error is fatal, and only a reopen recovers. - `DB::Resume()` writes a new MANIFEST when `versions_->io_status()` is an `IOError`. Without this change, a `SetOptions()` before the `Resume()` made it skip that write. With this change the write happens, and it clears the quarantine. - `versions_->io_status()` keeps an old MANIFEST write error until a new MANIFEST is written, and a `SetOptions()` call does not reset it. Several callers read it after their own failure and report it as their own error. This misattribution is older than this change: it already happened between a failed MANIFEST write and the next call with an OK MANIFEST I/O status. The worst case is an old non-retryable error that some call left without setting a background error. `DropColumnFamily()`, `CreateColumnFamily()`, `DeleteFilesInRanges()`, `PromoteL0()`, `IncreaseFullHistoryTsLow()`, the level refit of `CompactRange()`, and the file number reservation before an ingestion are such calls. A caller that takes that old error can set it as a fatal background error, and only a reopen recovers from that. This change fixes the two cases above (an atomic flush that does not wait, and a compaction that fails before its commit). Known cases that stay, all older than this change: - An atomic flush that waits for another flush still returns the old error. This needs two atomic flushes at the same time. - A compaction whose commit fails before MANIFEST I/O, for example with `Corruption` from `LoadTableHandlers()`, reports the old `IOError` with reason `kManifestWrite` instead of its own `Corruption`. The outputs of that compaction were never committed. - A compaction or flush that fails for its own reason picks the reason `kManifestWrite` or `kManifestWriteNoWAL` when the old error exists. For a retryable error of a compaction, this makes it a hard error instead of a soft error. Automatic recovery or `DB::Resume()` then writes a new MANIFEST and recovers, unless an atomic flush is running at that time. The hard error stops the DB, so that atomic flush returns the old error. When the old error is not retryable, this makes it a fatal error, and neither automatic recovery nor `DB::Resume()` recovers. Upstream recovers in this case when a `SetOptions()` ran after the old error, and goes fatal already at the compaction when none ran. The worse case is with `paranoid_checks=false`. By the severity table, a non-retryable `IOError` is no error for `kCompaction` and `kFlushNoWAL`, but a fatal error for `kManifestWrite` and `kManifestWriteNoWAL`. A `NoSpace` error is no error for `kCompaction`, but a hard error for `kManifestWrite`. - Other calls that write the MANIFEST, such as `ApplyWALToManifest()` and the record of the OPTIONS file number in `WriteOptionsFile()`, set `versions_->io_status()` as a background error when their `LogAndApply()` fails, also when it failed before MANIFEST I/O. Three compaction paths in `DBImpl::BackgroundCompaction()` read `versions_->io_status()` after their `LogAndApply()` also when it succeeded: the FIFO deletion compaction, the FIFO trivial copy (a temperature change), and the trivial move. A complete fix tracks the MANIFEST I/O status of each batch, and lets a caller report `versions_->io_status()` only when its own commit failed on MANIFEST I/O. That touches `LogAndApply()`, flush, compaction and the DB callers above, and is planned as a follow-up. - An ingestion whose MANIFEST write fails deletes its ingested file in the rollback, even when the file is quarantined. This gap is older than this change, and it is on a different code path. A follow-up will fix it. - No API, option or format change. Differential Revision: D134907305
Summary: Background: when a MANIFEST write fails, RocksDB cannot know whether the failed edit is in the MANIFEST that CURRENT names. For example, the record can be written and synced before the error is reported. Or a batch can create a new MANIFEST, rename CURRENT to it, and then get an error from the directory sync. So `VersionSet::ProcessManifestWrites()` puts the new files of the failed batch (the edits of one `ProcessManifestWrites()` call) in the error handler quarantine, and the purge of obsolete files does not delete them. When a MANIFEST write fails, `ProcessManifestWrites()` also resets `descriptor_log_`, and a batch without a `descriptor_log_` sets `new_descriptor_log`. So the next batch that writes the MANIFEST always starts a new MANIFEST. Once that new MANIFEST is installed, the old one is no longer used, and the quarantine is cleared. `VersionSet::io_status_` keeps the MANIFEST write error until then. How the code got into this state: - Since meta#7034, `ProcessManifestWrites()` resets `io_status_` after any call whose MANIFEST I/O status is OK. It records only the first error. - meta#12030 added the quarantine. It added the files of a failed batch only inside the "first error" block, and it cleared the quarantine together with the reset of `io_status_`. At that time `SetOptions()` still wrote to the MANIFEST. - meta#13384 made `SetOptions()` skip the MANIFEST write. So every `SetOptions()` became a call with an OK MANIFEST I/O status that writes no new MANIFEST. The problems: 1. Two kinds of calls have an OK MANIFEST I/O status but write no new MANIFEST: - `DBImpl::SetOptions()` installs a new Version through `LogAndApply()` with an edit that writes nothing to the MANIFEST (`skip_manifest_write`). - A batch can fail before any MANIFEST I/O, for example when `LoadTableHandlers()` fails with `paranoid_checks`. A compaction that is already running when a flush's MANIFEST write fails still commits, because the background error is checked only when the compaction starts. After such a call, the old MANIFEST can still hold the failed edit, but its files are no longer quarantined. 2. A second failed MANIFEST write, while `io_status_` already holds an earlier error, does not quarantine its own files. This is a problem when the second batch creates a new MANIFEST and renames CURRENT to it, and then reports an error. CURRENT names the new MANIFEST, and it lists the new files. In both cases, the purge after the failed job, or the purge in a clean `Close()`, deletes the files. When CURRENT names a MANIFEST that lists a deleted file, every `DB::Open()` fails with "Corruption: IO error: No such file or directory ... .sst The file MANIFEST-... may be corrupted". A db_stress crash test hit problem 1: a flush's MANIFEST write got an injected write error, a `SetOptions()` ran about 1.6 ms later and cleared the quarantine, the flush job deleted its SST, and the harness killed the process. This change cannot repair a DB that already lost such an SST. Example of problem 2, reproduced by the new unit test: ``` 1. DropColumnFamily(): the MANIFEST record is synced, then the write reports an error. io_status_ = that error. No background error is set. 2. (optional) SetOptions() succeeds. 3. Flush: starts a new MANIFEST that lists the flushed SST, renames CURRENT to it, then SetCurrentFile() reports an error. io_status_ is already bad, so the SST is not quarantined. 4. The flush job's purge deletes the SST. The next DB::Open() fails. ``` Without step 2, upstream fails this way. With step 2, upstream passes, because `SetOptions()` resets `io_status_` before the flush, so the flush failure is the "first" error. Fixing problem 1 alone would make the run with step 2 fail too, because `SetOptions()` would no longer reset `io_status_`. So this change fixes both problems. New behavior: - Every MANIFEST write failure adds the files of its batch to the quarantine. `io_status_` keeps the first error. A file number is added only once, because automatic recovery can retry many times, each retry adds the old MANIFEST again, and each `FindObsoleteFiles()` copies the list. The check uses a hash set of the numbers already in the list, so its cost stays linear in the number of files. A failed batch can have many files, for example a large ingestion or a refit of a whole level, and the check runs under the DB mutex. - `io_status_` is reset and the quarantine is cleared only when the call wrote and installed a new MANIFEST (s is OK and `new_descriptor_log` is set). A batch that skips the MANIFEST write never sets `new_descriptor_log`. Until then, the files stay quarantined. This is enough for recovery: because `descriptor_log_` was reset, the next real MANIFEST write (for example, a non-atomic flush, a compaction, or the forced write in `DB::Resume()`) starts a new MANIFEST, and its success clears the whole quarantine. - `DBImpl::SetOptions()` sets no background error after its `LogAndApply()`. This part is required, not a cleanup. Since meta#13384, that call does no MANIFEST I/O and is never grouped with other writers, so a non-OK `io_status()` there always belongs to another, earlier batch. The removed block was `if (!versions_->io_status().ok()) { assert(!s.ok()); SetBGError(...); }`. With only the `VersionSet` change, `io_status()` stays non-OK after a successful `SetOptions()`. Then a debug build fails the assert, and a release build sets the old error as a fatal background error, so the next write fails. A backport must include this part. - Two callers do not take the old error in `versions_->io_status()` as their own when they did not fail on MANIFEST I/O: - An atomic flush checks `versions_->io_status()` while it waits for its turn to install. The check stops the wait when the flush that it waits for failed its MANIFEST write. A flush that does not wait for another flush, while the DB is not stopped, ignores the error. With no background error, it installs its result. That install writes a new MANIFEST, and its success clears the old error and the quarantine. With a soft background error, a flush that is not for recovery gets that soft error from the code after the wait and does not install, so it reports its own error. A recovery flush installs, which writes the new MANIFEST. While the DB is stopped, the flush returns the error and does not install, as before this change while io_status() holds the error. The check must stay in that case: when the DB is stopped, the code after the wait checks only the recovery error, which is OK when no recovery runs. This also fixes an upstream case. Upstream, a `SetOptions()` after a failed MANIFEST write resets io_status(). A later atomic flush could then install its result after a fatal background error stopped the DB. With this change, io_status() keeps the error until a new MANIFEST is written, so that flush returns the error instead. Without any old MANIFEST error, an atomic flush that is already running can still install after a fatal background error. That is older than this change and not about the MANIFEST, so this change leaves it. - `CompactionJob::Install()` takes `versions_->io_status()` as the job's I/O status only when the commit itself failed. A compaction that failed before its commit, for example when it could not write an output file, keeps its own I/O status. - The SstFileManager recovers from a soft `NoSpace` error with a direct clear in `ErrorHandler::RecoverFromBGError()`, which does not go through `DBImpl::ResumeImpl()` and writes no MANIFEST. When the quarantine is not empty or `versions_->io_status()` is not OK, the direct clear first writes a new MANIFEST with an empty edit, as `DBImpl::ResumeImpl()` does. If that write fails, the direct clear sets the error of that write as a background error with reason `kManifestWrite`, does not clear, and returns. The SstFileManager then retries, unless that error is fatal. If shutdown has started, the direct clear does not write and returns `ShutdownInProgress`, as `DBImpl::ResumeImpl()` does. Some calls set no background error when their MANIFEST write fails, for example a failed `PromoteL0()` or `DropColumnFamily()`, so a later soft error can find files in the quarantine. Without this part, a debug build then fails the assert in `ErrorHandler::ClearBGError()` that the quarantine is empty, and a release build clears the error while the old MANIFEST may still hold the failed edit. Upstream already fails the assert after a failed `PromoteL0()` followed by a soft error, and this change fixes that case too. With the rest of this change, a `SetOptions()` between them keeps the quarantine, so without this part the assert would fire in that case as well. This change closes both problems above: the `SetOptions()` trigger, the "failure before MANIFEST I/O" trigger, and the second failed MANIFEST write, with or without a `SetOptions()` in between. The same rule also covers a running compaction whose commit fails at the CURRENT step after a flush's MANIFEST write failed (derived from the code, not tested separately). Consequences: - Quarantined files can stay longer, until the next new MANIFEST or a reopen. This only delays their deletion. - The automatic clear of a soft `NoSpace` error by the SstFileManager can take longer after an older failed MANIFEST write. It writes a new MANIFEST first, and it keeps the error if that write fails. This is needed because the older failed write may still be in the old MANIFEST, and clearing the error without a new MANIFEST would forget that. If that MANIFEST write fails, its error becomes a hard error, so writes stop, and the later retry runs the full recovery of `DB::Resume()`, which flushes all column families. If the failed write has a non-retryable `IOError`, the error is fatal, and only a reopen recovers. - `DB::Resume()` writes a new MANIFEST when `versions_->io_status()` is an `IOError`. Without this change, a `SetOptions()` before the `Resume()` made it skip that write. With this change the write happens, and it clears the quarantine. - `versions_->io_status()` keeps an old MANIFEST write error until a new MANIFEST is written, and a `SetOptions()` call does not reset it. Several callers read it after their own failure and report it as their own error. This misattribution is older than this change: it already happened between a failed MANIFEST write and the next call with an OK MANIFEST I/O status. The worst case is an old non-retryable error that some call left without setting a background error. `DropColumnFamily()`, `CreateColumnFamily()`, `DeleteFilesInRanges()`, `PromoteL0()`, `IncreaseFullHistoryTsLow()`, the level refit of `CompactRange()`, and the file number reservation before an ingestion are such calls. A caller that takes that old error can set it as a fatal background error, and only a reopen recovers from that. This change fixes the two cases above (an atomic flush that does not wait, and a compaction that fails before its commit). Known cases that stay, all older than this change: - An atomic flush that waits for another flush still returns the old error. This needs two atomic flushes at the same time. - A compaction whose commit fails before MANIFEST I/O, for example with `Corruption` from `LoadTableHandlers()`, reports the old `IOError` with reason `kManifestWrite` instead of its own `Corruption`. The outputs of that compaction were never committed. - A compaction or flush that fails for its own reason picks the reason `kManifestWrite` or `kManifestWriteNoWAL` when the old error exists. For a retryable error of a compaction, this makes it a hard error instead of a soft error. Automatic recovery or `DB::Resume()` then writes a new MANIFEST and recovers, unless an atomic flush is running at that time. The hard error stops the DB, so that atomic flush returns the old error. When the old error is not retryable, this makes it a fatal error, and neither automatic recovery nor `DB::Resume()` recovers. Upstream recovers in this case when a `SetOptions()` ran after the old error, and goes fatal already at the compaction when none ran. The worse case is with `paranoid_checks=false`. By the severity table, a non-retryable `IOError` is no error for `kCompaction` and `kFlushNoWAL`, but a fatal error for `kManifestWrite` and `kManifestWriteNoWAL`. A `NoSpace` error is no error for `kCompaction`, but a hard error for `kManifestWrite`. - Other calls that write the MANIFEST, such as `ApplyWALToManifest()` and the record of the OPTIONS file number in `WriteOptionsFile()`, set `versions_->io_status()` as a background error when their `LogAndApply()` fails, also when it failed before MANIFEST I/O. Three compaction paths in `DBImpl::BackgroundCompaction()` read `versions_->io_status()` after their `LogAndApply()` also when it succeeded: the FIFO deletion compaction, the FIFO trivial copy (a temperature change), and the trivial move. A complete fix tracks the MANIFEST I/O status of each batch, and lets a caller report `versions_->io_status()` only when its own commit failed on MANIFEST I/O. That touches `LogAndApply()`, flush, compaction and the DB callers above, and is planned as a follow-up. - An ingestion whose MANIFEST write fails deletes its ingested file in the rollback, even when the file is quarantined. This gap is older than this change, and it is on a different code path. A follow-up will fix it. - No API, option or format change. Differential Revision: D134907305
Summary:
After #6949 , VersionSet::io_status_ can be concurrently accessed by multiple
threads without lock, causing tsan test to fail. For example, a bg flush thread
resets io_status_ before calling LogAndApply(), while another thread already in
the process of LogAndApply() reads io_status_. This is a bug.
We do not have to reset io_status_ each time we call LogAndApply(). io_status_
is part of the state of VersionSet, and it indicates the outcome of preceding
MANIFEST/CURRENT files IO operations. Its value should be updated only when:
failure without process restart, e.g. calling Resume().
Test Plan (devserver):
COMPILE_WITH_TSAN=1 make check
COMPILE_WITH_TSAN=1 make db_test2
./db_test2 --gtest_filter=DBTest2.CompactionStall