Skip to content

Set Keeper component in StorageMergeTree mutation-file methods - #109297

Merged
PedroTadim merged 2 commits into
ClickHouse:masterfrom
groeneai:fix-storagemergetree-keeper-component-tracking
Jul 9, 2026
Merged

PedroTadim merged 2 commits into
ClickHouse:masterfrom
groeneai:fix-storagemergetree-keeper-component-tracking

Conversation

@groeneai

@groeneai groeneai commented Jul 3, 2026 •

Copy link
Copy Markdown
Collaborator

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 a LOGICAL_ERROR (Current component is empty) that could be raised by mutation operations such as KILL MUTATION on a MergeTree table that stores its metadata in Keeper, when the enforce_keeper_component_tracking server setting is enabled.

Description

StorageMergeTree methods that perform MergeTreeMutationEntry disk I/O (commit / removeFile / writeCSN, and reading entries at load) can reach Keeper when the table's disk stores its metadata in Keeper (metadata_type='keeper'). With enforce_keeper_component_tracking=1, the ZooKeeper request path requires a thread-local component scope set via Coordination::setCurrentComponent. Without it the request raises LOGICAL_ERROR: Current component is empty, please set it for your scope using Coordination::setCurrentComponent, which is caught and handled in release builds but aborts the server in debug and sanitizer builds.

One concrete trigger: KILL MUTATION on such a table -> StorageMergeTree::killMutation -> MergeTreeMutationEntry::removeFile -> disk existsFile -> Keeper multi request with no component scope up the stack.

StorageSharedMergeTree already annotates its Keeper-touching call sites with setCurrentComponent guards; StorageMergeTree had none. This adds a scoped guard at the entry of every StorageMergeTree method that does MergeTreeMutationEntry disk I/O: startMutation, setMutationCSN, killMutation, loadMutations, clearOldMutations. Together these cover all nine mutation-entry I/O sites in the file.

The guards are unconditional, valid public code and a harmless no-op in builds without a keeper-metadata disk (plain disks do not route these operations through Keeper), matching the accepted StorageSharedMergeTree guard pattern. No behavior, on-disk format, or setting change.

No related open issue found (checked open/closed issues and open PRs). A targeted regression test would need a keeper-based metadata disk (metadata_type='keeper'), whose registration is gated behind a build flag not available in a community build, so this is a source-only fix; the affected path is not reachable on a plain community checkout.

Version info

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

@groeneai

groeneai commented Jul 3, 2026

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Deterministic by code path (KILL MUTATION etc. on a metadata_type='keeper' disk under enforce_keeper_component_tracking=1 -> Keeper request with no component scope -> LOGICAL_ERROR). Not reproducible on a community checkout: the keeper-metadata disk registration is behind a build flag (unknown metadata storage type: keeper), so a runtime repro is not possible in a public build.
b Root cause explained? StorageMergeTree mutation-file methods do MergeTreeMutationEntry disk I/O (commit/removeFile/writeCSN, and reading entries). On a keeper-metadata disk that I/O reaches Keeper; with enforce_keeper_component_tracking=1 the ZooKeeper request path asserts a thread-local component scope. StorageMergeTree set none, so the request raises LOGICAL_ERROR: Current component is empty (aborts in debug/sanitizer builds).
c Fix matches root cause? Directly sets the missing component scope with Coordination::setCurrentComponent(...) at the entry of each affected method, exactly the mechanism enforce_keeper_component_tracking checks. Same guard pattern StorageSharedMergeTree/StorageKeeperMap already use.
d Test intent preserved / new tests added? No existing test modified. A regression test requires a keeper-metadata disk unavailable in a community build, so this is source-only (the private side keeps a keeper-metadata regression test).
e Both directions demonstrated? Runtime abort not reproducible on a public checkout (see a). Verified instead that the guards compile+link and are a harmless no-op on a plain disk: with enforce_keeper_component_tracking=1 server-side, startup loadMutations, ALTER ... UPDATE (startMutation/setMutationCSN) and KILL MUTATION all succeed with no LOGICAL_ERROR and the server stays alive.
f Fix is general across code paths? Grepped all nine MergeTreeMutationEntry disk-I/O sites in the file (commit/removeFile/writeCSN/ctor); every one is inside one of the five guarded methods (startMutation, setMutationCSN, killMutation, loadMutations, clearOldMutations). No unguarded sibling site remains.
g Fix generalizes across inputs? N/A — not an input/datatype-dependent code bug. The trigger is the disk metadata type + server setting, both covered by guarding the methods regardless of mutation contents.
h Backward compatible? Yes. No setting default, on-disk/wire format, or validation change. Guards are unconditional public code; on non-keeper disks they set a thread-local string that is never read.
i Invariants and contracts preserved? Yes. ComponentGuard is a scoped RAII guard (restores the previous component in its destructor, non-movable/non-copyable) placed at method entry so it covers all paths incl. early return/exception. No locking, ordering, or data-structure invariant is touched.

Origin: handoff of a public-code fix that syncs to the private repo; the affected src/Storages/StorageMergeTree.cpp has no cloud-only markers, so the fix belongs upstream.

Session id: cron:clickhouse-worker-slot-4:20260703-131800

@groeneai

groeneai commented Jul 3, 2026

Copy link
Copy Markdown
Collaborator Author

cc @CurtizJ @alesapin — could you review this? It adds Coordination::setCurrentComponent guards to the StorageMergeTree mutation-file methods (startMutation/setMutationCSN/killMutation/loadMutations/clearOldMutations) so their MergeTreeMutationEntry disk I/O has a component scope when it reaches Keeper on a keeper-metadata disk, matching the existing StorageSharedMergeTree pattern. Without it, enforce_keeper_component_tracking=1 raises LOGICAL_ERROR: Current component is empty (e.g. on KILL MUTATION).

@Algunenano Algunenano added the can be tested Allows running workflows for external contributors label Jul 3, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jul 3, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [cbccea3]

Summary: ❌

job_name test_name status info comment
Finish Workflow FAIL
python3 ./ci/jobs/scripts/workflow_hooks/new_tests_check.py FAIL IGNORED

AI Review

Summary

This PR adds Coordination::setCurrentComponent guards to the StorageMergeTree mutation-entry paths that can touch Keeper-backed metadata, and the current head correctly widens the scope in alter and startMutation so rollback- and destructor-driven removeFile paths stay covered. I traced the remaining MergeTreeMutationEntry read/write/remove sites in StorageMergeTree and did not find any unguarded keeper-facing mutation-file I/O left in the PR head.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Jul 3, 2026
@PedroTadim
PedroTadim requested a review from Algunenano July 3, 2026 13:42
@groeneai

groeneai commented Jul 3, 2026

Copy link
Copy Markdown
Collaborator Author

