Skip to content

Check a local part with no node in ZooKeeper instead of fetching it - #119189

Merged
alexey-milovidov merged 5 commits into
masterfrom
fetch-entry-part-missing-in-zk-118002
Sep 21, 2026
Merged

alexey-milovidov merged 5 commits into
masterfrom
fetch-entry-part-missing-in-zk-118002

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Sep 10, 2026 •

Copy link
Copy Markdown
Member

A part that is Active locally while its node under replicas/<r>/parts/ is gone - a self-inconsistent state crash recovery can leave behind - wedged the replication queue for good. The GET_PART entry for that part was executed, because the already-have-it guard in executeLogEntry skips an entry only when the local part's node exists; the fetch then downloaded the whole part from a peer and renameTempPartAndReplaceImpl threw DUPLICATE_DATA_PART for the copy that was already there. Nothing in the retry path reconciles the two, so the entry was retried forever: SYSTEM SYNC REPLICA never returned, the queue stayed non-empty for monitoring, and every round re-downloaded the part just to throw it away.

The merge selection has handled this state for a long time - a part that "exists locally but not in ZooKeeper" is not used for a merge and is enqueued for a check. The fetch path now does the same: it enqueues the part for check and stops instead of fetching. The part check thread is the designed reconciler - it adds the missing node when the local part is intact and detaches the part when it is not - and after it runs, the entry is either skipped by the guard above or has nothing in its way. The entry stays in the queue meanwhile, where the exponential backoff of a failed entry keeps the retries apart, and a retry now costs nothing.

Closes: #118002

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 to CHANGELOG.md):

Fixed the replication queue never draining when a part is present locally but its node in ZooKeeper is missing: the GET_PART entry for such a part re-downloaded it and failed with DUPLICATE_DATA_PART on every retry. The part is now enqueued for a check, which reconciles the state.


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

Version info

  • Merged into: 26.10.1.323-master (included in 26.10 and later)

A part that is in the working set while its node under
`replicas/<r>/parts/` is gone - a state crash recovery can leave behind
- wedged the replication queue: the already-have-it guard skips an entry
only when the node exists, so the `GET_PART` entry fetched the whole
part from a peer and then threw `DUPLICATE_DATA_PART` for the copy that
was already there, forever, re-downloading the part every round.

The merge selection has always enqueued such a part for check instead of
using it. The fetch path now does the same and stops: the part check
thread adds the node back when the local part is intact and detaches the
part when it is not, and the entry is then either skipped or unobstructed.

Closes: #118002
@clickhouse-gh

clickhouse-gh Bot commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [e7dedb2]

Summary: ❌

job_name test_name status info comment
Build profile diff ERROR

AI Review

Summary

This PR correctly stops the immediate DUPLICATE_DATA_PART retry loop by refusing to fetch or rebuild a part that already exists locally, but the new recovery path is still unsafe. Both changed call sites now rely on the part-check thread to reconcile "local part exists, replicas/<r>/parts/<name> is missing", and that thread still treats such parts as unexpected local leftovers that are rechecked until they are 5 minutes old and then detached, not repaired. Because the queue now drains by eventually discarding the active part instead of restoring its Keeper metadata, I would not approve this as-is.

Findings

❌ Blockers

  • [src/Storages/MergeTree/ReplicatedMergeMutateTaskBase.cpp:278-298] The new guard assumes enqueuePartForCheck preserves an intact local part by recreating its missing Keeper node, but the checker still does not do that. When the part is younger than MAX_AGE_OF_LOCAL_PART_THAT_WASNT_ADDED_TO_ZOOKEEPER, ReplicatedMergeTreePartCheckThread::checkPartImpl only schedules a later recheck; once it is older, it returns DetachUnexpected instead (src/Storages/MergeTree/ReplicatedMergeTreePartCheckThread.cpp:450-469). checkPartAndFix then calls outdateUnexpectedPartAndCloneToDetached (src/Storages/MergeTree/ReplicatedMergeTreePartCheckThread.cpp:497-501), which removes an active part from the working set (src/Storages/MergeTree/MergeTreeData.cpp:7466-7475). So the queue no longer spins on DUPLICATE_DATA_PART, but it still resolves this state by eventually dropping the local part and hoping a later fetch or re-merge can rebuild it. If this replica is the only intact copy, the follow-up path can still end in the existing "lost forever" handling. The same incorrect assumption is now present in StorageReplicatedMergeTree::executeLogEntry for GET_PART / ATTACH_PART as well (src/Storages/StorageReplicatedMergeTree.cpp:2641-2659).
  • Suggested fix: make the reconciliation path actually restore replicas/<r>/parts/<name> for a verified intact local part, or route this case through a different repair path that keeps the part active until that metadata is restored. Both new call sites should use that same repair.
