Repository navigation
fix(#8626): a sync forces only the data files written since their last fsync - #8630
Conversation
…t fsync A clean close ran force(true) on every data file of the database before deleting the WAL, whether or not anything had been written to it. Each call still costs a device cache flush, about 0.5 ms per bucket or index file on an NVMe drive, so a read-only open + close of a database with 100 indexed types spent about 100 ms persisting nothing. - PaginatedComponentFile tracks what it owes the disk since its last successful fsync: nothing, data (a page write) or metadata (created, renamed, or unknown after a crash). A writer raises the state after its bytes reached the channel; a sync claims it before forcing and gives it back when the force fails, so no write can slip between them. - FileManager.syncFiles() (clean close, WAL rotation) forces only the files that owe something: fdatasync for page writes, full fsync plus one fsync per directory for created or renamed files. A read-only session forces nothing. - Recovery marks every file unsynced (what reached the disk before the crash is unknown) and now forces the data files before deleting the WAL it replayed. It used to delete it right away, so a power loss before the next clean close lost the recovered pages. If that fsync fails the WAL is kept in the inactive pool, as #4934 does on close. - Restore fsyncs every extracted file and the database directory before reporting success: a database opened on it no longer gets every file forced by its first clean close.
|
Warning Review limit reachedNext included review available in 23 minutes. View limit detailsLimit details: You’ve used all 4 included reviews currently available. You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. Review configuration: ⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Advanced Run ID: 📒 Files selected for processing (9)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
This pull request does not currently match the merge queue conditions, so it cannot be queued from here. The box comes back if it matches again. |
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 22 |
🟢 Coverage 97.26% diff coverage · -6.06% coverage variation
Metric Results Coverage variation ✅ -6.06% coverage variation Diff coverage ✅ 97.26% diff coverage Coverage variation details
Coverable lines Covered lines Coverage Common ancestor commit (de966e7) 197713 166254 84.09% Head commit (d59659d) 230342 (+32629) 179740 (+13486) 78.03% (-6.06%) Coverage variation is the difference between the coverage for the head and common ancestor commits of the pull request branch:
<coverage of head commit> - <coverage of common ancestor commit>Diff coverage details
Coverable lines Covered lines Diff coverage Pull request (#8630) 73 71 97.26% Diff coverage is the percentage of lines that are covered by tests out of the coverable lines that the pull request added or modified:
<covered lines added or modified>/<coverable lines added or modified> * 100%
NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.
|
Review of #8630 (skip fsync of untouched data files on close / WAL rotation) Overall this is a well-reasoned change. The three-state per-file flag, the write-then-raise / claim-then-force ordering, giving the state back on a failed force (#4934), and the recovery fix (fsync before dropping the replayed WAL) all look correct to me. Tests cover the state machine, the read-only close, and the failing-fsync recovery path, with mutation checks noted in the description. I found nothing blocking. Points below are in rough priority order. 1. Concurrent Claiming the state (swap to clean) before forcing means a second caller that arrives while the first is mid- Concretely: the rotation timer thread ( It is probably only reachable if the rotation timer is not stopped or drained before the close path syncs. The description says callers wait for the page writes to complete, but not that the syncs themselves are serialized. Either of these would close the gap:
2. No test for the concurrency protocol (medium/low) The javadoc on 3. Directory fsync is best-effort but
4. Smaller notes
Style / CLAUDE.md Conventions look followed: imports rather than FQNs, Verdict Good change with a sensible durability improvement bundled in (recovery and restore). I would address point 1 (or document why it cannot happen) and add the stress test from point 2 before merging; the rest is optional. |
|
Review points addressed in #8631: |
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #8630 +/- ##
============================================
- Coverage 75.76% 75.75% -0.01%
+ Complexity 3421 3417 -4
============================================
Files 2055 2055
Lines 197713 197772 +59
Branches 41603 41622 +19
============================================
+ Hits 149789 149817 +28
- Misses 31501 31534 +33
+ Partials 16423 16421 -2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
…a known regression (#89) (#90) KNOWN_REGRESSIONS becomes a list per situation, and vector gains the October pin (417314c): only the embedded arm's clean close grows with what is stored (5.4, 12.5, 88, and 1,400 ms at 10k to 10M; the server's stays near 5 ms), because the lane reads the index statistics before close (BUGS F159, disclosed on the page as an upper bound) and at this pin a clean close fsyncs every data file it holds (ArcadeData#8626, fixed by ArcadeData#8630 after the pin). On the merged October rows the gate now prints vector/lc10m as KNOWN and reports 0 failures. The entry comes out at the re-pin. Claude-Session: https://claude.ai/code/session_01Lj5isEaoaGCeaZe5nNoWX4 Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
Closes #8626
Problem
LocalDatabase.close()callsFileManager.syncFiles(), which ranforce(true)on every data file before deleting the WAL, whether or not anything had been written to it. Each call still costs a device cache flush, about 0.5 ms per bucket or index file on an NVMe drive. So a read-only open + close of a database with 100 indexed types took about 100 ms.Fix
The issue proposes a per-file dirty flag or skipping
syncFiles()when no transaction committed. This PR uses a per-file flag with three states, which answers both questions in the issue:PaginatedComponentFiletracks what it owes the disk since its last successful fsync:SYNC_CLEAN,SYNC_DATA(a page write) orSYNC_METADATA(created, renamed, or unknown after a crash). The protocol has no gap for a concurrent write. A writer raises the state after its bytes reach the channel. A sync claims the state (swaps it to clean) before it forces, and gives it back if the force fails ([storage] Clean close deletes all WAL files even when the preceding data fsync failed #4934), so the next pass retries.syncFiles()forces only the files that owe something. Page writes getfdatasync(force(false)), which also persists the file-size change of an append. Created or renamed files get a fullfsyncplus one fsync per directory, so their directory entry survives a power loss too. The old code never guaranteed that. The file-level check is better than a database-level "no commit since open" check: a session that writes to 1 file out of 200 syncs only that file, on close and on every WAL rotation.Nothing else writes to these files: every page write goes through
PaginatedComponentFile.write(), including recovery, HA apply and compaction.SnapshotInstalleralready fsyncs what it installs.Measurements
Clean close p50 after a read-only open, the issue's scenario, one NOTUNIQUE LSM index per type, macOS/APFS:
An fsync on a clean file costs little on APFS. The saving is much larger on Linux NVMe (the issue measured 100 ms at 100 types) and on cloud block storage.
Tests
PaginatedComponentFileSyncStateTest: state transitions (create, write, reopen, read, rename, recovery mark, unconditional force), and a failed fsync that leaves the file owing the sync.Issue8626CleanCloseFsyncTest: a read-only session close forces 0 files. A write session close forces exactly the files it wrote. Recovery leaves every file synced.Issue8626RecoveryFsyncFailureTest: a recovery whose fsync fails keeps the replayed WAL, and the following close preserves WAL + lock.syncFiles()the reproducer fails. When recovery drops the WAL without an fsync, both recovery tests fail.engine,database,index.lsmpackages), 122 integration restore/backup tests, all green.