Skip to content

Fix race between INSERT and DROP_RANGE causing LOGICAL_ERROR in paranoid check of covered parts. - #96164

Merged
alexey-milovidov merged 1 commit into
masterfrom
fix-preactive-drop-range-race
Feb 6, 2026
Merged

alexey-milovidov merged 1 commit into
masterfrom
fix-preactive-drop-range-race

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Feb 6, 2026 •

Copy link
Copy Markdown
Member

This is very interesting and not easy to understand, but the explanation looks correct.

The paranoidCheckForCoveredPartsInZooKeeper assertion was failing sporadically in test 01154_move_partition_long because a part's ZooKeeper node remained after executeDropRange removed all known parts from ZK.

Root cause: a race between a concurrent INSERT on the originating replica and executeDropRange processing on the same replica.

The sequence of events:

  1. INSERT thread calls renameTempPartAndAdd — the part enters data_parts in PreActive state, then the data_parts lock is released.

  2. INSERT thread executes the ZK multi-op (commitPart) — the part node is created in ZooKeeper under /replicas/{replica}/parts/{part_name}. The ephemeral block number lock is removed atomically by this multi-op.

  3. executeDropRange runs on the same replica:

    • removePartProducingOpsInRange waits for currently executing queue entries, but INSERT on the originating replica bypasses the queue.
    • removePartsInRangeFromWorkingSetAndGetPartsToRemoveFromZooKeeper enumerates local parts filtered to Active/Outdated/Deleting states — PreActive parts are skipped.
    • removePartsFromZooKeeperWithRetries removes ZK nodes only for the parts it found — the INSERT's ZK node is not in the list.
    • paranoidCheckForCoveredPartsInZooKeeper finds the INSERT's ZK node still present — throws LOGICAL_ERROR.
  4. INSERT thread calls transaction.commit() — the part transitions from PreActive to Active, but the DROP_RANGE has already completed.

clearLockedBlockNumbersInPartition (called before the DROP_RANGE log entry is created) cannot prevent this race: by the time it runs, the INSERT has already completed its ZK multi-op, which atomically removed the block number lock and created the part node.

Fix: add waitForPreActivePartsInRange that waits on a condition variable (preactive_parts_cv) for all PreActive parts in the drop range to finish committing or rolling back. The condition variable is notified by Transaction::commit and Transaction::rollback. Since clearLockedBlockNumbersInPartition prevents new INSERTs from committing in the range, the wait is guaranteed to terminate.

The wait is added before removePartsInRangeFromWorkingSetAndGetPartsToRemoveFromZooKeeper in executeDropRange and both removal sites in executeReplaceRange.

Also,

❯ How parts in Keeper can be PreActive? 

● You're right to challenge this — let me be precise. Parts in ZooKeeper don't have a "PreActive" state. A ZK node under /replicas/{replica}/parts/ either exists or it doesn't.

  The issue is on the local side: removePartsInRangeFromWorkingSetAndGetPartsToRemoveFromZooKeeper builds the list of ZK nodes to remove by enumerating local parts filtered to Active/Outdated/Deleting states. A part that's locally PreActive is skipped, so its ZK node is never added to the removal list.

  But let me re-examine whether this window actually exists during INSERT. Let me trace the exact ordering more carefully.

  The issue is that removePartsInRangeFromWorkingSetAndGetPartsToRemoveFromZooKeeper builds the list of ZK nodes to remove by enumerating local parts:
  - Line 5292: grabActivePartsToRemoveForDropRange → only Active
  - Line 5298-5299: getDataPartsVectorInPartitionForInternalUsage({Outdated, Deleting}, ...) → only Outdated/Deleting

  A part that's locally PreActive is skipped by both queries. So its corresponding ZK node is never added to the removal list.

  The INSERT flow in ReplicatedMergeTreeSink::commitPart creates this situation:

  Line 914:  renameTempPartAndAdd → part is PreActive in data_parts (under lockParts)
             lockParts released
  Line 930:  transaction.renameParts() → renamed on disk
  Line 939:  ZK multi-op → creates /replicas/{replica}/parts/{part_name} in ZK
  Line 944:  transaction.commit() → PreActive → Active

  Between lines 914 and 944, the data_parts lock is NOT held. executeDropRange can interleave:

  T1 (INSERT): lockParts → add PreActive to data_parts → unlock     [line 914]
  T2 (DROP):   lockParts → query Active parts (misses PreActive) → unlock
  T1 (INSERT): ZK multi-op succeeds → ZK node now exists           [line 939]
  T2 (DROP):   removePartsFromZooKeeperWithRetries (doesn't include INSERT's node)
  T2 (DROP):   paranoidCheck → finds INSERT's ZK node → LOGICAL_ERROR
  T1 (INSERT): transaction.commit() → Active                        [line 944]

  Or even simpler — the INSERT's ZK multi-op may happen before DROP takes the lock, but since it only queries Active/Outdated/Deleting, the PreActive part is invisible regardless.

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

