Repository navigation
Description
Follow-up to #6133, which made "does this database still have pages in the flush pipeline?" an O(1) counter read. The cost of one check is now negligible, but both callers still discover the answer by sleeping and asking again, so the latency they pay is set by the poll interval rather than by when the pages actually land.
Where
1. The bulk drain, PageManagerFlushThread.waitAllPagesOfDatabaseAreFlushed (engine/src/main/java/com/arcadedb/engine/PageManagerFlushThread.java:209), polls every DEFAULT_FLUSH_WAIT_POLL_MILLIS = 10 ms (line 64/240). Every close(), rename(), index compaction, backup suspension and the snapshot barrier's step 1 goes through it, and each of them rounds up to the next 10 ms boundary even when the pipeline was already empty or emptied 9 ms ago. On a process cycling through many databases that is pure dead time.
2. The residual drain, waitPendingPagesOfDatabaseUntil (line 398), polls every 1 ms - and its caller, PageManager.openSnapshot (engine/src/main/java/com/arcadedb/engine/PageManager.java:560), runs it with both the database's apply write lock and the JVM-wide page-manager lock held. That is deliberate and correct (publication is excluded, so the drain converges by construction), but it means every Thread.sleep(1) there is a millisecond in which no committer of any database in the JVM can publish a page. The method's own javadoc argues for the 1 ms sleep over a spin, and that trade-off is right as long as polling is the only option - it is the polling itself that is now avoidable.
Why this is newly cheap to fix
Before #6133 the decrement was scattered across five call sites, so there was no single place to hook a "this database just drained" signal without risking a missed or duplicated notify. After #6133 there is exactly one: FlushPageIndex.release() (engine/src/main/java/com/arcadedb/engine/FlushPageIndex.java:229) is the only path on which a database's pending count goes down, and removeAllOfDatabase is the only other way an entry leaves.
Proposed
Give FlushPageIndex a per-database monitor (the same shape as the existing suspendLocks / replayDrainLocks / snapshotBarrierLocks maps) and notifyAll() from release() on the count reaching zero, plus from removeAllOfDatabase. Both waits then become lock.wait(interval) instead of Thread.sleep(interval):
- they wake the moment the last page lands, instead of up to a full interval later;
- the bounded
wait()keeps the existing timeout machinery working unchanged -waitAllPagesOfDatabaseAreFlushed's no-progress window still needs periodic re-evaluation, andwaitPendingPagesOfDatabaseUntilstill needs its deadline check, so neither becomes an unbounded park; - the snapshot barrier's JVM-wide lock hold drops from "number of residual pages, rounded up to whole milliseconds" to "the time the writes actually take".
Alternative considered and rejected: shortening DEFAULT_FLUSH_WAIT_POLL_MILLIS. It trades dead latency for CPU burn on the exact path that already holds a JVM-wide lock, and it cannot get below one scheduling quantum, so it improves the average without touching the worst case.
Verification
- A test that measures the wake latency of
waitAllPagesOfDatabaseAreFlushedagainst a page that is released mid-wait: today it is quantised to the poll interval, after the change it should track the release. - The existing
Issue6133PendingPagesPerDatabaseTest,PageManagerFlushSuspend*TestandFlushRobustnessTestmust stay green - the notify must not be able to be missed between the counter reaching zero and the waiter parking (take the monitor before the final count read, standard guarded-wait shape).
Activity
fix(#6199): a purged database carries the drained state on its counte…
test(#6199, #6200): the success-path timeouts stop being a flake sour…
fix(#6199, #6200): the flush drains wake on the page, and one suspend…