Repository navigation
fix(#4926,#4934): torn-write repair on recovery; fsync-gated WAL dele… - #5052
Conversation
…tion on close #4926 (HIGH, silent corruption): a 64KB page flush spans many disk sectors and the MVCC version header lives in sector 0. A torn write at power loss can persist the NEW version header while the delta sectors still hold the PREVIOUS content. Recovery's <= skip declared such a page 'already applied' and left it silently corrupt (new version, old bytes) despite an intact, fully-fsynced WAL - undetectable, since every later MVCC check accepts the page. The skip is now strictly <, and the equal-version case RE-APPLIES the WAL delta: idempotent when the page is intact (the delta carries absolute bytes for exactly that version), repairing when it is torn. Skipping STRICTLY older entries remains, as their delta would overwrite newer content. Red-first verified with a forged torn write (version header says v2, content still v1): the old code left the stale value behind forever, the fix recovers the committed one. #4934 (MEDIUM, unrecoverable committed data): a clean close deleted every WAL file even when the preceding data fsync FAILED - and after a failed fsync the OS may have dropped the dirty pages (fsyncgate semantics), so the WAL held the only durable copy. FileManager.syncFiles() now reports failure; a failed pre-close fsync makes the close crash-equivalent (WAL and lock file preserved via the #5012 machinery, recovery on next open), and the runtime WAL-rotation drop pass aborts when its pre-drop fsync fails, retrying on the next pass. Red-first verified: the old code deleted the WAL despite the failed fsync. Verified with the full commit-path sweep (union with the companion point-of-no-return branch): 372 classes, 2851 tests, 0 failures.
|
Tick the box to add this pull request to the merge queue (same as
|
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 0 |
🟢 Coverage 90.00% diff coverage · -7.22% coverage variation
Metric Results Coverage variation ✅ -7.22% coverage variation Diff coverage ✅ 90.00% diff coverage Coverage variation details
Coverable lines Covered lines Coverage Common ancestor commit (cd0a3cc) 134936 100437 74.43% Head commit (a9117af) 166742 (+31806) 112073 (+11636) 67.21% (-7.22%) 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 (#5052) 10 9 90.00% 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.
There was a problem hiding this comment.
Code Review
This pull request improves WAL and recovery correctness by repairing torn page writes (re-applying WAL deltas when the log version equals the on-disk page version) and preserving the WAL and lock files as crash-equivalent if a data fsync fails during database close or WAL rotation. Additionally, comprehensive unit tests have been added to verify these behaviors. The review feedback suggests adhering to the Google Java Style Guide by adding braces to single-statement conditional blocks in LocalDatabase.java and TransactionManager.java.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| if (!drop && !fileManager.syncFiles()) | ||
| // #4934: the fsync failed, so the OS may have dropped the dirty pages (fsyncgate semantics) - the | ||
| // pages the wait above considered flushed may never reach the platter. Deleting the WAL below would | ||
| // then make the committed data unrecoverable after a power loss, with only a log line as evidence. | ||
| // Treat the close as crash-equivalent instead: preserve WAL + lock file, recover on next open. | ||
| dataSafeOnDisk = false; |
There was a problem hiding this comment.
According to the Google Java Style Guide (Section 4.1.1), braces {} are required for all if statements, even when the body contains only a single statement or a multi-line comment. Adding braces improves readability and prevents potential bugs during future modifications.
| if (!drop && !fileManager.syncFiles()) | |
| // #4934: the fsync failed, so the OS may have dropped the dirty pages (fsyncgate semantics) - the | |
| // pages the wait above considered flushed may never reach the platter. Deleting the WAL below would | |
| // then make the committed data unrecoverable after a power loss, with only a log line as evidence. | |
| // Treat the close as crash-equivalent instead: preserve WAL + lock file, recover on next open. | |
| dataSafeOnDisk = false; | |
| if (!drop && !fileManager.syncFiles()) { | |
| // #4934: the fsync failed, so the OS may have dropped the dirty pages (fsyncgate semantics) - the | |
| // pages the wait above considered flushed may never reach the platter. Deleting the WAL below would | |
| // then make the committed data unrecoverable after a power loss, with only a log line as evidence. | |
| // Treat the close as crash-equivalent instead: preserve WAL + lock file, recover on next open. | |
| dataSafeOnDisk = false; | |
| } |
References
- Google Java Style Guide Section 4.1.1: Braces are used with if, else, for, do and while statements, even when the body is empty or contains only a single statement. (link)
| if (!database.getFileManager().syncFiles()) | ||
| // #4934: the fsync failed - the data this WAL protects may never reach the disk. Dropping | ||
| // the WAL now would make it unrecoverable; abort this pass, the rotation retries later. | ||
| return false; |
There was a problem hiding this comment.
According to the Google Java Style Guide (Section 4.1.1), braces {} are required for all if statements, even when the body contains only a single statement or a multi-line comment. Adding braces improves readability and prevents potential bugs during future modifications.
| if (!database.getFileManager().syncFiles()) | |
| // #4934: the fsync failed - the data this WAL protects may never reach the disk. Dropping | |
| // the WAL now would make it unrecoverable; abort this pass, the rotation retries later. | |
| return false; | |
| if (!database.getFileManager().syncFiles()) { | |
| // #4934: the fsync failed - the data this WAL protects may never reach the disk. Dropping | |
| // the WAL now would make it unrecoverable; abort this pass, the rotation retries later. | |
| return false; | |
| } |
References
- Google Java Style Guide Section 4.1.1: Braces are used with if, else, for, do and while statements, even when the body is empty or contains only a single statement. (link)
Review: torn-write repair on recovery; fsync-gated WAL deletion (#4926, #4934)Overall this is a careful, well-reasoned durability fix. The comments are excellent (they explain the why, the failure mode, and the fsyncgate semantics), the ordering of the lazy fsync-before-drop is correct, and both fixes are backed by red-first regression tests. Nice work. A few observations below, mostly one substantive limitation to confirm and some minor nits. Correctness / design1. Torn-write repair is partial when a page accumulated multiple unflushed versions (please confirm this is acceptable).
The strictly- 2. Idempotency of re-apply side effects - verified OK, flagging for the record. Re-applying an equal-version entry now runs the full apply path, including cleanWALFiles return-on-fsync-failure - looks correct
LocalDatabase.closeInternal - behavior preservedThe refactor to Tests
None of these are blockers. The main ask is confirming the scope of the partial-repair limitation (point 1). |
…test hardening Claude's main point was correct: the equal-version re-apply repairs only the NEWEST coalesced version's delta region. Async flush can merge v2/v3/v4 into one physical page write, so if that write tears, the v2/v3 regions (skipped as strictly-older) can stay torn while only v4 is repaired. The version header alone cannot distinguish a torn page from an intact one, so the complete fix needs per-page checksums (the longer-term direction #4926 itself notes) - too large and too risky to fold into this targeted PR on the engine's most delicate path. Documented as a known limitation in the recovery code comment and the release notes, and filed as follow-up #5054. This fix still strictly improves on the old <= skip (which repaired nothing) and fully covers the single-flush case. Gemini (braces, Section 4.1.1): added braces to the two #4934 ifs where a multi-line comment sits between the condition and its single statement - exactly the readability trap braces prevent. CLAUDE.md permits braces on single-statement ifs (it only does not require them), so this honors both. Test nits (Claude): imported the inline FQNs (Field, FileChannel), asserted setSuspended(...) actually engaged so the 'data never reaches disk' precondition cannot silently fail, and removed the trailing double blank line. Runtime is 0.34s for both tests, so no @tag(slow). Idempotency of the equal-version re-apply (updatePageCount, applyReplicatedPageUpdate) was independently confirmed by the reviewer. Both regression tests still green.
Review: fix(#4926,#4934) - torn-write repair on recovery; fsync-gated WAL deleteOverall this is a careful, well-reasoned durability fix. The reasoning in the code comments is excellent, the two changes are logically independent and both correct, and each ships a red-first regression test. A few observations below, none blocking. Correctness - looks solid
Performance considerations
Known limitation - honestly documentedThe multi-version accumulation gap (v1 on disk, coalesced v2->v3->v4 flush tears, only v4 region repaired) is clearly called out and tracked in #5054. Good to see it stated rather than hidden. Nit: the comment block at Test coverage
Minor
Nice work - the fix is correct, minimal, and the comments make the durability reasoning auditable. My only real asks are the comment reflow and a test for the rotation-abort branch. |
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #5052 +/- ##
============================================
- Coverage 65.45% 65.44% -0.01%
+ Complexity 839 838 -1
============================================
Files 1687 1687
Lines 134936 134939 +3
Branches 28857 28859 +2
============================================
- Hits 88319 88312 -7
- Misses 34536 34546 +10
Partials 12081 12081 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
…test hardening Claude's main point was correct: the equal-version re-apply repairs only the NEWEST coalesced version's delta region. Async flush can merge v2/v3/v4 into one physical page write, so if that write tears, the v2/v3 regions (skipped as strictly-older) can stay torn while only v4 is repaired. The version header alone cannot distinguish a torn page from an intact one, so the complete fix needs per-page checksums (the longer-term direction #4926 itself notes) - too large and too risky to fold into this targeted PR on the engine's most delicate path. Documented as a known limitation in the recovery code comment and the release notes, and filed as follow-up #5054. This fix still strictly improves on the old <= skip (which repaired nothing) and fully covers the single-flush case. Gemini (braces, Section 4.1.1): added braces to the two #4934 ifs where a multi-line comment sits between the condition and its single statement - exactly the readability trap braces prevent. CLAUDE.md permits braces on single-statement ifs (it only does not require them), so this honors both. Test nits (Claude): imported the inline FQNs (Field, FileChannel), asserted setSuspended(...) actually engaged so the 'data never reaches disk' precondition cannot silently fail, and removed the trailing double blank line. Runtime is 0.34s for both tests, so no @tag(slow). Idempotency of the equal-version re-apply (updatePageCount, applyReplicatedPageUpdate) was independently confirmed by the reviewer. Both regression tests still green. (cherry picked from commit a9117af)
…tion on close
#4926 (HIGH, silent corruption): a 64KB page flush spans many disk sectors and the MVCC version header lives in sector 0. A torn write at power loss can persist the NEW version header while the delta sectors still hold the PREVIOUS content. Recovery's <= skip declared such a page 'already applied' and left it silently corrupt (new version, old bytes) despite an intact, fully-fsynced WAL - undetectable, since every later MVCC check accepts the page. The skip is now strictly <, and the equal-version case RE-APPLIES the WAL delta: idempotent when the page is intact (the delta carries absolute bytes for exactly that version), repairing when it is torn. Skipping STRICTLY older entries remains, as their delta would overwrite newer content. Red-first verified with a forged torn write (version header says v2, content still v1): the old code left the stale value behind forever, the fix recovers the committed one.
#4934 (MEDIUM, unrecoverable committed data): a clean close deleted every WAL file even when the preceding data fsync FAILED - and after a failed fsync the OS may have dropped the dirty pages (fsyncgate semantics), so the WAL held the only durable copy. FileManager.syncFiles() now reports failure; a failed pre-close fsync makes the close crash-equivalent (WAL and lock file preserved via the #5012 machinery, recovery on next open), and the runtime WAL-rotation drop pass aborts when its pre-drop fsync fails, retrying on the next pass. Red-first verified: the old code deleted the WAL despite the failed fsync.
Verified with the full commit-path sweep (union with the companion point-of-no-return branch): 372 classes, 2851 tests, 0 failures.
What does this PR do?
A brief description of the change being made with this pull request.
Motivation
What inspired you to submit this pull request?
Related issues
A list of issues either fixed, containing architectural discussions, otherwise relevant
for this Pull Request.
Additional Notes
Anything else we should know when reviewing?
Checklist
mvn clean packagecommand