Skip to content

Clear the part check queue when SYSTEM RESTORE REPLICA reissues block numbers - #118704

Open
tuanpach wants to merge 1 commit into
ClickHouse:masterfrom
tuanpach:fix-stale-part-check-queue-after-restore-replica
Open

tuanpach wants to merge 1 commit into
ClickHouse:masterfrom
tuanpach:fix-stale-part-check-queue-after-restore-replica

Conversation

@tuanpach

@tuanpach tuanpach commented Sep 8, 2026 •

Copy link
Copy Markdown
Member

Related: #103537

SYSTEM RESTORE REPLICA reattaches every part under a freshly allocated block number, but leaves the part check queue untouched. A part name is its block range, so a queued name then denotes nothing: the check reads the renamed part as lost, and createEmptyPartInsteadOfLost recreates it over a range the reissued counter has since given to unrelated inserts. The write is rejected, but only after the part got its final on-disk name, leaving a zero-row part that the next load reports as unexpected. Paired with the empty part DROP PARTITION leaves for the same range, checkPartsImpl ends up with two zero-row parts that neither contain nor are disjoint from each other - the LOGICAL_ERROR "Part A intersects previous part B" that #103537 tolerates.

Seen on BuzzHouse for #100943, where all_2_11_3 came back as all_0_0_0 while blocks 2, 3 and 5 were reissued: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=100943&sha=3308ce056bdfd5672c3c300ccae03968beac9846&name_0=PR&name_1=BuzzHouse%20%28arm_asan_ubsan%29

The restore now drops the stale names and pauses the check task for the whole renumbering, since clearing the queue cannot stop a check that already started. createEmptyPartInsteadOfLost also tests replaced_parts before committing to disk, so a rejected empty part leaves only its tmp_empty_ directory - that path is reachable without a restore.

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 SYSTEM RESTORE REPLICA leaving stale entries in the part check queue. Because the restore reassigns block numbers, such an entry made a renamed part look lost and caused an empty part to be written over a block range holding unrelated data, leaving a zero-row part on disk that broke the next attach with Part ... intersects previous part ....


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

…ck numbers

`restoreMetadataInZooKeeper` moves every part to `detached/`, builds a fresh
Keeper path and reattaches the parts under newly allocated block numbers. A
part name is its block range, so a name queued for check before that point
denotes nothing afterwards, and the queue is never cleared.

When the deferred check fires, the name is absent from Keeper, absent locally,
and no active part contains its range - the reattached copy sits at a lower
block number. `ReplicatedMergeTreePartCheckThread::onPartIsLostForever` reads
that as a lost part and calls `createEmptyPartInsteadOfLost`, which recreates
it over a block range that the reissued counter has since handed to unrelated
inserts. The guard against covering live data rejects the write, but only after
`renameParts` gave the part its final on-disk name, and rollback merely moves it
to Outdated. A zero-row part is left behind that is in no Keeper list, so the
next load reports it as unexpected. Together with the empty part that
`DROP PARTITION` leaves for the same range, `checkPartsImpl` then holds two
zero-row parts that neither contain nor are disjoint from each other, which used
to abort the attach thread with `LOGICAL_ERROR` "Part A intersects previous
part B" and is tolerated since ClickHouse#103537.

Observed by BuzzHouse on ClickHouse#100943, where the table was converted with
`ATTACH TABLE ... AS REPLICATED`, unstuck with `SYSTEM RESTORE REPLICA`, and the
merge result `all_2_11_3` came back as `all_0_0_0` while blocks 2, 3 and 5 were
reissued to new inserts:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=100943&sha=3308ce056bdfd5672c3c300ccae03968beac9846&name_0=PR&name_1=BuzzHouse%20%28arm_asan_ubsan%29

`clearQueue` drops the stale names, and the task is paused for the whole
renumbering because `clearQueue` cannot stop a check that already started: it
holds its own copy of the name and runs without `parts_mutex`.

`createEmptyPartInsteadOfLost` now tests `replaced_parts` before committing the
part to disk, so a rejected empty part leaves only its `tmp_empty_` directory,
which part loading skips and temporary directory cleanup removes. That path is
reachable without a restore - the comment beside the throw describes a replica
cloning while the source loses a covering part.

Related: ClickHouse#103537
@clickhouse-gh

clickhouse-gh Bot commented Sep 8, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [d98dc0c]

Summary: ❌