Tests
  • ⚠️ Add a focused regression that drives the checker through the full reconciliation path and proves that an intact local part keeps serving reads and gets its replicas/<r>/parts/<name> node restored. The new 05229 / 05230 tests stop after observing the interim log or exception, so they would still pass with the current detach-after-5-min behavior.
Final Verdict

Status: ⚠️ Request changes

Minimum required actions:

  1. Replace the current enqueuePartForCheck-based handling with a repair path that preserves an intact local part and restores its missing Keeper metadata instead of detaching it.
  2. Add a regression that covers that full repair path, not only the first retry after the queue entry notices the inconsistency.

LLVM Coverage Report

Measured on commit e7dedb2.

Metric Baseline Current Δ
Lines 88.40% 88.50% +0.10%
Functions 91.80% 91.80% +0.00%
Branches 80.60% 80.70% +0.10%

Changed lines: Changed C/C++ lines covered: 35/35 (100.00%) · 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 10, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-replication ReplicatedMergeTree* + replication log/state transitions, eventual consistency mechanics. label Sep 10, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 10, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing commit e7dedb26898562f3a79b653e16d9adca2d31af99 with master failed: RuntimeError: CI logs cluster query failed: SELECT file, countIf(side = 'pr') AS pr_cou….

See the job log for details.

alexey-milovidov and others added 3 commits September 12, 2026 19:40
The test removes the part's node under `replicas/<r>/parts/` by hand and then
watches the `ReplicatedMergeTree` queue entry written for it. Neither structure
exists when the engine is substituted for one that keeps its metadata elsewhere,
so the test asserted nothing there and failed. Tag it `no-shared-merge-tree`.
`MERGE_PARTS` and `MUTATE_PART` entries never reach `executeLogEntry`:
`scheduleDataProcessingJob` sends them to `MergeFromLogEntryTask` and
`MutateFromLogEntryTask`, whose shared
`ReplicatedMergeMutateTaskBase::checkExistingPart` only skipped the entry when
the local part's node under `replicas/<r>/parts/` exists. So the same state - the
part is `Active` locally while its node in ZooKeeper is gone - still fell through
to the merge, the mutation, or, when `prepare` declines the local execution
(`always_fetch_merged_part`, `always_fetch_mutated_part`, single-replica merge
selection, missing source parts, zero-copy handoff), to `executeFetch`. Each of
those produces the part that is already there and throws `DUPLICATE_DATA_PART`,
which is the loop this pull request is about.

`checkExistingPart` now enqueues such a part for check and reports
`PART_MISSING_IN_ZOOKEEPER`, the same reconciliation the `GET_PART` and
`ATTACH_PART` path does. No exception is thrown there, because
`ReplicatedMergeMutateTaskBase::executeStep` does not save a
`PART_IS_TEMPORARILY_LOCKED` exception and the queue's exponential backoff is
armed by `updateLastExeption`; the task asks the background assignee to postpone
the next attempt instead, through the existing `postpone_next_attempt`.

Also renumber the test to `05229` - `05188` was taken on master meanwhile - and
add `05230_merge_entry_for_local_part_missing_in_zookeeper` for the merge entry.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
* or a mutation of the local source parts, or a fetch from a peer when `prepare` declines the
* local execution - `renameTempPartAndReplaceImpl` throws `DUPLICATE_DATA_PART` for the part
* that is already there, and nothing in the retry path reconciles the two, so the entry is
* retried forever. The part check thread is what reconciles it: it adds the missing node when

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.

The new guard assumes that enqueuePartForCheck will preserve an intact local part by re-adding its missing Keeper node, but that is not what the reconciler does today. In ReplicatedMergeTreePartCheckThread::checkPartImpl, the "part exists locally, but /replicas/<r>/parts/<name> does not" branch just waits until the part is older than MAX_AGE_OF_LOCAL_PART_THAT_WASNT_ADDED_TO_ZOOKEEPER, then returns DetachUnexpected (ReplicatedMergeTreePartCheckThread.cpp:450); outdateUnexpectedPartAndCloneToDetached then removes the active part from the working set instead of recreating the znode (MergeTreeData.cpp:7466).

So this PR no longer wedges on DUPLICATE_DATA_PART, but it fixes the queue by eventually dropping a still-readable part and relying on a later fetch/re-merge. If this replica is the only place that still has valid data for the result part, that follow-up path can even fall through to the existing "lost forever" handling. I think the fix needs a real "repair Keeper metadata for an intact local part" step here, or a different reconciliation path that keeps the part active while restoring its node.

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Ok.

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 pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

2 participants