TopologyPreview

What applying a candidate topology would do, worked out on a copy: nothing it came from changes. Take one with LocalHeddle.previewApply from a running engine, or with LocalHeddleSnapshot.previewApply from a stored snapshot.

It answers for the engine straight after the LocalHeddle.apply, not once things settle. The two differ only where result says so, and no refused change lands by itself: completing or cancelling a lease never re-runs a reload. A StepRefusal.Draining lands on a later apply of the same topology, once its blockingLeases have settled. A StepRefusal.Refused is for a reason that waiting on this engine's leases does not cure, save the one LocalHeddle.apply names: a reweight whose lease cannot move within Long is refused until that lease settles, and then lands on a later apply too. Everything else in the candidate is in after already. A removed lane is closed at once and owed nothing, ceilings and lanes switch at once, and a reweight's open leases have moved onto its new edge.

Who goes next has only a "straight after" answer: the picks after that depend on charges not yet made.

Samples

val before = Topology("pool", listOf(Topology.Lane("heavy", Weight.of(2)), Topology.Lane("light"), Topology.Lane("spare")))
val (heddle, _) = LocalHeddle.fromTopology(before, LocalPolicy(defaultReservation = 3L), epoch = 0L)
heddle.reconcileCapacity(totalSlots = 4L)
val heavy = before.groupId("heavy")
val light = before.groupId("light")
val spare = before.groupId("spare")
val running = (heddle.acquire(setOf(spare), setOf(spare), startedAt = 0L) as AcquireOutcome.Acquired).lease

// The edit: heavy goes to weight 3 and spare is removed, while spare still runs a job.
val edited = Topology("pool", listOf(Topology.Lane("heavy", Weight.of(3)), Topology.Lane("light")))
val seen = heddle.revision
val preview = heddle.previewApply(edited, generation = 1L)

// Spare would close at once and wait for its job; nothing else waits.
val waits = preview.result.refusals.single()
check(waits is StepRefusal.Draining && waits.child == spare && waits.blockingLeases == listOf(running))
// Straight after, spare is owed nothing, and heavy and light share the slots 3 : 1.
val queued = setOf(heavy, light, spare)
check(preview.after.entitlement(heavy, queued, totalSlots = 4L) == Rational.of(3L))
check(preview.after.entitlement(light, queued, totalSlots = 4L) == Rational.ONE)
check(preview.after.entitlement(spare, queued, totalSlots = 4L) == Rational.ZERO)
// The engine itself has not moved: spare keeps its share until the apply.
check(heddle.revision == seen && preview.basis == seen)
check(heddle.entitlement(spare, queued, totalSlots = 4L) == Rational.ONE)

// The admin confirms. Nothing happened in between, so the apply does what the preview said.
check(heddle.revision == preview.basis)
check(heddle.apply(edited, generation = 1L) == preview.result)
val before = Topology("pool", listOf(Topology.Lane("heavy", Weight.of(2)), Topology.Lane("light"), Topology.Lane("spare")))
val policy = LocalPolicy(defaultReservation = 3L)
val (heddle, _) = LocalHeddle.fromTopology(before, policy, epoch = 0L)
heddle.reconcileCapacity(totalSlots = 4L)
val spare = before.groupId("spare")
val running = (heddle.acquire(setOf(spare), setOf(spare), startedAt = 0L) as AcquireOutcome.Acquired).lease
val stored = Json.encodeToString(LocalHeddleSnapshot.serializer(), heddle.persist())

// On the admin screen: decode what was stored, and preview the edit with the engine's policy.
val decoded = LocalHeddle.decode(stored, Json)
check(decoded is RestoreResult.Restored)
val edited = Topology("pool", listOf(Topology.Lane("heavy", Weight.of(3)), Topology.Lane("light")))
val preview = decoded.snapshot.previewApply(policy, edited, generation = 1L)

// The answer the engine gives, with no revision to compare against.
val waits = preview.result.refusals.single()
check(waits is StepRefusal.Draining && waits.child == spare && waits.blockingLeases == listOf(running))
check(preview.basis == null)
check(preview.result == heddle.previewApply(edited, generation = 1L).result)

Properties

Link copied to clipboard

the engine straight after that apply, to ask who goes next, why a leaf waits, and what share it is owed.

Link copied to clipboard

the LocalHeddle.revision the preview was worked out at. While the engine still reads this revision, an apply at the same generation returns result and leaves the engine answering as after does. null for a preview of a snapshot, which has no revision.

Link copied to clipboard

exactly what LocalHeddle.apply would return, at the same generation.

Functions

Link copied to clipboard
open override fun toString(): String