Skip to content

Fix flaky test 03001_consider_lwd_when_merge - #107812

Merged
alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-flaky-03001-consider-lwd-when-merge
Jun 18, 2026
Merged

alexey-milovidov merged 1 commit into
ClickHouse:masterfrom
groeneai:fix-flaky-03001-consider-lwd-when-merge

Conversation

@groeneai

@groeneai groeneai commented Jun 18, 2026 •

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

Fixes flakiness of 03001_consider_lwd_when_merge under randomized MergeTree settings. Requested by @ alexey-milovidov in #107457 (comment).

The test calibrates the 80000-byte max_bytes_to_merge_at_{min,max}_space_in_pool thresholds against the on-disk size of a 10000-row UInt64 CODEC(NONE) part (10000 * 8 = 80000 bytes of column data). It relies on exclude_deleted_rows_for_part_size_in_merge scaling that size down by existing_rows / total_rows so the final OPTIMIZE can merge two otherwise too-large parts into one.

The CI test runner randomizes MergeTree settings, including index_granularity over [1, 65536]. A small granularity multiplies the number of granules, so the per-granule overhead (marks files + primary.idx) inflates bytes_on_disk far beyond the 80000-byte data size: at index_granularity = 1 a 10000-row part is ~690 KB on disk instead of ~85 KB. The deletion-scaled size then stays above the threshold, and the final OPTIMIZE (which must succeed) fails with CANNOT_ASSIGN_OPTIMIZE.

Fix: pin index_granularity = 8192 (the default) in the CREATE TABLE SETTINGS so the part size matches what the byte thresholds were calibrated for. Explicit CREATE-level settings override the runner's randomized values, so the rest of the randomization is preserved.

CI failure: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=107457&sha=77b6e9e71f92036d5855dedcf83435d1507431eb&name_0=PR&name_1=Stateless%20tests%20%28amd_tsan%2C%20s3%20storage%2C%20parallel%2C%202%2F2%29 (Stateless tests (amd_tsan, s3 storage, parallel, 2/2), culprit setting --index_granularity 1). Reproduced locally; with the pin the test passes 50/50 over randomized index_granularity drawn from [1, 65536].

Version info

  • Merged into: 26.6.1.992

The test calibrates the 80000-byte max_bytes_to_merge_at_*_space_in_pool
thresholds against the on-disk size of a 10000-row UInt64 CODEC(NONE) part
(10000 * 8 = 80000 bytes of column data). It relies on
exclude_deleted_rows_for_part_size_in_merge scaling that size down by
existing_rows/total_rows so the final OPTIMIZE can merge two otherwise
too-large parts.

The CI test runner randomizes MergeTree settings, including index_granularity
over [1, 65536]. A small granularity multiplies the number of granules, so the
per-granule overhead (marks files + primary.idx) inflates bytes_on_disk far
beyond the 80000-byte data size: at index_granularity = 1 a 10000-row part is
~690 KB on disk instead of ~85 KB. The deletion-scaled size then stays above
the threshold and the final OPTIMIZE fails with CANNOT_ASSIGN_OPTIMIZE.

Pin index_granularity = 8192 (the default) in the CREATE TABLE SETTINGS so the
part size matches what the byte thresholds were calibrated for. Explicit
CREATE-level settings override the runner's randomized values, so the rest of
the randomization is preserved. Reproduced on this PR's CI run via
--index_granularity 1 (CANNOT_ASSIGN_OPTIMIZE); with the pin the test passes
50/50 over randomized index_granularity drawn from [1, 65536].

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author

Pre-PR validation gate

# Question Answer
a Deterministic repro? Yes. clickhouse-client --allow_merge_tree_settings --index_granularity=1 running the test SQL fails 100% with Code: 388 CANNOT_ASSIGN_OPTIMIZE on the final OPTIMIZE (output 2\n2+error), byte-identical to the CI failure. CIDB's own minimizer on this PR's run isolated the culprit to --index_granularity 1.
b Root cause explained? The 80000-byte merge thresholds are calibrated against a 10000-row UInt64 CODEC(NONE) part = exactly 80000 bytes of column data. The runner randomizes index_granularity over [1, 65536]; a small value multiplies granules, so marks + primary.idx overhead inflates bytes_on_disk (~690 KB at granularity 1 vs ~85 KB at 8192). Even after exclude_deleted_rows_for_part_size_in_merge scales by existing_rows/total_rows, the size stays above 80000, so the final OPTIMIZE (which must succeed) is rejected.
c Fix matches root cause? Yes. Pinning index_granularity = 8192 (the default) in CREATE TABLE SETTINGS restores the part size the byte thresholds were calibrated for. It targets exactly the one randomized setting that breaks the calibration, not a blanket no-random-merge-tree-settings tag.
d Test intent preserved / new tests added? Yes. The test still verifies that exclude_deleted_rows_for_part_size_in_merge makes lightweight-deleted parts mergeable: the first two OPTIMIZEs still must fail (parts too big), and the third still must succeed (deletion-scaled size under threshold). Only the part's physical layout is fixed; all other settings stay randomized.
e Both directions demonstrated? Yes. With --index_granularity=1: original test FAILS (CANNOT_ASSIGN_OPTIMIZE), patched test PASSES (2\n2\n1, matches .reference). Patched test passes 50/50 over index_granularity drawn from [1, 65536], including the known-bad small values and the index_granularity=2 + auto_statistics_types combo seen on PR #103229.
f Fix is general, not a narrow patch? N/A (flaky test setting fix, not a code bug). The pin covers the whole fragile class: any small randomized index_granularity is overridden, so the failure cannot recur regardless of which low value the runner draws.

Session id: cron:clickhouse-worker-slot-2:20260618-001500

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @alexey-milovidov — could you review this? Per your request on #107457: pins index_granularity = 8192 in the test's CREATE TABLE so the 80000-byte merge thresholds stay calibrated against the part's bytes_on_disk, which a randomized small granularity inflates via per-granule marks/primary.idx overhead. Passes 50/50 over randomized granularity.

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

clickhouse-gh Bot commented Jun 18, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [6751d65]

Summary: ✅


AI Review

Summary

This PR fixes the randomized-settings flake in 03001_consider_lwd_when_merge by pinning index_granularity = 8192 for the MergeTree table while preserving the test's max_bytes_to_merge_at_*_space_in_pool and exclude_deleted_rows_for_part_size_in_merge checks. I found no correctness, coverage, or PR metadata issues that need inline comments.

Final Verdict

Status: ✅ Approve

@alexey-milovidov alexey-milovidov self-assigned this Jun 18, 2026
@clickhouse-gh clickhouse-gh Bot added the pr-ci label Jun 18, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.20% 85.20% +0.00%
Functions 92.30% 92.30% +0.00%
Branches 77.40% 77.40% +0.00%

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

Newly covered by added/modified tests: 406 line(s), 15 function(s) across 116 file(s) · Details

Top files
  • src/Storages/StorageReplicatedMergeTree.cpp: 65 line(s)
  • src/Storages/StorageFilesystem.cpp: 12 line(s)
  • src/AggregateFunctions/TimeSeries/AggregateFunctionTimeSeriesGroupArray.h: 10 line(s), 8 function(s)
  • src/Databases/DatabaseMetadataDiskSettings.cpp: 10 line(s)
  • src/IO/S3/copyS3File.cpp: 10 line(s)

Full report

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Jun 18, 2026
Merged via the queue into ClickHouse:master with commit 2fb6c49 Jun 18, 2026
165 checks passed
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jun 18, 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.

3 participants