withEngine
Run block on the engine, under the pool's lock, and answer what it returned. This is the one way to reach the engine once it is in a pool: LocalHeddle.persist for a snapshot, LocalHeddle.explain for why a lane waits, LocalHeddle.previewPicks for who goes next, LocalHeddle.apply for a reload, LocalHeddle.reconcileLeases to find orphans. Every other thread waits on the lock meanwhile, so keep the block short.
The block is handed the pool's PoolView beside the engine, taken when the block starts: each leaf's queued reservations, the open count and the slot count. Its PoolView.waiting is the set the engine's own calls take as waiting, and a guessed one skews them: an overrunning LocalHeddle.complete or an LocalHeddle.extend observes it as demand, so a set missing a queued lane changes later picks. explain(leaf, view.waiting, view.waiting) answers why a lane waits, as the pool's next pick sees it, and previewPicks(view.queues, view.waiting) forecasts who goes next: exact when resize raises the slots with nothing else happening, and a forecast otherwise, since a completion at a cost other than its reservation, or a waiter arriving, leaving or being refused, can change the order (see PoolView). To settle a lease the pool granted, prefer complete or cancel: they work from inside the block and use the live set.
When the block ends, however it ends, the pool catches up with what it did, then hands out slots and resumes the waiters it granted, refused or failed, after releasing the lock:
a pool lease the engine no longer holds, completed or cancelled through the engine, frees its slot;
the waiters of a leaf no lease can end at any more, say one a reload removed, are refused;
a waiter the block's changes made grantable, say by a reload lifting a ceiling, is granted.
A refusal reads the tree as it stands when the waiter arrives, so a waiter for a leaf that a later block attaches is refused before that block runs: attach the leaf first, then wait on it.
The slot count is the pool's: change it with resize. A block that calls LocalHeddle.reconcileCapacity itself changes the engine only until the block ends, when the pool reconciles the engine back to slots.
What it does not catch up with: a lease the block opens with LocalHeddle.acquire or LocalHeddle.admit is never counted against slots, though complete and cancel still close it, and a lease the pool already let go of is never counted again. That is the one an engine refused to take back from a cancelled waiter (see Failures, above): the engine still holds it, and LocalHeddle.reconcileLeases reports it as an orphan.
Lease ids are never reused under a pool: the engine numbers each lease one past the last in its epoch, a reload keeps every open lease's id, and the pool's engine is never replaced. So a late complete for a lease the block already closed answers LeaseOutcome.Unknown (or LeaseOutcome.Stale for a lease from an earlier epoch), and never closes another caller's lease.
A block may call back into the pool, complete, cancel, resize or withEngine; that inner call hands out nothing and resumes nothing, and this call does both once the block ends. The view the block was handed is a copy, so it does not follow those calls: view takes a fresh one.
Parameters
given the engine and the pool's view as the block starts.
Samples
val capped = Topology("farm", listOf(Topology.Lane("web", Weight.of(3), maxConcurrent = 1L), Topology.Lane("mobile")))
val web = capped.groupId("web")
val (heddle, booted) = LocalHeddle.fromTopology(capped, LocalPolicy(defaultReservation = 3L), epoch = 0L)
check(booted.refusals.isEmpty())
val pool = FairSharePool(heddle, slots = 2, TimeSource.Monotonic, costUnit = 1.minutes)
val first = pool.acquire(web)
check(first is SlotOutcome.Granted)
coroutineScope {
val second = async(start = CoroutineStart.UNDISPATCHED) { pool.acquire(web) }
check(!second.isCompleted) // web is at its cap
// Everything that touches the engine goes through the pool, which hands over a view of its
// queues too: why web waits, as the pool's next pick sees it.
val why = pool.withEngine { engine, view -> engine.explain(web, view.waiting, view.waiting) }
check(why == WaitExplanation.AtCeiling(web, limit = 1L))
// A reload, a snapshot, a search for orphans: the same way in.
val uncapped = Topology("farm", listOf(Topology.Lane("web", Weight.of(3)), Topology.Lane("mobile")))
val reload = pool.withEngine { engine, _ -> engine.apply(uncapped, generation = 1L) }
check(reload.refusals.isEmpty())
val granted = second.await()
check(granted is SlotOutcome.Granted)
pool.complete(granted.lease.id, actualCost = 3L)
}
pool.complete(first.lease.id, actualCost = 3L)
// Lowering the count closes nothing: it only holds back the next grant.
pool.resize(1)val farm = Topology("farm", listOf(Topology.Lane("web", Weight.of(3)), Topology.Lane("mobile")))
val web = farm.groupId("web")
val mobile = farm.groupId("mobile")
val (heddle, booted) = LocalHeddle.fromTopology(farm, LocalPolicy(defaultReservation = 3L), epoch = 0L)
check(booted.refusals.isEmpty())
val pool = FairSharePool(heddle, slots = 2, TimeSource.Monotonic, costUnit = 1.minutes)
val running = listOf(pool.acquire(web), pool.acquire(mobile)).map { (it as SlotOutcome.Granted).lease }
coroutineScope {
// Five builds queue behind the two running, each with its estimate in minutes.
val builds = listOf(web to 3L, web to 12L, mobile to 3L, web to 3L, mobile to 3L)
builds.forEach { (lane, minutes) ->
launch(start = CoroutineStart.UNDISPATCHED) { pool.withSlot(lane, reservation = minutes) { } }
}
// A gauge needs no engine: each lane's queued estimates, head first, and the slots in use.
val view = pool.view()
check(view.queues == mapOf(web to listOf(3L, 12L, 3L), mobile to listOf(3L, 3L)))
check(view.open == 2 && view.slots == 2)
// Who goes next is a question for the engine, about the pool's own queues: ask it inside
// withEngine, so the view and the answer are of the same moment.
val next = pool.withEngine { engine, queued -> engine.previewPicks(queued.queues, queued.waiting) }
check(next == listOf(web, mobile, web, mobile, web))
running.forEach { pool.complete(it.id, actualCost = it.reserved) }
}
check(pool.view() == PoolView(emptyMap(), open = 0, slots = 2))