Skip to content

Fix flaky test 03100_lwu_07_merge_patches - #108594

Merged
alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:fix-flaky-03100-lwu-07-merge-patches-level
Jul 29, 2026
Merged

alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
groeneai:fix-flaky-03100-lwu-07-merge-patches-level

Conversation

@groeneai

Copy link
Copy Markdown
Collaborator

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 flaky test 03100_lwu_07_merge_patches.

Description

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 (count() = 13 holds); only the level subfield flips, which made the name assertion non-deterministic.

Note that SYSTEM STOP MERGES is not a viable alternative here: an explicit OPTIMIZE ... PARTITION ... FINAL throws ABORTED "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 with replaceRegexpOne, 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.

Public CI evidence (play.clickhouse.com, 30d): 2 master failures (2026-06-07 Stateless tests (amd_debug, distributed plan, s3 storage, parallel), 2026-06-11 Stateless tests (arm_asan_ubsan, azure, parallel)) plus failures across several unrelated PRs, all showing the identical _1_10 vs _2_10 level diff.

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]>
@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. A patch part merged from level-0 sources lands at level 1 (..._3_11_1_10); when a stray background merge first bumps a source to level 1, OPTIMIZE FINAL merges {level-1, level-0} and lands at level 2 (..._3_11_2_10). Reproduced on demand locally (two-step: OPTIMIZE FINAL, one more UPDATE, OPTIMIZE FINAL -> level 2). Matches the exact CIDB-documented diff _1_10 vs _2_10.
b Root cause explained? Yes. Merged part level = max(source levels) + 1. A background merge bumps a subset of patch parts to level 1 before the explicit OPTIMIZE ... PARTITION ... FINAL, so the merged patch part is level 2 instead of level 1. The test asserts on system.parts.name, which encodes the level subfield, so the assertion is non-deterministic. Row data is always correct.
c Fix matches root cause? Yes. Masks exactly and only the volatile merge-level subfield of the patch part name via replaceRegexpOne, leaving block range, mutation version, row counts and ordering asserted. Not a band-aid (no widened bounds, no no-random-*, no data reduction). SYSTEM STOP MERGES was rejected because an explicit OPTIMIZE ... PARTITION ... FINAL throws ABORTED "Cancelled merging parts" under stopped merges (StorageMergeTree::merge:1457), and re-enabling merges before the OPTIMIZE reopens the race.
d Test intent preserved / new tests added? Yes. Still asserts the patch parts (block range _3_11_, mutation _10), the row count 13, the 5-to-1 patch-part collapse after OPTIMIZE FINAL, the full SELECT * data, and count()=13. Only the noise level digit is masked. No source change, so no new regression test required.
e Both directions demonstrated? Yes. Old reference ..._3_11_1_10 != CIDB race value ..._3_11_2_10 (old test FAILS on the race); both _1_10 and _2_10 mask to ..._3_11_<lvl>_10 (new test PASSES). Runner: 100/100 passes with randomization enabled.
f Fix is general across code paths? N/A (test-only assertion-masking fix, not a code bug).
g Fix generalizes across inputs (params/datatypes/wrappers)? N/A (test-only assertion-masking fix, not a code bug).
h Backward compatible? (maintainer-approved exception only) N/A (test-only assertion-masking fix, not a code bug).
i Invariants and contracts preserved? N/A (test-only assertion-masking fix, not a code bug).

Session id: cron:clickhouse-worker-slot-7:20260626-102900

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @CurtizJ could you review this? It de-flakes 03100_lwu_07_merge_patches by masking only the volatile merge-level subfield of the patch part name in the two system.parts assertions: a background merge can bump a patch part to level 1 before the explicit OPTIMIZE FINAL, so the merged part lands at level 2 (_3_11_2_10) instead of 1 (_3_11_1_10). Row counts, block range, mutation version and the 5-to-1 part collapse are still asserted.

@nikitamikhaylov nikitamikhaylov added the can be tested Allows running workflows for external contributors label Jun 26, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jun 26, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [38257a2]

