previewPicks
Who would go next: the leaves reservations.size calls to LocalHeddle.acquire would pick, in order, if nothing else happened in between. Changes nothing, not even the wake memory.
waiting gives each leaf's queued work as a count. A leaf counts as waiting while its count is above zero, and each pick takes one from the leaf it picked. Pick i reserves reservations[i], and its eligible set is eligible among the leaves still waiting. The list ends early at the first call that would not acquire.
Throws
when a count is negative or a reservation is not in 1..MAX_RESERVATION.
Samples
val tree = twoLanes()
val heavy = tree.groupId("heavy")
val light = tree.groupId("light")
val policy = LocalPolicy(defaultReservation = 3L)
val (heddle, booted) = LocalHeddle.fromTopology(tree, policy, epoch = 0L)
check(booted.refusals.isEmpty())
heddle.reconcileCapacity(totalSlots = 2L)
val stored = Json.encodeToString(LocalHeddleSnapshot.serializer(), heddle.persist())
// On the admin screen: decode the stored snapshot and view it with the engine's policy.
val decoded = LocalHeddle.decode(stored, Json)
check(decoded is RestoreResult.Restored)
val view = decoded.snapshot.view(policy)
val queued = setOf(heavy, light)
check(view.previewPicks(mapOf(heavy to 2, light to 1), eligible = queued, reservations = List(3) { 3L }) == listOf(heavy, light, heavy))
check(view.explain(heavy, waiting = queued, eligible = queued) == WaitExplanation.WouldBePickedNext)
check(view.entitlement(light, waiting = queued, totalSlots = 3L) == Rational.ONE)
// The engine gives the same answers, and asking either moved nothing.
check(heddle.previewPicks(mapOf(heavy to 2, light to 1), queued, List(3) { 3L }) == listOf(heavy, light, heavy))
check((heddle.acquire(queued, queued, startedAt = 0L) as AcquireOutcome.Acquired).lease.leaf == heavy)Who would go next, with each leaf's own next job: the leaves a caller's loop over queues would pick, in order, if nothing else happened in between. Changes nothing, not even the wake memory.
queues lists each leaf's queued jobs by their reservations, head first. A leaf counts as waiting while its list is not empty. Each pick does what that caller does: LocalHeddle.acquire with reservation among the waiting leaves in eligible, then size the lease to the winner's head, then pop the head: when the head is smaller than reservation, LocalHeddle.shrink the lease by the difference, and when it is larger, LocalHeddle.extend it. Neither makes a pick, so the order is the one the slot's own reservation decides, and the head sets what the lease holds. The shrink comes straight after the pick, so the next pick reads the positions an acquire at the head would have left (#3648).
The list ends when no waiting leaf is eligible, or at the first call that is refused. When the acquire lands and its extend is the call refused, that pick is left out and the list ends there, even if the root could fund the next leaf: the caller's lease would hold less than its job needs, and its loop stops at it.
Throws
when reservation or any queued reservation, not only a head, is not in 1..MAX_RESERVATION.
Samples
val farm = draft("farm") {
group("web", Weight.of(3)) { lane("builds") }
group("mobile") { lane("builds") }
}
val web = farm.groupId("web", "builds")
val mobile = farm.groupId("mobile", "builds")
val policy = LocalPolicy(defaultReservation = 3L)
val (heddle, booted) = LocalHeddle.fromTopology(farm, policy, epoch = 0L)
check(booted.refusals.isEmpty())
heddle.reconcileCapacity(totalSlots = 4L)
val stored = Json.encodeToString(LocalHeddleSnapshot.serializer(), heddle.persist())
// On the admin screen: each lane's queued builds by their estimates in minutes, the next one first.
val queues = mapOf(web to listOf(3L, 12L, 3L, 3L), mobile to listOf(3L, 3L, 3L))
val decoded = LocalHeddle.decode(stored, Json)
check(decoded is RestoreResult.Restored)
val view = decoded.snapshot.view(policy)
val both = setOf(web, mobile)
check(view.previewPicks(queues, eligible = both) == listOf(web, mobile, web, mobile, web, web, mobile))
check(view.previewPicks(queues, eligible = both) == heddle.previewPicks(queues, eligible = both))