Skip to content

Rework projection part storage around an owned-set descriptor API - #113278

Draft
arsenmuk wants to merge 1 commit into
masterfrom
arsenmuk/projection-storage-format-prep
Draft

arsenmuk wants to merge 1 commit into
masterfrom
arsenmuk/projection-storage-format-prep

Conversation

@arsenmuk

@arsenmuk arsenmuk commented Aug 4, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • Improvement

Changelog entry:

Rework projection handling in the MergeTree part storage layer around an explicit, manifest-driven projection set. Fixes projections lost by mutations on object-storage disks; CHECK TABLE now reports a projection the part owns but whose directory is missing as broken instead of skipping it, and regenerates a projection's lost checksums.txt from data instead of failing the whole part.


Preparation for the flat projection storage format (#108443) — the layout-independent half, split out to be reviewable on its own. The on-disk format is unchanged; behavior of healthy parts is unchanged.

Major changes

  1. Owned projection set on IDataPartStorage. Each part storage tracks the projections its part owns as Projection descriptors (getProjections / createProjection / renameProjection / removeProjection), replacing ad-hoc ".proj" string paths and directory walks across the create/rename/freeze/remove/check paths. checksums.txt is the authoritative manifest: only manifest-listed projections are loaded, and an on-disk directory the manifest does not list is not adopted at load.

  2. Fix: mutations lost projections on object-storage disks (query-time FILE_DOESNT_EXIST on a vacated tmp_mut_* path). Object-storage disks run deferred transactions (Do not use fake tx for object storage disks #89658), so a disk probe at mutation finalize cannot see the rebuilt projection's still-queued rename; the owned set is now maintained by the write path itself and probed from disk only on part load. Covered by the new test_os_* matrix ({full, packed} × {local, s3}).

  3. Fix: CHECK TABLE / part load report a projection the part owns but whose directory is missing as a broken projection (previously skipped silently), and regenerate a projection's missing checksums.txt from data (previously this failed the whole part).

Side changes

  • Mutations fold carried (hardlinked) projections into the new part's checksums.txt before finalize, so the manifest commits atomically with the part.
  • Regenerating a lost part-level checksums.txt restores the projection entries (previously the repaired manifest silently dropped every projection).
  • freezeRemote is folded into freeze(dst_disk); packed freeze copies projections from the owned set instead of a disk walk, so residue directories are not carried into clones.
  • Replication fetch creates projection directories through createProjection, sweeping stale residue of a failed same-named attempt before download.
  • Table RENAME repoints loaded projection sub-part storages (their roots kept the old table path).
  • Backups place a projection under its logical <name>.proj name, independent of the on-disk layout.
  • Stale temporary-directory reclaim is deduplicated into MergeTreeData::reclaimStaleTemporaryDirectory.
  • Tests: a gtest for the owned-set invariants; integration suite test_projection_storage_format — manifest desync/repair, CHECK TABLE classification, packed-storage projection lifecycle, and the object-storage rebuild/carry matrix.

Exact scenarios changed (vs master)

  • Mutation rebuilding or carrying a projection on an object-storage disk: projection survives; was lost at query time (b, e).
  • CHECK TABLE on a part owning a projection whose directory is missing: reports broken projection; was silent (c).
  • CHECK TABLE / load of a projection missing its own checksums.txt: recomputed and persisted; previously failed the part (d).
  • Load of a part with a projection directory not listed in checksums.txt: ignored (not adopted); previously could be picked up by the disk walk.
  • Everything else — INSERT/merge/mutate/fetch/freeze/backup/move/detach on healthy parts — is behavior-preserving; test_broken_projections (the pre-existing suite) passes unchanged.

Preparation for the flat projection storage format (#108443): the
layout-independent half of that work. The part-storage layer is reworked
around an explicit owned projection set with struct Projection
descriptors, seeded from the checksums.txt manifest, replacing ad-hoc
".proj" string paths and directory walks. On-disk format and behavior of
healthy parts are unchanged.

- IDataPartStorage gains getProjections/setProjections (the owned set;
  reads throw if it was never seeded, so residue of an unrelated
  same-named part is never silently adopted), detectProjections (an
  explicit disk-truth probe for the few places that must look at the
  disk), createProjection/renameProjection/removeProjection/
  removeProjectionResidue, and getProjectionStorageForWrite, which
  requires the descriptor produced by createProjection so a writer can
  never build over an unswept directory. freezeRemote is folded into
  freeze(dst_disk).
- checksums.txt is the authoritative manifest of which projections a
  part owns: loadProjections loads only manifest-listed projections, and
  mutations fold carried (hardlinked) projections into the new part's
  checksums before finalize, so the manifest commits atomically with the
  part.
- checkDataPart iterates the declared projections against a disk scan:
  an owned-but-missing projection is reported broken instead of silently
  skipped, and a projection whose own checksums.txt is missing gets it
  recomputed from data and persisted.
- The owned set is seeded from the manifest-driven disk probe only on
  the part LOAD path (loadColumnsChecksumsIndexes). The write path
  relies on the set maintained by createProjection/renameProjection: a
  probe at mutation finalize would miss directories still staged in a
  deferred object-storage transaction and lose the projection,
  surfacing as a query-time FILE_DOESNT_EXIST on a vacated tmp_mut_
  path.
- Tests: a gtest for the owned-set invariants; integration coverage for
  manifest desync/repair, CHECK TABLE classification, packed-storage
  projection lifecycle, and the object-storage rebuild/carry matrix
  ({full,packed} x {local,s3}) asserting fail-closed via CHECK TABLE.

Co-Authored-By: Claude Fable 5 <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Aug 4, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [347ced2]

Summary: ✅


AI Review

Summary

This PR reworks projection ownership around an explicit per-part owned set and fixes several real projection desync cases, but the invariant is not yet applied consistently. I found two correctness holes where a same-named stray projection directory can still escape the checksums.txt authority boundary, plus one lifecycle gap where Full-storage freeze / clone still copies residue outside the owned set.

Findings

❌ Blockers

  • [src/Storages/MergeTree/IMergeTreeDataPart.cpp:1421] loadColumnsChecksumsIndexes seeds the owned set from every metadata-declared projection name, so a part created with materialize_projections_on_insert = 0 can still treat a stray on-disk p.proj as owned. Later getProjections consumers such as the hardlink-carry mutation path and packed freeze propagate that residue into new parts even though the manifest omitted it.
  • [src/Storages/MergeTree/checkDataPart.cpp:379] The repair path stops ignoring a declared projection as soon as its directory exists on disk, even when the parent manifest never listed it. If the parent part loses checksums.txt, checkDataPart recomputes and persists a p.proj entry for that stray directory, effectively adopting residue into the regenerated manifest.
    ⚠️ Majors
  • [src/Storages/MergeTree/DataPartStorageOnDiskBase.cpp:677] The Full-storage freeze / clonePart path still copies the whole part directory before seedFrozenCopy narrows ownership. residue.proj and *.tmp_proj therefore survive into shadow or moved clones, so the new owned-set invariant only holds for Packed storage.
Final Verdict

Changes requested.

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.50% 86.50% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 78.70% 78.70% +0.00%

Changed lines: Changed C/C++ lines covered: 879/971 (90.53%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-improvement Pull request with some product improvements label Aug 4, 2026
if (IDataPartStorage::Projection::dirNameType(file_name) == IDataPartStorage::Projection::Status::Live)
owned_projection_dirs.push_back(file_name);
for (const auto & projection : metadata_snapshot->projections)
owned_projection_dirs.push_back(IDataPartStorage::Projection::dirName(projection.name, false));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

loadColumnsChecksumsIndexes is still treating a metadata-declared projection directory as owned even when checksums.txt never committed it. A concrete trace is a part created with materialize_projections_on_insert = 0: if a stray p.proj appears on disk, startup leaves it unloaded, but this candidate list still makes getProjections return it. The later hardlink-carry path in MutateTask and the packed freeze path in DataPartStorageOnDiskPacked both trust getProjections as authoritative, so that stray directory gets carried into the new part or shadow copy. That breaks the "checksums.txt is the commit point" contract the PR is trying to establish.

const auto exception_message = getCurrentExceptionMessage(true);
/// A projection the part owns (loaded from its manifest or listed in checksums.txt) but whose
/// directory is gone is broken, not absent; skip only projections this part never materialized.
if (!projection_on_disk && !loaded && !checksums_txt.files.contains(projection_file))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This skip condition flips as soon as a declared projection directory exists on disk, even if the parent manifest never listed it. In the repair path that means a stray same-named p.proj stops being ignored: loadChecksums calls checkDataPart, projection_on_disk is true, we compute projection_checksums, and lines 457-460 add p.proj to the regenerated checksums.txt. So a part that previously followed the "checksums.txt is authoritative" rule can adopt residue just because its own manifest was lost.

/*initialize=*/ !to_detached && !params.external_transaction);

return frozen_storage;
seedFrozenCopy(*new_storage);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the time we reach seedFrozenCopy, the base freeze / clonePart path has already copied the whole part directory recursively. That means Full storage still carries residue.proj and *.tmp_proj into shadow or moved clones even though the new owned-set API is supposed to be the authority for projection lifecycle. The packed path below was rewritten to recurse from getProjections; this base path still bypasses that invariant for Full parts.

@alesapin alesapin self-assigned this Aug 5, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. label Sep 4, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 10, 2026

Copy link
Copy Markdown
Contributor

Dear @arsenmuk, this PR hasn't been updated for a while. Will you continue working on it? If not, please close it. Otherwise, ignore this message.

@clickhouse-gh clickhouse-gh Bot added comp-part-formats Formats of MergeTree data parts in the public repository, that is how a part is laid out on disk ... and removed comp-mergetree MergeTree* family: parts, merges, primary index, column statistics, background data transformation. labels Nov 19, 2026

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

comp-part-formats Formats of MergeTree data parts in the public repository, that is how a part is laid out on disk ... pr-improvement Pull request with some product improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants