Repository navigation
Fix flaky test 03100_lwu_07_merge_patches - #108594
alexey-milovidov merged 2 commits into
Conversation
The test asserted on the exact patch part name returned by system.parts after OPTIMIZE TABLE ... PARTITION ID '...' FINAL. The patch part name encodes the merge level (the third numeric subfield, e.g. _3_11_1_10). A background merge can bump the level of some patch parts before the explicit OPTIMIZE FINAL runs, so the merged part lands at level 2 instead of level 1 (..._3_11_2_10 instead of ..._3_11_1_10). The merged level is max(source levels) + 1, so a stray level-1 source pushes the result to level 2. The row data is always correct; only the level subfield flips, which made the name assertion non-deterministic. Mask the merge level subfield in the patch part names with replaceRegexpOne while keeping the rest of the name (block range and mutation version), the row counts, the part collapse (5 patch parts to 1 after OPTIMIZE FINAL) and the data-correctness checks intact. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-worker-slot-7:20260626-102900 |
|
cc @CurtizJ could you review this? It de-flakes |
|
Workflow [PR], commit [38257a2] Summary: ✅ AI ReviewSummaryThis PR de-flakes Final Verdict✅ No findings. |
LLVM Coverage Report
Changed lines: No C/C++ source files changed — skipping uncovered code analysis. Newly covered by added/modified tests: 403 line(s), 28 function(s) across 116 file(s) · Details Top files
|
CI finish ledger — bd140d6Every failure below has an owner; none is in this PR's diff (
Session id: cron:our-pr-ci-monitor:20260626-173000 |
Ledger update:
|
CI finish ledger — 38257a2Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task
CI is fully finished on this head with no failing checks. Session id: cron:our-pr-ci-monitor:20260729-103000 |
The test lists the five patch parts produced by its five lightweight UPDATEs
from system.parts, and only then runs an explicit
OPTIMIZE TABLE ... PARTITION ID 'patch-...' FINAL and asserts they collapsed
into one part. That first listing was not deterministic.
Patch parts are ordinary background merge candidates: MergeTreePartsCollector
declares affordable_kinds{Regular, Patch} (same in the replicated collector),
and nothing in selection exempts them. Five tiny parts in one partition are an
ideal merge range, so a background merge races the test's next statement and
sometimes collapses them into all_3_11_<lvl>_10 with 13 rows before the first
listing runs.
PR ClickHouse#108594 masked the merge level in the patch part names, which fixed the
variant where only the level differed. This is the same race caught earlier,
where the block range and the row count change too, so a level mask cannot
express it.
Pinning max_bytes_to_merge_at_max_space_in_pool = 1 stops background merges
from selecting the patch parts, while the explicit OPTIMIZE FINAL still merges
them: it goes through selectAllPartsToMergeWithinPartition, which never reads
that limit. The setting's own documentation states this contract, and
04546_text_index_merge_fsync and 04654_text_index_merge_with_empty_part
already use the idiom for the same purpose. The assertion is unchanged and
.reference is untouched.
Validated by forcing the background merge to win deterministically: without
the pin the test fails with exactly the reported diff, with the pin it passes
and the explicit OPTIMIZE still merges the five parts into one. 50/50 passes
under full randomization.
The test lists the five patch parts produced by its five lightweight UPDATEs
from system.parts, and only then runs an explicit
OPTIMIZE TABLE ... PARTITION ID 'patch-...' FINAL and asserts they collapsed
into one part. That first listing was not deterministic.
Patch parts are ordinary background merge candidates: MergeTreePartsCollector
declares affordable_kinds{Regular, Patch} (same in the replicated collector),
and nothing in selection exempts them. Five tiny parts in one partition are an
ideal merge range, so a background merge races the test's next statement and
sometimes collapses them into all_3_11_<lvl>_10 with 13 rows before the first
listing runs.
PR ClickHouse#108594 masked the merge level in the patch part names, which fixed the
variant where only the level differed. This is the same race caught earlier,
where the block range and the row count change too, so a level mask cannot
express it.
Pinning max_bytes_to_merge_at_max_space_in_pool = 1 stops background merges
from selecting the patch parts, while the explicit OPTIMIZE FINAL still merges
them: it goes through selectAllPartsToMergeWithinPartition, which never reads
that limit. The setting's own documentation states this contract, and
04546_text_index_merge_fsync and 04654_text_index_merge_with_empty_part
already use the idiom for the same purpose. The assertion is unchanged and
.reference is untouched.
Validated by forcing the background merge to win deterministically: without
the pin the test fails with exactly the reported diff, with the pin it passes
and the explicit OPTIMIZE still merges the five parts into one. 50/50 passes
under full randomization.
(cherry picked from commit a899045)
The test lists the five patch parts produced by its five lightweight UPDATEs
from system.parts, and only then runs an explicit
OPTIMIZE TABLE ... PARTITION ID 'patch-...' FINAL and asserts they collapsed
into one part. That first listing was not deterministic.
Patch parts are ordinary background merge candidates: MergeTreePartsCollector
declares affordable_kinds{Regular, Patch} (same in the replicated collector),
and nothing in selection exempts them. Five tiny parts in one partition are an
ideal merge range, so a background merge races the test's next statement and
sometimes collapses them into all_3_11_<lvl>_10 with 13 rows before the first
listing runs.
PR ClickHouse#108594 masked the merge level in the patch part names, which fixed the
variant where only the level differed. This is the same race caught earlier,
where the block range and the row count change too, so a level mask cannot
express it.
Pinning max_bytes_to_merge_at_max_space_in_pool = 1 stops background merges
from selecting the patch parts, while the explicit OPTIMIZE FINAL still merges
them: it goes through selectAllPartsToMergeWithinPartition, which never reads
that limit. The setting's own documentation states this contract, and
04546_text_index_merge_fsync and 04654_text_index_merge_with_empty_part
already use the idiom for the same purpose. The assertion is unchanged and
.reference is untouched.
Validated by forcing the background merge to win deterministically: without
the pin the test fails with exactly the reported diff, with the pin it passes
and the explicit OPTIMIZE still merges the five parts into one. 50/50 passes
under full randomization.
(cherry picked from commit a899045)
The test lists the five patch parts produced by its five lightweight UPDATEs
from system.parts, and only then runs an explicit
OPTIMIZE TABLE ... PARTITION ID 'patch-...' FINAL and asserts they collapsed
into one part. That first listing was not deterministic.
Patch parts are ordinary background merge candidates: MergeTreePartsCollector
declares affordable_kinds{Regular, Patch} (same in the replicated collector),
and nothing in selection exempts them. Five tiny parts in one partition are an
ideal merge range, so a background merge races the test's next statement and
sometimes collapses them into all_3_11_<lvl>_10 with 13 rows before the first
listing runs.
PR ClickHouse#108594 masked the merge level in the patch part names, which fixed the
variant where only the level differed. This is the same race caught earlier,
where the block range and the row count change too, so a level mask cannot
express it.
Pinning max_bytes_to_merge_at_max_space_in_pool = 1 stops background merges
from selecting the patch parts, while the explicit OPTIMIZE FINAL still merges
them: it goes through selectAllPartsToMergeWithinPartition, which never reads
that limit. The setting's own documentation states this contract, and
04546_text_index_merge_fsync and 04654_text_index_merge_with_empty_part
already use the idiom for the same purpose. The assertion is unchanged and
.reference is untouched.
Validated by forcing the background merge to win deterministically: without
the pin the test fails with exactly the reported diff, with the pin it passes
and the explicit OPTIMIZE still merges the five parts into one. 50/50 passes
under full randomization.
(cherry picked from commit a899045)
The test lists the five patch parts produced by its five lightweight UPDATEs
from `system.parts`, runs an explicit
`OPTIMIZE TABLE ... PARTITION ID 'patch-...' FINAL`, and lists them again to
assert they collapsed into one part. The name printed by the second listing was
not deterministic.
Patch parts are ordinary background merge candidates: `MergeTreePartsCollector`
declares `affordable_kinds{Regular, Patch}` and nothing in selection exempts
them. Five tiny parts in one partition are an ideal merge range, so a background
merge can collapse them into `patch-...-all_3_11_1_10` before the explicit
`OPTIMIZE` runs. The `OPTIMIZE` then re-merges that already-merged part, and
since a merged part's level is max(source levels) + 1
(`FutureMergedMutatedPart`), the printed name becomes `_3_11_2_10` while the
reference says `_3_11_1_10`. Only the level subfield differs; the row counts and
the data are unaffected.
The fix pins `max_bytes_to_merge_at_max_space_in_pool = 1` so the background
selector cannot pick the parts up, while the test's own `OPTIMIZE FINAL` still
merges them: merges initiated by `OPTIMIZE FINAL` ignore that limit, which is
documented on the setting and holds mechanically, since the only reader of it on
the merge path is `CompactionStatistics::getMaxSourcePartsBytesForMerge`, called
from the background selector, and
`MergeTreeDataMergerMutator::selectAllPartsToMergeWithinPartition` (the
`OPTIMIZE` path) consults no size limit at all.
The assertions are unchanged and `.reference` is untouched, so the test keeps
pinning the final level to exactly 1: if a background merge ever wins again, it
fails loudly rather than being masked.
This is the same fix ClickHouse#114236 applied to the v2 twin
(`03100_lwu_07_merge_patches`). That twin also carries a level mask from
ClickHouse#108594, but the mask is not needed here and is deliberately not added: it hides
only the level subfield, which the pin already makes deterministic, and it would
throw away the level assertion. The same idiom is used for the same purpose by
`03100_lwu_54_patch_merge_spanning_part_version`, added in ClickHouse#115064: it pins
`max_bytes_to_merge_at_max_space_in_pool = 1` with the comment "Only the
explicit `OPTIMIZE` below may merge the patch parts".
The v1 clone (`c324f831097ec4c`) was taken from a master state that predated
both hardenings, and ClickHouse#116007 restored the file byte-identically, so it never
received either one.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix flaky test
03100_lwu_07_merge_patches.Description
The test asserted on the exact patch part name returned by
system.partsafterOPTIMIZE TABLE ... PARTITION ID '...' FINAL. The patch part name encodes the merge level (the third numeric subfield, e.g...._3_11_1_10).A background merge can bump the level of some patch parts before the explicit
OPTIMIZE FINALruns, so the merged part lands at level 2 instead of level 1 (..._3_11_2_10instead of..._3_11_1_10). The merged level ismax(source levels) + 1, so a stray level-1 source pushes the result to level 2. The row data is always correct (count() = 13holds); only the level subfield flips, which made the name assertion non-deterministic.Note that
SYSTEM STOP MERGESis not a viable alternative here: an explicitOPTIMIZE ... PARTITION ... FINALthrowsABORTED "Cancelled merging parts"while merges are stopped (StorageMergeTree::merge), and re-enabling merges right before the OPTIMIZE reopens the same race. So this masks only the volatile level subfield in the patch part names withreplaceRegexpOne, keeping the rest of the name (block range and mutation version), the row counts, the part collapse (5 patch parts to 1 afterOPTIMIZE FINAL) and the data-correctness checks intact.Public CI evidence (play.clickhouse.com, 30d): 2 master failures (2026-06-07
Stateless tests (amd_debug, distributed plan, s3 storage, parallel), 2026-06-11Stateless tests (arm_asan_ubsan, azure, parallel)) plus failures across several unrelated PRs, all showing the identical_1_10vs_2_10level diff.