entitlement

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.

"Competing" is the set a pick takes its front over: an ACTIVE child counts when a leaf of waiting that a pick could end at lies under it. So work no descent can reach, behind a closing edge, at a group with an active child or at one that is not leasable, competes for nothing and is owed nothing, and it takes nothing from its siblings' shares. So does work beneath a group at its concurrency ceiling (WaitExplanation.AtCeiling), which no pick goes to until a lease beneath that group closes. 0 when leaf is not in waiting, is not a leaf a pick could end at, or is beneath a group at its ceiling.

A share, not a promise: the slots a leaf gets over a run track it only while the waiting set holds still, and a leaf that is waiting but rarely eligible gets less.

Throws

when the exact share does not fit in Long numerators.

Samples

val tree = twoLanes()
val root = tree.groupId()
val heavy = tree.groupId("heavy")
val light = tree.groupId("light")
val (heddle, booted) = LocalHeddle.fromTopology(tree, LocalPolicy(defaultReservation = 3L), epoch = 0L)
check(booted.refusals.isEmpty())
heddle.reconcileCapacity(totalSlots = 2L)

val queued = setOf(heavy, light)
// Light is behind at the pool: at twice the weight, heavy's turn comes due sooner.
val why = heddle.explain(light, waiting = queued, eligible = queued)
check(why is WaitExplanation.BehindAtLevel && why.parent == root && why.nextChild == heavy)
check(heddle.explain(heavy, waiting = queued, eligible = queued) == WaitExplanation.WouldBePickedNext)
check((heddle.acquire(queued, queued, startedAt = 0L) as AcquireOutcome.Acquired).lease.leaf == heavy)

// While both stay queued, heavy is owed 2 slots of every 3 and light 1.
check(heddle.entitlement(heavy, waiting = queued, totalSlots = 3L) == Rational.of(2L))
check(heddle.entitlement(light, waiting = queued, totalSlots = 3L) == Rational.ONE)
// A group with nothing queued is simply not waiting.
check(heddle.explain(light, waiting = setOf(heavy), eligible = setOf(heavy)) == WaitExplanation.NotWaiting)