Version info

  • Merged into: 26.2.1.339

…oid check

The `paranoidCheckForCoveredPartsInZooKeeper` assertion was failing sporadically
in test `01154_move_partition_long` because a part's ZooKeeper node remained
after `executeDropRange` removed all known parts from ZK.

Root cause: a race between a concurrent INSERT on the originating replica and
`executeDropRange` processing on the same replica.

The sequence of events:

1. INSERT thread calls `renameTempPartAndAdd` — the part enters `data_parts`
   in PreActive state, then the data_parts lock is released.

2. INSERT thread executes the ZK multi-op (`commitPart`) — the part node is
   created in ZooKeeper under `/replicas/{replica}/parts/{part_name}`.
   The ephemeral block number lock is removed atomically by this multi-op.

3. `executeDropRange` runs on the same replica:
   - `removePartProducingOpsInRange` waits for currently executing queue
     entries, but INSERT on the originating replica bypasses the queue.
   - `removePartsInRangeFromWorkingSetAndGetPartsToRemoveFromZooKeeper`
     enumerates local parts filtered to Active/Outdated/Deleting states —
     PreActive parts are skipped.
   - `removePartsFromZooKeeperWithRetries` removes ZK nodes only for the
     parts it found — the INSERT's ZK node is not in the list.
   - `paranoidCheckForCoveredPartsInZooKeeper` finds the INSERT's ZK node
     still present — throws `LOGICAL_ERROR`.

4. INSERT thread calls `transaction.commit()` — the part transitions from
   PreActive to Active, but the DROP_RANGE has already completed.

`clearLockedBlockNumbersInPartition` (called before the DROP_RANGE log entry
is created) cannot prevent this race: by the time it runs, the INSERT has
already completed its ZK multi-op, which atomically removed the block number
lock and created the part node.

Fix: add `waitForPreActivePartsInRange` that waits on a condition variable
(`preactive_parts_cv`) for all PreActive parts in the drop range to finish
committing or rolling back. The condition variable is notified by
`Transaction::commit` and `Transaction::rollback`. Since
`clearLockedBlockNumbersInPartition` prevents new INSERTs from committing
in the range, the wait is guaranteed to terminate.

The wait is added before `removePartsInRangeFromWorkingSetAndGetPartsToRemoveFromZooKeeper`
in `executeDropRange` and both removal sites in `executeReplaceRange`.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Feb 6, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [30c81e8]

Summary: ❌

job_name test_name status info comment
Stateless tests (amd_ubsan, sequential) failure
03032_dynamically_resize_filesystem_cache_2 FAIL cidb, issue
AST fuzzer (amd_ubsan) failure
Logical error: Cannot convert nested result of function A with type B to the expected result type C: D (STID: 4827-671c) FAIL cidb, issue

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Feb 6, 2026
@alexey-milovidov alexey-milovidov self-assigned this Feb 6, 2026
@alexey-milovidov
alexey-milovidov merged commit 158cc8f into master Feb 6, 2026
127 of 129 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-preactive-drop-range-race branch February 6, 2026 19:25
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Feb 6, 2026
@tavplubix tavplubix added the post-approved Approved, but after the PR is merged. label Mar 17, 2026
groeneai added a commit to groeneai/ClickHouse that referenced this pull request Apr 16, 2026
…rOnStart

The paranoid check on startup verifies that all ZK parts covered by another
ZK part either exist on disk or are in the parts_to_fetch list. However, it
does not check whether a local active part already covers the ZK part.

This legitimately happens after a merge or mutation: the covering part is
active locally, the covered part was cleaned from disk by
clearOldPartsAndRemoveFromZK, but the covered part's ZK entry has not been
cleaned up yet (ZK cleanup can lag behind disk cleanup, or the server was
restarted between the two). The data is preserved in the local covering
part, and the stale ZK entry will be removed by the cleanup thread after
startup.

Without this fix, the chassert(false) fires as a false positive in
debug/sanitizer builds during stress tests, particularly in the
createLogEntriesToFetchBrokenParts() call path (STID: 2508-5dc3,
2508-6644). Prior fixes PR ClickHouse#96164, ClickHouse#96705, and ClickHouse#100652 addressed other
false-positive scenarios in this function but missed the case where a local
active part covers the ZK part.

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

Labels

post-approved Approved, but after the PR is merged. pr-ci pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants