LocalHeddleView

The questions you can ask a LocalHeddle without changing it, asked of a saved state instead of a running engine: who goes next (previewPicks), why a leaf is waiting (explain), what share a leaf is owed (entitlement), and what share every group is owed (shareLine). An admin screen that holds only the last stored snapshot gets the same answers the engine that took it would give, with no engine to start and nothing to move.

Get one from a snapshot with LocalHeddleSnapshot.view. The engine answers these questions through a view of its own state, so the two cannot disagree: a view of the snapshot LocalHeddle.persist returns answers exactly as that engine did when it took it.

A view is a value. It reads the snapshot's books, wake memory, capacity and last applied topology, with a count of the leases open beneath each node that has a ceiling (Topology.Node.maxConcurrent). Each question that picks plays its picks forward on a throwaway engine built from them, so asking twice gives the same answer twice; the share questions pick nothing, and a share line takes the wake step on a copy of the wake memory. It needs the policy too, which a snapshot does not keep: pass the one the engine ran with.

A view from a snapshot shares nothing with any engine, and each question only reads it, so any number of threads may ask one view at once. The engine's own calls go through a view that reads its live state for the length of one call, under the same single writer as every other call.

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)

Functions

Link copied to clipboard
fun entitlement(leaf: GroupId, waiting: Set<GroupId>, totalSlots: Long): Rational

leaf's fair share of totalSlots slots right now, as an exact rational: its weight's share among the siblings competing beside it, times its parent's share among the siblings beside that, up to the root, times totalSlots. This is the share an idealised fair scheduler (generalised processor sharing) would give it while waiting stays as it is.

Link copied to clipboard
fun explain(leaf: GroupId, waiting: Set<GroupId>, eligible: Set<GroupId>, reservation: Long = policy.defaultReservation): WaitExplanation

Why leaf is not, or is, what the next LocalHeddle.acquire on these same arguments picks. Changes nothing, not even the wake memory.

Link copied to clipboard
fun previewPicks(waiting: Map<GroupId, Int>, eligible: Set<GroupId>, reservations: List<Long>): List<GroupId>

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.

fun previewPicks(queues: Map<GroupId, List<Long>>, eligible: Set<GroupId>, reservation: Long = policy.defaultReservation): List<GroupId>

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.

Link copied to clipboard
fun shareLine(waiting: Set<GroupId>): ShareLine

What every group is owed of the whole pool while waiting holds still, read once and answered group by group: see ShareLine.share. Where entitlement answers for a leaf a pick can end at, a line answers for any group, the root and every group above a lane included.