shrink

fun shrink(lease: LeaseId, amount: Long): LeaseOutcome

Hand back amount of what lease reserves, up the path it was opened on, as complete hands back an unused remainder. The lease stays open, reserving amount less. It charges nothing and observes nothing, so it takes no waiting set, and it is never refused, as complete is not: a draining path takes the refund as it takes cancel's.

Its use is straight after a pick, when the work the slot went to is shorter than the reservation the slot was acquired at. There, before any other call, it leaves every position and the wake memory as if the lease had been opened at the smaller size: the grant and the refund fall in the same step of the parent's clock. Later, it is a refund like any other, and moves the lane's position back as complete's remainder would.

Returns LeaseOutcome.Done with nothing charged, or LeaseOutcome.Unknown or LeaseOutcome.Stale when no lease is open under that id.

Shrunk by all it reserves, the lease stays open at 0, and a reload removing its lane waits on it as on any open lease, as StepRefusal.Draining with nothing outstanding when it is the only one there, until it is completed or cancelled.

Throws

when amount is not in 1..Lease.reserved: a lease cannot hand back more than it reserves, and a lease reserving nothing, one admitted past the lifetime limit or shrunk whole already, has nothing to hand back.

Samples

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

val queued = setOf(heavy, light)
val picked = heddle.acquire(waiting = queued, eligible = queued, startedAt = 0L)
check(picked is AcquireOutcome.Acquired && picked.lease.reserved == 8L)

// The winner's next job takes 2 units: hand back the other 6 before anything else happens.
check(heddle.shrink(picked.lease.id, amount = 6L) == LeaseOutcome.Done(charged = 0L, overrun = 0L, unrecorded = 0L))
check(heddle.snapshot().leases.single().reserved == 2L)

// It completes at its size with nothing left to hand back; a shrink past what it reserves throws.
val tooFar = try {
    heddle.shrink(picked.lease.id, amount = 3L)
} catch (expected: IllegalArgumentException) {
    expected
}
check(tooFar is IllegalArgumentException)
check(heddle.complete(picked.lease.id, actualCost = 2L, waiting = queued) == LeaseOutcome.Done(charged = 2L, overrun = 0L, unrecorded = 0L))