attach

fun Topology.attach(group: List<String>, name: String, ledger: EntitlementLedger, generation: Long): List<TopologyStep>

The steps that attach the child name to the declared group at group: attach of the one-level path GroupPath.Attached(group, name).

Throws

if group does not name a group with a template, if name is empty, if generation is negative, or if ledger belongs to another root, as Topology.Companion.diff refuses it. Nothing else about name is refused.

Samples

val pool = draft("pool") {
    group("tenants") {
        lane("x")
        template(Weight.of(2))
    }
}
var ledger = EntitlementLedger.bootstrap(pool.groupId(), mapOf(ReplicaId("me") to 100L), "boot")
fun apply(steps: List<TopologyStep>) {
    for (step in steps) {
        val patch = when (step) {
            is TopologyStep.Prepare -> ledger.prepare(step.record)
            is TopologyStep.Activate -> ledger.activate(step.edge)
            is TopologyStep.Close -> ledger.close(step.edge)
            is TopologyStep.Retire -> ledger.retire(step.edge)
            is TopologyStep.Reconcile -> error("nothing here carried work")
        }
        ledger = ledger.piece(checkNotNull(patch))
    }
}
apply(Topology.diff(pool, ledger, generation = 0))

// Named like the declared lane `x`, and still its own child: attached names have their own namespace.
val tenant = pool.attachedChildId(listOf("tenants"), "x")
check(tenant != pool.groupId("tenants", "x"))
val attach = pool.attach(listOf("tenants"), "x", ledger, generation = 0)
check(attach.map { it::class } == listOf(TopologyStep.Prepare::class, TopologyStep.Activate::class))
check((attach.first() as TopologyStep.Prepare).record.weight == Weight.of(2))
apply(attach)
// Asking again is harmless, and a reload that keeps the template keeps the child.
check(pool.attach(listOf("tenants"), "x", ledger, generation = 0).isEmpty())
check(Topology.diff(pool, ledger, generation = 0).isEmpty())

// Detached, it is closed and retired; attached again, it gets a fresh edge.
apply(pool.detach(listOf("tenants"), "x", ledger))
check(ledger.lineageOf(tenant) == null)
check(pool.attach(listOf("tenants"), "x", ledger, generation = 0).first() is TopologyStep.Prepare)

fun Topology.attach(path: GroupPath, ledger: EntitlementLedger, generation: Long): List<TopologyStep>

The steps that attach the child at path, read from root down, at the weight the template it is attached under gives, and every attached group above it that is not attached yet, in the same list: [Prepare, Activate] for a new child of an attached group, the prepares top-down and then the activations top-down when its ancestors are new too, [Activate] when only the prepare landed, and [] when the whole path is attached. Each name is opaque and lives in its own namespace, so it never collides with a declared child, however it is spelt.

An attached child whose template carries a template is a group: the consumer attaches children beneath it with a GroupPath.Nested, and it is never leased itself. Attaching it alone attaches no child beneath it. Attaching one again after a detach brings none of the children that detach closed back: their CLOSING edges retire in the same list, refused while work beneath them is outstanding, as a diff retires them.

Like Topology.Companion.diff it reads what exists from ledger, so asking twice is harmless, and the same contracts on TopologyStep apply. Two more shapes follow from them, for each group on the path. A child detached but still draining is re-attached on a new edge that waits for the old one to retire: [Prepare, Retire, Activate], and the prepared edge alone records that the child is wanted until the activate lands. A child whose edge is at an older template weight is moved to the current one: [Prepare, Close, Retire, Activate].

Each edge is numbered as Topology.Companion.diff numbers its edges: at generation, raised past every generation the ledger holds, retired edges included, so a re-attach never reuses an id. The ledger carries that floor, so an attach reads no more of it than the path's own edges.

Parameters

generation

a counter, >= 0, for the edges this attach prepares.

Throws

if path is a GroupPath.Declared, if no template reaches it (findGroupId is null), if generation is negative, or if ledger belongs to another root, as Topology.Companion.diff refuses it.

Samples

val ci = draft("ci") {
    // Each repository weighs 2 beside the nightly lane, and each author caps at one job.
    group("repos") { template(Weight.of(2), template = Topology.ChildTemplate(Weight.ONE, maxConcurrent = 1L)) }
    lane("nightly")
}
val (heddle, booted) = LocalHeddle.fromTopology(ci, LocalPolicy(defaultReservation = 1L), epoch = 0L)
check(booted.refusals.isEmpty())
heddle.reconcileCapacity(totalSlots = 4L)

// Attaching an author attaches its repository too, in the same unit.
val kuilt = GroupPath.Attached(listOf("repos"), "kuilt")
val alice = GroupPath.Nested(kuilt, "alice")
check(heddle.attach(alice, generation = 1L).refusals.isEmpty())
val aliceId = checkNotNull(ci.findGroupId(alice))
check(aliceId == GroupId("ci/repos/!kuilt/!alice") && ci.findPath(aliceId) == alice)

// Work is charged to the author; the repository itself is never leased.
val kuiltId = checkNotNull(ci.findGroupId(kuilt))
check(heddle.admit(kuiltId, waiting = setOf(kuiltId), startedAt = 0L) == AcquireOutcome.NoLiveLineage(kuiltId))
val lease = (heddle.acquire(setOf(aliceId), setOf(aliceId), startedAt = 0L) as AcquireOutcome.Acquired).lease
check(lease.leaf == aliceId)

// Detaching the repository closes alice too, and waits for her running job.
val detached = heddle.detach(kuilt)
check(detached.refusals.isNotEmpty() && detached.refusals.all { it is StepRefusal.Draining })
check(heddle.complete(lease.id, actualCost = 1L, waiting = emptySet()) is LeaseOutcome.Done)
check(heddle.detach(kuilt).refusals.isEmpty())