Skip to content

Fix CHECK TABLE re-fetch loop on parts with unknown-projection orphan checksum entries - #104098

Closed
groeneai wants to merge 3 commits into
ClickHouse:masterfrom
groeneai:groeneai/fix-100413-orphan-proj-checksum-fetch
Closed

groeneai wants to merge 3 commits into
ClickHouse:masterfrom
groeneai:groeneai/fix-100413-orphan-proj-checksum-fetch

Conversation

@groeneai

@groeneai groeneai commented May 5, 2026

Copy link
Copy Markdown
Collaborator

Problem

When a replica fetches a part containing a projection that was dropped
from the table schema, the sender's getProjectionParts does not
include the unknown projection, so its .proj/ subdirectory is not
transferred -- but checksums.txt is copied verbatim from the sender
and still references the orphan entry.

checkDataPart (called from CHECK TABLE and the background
ReplicatedMergeTreePartCheckThread) then computed checksums_data
from the actual on-disk files (which do not include the missing
.proj) and called checksums_txt.checkEqual(checksums_data, ...),
which threw NO_FILE_IN_DATA_PART for the orphan entry.

The background thread caught the exception, marked the part broken,
and re-fetched it from the same replica -- producing the same state
and looping indefinitely.

Fix

Strip orphan .proj entries from checksums_txt whose directory is
not on disk before checkEqual, in src/Storages/MergeTree/checkDataPart.cpp.

This mirrors the existing handling for the local-attach variant
(PR #99623) where the directory is on disk but unknown to the table;
together they cover both ways an unknown projection can outlive the
table schema:

Variant Directory on disk checksums.txt entry Existing handling
Local attach (DETACH + drop projection + ATTACH) yes (orphan dir) yes PR #99623 (existing)
Fetch (sender drops projection, receiver SYSTEM SYNC REPLICA) no yes (verbatim copy) this PR

In both cases is_broken_projection is set so the caller can react
gracefully.

Test

Added tests/queries/0_stateless/04203_100413_check_table_unknown_projection_after_fetch.sh
which:

  1. Sets up a 2-replica ReplicatedMergeTree,
  2. Adds and materializes projection pp on replica 1,
  3. Detaches the partition, drops pp from the table schema, drops
    replica 2's detached copy,
  4. Re-attaches on replica 1 (orphan pp.proj/ on disk, orphan
    pp.proj entry in checksums.txt),
  5. SYSTEM SYNC REPLICA on replica 2 -- forces a fetch where the
    .proj/ subdirectory is not transferred but checksums.txt is,
  6. Runs CHECK TABLE on replica 2 with check_query_single_value_result = 1.

Pre-fix, step 6 returned 0 and the server log showed Part 0_5_5_0 looks broken. Removing it and will try to fetch. (the start of the
re-fetch loop). Post-fix it returns 1.

Verified locally: 20/20 passes with random settings. Existing tests
03822_attach_with_unknown_projection and
03145_non_loaded_projection_backup are unaffected.

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 an infinite re-fetch loop when a ReplicatedMergeTree replica fetches a part that contains a projection unknown to the current table schema (e.g. the projection was dropped while the part was detached on the sender). Previously, the sender skipped the unknown .proj/ subdirectory but copied checksums.txt verbatim -- the receiver's checkDataPart then threw NO_FILE_IN_DATA_PART, marked the part broken, and re-fetched it forever. checkDataPart now strips orphan .proj entries from checksums_txt whose directory is not on disk, mirroring the existing handling for the local-attach variant.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Closes #100413


Session: cron:clickhouse-ci-task-worker:20260505-111500

… checksum entries

When a replica fetches a part containing a projection that was dropped
from the table schema, the sender's `getProjectionParts` does not
include the unknown projection, so its `.proj/` subdirectory is not
transferred -- but `checksums.txt` is copied verbatim from the sender
and still references the orphan entry.

`checkDataPart` (called from `CHECK TABLE` and the background
`ReplicatedMergeTreePartCheckThread`) then computed `checksums_data`
from the actual on-disk files (which do not include the missing
`.proj`) and called `checksums_txt.checkEqual(checksums_data, ...)`,
which threw `NO_FILE_IN_DATA_PART` for the orphan entry.

The background thread caught the exception, marked the part broken,
and re-fetched it from the same replica -- producing the same state
and looping indefinitely.

Strip orphan `.proj` entries from `checksums_txt` whose directory is
not on disk before `checkEqual`.  This mirrors the existing handling
for the local-attach variant (PR ClickHouse#99623) where the directory is on
disk but unknown to the table; together they cover both ways an
unknown projection can outlive the table schema.

Add `tests/queries/0_stateless/04203_100413_check_table_unknown_projection_after_fetch.sh`
which exercises the fetch path (`SYSTEM SYNC REPLICA` after dropping
the projection) and verifies `CHECK TABLE` succeeds on the receiver.

Closes ClickHouse#100413
@groeneai

groeneai commented May 5, 2026

Copy link
Copy Markdown
Collaborator Author

Pre-PR Validation Gate (per task-worker procedure):

a) Deterministic repro? ✅
The new test 04203_100413_check_table_unknown_projection_after_fetch.sh deterministically fails on master and passes on this branch. Confirmed by building both binaries and running the test against each. Pre-fix the server log shows Part 0_5_5_0 looks broken. Removing it and will try to fetch. and CHECK TABLE returns 0.

b) Root cause explained? ✅
On the sender, getProjectionParts does not include projections unknown to the table schema, so the send_projections loop skips their .proj/ subdirectories. But checksums.txt is copied verbatim and still references those entries. On the receiver, checkDataPart computes checksums_data from the actually-on-disk files (no .proj for the orphan) and calls checksums_txt.checkEqual(checksums_data, ...), which throws NO_FILE_IN_DATA_PART for the orphan entry. The catch in ReplicatedMergeTreePartCheckThread::checkPartImpl then marks the part broken and re-fetches it -- producing the same state.