CI finish ledger — f9233ea

Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task whose fixing-PR link will be posted here when it opens. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Stateless tests (amd_asan_ubsan, distributed plan, parallel, 1/2) / 01550_create_map_type flaky (CI diagnosis: 5/8 reruns failed; 9 unrelated PRs + 1 master in 30d; unrelated to this PR's StorageMergeTree keeper-component guards) a fix task is created (investigating at full effort, fixing-PR link to follow here)
CH Inc sync — CH Inc sync (private, not actionable)

No PR-caused failure: this PR touches only src/Storages/StorageMergeTree.cpp (5 setCurrentComponent guards) and cannot affect a Map-type stateless test.

Session id: cron:our-pr-ci-monitor:20260703-173000

@groeneai

groeneai commented Jul 3, 2026

Copy link
Copy Markdown
Collaborator Author

Ledger update for the 01550_create_map_type line above: its owner is a fixing PR, not a fix task.

Fixing PR: #109178 (external, open) "Preserve original key order in bucketed Map serialization".

Root cause: bucketed Map serialization (#99200) reorders map keys by hash bucket, breaking positional ColumnMap::compareAt and the operations that depend on it (ORDER BY, DISTINCT, GROUP BY, min/max, equality filters on Map). 01550_create_map_type builds MergeTree tables with a Map column as the ORDER BY key and compares Map results, so it is a direct victim (signature: "result differs with reference"). #109178 records a MapBucketIndexes substream to reconstruct original key order on read, fixing the mismatch at the source. Not caused by this PR (StorageMergeTree keeper-component guards are unrelated to Map serialization).

Our own competing PR #104540 was closed 2026-07-02 in favour of #109178.

@groeneai
groeneai force-pushed the fix-storagemergetree-keeper-component-tracking branch from f9233ea to 970ba2d Compare July 4, 2026 11:28
@groeneai

groeneai commented Jul 4, 2026

Copy link
Copy Markdown
Collaborator Author

Rebased onto current master to resolve the merge conflict; force-pushed f9233ea -> 970ba2d. Conflict was mechanical: master's recent change to loadMutations() dropped the currently_processing_in_background_mutex lock (replaced with a constructor-only chassert, STID 3367-4813); this PR added the setCurrentComponent guard at that method's entry. Resolution keeps both — guard first, then master's comment + chassert. The net change is logically identical to the approved diff (verified: the added/removed line-set is byte-for-byte unchanged; still 1 file, +12/-0). Re-verified on the rebased binary: capped server with enforce_keeper_component_tracking=1, exercised loadMutations (ATTACH) / startMutation + setMutationCSN (ALTER UPDATE) / killMutation on a plain MergeTree table — no LOGICAL_ERROR, server healthy.

Comment thread src/Storages/StorageMergeTree.cpp Resolved
StorageMergeTree methods that perform MergeTreeMutationEntry disk I/O
(commit / removeFile / writeCSN, and reading entries at load) can reach
Keeper when the table's disk stores metadata in Keeper
(metadata_type='keeper'). With enforce_keeper_component_tracking=1 the
ZooKeeper request path requires a thread-local component scope set via
Coordination::setCurrentComponent; without it the request throws
LOGICAL_ERROR: "Current component is empty, please set it for your scope
using Coordination::setCurrentComponent", which aborts the server in
debug and sanitizer builds.

One concrete trigger: KILL MUTATION on a MergeTree table on a
keeper-metadata disk -> killMutation -> MergeTreeMutationEntry::removeFile
-> disk existsFile -> Keeper multi request with no component scope.

StorageSharedMergeTree already annotates its Keeper-touching call sites
with setCurrentComponent guards; StorageMergeTree had none. Add a scoped
guard covering every path that does MergeTreeMutationEntry disk I/O:

- setMutationCSN, killMutation, loadMutations, clearOldMutations: guard at
  method entry.
- alter and startMutation: guard at method entry rather than inside the
  prepareMutationEntry helper. The helper commits mutation_*.txt but leaves
  the entry unregistered (is_registered == false) and returns it to the
  caller; the caller may then remove that file LATER and OUTSIDE the helper
  scope -- on the pre-commit unwind (the PreparedMutationEntry destructor
  runs removeFile), on the explicit alter rollback (held_entry->removeFile),
  or when addPreparedMutationEntry throws in startMutation. Guarding the
  whole caller path keeps the component scope active across the commit AND
  the rollback removeFile. prepareMutationEntry itself no longer sets a
  guard because both of its callers now do.

These cover all mutation-entry disk I/O sites in the file, including the
destructor removeFile paths.

The guards are unconditional, valid public code and a harmless no-op in
builds without a keeper-metadata disk (plain disks do not route these
operations through Keeper). No behavior, format, or setting change.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@groeneai
groeneai force-pushed the fix-storagemergetree-keeper-component-tracking branch from 970ba2d to 4e962f4 Compare July 4, 2026 12:51
@groeneai

groeneai commented Jul 4, 2026

Copy link
Copy Markdown
Collaborator Author

You are right, that was a real hole. prepareMutationEntry returns the MergeTreeMutationEntry (still is_registered == false) to alter, and its file is removed later and outside the helper scope:

  • pre-commit unwind (StorageMergeTree.cpp:542-566): the std::optional<PreparedMutationEntry> prepared destructs during unwinding -> ~MergeTreeMutationEntry (MergeTreeMutationEntry.cpp:204-211, !is_registered) -> removeFile() -> disk->existsFile/removeFileIfExists;
  • explicit rollback (StorageMergeTree.cpp:702): held_entry->removeFile().

Both run after prepareMutationEntry has already returned, so a helper-local guard cannot cover them. Fixed by moving the scope to the whole caller path:

  • guard at the entry of StorageMergeTree::alter (covers the unwind destructor at 542-566 and the explicit held_entry->removeFile() at 702);
  • guard at the entry of StorageMergeTree::startMutation (symmetric case: if addPreparedMutationEntry throws, the unregistered by-value entry destructs -> removeFile);
  • prepareMutationEntry no longer sets its own guard, since both callers now hold one spanning the commit AND the rollback removeFile.

setMutationCSN, killMutation, loadMutations, clearOldMutations keep their entry guards (independent external entry points). That now covers every MergeTreeMutationEntry disk-I/O site in the file, including the destructor removeFile paths.

Pushed as 4e962f4. Verified on a plain-disk build with enforce_keeper_component_tracking=1: ALTER UPDATE, RENAME COLUMN, KILL MUTATION, and DETACH/ATTACH all run with no Current component is empty LOGICAL_ERROR (the keeper-metadata rollback itself is not reproducible on a community checkout, as noted in the description).

@groeneai

groeneai commented Jul 4, 2026

Copy link
Copy Markdown
Collaborator Author

CI finish ledger — 4e962f4

CI fully finished on this head (Finish Workflow completed 2026-07-04T16:44:02Z, no checks queued/in_progress). Every failure has an owner.

Check / test Reason Owner / fixing PR
Finish Workflow :: Post Hooks infra (praktika finalization post-hook transient, 195 hits / 87 PRs / 0 master in 14d; not PR-caused) a fix task is created (investigating at full effort — fixing PR link to follow here)

No test-level failures. Post Hooks is the praktika Finish-Workflow finalization step (report upload / CIDB insert), unrelated to this PR's Keeper-component tracking change.

Session id: cron:our-pr-ci-monitor:20260704-170000

@Algunenano Algunenano self-assigned this Jul 6, 2026

@Algunenano Algunenano left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@groeneai Please don't be so verbose with the comments. In this case they are unnecessary. Please remove them

@PedroTadim

PedroTadim commented Jul 7, 2026 •

Copy link
Copy Markdown
Member

@groeneai please trim the comments. I need this PR to get merged

Requested by reviewers: the setCurrentComponent guards mirror the
existing StorageSharedMergeTree pattern and are self-explanatory; the
rationale lives in the PR description.

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

groeneai commented Jul 7, 2026

Copy link
Copy Markdown
Collaborator Author

@Algunenano done, comments removed in cbccea3. The guards mirror the StorageSharedMergeTree pattern and the rationale is in the PR description.

@clickhouse-gh

clickhouse-gh Bot commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.70% 85.70% +0.00%
Functions 92.70% 92.60% -0.10%
Branches 77.90% 77.90% +0.00%

Changed lines: Changed C/C++ lines covered: 13/14 (92.86%) · Uncovered code

Full report · Diff report

@groeneai

groeneai commented Jul 7, 2026

Copy link
Copy Markdown
Collaborator Author

CI finish ledger — cbccea3

CI is complete. This commit only trims code comments (per @Algunenano / @PedroTadim) — the guard logic is unchanged. The only non-green check is a pre-existing perf-comparison instability, not caused by this PR.

Check / test Reason Owner / fixing PR
Performance Comparison (arm_release, master_head, 5/6) 22 unstable + 34 slower queries, 5124 rows success a fix task is investigating the arm_release perf-comparison "unstable queries / errors while building the report" class at full effort
Finish Workflow (Post Hooks) / Mergeable Check / PR aggregators reflecting the perf-comparison result above same
CH Inc sync private ClickHouse-Inc sync CH Inc sync (private, not actionable by us)

Session id: cron:our-pr-ci-monitor:20260707-220000

@PedroTadim
PedroTadim added this pull request to the merge queue Jul 9, 2026
Merged via the queue into ClickHouse:master with commit 8a863a7 Jul 9, 2026
341 of 345 checks passed
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 9, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…ee-keeper-component-tracking

Set Keeper component in StorageMergeTree mutation-file methods
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 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

Development

Successfully merging this pull request may close these issues.

4 participants