lag

fun lag(group: GroupId): Rational?

How far group stands from its share at its parent, in units of service: positive when it is owed that much, negative when it has had that much more than its share. A team two units of service behind where its weight puts it reads 2, and the siblings that ran ahead of it read below 0 between them by as much.

Over one parent's competing children the lags add up to 0. A grant of r units to one of them, s, moves s by −r · (1 − w_s / Σw) and each competing sibling t by +r · w_t / Σw, where Σw is the competing siblings' summed weight.

It is weight × (front − position), read at group's parent as a pick arriving there now would read it: after the seat and wake step that pick takes first, wake clamp included. The position is group's virtual service there, and the front its competing siblings' weighted mean, the one WaitExplanation.BehindAtLevel reports. So wherever LocalHeddleView.explain answers WaitExplanation.BehindAtLevel at that parent for a leaf beneath group, this is weight × (frontWide − leafSideVirtualServiceWide), for every outstanding below Long.MAX_VALUE: a competing child asks for more than it holds however much that is, so the pick counts it in its front as this line does (#3707). A child holding exactly Long.MAX_VALUE still competes but asks for nothing, so the pick leaves it out of its front and this line does not; that child is far past the lifetime limit, where no pick serves it.

A child that has competed since its parent began reads what it is owed of the service handed out beneath that parent. A child coming back to compete does not always read 0. One the engine saw stop asking keeps the lag it left with. One clamped as it wakes, or a newborn, lands at the front on its own grid: 0, or less than a sixteenth of a unit below it, one tick of that grid. A clamped one with leases open lands past the front by what they reserve, so it reads that much below 0 (#3238). A LocalPolicy.sleeperCredit c leaves a clamped one above zero instead, by up to c × (1 − w / Σw), since it counts in the front it lands behind. So "just served reads negative" holds from a level start, not in general.

null for the root; for a group that does not compete, which off the root is exactly a group whose share is 0; and for each child of a parent where no step can be taken, because its weights fail the weight budget or the step would pass the position limit. Such a child still competes, so its share is above 0 while its lag is null. The parent fails only the rows beneath it, never the line.

Each answer is formed when it is asked for, exactly, from what the line took once. How far the parent has climbed costs nothing: the front and the position are subtracted in full before anything narrows.

Throws

when the lag itself does not fit an exact Rational over Long.

Samples

val tree = draft("pool") {
    lane("web")
    lane("mobile")
    lane("docs")
}
val pool = tree.groupId()
val web = tree.groupId("web")
val mobile = tree.groupId("mobile")
val docs = tree.groupId("docs")
val (heddle, booted) = LocalHeddle.fromTopology(tree, LocalPolicy(defaultReservation = 1L), epoch = 0L)
check(booted.refusals.isEmpty())
heddle.reconcileCapacity(totalSlots = 2L)
val both = setOf(web, mobile)

// Both wait, but this slot can run only web: web takes one unit, half a unit past its share.
check(heddle.acquire(both, eligible = setOf(web), startedAt = 0L) is AcquireOutcome.Acquired)
val line = heddle.shareLine(both)
check(line.lag(web) == Rational.of(-1L, 2L))
check(line.lag(mobile) == Rational.of(1L, 2L))
// Docs has nothing queued and the pool has no siblings: neither stands anywhere.
check(line.lag(docs) == null && line.lag(pool) == null)

// explain reads the same distance: web's side stands half a unit past the front.
val why = heddle.explain(web, waiting = both, eligible = both)
check(why is WaitExplanation.BehindAtLevel && why.nextChild == mobile)
check(checkNotNull(why.frontWide).toDouble() - checkNotNull(why.leafSideVirtualServiceWide).toDouble() == -0.5)

// Docs was idle all along. Queued now, it joins at the front: owed nothing, owing nothing.
check(heddle.shareLine(setOf(web, mobile, docs)).lag(docs) == Rational.ZERO)