previewApply

fun previewApply(new: Topology, generation: Long, startedFrom: Topology): PreviewOutcome

What the checked apply of new at generation from startedFrom would do now: ApplyOutcome.TreeChanged exactly when it would refuse, and otherwise PreviewOutcome.Previewed with what the unchecked previewApply returns. A read, like that one, whichever it returns.

Both answer for the engine as it is when called. A tree applied between this preview and the save still refuses the save, so a page that previewed need not compare trees itself.

Throws

as apply does, whether or not the tree has changed.

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)

fun previewApply(new: Topology, generation: Long): TopologyPreview

What apply of new at generation would do, worked out on a copy of this engine: the ApplyResult it would return, and the engine straight after it as a LocalHeddleView. A read: it changes nothing here, the wake memory and revision included, and persist returns the same snapshot after it as before.

Pass the generation you will apply with. The edges a reload adds are named by it, so a preview at one generation and an apply at another name different edges in ApplyResult.applied. Whether anything else can differ between the two is not established, so do not rely on it.

The preview holds for the engine at TopologyPreview.basis. Compare that with revision before acting on it: a lease, a charge or a reload since then can change the answer. A revision moves on every acquire, refused ones included, so on a busy engine it will have moved by the time a person confirms; preview again then rather than refuse the apply. apply takes its diff on the ledger it applies to, so applying after the engine moved on is still correct, just not necessarily what was previewed. Whether someone else applied a tree since the edit started is a different question, which revision cannot answer: pass startedFrom to the checked previewApply and apply for that.

Shares over a candidate tree with no engine behind it need no preview: boot one with fromTopology and ask it entitlement.

Parameters

generation

as apply's, and the same one.

Throws

as apply does: 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, _) = 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 candidate = draft("farm") {
    group("web", Weight.of(3)) { lane("builds") }
    group("mobile") { lane("builds") }
}
val web = candidate.groupId("web", "builds")
val mobile = candidate.groupId("mobile", "builds")
val (scratch, booted) = LocalHeddle.fromTopology(candidate, LocalPolicy(defaultReservation = 3L), epoch = 0L)
check(booted.refusals.isEmpty())

// Eight agents, both teams busy: web is owed six, mobile two.
val busy = setOf(web, mobile)
check(scratch.entitlement(web, busy, totalSlots = 8L) == Rational.of(6L))
check(scratch.entitlement(mobile, busy, totalSlots = 8L) == Rational.of(2L))