Skip to content

Cherry pick #111992 to 26.6: Fix a mutated part losing files to the temporary directory cleanup - #120554

Merged
PedroTadim merged 16 commits into
backport/26.6/111992from
cherrypick/26.6/111992
Sep 17, 2026
Merged

PedroTadim merged 16 commits into
backport/26.6/111992from
cherrypick/26.6/111992

Conversation

@robot-ch-test-poll3

Copy link
Copy Markdown
Contributor

Original pull-request #111992

Do not merge this PR manually

This pull-request is a first step of an automated backporting.
It contains changes similar to calling git cherry-pick locally.
If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.

The check results does not matter at this step - you can safely ignore them.

Before you resolve anything

Conflicts are often caused by a prerequisite change that has not been backported yet, rather than by a real divergence. The bot re-tries this cherry-pick against the release branch on every run, so if that is the case here it will merge itself as soon as the prerequisite lands, and you will see a comment saying so. Manual resolution is only needed while the conflict persists.

Troubleshooting

If the conflicts were resolved in a wrong way

If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

robot-clickhouse and others added 15 commits July 26, 2026 23:30
`MutateFromLogEntryTask::finalize` released `mutate_task` - and with it the RAII
guard that registers `tmp_mut_<part>` in `MergeTreeData::TemporaryParts` - before
calling `MergeTreeData::Transaction::renameParts`, which is what actually renames
the directory on disk. Between those two points the temporary directory of a part
that is already precommitted was not protected from `clearOldTemporaryDirectories`.

When the cleanup thread hit that window it started removing the directory while
`renameParts` was moving it to the persistent name, so the part became active with
part of its files already deleted while `checksums.txt` still listed all of them:

    19:34:46.048315  Renaming temporary part tmp_mut_97_0_0_0_7 to 97_0_0_0_7
    19:34:46.049102  Removing temporary directory .../tmp_mut_97_0_0_0_7/
    19:34:46.049168  Renaming part to 97_0_0_0_7

Reads of such a part fail with

    Code: 1001. DB::Exception: std::filesystem::filesystem_error: filesystem error:
    in file_size: No such file or directory [".../97_0_0_0_7/foo2.cmrk2"]

because `MergeTreeMarksLoader` derives the marks path from the part's checksums,
and every following mutation of the part fails permanently with

    Code: 424. DB::ErrnoException: Cannot link .../97_0_0_0_7/foo2.bin to
    .../tmp_mut_97_0_0_0_8/num2.bin ... (CANNOT_LINK)

Rename the part while `mutate_task` is still alive, the way
`MergeTreeDataMergerMutator::renameMergedTemporaryPart` already does for merges
("Explicitly rename part while still holding the lock for tmp folder to avoid
cleanup"). The reset of `mutate_task` stays where it was relative to
`checkPartChecksumsAndCommit`, so the fallback fetch on a checksum mismatch still
runs without the guards being held.

The window was introduced by #96376, which moved the reset in front of
`renameParts`. Every other caller of `renameTempPartAndReplace` with
`rename_in_transaction=true` renames immediately afterwards.

The new stateless test pauses a mutation right before the rename with a failpoint
and checks that the cleanup thread reports the directory as in use instead of
removing it.

https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=423ec1ba3ac4288ce5b934235be0c6700e0f7c14&name_0=MasterCI&name_1=Integration%20tests%20%28amd_msan%2C%201%2F8%29

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…ir-cleanup-race

# Conflicts:
#	src/Common/FailPoint.cpp
#	src/Storages/MergeTree/MutateFromLogEntryTask.cpp
…ir-cleanup-race

# Conflicts:
#	src/Storages/MergeTree/MutateFromLogEntryTask.cpp
The AI review pointed out that the test only observed the temporary
directory while the mutation task, and with it the `TemporaryParts`
guard, was still alive: the `rmt_mutate_task_pause_before_rename_part`
failpoint fires before both `transaction_ptr->renameParts()` and
`mutate_task.reset()`, so the assertion held for the old ordering as
well and the test could not detect a reintroduced race.

Add a second pauseable failpoint,
`rmt_mutate_task_pause_after_temporary_part_released`, right after
`mutate_task.reset()` — exactly the window the fix is about. With the
fix, the temporary directory has already been renamed under the guard
and there is nothing left to remove; with the old ordering it is still
on disk and no longer registered in `TemporaryParts`, so the cleanup
thread deletes it.

The test now synchronizes on the failpoints with
`SYSTEM WAIT FAILPOINT ... PAUSE` instead of polling for the first
cleanup log line, and waits for a counted number of finished cleanup
iterations (`Scheduling next cleanup` in `system.text_log`) before
sampling the decision, so both phases observe at least one full pass of
`clearOldTemporaryDirectories`.

Verified locally on a `ReplicatedMergeTree` server: the test passes
three times in a row with the fix, and fails deterministically
(`removed_after_release` becomes `1`) when `renameParts` is moved back
below `mutate_task.reset()`.
Both `rmt_mutate_task_pause_before_rename_part` and
`rmt_mutate_task_pause_after_temporary_part_released` are server-global and pause
every `ReplicatedMergeTree` mutation. Under `set -e` a timed-out
`SYSTEM WAIT FAILPOINT ... PAUSE` or a failing `wait_for_cleanup_iterations` exited the
script before the explicit `SYSTEM DISABLE FAILPOINT` calls, leaving the failpoints armed
for the rest of the stateless run. Disable them unconditionally from an `EXIT` trap;
disabling a pauseable failpoint also resumes the threads paused at it.
Capture a `start_time` before creating the table and filter both the
cleanup-row and cleanup-iteration queries over `system.text_log` by
`event_time_microseconds >= toDateTime64('$start_time', 6)`, so stale
rows from a previous run (e.g. under a fixed --database, where the
logger names are the same) cannot satisfy or break the assertions.

Co-Authored-By: Claude Fable 5 <[email protected]>
…nup-race

Fix a mutated part losing files to the temporary directory cleanup
@robot-ch-test-poll3 robot-ch-test-poll3 added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default labels Sep 17, 2026
Every conflict is master-only context the cherry-pick drags along, not a
divergence in the change itself. Kept only what it contributes and dropped:

- `new_part->getDataPartStorage().commitTransaction();` after
  `renameTempPartAndReplace`. No release branch has it; they only
  `precommitTransaction` above.
- The `rmt_mutate_task_pause_after_zero_copy_lock` and
  `merge_tree_background_task_marked_for_deletion` failpoints, and the
  matching `extern`. Neither is registered on the older branches, and nothing
  in this file uses them, so taking the `extern` would not link. Note that a
  blank line among the `PAUSEABLE(...) \` entries would silently end the macro
  list, so the two new entries are appended without one.
- The `MutateFromLogEntryTask::~MutateFromLogEntryTask` destructor and
  `Stopwatch commit_watch;`, both master-only and unused here.

What lands is the fix itself: the two new failpoints, and
`transaction_ptr->renameParts()` moved out of the `try` block to before
`mutate_task.reset()`, so the rename happens while the `TemporaryParts` guard
still keeps `clearOldTemporaryDirectories` off the `tmp_mut_<part>` directory.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants