Repository navigation
Conversation
Projection parts were finalized with a hardcoded no-sync flag in MergeTreeDataWriter::writeProjectionPartImpl, so an acknowledged INSERT (fsync_after_insert), MATERIALIZE PROJECTION, or row-rewriting mutation left every file under <part>/<projection>.proj/ un-fsynced while the parent part's files were fsynced. A power loss shortly after the acknowledgment could leave the projection directory as all-zero-byte files, marking the projection broken on load and keeping it broken indefinitely (CHECK TABLE fails with BROKEN_PROJECTION, force_optimize_projection queries fail). Fix: propagate the caller's sync decision into writeProjectionPartImpl and its two wrappers. The INSERT path passes fsync_after_insert; the mutation and merge paths pass their need_sync decision. For a projection sub-merge that re-merges multiple temporary parts, the final part is produced by a nested MergeTask, so the parent's sync decision is threaded through MergeProjectionPartsTask into that task via a force_sync flag; the single-temp-part case is covered by the temp write (the part is only renamed into place, which preserves the fsynced content). The merge-path need_sync is now computed before the projection temp parts are written. Behavior only changes when a durability setting is enabled; the default (settings off) is unchanged, and no on-disk, wire, or setting format changes. Closes: ClickHouse#111318 Co-Authored-By: Claude Fable 5 <[email protected]>
…merges Address two durability gaps found in code review of the projection fsync fix: - Merge-path projection temp parts are written while streaming rows in executeImpl(), before finalize() ran, so need_sync still held its default false. Compute need_sync in prepare() (right after the input size bounds are known) so it is correct for every projection write, including the streaming pre-calculate flush whose sole temp part is only renamed into place. - MergeProjectionsStage::prepareProjections merges projections that already exist in all source parts via a child MergeTask that did not inherit the parent merge's sync decision. Pass the parent's need_sync as force_sync so the merged projection is fsynced whenever the parent part is. Extend 04548_projection_fsync with an OPTIMIZE (merge) FileSync assertion so the merge paths are exercised, and condense the header comment. Co-Authored-By: Claude Fable 5 <[email protected]>
Comment-only and test-text changes (no functional change): - prepare(): executeImpl() -> executeImpl in the comment. - test: "rebuilt" -> "merged" for the OPTIMIZE section (existing projection parts are merged, not rebuilt); backtick INSERT / MATERIALIZE PROJECTION / ALTER in comments. Co-Authored-By: Claude Fable 5 <[email protected]>
Internal second-model review — adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 7 findings across 1 full pass + 1 fix round + 1 delta recheck).
Severity: ❌ blocker / Session id: cron:clickhouse-author-slot-0:20260722-105200 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-author-slot-0:20260722-105200 |
|
cc @KochetovNicolai @alesapin — could you review this? Projection parts were finalized with a hardcoded no-sync flag in |
|
Workflow [PR], commit [4237f28] Summary: ❌
AI ReviewSummaryThe PR propagates the parent operation's fsync decision to projection writes and sub-merges, adds projection-directory syncs, and covers inline and deferred insert fsync, merges, and mutations. Reviewed head: Findings❌ Blockers
Tests
Final Verdict❌ Block — propagate the projection-directory sync errors and cover the failure path. The existing author-dismissed finding is retained here without a duplicate inline comment or thread reply. |
CI finish ledger — 2b85f39Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task whose fixing-PR link will be posted here when it opens. This PR's own diff (projection-part fsync in
Session id: cron:our-pr-ci-monitor:20260723-040000 |
|
Fixing PR for the The failure is the plain-rewritable |
…-projection-fsync Resolve two conflicts in MergeTreeDataWriter, both in writeProjectionPartImpl and its two wrappers. Master added a `try_adaptive_codec` parameter to the same parameter lists this branch adds `sync` to; the two are orthogonal, so both are kept in declaration order (`sync`, then `try_adaptive_codec`) and every caller passes both explicitly. Also renumber the regression test 04548 -> 04684: master gained 04548_compact_multi_codec_dedup and 04548_part_storage_type_column while this branch was open. 04684 is free on master, in all open pull requests, and across the local worktrees. CI's check_gaps_in_tests_numbers only flags gaps, never collisions, so a collision would have shipped silently. Verified after the merge that the sync decision is still propagated at every call site rather than falling back to the parameter default: the four projection write sites pass fsync_after_insert / need_sync, and the four projection sub-merge sites pass force_sync. Only the top-level parent merges keep the default, which is correct because they compute need_sync from the thresholds themselves.
The merge fsync thresholds were both set to 1, which made that half of the test vacuous. An aggregate projection holds one row per group, so the projection part carries 7 rows where the parent carries 5000. At a threshold of 1 the projection sub-merge clears needSyncPart on its own and syncs regardless of whether the parent's decision is propagated to it, so the assertion could not observe the force_sync plumbing at all. Measured with force_sync removed from ExecuteAndFinalizeHorizontalPart::prepare while leaving the rest of the fix in place: at thresholds of 1 the test still passed, so it would not have caught a regression in that plumbing. Raise min_rows_to_fsync_after_merge to 1000, which sits above the projection part and below the parent part, and set min_compressed_bytes_to_fsync_after_merge to 0 so the byte arm cannot satisfy the check by itself (needSyncPart ORs the two arms). The projection sub-merge now syncs only because the parent's decision reaches it. With that change the same force_sync-removed build reports a merge delta of 0 and fails, while the full fix reports 1 and passes. Verified stable over repeated runs and with three concurrent copies of the test, so it stays parallel-safe and needs no no-parallel tag. Also wrap the literal MATERIALIZE PROJECTION in the writeTempProjectionPart comment in inline code, per the repository conventions.
The materialization block only checked that rows and the projection were readable, which passes whether or not the mutation propagates its sync decision. The mutation's fsyncs run on the IO thread pool, so they are not attributed to the ALTER in system.query_log, which is why the earlier version gave up on measuring them. They are, however, attributed to the produced part in system.part_log, which makes the delta observable after all. Add a row-rewriting ALTER ... UPDATE next to the MATERIALIZE PROJECTION so both temp-projection mutation writes are exercised, apply the same threshold-between-projection-and-parent reasoning as the merge case, and assert on MutatePart FileSync in system.part_log. The assertion is an absolute bound on one table rather than a comparison against a projection-less table: a plain row-rewriting UPDATE touches a different set of files than a projection rebuild, so a cross-table comparison would not be meaningful. Measured with both MutateTask sync arguments reverted to false, the two mutations report 6 and 9 file syncs (min 6); with the fix they report 7 and 10 (min 7). The bound of 6 therefore separates the two, and the reverted build now fails this assertion. Verified stable over repeated runs and with three concurrent copies.
The file-sync counts the test asserts on only exist for Full part storage. A Packed part is a single blob, so there are no per-file syncs to count and the deltas read 0 even when the projection is correctly synced. min_bytes_for_wide_part controls Wide versus Compact, not Full versus Packed, so the fixtures were not actually pinned. min_bytes_for_full_part_storage is randomized by the test runner over 0..512 MB, so roughly a third of CI runs would have placed these 10000-row parts in Packed storage and failed the test. Reproduced by forcing min_bytes_for_full_part_storage to 512 MB, a value inside the randomized range: system.part_log reports part_storage_type = Packed and the mutation assertion drops to 0, failing the test. With the setting pinned to 0 the same parts are Full and it passes. The sibling test 04546_text_index_merge_fsync.sql pins this setting for the same reason. Re-checked that the test still fails when the mutation sync propagation is reverted, so pinning the storage type did not weaken the assertion.
|
Merged master to resolve the conflict (the branch was ~7950 commits behind). Two conflicts, both in Since the parameter threading is the whole fix, I re-checked after the merge that the sync decision is actually propagated at every call site rather than silently falling back to a parameter default: the four projection write sites pass Re-verified the fix in both directions on the merged tree: with the one-line root cause reverted the regression test reports an insert file-sync delta of 0 and fails, with the fix it reports 1 and passes. Three test changes while re-validating, all of which make the test strictly harder to pass:
Also pinned The test stays parallel-safe (verified with three concurrent copies) and needs no |
Syncing the files inside <part>/<projection>.proj/ does not make that directory's own entries durable: fsync of the parent part directory persists only the .proj entry itself. After a power loss the projection directory could come back empty or partial even though every one of its files had been fdatasynced, leaving the projection broken on load, which is the failure this PR set out to fix. Measured with strace on an INSERT into a table with an aggregate projection and fsync_after_insert = 1, fsync_part_directory = 1: all 14 pr.proj files were fdatasynced, the parent tmp_insert_ and final part directories were fdatasynced, and pr.proj itself was never fsynced. Take a directory sync guard on the projection storage, gated on fsync_part_directory, mirroring what writeTempPartImpl already does for the base part. The guard is released when the function returns, after every projection file has been created, so the fsync observes a complete directory. Only the INSERT path needed this. The mutation and merge paths route the final projection through MergeProjectionPartsTask, whose renameTo already syncs the renamed directory and its parent under the same setting; that was confirmed by the same strace measurement. The regression test asserts DirectorySync rather than FileSync, since DirectorySync counts directory fsyncs only and so cannot be satisfied by the projection file syncs the existing assertion already covers. Co-Authored-By: Claude Fable 5 <[email protected]>
The mutation assertion counts files, so anything unrelated that adds one satisfies it without any projection file being synced. Implicit statistics add exactly one such file to the mutated part. Both settings that produce them are randomized by the test runner, and materialize_statistics_on_insert is set to true in 95% of runs. Measured on a build with the mutation projection sync propagation reverted: the minimum FileSync per mutated part is 6 with statistics off, so the assertion fails as intended, but 7 with materialize_statistics_on_insert enabled, so it passes. The assertion could therefore not detect the regression it guards in nearly every CI run. Pin both settings off. Re-checked on the same reverted build with the runner forcing materialize_statistics_on_insert = 1 that the pinned test still fails, so this restores discrimination rather than silencing the assertion. Co-Authored-By: Claude Fable 5 <[email protected]>
The projection directory guard was taken in writeProjectionPartImpl and released when that function returned. On Packed storage the projection's data.packed is not created there: finalizePartAsync only buffers the logical files, and the archive dentry appears later, when the part's transaction is precommitted. Measured with strace on an INSERT into a Packed table, the fdatasync of pr.proj therefore ran before pr.proj/data.packed existed, so the archive was still not durable after a power loss even though the directory was synced. Move the projection directory sync into MergeTreeTemporaryPart::finalize, right after each projection's precommitTransaction, so it observes a complete directory on both Full and Packed storage. Extend the regression test with a Packed fixture, asserting on ProfileEvents['DirectorySync'] and pinning min_bytes_for_full_part_storage high so the fixture really produces a Packed part (verified via system.part_log part_storage_type). Co-Authored-By: Claude Fable 5 <[email protected]>
The Packed fixture only compared ProfileEvents['DirectorySync'], which the directory guard increments. On Packed storage a projection file sync merely sets FinalizePlan::need_sync and data.packed is synced later only when that flag is set, so the assertion stayed green with the projection sync argument dropped. Measured: with that argument forced false the directory assertion still reported 1 while the archive was never fsynced. A Packed part has exactly one file to sync, so assert a FileSync delta of exactly one against the projection-less table. That reddens on the dropped sync argument and on a stray extra sync. Co-Authored-By: Claude Fable 5 <[email protected]>
Co-Authored-By: Claude Fable 5 <[email protected]>
The mutation assertion was an absolute bound on one table's FileSync count, but the number of files a part holds depends on the serialization layout, and CI randomizes it: serialization_info_version = 'basic' drops serialization.json and string_serialization_version = 'single_stream' drops the key.size pair. Measured parent-part file counts of 13, 14, 16 and 20 across four randomized arms, so no constant can be right. The CI runner's minimizer reported the test failing 17/17 and 56/56 with those settings and 0/63 and 0/76 without randomization. The bound was also vacuous in the other direction: on one of fourteen randomized combinations it still read above the threshold with the fix removed. Compare against an identical projection-less table running the same mutation instead, so a layout change moves both readings and cancels out. The two mutations have to sit at the same position in their part's mutation chain: a mutation's sync count depends on that position, not only on the files it writes (on a table with no projection at all, the same UPDATE syncs 7 files as the first mutation and 9 as the second), so the plain table gets a matching no-op mutation. The remaining delta is one per rebuilt projection and does not track how many files the projection holds, so it is asserted exactly. Verified with the fix present on 14 seeded MergeTreeSettingsRandomizer combinations, both settings sets the runner blamed, and 3 concurrent copies; and with the mutation sync argument forced false, where all 14 combinations report 0.
|
The The bound was also vacuous in the other direction: on 1 of 14 randomized combinations it still read above the threshold with the fix removed, so it could have missed a real regression. The assertion is now a delta against an identical projection-less table running the same mutation, so a layout change moves both readings and cancels out. One thing that is not obvious: the two mutations have to sit at the same position in their part's mutation chain. A mutation's sync count depends on that position and not only on the files it writes. On a table with no projection at all, the same Validation, with the Build ID read from the running server in each arm:
Also 3 concurrent copies pass, so no One method note, in case it is useful to anyone reading this later: my first sweep was itself vacuous. A quoting bug left the settings string empty, so all 14 runs passed with no randomization at all. I caught it only by adding the old test as a control arm, on the principle that a sweep in which the known-bad version never fails is not exercising the settings. Session id: cron:clickhouse-maint-slot-13:20260801-221100 |
A merged projection is written straight into the final <part>/<projection>.proj of the result part: prepareProjections builds a child MergeTask whose parent_part is set, which makes local_tmp_prefix empty, and finalizeProjectionsAndWholeMerge only commits the transaction. On a local disk that commit is FakeDiskTransaction::commit, a no-op, so unlike the temp-rename flow MergeProjectionPartsTask uses there was no fsync of the nested projection directory. Syncing the files inside pr.proj does not make the directory's own entries durable, so with fsync_part_directory = 1 an OPTIMIZE or background merge where every source part already has the projection could still come back after power loss with a durable pr.proj entry in the parent part but missing entries inside pr.proj, even though the files themselves were fdatasynced. Measured with strace on such a merge: 15 fdatasync calls on the result part's pr.proj files, one on the parent part directory, none on pr.proj itself, and no .tmp_proj rename to piggyback on. The guard sits below commitTransaction because that is where Packed storage finalizes data.packed; taken any earlier it would sync a directory that does not hold the archive yet. Ordering confirmed by trace on both Full and Packed storage. The test asserts a DirectorySync delta for the OPTIMIZE against an identical projection-less table, matching the two insert arms. Disabling the guard reddens that assertion, and only that assertion, on all 14 seeded settings-randomizer combinations.
The merge directory-sync assertion ran only on a table pinned to Full storage, but the ordering of the guard is storage-sensitive: data.packed is finalized when the projection's transaction is committed, so a guard taken before that would sync a directory that does not hold the archive yet. A regression affecting only Packed merge finalization would have passed. The two Packed tables also needed the merge fsync thresholds the Full fixtures carry, otherwise the projection sub-merge clears its sync decision on its own and the assertion measures nothing.
…directory The directory guard runs independently of whether the projection sub-merge inherited the parent's sync decision, so the directory assertion alone stays green if that propagation is dropped: data.packed is synced only when the sub-merge asked for it. A Packed projection is one archive, so the delta against the projection-less table is exactly one and a missing or stray sync both redden.
|
🕵 @Ergus, all four of your open review threads are addressed in the current head (978f3de), and each has a reply from the author:
CI shows no failures so far. Could you take another look and resolve the threads or update your review? |
|
🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=111392&sha=978f3def1ae1ca265565692c17b38009d9203a7c&name_0=PR&name_1=Upgrade%20check%20%28amd_release%29 and provide a fix in a separate PR. If the fix is already in progress, link it here. The |
|
This is #120975, and #125293 is the fix in progress. All 162 lines of #125293 alone will not stop this failure, though. It changes master's copy of 04653, and this check does not run that copy: the stress phase runs the previous release's tests against the previous release's server ( #125293 has had no review since 2026-10-12. The route that needs no backports is a server-side fix, because the upgraded server is the PR's own build: my #118499 (skip mutating an empty part that cleanup is about to drop) did that, and was closed when #125293 was opened. I can resubmit it as a new PR if you prefer that route. |
CI finish ledger - 978f3deEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
CIDB has no other Session id: cron:our-pr-ci-monitor:20261019-120004 |
Resolve the conflict in `MergeTreeDataWriter::writeProjectionPart`: keep the PR's `sync` argument and master's new `context` argument to `writeProjectionPartImpl`.
|
🕵 Merged The only red check on the previous head was I resolved the four review threads: each one is covered by the code and test changes from 59f7d84 and 6cdee91 that the replies describe (the |
|
🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=111392&sha=67c87fe087cef18057570cdb74274349ab2d7e1c&name_0=PR&name_1=Build%20profile%20diff and provide a fix in a separate PR. If the fix is already in progress, link it here. The only red check on 67c87fe was I merged |
|
The The fix for this signature is my open PR #126200: a readiness probe that the server rejects for its own load (exception code 202 or 241) ends the job green with |
|
🕵 Merged The only red check on the previous head (46c15f1) was @Ergus, all review threads are resolved. Your 2026-10-07 review still shows as changes requested; could you take another look? |
|
Dear @Ergus, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself. |
Ergus
left a comment
There was a problem hiding this comment.
Please remove the "(this isn't important though)" From the PR title before merging.
|
Thanks, removed the suffix from the title. The changelog category stays |
Since ClickHouse#126251 an INSERT with `fsync_after_insert` defers the fsync of its parts (`group_fsync_before_insert_finish`, `auto` = on for non-replicated tables on local disks): the part is written without fsync and `DeferredPartsSync` fsyncs every regular file under the part directory, projections included, before the INSERT finishes. The projection write still took `fsync_after_insert` alone, so with the group fsync each projection file was fsynced inline and then once more. Measured with strace on a Packed part: `tmp_insert_all_1_1_0/pr.proj/data.packed` inline, then `all_1_1_0/pr.proj/data.packed` deferred. The projection now follows the parent part's decision. `04684_projection_fsync` caught it on the master merge (`packed insert projection adds exactly one file sync` read 2). Its INSERT tables now pin `group_fsync_before_insert_finish = 0`, the inline path that replicated tables use by default: under the group fsync master already syncs the projection files (Full parts on master: 31 vs 17 file syncs with it, 17 vs 17 without), so those arms would no longer fail without the fix. A new Packed arm with the group fsync asserts the projection adds exactly one file sync. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
The master merge brought in #126251: with |
The test counts fsyncs, so its run time is mostly the disk's fsync latency, and on the Fast test runner that latency varies several times between runs. 04684 took 0.8-2.8 s on every earlier head of this PR and 19.9 s on c3ee34c, where every statement that fsyncs ran slower (a 10000-row INSERT with `fsync_after_insert` took 1.5 s, the 1.1M-row INSERT without it 0.2 s) and `04622_detach_partition_fsync_part_directory` went from 0.6-1.0 s to 4.9 s in the same job. The 15 s limit for a new test cannot hold with that tail, and removing fsyncs would weaken what the test checks. It still runs in every other stateless job, like `01643_merge_tree_fsync_smoke`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
| projection->getDataPartStorage().precommitTransaction(); | ||
|
|
||
| if (fsync_projection_directory) | ||
| { SyncGuardPtr projection_sync_guard = projection->getDataPartStorage().getDirectorySyncGuard(); } |
There was a problem hiding this comment.
[Blocker] Propagate projection-directory fsync failures before committing the part
This guard is destroyed without calling sync explicitly. LocalDirectorySyncGuard catches every sync exception in its destructor and only logs it, so an EIO while syncing the projection directory does not fail MergeTreeTemporaryPart::finalize. MergeTreeSink::finishDelayedChunk then commits the part, allowing an INSERT with fsync_after_insert = 1, fsync_part_directory = 1 to succeed without establishing durability of the projection's directory entries. A subsequent power loss can still leave the projection incomplete. The identical new guard in MergeTask.cpp:2770 lets merges complete after the same failure.
Please call sync explicitly on each non-null projection guard before proceeding, as VersionMetadataOnDisk already does, so the exception reaches the insert/merge caller. Add focused coverage using local_directory_sync_fail: the current DirectorySync assertions cannot detect this failure because the counter increments before the sync succeeds.
There was a problem hiding this comment.
Measured on master fdc4389b67de: with local_directory_sync_fail enabled, an INSERT with fsync_after_insert = 1, fsync_part_directory = 1 succeeds after the parent part's own guard in MergeTreeDataWriter::writeTempPartImpl logs the injected EIO, so the projection guards match the existing part-directory behaviour. Failing the query on a directory-sync error would have to cover the part-directory and rename guards too, so this PR keeps the existing idiom.
|
@groeneai Merge master, tyhe CI error seems unrelated with this PR changes |
…-projection-fsync Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Merged master in 4237f28, no conflicts; The red check on the previous head was |
Closes: #111318
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Projection parts are now fsynced together with the rest of a data part. Previously projection files (under
<part>/<projection>.proj/) were never fsynced onINSERT(withfsync_after_insert),ALTER TABLE ... MATERIALIZE PROJECTION, or a row-rewriting mutation (with the*_to_fsync_after_mergethresholds), so a power loss right after an acknowledged operation could leave the projection as all-zero-byte files, marked broken on load and reported byCHECK TABLEasBROKEN_PROJECTION.Description
MergeTreeDataWriter::writeProjectionPartImplfinalized every projection part with a hardcoded no-sync flag (finalizePartAsync(..., false)), while the parent part's own files received the correct sync flag. That function serves theINSERTpath (writeProjectionPart) and the mutation / merge path (writeTempProjectionPart), so underfsync_after_insert = 1(INSERT) or the merge/mutation fsync thresholds (MATERIALIZE PROJECTION,ALTER ... DELETE/UPDATE), the projection files were written but never fsynced. After a power loss the parent rows survived but the projection directory could end up all-zero-byte, broken on load and never self-healed.Fix: propagate the caller's sync decision into
writeProjectionPartImpland its two wrappers. INSERT passesfsync_after_insert; mutation and merge pass theirneed_syncdecision. When a projection sub-merge re-merges several temporary parts, the final part is produced by a nestedMergeTask, so the parent's decision is carried throughMergeProjectionPartsTaskto that task via aforce_syncflag; the single-temp-part case is covered by the temp write itself, since it is only renamed into place (rename preserves the fsynced content). The merge-pathneed_syncis now computed before the projection temp parts are written. Withgroup_fsync_before_insert_finish(#126251) anINSERTfsyncs its parts, projections included, only when it finishes, so the projection follows the part's decision and is not fsynced inline as well.Behavior only changes when a durability setting is enabled; with the settings off the behavior is unchanged, and there are no on-disk, wire, or setting format changes.
Report URL for the fsync-coverage finding: #111318
Verified two ways (details in the pre-PR validation gate comment):
strace -e fsync,fdatasyncon the INSERT path shows 0 projection-file fsyncs onmasterHEAD and 10 with this fix; the new stateless test04684_projection_fsyncreadsProfileEvents['FileSync']fromsystem.query_logand fails onmasterHEAD (insert projection adds file syncs 0) and passes with the fix (1). Mutation and merge paths verified viastrace(allpr.projfiles fsynced) plusCHECK TABLEandforce_optimize_projection.Workflow [PR]
Sync PR [sync-upstream/pr/111392]