Skip to content

Do not keep a ReplicatedMergeTree table read-only after a restart because of a part already copied to detached/ - #133436

Merged
alexey-milovidov merged 3 commits into
ClickHouse:masterfrom
groeneai:fix-rmt-detached-copy-after-restart
Nov 22, 2026
Merged

alexey-milovidov merged 3 commits into
ClickHouse:masterfrom
groeneai:fix-rmt-detached-copy-after-restart

Conversation

@groeneai

@groeneai groeneai commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator

Closes: #133415

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):

A ReplicatedMergeTree table no longer stays read-only with TOO_MANY_UNEXPECTED_DATA_PARTS after a restart because of parts that had already been copied to detached/ as broken, covered-by-broken, unexpected or clone and were not cleaned up yet: at startup such a part is removed when all its files are hard links of its copy in detached/. Closes #133415.

Description

On 26.10.3.7 (#133415), restarting a server while a part that the part check, CHECK TABLE or a lost-replica clone had copied to detached/broken_<name> (or covered-by-broken_, unexpected_, clone_) is still on disk leaves the ReplicatedMergeTree table read-only on every restart with TOO_MANY_UNEXPECTED_DATA_PARTS, while SELECTs return only the remaining parts.

Root cause: these four paths hard-link the part into detached/ and make it Outdated in memory only; cleanup removes the directory later. After a restart the directory is listed again and is no longer in Keeper, and checkPartsImpl recognises such leftovers only through a covering part (a local expected part, or the empty part that DROP/DETACH write), which these paths do not create. So it counts as an uncovered unexpected part against replicated_max_ratio_of_wrong_parts.

Change: checkPartsImpl indexes the detached/ entries with these four prefixes once. An unexpected part whose directory has exactly the files of such a copy, each the same inode (existing IDisk::stat), is not counted and is removed instead of being renamed to ignored_<name>; its data stays in the detached copy. An incomplete copy, other prefixes, disks without stat and zero-copy clones (full copies) keep today's behaviour. Parts still listed in Keeper load as before: the issue proposed filtering at load time, but a covered-by-broken source part is still in Keeper then and can be the only copy of its data.

Validation: the new integration test covers all four prefixes (broken, with the part refetched from the other replica, covered-by-broken, unexpected, clone) and three copies that must still leave the table read-only: one misses a file, one has a file that is a copy, not a hard link, one has no prefix. On master the four positive cases fail with the issue's error; with the fix all five pass (3 runs).


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

Version info

  • Merged into: 26.12.1.156-master (included in 26.12 and later)

groeneai and others added 3 commits November 21, 2026 18:27
…lready copied to detached/

`makeCloneInDetached` hard-links a part into `detached/<reason>_<name>`,
and four callers then make the original Outdated in memory only:
`removePartAndEnqueueFetch` (`broken`, `covered-by-broken`),
`outdateUnexpectedPartAndCloneToDetached` (`unexpected`) and
`cloneReplica` (`clone`). The directory is removed later by the cleanup
thread. If the server restarts first, the directory is loaded again; it is
not in Keeper any more, so it is an unexpected part, and `checkPartsImpl`
recognises such leftovers only through a covering part (a local expected
part, or the empty part that DROP/DETACH write for this reason). The four
clone paths create no covering part, so the leftover counts as an uncovered
unexpected part against `replicated_max_ratio_of_wrong_parts`, and the table
stays read-only with TOO_MANY_UNEXPECTED_DATA_PARTS on every restart.

`checkPartsImpl` now lists `detached/` once (`getDetachedParts`) when there
are unexpected parts, and indexes the entries with one of the four prefixes
by disk and part info (`_tryN` is stripped by `parseDetachedPartName`). An
unexpected part whose directory has exactly the files of such a copy,
recursively and each the same device and inode (`IDisk::stat`), is not
counted and is removed instead of being renamed to `ignored_<name>`: every
removed file is a hard link of the copy, so no data is lost, and the server
had already decided before the restart that the part is Outdated. Removal
errors are logged, like the `ignore_error` rename in the same loop.

What keeps today's behaviour:
- an incomplete copy (a crash inside `makeCloneInDetached`), compared both ways;
- the no-prefix DETACH/DROP clone, which has its own empty covering part;
- disks without `supportsStat`, and zero-copy clones (full copies);
- parts still listed in Keeper. The issue proposed filtering at load time,
  but a `covered-by-broken` source part is still in Keeper until cleanup and
  can be the only copy of its data on a single replica.

The new integration test reproduces the three paths (CHECK TABLE on a
corrupted part, the part check on a part removed from Keeper, and a lost
replica cloned with `detach_old_local_parts_when_cloning_replica`), each
followed by `kill -9` and a restart while SYSTEM STOP CLEANUP keeps the
outdated directory, plus an incomplete copy that must still make the table
read-only. On master the three positive cases fail with the issue's error.

Closes ClickHouse#133415

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Add a case where a merged part is broken and its two source parts are
copied to `covered-by-broken_<name>`, and make the negative case also
cover a copy with the same file names where one file is a copy instead of
a hard link, so both the prefix list and the inode comparison are tested.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review (click to expand)
Finding Verdict
⚠️ No test covered the covered-by-broken prefix: removing it from the allow-list left every case green AGREE, fixed: test_covered_by_broken_copy
⚠️ The same-inode half of the match was untested: a names-only comparison passed every case AGREE, fixed: a copy with one non-hard-linked file must keep the table read-only
⚠️ Nothing pinned the prefix allow-list: accepting any prefix left every case green AGREE, fixed: a matching copy without a prefix must keep the table read-only
💡 The clone case did not assert that no ignored_ copy is created AGREE, fixed
💡 An I/O error while reading a <reason>_<part> entry in detached/ propagates and leaves the table read-only Noted, not changed: every such entry is created by the server itself

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. The new integration test on master: the four positive cases (broken, covered-by-broken, unexpected, clone) fail on every run because the table stays read-only with TOO_MANY_UNEXPECTED_DATA_PARTS.
b Root cause explained? Yes. makeCloneInDetached hard-links the part into detached/<reason>_<name> and the four callers make the original Outdated in memory only. After a restart before cleanup it is an unexpected part, and checkPartsImpl recognises leftovers only through a covering part, which these paths do not create.
c Fix matches root cause? Yes. The leftover is recognised in the existing leftover classification of checkPartsImpl by the only trace these paths leave: a complete copy in detached/ with the same inodes. The sanity ratio is unchanged.
d Test intent preserved / new tests added? New integration test test_replicated_merge_tree_detached_copy_after_restart: broken, covered-by-broken, unexpected, clone, plus three copies that must still keep the table read-only (one misses a file, one has a file that is a copy, not a hard link, one has no prefix). No existing test changed.
e Both directions demonstrated? Yes. On master the 4 positive cases fail and the mismatched-copy case passes; with the fix 5 of 5 pass (3 runs). A mutant that drops covered-by-broken and compares file names only fails the covered-by-broken and mismatched-copy cases. A mutant that also accepts unprefixed copies fails the mismatched-copy case.
f Fix is general across code paths? One rule covers all four clone-then-outdate call sites (broken, covered-by-broken, unexpected, clone), each with its own test case. The no-prefix DETACH/DROP path already writes an empty covering part and is excluded; a test pins it. Rename-based detaches leave no original behind.
g Fix generalizes across inputs (params/datatypes/wrappers)? Wide parts, compact parts and parts with a projection were checked locally; an incomplete .proj copy keeps the part. _tryN copies are matched by part info. Disks without stat and zero-copy clones (full copies) keep today's behaviour.
h Backward compatible? (maintainer-approved exception only) Yes. No setting, format or Keeper change. Parts still listed in Keeper load as before.
i Invariants and contracts preserved? Detection has no side effects, so the optimistic first pass can still retry. Removal happens only after the sanity check, in the loop that already moves every unexpected part out of the table. Every removed file is a hard link of the detached copy, so no data is lost. Removal errors are logged, as for the existing ignored_ rename.

Session id: cron:clickhouse-impl-slot-7:20261121-193100

@clickhouse-gh

clickhouse-gh Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [1c72585]

Summary: ✅


AI Review

Summary

The change prevents a ReplicatedMergeTree table from remaining read-only after restart when unexpected parts already have complete hard-linked copies in detached/. It preserves the detached copies and removes the duplicate originals after the startup sanity check. Reviewing the current diff, startup and removal paths, regression tests, linked issue, and all prior discussion revealed no actionable blockers or majors.

Missing context / blind spots
  • ⚠️ Runtime validation was not repeated here because this checkout has no built ClickHouse binary. The author reports baseline failures and repeated successful runs with the fix. The current CI report shows dropped bugfix-validation jobs; rerunning the added integration module would provide independent runtime confirmation.
Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default can be tested Allows running workflows for external contributors comp-replication ReplicatedMergeTree* + replication log/state transitions, eventual consistency mechanics. labels Nov 21, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 1c72585fa with master a2126dbc1 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Object file sizes

4 object files changed (+33.35 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/dbms.dir/Storages/StorageReplicatedMergeTree.cpp.o 5.13 MiB 5.16 MiB +33.35 KiB (+0.64%)

716 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

7 translation units recompiled, 28 s compile time in total, 7 of them have a recent master baseline.

Job report

@alexey-milovidov alexey-milovidov self-assigned this Nov 22, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Nov 22, 2026
Merged via the queue into ClickHouse:master with commit b2b9bcf Nov 22, 2026
575 of 579 checks passed
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-replication ReplicatedMergeTree* + replication log/state transitions, eventual consistency mechanics. pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

3 participants