Skip to content

Add missing Keeper component guard to MutateFromLogEntryTask/MergeFromLogEntryTask destructors - #110664

Merged
alexey-milovidov merged 5 commits into
masterfrom
fix-zerocopylock-missing-keeper-component
Jul 17, 2026
Merged

alexey-milovidov merged 5 commits into
masterfrom
fix-zerocopylock-missing-keeper-component

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Jul 16, 2026 •

Copy link
Copy Markdown
Member

Related: #110590
Related: #110670

Changelog category (leave one):

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

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

Fix a LOGICAL_ERROR (Current component is empty, please set it for your scope using Coordination::setCurrentComponent) that could abort the server in debug/sanitizer builds (or fail under enforce_keeper_component_tracking=1) when a zero-copy exclusive lock was released outside any Keeper component scope. This happened in two sibling paths: (1) when a replicated merge or mutation task holding the lock was destroyed before its explicit unlock was reached, e.g. on cancellation, and (2) when a background zero-copy part move acquired/released the lock. MutateFromLogEntryTask and MergeFromLogEntryTask now release their zero_copy_lock under a Coordination::setCurrentComponent guard in an explicit destructor (matching the existing DDLWorker::shutdown precedent for EphemeralNodeHolder), and MergeTreeData::moveParts sets the component around its zero-copy section (tryCreateZeroCopyExclusiveLock, waitZeroCopyLockToDisappear, and the local lock's release). A deterministic regression test (04603_zero_copy_lock_dtor_keeper_component, adapted from #110670) covers both paths: two pauseable failpoints let it destroy a mutation task while it provably holds the zero-copy lock, and a synchronous ALTER TABLE ... MOVE PART to a zero-copy disk exercises the moveParts path.

Found while investigating an unrelated CI failure on #110590:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=110590&sha=5695f5662ad462d511ecdd527cbfefcd51f46a37&name_0=PR&name_1=Stateless%20tests%20%28amd_debug%2C%20flaky%20check%29

Documentation entry for a feature (a user-readable short description of the changes that goes into user-facing documentation.md):

N/A

Version info

  • Merged into: 26.7.1.1106 (included in 26.7 and later)

…mLogEntryTask destructors

Both tasks hold a `zero_copy_lock` (a `ZeroCopyLock` wrapping a
`zkutil::ZooKeeperLock`) while performing a replicated mutation/merge with
`allow_remote_fs_zero_copy_replication` enabled. The lock is released
explicitly in `prepare()`/`finalize()` on the normal paths, but if the task
is destroyed while still holding the lock -- e.g. on cancellation before
that explicit unlock is reached -- the implicit destructor chain
(`~MutateFromLogEntryTask`/`~MergeFromLogEntryTask` -> `~ZeroCopyLock` ->
`~ZooKeeperLock` -> `unlock()`) issues a real ZooKeeper request to remove
the lock's ephemeral node.

That destructor chain runs from `MergeTreeBackgroundExecutor::routine`'s
generic task cleanup, which has no Keeper component scope set. With
`enforce_keeper_component_tracking=1` (as in integration tests and debug/
sanitizer builds), the request throws `LOGICAL_ERROR: Current component is
empty, please set it for your scope using Coordination::setCurrentComponent`,
which aborts the server.

Add an explicit destructor to each task that resets `zero_copy_lock` under
a `Coordination::setCurrentComponent` guard, following the precedent of
`DDLWorker::shutdown` explicitly clearing `active_node_holders` under a
guard rather than putting the guard in the shared, generic
`ZooKeeperLock`/`ZeroCopyLock` destructor itself.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=110590&sha=5695f5662ad462d511ecdd527cbfefcd51f46a37&name_0=PR&name_1=Stateless%20tests%20%28amd_debug%2C%20flaky%20check%29
Related: #110590
@clickhouse-gh

clickhouse-gh Bot commented Jul 16, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [70a750e]

Summary: ✅


AI Review

Summary

This PR adds explicit Coordination::setCurrentComponent guards around zero-copy lock release in MutateFromLogEntryTask / MergeFromLogEntryTask teardown, adds the matching guard to the background MergeTreeData::moveParts zero-copy path, and extends stateless coverage so the test now distinguishes the fix from the old behavior. I re-checked the current diff and all prior discussion, including the two still-open bot threads, and did not find any remaining correctness, safety, or evidence gaps that need new review comments.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Jul 16, 2026
@nikitamikhaylov

Copy link
Copy Markdown
Member

Check this: #110670

alexey-milovidov and others added 2 commits July 16, 2026 15:29
`MergeTreeData::moveParts` acquires the same `ZeroCopyLock` as the
replicated merge/mutate tasks, but from an unguarded scope: it calls
`tryCreateZeroCopyExclusiveLock` and `waitZeroCopyLockToDisappear`, and
the local `lock` releases its ephemeral node via `~ZooKeeperLock` at
scope exit -- all of which issue ZooKeeper requests.

Both background call sites (`scheduleDataMovingJob` and the async branch
of `movePartsToSpace`) run `moveParts` through `ExecutableLambdaAdapter`,
which never sets a Keeper component. So with
`enforce_keeper_component_tracking=1`, zero-copy part moves still hit the
`LOGICAL_ERROR` "Current component is empty" (a server abort in
debug/sanitizer builds), even after the merge/mutate task destructors
were guarded.

Set `Coordination::setCurrentComponent` at the top of the zero-copy
branch of `moveParts`, covering lock acquisition, the wait loop, and the
local `lock`'s destruction.

Found while reviewing #110664:
#110664

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

Thanks @nikitamikhaylov — #110670 is essentially the same fix: it adds guarded destructors to MergeFromLogEntryTask / MutateFromLogEntryTask (same as here), and additionally a rmt_mutate_task_pause_after_zero_copy_lock failpoint plus a stateless regression test (04546_zero_copy_lock_dtor_keeper_component).

Two differences after the latest push here:

They are near-duplicates, so only one should land. Whichever you prefer, the ideal end state combines both: the task-destructor guards and the moveParts guard and the regression test. Happy to fold the moveParts guard into #110670, or to add the failpoint/test here — your call on which PR to keep.

…t a Keeper component

The AI review kept this PR at request-changes because nothing in-tree
proved the guarded destructor path is exercised: the existing failpoints
(`rmt_mutate_task_pause_in_prepare`, `rmt_merge_task_sleep_in_prepare`)
fire before `tryCreateZeroCopyExclusiveLock`, so a future refactor could
drop the guards and CI would stay green.

Add two pauseable failpoints and a stateless test:
- `rmt_mutate_task_pause_after_zero_copy_lock` pauses `MutateFromLogEntryTask::prepare`
  right after the zero-copy exclusive lock is acquired, so the task provably
  holds the lock;
- `merge_tree_background_task_marked_for_deletion` pauses
  `MergeTreeBackgroundExecutor::removeTasksCorrespondingToStorage` right after
  active tasks are flagged `is_currently_deleting`, guaranteeing the resumed
  task takes the destruction path (cancel + destroy via the task destructor)
  instead of being requeued and finalized normally.

The test `04603_zero_copy_lock_dtor_keeper_component` synchronizes on both
failpoints, drops the table while the mutation task still holds the lock, and
checks the server survives `~ZooKeeperLock` releasing the ephemeral node from
the destructor (stateless runs enable `enforce_keeper_component_tracking`, so
before this PR's guards that release threw the `LOGICAL_ERROR` "Current
component is empty ..."). A second scope covers the sibling
`MergeTreeData::moveParts` path via a synchronous `ALTER TABLE ... MOVE PART`
to a zero-copy disk.

The failpoints and test are adapted from #110670 by @groeneai (renumbered to
04603 because 04546 is already taken on master).

Related: #110670

Co-Authored-By: Claude Fable 5 <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

@groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=110664&sha=7bf9c474f2de227b55fc3510d97b158c1bbc5639&name_0=PR&name_1=Stateless%20tests%20%28amd_tsan%2C%20s3%20storage%2C%20sequential%2C%202%2F2%29 and provide a fix in a separate PR. If the fix is already in progress, link it here.

It is a ThreadSanitizer data race in DB::BufferBase::set (STID: 1679-3e61) during 03912_encrypted_s3_cache_cancel_read, which then kills the server ("Server died"). It is unrelated to this PR (which only adds Coordination::setCurrentComponent guards): the same STID has hit unrelated PRs #110453, #110531, #110638, #110433, #109459 and #108377 over the last two weeks, and I found no open issue or fix PR for it yet.

@groeneai

Copy link
Copy Markdown
Collaborator

@alexey-milovidov Investigated. STID 1679-3e61 is the write side of the shared-remote-reader race in CachedOnDiskReadBufferFromFile::getInfoForLog, and it is already fixed on master by #110154 (merged 2026-07-16 15:52 UTC).

Root cause: state->buf is a file segment's remote reader (here ReadBufferFromS3), shared across readers via FileSegment::getRemoteFileReader/setRemoteFileReader and synchronized only by downloader election, whose term lasts a single nextImpl(). After the segment is completed and a new downloader is re-elected, the SCOPE_EXIT diagnostics in nextImplStep (getInfoForLog) read the reader's buffer pointers (available()/end()) while the new downloader mutates them via BufferBase::set in ReadBufferFromS3::nextImpl -> the TSan race, then "Server died". The data reads were already serialized; only the diagnostic read was racy.

#110154 releases state (and thus the reference to the shared remote reader) before completing the segment and re-electing a downloader, so a throw leaves state empty and getInfoForLog never touches a reader another thread now owns.

The failing run here (7bf9c474) is ~89 commits behind the fix merge and does not contain it, so a rebase / merge of master past befb96d will pick it up. No separate PR needed.

Report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=110664&sha=7bf9c474f2de227b55fc3510d97b158c1bbc5639&name_0=PR&name_1=Stateless%20tests%20%28amd_tsan%2C%20s3%20storage%2C%20sequential%2C%202%2F2%29

… bug

Address the AI review (Request changes) on `5ea61b78`. The previous version of
`04603_zero_copy_lock_dtor_keeper_component.sh` only checked server liveness,
which cannot tell the fixed code from the buggy code: `~ZooKeeperLock` catches
and logs `unlock` failures, and the background moves executor catches task
exceptions, so a missing `Coordination::setCurrentComponent` guard logs
`Current component is empty ...` instead of aborting the server.

Two fixes:

- Add an observable that fails when a guard is removed: after both scopes,
  assert `system.text_log` contains no `Current component is empty` message
  produced during the test. The background `DROP TABLE rmt SYNC` result is no
  longer discarded either (`wait` now propagates its exit status under `set -e`).

- Make the part-move scope actually exercise the background `MergeTreeData::moveParts`
  path. A synchronous `ALTER ... MOVE PART` runs `moveParts` on the query thread
  under `StorageReplicatedMergeTree::alter`'s existing component scope, so it never
  hit the bug. Setting `alter_move_to_space_execute_async = 1` schedules the move on
  a background moves thread via `ExecutableLambdaAdapter` with no component set
  upstream, and the test waits until the part is relocated to `s3_disk` (which never
  happens if the guard is missing, since the background task throws first).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.00% 86.00% +0.00%
Functions 91.80% 91.80% +0.00%
Branches 78.10% 78.20% +0.10%

Changed lines: Changed C/C++ lines covered: 29/31 (93.55%) · Uncovered code

Full report · Diff report

@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.

LGTM

@alexey-milovidov alexey-milovidov self-assigned this Jul 17, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Jul 17, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member Author

Check this: #110670

Checked, we should merge both.

Merged via the queue into master with commit eda0515 Jul 17, 2026
179 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-zerocopylock-missing-keeper-component branch July 17, 2026 15:33
alexey-milovidov added a commit that referenced this pull request Jul 17, 2026
Master merged `04603_zero_copy_lock_dtor_keeper_component` (#110664),
which collides with this PR's `04603_qbit_distance_parallel_replicas_scalar_subquery`.
Renumber the two QBit tests to the next free numbers:
- `04603_qbit_distance_parallel_replicas_scalar_subquery` -> `04604`
- `04604_qbit_distance_parallel_replicas_constant_reference` -> `04605`

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 17, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
`MergeTreeData::moveParts` acquires the same `ZeroCopyLock` as the
replicated merge/mutate tasks, but from an unguarded scope: it calls
`tryCreateZeroCopyExclusiveLock` and `waitZeroCopyLockToDisappear`, and
the local `lock` releases its ephemeral node via `~ZooKeeperLock` at
scope exit -- all of which issue ZooKeeper requests.

Both background call sites (`scheduleDataMovingJob` and the async branch
of `movePartsToSpace`) run `moveParts` through `ExecutableLambdaAdapter`,
which never sets a Keeper component. So with
`enforce_keeper_component_tracking=1`, zero-copy part moves still hit the
`LOGICAL_ERROR` "Current component is empty" (a server abort in
debug/sanitizer builds), even after the merge/mutate task destructors
were guarded.

Set `Coordination::setCurrentComponent` at the top of the zero-copy
branch of `moveParts`, covering lock acquisition, the wait loop, and the
local `lock`'s destruction.

Found while reviewing ClickHouse#110664:
ClickHouse#110664

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…-missing-keeper-component

Add missing Keeper component guard to MutateFromLogEntryTask/MergeFromLogEntryTask destructors
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
Master merged `04603_zero_copy_lock_dtor_keeper_component` (ClickHouse#110664),
which collides with this PR's `04603_qbit_distance_parallel_replicas_scalar_subquery`.
Renumber the two QBit tests to the next free numbers:
- `04603_qbit_distance_parallel_replicas_scalar_subquery` -> `04604`
- `04604_qbit_distance_parallel_replicas_constant_reference` -> `04605`

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

Labels

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.

4 participants