apply
Move the tree to new: add what it adds, remove what it removes, and give every reweighted group a new edge at its new weight, as Topology.Companion.diff lists the steps. Open leases survive it, and each is still charged once, at complete.
One unit, swapped in once. The diff is taken here, on the ledger it is applied to, and the whole list runs on a working copy that replaces the ledger at the end, so nothing is ever leased against a state part-way through a reload. A child whose change cannot land yet is reported in ApplyResult.refusals, and every other child's change still lands in the same swap. Apply the same topology again later, and only what is left is listed. Nothing activates that the root cannot reach: a new group left out takes every group beneath it out too, each reported, as StepRefusal.Draining on the same leases when the group waits on a drain and as StepRefusal.Refused otherwise. A running group whose change is refused keeps its old edge, so what is beneath it still lands. The same holds for attach and for a boot.
A reweight moves its open leases with it. A changed weight is a new edge, and an open lease whose path crosses the old one moves onto the new one in the same unit: its reservation is handed back up the old path, the old edge retires with nothing outstanding, the new one activates (taking over the old edge's spend with a TopologyStep.Reconcile when the group is internal), and the reservation is delegated down the new path, which the lease charges from then on. A lease under a group and its child, both reweighted, crosses two old edges and moves once. It keeps its id, leaf and reservation, so the caller sees nothing change. The move counts the reservation again on every edge the old and new paths share, so where one of them has no room for it within Long the lease is not moved and the child's change is refused, as StepRefusal.Refused, until the lease settles (see "The lifetime limit"). The reweighted group's fairness history resets: its new edge is seated at its parent's front, like a newcomer's, so it is neither owed for its past nor charged for it. A child added back before its removal drained is a new edge for the same group, and its draining leases move onto it the same way.
A removal drains. A removed child is closed at once, so nothing new is leased there, and retires once its open leases complete, whatever they reserve; until then it is StepRefusal.Draining, naming the leases it waits for. A removal under a reweighted group wins: that group's reweight waits too, as StepRefusal.Draining, and changes nothing until every lease beneath it can move. A lease whose work is gone is found by reconcileLeases and settled with complete, so an orphan holds a reload up no longer than that.
A rename or a move is a delete plus an add: the new group starts with no history, and the old one drains under its old id. While a top-level group drains, an overrun charged by one of its leases is reported in LeaseOutcome.Done but recorded on no edge (see "A draining path").
Parameters
a counter, >= 0, for the edges this reload adds; raised past every generation the ledger holds, so a reused counter never reuses an id.
Throws
if new's root is not root, or generation is negative.
Samples
val before = Topology("pool", listOf(Topology.Lane("heavy", Weight.of(2)), Topology.Lane("light"), Topology.Lane("spare")))
val (heddle, booted) = LocalHeddle.fromTopology(before, LocalPolicy(defaultReservation = 3L), epoch = 0L)
check(booted.refusals.isEmpty())
heddle.reconcileCapacity(totalSlots = 4L)
val heavy = before.groupId("heavy")
val spare = before.groupId("spare")
val running = (heddle.acquire(setOf(heavy), setOf(heavy), startedAt = 0L) as AcquireOutcome.Acquired).lease
val leftover = (heddle.acquire(setOf(spare), setOf(spare), startedAt = 0L) as AcquireOutcome.Acquired).lease
// While both leases are open, heavy goes to weight 3 and spare is removed.
val after = Topology("pool", listOf(Topology.Lane("heavy", Weight.of(3)), Topology.Lane("light")))
val reload = heddle.apply(after, generation = 1L)
// Spare is closed to new work and waits for its lease; heavy's lease moved onto its new edge.
val waiting = reload.refusals.single()
check(waiting is StepRefusal.Draining && waiting.child == spare && waiting.blockingLeases == listOf(leftover))
check(heddle.complete(running.id, actualCost = 3L, waiting = emptySet()) == LeaseOutcome.Done(charged = 3L, overrun = 0L, unrecorded = 0L))
check(heddle.complete(leftover.id, actualCost = 2L, waiting = emptySet()) == LeaseOutcome.Done(charged = 2L, overrun = 0L, unrecorded = 0L))
// Once the drain is over, the same reload finishes the job.
check(heddle.apply(after, generation = 1L).refusals.isEmpty())apply new at generation, but only if topology is still startedFrom, the tree the edit was made from: the save of an admin page that two people may edit at once. When someone applied another tree in between, it applies nothing and returns ApplyOutcome.TreeChanged naming the running tree; otherwise it returns ApplyOutcome.Applied with exactly what apply returns.
It compares trees, not revisions. revision moves on every acquire, so a save checked against it would be refused on any engine that granted a lease while the admin was editing. Leases, charges, capacity and attach or detach leave topology as it is, so none of them refuses a save; only an apply of a tree unequal to startedFrom does. Trees are compared whole and in the order written, the same equality Topology has.
A refused save is a read: it changes nothing, revision included, and persist returns the same snapshot after it as before.
Parameters
the topology the edit was read from, before anyone changed it.
Throws
as apply does, whether or not the tree has changed: new's root is not root, or generation is negative.
Samples
val (heddle, _) = LocalHeddle.fromTopology(twoLanes(), LocalPolicy(defaultReservation = 3L), epoch = 0L)
heddle.reconcileCapacity(totalSlots = 2L)
val heavy = heddle.topology.groupId("heavy")
fun Topology.withLane(name: String): Topology {
val document = toDocument()
return (Topology.from(document.copy(children = document.children + TopologyDocument.Lane(name))) as TopologyParse.Valid).topology
}
// Both pages load the running tree, and each admin adds a lane.
val startedFrom = heddle.topology
val first = startedFrom.withLane("batch")
val second = startedFrom.withLane("nightly")
// Work goes on while they edit: a lease moves the revision, but not the tree.
val seen = heddle.revision
check(heddle.acquire(setOf(heavy), setOf(heavy), startedAt = 0L) is AcquireOutcome.Acquired)
check(heddle.revision != seen && heddle.topology == startedFrom)
// The first admin sees the change and its preview, confirms, and the save applies what was shown.
check(startedFrom.changesTo(first) == listOf(TopologyChange.Added(listOf(2), listOf("pool", "batch"), Topology.Lane("batch"))))
val preview = heddle.previewApply(first, generation = 1L, startedFrom = startedFrom)
check(preview is PreviewOutcome.Previewed && preview.preview.result.refusals.isEmpty())
check(heddle.apply(first, generation = 1L, startedFrom = startedFrom) == ApplyOutcome.Applied(preview.preview.result))
// The second edit started from a tree that is no longer running. Saved as it stands, it would
// also take away the lane the first admin just added, so the save refuses and changes nothing.
val before = heddle.revision
val refused = heddle.apply(second, generation = 2L, startedFrom = startedFrom)
check(refused is ApplyOutcome.TreeChanged && refused.current == first && heddle.revision == before)
check(
refused.current.changesTo(second) == listOf(
TopologyChange.Added(listOf(2), listOf("pool", "nightly"), Topology.Lane("nightly")),
TopologyChange.Removed(listOf(2), listOf("pool", "batch"), Topology.Lane("batch")),
),
)
// So the page reloads: the admin makes the edit again on the running tree, and saves from that.
val redone = refused.current.withLane("nightly")
check(refused.current.changesTo(redone) == listOf(TopologyChange.Added(listOf(3), listOf("pool", "nightly"), Topology.Lane("nightly"))))
check(heddle.apply(redone, generation = 2L, startedFrom = refused.current) is ApplyOutcome.Applied)
check(heddle.topology == redone)