Skip to content

Fix 02378_part_log_profile_events flakiness in the MergeParts section - #110640

Merged
alexey-milovidov merged 8 commits into
ClickHouse:masterfrom
groeneai:fix-02378-part-log-mergeparts-flush-race
Aug 1, 2026
Merged

alexey-milovidov merged 8 commits into
ClickHouse:masterfrom
groeneai:fix-02378-part-log-mergeparts-flush-race

Conversation

@groeneai

@groeneai groeneai commented Jul 16, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #108572

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):

...

Description

02378_part_log_profile_events is flaky in the MergeParts section: it asserted count() > 2 on the MergeParts rows written by OPTIMIZE TABLE test FINAL.

That count is not a property of OPTIMIZE. StorageMergeTree::optimize loops over the partition ids and runs one foreground merge per partition, and the table has two partitions (PARTITION BY key >= 128), so OPTIMIZE itself contributes exactly two rows. count() > 2 therefore passed only when unrelated background merges happened to have run first, and failed with Error: 2 when they had not.

The fix makes both sides deterministic. max_bytes_to_merge_at_max_space_in_pool = 1 suppresses background merges, so the only merges in the table are the two driven by OPTIMIZE; the assertion becomes the exact expected values, count() == 2 and MergedRows == 512. The setting is only consulted when selecting a background merge (OPTIMIZE goes through selectAllPartsToMergeWithinPartition, which does not read it), so OPTIMIZE still merges every part of each partition. 36 stateless tests already pin this setting, and an explicit per-CREATE SETTINGS clause wins over the runner's randomization, so no new tag is needed.

This also removes the retry-flush loop added earlier in this pull request. It was unreachable: StorageMergeTree::merge finishes the merge with executeHere, whose loop runs MergePlainMergeTreeTask::finish -> write_part_log before OPTIMIZE returns, so the MergeParts rows are always queued by the time the test flushes. A single SYSTEM FLUSH LOGS part_log is sufficient. The MutatePart section keeps its own retry loop, which guards a genuinely asynchronous path.

Validation (25-30 runs per arm, debug build): the pre-change count() > 2 assertion fails 12/12 once background merges are suppressed; the new count() == 2 assertion fails 30/30 if the pin is removed, so both edits are load-bearing; the committed form passes 30/30. Reference output unchanged.

Failure sample (Stateless tests (amd_tsan, parallel), PR #108572): https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=108572&sha=6838c33f876538a34fdd6beb9de4320905ec3236&name_0=PR&name_1=Stateless%20tests%20%28amd_tsan%2C%20parallel%29

Version info

  • Merged into: 26.8.1.602 (included in 26.8 and later)

The MergeParts check runs a single SYSTEM FLUSH LOGS immediately after
OPTIMIZE TABLE ... FINAL, but a merge's part_log entry can be written
asynchronously after OPTIMIZE returns, so count() occasionally reads 2
instead of the expected 3 (Error: 2). Apply the same retry-flush loop
the MutatePart section already uses.

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? No — ~1% timing race (CI diagnosis: "not reproducible, likely transient"); 0/60 locally even under ThreadFuzzer. Root cause identified from the failure shape + code path instead.
b Root cause explained? Yes. OPTIMIZE TABLE ... FINAL returns before the merge's part_log entry is written (async, in MergePlainMergeTreeTask::finish). The single SYSTEM FLUSH LOGS part_log then sees 2 of 3 MergeParts rows → count() > 2 fails as Error: 2.
c Fix matches root cause? Yes. Retries SYSTEM FLUSH LOGS part_log until >2 MergeParts entries are visible, closing the async-flush window.
d Test intent preserved? Yes. count() > 2 assertion and .reference unchanged; only an async-flush retry added, mirroring the existing MutatePart guard in the same test.
e Demonstrated in both directions? Partial — the fix passes 20/20 full-test runs and 60/60 MergeParts-section runs; the pre-fix failure is ~1% and not locally reproducible, but the fix directly removes the flush-timing window.
f Fix general, not a narrow patch? Yes. The MutatePart section already had this guard; this brings the MergeParts section to parity — the last section that lacked it.
g Generalizes across inputs? N/A (flaky test flush-timing fix, not a code bug).
h Backward compatible? N/A (test-only change).
i Invariants preserved? N/A (test-only change).

Session id: cron:clickhouse-worker-slot-4:20260716-061500

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @CurtizJ — could you review this? Test-only fix: the MergeParts section asserted count() > 2 right after a single SYSTEM FLUSH LOGS following OPTIMIZE FINAL, but the merge's part_log entry is written asynchronously, so it occasionally saw only 2 rows (Error: 2). Applies the same retry-flush loop the MutatePart section in the same test already uses; assertion and reference unchanged.

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

clickhouse-gh Bot commented Jul 16, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [cdede53]

Summary: ✅


AI Review

Summary

This PR makes 02378_part_log_profile_events deterministic by disabling incidental background merges with max_bytes_to_merge_at_max_space_in_pool = 1 and then asserting the exact MergeParts footprint of OPTIMIZE TABLE ... FINAL. The previous inline issue about count() > 2 depending on unrelated background merges is fixed in the current code, and I did not find any new actionable review findings in the diff.

Final Verdict

✅ No new findings.

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

Copy link
Copy Markdown
Collaborator Author

CI finish ledger — c8e05d5

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_tsan, s3 storage, parallel, 2/3) / 01730_distributed_group_by_no_merge_order_by_long flaky MEMORY_LIMIT (112 unrelated PRs, 14 master / 30d) #109847 (external @ azat, open)
Stateless tests (amd_tsan, s3 storage, parallel, 2/3) / Scraping system tables CI-infra minio_audit_logs dump timeout (135 unrelated PRs, 15 master / 30d) #110099 (ours, open)

