PoolView

data class PoolView(val queues: Map<GroupId, List<Long>>, val open: Int, val slots: Int)

What a FairSharePool holds at one moment: who is queued for which lane, how many slots are in use, and how many there are. A status page that shows "who goes next" reads it rather than keeping its own count beside the pool's, which would race it.

Get one from FairSharePool.view, or as the second argument of a FairSharePool.withEngine block. Either way it is copied under the pool's lock, so it shares nothing with the pool and does not change as the pool does: read it from any thread, keep it as long as you like.

queues has the shape LocalHeddle.previewPicks takes, so inside a withEngine block engine.previewPicks(view.queues, view.waiting) forecasts the order the pool grants in. The forecast is exact when the slots are raised with FairSharePool.resize and nothing else happens before the grants. Anything else can change the order: a lease completed at a cost other than its reservation, which refunds or overruns against the waiting set of that moment, or a waiter arriving, leaving or being refused. The preview also stops where the pool goes on: at a waiter whose lease the engine would not extend to its reservation, which the pool grants at the default, and at a leaf the engine reports as WaitExplanation.NoLiveLineage or WaitExplanation.LifetimeExhausted, whose waiters the pool refuses.

Throws

when open or slots is negative, a queue is empty, or a reservation is not in 1..LocalHeddle.MAX_RESERVATION.

Samples

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))

Constructors

Link copied to clipboard
constructor(queues: Map<GroupId, List<Long>>, open: Int, slots: Int)

Properties

Link copied to clipboard
val open: Int

how many leases the pool counts against slots, including any it adopted from the engine. It can be above slots after FairSharePool.resize lowered the count.

Link copied to clipboard

each leaf with a waiter queued, in the order its first waiter arrived, mapped to its waiters' reservations, head first. A leaf with no waiter is absent, never mapped to an empty list.

Link copied to clipboard
val slots: Int

how many leases may be open at once.

Link copied to clipboard

Every leaf with a waiter queued: the set the engine's calls take as waiting and eligible.