c) Fix matches root cause? ✅
Strips orphan .proj entries from checksums_txt whose directory is not on disk, before checkEqual -- so the inconsistency that drives the throw is resolved at the consistency check itself. Mirrors the existing PR #99623 block that handles the symmetric local-attach variant (directory on disk, unknown to the table). No widening of bounds, no broad no-random-* tag, no data-size reduction.

d) Test intent preserved? / New tests added? ✅
Added a new regression test (04203) that explicitly exercises the fetch path the issue describes. Existing tests 03822_attach_with_unknown_projection and 03145_non_loaded_projection_backup continue to pass unchanged.

e) Both directions demonstrated? ✅

  • Without the fix: test fails (CHECK TABLE returns 0, server log shows the re-fetch trigger).
  • With the fix: test passes 20/20 with random settings + MergeTree settings randomization.

f) Fix is general, not a narrow patch? ✅
The strip is keyed on a content-level invariant (.proj entry present in checksums.txt && directory absent from storage), not on a specific source -- so it covers fetch, restore, and any future producer that leaves an orphan .proj reference. The symmetric local-attach variant remains handled by the existing PR #99623 block. No defensive null-check; the strip targets exactly the consistency-violating state.

@groeneai

groeneai commented May 5, 2026

Copy link
Copy Markdown
Collaborator Author

cc @CheSema @KochetovNicolai — could you review this? Follow-up to #99623: the existing block in checkDataPart strips orphan .proj entries when the directory is on disk but unknown to the table; this PR adds the symmetric strip for the fetch case where checksums.txt references a .proj whose directory was never transferred (sender skipped it because getProjectionParts excluded the unknown projection). Pre-fix, checkDataPart threw NO_FILE_IN_DATA_PART and the receiver looped re-fetching the same part. Closes #100413.

@clickhouse-gh

clickhouse-gh Bot commented May 5, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [acaea9f]

Summary: ❌

job_name test_name status info comment
Stateless tests (amd_debug, parallel) FAIL
Server died FAIL cidb
Logical error: Virtual row boundary violated in MergingSortedAlgorithm for input A on sort column 'B' C: the virtual row announced D but the source then produced E, which would mis-order the merge (STID: 2651-3359) FAIL cidb
Unit tests (asan_ubsan, function_prop_fuzzer) FAIL
FunctionsStress FAIL cidb

AI Review

Summary
  • This PR fixes the replicated fetch path where checksums.txt can retain an orphan .proj checksum entry after the sender skips an unknown projection directory. The added checkDataPart handling is narrowly scoped to missing projection directories, preserves the existing error for unknown projection directories that are actually present on disk, and the regression test covers the receiver-side SYSTEM SYNC REPLICA plus CHECK TABLE flow.
Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label May 5, 2026
@zlareb1

zlareb1 commented Jun 7, 2026

Copy link
Copy Markdown
Member

@groeneai check CI failures.

@groeneai

groeneai commented Jun 7, 2026

Copy link
Copy Markdown
Collaborator Author

zlareb1, triaged the 3 failures on 4f8f013c. None caused by this PR. Plan: rebase past the post-base revert; the failures will clear.

Root cause (single): the run's base 3fa1ed2d (2026-06-03 05:55Z) sits in the ~9h window where master carried PR #104690 ("Add UntrackedMemory asynchronous metric", merged 2026-06-02 21:16Z), which introduced a UBSAN bug in boost::intrusive::list_node access in DB::UntrackedMemoryRegistry::remove. It was reverted at 3930f8ab on 2026-06-03 06:28Z (~24 min after our merge-base). All 3 failures trace to that.

# Check / Test Attribution Evidence
1 Unit tests (asan_ubsan) Pre-existing trunk bug from #104690 UBSAN: member access within null pointer of type 'boost::intrusive::list_node<void *>' at contrib/boost/boost/intrusive/detail/list_node.hpp:66:10, stack into DB::UntrackedMemoryRegistry::remove (src/Common/UntrackedMemoryRegistry.cpp:36) → DB::ThreadStatus::~ThreadStatus. 131 PRs / 146 hits / 14d trunk-wide; 5 master hits in the merge→revert window, 0 since.
2 Stateless tests (amd_tsan, parallel, 1/2) → 00096_obfuscator_save_load Same root cause (chronic flaky on TSAN; spike from #104690) clickhouse-obfuscator aborts on ThreadStatus::~ThreadStatus path. Daily TSAN failures: 2026-06-02=18, 06-03=207, 06-04=1, 06-05=6, 06-06=2. PR #104690's own CI logged 10 hits of these tests; revert dropped to baseline. Cross-PR: 113 PRs / 124 hits / 30d. CI rerun harness flagged "flaky (3/11 reruns failed)". 3 master hits / 30d — all on 2026-06-03 in the window.
3 Stateless tests (amd_tsan, parallel, 1/2) → 00175_obfuscator_schema_inference Same root cause Same clickhouse-obfuscator Aborted shape. CI rerun harness "flaky (1/9)". 112 PRs / 126 hits / 30d. 5 master hits / 30d — all in the same window.

PR-relatedness check: this PR's diff is src/Storages/MergeTree/checkDataPart.cpp (+orphan .proj strip in CHECK TABLE re-fetch path) and 04203_*.sh|.reference. None of the failures touch projections, replication, MergeTree, or the obfuscator binary's source. Causal link to our code: zero.

Reapply attempt for #104690 is open as #106386 (mstetsyuk) and is what should fix the upstream bug; not in our path.

Action: rebasing the branch onto current master (post-revert) and pushing. No other code change needed for these failures. Will report back if anything reappears on the new SHA.

session: cron:clickhouse-ci-task-worker:20260607-082800

@clickhouse-gh

clickhouse-gh Bot commented Jun 7, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.60% 84.50% -0.10%
Functions 92.30% 92.30% +0.00%
Branches 77.20% 77.20% +0.00%

Changed lines: Changed C/C++ lines covered by tests: 11/11 (100.00%) | Lost baseline coverage: none · Uncovered code

Full report · Diff report

@groeneai

groeneai commented Jun 7, 2026

Copy link
Copy Markdown
Collaborator Author

CI Ledger — covered SHA: acaea9f0a40ea498835082b45bc10b7fd55c1e4f

CI finished. Both failures classified and already tracked elsewhere; neither is caused by this PR's diff (src/Storages/MergeTree/checkDataPart.cpp + 04203_* regression test).

Check Test Classification Disposition
Stateless tests (amd_debug, parallel) STID 2651-3359 Virtual row boundary violated in MergingSortedAlgorithm chronic master debug-only assertion (not PR-caused) Tracked in our umbrella task for STID 2651-3359; fix PR #106664 in flight (CI Fix or Improvement, debug-only chassert silencer; deeper merge-ordering correctness deferred to vdimir per bot review).
Unit tests (asan_ubsan, function_prop_fuzzer) FunctionsStress (Unsupported argument types for function geohashesInBox) chronic (99 distinct PRs / 184 hits / 30d) Tracked under the FunctionsStress non-determinism family (issue #104877). Not caused by this PR.

This PR's diff does not touch sorting, merging, or function-prop-fuzzer surfaces.

@groeneai

Copy link
Copy Markdown
Collaborator Author

Closing: superseded by #108118 (merged 2026-07-22), which landed the same fix in src/Storages/MergeTree/checkDataPart.cpp via commit 04235cb.

Master now drops leftover .proj checksums entries for projections unknown to the current metadata and absent on disk, before the checkEqual in checkDataPart (the fetch-side orphan case this PR targeted). It additionally removes the broad try/catch in Fetcher::downloadPartToDisk so genuine base-file corruption stays fatal, which this PR did not address. Coverage for both the re-attach and the inter-replica fetch paths is in 03822_attach_with_unknown_projection.sh.

The re-fetch loop from #100413 is fixed on master, so there is nothing left for this PR to add.

@groeneai groeneai closed this Jul 25, 2026
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 pr-bugfix Pull request with bugfix, not backported by default

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ReplicatedMergeTree: CHECK TABLE fails after fetching part with unknown projection (re-fetch loop)

4 participants