Skip to content

Never delete an existing Distributed spool file during batch recovery - #118211

Merged
alexey-milovidov merged 1 commit into
masterfrom
fix-distributed-recovery-broken-twin-deletion
Sep 5, 2026
Merged

alexey-milovidov merged 1 commit into
masterfrom
fix-distributed-recovery-broken-twin-deletion

Conversation

@alexey-milovidov

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

Copy link
Copy Markdown
Member

After #117034, DistributedAsyncInsertBatch::recoverBatch treated a same-named twin in the broken/ directory as proof that every file listed before it in current_batch.txt had already been acknowledged, and finalized those files with markAsSend, deleting them. That inference only holds for servers that already have ordered split retries. The previous sendSeparateFiles skipped a file after a transient error such as TOO_MANY_PARTS and continued with the next one, so a server being upgraded from any earlier release can leave an unsent file in front of a quarantined one. Recovery on the new binary then deleted rows that never reached any shard, silently.

This change removes the inference. A still existing file listed before a quarantined twin is kept: the missing quarantined member already makes recoverBatch return false, the stale batch file is dropped, and the surviving files are resent through the normal queue. Duplicates are possible in this edge case, data loss is not.

The recovery integration test now models the pre-upgrade on-disk state and asserts that the unsent predecessor is delivered.

Closes: #118153
Related: #117034
Related: #118138
Related: #118141

Changelog category (leave one):

  • Bug Fix

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

Fix silent data loss on upgrade in Distributed background inserts with background_insert_split_batch_on_failure: batch recovery no longer deletes a spool file that was never sent when a later file of the same batch was quarantined into broken/ by a previous server version.


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

`DistributedAsyncInsertBatch::recoverBatch` treated a twin in the `broken/`
directory as proof that every file listed before it in `current_batch.txt`
had been acknowledged, and finalized them with `markAsSend`. That inference
only holds for servers with ordered split retries. The previous
`sendSeparateFiles` skipped a file after a transient error and continued with
the next one, so a server being upgraded can leave an unsent file in front of
a quarantined one. Recovery then deleted rows that never reached any shard.

Keep such files and let the normal queue resend them: the missing quarantined
member already makes `recoverBatch` return `false`, and the surviving files
are processed from `pending_files`. Duplicates are possible, data loss is not.

The recovery test now models the pre-upgrade state and asserts that the
unsent predecessor is delivered.

Closes: #118153
@clickhouse-gh

clickhouse-gh Bot commented Sep 5, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [6f6e2cd]

Summary: ✅


AI Review

Summary

This PR removes the recovery-time assumption that a broken/ twin proves every earlier entry in current_batch.txt was already acknowledged, so an upgrade no longer deletes an unsent Distributed spool file. I traced the changed recovery logic through the surrounding split-send and queue-rebuild paths, compared it with the linked issue and current regression tests, and did not find a remaining correctness gap in the touched surface.

Final Verdict

✅ Approved

LLVM Coverage Report

Measured on commit 6f6e2cd.

Metric Baseline Current Δ
Lines 88.80% 88.80% +0.00%
Functions 92.20% 92.10% -0.10%
Branches 81.10% 81.10% +0.00%

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

Full report · Diff report

@zlareb1

zlareb1 commented Sep 5, 2026

Copy link
Copy Markdown
Member

Hand-verified on the PR head: this fixes the loss direction of #118153 completely.

The removed block was exactly the markAsSend + files.erase(begin, processed_files) path that deleted an unsent predecessor on the strength of a broken/ twin. With it gone, recoverBatch now:

  • skips only the genuinely-missing prefix (removed in order = already processed), then
  • returns false as soon as it hits the still-quarantined member (the !fs::exists(file) guard in the surviving suffix).

On false, the caller (processFilesWithBatching) does not call batch.send, so no surviving .bin is finalized or deleted; it drops the stale current_batch.txt and the surviving files stay on disk and are resent through the normal queue. That is at-least-once — duplicates possible, loss impossible — which is the correct trade for this edge. No residual deletion path remains in recoverBatch.

One backport note: the 26.8 / 26.6 backports of #117034 (#118141 / #118139) already merged with the deletion inference and without this fix, so they carry the loss window until #118211 is cherry-picked there too; #118138 (26.3) / #118140 (26.7) should land only together with it.

@clickhouse-gh

clickhouse-gh Bot commented Sep 5, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 6f6e2cd00 with master cdc26e3bd (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

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

Job report

@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 Thanks for the hand verification. Agreed on the mechanics: after the change recoverBatch only skips the genuinely-missing prefix and returns false at the first still-quarantined member, so processFilesWithBatching never finalizes or deletes a surviving .bin; the stale current_batch.txt is dropped and the files are resent through the normal queue (at-least-once).

On the backport note: 26.3 (#118138) carries this identical commit, and once this PR merges the pr-must-backport label will produce the 26.6 / 26.7 / 26.8 backports so the release branches that already merged #117034 pick up the fix.

@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.

Ok.

@alexey-milovidov alexey-milovidov self-assigned this Sep 5, 2026
@alexey-milovidov alexey-milovidov removed pr-must-backport Pull request should be backported intentionally. Use this label with great care! pr-critical-bugfix labels Sep 5, 2026
@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Sep 5, 2026
Merged via the queue into master with commit 555771b Sep 5, 2026
174 checks passed
@alexey-milovidov
alexey-milovidov deleted the fix-distributed-recovery-broken-twin-deletion branch September 5, 2026 12:59
@zlareb1

zlareb1 commented Sep 5, 2026

Copy link
Copy Markdown
Member

TL;DR: pr-must-backport was removed from this PR about an hour before it merged, so the robot opened no backports — but 26.6 and 26.8 still need this fix.

  1. The earlier plan on this PR was that the pr-must-backport label would produce the 26.6/26.7/26.8 backports on merge. The label (together with pr-critical-bugfix) was removed at 11:53 UTC and the PR merged at 12:59 UTC with only comp-distributed, so no backport PRs were created.

  2. Current branch state, verified by content on the branch heads: 26.6 (Backport #117034 to 26.6: Keep Distributed spool files after transient split errors #118139, merged) and 26.8 (Backport #117034 to 26.8: Keep Distributed spool files after transient split errors #118141, merged) carry the original Keep Distributed spool files after transient split errors #117034 code including the quarantine-based finalization block that this PR removed — recoverBatch on those branches still calls markAsSend on every spool file listed in front of a quarantined one, which is the silent row loss tracked in Distributed batch recovery after #117034 deletes a never-sent spool file when a later file of the batch was quarantined into broken/ — silent data loss on upgrade #118153 when the batch was written by a pre-Keep Distributed spool files after transient split errors #117034 server. 26.3 (Backport #117034 to 26.3: Keep Distributed spool files after transient split errors #118138) and the still-open 26.7 pick (Backport #117034 to 26.7: Keep Distributed spool files after transient split errors #118140) already carry this PR's commit folded in, so they are fine.

  3. Could you open 26.6 and 26.8 cherry-picks of this PR (or restore the label so the robot does)? Until then, 26.6 and 26.8 are the only branches that ship Keep Distributed spool files after transient split errors #117034 without the follow-up.

@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-distributed Distributed table engine & query routing across shards (sharding/load balancing). pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

3 participants