Skip to content

Fix flaky 02676_optimize_old_parts_replicated by capping max_merge_selecting_sleep_ms - #108916

Closed
groeneai wants to merge 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/fix-02676-optimize-old-parts-merge-selecting-backoff
Closed

groeneai wants to merge 1 commit into
ClickHouse:masterfrom
groeneai:groeneai/fix-02676-optimize-old-parts-merge-selecting-backoff

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

...

Description

Closes: #107506

02676_optimize_old_parts_replicated is flaky: it intermittently prints 3/extra rows instead of 2 at the "With merge replicated partition only" sections, predominantly on Stateless tests (amd_tsan, s3 storage, parallel). Reruns always pass (timing flaky).

Root cause: the test pins merge_selecting_sleep_ms=1000 on its force-merge tables but leaves max_merge_selecting_sleep_ms at the default 60000. The merge-selecting task uses an adaptive backoff (StorageReplicatedMergeTree::mergeSelectingTask): every CannotSelect poll multiplies the sleep by merge_selecting_sleep_slowdown_factor (1.2) and clamps it up to max_merge_selecting_sleep_ms. While the freshly inserted partition is younger than min_age_to_force_merge_seconds, repeated CannotSelect polls inflate the interval; under TSan + S3 + parallel background-pool starvation it grows to tens of seconds (up to 60s), so once the part becomes eligible the next selection poll can be far beyond the test's 100s wait_for_number_of_parts budget and the force-merge has not happened yet.

Fix: cap max_merge_selecting_sleep_ms=2000 on the four force-merge tables (the same pattern already used by 02448_clone_replica_lost_part.sh and 02995_forget_partition.sh). This bounds the worst-case wait between merge-selection attempts to 2s while keeping the merge-selection logic exercised. The test_without_merge table is left untouched (it must NOT merge). Neither setting is part of CI settings randomization, so this is deterministic.

Validated locally (debug build, embedded Keeper): the merge-selecting interval inflates 1.3s -> 11s -> ... -> 60s without the cap, and is held at exactly 2s with it. All five sections reach their reference values within 1-3s with the fix.

…lecting_sleep_ms

The test pins merge_selecting_sleep_ms=1000 on its force-merge tables but
leaves max_merge_selecting_sleep_ms at the default 60000. The merge-selecting
task backs off geometrically on every CannotSelect poll and clamps up to
max_merge_selecting_sleep_ms, so under TSan+S3+parallel pool starvation the
poll interval grows to tens of seconds and the force-merge of an old partition
is selected after the test's 100s wait budget, yielding 3 parts instead of 2.

Cap max_merge_selecting_sleep_ms=2000 on the four force-merge tables (matching
02448_clone_replica_lost_part.sh and 02995_forget_partition.sh) so the worst
case wait between selection attempts is 2s. test_without_merge is untouched.

Closes: ClickHouse#107506

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. With the test's current settings (merge_selecting_sleep_ms=1000, max_merge_selecting_sleep_ms defaulting to 60000) the merge-selecting poll interval inflates on every CannotSelect: locally observed 1305 -> 1626 -> 2100 -> ... -> 10989ms after 40s idle, climbing toward 60s. The force-merge of an eligible old partition is then selected after the test's 100s wait budget -> 3 parts instead of 2.
b Root cause explained? StorageReplicatedMergeTree::mergeSelectingTask backs off geometrically: each CannotSelect does sleep *= merge_selecting_sleep_slowdown_factor (1.2) clamped UP to max_merge_selecting_sleep_ms. While inserted parts are younger than min_age_to_force_merge_seconds, repeated CannotSelect polls inflate the interval; under TSan+S3+parallel pool starvation it reaches tens of seconds, creating a dead window longer than the 100s wait.
c Fix matches root cause? Yes. Capping max_merge_selecting_sleep_ms=2000 bounds the worst-case wait between selection attempts to 2s, directly removing the dead window. Verified locally the interval is held at exactly 2000ms with the cap.
d Test intent preserved? Yes. The test still exercises old-parts force-merge-on-partition; assertions and reference unchanged. The cap only bounds the polling interval; the test_without_merge table (which must NOT merge) is untouched.
e Demonstrated both directions? Yes. Without the cap the interval inflates toward 60s (reproducing the late-merge window); with the cap it stays at 2s and all five sections reach their reference values within 1-3s on a local debug build with embedded Keeper.
f Fix general, not a narrow patch? N/A (flaky test setting fix, not a code bug). The cap is applied to all four force-merge tables that rely on the background merge completing within the wait.
g Generalizes across inputs? N/A (flaky test setting fix, not a code bug).
h Backward compatible? N/A (flaky test setting fix, not a code bug).
i Invariants/contracts preserved? N/A (flaky test setting fix, not a code bug).