Not PR-caused: this PR only touches the 02378_part_log_profile_events test (MergeParts retry-flush loop); neither failure is in that file or explained by the change.

Session id: cron:our-pr-ci-monitor:20260716-110000

groeneai added 6 commits July 29, 2026 14:31
…inistic

The MergeParts section waited for and asserted `count() > 2`, but that is not a
property of the OPTIMIZE the section tests. The table has two partitions
(PARTITION BY key >= 128) and `StorageMergeTree::optimize` with FINAL and no
partition runs one foreground merge per partition
(`selectAllPartsToMergeWithinPartition` returns a single choice covering all
parts of the partition), so a fully logged OPTIMIZE FINAL produces exactly two
MergeParts entries. The third entry the assertion required could only come from
an incidental background merge, so the test passed only when extra merges
happened to show up and reported `Error: 2` when they did not.

Disable background merges for the table with
max_bytes_to_merge_at_max_space_in_pool = 1 (merges started by OPTIMIZE FINAL
ignore this setting) and assert the resulting deterministic values: exactly two
MergeParts entries and exactly 512 merged rows.

Measured on a debug build: with the setting pinned the previous `> 2` form fails
8/8 with `Error: 2`, the reported CI signature; without the pin the new `== 2`
form fails because incidental background merges add entries; the committed
combination passes 50/50.
Both merges that OPTIMIZE TABLE ... FINAL starts run synchronously inside the
query: StorageMergeTree::merge builds a MergePlainMergeTreeTask and runs it via
executeHere, which loops executeStep to completion, and
MergePlainMergeTreeTask::finish calls write_part_log before returning. The
part_log entries for both partitions are therefore already queued when OPTIMIZE
returns, so the retry loop could never observe a late entry.

Verified on a debug build: 50/50 passes with the loop removed.
…l string

The previous commit wrapped literal names in backticks in every new comment, but
the comment above CREATE TABLE is inside a double-quoted shell string, so the
backticks were command substitutions: the test failed with
"max_bytes_to_merge_at_max_space_in_pool: command not found" and every query
after it ran against a broken statement.

Keep the backticks only in the shell # comment, where they are inert, and note
why the SQL comment cannot have them.
The comment explaining the new `max_bytes_to_merge_at_max_space_in_pool`
setting sat inside the double-quoted shell string holding the SQL, where a
backtick starts a command substitution, so the literal names could not be
wrapped in inline code blocks as the repository instructions require. Move it
above the `${CLICKHOUSE_CLIENT}` invocation, where backticks are inert, and
drop the parenthetical that explained the shell mechanics.

CI provenance of the flaky failure this pull request fixes:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=108572&sha=6838c33f876538a34fdd6beb9de4320905ec3236&name_0=PR&name_1=Stateless%20tests%20%28amd_tsan%2C%20parallel%29
@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - cdede53

Every failure below has an owner: a fixing PR (mine 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/174 check-runs completed, 0 failed -

All 174 check-runs on this head are completed with no failure, timed_out or
action_required conclusion, the praktika Finish Workflow aggregator is green, and CIDB
records zero FAIL or ERROR rows for this commit.

This supersedes the earlier ledger keyed to c8e05d5a65, which is four commits behind this
head.

Session id: cron:our-pr-ci-monitor:20260730-180000

@alexey-milovidov alexey-milovidov self-assigned this Aug 1, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Aug 1, 2026
Merged via the queue into ClickHouse:master with commit 352479b Aug 1, 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 Aug 1, 2026
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