Skip to content

Do not leak the TTL merge slot of a merge that never runs - #121236

Merged
alexey-milovidov merged 8 commits into
masterfrom
fix-ttl-merge-slot-leak
Oct 3, 2026
Merged

alexey-milovidov merged 8 commits into
masterfrom
fix-ttl-merge-slot-leak

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Sep 20, 2026 •

Copy link
Copy Markdown
Member

A merge with TTL takes one of the max_number_of_merges_with_ttl_in_pool slots the moment it is selected, because system.merges — which the limit is counted from — only gets an entry once the merge task starts running. The slot used to be given back by that entry's destructor, so a merge that was selected and then dropped before it ever ran never gave its slot back. The slots are server-wide and there are two of them by default, so two such merges are enough to stop every table on the server from running another merge with TTL until it is restarted, and expired rows are then never deleted.

Dropping a selected merge is not exotic: the background pool discards a task that is still queued when its table goes away, which is what a DROP TABLE shortly after an insert of expired rows does.

That is how 04790_ttl_drop_merge_text_index and 05106_ttl_only_drop_parts_column_ttl failed together on master. At 05:19:42 two TTLDrop merges were selected on the inner table of a TimeSeries table and its database was dropped 274 ms later; from that point on not one of the ~4000 merges the job still ran was a merge with TTL, and the two tests timed out 9 and 21 minutes later waiting for a background TTL merge the server could no longer assign. Both tests are correct — the server was wedged.

The slot is now held by MergeList::TTLMergeSlot, an RAII token that lives in the selected merge itself (MergeMutateSelectedEntry for MergeTree, the task for ReplicatedMergeTree), so it is given back however the merge ends: it ran, it was cancelled, it threw, or it was dropped before it started. bookMergeWithTTL, cancelMergeWithTTL and the MergeList::onEntryDestroy override are gone, so there is no longer a way to take a slot without owning it.

The new test drives the exact path with a one-shot fail point that drops a selected merge with TTL, then checks that a table which insists on every slot being free can still run its own.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=master&sha=5e3546b1e1b3be5c651cd5deb0039361bbc57671&name_0=MasterCI&name_1=Stateless%20tests%20%28amd_debug%2C%20distributed%20plan%2C%20s3%20storage%2C%20parallel%29

Related: #119717
(the same "selection is not execution" gap, but on the per-partition merge_with_ttl_timeout timer; this pull request does not address it)

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fix background merges with TTL silently stopping for the whole server. A merge with TTL that was selected and then dropped before it ran — for example because its table was dropped right after the expired rows were inserted — leaked one of the max_number_of_merges_with_ttl_in_pool slots, which are shared by all tables. After two such merges no table would delete expired rows any more until the server was restarted.

🤖 Generated with Claude Code


Workflow [PR]
Sync PR [sync-upstream/pr/121236]

Version info

  • Merged into: 26.10.1.1435-master (included in 26.10 and later)

A merge with TTL takes one of the `max_number_of_merges_with_ttl_in_pool`
slots the moment it is selected, because `system.merges` - which the limit is
counted from - only gets an entry once the merge task starts running. The slot
was given back by that entry's destructor, so a merge that was selected and
then dropped before it ever ran never gave its slot back. The slots are
server-wide and there are two of them by default, so two such merges stop every
table on the server from running another merge with TTL until it is restarted,
and expired rows are never deleted.

Dropping a selected merge is not exotic: the background pool discards a task
that is still queued when its table goes away, which is what a `DROP TABLE`
shortly after an insert of expired rows does.

That is how `04790_ttl_drop_merge_text_index` and
`05106_ttl_only_drop_parts_column_ttl` failed together on master: at 05:19:42
two `TTLDrop` merges were selected on the inner table of a `TimeSeries` table
and its database was dropped 274 ms later; from there on not one of the ~4000
merges the job still ran was a merge with TTL, and the two tests timed out 9 and
21 minutes later waiting for a background TTL merge the server could no longer
assign. Both tests are correct.

The slot is now held by `MergeList::TTLMergeSlot`, an RAII token that lives in
the selected merge itself - `MergeMutateSelectedEntry` for `MergeTree`, the task
for `ReplicatedMergeTree` - so it is given back however the merge ends: it ran,
it was cancelled, it threw, or it was dropped before it started.

https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=master&sha=5e3546b1e1b3be5c651cd5deb0039361bbc57671&name_0=MasterCI&name_1=Stateless%20tests%20%28amd_debug%2C%20distributed%20plan%2C%20s3%20storage%2C%20parallel%29

Co-Authored-By: Claude Opus 5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Sep 20, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [c90939c]

Summary: ✅


AI Review

Summary

This PR moves TTL-merge slot ownership from system.merges entries to explicit RAII tokens attached to the selected merge object (MergeMutateSelectedEntry for plain MergeTree, MergeFromLogEntryTask for replicated merges), so a slot is released even when a selected TTL merge is dropped before execution. The current diff preserves the intended slot accounting and the updated failpoint test now exercises destruction after MergePlainMergeTreeTask takes ownership, which addresses the earlier lifetime gap. I do not see any remaining correctness or coverage issues in the current code.

Final Verdict

Status: ✅ Approve

LLVM Coverage Report

Measured on commit c90939c.

Metric Baseline Current Δ
Lines 88.10% 88.10% +0.00%
Functions 91.70% 91.70% +0.00%
Branches 79.50% 79.50% +0.00%

Changed lines: Changed C/C++ lines covered: 53/55 (96.36%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added pr-bugfix Pull request with bugfix, not backported by default comp-ttl TTL rules and TTL merges (moving/recompressing/rolling-up/deleting parts). labels Sep 20, 2026
@tiandiwonder tiandiwonder self-assigned this Sep 21, 2026
Comment thread src/Storages/MergeTree/MergeList.h Resolved
@alexey-milovidov alexey-milovidov mentioned this pull request Sep 21, 2026
1 task done
alexey-milovidov and others added 2 commits September 25, 2026 22:04
…ot_not_leaked_when_merge_dropped`

`mt_drop_selected_ttl_merge_once` is server-wide, so an `EXIT` trap now disables it
and drops the test tables however the script ends.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Comment thread src/Storages/StorageMergeTree.cpp Resolved
@clickhouse-gh

clickhouse-gh Bot commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing c90939cc7 with master 9672750a8 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

245 translation units recompiled, 2591 s compile time in total, 245 of them have a recent master baseline.

Job report

@clickhouse-gh

clickhouse-gh Bot commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 32 queries analysed

This PR changes only how the server counts slots for TTL merges. The count is now held by a token that gives the slot back on its own, plus a new test hook and a stateless test. None of this runs during benchmark SELECT queries. The only flagged query is clickbench Q28 at -6.6% (1106.5 ms to 1034.0 ms). The two tests disagree on it and the measurements were noisy, so it stays not_sure and does not need action. No performance effect is expected from this change, and none was measured.

clickbench

⚠️ 1 inconclusive

Flagged queries (1 of 43)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
⚠️ 28 not_sure 1106 1034 -6.6% <0.0001 This PR only changes how TTL merge slots are counted for background merges, which a read-only ClickBench SELECT never touches. The -6.6% shift is borderline and the measurements were noisy, so it should stay not_sure.

Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on.

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: 075f248a-cd75-4b6f-ae41-72d0b9b29305
  • MIRAI run: bde75474-4fb1-42eb-9667-5180fefe22f5
  • PR check IDs:
    • clickbench_3004211_1790999380
    • clickbench_3004224_1790999380
    • clickbench_3004235_1790999380
    • tpch_adapted_1_official_3004242_1790999380
    • tpch_adapted_1_official_3004267_1790999380
    • tpch_adapted_1_official_3004281_1790999380

groeneai added a commit to groeneai/ClickHouse that referenced this pull request Sep 30, 2026
The amd_tsan flaky check failed the last scenario 8 times in 238 runs: the
TTL drop never ran. `max_number_of_merges_with_ttl_in_pool` is compared with
the number of TTL merges of the whole server. The job log shows two TTLDrop
merges selected on `t_drop_column_zero` while `SYSTEM STOP MERGES` was in
effect and then dropped without running. The slots they booked were never
given back (the leak ClickHouse#121236 fixes), so from then on no table on that
server got a TTL merge.

The four scenarios that wait for a background TTL merge now set the limit
to 100, as 02293 and 04321 do. The two that must prepare the part first keep
it at 0 until then and raise it with `MODIFY SETTING` instead of
`STOP/START MERGES`; the other two need neither. So the test no longer
selects a TTL merge that is then dropped.

With two TTL merges of another database paused on the server
(`merge_task_projection_stage_pause`), the previous version fails scenarios
11-14 as in CI and this one passes (1/1, and 20/20 randomized). 100/100
randomized at --jobs 32. The master build still fails 11 and 13, and the
mutants M9, M10, M12 still fail 12, 13 and 14.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@alexey-milovidov alexey-milovidov added the blocker This issue / pr blocks a new release label Oct 2, 2026
…leak

# Conflicts:
#	src/Common/FailPoint.cpp
#	src/Storages/StorageMergeTree.cpp

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks reasonable.

alexey-milovidov and others added 4 commits October 3, 2026 00:20
# Conflicts:
#	src/Common/FailPoint.cpp
#	src/Storages/StorageMergeTree.cpp
…_merge_once`

The fail point used to fire before `MergePlainMergeTreeTask` was constructed, so the only owner
of the TTL merge slot it dropped was the local `merge_entry` in `scheduleDataProcessingJob`. The
leak it guards against happens later: the task has been queued and is discarded when its table
goes away, and then the slot's owner is the task's `merge_mutate_entry`. Fire the fail point
after the task is constructed and release the local reference first, so the task is the last
owner of the selected entry when it dies, as for a queued task.

Addresses review: #121236

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…x-ttl-merge-slot-leak

# Conflicts:
#	src/Storages/StorageMergeTree.cpp
…ttest`

The style check `failpoint_tests_are_isolated` requires a test that enables a fail point to be
kept out of the fast test, because a fail point is server-global state and such a test must run
alone.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=121236&sha=ba55e87113b679c53c142df83bcc64c97111d5b7&name_0=PR&name_1=Style%20check
PR: #121236

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 3, 2026
Merged via the queue into master with commit f5c60f8 Oct 3, 2026
149 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-ttl-merge-slot-leak branch October 3, 2026 19:53
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 3, 2026
valerypetrov added a commit to valerypetrov/ClickHouse that referenced this pull request Oct 4, 2026
Master's ClickHouse#121236 removed the try block around the merge entry construction
in StorageMergeTree, which conflicted with the branch's patch-aware
estimateNeededDiskSpace call. The call is kept at the new indentation.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
leshikus pushed a commit to leshikus/ClickHouse that referenced this pull request Oct 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

blocker This issue / pr blocks a new release comp-ttl TTL rules and TTL merges (moving/recompressing/rolling-up/deleting parts). pr-bugfix Pull request with bugfix, not backported by default 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