Repository navigation
fix(#8626): serialize syncFiles() so a concurrent sync cannot vouch for an fsync still in flight - #8631
Conversation
…or an fsync still in flight A sync claims a file's pending state before it forces it. A second syncFiles() running at the same time - a clean close racing the WAL rotation timer, which the close only stops after its own sync - found the claimed file clean and returned true while the first fsync was still in flight. When that fsync then failed, the close had already decided the data was safe and deleted the WAL. syncFiles() now runs under a dedicated lock (not the FileManager monitor, which dropFile holds across I/O). A caller that finds a file clean knows a completed sync forced it; a failed sync has given the state back, so the next caller forces the file itself and sees the failure. Tests: a deterministic claim-then-write ordering test, a concurrent syncFiles() test where the in-flight fsync fails (fails without the lock), and a writers-versus-syncer stress test. Javadoc notes that the directory fsync is best effort and that force(boolean) does not force the directory.
|
Warning Review limit reachedNext included review available in 3 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 (3)
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 | 0 |
🟢 Coverage 100.00% diff coverage · -6.05% coverage variation
Metric Results Coverage variation ✅ -6.05% coverage variation Diff coverage ✅ 100.00% diff coverage Coverage variation details
Coverable lines Covered lines Coverage Common ancestor commit (7bb0c11) 197772 166304 84.09% Head commit (8b4ee85) 230346 (+32574) 179755 (+13451) 78.04% (-6.05%) 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 (#8631) 4 4 100.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.
ReviewThe fix is small and correct. I found no blocking issues. Fix (
Tests
Nits
The implementation looks good to merge. I would just harden the stress-test assertion above. |
…start The syncer loop was a while loop checked against a flag the main thread flips once the writers are done: on a loaded runner all four writers could finish before the syncer thread ran once, leaving its forced count at 0. It is now a do-while, and the writers wait for its first pass, which always forces the created file's metadata. awaitBlocked also accepts WAITING, so the concurrent-sync test keeps working if the sync lock ever becomes a java.util.concurrent lock. syncFiles() Javadoc trimmed.
|
Review The change is small and correct for the race it targets. Serializing Code
Tests The tests are deterministic where it matters: they use the channel lock plus
Perf / security No security concerns. Performance is unchanged in the common case: with clean files the lock is uncontended and each call is one Overall this is good to merge once the |
…them spinning - The stress test now drives FileManager.syncFiles() from two syncer threads, so it exercises the sync lock as well as the claim/raise protocol, and clears the writing flag in a finally: a failed writer left the syncers looping. - The concurrent-sync test hands the second thread over through an AtomicReference; awaitBlocked documents why a parked thread is one waiting for the sync lock. - Javadoc: a closed file is skipped by syncFiles() and keeps what it owes; the public force(boolean) claims the state outside the sync lock, so it must not run concurrently with a syncFiles() whose result is relied on.
|
Review The fix looks correct and I have no blocking findings. I read the diff and the surrounding Correctness
Nits (non-blocking)
Tests
LGTM. |
…ckage-private - syncFiles() is held across fsyncs, and the engine runs work on virtual threads: a virtual thread blocked on a monitor pins its carrier thread, one parked on a ReentrantLock does not. - PaginatedComponentFile.force(boolean) claims the pending state outside the sync lock, so a production caller could reopen the race syncFiles() closes. Only tests in the package call it: now package-private. - The concurrent-sync test waits until the second caller is queued on the sync lock itself (hasQueuedThread) instead of inferring it from the thread state. It still fails without the lock.
ReviewOverall this is a small, well-reasoned fix and I have no blocking concerns. What I checked
Suggestions (non-blocking)
TestsThe three new tests target the right things:
There is no raw wall-clock assertion; timeouts are only used as hang detectors, consistent with the CLAUDE.md guidance. LGTM. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #8631 +/- ##
============================================
- Coverage 75.76% 75.76% -0.01%
- Complexity 3417 3422 +5
============================================
Files 2055 2055
Lines 197772 197776 +4
Branches 41622 41622
============================================
- Hits 149849 149839 -10
- Misses 31510 31523 +13
- Partials 16413 16414 +1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Follow-up to #8630 (issue #8626), addressing the code review on that PR.
Concurrent
syncFiles()callers#8630 made a sync claim a file's pending state (swap it to clean) before forcing it. A second
syncFiles()running at the same time found the claimed file clean and returnedtruewhile the first fsync was still in flight. This happens in practice: a clean close runs its sync while the WAL rotation timer may be inside its own, and the timer is only stopped byTransactionManager.close()after that. If the timer's fsync then failed, the close had already decided the data was safe and deleted the WAL.syncFiles()now runs under a dedicated lock. It is not theFileManagermonitor, becausedropFile()holds that one across I/O. With clean files returning immediately, the lock costs almost nothing. A caller that finds a file clean now knows a completed sync forced it. A failed sync has given the state back, so the next caller forces the file itself and sees the failure.Tests (
PaginatedComponentFileSyncStateTest)writeLandingAfterAClaimIsLeftForTheNextSync: deterministic, using the channel lock. A write that lands after a sync's claim keeps the file marked for the next sync.concurrentSyncWaitsForTheOneInFlightAndSeesItsFailure: a secondsyncFiles()blocks while the first fsync is in flight, and returnsfalsewhen that fsync fails. It fails without the lock.concurrentWritesAndSyncsLoseNeitherPagesNorPendingState: 4 writers against a syncer loop. Every page is intact and the file ends settled.Javadoc
syncFiles(): the directory fsync is best effort (as inFileUtils.forceDirectory, and a no-op on Windows).force(boolean): does not force the parent directory.Regression: 191 engine tests (WAL, recovery, fsync, close, transaction manager, ACID, #8626 suites), all green.
Not done from the review: a
Profilerline forsyncedFiles. The Profiler's monotonic stats must be carried across a database close (retainedStats), so it is more than a one-line addition, and it is diagnostic only.