Skip to content

Improve atomicity of SetOptions, skip manifest write - #13384

Closed
pdillinger wants to merge 8 commits into
meta:mainfrom
pdillinger:set_options_no_manifest_write
Closed

pdillinger wants to merge 8 commits into
meta:mainfrom
pdillinger:set_options_no_manifest_write

Conversation

@pdillinger

@pdillinger pdillinger commented Feb 7, 2025 •

Copy link
Copy Markdown
Contributor

Summary: Motivated by code review issue in #13316, we don't want to release the DB mutex in SetOptions between updating the cfd latest options and installing the new Version and SuperVersion. SetOptions uses LogAndApply to install a new Version but this currently incurs an unnecessary manifest write. (This is not a big performance concern because SetOptions dumps a new OPTIONS file, which is much larger than the redundant manifest update.) Since we don't want IO while holding the DB mutex, we need to get rid of the manifest write, and that's what this change does. We introduce a kind of dummy VersionEdit that allows the existing code paths of LogAndApply to install a new Version (with the updated mutable options), recompute resulting compaction scores etc., but without the manifest write.

Part of the validation for this is new assertions in SetOptions verifying the consistency of the various copies of MutableCFOptions. (I'm not convinced we need it in SuperVersion in addition to Version, but that's not for here and now.) These checks depend on defaulted operator== so depend on C++20.

Test Plan: New unit test in addition to new assertions. SetOptions already tested heavily in crash test. Used
ROCKSDB_CXX_STANDARD=c++20 make -j100 check to ensure the new assertions are verified

Summary: Motivated by code review issue in meta#13316, we don't want to
release the DB mutex in SetOptions between updating the cfd latest
options and installing the new Version and SuperVersion. SetOptions uses
LogAndApply to install a new Version but this currently incurs an
unnecessary manifest write. (This is not a big performance concern
because SetOptions dumps a new OPTIONS file, which is much larger than
the redundant manifest update.) Since we don't want IO while holding the
DB mutex, we need to get rid of the manifest write, and that's what this
change does. We introduce a kind of dummy VersionEdit that allows the
existing code paths of LogAndApply to install a new Version (with the
updated mutable options), recompute resulting compaction scores etc.,
but without the manifest write.