job_name test_name status info comment
Stateless tests (arm_asan_ubsan, azure, parallel) FAIL
00084_external_aggregation FAIL cidb

AI Review

Summary

This PR fixes a real SYSTEM RESTORE REPLICA failure mode by clearing stale part-check queue entries and by moving one createEmptyPartInsteadOfLost rejection ahead of the final on-disk rename. The direction is right, but the current patch still leaves the stale-name path reachable through synchronous CHECK TABLE, and the empty-part rollback fix is only partial.

Findings

❌ Blockers

  • [src/Storages/StorageReplicatedMergeTree.cpp:7437] temporaryPause() only stops the scheduled part_check_thread; it does not block synchronous CHECK TABLE. SYSTEM RESTORE REPLICA does not take an exclusive table or DDL lock, and CHECK TABLE still calls checkPartAndFix directly after taking the same pause holder (src/Storages/StorageReplicatedMergeTree.cpp:10517-10536, src/Interpreters/InterpreterCheckQuery.cpp:111-149). A CHECK TABLE that overlaps restore can therefore still process a pre-restore part name after block numbers were reissued, recreating the stale-name to empty-part path this PR is meant to remove.
  • [src/Storages/StorageReplicatedMergeTree.cpp:11635] The rename-order change only fixes the replaced_parts rejection. Every later return false / throw in createEmptyPartInsteadOfLost still happens after commitTransaction() and transaction.renameParts(), while MergeTreeData::Transaction::rollback only removes the part from the working set or marks it Outdated; it does not rename the directory back (src/Storages/MergeTree/MergeTreeData.cpp:11006-11094). A rejected empty part can still leave an unexpected final-name directory on disk, so the rollback issue is not actually closed.
Tests
  • ⚠️ The new stateless test only proves that SYSTEM RESTORE REPLICA clears an already queued background check. It does not exercise the synchronous CHECK TABLE overlap above, and it does not cover any post-transaction.renameParts() rejection in createEmptyPartInsteadOfLost.
Final Verdict
  • Status: ⚠️ Request changes
  • Minimum required actions: serialize SYSTEM RESTORE REPLICA against synchronous part checks as well as the background worker, and complete the createEmptyPartInsteadOfLost fix so every rejection path before ZooKeeper commit avoids leaving a final-name part directory behind.

LLVM Coverage Report

Measured on commit d98dc0c.

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

Changed lines: Changed C/C++ lines covered: 22/24 (91.67%) · 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 8, 2026
int32_t max_parts_metadata_version = 0;

/// No check may run while block numbers are reissued below.
auto pause_checking_parts = part_check_thread.temporaryPause();

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.

temporaryPause() only deactivates the scheduled part_check_thread; it does not block the synchronous CHECK TABLE path. InterpreterSystemQuery::restoreReplica does not take a DDL or exclusive table lock, while CHECK TABLE can still do getCheckTaskList() / checkDataNext() and call checkPartAndFix directly (StorageReplicatedMergeTree.cpp:10517-10536, InterpreterCheckQuery.cpp:111-149). So a CHECK TABLE started just before or during SYSTEM RESTORE REPLICA can still validate a pre-restore part name after block numbers were reissued, recreating the stale-name to createEmptyPartInsteadOfLost path this PR is trying to remove. This needs a guard that covers every part-check entrypoint, for example rejecting CHECK TABLE while are_restoring_replica is set or serializing restore with CHECK TABLE via an exclusive table or DDL lock.

lost_part_name, fmt::join(part_names, ", "));
}

new_data_part->getDataPartStorage().commitTransaction();

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.

This only moves the replaced_parts rejection before the on-disk rename. The later return false / throw paths in this function (is_readonly, covering_virtual.empty(), isGoingToBeDropped, duplicate-part checks, keeper failures) still happen after commitTransaction() plus transaction.renameParts(), and MergeTreeData::Transaction::rollback only removes the part from the working set or marks it Outdated without renaming the directory back. So a rejected empty part can still leave an unexpected final-name directory on disk; the fix is incomplete unless those post-rename checks move before the rename or rollback restores the tmp_empty_ path.

@clickhouse-gh clickhouse-gh Bot added the comp-replication ReplicatedMergeTree* + replication log/state transitions, eventual consistency mechanics. label Sep 8, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing d98dc0c9b with master 55c256363 (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.

Compile time of recompiled translation units

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

Job report

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

Labels

comp-replication ReplicatedMergeTree* + replication log/state transitions, eventual consistency mechanics. 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