admit

fun admit(leaf: GroupId, waiting: Set<GroupId>, startedAt: Long, reservation: Long = policy.defaultReservation): AdmitOutcome

Charge work another scheduler already started at leaf: open a lease for it with no pick, delegating reservation down the leaf's lineage, and leave the wake memory as acquire(waiting, eligible = {leaf}) would have (#3320). Never refused for supply, since the work is already running; a shortfall at the root is minted.

waiting is required, as it is on extend and complete: a charge is observed against who is waiting now, never against who was last seen waiting (#3471). At each group down leaf's lineage it seats the newborns and clamps the wakers against waiting, as that pick does, then delegates reservation down the lineage without picking. The equivalence holds wherever that acquire would lease; where it would be refused, this still charges.

Nor is it refused at a concurrency ceiling (see "Ceilings"), so with ceilings set the equivalence covers only a leaf with no group at its ceiling on its lineage. It observes waiting as that acquire does, every leaf beneath a group at its ceiling counted idle, so beside a capped sibling the two still agree. For a capped leaf they part: the acquire picks nothing and returns AcquireOutcome.NothingEligible, while this charges the work and observes leaf as waiting, since it is running.

Returns AcquireOutcome.NoLiveLineage when leaf is not leasable (the root, a group with an active child, or a group the topology declares no lane: see "What a lease ends at"), or has no lineage of active edges to the root. The ledger is then unchanged, but the wake memory has observed waiting at the root, as the refused acquire's pick would have when the engine can fund the reservation; where it cannot, that acquire returns AcquireOutcome.SupplyExhausted and observes nothing.

Never refused at the lifetime limit either (see "The lifetime limit"). A reservation that an edge of the lineage has no room for within Long, or that the root cannot fund in whole, is reserved nowhere: the lease opens with Lease.reserved 0, nothing else changes, the wake memory included, and its cost arrives at complete as an overrun.

Throws

Samples

val farm = draft("farm") {
    lane("web", Weight.of(3))
    lane("mobile", maxConcurrent = 1)
    group("forks") { template() }
}
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())

// No capacity is reconciled, so there is nothing to hand out; an admitted build is charged anyway.
val both = setOf(web, mobile)
check(heddle.acquire(both, both, startedAt = 0L) == AcquireOutcome.SupplyExhausted)
val byHand = heddle.admit(mobile, waiting = both, startedAt = 0L)
check(byHand is AcquireOutcome.Acquired)

// Mobile is now at its ceiling of one, and a second build started by hand is still charged.
val another = heddle.admit(mobile, waiting = both, startedAt = 1L)
check(another is AcquireOutcome.Acquired)
check(heddle.complete(another.lease.id, actualCost = 5L, waiting = emptySet()) == LeaseOutcome.Done(charged = 5L, overrun = 2L, unrecorded = 0L))

// A group with a template and nothing attached is not somewhere a build can run.
val forks = farm.groupId("forks")
check(heddle.admit(forks, waiting = setOf(forks), startedAt = 2L) == AcquireOutcome.NoLiveLineage(forks))