Repository navigation
fix(#6259): a committer waits for flush-queue room before the page-manager lock, not inside it - #6269
Conversation
…nager lock, not inside it publishPages holds the JVM-wide page-manager lock across BOTH halves of publication - the synchronous page write and the scheduleFlushOfPages enqueue - and it has to: the snapshot t0 barrier (#6075/#6125) takes that same lock precisely so no committer can put a page on disk OR into the flush pipeline from there on. The enqueue blocked, though: while (running) { if (queue.offer(new PagesToFlush(pages), 1, SECONDS)) return; } So whenever the bounded flush queue filled - a write burst, a slow or contended volume, an fsync spike, no suspension or backup involved - the committing thread parked in that offer holding a lock every committer in the process needs, and one database's burst serialized the commits of every unrelated database in the JVM, including ones on idle volumes whose pages could have been written immediately. The queue is meant to backpressure the writers it belongs to; what was wrong is only that the wait was charged to everybody. So the wait moves ahead of the lock, the shape #6200 established for the deferred-RAM cap: a committer reserves its queue slot before lock() and reaches the enqueue with the slot already its own, so the offer inside the lock cannot block. The enqueue itself does not move, so the barrier's convergence argument is untouched. Admission is derived from the queue's OWN size plus a reservation counter (size + reservations < capacity) rather than from a Semaphore mirroring the queue: a permit count has to be kept in step by hand at every enqueue and every poll, and one missed release silently shrinks the pipeline for the life of the process, while reading the queue tells the truth by construction - a batch that reaches it by any other route narrows admission instead of corrupting the bound. The flush thread signals a free slot the moment it POLLS a batch, before writing it, so a slow disk delays the writes and not the admission of the next committer. The resume's re-enqueue of deferred batches and the shutdown sentinel go through the same admission: a resume can carry thousands of batches, and without a reservation it could fill the queue under a committer holding one, putting that committer's offer back inside the lock. Also adds flushQueueWaits to the page-manager stats and the Profiler. Queue length is an instant, sampled between two bursts as easily as during one; this is cumulative evidence that commits were actually held. The regression test pins the invariant rather than the symptom - while a committer waits for queue room, another thread must still be able to take the page-manager lock - and was verified red against the previous shape. Full engine suite green (12090 tests). 40000 commits took 629-656 ms with the admission step and 633-652 ms without.
|
Tick the box to add this pull request to the merge queue (same as
|
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 30 |
🟢 Coverage 87.07% diff coverage · -6.53% coverage variation
Metric Results Coverage variation ✅ -6.53% coverage variation Diff coverage ✅ 87.07% diff coverage Coverage variation details
Coverable lines Covered lines Coverage Common ancestor commit (4359848) 158758 125225 78.88% Head commit (0b7d89a) 191199 (+32441) 138321 (+13096) 72.34% (-6.53%) 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 (#6269) 116 101 87.07% 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: fix(#6259) - committer waits for flush-queue room before the page-manager lock, not inside it I traced the concurrency invariants by hand (the Correctness The wait/notify pattern in One thing worth double-checking against CI rather than my read of the code: Design Minor nits (non-blocking)
Test coverage Performance / Security Overall: a well-reasoned, well-tested fix for a real cross-database stall. Nothing here blocks merge from my read. |
Follow-up to the code review of #6269. Nothing it raised was blocking; these are the four points it made, applied. - A concurrent-stress test for the CAS loop of tryReserveQueueSlot, which the deterministic tests could not reach. Phase 1 races 12 threads on reservations alone, so the assertion is exact - with nothing enqueued the bound IS the number of reservations held at once - and it also asserts the race actually contended, or it would prove nothing. Phase 2 then runs the whole path (reserve, enqueue, drain) concurrently so the two halves of the sum move against each other, and asserts the accounting returns to zero. Verified red against a naive time-of-check/time-of-use version of the loop: it fails in 0.2 s, which is the regression a future refactor of this class would make. - A note on why closeAndJoin returns immediately in noPathLeavesAReservationBehind: the flush thread there was constructed but never started, and join() on a NEW thread returns at once. What that assertion is about is the shutdown flag, not the join. - The two "insurance" release paths now say what they cover going FORWARD rather than only that they are unreachable today: any statement a later refactor inserts between the reservation and the hand-off. The one in writePagesNoBackpressure is in fact reachable today - putPageInReadCache does I/O - and now says so. - Column alignment of the two new constants.
ReviewWent through PageManager.java, PageManagerFlushThread.java, Profiler.java, and the new Issue6259FlushQueueAdmissionTest in detail, tracing the reservation lifecycle through every exit path. Correctness of the core mechanism The admission control is the interesting part, and it holds up:
No correctness bug found in the mechanism itself. It is a genuinely tricky bit of lock-free bookkeeping and the invariant reasoning in the comments matches what the code actually does, which is not always true of comments this size. Minor / non-blocking observations
Verification Was not able to run mvn in this review sandbox (tool execution required approval that was not available in this context), so verification here is static: reading the full diff plus the invariant reasoning above rather than executing the test suite. The PR description states the full engine suite (12090 tests) plus the targeted #6125/#6200 regression suites pass, and the new test theAdmissionBoundHoldsUnderContention specifically stress-tests the CAS bound under 12-way contention, which is the right test for the trickiest part of this change. That CI run (and a fresh one on the final diff) should be the actual gate before merge. Summary Solid, well-scoped fix for a real production issue (JVM-wide commit stalls caused by one database write burst). The design choice to derive admission from the queue own size rather than a hand-maintained semaphore is the right call, for exactly the reason given: no silent permanent leak from a missed release. No blocking issues found. |
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #6269 +/- ##
==========================================
+ Coverage 70.11% 70.15% +0.04%
==========================================
Files 1838 1838
Lines 158758 158846 +88
Branches 33489 33511 +22
==========================================
+ Hits 111318 111444 +126
+ Misses 33572 33537 -35
+ Partials 13868 13865 -3 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Fixes #6259.
The bug
PageManager.publishPagesholds the JVM-wide page-manager lock across both halves of publication - the synchronous page write and thescheduleFlushOfPagesenqueue - and it has to: the snapshot t0 barrier (#6075/#6125) takes that same lock precisely so that from there on no committer can put a page on disk or into the flush pipeline, which is what makes its residual drain converge by construction.The enqueue blocked, though:
So whenever the bounded flush queue (
arcadedb.pageFlushQueue, default 512, 8 under thelow-ramprofile) filled, the committing thread parked in that offer holding a lock every committer in the process needs. The queue fills whenever commits outrun the disk for a moment: a write burst, a slow or contended volume, an fsync spike, a compaction landing at the wrong time. No suspension, no backup, no HA involved - this is the non-suspension twin of #6200, which removed one trigger for this stall and left the other in place.The result was that one database's write burst serialized the commits of every unrelated database in the JVM, including databases on a different, idle volume whose pages could have been written immediately.
The fix
The bounded queue is meant to backpressure the writers it belongs to; that part is right. What was wrong is only that the wait was served while holding a JVM-wide lock, so it was charged to everybody. The enqueue cannot move out of the lock without reintroducing the gap #6125 removed - so the wait moves ahead of it, exactly the shape #6200 established for the deferred-RAM cap ("the ceiling is global because heap is; the response must not be").
A committer now reserves its flush-queue slot before
lock()and reaches the enqueue one line later with the slot already its own, so theofferinside the lock cannot block. The blocking offer stays as the safety net it always was.Deliberately not a semaphore
The issue's own proposal suggested expressing the wait as "a permit/semaphore sized to the queue". This does not do that, and the difference is the point.
A permit count has to be kept in step with the queue by hand at every enqueue and every poll, and a single missed release silently shrinks the pipeline for the life of the process - an invisible, permanent bug with no symptom anyone would trace back. Admission here is derived from the queue's own size plus a counter of slots promised but not yet used:
so
size + reservations <= capacityholds by construction, the holder of a reservation is guaranteed a free slot, and a batch that reaches the queue by any other route - a white-box test, a future call site - narrows admission instead of corrupting the bound. The cost is oneArrayBlockingQueue.size()per publication, which takes the same internal lock theofferon the next line takes anyway (andpoll(timeout)releases that lock while it waits, so it is never held against a publication).Signalled, not polled
The flush thread wakes a waiting committer the moment it polls a batch - before writing it, because the slot is free from that point, and a slow write should delay the writes rather than the admission of the next committer. When nobody is waiting (the normal state of a queue that is not full) the signal costs one volatile read per batch, the same shape as the drain signal of #6199.
Two paths the issue did not mention
Both would have reintroduced the bug in a narrower window, so both go through the same admission:
runningis false and the queue is drained.Observability
Adds
flushQueueWaitsto the page-manager stats and to both Profiler outputs.pageFlushQueueLengthis an instant, sampled between two bursts as easily as during one; this is cumulative evidence that commits were actually held. A committer held for more than 10 s also says so once, at WARNING, naming the queue and the configuration key.Verification
Issue6259FlushQueueAdmissionTest, 4 tests. The central one pins the invariant rather than the symptom, as the issue asked: while a committer is waiting for queue room, another thread must be able to take the page-manager lock. The flush thread is wedged on the batch monitor its write loop takes - a real code path, stopping the pipeline exactly where a stalled disk would - and always released on a deadline of its own, since a flush thread left wedged would hang every other test sharing the JVM.That test was verified red against the previous shape: with the reservation removed it fails at exactly that assertion, having waited out the full 60 s for a lock the committer was holding while parked in
offer.The other three cover the mechanism: exactly
arcadedb.pageFlushQueuecallers are admitted and the next one waits; a reserved slot cannot be taken by another committer; and no path out of a publication leaves a reservation behind (a leak is silent and permanent, so it is asserted rather than inspected, with anassertguarding the fail-open direction).enginesuite green: 12090 tests, 0 failures, 0 errors.Issue6125SnapshotBarrierAndCapTestgreen - it is what proves the publication lock still covers the enqueue.Issue6200PerDatabaseDeferredBacklogTestand the whole flush-suspend suite green.Follow-up left open
Making the flush queue per-database remains worth measuring: it would bound each database's backlog independently and remove the cross-database coupling at its source. It does not fix this bug on its own - a committer that blocks on its OWN full queue while holding the JVM-wide lock still stalls everyone - so it would have to be combined with the admission control in this PR regardless, which is why it is not bundled here.