Session id: cron:clickhouse-worker-slot-0:20260630-092600

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @alesapin — could you review this test-only flaky fix? 02676_optimize_old_parts_replicated intermittently fails on amd_tsan/s3 because it pins merge_selecting_sleep_ms=1000 but leaves max_merge_selecting_sleep_ms at the 60000 default, so the merge-selecting backoff inflates the poll interval to tens of seconds and the partition force-merge lands after the 100s wait. The fix caps max_merge_selecting_sleep_ms=2000 on the four force-merge tables (same pattern as 02448_clone_replica_lost_part and 02995_forget_partition).

@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Jun 30, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jun 30, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [df5e03e]

Summary: ❌

job_name test_name status info comment
Stress test (amd_tsan) FAIL
Hung check failed, possible deadlock found FAIL cidb, issue

AI Review

Summary

This PR only updates tests/queries/0_stateless/02676_optimize_old_parts_replicated.sh to set max_merge_selecting_sleep_ms=2000 on the four ReplicatedMergeTree force-merge cases. The cap matches StorageReplicatedMergeTree's adaptive merge-selection backoff behavior and preserves the expected reference output; I did not find a new issue requiring inline review comments.

Final Verdict

Status: ✅ Approve

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

clickhouse-gh Bot commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.70% 85.72% +0.01%
Functions 91.47% 91.47% +0.00%
Branches 78.27% 78.30% +0.04%

Changed lines: No C/C++ source files changed — skipping uncovered code analysis.

Newly covered by added/modified tests: 100 line(s), 3 function(s) across 41 file(s) · Details

Top files
  • src/Storages/StorageReplicatedMergeTree.cpp: 8 line(s)
  • src/Functions/FunctionVariantAdaptor.cpp: 6 line(s)
  • src/Storages/MutationCommands.cpp: 6 line(s)
  • src/AggregateFunctions/AggregateFunctionSparkbar.cpp: 5 line(s)
  • src/Functions/stringBytes.h: 5 line(s)

Full report

@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger — df5e03e

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
Stress test (amd_tsan) / Hung check failed, possible deadlock found deadlock (chronic hung-check family) #108212 (ours, merged) / #105905 (ours, open)
CH Inc sync — CH Inc sync (private, not actionable)

This PR (#108916) is itself the test-only fix for the flaky 02676_optimize_old_parts_replicated it owns (Closes #107506); the only red here is the unrelated chronic hung-check deadlock, already owned by the fixing PRs above. CI fully finished (Finish Workflow + Mergeable Check complete).

Session id: cron:our-pr-ci-monitor:20260630-183000

@groeneai

Copy link
Copy Markdown
Collaborator Author

Superseded: master commit e1d7ad5 ("Fix flaky test 02676_optimize_old_parts_replicated", merged 2026-07-12) already fixes this and closed #107506. That fix is a superset of this PR: besides capping max_merge_selecting_sleep_ms (1000 vs my 2000) it also sets number_of_free_entries_in_pool_to_execute_optimize_entire_partition=1 to defeat the pool-slots gate in getBestPartitionToOptimizeEntire. Closing this as redundant.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Flaky test: 02676_optimize_old_parts_replicated

2 participants