Repository navigation
Fsync restored part files on RESTORE to survive power loss - #111378
alexey-milovidov merged 6 commits into
Conversation
RESTORE materializes a MergeTree part outside the INSERT write path: MergeTreeData::restorePartFromBackup copies each part file via IBackup::copyFileToDisk, which only writes and finalizes the file (no fdatasync). The part directory is fsynced by the later rename when fsync_part_directory is set, but the file contents never are, even when the table enables fsync_after_insert. On a filesystem with delayed allocation the data blocks are therefore not durable when RESTORED is returned, so a power loss shortly after can leave the restored files at 0 bytes and the part is dropped as broken on the next start. Thread an optional sync flag through IBackup::copyFileToDisk (defaulted to false, so other callers are unchanged). When set, BackupImpl copies through a live destination buffer and fdatasyncs its contents before returning, mirroring the INSERT and replicated-fetch paths. restorePartFromBackup passes fsync_after_insert && !disk->isRemote(), so a restored part on a local disk gets the same durability as an inserted one; on object storage the object is durable once finalized. Closes: ClickHouse#111321 Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-author-slot-6:20260722-105400 |
Internal second-model review — adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 1 finding;
Severity: ❌ blocker / Session id: cron:clickhouse-author-slot-6:20260722-105400 |
|
cc @pamarcos @SmitaRKulkarni — could you review this? It makes |
|
Workflow [PR], commit [9e5af08] Summary: ✅
AI ReviewSummaryThis PR makes Final VerdictStatus: ✅ Approve |
The sync parameter was added as a defaulted argument on the virtual IBackup::copyFileToDisk (and the BackupImpl overrides). clang-tidy's google-default-arguments check prohibits default arguments on virtual methods and is enforced as -warnings-as-errors in the arm_tidy build, so the build failed there. Drop the default and pass sync explicitly at the three call sites that relied on it (base-backup delegate, StorageLog, StorageStripeLog). No behavior change: those callers already used false.
The previous commit gated the base-backup delegation branch in BackupImpl::copyFileToDisk on !sync, forcing entries that come entirely from the base backup (size == base_size) down the generic copy path when sync was requested. For an encrypted-by-disk entry that path reads the base via the public readFile(), which always requests unencrypted data, so restoring an encrypted table from an incremental backup with fsync_after_insert=1 failed with CANNOT_RESTORE_TO_NONENCRYPTED_DISK and left the table empty. Delegate full-base entries to the base backup and forward sync to it: the base is itself a BackupImpl that honours sync and can read its own encrypted entries, so durability is preserved without going through the unencrypted public read path.
|
Pushed two follow-up commits. 1. Fix 2. Fix an encrypted incremental-backup restore regression (found by internal review, then reproduced locally). The first commit gated the base-backup delegation branch on Local verification on the rebuilt binary:
|
The getBaseBackup()->copyFileToDisk(..., sync) branch was previously untested. It restores files that come entirely from the base backup, and it already regressed once during this PR (encrypted entries threw CANNOT_RESTORE_TO_NONENCRYPTED_DISK). Add an encrypted MergeTree case that takes a full backup, an unchanged incremental backup, and restores the incremental with fsync_after_insert = 1, asserting the base-backup files are fsynced. Co-Authored-By: Claude Opus 4.8 <[email protected]>
The encrypted incremental restore case creates a table on an inline
disk(type = encrypted, ...). The encrypted disk type is registered only
under USE_SSL (registerDisks.cpp), and the Fast test build is compiled
without SSL, so the CREATE TABLE fails there with
UNKNOWN_ELEMENT_IN_CONFIG ("unknown disk type: encrypted"). Exclude the
test from the Fast test job, matching the other encrypted-disk stateless
tests (03801, 03820). No effect on the full Linux stateless runs where
the fix and both directions are already exercised.
Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Feedback from GPT-5.6 Sol: I found two remaining implementation gaps and one test-evidence gap at
The existing Finally, because the reported impact is data loss after an acknowledged operation, the changelog category should be |
…ied file count Address review on ClickHouse#111378 (RESTORE fsync durability): - Object-key (lightweight snapshot) entries dropped the sync request: the copyFileToDisk(String) dispatch forwarded to the object-key copy without sync, so restoring object-key entries into a local target with fsync_after_insert reintroduced the durability hole. Route the sync case through a live write buffer + finalize + sync via a new copyObjectKeyEntryToDiskSynced helper (defined unconditionally so it is type-checked in every build; only reachable in the cloud build). The helper mirrors the generic copy path's encryption handling: it rejects an encrypted entry restored to a non-encrypted disk and uses writeEncryptedFile for encrypted-by-disk entries so their bytes are not re-encrypted. - Zero-byte entries returned after createFile, before the sync logic. A required empty part file (e.g. the .bin of an all-empty Array column) is fsynced by INSERT, so create it through a live buffer + finalize + sync when sync is requested, restoring INSERT parity. - Strengthen 04550: add an empty Array(UInt32) column (zero-byte .bin), and assert the on/off FileSync delta covers the number of files RESTORE actually copies (physical files minus the version-metadata files RESTORE skips), instead of comparing against system.parts.files. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Thanks, all three addressed in 9e5af08.
Agree the impact is data loss after an acknowledged operation; switched the changelog category to |
Internal second-model review - adjudication log (click to expand)Pre-publication review of the resulting code by an independent model (engine: codex; 1 finding; bounded: 1 full pass + 1 fix round).
Severity: ❌ blocker / |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 43/88 (48.86%) · Uncovered code |
CI finish ledger — 9e5af08Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task
CI is fully finished on this commit with zero failing checks. Session id: cron:our-pr-ci-monitor:20260729-043000 |
|
📊 Cloud Performance Report 🔴 AI verdict: This PR only changes BACKUP/RESTORE fsync durability (BackupImpl copyFileToDisk gains a sync path, restorePartFromBackup fsyncs restored part files); it does not touch the SELECT query-execution hot path any ClickBench query exercises. Q4 (-10.5%) and Q15 (-11.6%) are consistent improvements that correctly land inside master's current variance band, and Q30's flagged +17.4% is off-path with a median still inside its band, so it is downgraded to not_sure. Q23's x2.1 slower is a large, consistent delta that the deterministic trust override locks in place, but because nothing here touches that query's path it is almost certainly environmental - re-run before treating it as a real regression. clickbench🔴 1 regressed · Flagged queries (2 of 43)
Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on. tpch_adapted_1_official🟢 No significant changes Debug info
|
What does it mean? Did you review this PR? Is it approved? |
Backport #111378 to 26.7: Fsync restored part files on RESTORE to survive power loss
Backport #111378 to 26.6: Fsync restored part files on RESTORE to survive power loss
…e_fsync_after_insert The test read the `files` column of `system.parts` to size the encrypted-incremental-restore fsync assertion. That column was added by 476e9fc in January, after 25.8 forked, so the automated 25.8 backport of #111378 (#115088) fails at runtime with UNKNOWN_IDENTIFIER even though the cherry-pick applied cleanly and the script is byte-identical to master. The empty shell variable then also fails the `{files:UInt64}` parameter binding. The count the assertion wants is how many files RESTORE copies and must therefore fsync. The test already measures exactly that for its first table via `SELECT path FROM system.parts` plus `find`, so feed the encrypted arm the same count and drop the column read. The two tables have identical schema, data and part-layout settings, so their parts hold the same file set; the physical count is also larger than the checksums map (18 vs 14 on master), since it covers checksums.txt, columns.txt and the zero-byte arr.bin, so the assertion gets stronger rather than weaker. Validated on the official 25.8 binary: the old test reproduces both errors, the new one runs to completion. On master the encrypted restore performs 20 FileSync events against a required 18, the two arms' part file lists are identical on both branches (18 files on master, 15 on 25.8), disabling fsync_after_insert still fails the test, and 50/50 runs pass with randomized settings. Related: #111378 Related: #115088 Co-Authored-By: Claude Fable 5 <[email protected]> (cherry picked from commit fd8fc0c)
Backport #111378 to 26.5: Fsync restored part files on RESTORE to survive power loss
Backport #111378 to 26.3: Fsync restored part files on RESTORE to survive power loss
…e_fsync_after_insert The test read the `files` column of `system.parts` to size the encrypted-incremental-restore fsync assertion. That column was added by 476e9fc in January, after 25.8 forked, so the automated 25.8 backport of ClickHouse#111378 (ClickHouse#115088) fails at runtime with UNKNOWN_IDENTIFIER even though the cherry-pick applied cleanly and the script is byte-identical to master. The empty shell variable then also fails the `{files:UInt64}` parameter binding. The count the assertion wants is how many files RESTORE copies and must therefore fsync. The test already measures exactly that for its first table via `SELECT path FROM system.parts` plus `find`, so feed the encrypted arm the same count and drop the column read. The two tables have identical schema, data and part-layout settings, so their parts hold the same file set; the physical count is also larger than the checksums map (18 vs 14 on master), since it covers checksums.txt, columns.txt and the zero-byte arr.bin, so the assertion gets stronger rather than weaker. Validated on the official 25.8 binary: the old test reproduces both errors, the new one runs to completion. On master the encrypted restore performs 20 FileSync events against a required 18, the two arms' part file lists are identical on both branches (18 files on master, 15 on 25.8), disabling fsync_after_insert still fails the test, and 50/50 runs pass with randomized settings. Related: ClickHouse#111378 Related: ClickHouse#115088 Co-Authored-By: Claude Fable 5 <[email protected]>
Backport #111378 to 25.8: Fsync restored part files on RESTORE to survive power loss
…ORE to survive power loss
…ORE to survive power loss
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
RESTOREnow fsyncs the restored part files when the destination table hasfsync_after_insertenabled, so a restored part is as durable against power loss as an inserted one. Previously restored files were only written and closed (never fsynced), so a power loss right afterRESTOREreturnedRESTOREDcould leave the parts torn and the table empty.Description
Closes: #111321
RESTOREmaterializes a MergeTree part outside theINSERTwrite path:MergeTreeData::restorePartFromBackupcopies each part file viaIBackup::copyFileToDisk, which only doescopyData+finalize()(nofdatasync). The part is then attached through the rename machinery, so the part directory is fsynced whenfsync_part_directoryis set, but the file contents are never fsynced, even when the table setsfsync_after_insert. On a filesystem with delayed allocation the file data blocks are therefore not durable whenRESTOREDis returned; a power loss shortly after can leave every restored file at 0 bytes and the part is dropped as broken on the next start.The two other part-materialization paths already fsync file contents:
INSERT(viafsync_after_insert->MergedBlockOutputStream) and the replicated fetch path (DataPartsExchange,if (sync) file->sync()). This makesRESTOREconsistent with them.Change:
IBackup::copyFileToDisktakes abool sync. When set,BackupImplcopies through a live destination buffer andfdatasyncs its contents before returning.MergeTreeData::restorePartFromBackuppassessync = fsync_after_insert && !disk->isRemote(), so a restored part on a local disk gets exactly the durability the table's fsync settings already promise an inserted part. On object storage the object is durable once finalized, so the flag is a no-op there.Three restore copy paths carry
sync:finalize+syncwhensyncis requested, mirroring the generic path's encryption handling (reject encrypted -> non-encrypted disk,writeEncryptedFilefor encrypted-by-disk entries);.binof an all-emptyArraycolumn), whichINSERTalso fsyncs, are created through a live buffer +finalize+syncinstead ofcreateFile.Regression test
04550_restore_fsync_after_insertasserts, via the per-queryFileSyncProfileEvents, that the on/offFileSyncdelta covers the number of filesRESTOREactually copies (including a zero-byte file) withfsync_after_insert = 1, and stays below it with= 0. It also covers encrypted incremental restore.Version info
26.8.1.1548(included in26.8and later)26.7.4.49,26.6.3.48,26.5.7.59,26.3.19.3,25.8.31.7