Repository navigation
Fsync access entities in DiskAccessStorage to survive power loss - #111346
alexey-milovidov merged 10 commits into
Conversation
DiskAccessStorage wrote access-entity .sql files (and the *.list index files), and removed them, without any fsync. An acknowledged CREATE/ALTER/ DROP USER or GRANT/REVOKE could therefore be silently lost on power loss: the tmp->final rename was never forced to disk, so the <uuid>.sql file disappears and the user cannot authenticate after restart. It was the only on-disk DDL-entity store not honoring fsync_metadata. Gated on fsync_metadata (default true), matching UDF/named-collection/ workload/table-metadata stores: - writeEntityFile (create/update): fsync the file content, then fsync the parent directory around the rename (a rename is durable only once the directory entry is persisted). - deleteAccessEntityOnDisk (DROP/REVOKE, old-file removal on replace): fsync the parent directory around the unlink. - writeListFile: fsync the file content (.list files are rebuilt from the .sql files, so no directory fsync). The parent-directory fsync reuses LocalDirectorySyncGuard. The setting is resolved from the query context and falls back to the global context for the background .list writer, mirroring getWriteSettings(). Adds integration test test_access_storage_fsync asserting the FileSync/ DirectorySync path via system.query_log ProfileEvents (integration, because stateless tests override the access storage to <memory/>). Closes: ClickHouse#68958 Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-author-slot-0:20260722-084600 |
Internal second-model review — adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 1 finding; bounded: 1 full pass + 1 fix round + 1 delta recheck).
Severity: ❌ blocker / Session id: cron:clickhouse-author-slot-0:20260722-084600 |
|
cc @pufit @kcmannem — could you review this? It makes |
|
Workflow [PR], commit [e83997b] Summary: ✅
AI ReviewSummaryThe PR makes local access-entity writes and removals in Missing context / blind spots
Final Verdict✅ Approve — no unresolved code findings. Current-head integration results remain pending. |
scheduleWriteLists() writes the fsync'd <id>.sql, then defers the .list index rewrite to a background thread and drops the need_rebuild_lists.mark marker so the next startup rescans the .sql files instead of trusting the stale .list indexes. The marker was created with a plain std::ofstream and never fsync'd, so a power loss between the .sql write and the background .list rewrite could lose the marker: on restart the constructor misses it, trusts the stale .list, and never rescans the surviving .sql, making an acknowledged CREATE/ALTER/DROP USER unreachable. This closed the .sql durability gap but left the PR missing its own end-to-end contract. Fsync the marker file and its parent directory when it is written, gated on fsync_metadata, reusing the WriteBufferFromFile + LocalDirectorySyncGuard pattern already used for the entity files. Write the marker before the early return taken when the background writer is still waiting: reloadAllAndRebuildLists() removes the marker without stopping that writer, so a following DDL would otherwise commit its entity file with no marker on disk. Extends the integration test with a recovery case (a fresh .sql absent from a stale .list is recovered on restart only when the marker is present, proving the marker is the load-bearing signal) and a marker-fsync case (a DDL syncs the marker file and directory on top of the entity write, honoring fsync_metadata). Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Confirmed and fixed in e10e707.
Added an integration recovery test for the "fresh One residual power-loss window remains and is out of scope for this change: the background list-writer runs without a query context, so it uses the global |
Internal second-model review - adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; two rounds: 1 full pass + 1 delta recheck).
Severity: # blocker / warn major / . nit. DEFERRED findings are recorded for a follow-up. Findings on hunks unchanged by the fix round are auto-dropped. Session id: cron:clickhouse-maint-slot-8:20260722-113200 |
scheduleWriteLists() writes the fsync'd <id>.sql, records need_rebuild_lists.mark, and defers the .list index rewrite to a background thread (1-min timer) or shutdown. That deferred writeLists() runs with no query context, so it re-resolved fsync_metadata to the GLOBAL value and silently dropped a session fsync_metadata=1: with global fsync_metadata=0 the background thread rewrote the .list unsynced and then removed the durable marker, leaving a power-loss window where the marker removal persists but the .list content does not. On restart the stale .list is trusted and the acknowledged CREATE/ALTER/DROP USER disappears even though its <id>.sql survived. Carry the strongest pending fsync requirement from scheduleWriteLists (pending_lists_fsync, OR-accumulated) into writeLists so the deferred .list write is as durable as the most-durable pending change, resetting it once the batch is written. The direction is safe: it can only fsync more, never less. Regression test: a session fsync_metadata=1 CREATE USER followed by a session fsync_metadata=0 SYSTEM RELOAD USERS must still fsync the .list (proving the carry). Verified both directions with distinct Build IDs (fails pre-fix, passes with fix).
Internal second-model review log (Gate B, round 3 delta) — click to expandIndependent code review of the round-2 delta (base
Both-directions verified with distinct Build IDs; full suite 5/5. |
A marker-driven rebuild (reloadAllAndRebuildLists on startup, or after a latched failed_to_write_lists) has no query context and pending_lists_fsync does not survive a restart, so writeLists() fell back to the global fsync_metadata. With global fsync_metadata=0 that rewrote the .list files unsynced and then removed the durable marker, re-opening the power-loss window across a restart: a second power loss left stale .list files with no marker and the acknowledged access change disappeared even though its fsync'd <uuid>.sql survived. The need_rebuild_lists.mark file's presence is the persistent, restart- surviving encoding of the obligation to durably materialize the .list from the .sql files, so force the fsync whenever the marker is present at rebuild entry. This also covers the latched failed_to_write_lists path, whose eventual rebuild runs with the marker still on disk. Adds test_marker_driven_rebuild_forces_list_fsync: a marker-present rebuild at fsync_metadata=0 must fsync the .list (FileSync>=1), while a no-marker rebuild at fsync_metadata=0 must not (targeted, not a blanket fsync). Co-Authored-By: Claude Opus 4.8 <[email protected]>
Internal review log (Gate B, round 3 — cross-restart marker fsync; click to expand)Second-model review (engine=codex) of the delta over base
No blockers or majors. The fsync-forcing logic was not contested. |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 65/71 (91.55%) · Uncovered code |
CI finish ledger — 1a0fd09CI fully finished (Finish Workflow = pass; 0 checks queued/in_progress; >20 min ingest buffer). This PR's own diff is Note: the
Session id: cron:our-pr-ci-monitor:20260722-233000 |
|
Fixing PR for the The failure is the plain-rewritable |
…diskaccessstorage-fsync Resolve the conflict with ClickHouse#125058, which moved the creation of `need_rebuild_lists.mark` to the start of `scheduleWriteLists` and added one more in `reload`. Both now go through `createNeedRebuildListsMark`, which syncs the mark and its directory when `fsync_metadata` is set. `reload` now creates the mark itself before `reloadAllAndRebuildLists`, so the check "a mark already exists, so its lists are owed durably" moves out of `reloadAllAndRebuildLists` to before that creation in `reload`, and to the constructor. Otherwise every `SYSTEM RELOAD USERS` would sync the lists even with `fsync_metadata = 0`. `scheduleWriteLists` now accumulates `pending_lists_fsync` before the `failed_to_write_lists` early return, like the mark creation, so a change made after a failed list write keeps its requirement too. Also trim the comments added by this PR. Co-Authored-By: Claude Opus 5.5 <[email protected]>
`LocalDirectorySyncGuard`'s destructor only logs a failed `fdatasync`, so call `sync` explicitly. A DDL whose rename, unlink or `need_rebuild_lists.mark` cannot be made durable now fails instead of being acknowledged. `need_rebuild_lists.mark` is synced too, so its syncs satisfied some of the old lower bounds by themselves. `DROP USER` now needs two directory syncs, `SYSTEM RELOAD USERS` one file sync per `.list` file, and the deferred-write test removes the mark so that only the requirement kept in memory can force the sync. Each assertion fails with the mechanism it covers removed. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The directory was opened for `fdatasync` only after the rename, unlink or mark creation. `LocalDirectorySyncGuard` warns that a close-reopen-fsync sequence is kernel dependent, so hold the directory fd across the change and sync that same fd, as `KeeperSnapshotManager` does. Co-Authored-By: Claude Opus 5.5 <[email protected]>
A failed directory fdatasync after the rename or unlink was thrown, but the change was already on disk while memory kept the old entity, so the failed DDL was applied later by `SYSTEM RELOAD USERS` or a restart. Moving the sync after the in-memory update is not enough either: the access storage callers take an exception as "nothing has changed" (the rollback of `MOVE`, the dependency cleanup of `remove`, the notifications in `AccessControl`). The directory is still opened before the change and synced after it, but a failure there is only logged by the `LocalDirectorySyncGuard` destructor, as for the other users of that guard. A failed sync of `need_rebuild_lists.mark` is still thrown: it happens before any entity file changes. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
@vitlibar one design choice for you to decide here: when (a) succeed, with the error logged, as it does now (like the other I tried (b). Failing before the in-memory update lets |
…write A GRANT, REVOKE or password change keeps the entity name, so updateNoLock does not call scheduleWriteLists and never raised pending_lists_fsync. With the global fsync_metadata=0, a CREATE USER at session fsync_metadata=0 followed by a GRANT at session fsync_metadata=1 left the deferred writer rewriting users.list without fsync and then removing need_rebuild_lists.mark; a power loss could keep the mark removal and lose the list write, so restart trusted the stale index and the user with its acknowledged grant became unreachable. Raise pending_lists_fsync from such an update while a list write is pending: that write removes the mark which also guards the updated file. Measured with strace on the real background writer (global fsync_metadata=0, CREATE@0 then GRANT@1): before, users.list is truncated and rewritten with no fdatasync, then the mark is unlinked; after, fdatasync(users.list) precedes the unlink. Co-Authored-By: Claude Opus 5.5 <[email protected]>
A rename-free update (GRANT, REVOKE, password change) did not schedule a .list rewrite, so at fsync_metadata=1 it synced only `<id>.sql`. The .list files on disk can still be unsynced: their rewrite may be pending (the mark was created at fsync_metadata=0, not synced), or already done by a writer or a clean shutdown running at a global fsync_metadata=0. A power loss could then leave the synced `<id>.sql` next to a stale .list and no mark, so the user and the acknowledged change were unreachable after restart. Such an update now goes through scheduleWriteLists like a rename: the synced mark is created before the change and removed only after a synced .list rewrite. This replaces the narrower carry of the previous commit, which covered only a pending rewrite and left the mark itself unsynced. Cost at the default fsync_metadata=1: one more file and directory sync per update plus a batched .list rewrite; 300 GRANTs took 0.64-0.94 s before and 0.74-0.83 s after on local NVMe. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Closes: #68958
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Access entities stored on local disk (users, roles, grants, quotas, row policies, settings profiles created via SQL) are now fsync'd when
fsync_metadatais enabled, so an acknowledgedCREATE/ALTER/DROP USER(andGRANT/REVOKE) is no longer silently lost on power loss. PreviouslyDiskAccessStoragewrote and removed the.sqlentity files without any fsync.Description
On 26.9 (and every earlier release), an acknowledged
CREATE USER+GRANTon the defaultlocal_directoryaccess storage can vanish after a power loss: neither the<uuid>.sqlfile nor its rename is forced to disk, and the user then fails withAUTHENTICATION_FAILED(reproduced 3/3 withdm-flakeyin #68958).DiskAccessStoragewas the only on-disk DDL-entity store ignoringfsync_metadata.Fix, gated on
fsync_metadata(defaulttrue; the session value on the query thread, the global one otherwise):writeEntityFilefsyncs the file, renames it, then fsyncs the directory;deleteAccessEntityOnDiskfsyncs the directory after the unlink. The directory is opened before each change. A failed directory fsync after the change is only logged, as for the otherLocalDirectorySyncGuardusers: the access storages and their callers take an exception as "nothing has changed".need_rebuild_lists.mark, which makes the next start rescan the.sqlfiles instead of trusting the.listindex, is fsynced with its directory, andwriteListFilefsyncs the.listfiles..listfiles are rewritten later by a background thread that has no query context, sopending_lists_fsynccarries to it the strongest requirement of the changes made meanwhile, and a rebuild that finds a mark left by earlier changes always fsyncs the lists before removing the mark. As the.listfiles on disk may be unsynced, aGRANTor password change atfsync_metadata1 also goes through the mark.tests/integration/test_access_storage_fsyncchecks theFileSync/DirectorySyncProfileEvents of each statement withfsync_metadata1 and 0, the session requirement carried to the deferred.listwrite, the mark-driven rebuild, and that the mark is what makes a stale.listrecoverable on restart. Each assertion fails with the mechanism it covers removed. It is an integration test because stateless tests use the<memory/>access storage.Workflow [PR]
Sync PR [sync-upstream/pr/111346]
Version info
26.11.1.965-master(included in26.11and later)