Part of the validation for this is new assertions in SetOptions
verifying the consistency of the various copies of MutableCFOptions.
(I'm not convinced we need it in SuperVersion in addition to Version,
but that's not for here and now.) These checks depend on defaulted
`operator==` so depend on C++20.

Light refactoring of VersionEdit.

Test Plan: New unit test in addition to new assertions. SetOptions
already tested heavily in crash test. Used
`ROCKSDB_CXX_STANDARD=c++20 make -j100 check` to ensure the new
assertions are verified
@facebook-github-bot

Copy link
Copy Markdown
Contributor

@pdillinger has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@cbi42 cbi42 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Consider the internal linter complaint.

@facebook-github-bot

Copy link
Copy Markdown
Contributor

@pdillinger has updated the pull request. You must reimport the pull request before landing.

@facebook-github-bot

Copy link
Copy Markdown
Contributor

@pdillinger has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@facebook-github-bot

Copy link
Copy Markdown
Contributor

@pdillinger merged this pull request in c9ce4a3.

ybtsdst pushed a commit to ybtsdst/rocksdb that referenced this pull request Apr 27, 2025
Summary:
Motivated by code review issue in meta#13316, we don't want to release the DB mutex in SetOptions between updating the cfd latest options and installing the new Version and SuperVersion. SetOptions uses LogAndApply to install a new Version but this currently incurs an unnecessary manifest write. (This is not a big performance concern because SetOptions dumps a new OPTIONS file, which is much larger than the redundant manifest update.) Since we don't want IO while holding the DB mutex, we need to get rid of the manifest write, and that's what this change does. We introduce a kind of dummy VersionEdit that allows the existing code paths of LogAndApply to install a new Version (with the updated mutable options), recompute resulting compaction scores etc., but without the manifest write.

Part of the validation for this is new assertions in SetOptions verifying the consistency of the various copies of MutableCFOptions. (I'm not convinced we need it in SuperVersion in addition to Version, but that's not for here and now.) These checks depend on defaulted `operator==` so depend on C++20.

Pull Request resolved: meta#13384

Test Plan:
New unit test in addition to new assertions. SetOptions already tested heavily in crash test. Used
`ROCKSDB_CXX_STANDARD=c++20 make -j100 check` to ensure the new assertions are verified

Reviewed By: cbi42

Differential Revision: D69408829

Pulled By: pdillinger

fbshipit-source-id: 4cf026010c6bb381e0ea27567cce2708d4678e7d
Tim-Reed pushed a commit to Tim-Reed/rocksdb that referenced this pull request Oct 1, 2026
Summary:
Motivated by code review issue in meta#13316, we don't want to release the DB mutex in SetOptions between updating the cfd latest options and installing the new Version and SuperVersion. SetOptions uses LogAndApply to install a new Version but this currently incurs an unnecessary manifest write. (This is not a big performance concern because SetOptions dumps a new OPTIONS file, which is much larger than the redundant manifest update.) Since we don't want IO while holding the DB mutex, we need to get rid of the manifest write, and that's what this change does. We introduce a kind of dummy VersionEdit that allows the existing code paths of LogAndApply to install a new Version (with the updated mutable options), recompute resulting compaction scores etc., but without the manifest write.

Part of the validation for this is new assertions in SetOptions verifying the consistency of the various copies of MutableCFOptions. (I'm not convinced we need it in SuperVersion in addition to Version, but that's not for here and now.) These checks depend on defaulted `operator==` so depend on C++20.

Pull Request resolved: meta#13384

Test Plan:
New unit test in addition to new assertions. SetOptions already tested heavily in crash test. Used
`ROCKSDB_CXX_STANDARD=c++20 make -j100 check` to ensure the new assertions are verified

Reviewed By: cbi42

Differential Revision: D69408829

Pulled By: pdillinger

fbshipit-source-id: 4cf026010c6bb381e0ea27567cce2708d4678e7d
xingbowang added a commit to xingbowang/rocksdb that referenced this pull request Nov 29, 2026
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()`
  (a related 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 block only
  reported that old error again. After a failed `DropColumnFamily()`, which
  sets no background error, a `SetOptions()` call with an invalid value would
  have stopped the DB with the old error.

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.
- After an earlier MANIFEST write error, a later failure that is not a
  MANIFEST I/O error still sees the old `IOError` in `versions_->io_status()`.
  Callers then report that old error as a MANIFEST write error. Examples:
  - An atomic flush fails before its install because of that old error. This
    was already true between the failure and the next OK call. This change
    adds the case after a `SetOptions()`. The flush can install only after a
    new MANIFEST is written, for example by `DB::Resume()`.
  - A running 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
    background error then has the severity of the old error (for example
    fatal instead of unrecoverable). The impact is low, because the outputs of
    that compaction were never committed.
- No API, option or format change.

Differential Revision: D134907305
xingbowang added a commit to xingbowang/rocksdb that referenced this pull request Nov 29, 2026
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()`
  (a related 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 block only
  reported that old error again. After a failed `DropColumnFamily()`, which
  sets no background error, a `SetOptions()` call with an invalid value would
  have stopped the DB with the old error.

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.
- After an earlier MANIFEST write error, a later failure that is not a
  MANIFEST I/O error still sees the old `IOError` in `versions_->io_status()`.
  Callers then report that old error as a MANIFEST write error. Examples:
  - An atomic flush fails before its install because of that old error. This
    was already true between the failure and the next OK call. This change
    adds the case after a `SetOptions()`. The flush can install only after a
    new MANIFEST is written, for example by `DB::Resume()`.
  - A running 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
    background error then has the severity of the old error (for example
    fatal instead of unrecoverable). The impact is low, because the outputs of
    that compaction were never committed.
- No API, option or format change.

Differential Revision: D134907305
xingbowang added a commit to xingbowang/rocksdb that referenced this pull request Nov 29, 2026
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()`
  (a related 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 block only
  reported that old error again. After a failed `DropColumnFamily()`, which
  sets no background error, a `SetOptions()` call with an invalid value would
  have stopped the DB with the old error.

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.
- After an earlier MANIFEST write error, a later failure that is not a
  MANIFEST I/O error still sees the old `IOError` in `versions_->io_status()`.
  Callers then report that old error as a MANIFEST write error. Examples:
  - An atomic flush fails before its install because of that old error. This
    was already true between the failure and the next OK call. This change
    adds the case after a `SetOptions()`. The flush can install only after a
    new MANIFEST is written, for example by `DB::Resume()`.
  - A running 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
    background error then has the severity of the old error (for example
    fatal instead of unrecoverable). The impact is low, because the outputs of
    that compaction were never committed.
- No API, option or format change.

Differential Revision: D134907305
xingbowang added a commit to xingbowang/rocksdb that referenced this pull request Nov 29, 2026
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()`
  (a related 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 block only
  reported that old error again. After a failed `DropColumnFamily()`, which
  sets no background error, a `SetOptions()` call with an invalid value would
  have stopped the DB with the old error.

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.
- After an earlier MANIFEST write error, a later failure that is not a
  MANIFEST I/O error still sees the old `IOError` in `versions_->io_status()`.
  Callers then report that old error as a MANIFEST write error. Examples:
  - An atomic flush fails before its install because of that old error. This
    was already true between the failure and the next OK call. This change
    adds the case after a `SetOptions()`. The flush can install only after a
    new MANIFEST is written, for example by `DB::Resume()`.
  - A running 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
    background error then has the severity of the old error (for example
    fatal instead of unrecoverable). The impact is low, because the outputs of
    that compaction were never committed.
- No API, option or format change.

Differential Revision: D134907305
xingbowang added a commit to xingbowang/rocksdb that referenced this pull request Nov 29, 2026
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()`
  (a related 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 block only
  reported that old error again. After a failed `DropColumnFamily()`, which
  sets no background error, a `SetOptions()` call with an invalid value would
  have stopped the DB with the old error.

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.
- After an earlier MANIFEST write error, a later failure that is not a
  MANIFEST I/O error still sees the old `IOError` in `versions_->io_status()`.
  Callers then report that old error as a MANIFEST write error. Examples:
  - An atomic flush fails before its install because of that old error. This
    was already true between the failure and the next OK call. This change
    adds the case after a `SetOptions()`. The flush can install only after a
    new MANIFEST is written, for example by `DB::Resume()`.
  - A running 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
    background error then has the severity of the old error (for example
    fatal instead of unrecoverable). The impact is low, because the outputs of
    that compaction were never committed.
- No API, option or format change.

Differential Revision: D134907305
xingbowang added a commit to xingbowang/rocksdb that referenced this pull request Nov 29, 2026
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()`
  (a related 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 block only
  reported that old error again. After a failed `DropColumnFamily()`, which
  sets no background error, a `SetOptions()` call with an invalid value would
  have stopped the DB with the old error.

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.
- After an earlier MANIFEST write error, a later failure that is not a
  MANIFEST I/O error still sees the old `IOError` in `versions_->io_status()`.
  Callers then report that old error as a MANIFEST write error. Examples:
  - An atomic flush fails before its install because of that old error. This
    was already true between the failure and the next OK call. This change
    adds the case after a `SetOptions()`. The flush can install only after a
    new MANIFEST is written, for example by `DB::Resume()`.
  - A running 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
    background error then has the severity of the old error (for example
    fatal instead of unrecoverable). The impact is low, because the outputs of
    that compaction were never committed.
- No API, option or format change.

Differential Revision: D134907305
xingbowang added a commit to xingbowang/rocksdb that referenced this pull request Nov 29, 2026
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()`
  (a related 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 block only
  reported that old error again. After a failed `DropColumnFamily()`, which
  sets no background error, a `SetOptions()` call with an invalid value would
  have stopped the DB with the old error.

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.
- After an earlier MANIFEST write error, a later failure that is not a
  MANIFEST I/O error still sees the old `IOError` in `versions_->io_status()`.
  Callers then report that old error as a MANIFEST write error. Examples:
  - An atomic flush fails before its install because of that old error. This
    was already true between the failure and the next OK call. This change
    adds the case after a `SetOptions()`. The flush can install only after a
    new MANIFEST is written, for example by `DB::Resume()`.
  - A running 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
    background error then has the severity of the old error (for example
    fatal instead of unrecoverable). The impact is low, because the outputs of
    that compaction were never committed.
- No API, option or format change.

Differential Revision: D134907305
xingbowang added a commit to xingbowang/rocksdb that referenced this pull request Nov 29, 2026
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()`
  (a related 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 block only
  reported that old error again. After a failed `DropColumnFamily()`, which
  sets no background error, a `SetOptions()` call with an invalid value would
  have stopped the DB with the old error.

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.
- After an earlier MANIFEST write error, a later failure that is not a
  MANIFEST I/O error still sees the old `IOError` in `versions_->io_status()`.
  Callers then report that old error as a MANIFEST write error. Examples:
  - An atomic flush fails before its install because of that old error. This
    was already true between the failure and the next OK call. This change
    adds the case after a `SetOptions()`. The flush can install only after a
    new MANIFEST is written, for example by `DB::Resume()`.
  - A running 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
    background error then has the severity of the old error (for example
    fatal instead of unrecoverable). The impact is low, because the outputs of
    that compaction were never committed.
- No API, option or format change.

Differential Revision: D134907305
xingbowang added a commit to xingbowang/rocksdb that referenced this pull request Nov 30, 2026
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
xingbowang added a commit to xingbowang/rocksdb that referenced this pull request Nov 30, 2026
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
xingbowang added a commit to xingbowang/rocksdb that referenced this pull request Nov 30, 2026
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
xingbowang added a commit to xingbowang/rocksdb that referenced this pull request Nov 30, 2026
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
xingbowang added a commit to xingbowang/rocksdb that referenced this pull request Nov 30, 2026
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
xingbowang added a commit to xingbowang/rocksdb that referenced this pull request Nov 30, 2026
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
xingbowang added a commit to xingbowang/rocksdb that referenced this pull request Nov 30, 2026
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
xingbowang added a commit to xingbowang/rocksdb that referenced this pull request Nov 30, 2026
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
xingbowang added a commit to xingbowang/rocksdb that referenced this pull request Nov 30, 2026
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
xingbowang added a commit to xingbowang/rocksdb that referenced this pull request Nov 30, 2026
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
xingbowang added a commit to xingbowang/rocksdb that referenced this pull request Nov 30, 2026
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
xingbowang added a commit to xingbowang/rocksdb that referenced this pull request Nov 30, 2026
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
xingbowang added a commit to xingbowang/rocksdb that referenced this pull request Nov 30, 2026
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
xingbowang added a commit to xingbowang/rocksdb that referenced this pull request Nov 30, 2026
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
xingbowang added a commit to xingbowang/rocksdb that referenced this pull request Nov 30, 2026
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
xingbowang added a commit to xingbowang/rocksdb that referenced this pull request Nov 30, 2026
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
xingbowang added a commit to xingbowang/rocksdb that referenced this pull request Dec 1, 2026
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
xingbowang added a commit to xingbowang/rocksdb that referenced this pull request Dec 1, 2026
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
xingbowang added a commit to xingbowang/rocksdb that referenced this pull request Dec 1, 2026
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
xingbowang added a commit to xingbowang/rocksdb that referenced this pull request Dec 1, 2026
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
xingbowang added a commit to xingbowang/rocksdb that referenced this pull request Dec 1, 2026
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
xingbowang added a commit to xingbowang/rocksdb that referenced this pull request Dec 1, 2026
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
xingbowang added a commit to xingbowang/rocksdb that referenced this pull request Dec 1, 2026
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
xingbowang added a commit to xingbowang/rocksdb that referenced this pull request Dec 1, 2026
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
xingbowang added a commit to xingbowang/rocksdb that referenced this pull request Dec 1, 2026
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
xingbowang added a commit to xingbowang/rocksdb that referenced this pull request Dec 1, 2026
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
xingbowang added a commit to xingbowang/rocksdb that referenced this pull request Dec 1, 2026
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
xingbowang added a commit to xingbowang/rocksdb that referenced this pull request Dec 1, 2026
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
xingbowang added a commit to xingbowang/rocksdb that referenced this pull request Dec 1, 2026
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
xingbowang added a commit to xingbowang/rocksdb that referenced this pull request Dec 1, 2026
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.
  After the new MANIFEST write succeeds, the direct clear runs the same full
  purge of obsolete files as `DBImpl::ResumeImpl()`, so files that were kept
  only because of the quarantine are deleted at once, for example the output of
  a failed compaction commit. Without the purge, such a file stays until the
  next full purge (a failed job, `delete_obsolete_files_period_micros`, or a
  reopen). The direct clear also signals `bg_cv_` when it ends, as
  `DBImpl::ResumeImpl()` does. `DB::Close()` waits on `bg_cv_` until no recovery
  runs, and the new MANIFEST write releases the DB mutex, so a close that starts
  during that write would otherwise wait forever.
  `SstFileManagerImpl::ClearError()` reads the background error of the DB only
  when it uses it. That changes no behavior. The direct clear can return a
  non-OK status, and an unused, unchecked copy of the error would fail the
  `ASSERT_STATUS_CHECKED` build.
- When the forced MANIFEST write in `DBImpl::ResumeImpl()` fails, it sets the
  error of that write as the background error, with reason `kManifestWrite`.
  `versions_->io_status()` keeps the first MANIFEST write error, which can be an
  older, non-retryable error. Reporting that older error would turn a retryable
  failure of the recovery's own write into a fatal error, and neither
  `DB::Resume()` nor automatic recovery could recover. Upstream has the same
  problem when no `SetOptions()` ran after the older error. With the rest of
  this change, the older error outlives `SetOptions()`, so without this part the
  same failure would go fatal also after a `SetOptions()`, where upstream
  recovers.

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 three cases above (an atomic flush that does not wait, a
  compaction that fails before its commit, and the forced MANIFEST write of
  `DBImpl::ResumeImpl()`). 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. The MANIFEST write that persists a reserved sequence number
    in `DBImpl::ResumeImpl()` (file-scoped WAL write error recovery) still
    sets `versions_->io_status()` when it fails.
  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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants