Skip to content

Reload the plain-rewritable filesystem when a rollback does not complete - #118623

Closed
CheSema wants to merge 1 commit into
masterfrom
fix-plain-rewritable-partial-rollback
Closed

CheSema wants to merge 1 commit into
masterfrom
fix-plain-rewritable-partial-rollback

Conversation

@CheSema

@CheSema CheSema commented Sep 7, 2026 •

Copy link
Copy Markdown
Member

Related: #118615

Reversing a directory move rewrites one prefix.path marker per directory, and each of those is a separate object storage write. MetadataOperationsHolder::rollback stops at the first undo() that throws, so the operations it never reached keep the writes they already made. The in-memory filesystem is unaffected, because a failed commit never publishes its snapshot — and the result is that object storage describes a filesystem this process does not have.

Nothing reconciled the two. PARTIALLY_ROLLED_BACK was recorded and then never read, so the only trace was a sentence appended to the exception, and the disagreement survived until the filesystem was next rebuilt from object storage. For a MergeTree part rename that means the merge reports failure while the directory is left under its committed name, and a later restart loads a part the server never committed.

This reloads the filesystem from object storage when the rollback did not complete. Note it does not undo the move: once the rollback has failed, the pre-move state is gone from object storage, so the guarantee is that the in-memory filesystem matches what is actually there. The reload runs only after a transaction has already failed, and serving a filesystem known to disagree with object storage is worse than listing the disk again.

The test needs a fault that fires only while undoing, hence the new failpoint: the existing plain_object_storage_write_fail_on_directory_move sits in rewriteSingleDirectory, which the forward pass calls first, so it can never leave a move half reversed.

Open question for reviewers. The recovery is a full disk relist (dropCache). It only fires after a rollback has already failed, so it should be rare, but on a large plain_rewritable disk it is a LIST storm at an already-bad moment. The cheaper alternative is a "needs reload" flag consumed by the next refresh(), but refresh() is not guaranteed to run, so the stale window would be unbounded. I chose correctness; happy to switch if you prefer bounded cost.

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fixed plain_rewritable disks continuing to serve a filesystem that disagrees with object storage after a metadata transaction failed to roll back completely. The disagreement previously surfaced only after a restart, when a directory could appear under a name the server never committed.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Workflow [PR]
Sync PR [sync-upstream/pr/118623]

Reversing a directory move rewrites one prefix.path marker per directory, and each
of those is a separate object storage write. MetadataOperationsHolder::rollback
stops at the first undo that throws, so the operations it never reached keep the
writes they already made. The in-memory filesystem is unaffected, because a failed
commit never publishes its snapshot, and the result is that object storage
describes a filesystem this process does not have.

Nothing reconciled the two. PARTIALLY_ROLLED_BACK was recorded and then never read,
so the only trace was a sentence appended to the exception, and the disagreement
survived until the filesystem was next rebuilt from object storage. For a MergeTree
part rename that means the merge reports failure while the directory is left under
its committed name, and a later restart loads a part the server never committed.

Reload the filesystem from object storage when the rollback did not complete. It
runs only after a transaction has already failed, and serving a filesystem that is
known to disagree with object storage is worse than listing the disk again.

The test needs a fault that fires only while undoing, because the existing
plain_object_storage_write_fail_on_directory_move sits in rewriteSingleDirectory,
which the forward pass calls first - so it can never leave a move half reversed.
@clickhouse-gh

clickhouse-gh Bot commented Sep 7, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [f36216b]

Summary: ❌

job_name test_name status info comment
Stress test (arm_tsan) FAIL
Cannot start clickhouse-server FAIL cidb
Check failed FAIL cidb
Build profile diff ERROR

AI Review

Summary

The PR teaches plain_rewritable metadata transactions to reload their in-memory filesystem from object storage after a rollback stops halfway through. That direction is correct, but the current implementation still leaves a window where another transaction can commit against the stale pre-reload tree, so I do not think this is safe to merge yet.

Findings
  • ❌ Blocker — [src/Disks/DiskObjectStorage/MetadataStorages/PlainRewritable/MetadataStorageFromPlainRewritableObjectStorage.cpp:438] The forced reload happens only after the metadata_mutex guard from line 409 has already been unwound. A second plain_rewritable transaction can therefore enter commit, clone the stale fs tree, satisfy Preconditions::runChecks against that stale snapshot, and publish more metadata before dropCache() reacquires the mutex. In the normal DiskObjectStorageTransaction::writeFile() path the blob key is already chosen from generateObjectKeyForPath(), so a write to A/x in that window can physically land under the directory whose prefix.path now says MOVED/, then "teleport" to MOVED/x after the delayed reload. Suggested fix: keep later commits fenced until the relist completes, or mark the disk unusable once partial rollback is detected.
Final Verdict

Changes requested.

LLVM Coverage Report

Measured on commit f36216b.

Metric Baseline Current Δ
Lines 88.80% 88.80% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 81.10% 81.10% +0.00%

Changed lines: Changed C/C++ lines covered: 58/62 (93.55%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Sep 7, 2026

try
{
metadata_storage.dropCache();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

dropCache() runs after the metadata_mutex guard from line 409 has already been unwound, so another plain_rewritable commit can slip in before the reload and validate itself against the stale fs snapshot. In the normal DiskObjectStorageTransaction::writeFile() path the blob key was already chosen via generateObjectKeyForPath(), so a write to A/x in that window can succeed against the remote directory whose prefix.path already says MOVED/, and only later reappear as MOVED/x after this relist. The recovery path needs to keep later commits out until the reload finishes (or fence the disk), otherwise partial rollback still corrupts subsequent transactions.

@clickhouse-gh clickhouse-gh Bot added the comp-object-storage-disks Object-storage disk layer: DiskObjectStorage, metadata storage adapters, transaction support for ... label Sep 7, 2026
@CheSema

CheSema commented Sep 8, 2026

Copy link
Copy Markdown
Member Author

That approach not better. We still has difference between memory and s3 state which we do not know.
I think we need just infinite retry on rollback.

@CheSema

CheSema commented Sep 10, 2026

Copy link
Copy Markdown
Member Author

Closing this in favour of a different fix.

Reloading the filesystem after a failed rollback does make the in-memory state agree with object storage, but it adopts a half-moved state that no transaction ever committed. At the point where the rollback stopped we do not know what object storage holds, so every decision taken from that state can be wrong, and adopting it is one such decision. The fence hole the review found has the same root.

The replacement retries every stage of undo until it succeeds, so a transaction cannot report failure while object storage still holds part of it. A stage that already succeeded never runs again, which is what MetadataStorageFromPlainObjectStorageMoveFileOperation::undo needs. Shutdown is the only exit, and the next start rebuilds the filesystem from object storage anyway.

I will link the replacement pull request here.

@CheSema

CheSema commented Sep 10, 2026

Copy link
Copy Markdown
Member Author

Replaced by #119235

CheSema added a commit that referenced this pull request Sep 11, 2026
A reversal that a shutdown abandons releases the metadata lock, and the
transactions that were waiting for it resume at once. They decide what to write
from a filesystem that object storage no longer matches, which is the state this
branch exists to prevent - so the disk now takes no further transaction after
that happens.

The mark is set while the commit still holds the lock, and it is checked right
after the lock is taken, because a transaction that was already waiting is past
the point where it was created. Reads are still served: the filesystem in memory
is the state that was committed, and a file resolves to the blob it did before.
`refresh` and `dropCache` stop reloading, since reloading would adopt a state no
transaction ever committed. Nothing here can make the two agree again, so this
lasts until the next start, which loads the filesystem from object storage.

`MetadataOperationsHolder::isPartiallyRolledBack` reports the state that decides
this. It is the one piece worth keeping from
#118623, which detected the same
thing and then reloaded the filesystem to match object storage.

Also fixes what CI reported on the tests: an override must not repeat the
default arguments of the method it overrides, and a `catch (...)` needs to say
that swallowing is intended.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
pull Bot pushed a commit to sowelswl/ClickHouse that referenced this pull request Sep 17, 2026
Reversing a failed `plain_rewritable` metadata transaction is not atomic. Each
object storage write is separate, and `MetadataOperationsHolder::rollback`
stops at the first `undo` that throws. The writes the reversal never reached
stay in object storage, while the in-memory filesystem still describes the
state before the transaction, because a failed commit never publishes its
snapshot. Nothing reconciles the two until the filesystem is rebuilt from
object storage, so a `MergeTree` part directory can turn up under a name the
server never committed.

The state of object storage is unknown at that point, so no decision taken
from it can be trusted, including the decision to accept it. Every stage of an
`undo` now repeats until object storage accepts it, and a stage that succeeded
never repeats. `MetadataStorageFromPlainObjectStorageMoveFileOperation` needs
the second half of that: it drops a temporary copy once the blob the copy holds
is back under its own key, so a retry that started the reversal over would copy
from an object an earlier stage has already removed.

A commit holds `metadata_mutex`, and so do `refresh` and `dropCache`, so no
other transaction and no reload can read the filesystem of this disk while a
reversal is still in progress.

A shutdown is the only exit. The retries stop, the transaction reports its
failure, and the next start loads the filesystem from object storage.

The retries live in the plain-rewritable operations, so `MetadataOperationsHolder`
and the other metadata storages keep their current behaviour.

Related: ClickHouse#118623

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-object-storage-disks Object-storage disk layer: DiskObjectStorage, metadata storage adapters, transaction support for ... pr-bugfix Pull request with bugfix, not backported by default

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant