Skip to content

Reserve disk space for the mutation of a part whose TTL has expired - #122481

Open
groeneai wants to merge 1 commit into
ClickHouse:masterfrom
groeneai:mutation-of-expired-part-reserves-space
Open

groeneai wants to merge 1 commit into
ClickHouse:masterfrom
groeneai:mutation-of-expired-part-reserves-space

Conversation

@groeneai

@groeneai groeneai commented Sep 26, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #122391
Related: #122280

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

Fixed a mutation (for example ALTER TABLE ... UPDATE) of a part whose TTL had expired for every row reserving only 1 MiB of disk space. On a storage policy with several disks, such a mutation wrote into the part's disk even when it was full, failed with No space left on device on every retry, and could make inserts into other tables on that disk fail. It now reserves the size of the part, like the mutation of any other part, and waits for free space.

Description

On 26.9.1.1629 and 26.8.10.6, an ALTER TABLE ... UPDATE of a part whose DELETE TTL has expired for every row, on a storage policy with several disks, reserves 1 MiB, writes into the part's full disk and fails with No space left on device on every retry. Inserts into other tables on that disk meanwhile fail with NOT_ENOUGH_SPACE (6 of 439 over 4 runs, 0 of 440 with this PR). Such a part survives, for example, while TTL merges are stopped.

Both mutation paths reserve through CompactionStatistics::estimateNeededDiskSpace, which counts a fully expired part as zero bytes, a skip meant for TTL drop merges (#43222). A mutation rewrites the part: ALTER UPDATE does not execute TTL, and even the ttl_only_drop_parts shortcut writes the whole result before swapping in an empty part. The size gate counts the part, but against the roomiest disk of the policy, while the mutation writes on the part's own disk.

Both paths (MergeTree and ReplicatedMergeTree) now reserve the part's size through a new estimateNeededDiskSpaceForMutation. Parts that have not expired reserve as before; merges and the gates are untouched (the merge side is #122391, whose AI review raised this). Trade-off: a mutation that writes little, such as a TTL-executing MATERIALIZE TTL, now waits for space on a full disk of such a policy, as it already does on a single disk.

The new integration test (plain, ttl_only_drop_parts and replicated cases) checks that the mutation does not start while the part's disk is full and reserves at least the part's size once space is freed. It fails on master, 26.9 and 26.8 and passes 13/13 runs with the fix; reverting either call site fails exactly its own cases.

🤖 Generated with Claude Code


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

…xpired

The disk space estimate that both mutation paths reserve skipped a part whose
TTL had expired for every row, so such a mutation reserved only the 1 MiB
minimum. The skip was added for TTL drop merges, which write nothing for such
a part. A mutation does write it: `ALTER UPDATE` and `MATERIALIZE COLUMN` do
not execute TTL, and even with `ttl_only_drop_parts` the whole mutation runs
before its result is replaced with an empty part.

On a single disk the mutation size gate hides this, because it counts expired
parts. On a volume with several disks the gate uses the free space of the
roomiest disk, so the mutation starts, writes into the part's own full disk and
fails with "No space left on device" on every retry. Inserts into other tables
on that disk then fail with NOT_ENOUGH_SPACE.

Mutations now reserve the size of the source part whether or not its TTL has
expired, through their own estimate. The merge estimate and the selection gates
are unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-review-bot PR origin: review-bot finding on a groeneai PR labels Sep 26, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 1 finding) and my own cold review. All findings are nits; none needed a code change.

# Sev Finding Verdict Evidence / action
1 💡 estimateNeededDiskSpace is still documented as "for merge or mutation" (CompactionStatistics.h:12), but no mutation calls it now AGREE, not changed #122391 and #115723 each insert a line right below that line, so editing it here conflicts with both. I can change it to "merge" if preferred
2 💡 The description said "a volume with several disks" AGREE, fixed in the description the mutation size gate takes the maximum over all disks of the policy (StoragePolicy::getMaxUnreservedFreeSpace), so any policy with several disks is affected
3 💡 On plain MergeTree a NOT_ENOUGH_SPACE refusal ends the selection round, so the table's later parts wait too AGREE, no change an expired part on a full disk now takes the path a part that has not expired already takes. Once TTL merges run, the TTL merge still removes such a part's rows with a 1 MiB reservation, which ends the wait
4 💡 MutateFromLogEntryTask also feeds this estimate to prefer_fetch_merged_part_size_threshold and the zero-copy lock sleep AGREE, intended an expired part now counts there like any other part

Severity: ❌ blocker / ⚠️ major / 💡 nit.

Session id: cron:clickhouse-review-slot-11:20260926-221503

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes: the new integration test fails 3/3 cases on master in every run, also on 26.9.1.1629 and 26.8.10.6.
b Root cause explained? Both mutation reservations use the merge estimate, which counts a fully expired part as 0 bytes, so a mutation reserves 1 MiB. With several disks the policy-wide gate passes on another disk and the write hits ENOSPC on the part's own disk.
c Fix matches root cause? Yes: mutations reserve the part's size, so they refuse cleanly before writing, as for a non-expired part. The gate and the merge estimate are unchanged.
d Test intent preserved / new tests added? New test_mutation_of_expired_part_reserves_space (plain DELETE + RECOMPRESS, ttl_only_drop_parts, ReplicatedMergeTree). The existing TTL and mutation tests pass.
e Both directions demonstrated? Master fails 3/3 and the fix passes 3/3 (13 fresh runs plus 2 --count 2). Reverting the plain call fails the 2 plain cases, the replicated call the replicated case, both calls all 3.
f Fix is general across code paths? One new function used by both mutation reservation sites (one per engine). Merge, dry run, fetch and move reservations are not mutations and keep their own estimates.
g Fix generalizes across inputs (params/datatypes/wrappers)? Both engines, two TTL shapes, ALTER UPDATE and MATERIALIZE TTL (Compact and Wide), JBOD and single disk. No type axis: the estimate depends only on part bytes.
h Backward compatible? (maintainer-approved exception only) No setting or format change. A cheap mutation of an expired part now waits for space on a full disk of a policy with several disks, as it already does on a single-disk policy.
i Invariants and contracts preserved? A mutation's reservation covers a rewrite of its part. Non-expired parts reserve byte-identically to master. Refusals take the existing error paths.

Session id: cron:clickhouse-impl-slot-4:20260926-205300

@clickhouse-gh

clickhouse-gh Bot commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [fff83e4]

Summary: ✅


AI Review

Summary

This PR splits mutation disk-space estimation from merge estimation so a fully expired part now reserves space like any other mutation, and it adds integration coverage for plain, ttl_only_drop_parts, and replicated cases. I did not find a correctness, regression, or coverage issue in the changed mutation reservation paths.

Final Verdict

✅ No blocking issues found.

LLVM Coverage Report

Measured on commit fff83e4.

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

Changed lines: Changed C/C++ lines covered: 10/10 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Sep 26, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-mutations ALTER UPDATE/DELETE and mutation execution over parts (including lightweight updates/deletes). label Sep 26, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 27, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing fff83e433 with master beb4d3dad (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of e7aebccc7; 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

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

Job report

This branch has not been deployed

No deployments
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 comp-mutations ALTER UPDATE/DELETE and mutation execution over parts (including lightweight updates/deletes). groeneai-origin-review-bot PR origin: review-bot finding on a groeneai PR pr-bugfix Pull request with bugfix, not backported by default

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant