ApplyOutcome

sealed interface ApplyOutcome

What a checked LocalHeddle.apply did: Applied the edit, or refused it whole because the tree it started from is no longer the one running, as TreeChanged.

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)

Inheritors

Types

Link copied to clipboard
data class Applied(val result: ApplyResult) : ApplyOutcome

The tree had not changed since the edit started, so the edit was applied: result is what the unchecked LocalHeddle.apply returns.

Link copied to clipboard
data class TreeChanged(val current: Topology) : ApplyOutcome, PreviewOutcome

Someone applied another tree since the edit started: LocalHeddle.topology is now current, not the tree the edit was made from. Nothing changed, LocalHeddle.revision included.