Skip to content

Keep quarantined files until a new MANIFEST is written - #15818

Open
xingbowang wants to merge 4 commits into
meta:mainfrom
xingbowang:export-D134907305
Open

xingbowang wants to merge 4 commits into
meta:mainfrom
xingbowang:export-D134907305

Conversation

@xingbowang

@xingbowang xingbowang commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

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:

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 Improve atomicity of SetOptions, skip manifest write #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.
    • When the commit of a compaction fails while versions_->io_status() is not
      OK, CompactionJob::Install() takes the error of that commit as the job's
      I/O status, not versions_->io_status(), which keeps the first MANIFEST
      write error and can be older. So a retryable failure of the commit stays a
      hard error, and DB::Resume() recovers. 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.
    If the direct clear fails after a close started (the close can start while the
    MANIFEST write or the purge releases the DB mutex), it returns
    ShutdownInProgress (or the fatal background error, if there is one), as
    DBImpl::ResumeImpl() does. That ends the recovery,
    so the close does not wait for a later retry by the SstFileManager, which may
    not come while the disk stays full. A foreground MANIFEST write, for example a
    failed PromoteL0(), can also fail in those windows. It sets no background
    error, and its files stay quarantined until a later new MANIFEST. So the debug
    assert in ErrorHandler::ClearBGError() allows a quarantine that is not empty
    while versions_->io_status() holds an error.
    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 cases above (an atomic flush that does not wait, a compaction that
    fails before its commit or in its own 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 its own Corruption, but
      with reason kManifestWrite, because DBImpl::BackgroundCompaction() picks
      the reason from versions_->io_status(). The outputs of that compaction
      were never committed.
    • The forced empty edit of DBImpl::ResumeImpl() and of the direct clear can
      be grouped with other writers in one ProcessManifestWrites() batch. Then
      the failure of another writer in the batch becomes the status of the forced
      edit. For example, Corruption from LoadTableHandlers() under
      paranoid_checks is then set with reason kManifestWrite, which makes it
      fatal. This is more likely during the direct clear, because background work
      keeps committing while a soft error is set.
    • 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 each
      caller report the error of its own batch. 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

@meta-codesync

meta-codesync Bot commented Nov 28, 2026

Copy link
Copy Markdown

@xingbowang has exported this pull request. If you are a Meta employee, you can view the originating Diff in D134907305.

@github-actions

github-actions Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

⚠️ clang-tidy: 2 warning(s) on changed lines

Completed in 866.3s.

Summary by check

Check Count
performance-inefficient-vector-operation 2
Total 2

Details

db/error_handler_fs_test.cc (2 warning(s))
db/error_handler_fs_test.cc:3531:5: warning: 'push_back' is called inside a loop; consider pre-allocating the container capacity before the loop [performance-inefficient-vector-operation]
db/error_handler_fs_test.cc:3760:5: warning: 'push_back' is called inside a loop; consider pre-allocating the container capacity before the loop [performance-inefficient-vector-operation]

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:
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
@meta-codesync meta-codesync Bot changed the title Keep quarantined files until a new MANIFEST is written Keep quarantined files until a new MANIFEST is written (#15818) Nov 29, 2026
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
@meta-codesync meta-codesync Bot changed the title Keep quarantined files until a new MANIFEST is written (#15818) Keep quarantined files until a new MANIFEST is written Nov 30, 2026
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
@meta-codesync meta-codesync Bot changed the title Keep quarantined files until a new MANIFEST is written Keep quarantined files until a new MANIFEST is written (#15818) Nov 30, 2026
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:
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. 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:
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.
  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
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.
  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
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.
  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
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.
  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
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.
  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
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.
  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
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.
  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
… atomic compaction unit covers it

Summary:
## Background

`TimedPut()` writes an entry of type `kTypeValuePreferredSeqno`. It carries its own sequence number and a smaller preferred sequence number, which comes from the write time through the seqno-to-time mapping. When a compaction proves that no older version of the key exists below the output level (`bottommost_level` or `KeyNotExistsBeyondOutputLevel()`), `CompactionIterator` swaps the entry to its preferred sequence number and writes it as a `kTypeValue`. Before the swap, it asks the compaction's range tombstone aggregator whether a tombstone would cover the entry at the preferred sequence number. If one would, it writes the entry unchanged, because a tombstone that is older than the entry must not delete it after the swap.

Compaction can cut its output between two versions of one user key: for example, when a snapshot keeps both versions and the file reaches its size limit, or at a grandparent or TTL cut, or at the start of a range tombstone. Adjacent files of one level that share a boundary user key form one atomic compaction unit (`Compaction::PopulateWithAtomicBoundaries()`). The exception is a file whose largest key is a range tombstone sentinel. When a range tombstone continues into the next file and the next file starts with a different user key, the earlier file ends at a sentinel key (the next file's first user key, `kMaxSequenceNumber`, `kTypeRangeDeletion`). Such a file ends its unit. When the cut falls between two versions of one user key, the earlier file ends at its last point key instead (`CompactionOutputs::AddRangeDels()`). Both files then share that user key and are one unit. When a later compaction reads a file of a unit with two or more files, it truncates the file's range tombstones at the bounds of the whole unit, not at the file's own bounds, because truncating at the shared user key could cut a tombstone between two versions of that key.

## The problem

A compaction reads each non-L0 input level through a `LevelIterator`. The `LevelIterator` adds the range tombstones of a file to the aggregator only when it opens that file. A tombstone in a later file of a multi-file unit is therefore not in the aggregator while the compaction is on a key in an earlier file of that unit, but after truncation at the unit bounds it can cover that key. The covered key can be the shared boundary key, a key before it in the earlier file, or a key from another input level whose range overlaps the unit. When such a key is a `TimedPut()` entry, the tombstone check of the swap passes, the entry is swapped to a preferred sequence number below the tombstone, and the tombstone, which goes to the same output, then covers it.

A worked example, reproduced by a new regression test (the sequence numbers are an example):

```
L6: [k6]
writes: DeleteRange [k2, k8) 102, snapshot S1
        TimedPut(k3) 103          preferred seqno 0 (an old write time)
        Put(k4, "old") 104, snapshot S2, Put(k4, "new") 105, Put(k7)
compaction 1 (L0 -> L5, cut between the two versions of k4):
  L5 file A: k3@103 (kTypeValuePreferredSeqno), k4@105
             tombstone dropped: no L6 data under A's range
  L5 file B: k4@104, k7, tombstone [k2, k8)102 kept: L6 has k6
  A and B share the user key k4, so they are one atomic compaction unit
release S1 and S2
compaction 2 (L5 -> L5, not bottommost):
  on k3@103, file B is not open, so the swap check sees no tombstone
  swap: k3@0 (kTypeValue)
  file B is opened; its tombstone, truncated to the unit start k3, covers k3@0
result: Get(k3) returns NotFound, and a later compaction drops k3@0
```

The value is lost even though the tombstone is older than the write. The crash test found it: a key written with `TimedPut()` was missing after recovery, and the MANIFEST showed a `TimedPut()` entry at the end of one L5 file and a range tombstone that starts at the same user key at the start of the next file, before the compaction that lost the value.

The bug needs all of these:
- a `TimedPut()` entry whose preferred sequence number is below the sequence number of an older range tombstone that covers its key;
- that tombstone in a file of a multi-file atomic compaction unit, in a later file than the entry's key position, with the entry's key inside the unit;
- a compaction with level compaction style whose output level is not the bottommost, and where no level below the output holds the key.

Only level compaction swaps below the bottommost level: `KeyNotExistsBeyondOutputLevel()` returns false for other compaction styles unless the compaction is bottommost. At the bottommost level the swap is safe: a tombstone that is older than the entry is also older than the earliest snapshot there, and bottommost compaction output drops such tombstones. A `Put()` is not affected, because it keeps its own sequence number.

Values that were already lost cannot be recovered. This change protects future compactions only.

## The fix

In a compaction that is not bottommost, `CompactionIterator` does not swap a `kTypeValuePreferredSeqno` entry whose user key is inside a multi-file atomic compaction unit of any non-L0 input level. It writes the entry unchanged, through the existing "not safe to swap" path. The entry keeps its own sequence number, which is newer than the tombstone, so the tombstone cannot cover it. It also keeps its packed preferred sequence number, so a later compaction can still swap it. A bottommost compaction swaps as before, because the swap is safe there (see above).

On the first entry that needs them, the iterator builds the user-key ranges of the units that have two or more files, over all non-L0 input levels, and merges them into sorted, disjoint ranges. A compaction without such entries does not build them. Each lookup is one binary search. The ranges come from `Compaction::boundaries()`, the unit bounds that each `LevelIterator` of this compaction passes with a file when it truncates the file's range tombstones. A file is in a multi-file unit when its unit bounds differ from its own smallest or largest key. So two files that only touch at a range tombstone sentinel stay separate units, as in `Compaction::PopulateWithAtomicBoundaries()`. The ranges also include the last file of each unit, where the swap would be safe, to keep the rule simple.

## Changes visible to existing users

Only for `TimedPut()` entries in a compaction that is not bottommost, with a key inside a multi-file atomic compaction unit: the entry is written unchanged until a later compaction swaps it. Until then:
- it keeps its packed preferred sequence number, 8 more bytes in the value;
- it is not passed to the compaction filter, because `InvokeFilterIfNeeded()` skips `kTypeValuePreferredSeqno`;
- it is not moved to a blob file, because `PrepareOutput()` extracts only `kTypeValue`;
- it is not counted in `num_timed_put_swap_preferred_seqno`.

## Paths checked

- L0 inputs: `VersionSet::MakeInputIterator()` opens every L0 input file when it builds the input iterator and adds its range tombstones at that time, so L0 cannot have the gap. The fix skips L0 units.
- Flush: `CompactionIterator` gets no compaction, so the swap branch never runs.
- Level compactions (automatic, manual, `CompactFiles()`) and remote compaction workers build their input in `MakeInputIterator()`, with one `LevelIterator` per non-L0 level. Subcompactions clip that iterator to their key range and can have the gap too. The ranges come from the inputs of the whole compaction, so they cover every subcompaction.
- `MergeHelper::MergeUntil()` and `CompactionOutputs::AddRangeDels()` also read the aggregator. With a tombstone that is loaded late, they can only keep an entry, or leave a copy of the tombstone out of an output file. A file drops its copy only when nothing older that the tombstone covers can remain.
- Other decisions that consult the aggregator do not lower a sequence number below an unseen tombstone. Sequence number zeroing in `PrepareOutput()` runs only at the bottommost level, where the tombstone is dropped as described above. The first tombstone check of the swap branch uses the entry's own sequence number; missing coverage there can only keep a covered entry, and that entry cannot come back to life, because the dropped copy of the tombstone already required that no snapshot needs the older data.

## Possible follow-ups

- Root cause: when the compaction `LevelIterator` opens a file of a multi-file unit, it could also add the range tombstones of the remaining files of that unit to the aggregator. Every reader of the aggregator would then see the full coverage of the unit, at the cost of opening table readers earlier. This change does not touch the `LevelIterator` or tombstone loading.
- A compaction into a level that is not the bottommost could avoid cutting between a point entry and a range tombstone that start at the same user key, as the bottommost branch of `CompactionOutputs::AddToOutput()` already does. That would remove one source of splits, but not cuts between two versions of a point key, and it cannot repair existing files.

Differential Revision: D134698940
…on inputs

Summary:
Background: in universal compaction, a compaction can start with a standalone range deletion file, which is a file that holds only one range tombstone (usually from an ingestion that replaces a key range). When that tombstone is visible to the earliest snapshot, `Compaction::FilterInputsForCompactionIterator()` leaves out each input file of the other input levels that the tombstone fully covers (whole-file filtering, added in PR meta#13078). `Compaction::input_levels(i)` then returns only the kept files, and the compaction reads only those.

The compaction reads each non-L0 input level with a `LevelIterator`. When it opens a file, it cuts the range tombstones of that file to the file's atomic compaction unit boundary. An atomic compaction unit is a run of files in one level that share user keys at their edges and must be compacted together. `Compaction::boundaries(i)` gives one boundary per file, and `LevelIterator` reads it by the file's position in `input_levels(i)`.

The problem: `boundaries(i)` follows the list of all input files of the level, not the list of kept files. After a file is left out, every later kept file in that level reads the boundary of a different (earlier) file. Its range tombstones are cut to that wrong key range and can stop covering keys they should hide. Two cases lose data:
- An older version of a key sits in a level below the kept file, under the kept file's range tombstone. The cut tombstone no longer hides it. If that level is below the output level, reads see the old value again. If that level is a lower input level of the same compaction, the compaction writes the old value to the output, also when the output is the last level. For example, with L5 files ["a", "b"] and ["c", "z"], where the second file holds a tombstone ["d", "e") that hides "d" in L6, a standalone deletion ["a", "d") leaves out the first file. The second file then reads the boundary ["a", "b"], its tombstone is dropped, and Get("d") returns the old L6 value. This happens both when the compaction writes into L5 and when it also reads the L6 file and writes into L6. The tests below reproduce both shapes.
- The kept file itself holds an older version of a key under its own range tombstone, for example because a snapshot kept it when the file was written and was released later. The cut tombstone does not hide that version, so it is written to the output and becomes visible again, also when the output is the last level.
A deleted key comes back in both cases. Keys that an earlier compaction already brought back stay visible; this change does not repair them.

Fix: when `FilterInputsForCompactionIterator()` checks the input files against a standalone range deletion, it also builds, for each input level, the list of boundaries of the kept files, in the same order as `input_levels(i)`. Each kept file keeps the boundary of its own unit, as computed from all input files, which is the same cut the compaction would use without filtering. When a left-out file was in the same unit as a kept file, that boundary is wider than the kept file. The extra key range lies inside the standalone deletion, which is newer than any key the kept file's tombstones could hide there, so the wider cut hides no extra live keys (derived from the code; one test below checks such a case). `boundaries(i)` returns the kept list when this check ran and the original list otherwise. A debug check in the `Compaction` constructor asserts that each non-L0 level has one boundary per file in `input_levels(i)`.

`CompactionIterator::BuildMultiFileUnitRanges()` also reads `boundaries(i)` and `input_levels(i)` by file position. It asserts that the two lists have the same size, and its comment says that both name the same files by position.

Who is affected: universal compaction where a standalone range deletion file starts the compaction and leaves out at least one file before a kept file in the same input level.
- On the primary, only delete-triggered and read-triggered universal compactions filter their inputs. Other universal compactions and `DB::CompactFiles()` on the primary do not. These two kinds read only the start level and the next non-empty level, so the hidden older value always sits below the output level or in the kept file itself.
- A remote compaction worker rebuilds every universal compaction with the earliest snapshot and filters its inputs, so on the worker any universal compaction that starts with a standalone range deletion file can be affected, including one from `DB::CompactFiles()`. Such a compaction can read three or more levels and write into the last level, so the hidden older value can also sit in a lower input level of the same compaction.
- Level and FIFO compaction do not filter inputs.
The fix is in `Compaction`, so it covers the primary and the remote worker.

Compatibility: no format, option or public API change. `boundaries()` has two callers: the compaction input iterator (`VersionSet::MakeInputIterator()`) and `CompactionIterator::BuildMultiFileUnitRanges()`. Both pair it with `input_levels(i)` by position. The subcompaction and stats code in `CompactionJob` uses `input_levels(i)` and does not read boundaries.

Differential Revision: D134907303
Summary:
With `atomic_flush=true`, a retryable error from the directory sync of a flush
can make the DB hang. By default the automatic error recovery hangs. With
`max_bgerror_resume_count=0`, a manual `DB::Resume()` hangs instead. Then
`DB::Close()` hangs too, so the process has to exit. With the WAL off, a crash
can also lose the writes of the failed flush.

Background: `DBImpl::AtomicFlushMemTablesToOutputFiles()` runs one flush job
per column family, then syncs the output directories, and only then installs
all results in one MANIFEST write. If anything fails before the install, the
function rolls back the picked memtables
(`MemTableList::RollbackMemtableFlush()`), so that a later flush can pick them
again. One such flush is the flush of the
automatic error recovery (auto resume by `ErrorHandler`).

The problem: the directory sync runs inside the branch for "all jobs
succeeded". When the sync fails, the function sets the error status there, but
the rollback is in the `else` branch of that check. The later rollbacks run
only when the status is still OK: the one after a failed wait for the install
turn, and the one inside `InstallMemtableAtomicFlushResults()` when the
MANIFEST write fails. So no rollback runs. The picked memtables keep their
"flush in progress" mark, although nothing was installed and the new SST files
are later deleted as obsolete.

This is a regression of
[PR meta#6385](meta#6385) ("Atomic flush
rollback once on failure"), first released in RocksDB 6.8. Before that change,
the rollback ran at the end of the function for any error except a dropped
column family, which covered a failed directory sync. That change moved the
rollback into the `else` branch, to avoid a second rollback after a failed
MANIFEST write.

Which errors trigger the bug:
- A retryable error from the sync becomes a soft error that stops background
  work, and the automatic recovery starts. With `max_bgerror_resume_count=0`,
  it is a hard error instead, and writes stop until `DB::Resume()`. In both
  cases the DB later needs a flush of the stuck memtables.
- A non-retryable error from the sync is reported with
  `BackgroundErrorReason::kFlushNoWAL`, with the WAL on or off. With the
  default `paranoid_checks=true`, it is a fatal error, and `DB::Resume()`
  fails at once. The stuck memtables do not change that result.
- A failed flush job, or a failed MANIFEST write, is not affected: those paths
  roll back already. Non-atomic flushes sync the directory inside the flush
  job and roll back on error.

Effect of the stuck memtables:
- Later flushes skip that column family while it has no newer immutable
  memtable. A later atomic flush can still install the other column families
  and write a larger last sequence number to the MANIFEST. The automatic error
  recovery does this: it requests a flush of every column family with
  immutable memtables, but the stuck column family is skipped when the flush
  job picks memtables.
- The recovery then waits forever for the stuck memtable to be flushed
  (`WaitForFlushMemTables()` in `DBImpl::ResumeImpl()`). `DB::Close()` waits
  for the recovery: it joins the automatic recovery thread, or waits while a
  manual `DB::Resume()` is in progress. So `DB::Close()` hangs too.
- The stuck column family can never flush again. When a newer memtable is
  sealed, the next atomic flush picks only the newer one and then waits
  forever to install it, because the stuck memtable is older. Immutable
  memtables pile up until writes stop.
- With the WAL off (`WriteOptions::disableWAL`), a crash then loses the writes
  in the stuck memtables, while newer writes to other column families
  survive. That breaks the atomic flush promise that `DB::Open()` recovery
  without a WAL gives a consistent point in time across column families. With
  the WAL on, no data is lost, because the stuck column family keeps its old
  log number and `DB::Open()` replays its writes from the WAL.

A crash test found this with many small atomic flushes and injected metadata
write errors, with the WAL off.

Fix: run the rollback whenever the status is an error that is not
`ShutdownInProgress`, also when the directory sync set the error. The rollback
code itself is unchanged. A small side effect: when a flush job returned
`ShutdownInProgress` and the directory sync then fails, the rollback runs.
That is harmless, because the DB is closing.

Compatibility: no format, option or public API change. Only the error path of
atomic flush changes.

Differential Revision: D134907304
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.
  - When the commit of a compaction fails while `versions_->io_status()` is not
    OK, `CompactionJob::Install()` takes the error of that commit as the job's
    I/O status, not `versions_->io_status()`, which keeps the first MANIFEST
    write error and can be older. So a retryable failure of the commit stays a
    hard error, and `DB::Resume()` recovers. 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.
  If the direct clear fails after a close started (the close can start while the
  MANIFEST write or the purge releases the DB mutex), it returns
  `ShutdownInProgress` (or the fatal background error, if there is one), as
  `DBImpl::ResumeImpl()` does. That ends the recovery,
  so the close does not wait for a later retry by the SstFileManager, which may
  not come while the disk stays full. A foreground MANIFEST write, for example a
  failed `PromoteL0()`, can also fail in those windows. It sets no background
  error, and its files stay quarantined until a later new MANIFEST. So the debug
  assert in `ErrorHandler::ClearBGError()` allows a quarantine that is not empty
  while `versions_->io_status()` holds an error.
  `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 cases above (an atomic flush that does not wait, a compaction that
  fails before its commit or in its own 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 its own `Corruption`, but
    with reason `kManifestWrite`, because `DBImpl::BackgroundCompaction()` picks
    the reason from `versions_->io_status()`. The outputs of that compaction
    were never committed.
  - The forced empty edit of `DBImpl::ResumeImpl()` and of the direct clear can
    be grouped with other writers in one `ProcessManifestWrites()` batch. Then
    the failure of another writer in the batch becomes the status of the forced
    edit. For example, `Corruption` from `LoadTableHandlers()` under
    `paranoid_checks` is then set with reason `kManifestWrite`, which makes it
    fatal. This is more likely during the direct clear, because background work
    keeps committing while a soft error is set.
  - 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 each
  caller report the error of its own batch. 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.
  - When the commit of a compaction fails while `versions_->io_status()` is not
    OK, `CompactionJob::Install()` takes the error of that commit as the job's
    I/O status, not `versions_->io_status()`, which keeps the first MANIFEST
    write error and can be older. So a retryable failure of the commit stays a
    hard error, and `DB::Resume()` recovers. 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.
  If the direct clear fails after a close started (the close can start while the
  MANIFEST write or the purge releases the DB mutex), it returns
  `ShutdownInProgress` (or the fatal background error, if there is one), as
  `DBImpl::ResumeImpl()` does. That ends the recovery,
  so the close does not wait for a later retry by the SstFileManager, which may
  not come while the disk stays full. A foreground MANIFEST write, for example a
  failed `PromoteL0()`, can also fail in those windows. It sets no background
  error, and its files stay quarantined until a later new MANIFEST. So the debug
  assert in `ErrorHandler::ClearBGError()` allows a quarantine that is not empty
  while `versions_->io_status()` holds an error.
  `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 cases above (an atomic flush that does not wait, a compaction that
  fails before its commit or in its own 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 its own `Corruption`, but
    with reason `kManifestWrite`, because `DBImpl::BackgroundCompaction()` picks
    the reason from `versions_->io_status()`. The outputs of that compaction
    were never committed.
  - The forced empty edit of `DBImpl::ResumeImpl()` and of the direct clear can
    be grouped with other writers in one `ProcessManifestWrites()` batch. Then
    the failure of another writer in the batch becomes the status of the forced
    edit. For example, `Corruption` from `LoadTableHandlers()` under
    `paranoid_checks` is then set with reason `kManifestWrite`, which makes it
    fatal. This is more likely during the direct clear, because background work
    keeps committing while a soft error is set.
  - 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 each
  caller report the error of its own batch. 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.
  - When the commit of a compaction fails while `versions_->io_status()` is not
    OK, `CompactionJob::Install()` takes the error of that commit as the job's
    I/O status, not `versions_->io_status()`, which keeps the first MANIFEST
    write error and can be older. So a retryable failure of the commit stays a
    hard error, and `DB::Resume()` recovers. 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.
  If the direct clear fails after a close started (the close can start while the
  MANIFEST write or the purge releases the DB mutex), it returns
  `ShutdownInProgress` (or the fatal background error, if there is one), as
  `DBImpl::ResumeImpl()` does. That ends the recovery,
  so the close does not wait for a later retry by the SstFileManager, which may
  not come while the disk stays full. A foreground MANIFEST write, for example a
  failed `PromoteL0()`, can also fail in those windows. It sets no background
  error, and its files stay quarantined until a later new MANIFEST. So the debug
  assert in `ErrorHandler::ClearBGError()` allows a quarantine that is not empty
  while `versions_->io_status()` holds an error.
  `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 cases above (an atomic flush that does not wait, a compaction that
  fails before its commit or in its own 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 its own `Corruption`, but
    with reason `kManifestWrite`, because `DBImpl::BackgroundCompaction()` picks
    the reason from `versions_->io_status()`. The outputs of that compaction
    were never committed.
  - The forced empty edit of `DBImpl::ResumeImpl()` and of the direct clear can
    be grouped with other writers in one `ProcessManifestWrites()` batch. Then
    the failure of another writer in the batch becomes the status of the forced
    edit. For example, `Corruption` from `LoadTableHandlers()` under
    `paranoid_checks` is then set with reason `kManifestWrite`, which makes it
    fatal. This is more likely during the direct clear, because background work
    keeps committing while a soft error is set.
  - 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 each
  caller report the error of its own batch. 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.
  - When the commit of a compaction fails while `versions_->io_status()` is not
    OK, `CompactionJob::Install()` takes the error of that commit as the job's
    I/O status, not `versions_->io_status()`, which keeps the first MANIFEST
    write error and can be older. So a retryable failure of the commit stays a
    hard error, and `DB::Resume()` recovers. 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.
  If the direct clear fails after a close started (the close can start while the
  MANIFEST write or the purge releases the DB mutex), it returns
  `ShutdownInProgress` (or the fatal background error, if there is one), as
  `DBImpl::ResumeImpl()` does. That ends the recovery,
  so the close does not wait for a later retry by the SstFileManager, which may
  not come while the disk stays full. A foreground MANIFEST write, for example a
  failed `PromoteL0()`, can also fail in those windows. It sets no background
  error, and its files stay quarantined until a later new MANIFEST. So the debug
  assert in `ErrorHandler::ClearBGError()` allows a quarantine that is not empty
  while `versions_->io_status()` holds an error.
  `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 cases above (an atomic flush that does not wait, a compaction that
  fails before its commit or in its own 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 its own `Corruption`, but
    with reason `kManifestWrite`, because `DBImpl::BackgroundCompaction()` picks
    the reason from `versions_->io_status()`. The outputs of that compaction
    were never committed.
  - The forced empty edit of `DBImpl::ResumeImpl()` and of the direct clear can
    be grouped with other writers in one `ProcessManifestWrites()` batch. Then
    the failure of another writer in the batch becomes the status of the forced
    edit. For example, `Corruption` from `LoadTableHandlers()` under
    `paranoid_checks` is then set with reason `kManifestWrite`, which makes it
    fatal. This is more likely during the direct clear, because background work
    keeps committing while a soft error is set.
  - 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 each
  caller report the error of its own batch. 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.
  - When the commit of a compaction fails while `versions_->io_status()` is not
    OK, `CompactionJob::Install()` takes the error of that commit as the job's
    I/O status, not `versions_->io_status()`, which keeps the first MANIFEST
    write error and can be older. So a retryable failure of the commit stays a
    hard error, and `DB::Resume()` recovers. 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.
  If the direct clear fails after a close started (the close can start while the
  MANIFEST write or the purge releases the DB mutex), it returns
  `ShutdownInProgress` (or the fatal background error, if there is one), as
  `DBImpl::ResumeImpl()` does. That ends the recovery,
  so the close does not wait for a later retry by the SstFileManager, which may
  not come while the disk stays full. A foreground MANIFEST write, for example a
  failed `PromoteL0()`, can also fail in those windows. It sets no background
  error, and its files stay quarantined until a later new MANIFEST. So the debug
  assert in `ErrorHandler::ClearBGError()` allows a quarantine that is not empty
  while `versions_->io_status()` holds an error.
  `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 cases above (an atomic flush that does not wait, a compaction that
  fails before its commit or in its own 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 its own `Corruption`, but
    with reason `kManifestWrite`, because `DBImpl::BackgroundCompaction()` picks
    the reason from `versions_->io_status()`. The outputs of that compaction
    were never committed.
  - The forced empty edit of `DBImpl::ResumeImpl()` and of the direct clear can
    be grouped with other writers in one `ProcessManifestWrites()` batch. Then
    the failure of another writer in the batch becomes the status of the forced
    edit. For example, `Corruption` from `LoadTableHandlers()` under
    `paranoid_checks` is then set with reason `kManifestWrite`, which makes it
    fatal. This is more likely during the direct clear, because background work
    keeps committing while a soft error is set.
  - 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 each
  caller report the error of its own batch. 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.
  - When the commit of a compaction fails while `versions_->io_status()` is not
    OK, `CompactionJob::Install()` takes the error of that commit as the job's
    I/O status, not `versions_->io_status()`, which keeps the first MANIFEST
    write error and can be older. So a retryable failure of the commit stays a
    hard error, and `DB::Resume()` recovers. 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.
  If the direct clear fails after a close started (the close can start while the
  MANIFEST write or the purge releases the DB mutex), it returns
  `ShutdownInProgress` (or the fatal background error, if there is one), as
  `DBImpl::ResumeImpl()` does. That ends the recovery,
  so the close does not wait for a later retry by the SstFileManager, which may
  not come while the disk stays full. A foreground MANIFEST write, for example a
  failed `PromoteL0()`, can also fail in those windows. It sets no background
  error, and its files stay quarantined until a later new MANIFEST. So the debug
  assert in `ErrorHandler::ClearBGError()` allows a quarantine that is not empty
  while `versions_->io_status()` holds an error.
  `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 cases above (an atomic flush that does not wait, a compaction that
  fails before its commit or in its own 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 its own `Corruption`, but
    with reason `kManifestWrite`, because `DBImpl::BackgroundCompaction()` picks
    the reason from `versions_->io_status()`. The outputs of that compaction
    were never committed.
  - The forced empty edit of `DBImpl::ResumeImpl()` and of the direct clear can
    be grouped with other writers in one `ProcessManifestWrites()` batch. Then
    the failure of another writer in the batch becomes the status of the forced
    edit. For example, `Corruption` from `LoadTableHandlers()` under
    `paranoid_checks` is then set with reason `kManifestWrite`, which makes it
    fatal. This is more likely during the direct clear, because background work
    keeps committing while a soft error is set.
  - 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 each
  caller report the error of its own batch. 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.
  - When the commit of a compaction fails while `versions_->io_status()` is not
    OK, `CompactionJob::Install()` takes the error of that commit as the job's
    I/O status, not `versions_->io_status()`, which keeps the first MANIFEST
    write error and can be older. So a retryable failure of the commit stays a
    hard error, and `DB::Resume()` recovers. 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.
  If the direct clear fails after a close started (the close can start while the
  MANIFEST write or the purge releases the DB mutex), it returns
  `ShutdownInProgress` (or the fatal background error, if there is one), as
  `DBImpl::ResumeImpl()` does. That ends the recovery,
  so the close does not wait for a later retry by the SstFileManager, which may
  not come while the disk stays full. A foreground MANIFEST write, for example a
  failed `PromoteL0()`, can also fail in those windows. It sets no background
  error, and its files stay quarantined until a later new MANIFEST. So the debug
  assert in `ErrorHandler::ClearBGError()` allows a quarantine that is not empty
  while `versions_->io_status()` holds an error.
  `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 cases above (an atomic flush that does not wait, a compaction that
  fails before its commit or in its own 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 its own `Corruption`, but
    with reason `kManifestWrite`, because `DBImpl::BackgroundCompaction()` picks
    the reason from `versions_->io_status()`. The outputs of that compaction
    were never committed.
  - The forced empty edit of `DBImpl::ResumeImpl()` and of the direct clear can
    be grouped with other writers in one `ProcessManifestWrites()` batch. Then
    the failure of another writer in the batch becomes the status of the forced
    edit. For example, `Corruption` from `LoadTableHandlers()` under
    `paranoid_checks` is then set with reason `kManifestWrite`, which makes it
    fatal. This is more likely during the direct clear, because background work
    keeps committing while a soft error is set.
  - 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 each
  caller report the error of its own batch. 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.
  - When the commit of a compaction fails while `versions_->io_status()` is not
    OK, `CompactionJob::Install()` takes the error of that commit as the job's
    I/O status, not `versions_->io_status()`, which keeps the first MANIFEST
    write error and can be older. So a retryable failure of the commit stays a
    hard error, and `DB::Resume()` recovers. 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.
  If the direct clear fails after a close started (the close can start while the
  MANIFEST write or the purge releases the DB mutex), it returns
  `ShutdownInProgress` (or the fatal background error, if there is one), as
  `DBImpl::ResumeImpl()` does. That ends the recovery,
  so the close does not wait for a later retry by the SstFileManager, which may
  not come while the disk stays full. A foreground MANIFEST write, for example a
  failed `PromoteL0()`, can also fail in those windows. It sets no background
  error, and its files stay quarantined until a later new MANIFEST. So the debug
  assert in `ErrorHandler::ClearBGError()` allows a quarantine that is not empty
  while `versions_->io_status()` holds an error.
  `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 cases above (an atomic flush that does not wait, a compaction that
  fails before its commit or in its own 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 its own `Corruption`, but
    with reason `kManifestWrite`, because `DBImpl::BackgroundCompaction()` picks
    the reason from `versions_->io_status()`. The outputs of that compaction
    were never committed.
  - The forced empty edit of `DBImpl::ResumeImpl()` and of the direct clear can
    be grouped with other writers in one `ProcessManifestWrites()` batch. Then
    the failure of another writer in the batch becomes the status of the forced
    edit. For example, `Corruption` from `LoadTableHandlers()` under
    `paranoid_checks` is then set with reason `kManifestWrite`, which makes it
    fatal. This is more likely during the direct clear, because background work
    keeps committing while a soft error is set.
  - 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 each
  caller report the error of its own batch. 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.
  - When the commit of a compaction fails while `versions_->io_status()` is not
    OK, `CompactionJob::Install()` takes the error of that commit as the job's
    I/O status, not `versions_->io_status()`, which keeps the first MANIFEST
    write error and can be older. So a retryable failure of the commit stays a
    hard error, and `DB::Resume()` recovers. 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.
  If the direct clear fails after a close started (the close can start while the
  MANIFEST write or the purge releases the DB mutex), it returns
  `ShutdownInProgress` (or the fatal background error, if there is one), as
  `DBImpl::ResumeImpl()` does. That ends the recovery,
  so the close does not wait for a later retry by the SstFileManager, which may
  not come while the disk stays full. A foreground MANIFEST write, for example a
  failed `PromoteL0()`, can also fail in those windows. It sets no background
  error, and its files stay quarantined until a later new MANIFEST. So the debug
  assert in `ErrorHandler::ClearBGError()` allows a quarantine that is not empty
  while `versions_->io_status()` holds an error.
  `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 cases above (an atomic flush that does not wait, a compaction that
  fails before its commit or in its own 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 its own `Corruption`, but
    with reason `kManifestWrite`, because `DBImpl::BackgroundCompaction()` picks
    the reason from `versions_->io_status()`. The outputs of that compaction
    were never committed.
  - The forced empty edit of `DBImpl::ResumeImpl()` and of the direct clear can
    be grouped with other writers in one `ProcessManifestWrites()` batch. Then
    the failure of another writer in the batch becomes the status of the forced
    edit. For example, `Corruption` from `LoadTableHandlers()` under
    `paranoid_checks` is then set with reason `kManifestWrite`, which makes it
    fatal. This is more likely during the direct clear, because background work
    keeps committing while a soft error is set.
  - 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 each
  caller report the error of its own batch. 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.
  - When the commit of a compaction fails while `versions_->io_status()` is not
    OK, `CompactionJob::Install()` takes the error of that commit as the job's
    I/O status, not `versions_->io_status()`, which keeps the first MANIFEST
    write error and can be older. So a retryable failure of the commit stays a
    hard error, and `DB::Resume()` recovers. 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.
  If the direct clear fails after a close started (the close can start while the
  MANIFEST write or the purge releases the DB mutex), it returns
  `ShutdownInProgress` (or the fatal background error, if there is one), as
  `DBImpl::ResumeImpl()` does. That ends the recovery,
  so the close does not wait for a later retry by the SstFileManager, which may
  not come while the disk stays full. A foreground MANIFEST write, for example a
  failed `PromoteL0()`, can also fail in those windows. It sets no background
  error, and its files stay quarantined until a later new MANIFEST. So the debug
  assert in `ErrorHandler::ClearBGError()` allows a quarantine that is not empty
  while `versions_->io_status()` holds an error.
  `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 cases above (an atomic flush that does not wait, a compaction that
  fails before its commit or in its own 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 its own `Corruption`, but
    with reason `kManifestWrite`, because `DBImpl::BackgroundCompaction()` picks
    the reason from `versions_->io_status()`. The outputs of that compaction
    were never committed.
  - The forced empty edit of `DBImpl::ResumeImpl()` and of the direct clear can
    be grouped with other writers in one `ProcessManifestWrites()` batch. Then
    the failure of another writer in the batch becomes the status of the forced
    edit. For example, `Corruption` from `LoadTableHandlers()` under
    `paranoid_checks` is then set with reason `kManifestWrite`, which makes it
    fatal. This is more likely during the direct clear, because background work
    keeps committing while a soft error is set.
  - 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 each
  caller report the error of its own batch. 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.
  - When the commit of a compaction fails while `versions_->io_status()` is not
    OK, `CompactionJob::Install()` takes the error of that commit as the job's
    I/O status, not `versions_->io_status()`, which keeps the first MANIFEST
    write error and can be older. So a retryable failure of the commit stays a
    hard error, and `DB::Resume()` recovers. 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.
  If the direct clear fails after a close started (the close can start while the
  MANIFEST write or the purge releases the DB mutex), it returns
  `ShutdownInProgress` (or the fatal background error, if there is one), as
  `DBImpl::ResumeImpl()` does. That ends the recovery,
  so the close does not wait for a later retry by the SstFileManager, which may
  not come while the disk stays full. A foreground MANIFEST write, for example a
  failed `PromoteL0()`, can also fail in those windows. It sets no background
  error, and its files stay quarantined until a later new MANIFEST. So the debug
  assert in `ErrorHandler::ClearBGError()` allows a quarantine that is not empty
  while `versions_->io_status()` holds an error.
  `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 cases above (an atomic flush that does not wait, a compaction that
  fails before its commit or in its own 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 its own `Corruption`, but
    with reason `kManifestWrite`, because `DBImpl::BackgroundCompaction()` picks
    the reason from `versions_->io_status()`. The outputs of that compaction
    were never committed.
  - The forced empty edit of `DBImpl::ResumeImpl()` and of the direct clear can
    be grouped with other writers in one `ProcessManifestWrites()` batch. Then
    the failure of another writer in the batch becomes the status of the forced
    edit. For example, `Corruption` from `LoadTableHandlers()` under
    `paranoid_checks` is then set with reason `kManifestWrite`, which makes it
    fatal. This is more likely during the direct clear, because background work
    keeps committing while a soft error is set.
  - 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 each
  caller report the error of its own batch. 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.
  - When the commit of a compaction fails while `versions_->io_status()` is not
    OK, `CompactionJob::Install()` takes the error of that commit as the job's
    I/O status, not `versions_->io_status()`, which keeps the first MANIFEST
    write error and can be older. So a retryable failure of the commit stays a
    hard error, and `DB::Resume()` recovers. 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.
  If the direct clear fails after a close started (the close can start while the
  MANIFEST write or the purge releases the DB mutex), it returns
  `ShutdownInProgress` (or the fatal background error, if there is one), as
  `DBImpl::ResumeImpl()` does. That ends the recovery,
  so the close does not wait for a later retry by the SstFileManager, which may
  not come while the disk stays full. A foreground MANIFEST write, for example a
  failed `PromoteL0()`, can also fail in those windows. It sets no background
  error, and its files stay quarantined until a later new MANIFEST. So the debug
  assert in `ErrorHandler::ClearBGError()` allows a quarantine that is not empty
  while `versions_->io_status()` holds an error.
  `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 cases above (an atomic flush that does not wait, a compaction that
  fails before its commit or in its own 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 its own `Corruption`, but
    with reason `kManifestWrite`, because `DBImpl::BackgroundCompaction()` picks
    the reason from `versions_->io_status()`. The outputs of that compaction
    were never committed.
  - The forced empty edit of `DBImpl::ResumeImpl()` and of the direct clear can
    be grouped with other writers in one `ProcessManifestWrites()` batch. Then
    the failure of another writer in the batch becomes the status of the forced
    edit. For example, `Corruption` from `LoadTableHandlers()` under
    `paranoid_checks` is then set with reason `kManifestWrite`, which makes it
    fatal. This is more likely during the direct clear, because background work
    keeps committing while a soft error is set.
  - 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 each
  caller report the error of its own batch. 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.
  - When the commit of a compaction fails while `versions_->io_status()` is not
    OK, `CompactionJob::Install()` takes the error of that commit as the job's
    I/O status, not `versions_->io_status()`, which keeps the first MANIFEST
    write error and can be older. So a retryable failure of the commit stays a
    hard error, and `DB::Resume()` recovers. 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.
  If the direct clear fails after a close started (the close can start while the
  MANIFEST write or the purge releases the DB mutex), it returns
  `ShutdownInProgress` (or the fatal background error, if there is one), as
  `DBImpl::ResumeImpl()` does. That ends the recovery,
  so the close does not wait for a later retry by the SstFileManager, which may
  not come while the disk stays full. A foreground MANIFEST write, for example a
  failed `PromoteL0()`, can also fail in those windows. It sets no background
  error, and its files stay quarantined until a later new MANIFEST. So the debug
  assert in `ErrorHandler::ClearBGError()` allows a quarantine that is not empty
  while `versions_->io_status()` holds an error.
  `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 cases above (an atomic flush that does not wait, a compaction that
  fails before its commit or in its own 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 its own `Corruption`, but
    with reason `kManifestWrite`, because `DBImpl::BackgroundCompaction()` picks
    the reason from `versions_->io_status()`. The outputs of that compaction
    were never committed.
  - The forced empty edit of `DBImpl::ResumeImpl()` and of the direct clear can
    be grouped with other writers in one `ProcessManifestWrites()` batch. Then
    the failure of another writer in the batch becomes the status of the forced
    edit. For example, `Corruption` from `LoadTableHandlers()` under
    `paranoid_checks` is then set with reason `kManifestWrite`, which makes it
    fatal. This is more likely during the direct clear, because background work
    keeps committing while a soft error is set.
  - 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 each
  caller report the error of its own batch. 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

This branch has not been deployed

No deployments
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.

1 participant