Summary: ✅


AI Review

Summary

This PR de-flakes 03100_lwu_07_merge_patches by masking only the merge-level component of patch-part names in the two system.parts assertions while keeping the row data, block ranges, mutation versions, and the post-OPTIMIZE FINAL part collapse under test. I reviewed the current diff and prior PR discussion and did not find a correctness, coverage, or PR-metadata issue that warrants an inline review comment.

Final Verdict

✅ No findings.

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Jun 26, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.30% 85.30% +0.00%
Functions 92.60% 92.60% +0.00%
Branches 77.50% 77.50% +0.00%

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
  • src/Disks/IO/CachedOnDiskReadBufferFromFile.cpp: 76 line(s), 1 function(s)
  • src/Interpreters/DeltaMetadataLog.cpp: 19 line(s), 1 function(s)
  • src/AggregateFunctions/AggregateFunctionQuantileGK.cpp: 15 line(s)
  • src/Functions/catboostEvaluate.cpp: 14 line(s), 1 function(s)
  • src/Storages/ObjectStorageQueue/ObjectStorageQueueUnorderedFileMetadata.cpp: 12 line(s), 1 function(s)

Full report

@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger — bd140d6

Every failure below has an owner; none is in this PR's diff (03100_lwu_07_merge_patches.sql/.reference), so none is PR-caused.

Check / test Reason Owner / fixing PR
Stateless (arm_binary, parallel) / 03518_alter_logical_race flaky, 32 PRs / 3 master in 30d #108060 (fixes flaky 03518)
Fast test (arm_darwin) / 03988_keeper_client_autocomplete flaky on macOS, 5 unrelated PRs / 0 master in 30d a fix task is created (investigating at full effort; fixing-PR link to follow here)
Mergeable Check / PR aggregators reflecting the reds above n/a

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

@groeneai

Copy link
Copy Markdown
Collaborator Author

Ledger update: Fast test (arm_darwin) / 03988_keeper_client_autocomplete

Fixing PR: #107438 (ours, open).

This flaky line has a root-cause fix already in flight. All recent failures (5/5 unrelated PRs in 30d, 0 master, incl. this PR's bd140d69) share one signature: every autocomplete assertion in stdout passes (command_completion: OK, leaf_completion: OK, ...); the FAIL is purely Reason: having stderror from a benign Net Exception: Socket is not connected (ENOTCONN, errno 57) at Poco::Net::SocketImpl::shutdown() -> Coordination::ZooKeeper::finalize() -> ~ZooKeeper() -> KeeperClient::main. It is macOS-only teardown noise; the command output is correct.

#107438 downgrades exactly that socket.shutdown() ENOTCONN case in ZooKeeper::finalize() to TRACE (a server-side log-level change, so it covers every keeper-client command, not only the 03135 it was opened for). That removes the spurious stderr and de-flakes this test. No per-test change needed here.

@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger — 38257a2

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
(none) — 174 checks completed, 0 failures

CI is fully finished on this head with no failing checks.

Session id: cron:our-pr-ci-monitor:20260729-103000

@alexey-milovidov alexey-milovidov self-assigned this Jul 29, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Jul 29, 2026
Merged via the queue into ClickHouse:master with commit c2967e0 Jul 29, 2026
180 checks passed
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 29, 2026
chhetripradeep pushed a commit to chhetripradeep/ClickHouse that referenced this pull request Aug 12, 2026
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.
github-actions Bot pushed a commit to Altinity/ClickHouse that referenced this pull request Aug 17, 2026
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)
github-actions Bot pushed a commit to Altinity/ClickHouse that referenced this pull request Aug 17, 2026
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)
github-actions Bot pushed a commit to Altinity/ClickHouse that referenced this pull request Aug 17, 2026
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)
pull Bot pushed a commit to SINHASantos/ClickHouse that referenced this pull request Aug 26, 2026
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.
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-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