Repository navigation
Fix LOGICAL_ERROR in OPTIMIZE ... DRY RUN racing a concurrent merge - #110916
alexey-milovidov merged 13 commits into
Conversation
MergeTreeData::optimizeDryRun executes a real MergeTask but, unlike the
real optimize/merge paths, takes no CurrentlyMergingPartsTagger and no
merge lock, so it does not register its source parts as being merged. A
concurrent real merge (or another DRY RUN) on the same parts computes the
same result part name and reserves the same tmp_merge_<result> directory,
tripping TemporaryParts::add ("Temporary part ... already added") and
aborting the server on debug/sanitizer builds.
The dry-run part is throwaway, so give its temporary directory a unique
_dry_run_<uuid> suffix. This makes the collision impossible on both
MergeTree and ReplicatedMergeTree. Real merges are unaffected: all real
callers of mergePartsToTemporaryPart pass an empty suffix, and the
directory still starts with tmp_merge_ so cleanup and startup logic match.
Add regression test 03823_optimize_dry_run_merge_race and a test-only
failpoint that pauses right after the temporary directory is reserved.
Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-author-slot-2:20260717-220147 |
Internal second-model review — adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 3 findings across the review + delta recheck; bounded: 1 full pass + 1 fix round + 1 delta recheck).
Severity: ❌ blocker / Session id: cron:clickhouse-author-slot-2:20260717-220147 |
|
cc @CurtizJ @alesapin — could you review this? It fixes a |
|
Workflow [PR], commit [446c75c] Summary: ✅
AI ReviewSummaryThis PR changes Final Verdict✅ No new findings. LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 38/38 (100.00%) · Uncovered code |
CI finish ledger — ac7cfc9CI fully finished on this head (Finish Workflow SUCCESS). One failure, owned:
Not PR-caused (unrelated to the OPTIMIZE DRY RUN temp-dir-collision fix this PR makes; pre-existing chronic trunk signature with an open fixing PR). Session id: cron:our-pr-ci-monitor:20260718-033000 |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 32/32 (100.00%) · Uncovered code |
A review round pointed out that appending a uniqueness token to the part name, as the first version of this fix did, can push the temporary directory past the filename limit while the corresponding real merge still fits. Part names are not length-capped: an all-integral partition key spells its values out, so ten `Int64` fields plus an `IPv4` give a 226-byte name, and `MergeTree` renames a part to `delete_tmp_<dir>` before removing it, so at that length only 8 of the 255 bytes are left. `OPTIMIZE ... DRY RUN` then failed with `ENAMETOOLONG` where an ordinary `OPTIMIZE` of the same parts succeeded. Trying to keep the token inside that remaining room does not work either: it has to be truncated for short part names, and a truncated token stops being unique (two dry runs can render the same name and re-trip the collision) or loses the marker that identifies the directory as a dry run. So do not put the part name there at all. The dry-run part is throwaway, so its directory is now `tmp_merge_dry_run_<uuid>`, which cannot collide with the merge of those parts and has a fixed size, independent of the part name. A UUID rather than a counter, because the directory can live on storage shared with other servers, where a process-local counter restarts and repeats itself. Nothing parses this name back into a part name, and it keeps the `tmp_merge_` prefix, which the temporary directory cleaner and the startup skip logic select by, so both still recognize it. The regression test races two dry runs across the part-name range (7, 9 and 226 bytes), asserts that the real merge of the 226-byte parts succeeds -- which is what makes a failing dry run a bug rather than a filesystem limit -- and checks each dry run's own merge counters, so an implementation that returned success without merging would no longer pass. It needs no blanket randomization tags: only the counter query has to opt out of parallel replicas, since the test's single node is not part of the randomized cluster definition. Co-Authored-By: Claude Fable 5 <[email protected]>
CI finish ledger - a9d634cEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task Both reds are one event: a pre-existing trunk
Everything else on this head is green: all 13 builds, Session id: cron:our-pr-ci-monitor:20260729-233000 |
|
Fixing PR for the plain_rewritable That is a revert of #111883 by @ alexey-milovidov, merged 2026-07-30T09:07:30Z, which restores the The race is verified gone on trunk, not just merged: keyed on the This head predates the revert, so the red here clears once the branch picks up master. One correction to my earlier ledger note on the onset: I flagged two possible pre-#111883 hits |
|
The The run reported two failures, It came from The relevant timeline:
The fix landed about twelve hours after the run, so it could not have been present. The updated branch ( |
CI finish ledger - 936d47eEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
Session id: cron:our-pr-ci-monitor:20260804-043000 |
# Conflicts: # src/Common/FailPoint.cpp # src/Storages/MergeTree/MergeTask.cpp
CI finish ledger - 8a565ddEvery failure below has an owner: a fixing PR (ours or external), or a full-effort fix task
Root cause, for the record: Nothing was pushed from this run: the newest commit on this branch is a maintainer's master merge, Session id: cron:our-pr-ci-monitor:20260820-043000 |
`need_prefix` was removed from `mergePartsToTemporaryPart` on master in `771c888fc443`, so after merging master the `optimizeDryRun` call site added by this pull request passed one argument too many and the build failed with `too many arguments to function call, expected at most 16, have 17`. CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=110916&sha=8a565dd963809c062999149848fdbfe92fde5d08&name_0=PR&name_1=Fast%20test Related: ClickHouse#110916
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction. Compile time of recompiled translation units60 translation units recompiled, 715 s compile time in total, 60 of them have a recent master baseline. Median compile-time ratio to the baselines is ×1.09 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio. |
|
Co-Authored-By: Claude Fable 5 <[email protected]>
alexey-milovidov
left a comment
There was a problem hiding this comment.
I will reset the submodule changes.
The fix LGTM.
I don't remember why we have the OPTIMIZE DRY RUN feature. What was the initial motivation for adding it?
It was introduced in #96122 ( |
CI finish ledger - 1b2c23eEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
No test-level failure was recorded at this head (302226 CIDB rows, 0 Session id: cron:our-pr-ci-monitor:20260821-083000 |
Address review: `tmp_merge_dry_run_<uuid>` was 54 bytes, and 65 once `IMergeTreeDataPart::remove` renames the directory to `delete_tmp_<dir>`, which is a larger fixed filename budget than a merge of ordinary parts takes. Keep only the low half of the UUID, written as 16 hexadecimal digits, so the basename is `tmp_merge_dry_run_<16 hex>` - 34 bytes, 45 after the rename - while the `dry_run_` marker that `clearOldTemporaryDirectories` and the startup skip logic select by, and the uniqueness the fix relies on, are both preserved. `MergeTask` now logs the temporary directory it reserved, and the test pins the resulting length in both directions.
`CLICKHOUSE_CLIENT` already carries `--send_logs_level` when `CLICKHOUSE_CLIENT_SERVER_LOGS_LEVEL` is set, and the client rejects the option twice with `option '--send_logs_level' cannot be specified more than once`. A query-level `SETTINGS` clause overrides it either way.
CI finish ledger - 2c212aaEvery failure below has an owner: a fixing PR (ours or external), or a full-effort fix task
One further note for the reader: The Session id: cron:our-pr-ci-monitor:20260823-063000 |
…dr_<12 hex>` The AI review asked for the temporary directory of `OPTIMIZE ... DRY RUN` to be derived from the budget of the merge it simulates, rather than pinning a larger fixed one. A dry run cannot claim exactly that budget: reusing the name of the real merge is what collides in the first place, and the `LOGICAL_ERROR` this pull request fixes would simply be raised by the real merge instead. What can be reduced is the excess, so the marker is now `dr_` and the token 48 random bits written as 12 hexadecimal digits. The basename is `tmp_merge_dr_<12 hex>`: 25 bytes instead of 34, 36 instead of 45 once `IMergeTreeDataPart::remove` prepends `delete_tmp_`. That is 6 bytes more than a merge of the shortest possible pair of parts and no more than a merge of any part whose name is 15 characters or longer. The `-- temporary directory budget` section of the test now merges the same pair of parts for real and prints the difference between the two directory names, instead of pinning the absolute lengths.
|
🕵 Pushed
Built and ran |
|
📊 Cloud Performance Report ✅ AI verdict: no significant changes detected. K_source=6 K_base=30 flagged=0/65 clickbench🟢 No significant changes tpch_adapted_1_official🟢 No significant changes Debug info
|
|
Measured per branch, master as the positive control: 26.3, 26.6, 26.7 and 26.8 all still carry CIDB over 90 days on Since the merge master is at 113 shas with zero occurrences, Port cost, three-way per file (base
What those hunks lack is @alexey-milovidov could you apply |
Closes: #114399
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixes a
LOGICAL_ERRORexception (Temporary part ... already added) whenOPTIMIZE ... DRY RUNruns concurrently with a real merge, or with anotherDRY RUN, on the same parts.Description
MergeTreeData::optimizeDryRunexecutes a realMergeTaskbut, unlike the real optimize/merge paths, takes noCurrentlyMergingPartsTaggerand no merge lock, so it never registers its source parts as "being merged". A concurrent real merge on the same parts computes the same result part name and reserves the sametmp_merge_<result>directory. The second reservation tripsTemporaryParts::add->LOGICAL_ERROR "Temporary part ... already added", aborting the server on debug and sanitizer builds. Seen on 17 unrelated PRs over the 30 days to 2026-08-11 (CIDBdefault.checks, 17 rows / 17 distinct PRs), never on master.The dry-run part is throwaway, so its directory need not be named after the result part. It is now
tmp_merge_dry_run_<uuid>, making the collision impossible on bothMergeTreeandReplicatedMergeTree. A UUID rather than a counter, because the directory can live on storage shared with other servers, where a process-local counter restarts and repeats itself.Leaving the part name out also avoids a length trap: appending a token to it can exceed the 255-byte filename limit while the real merge of the same parts still succeeds. Part names are not length-capped (an all-integral partition key spells its values out, so ten
Int64fields plus anIPv4reach 226 bytes) andMergeTreeprependsdelete_tmp_before removal, leaving 8 bytes. The new name has a fixed size.It is never parsed back into a part name and still starts with
tmp_merge_, which the cleaner and the startup skip logic select by. Only adry_run_suffix takes the new path, so top-level and projection merges keep byte-identical names.Not a duplicate of the
DRY RUNfix linked above (rows_sources under a soft timeout). Tracking issue: #114399.Test
03823_optimize_dry_run_merge_raceraces twoDRY RUNs on the same parts via a new failpoint pausing after the directory is reserved, at part-name lengths 7, 9 and 226, asserting the real merge succeeds at 226 and eachDRY RUNreally merged.Workflow [PR]
Sync PR [sync-upstream/pr/110916]