Skip to content

Cherry pick #111196 to 26.3: Fix data loss when replicated INSERT commit recovery is interrupted - #119609

Merged
robot-clickhouse-ci-2 merged 11 commits into
backport/26.3/111196from
cherrypick/26.3/111196
Sep 12, 2026
Merged

robot-clickhouse-ci-2 merged 11 commits into
backport/26.3/111196from
cherrypick/26.3/111196

Conversation

@robot-clickhouse-ci-2

Copy link
Copy Markdown
Contributor

Original pull-request #111196

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

kakao-tars-case and others added 10 commits July 21, 2026 17:59
…ncelled while the Keeper commit status is unknown

When the Keeper multi request committing an INSERT ends with a hardware
error (e.g. connection loss), the request may or may not have been
applied, so `ReplicatedMergeTreeSinkImpl::commitPart` verifies the status
with an `exists` check under `ZooKeeperRetriesControl`. The preservation
logic previously lived in the `actionAfterLastFailedRetry` callback,
which only runs when the retries are exhausted by Keeper errors: a
`max_execution_time` timeout, `KILL QUERY` or any non-retryable exception
thrown during the recovery bypassed it, the exception left `commitPart`
and `MergeTreeData::Transaction` rolled back the local part. Keeper,
however, could already contain the part metadata, the replication log
entry and the deduplication block id, so the part became lost forever
(replaced by an empty part by the part check), and retrying the same
INSERT was silently deduplicated - permanent data loss. The regression
exists since 23.12: the verify-first recovery introduced by #56917
interacts with the time limit checks between retries introduced by

Now the recovery result is explicit: unless the recovery loop completes
the Keeper checks and produces a result, the commit status is treated as
unknown on any exit path, and the local part is preserved - committed
locally and enqueued for a part check, which keeps it if it is present
in Keeper or drops it otherwise. `actionAfterLastFailedRetry` is
deliberately not used: its contract does not cover cancellation or
non-retryable failures. On these paths the INSERT now fails with
`UNKNOWN_STATUS_OF_INSERT` instead of `TIMEOUT_EXCEEDED` /
`QUERY_WAS_CANCELLED`, consistently with the exhausted-retries path,
so that clients do not interpret the failure as "the data was not
inserted".

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

`writeExistingPart` (used by `ATTACH PART` and `RESTORE`) rolls back the
on-disk rename of the part on any exception from `commitPart` unless
`new_part_was_committed_to_zookeeper_after_rename_on_disk` is set. The
preserve path commits the part into the working set and throws
`UNKNOWN_STATUS_OF_INSERT`, but did not set the flag, so
`try_rollback_part_rename` renamed the already-committed part back to
`detached/` or `tmp_restore_*`, leaving an active part whose directory
is outside the table data path. Set the flag before committing, in the
same way as the successful recovery path does.

Co-Authored-By: Claude Fable 5 <[email protected]>
When the recovery of an ambiguous Keeper commit is aborted by a query
timeout or `KILL QUERY`, the pipeline executor cancels the query before
the exception-carrying chunk of the sink reaches the end of the
pipeline, so the sink's `UNKNOWN_STATUS_OF_INSERT` is dropped and the
client receives the original `TIMEOUT_EXCEEDED` / `QUERY_WAS_CANCELLED`
error. Assert these codes instead of `UNKNOWN_STATUS_OF_INSERT`. The
invariants that actually guard the fix - the part stays readable, the
retried `INSERT` is deduplicated, `lost_part_count` stays 0 - are
asserted separately.

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=111196&sha=5b6739251333d0d68bc3be36ff66860f9ee19ac9&name_0=PR
PR: #111196

Co-Authored-By: Claude Fable 5 <[email protected]>
The test used an unstable , for which ClickHouse disables deduplication. As a result, retrying the insert correctly created another part and made the row-count assertion fail. Add  to the original and retried inserts so they generate the same deduplication block ID and exercise the intended ambiguous Keeper commit path.
Set `new_part_was_committed_to_zookeeper_after_rename_on_disk` only after the local `MergeTreeData::Transaction::commit` succeeds in the ambiguous Keeper recovery path. This keeps the existing `writeExistingPart` rename rollback when the local commit itself fails while still protecting a successfully preserved part from the terminal unknown-status exception.
Apply the same ordering as in the ambiguous-commit preserve path to the
two success paths of `commitPart`: set
`new_part_was_committed_to_zookeeper_after_rename_on_disk` only after
`transaction.commit` succeeds. If the local commit throws before the
part becomes visible in the working set, `writeExistingPart` can still
rename the part back to `detached/` or `tmp_restore_*`, where the data
is preserved instead of being deleted by the cleanup thread.

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

Refresh the branch on top of current `master` to re-run CI. The branch was
25707 commits behind. The merge is clean; the change to
`ReplicatedMergeTreeSink.cpp` and the integration tests are unchanged.
…-ambiguous-commit-rollback

Fix data loss when replicated INSERT commit recovery is interrupted
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 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-critical-bugfix labels Sep 12, 2026
Resolve the conflict in `ReplicatedMergeTreeSink::commitPart`: the 26.3
branch does not contain the master-only failed-quorum cleanup race handling
(the `PreActive` state check under `lockParts`), so the release-branch side
of the conflict is kept and only this pull request's own change is re-applied
on top - set `new_part_was_committed_to_zookeeper_after_rename_on_disk` after
committing the transaction on the unknown-commit-status path, so
`writeExistingPart` does not move a locally committed part back to its
original 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-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! pr-critical-bugfix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants