fromTopology

fun fromTopology(topology: Topology, policy: LocalPolicy, epoch: Long): LocalHeddle.Boot

An engine running topology, issuing lease ids under epoch, with no supply yet: it mints its own once reconcileCapacity is called. Its root is the topology's root. It comes back with what booting it did, as a Boot.

It boots the way every later reload arrives: the steps apply would run for topology on an empty ledger, at generation 0, under the same weight budget. So the ids it starts with are exactly Topology.lower's, and there is no second, unfloored way to build a tree.

A boot leaves out what does not fit, and says so. Where the weights of one group's children do not fit the budget together (see "Failure surface"), that group and every group beneath it are left out, each reported in Boot.result as a StepRefusal.Refused, and the rest of the tree boots. When the group is the root, nothing boots, and every group is reported. Unlike a reload's, the answer does not depend on which sibling's activation tipped the set over, so it is the same whatever the children are named and whatever order they are written in. apply a tree that fits to bring the groups left out back.

Samples

val tree = twoLanes()
val heavy = tree.groupId("heavy")
val light = tree.groupId("light")
val (heddle, booted) = LocalHeddle.fromTopology(tree, LocalPolicy(defaultReservation = 3L), epoch = 0L)
check(booted.refusals.isEmpty())
// Four slots: the engine mints its own supply ahead of them.
heddle.reconcileCapacity(totalSlots = 4L)

// Both groups have work queued, and this slot can run either.
val queued = setOf(heavy, light)
val first = heddle.acquire(waiting = queued, eligible = queued, startedAt = 100L)
check(first is AcquireOutcome.Acquired)

// The job overran its 3-unit estimate: all 5 units are charged, 2 of them as overrun.
check(heddle.complete(first.lease.id, actualCost = 5L, waiting = queued) == LeaseOutcome.Done(charged = 5L, overrun = 2L, unrecorded = 0L))

// Over many slots the shares follow the weights, 2 : 1.
val picks = (0 until 30).map {
    val lease = (heddle.acquire(queued, queued, startedAt = it.toLong()) as AcquireOutcome.Acquired).lease
    heddle.complete(lease.id, actualCost = 3L, waiting = queued)
    lease.leaf
}
check(picks.count { it == heavy } in 19..21)
// The admin's file: web gets three parts to mobile's one, and forks are attached as they open.
val text = """
    {"root": "farm", "children": [
      {"type": "lane", "name": "web", "weight": "3"},
      {"type": "lane", "name": "mobile"},
      {"type": "group", "name": "forks", "template": {"maxConcurrent": 1}}
    ]}
"""
val parsed = Topology.from(Json.decodeFromString<TopologyDocument>(text))
// Invalid would carry every problem in the file, each with its address, for the admin to fix.
check(parsed is TopologyParse.Valid)
val farm = parsed.topology
val policy = LocalPolicy(defaultReservation = 3L)
// The epoch is a boot counter you persist and raise on every start.
val (heddle, booted) = LocalHeddle.fromTopology(farm, policy, epoch = 0L)
check(booted.refusals.isEmpty())
heddle.reconcileCapacity(totalSlots = 4L)

// A contributor opens a fork while the program runs.
check(heddle.attach(listOf("forks"), "ana", generation = 1L).refusals.isEmpty())
val web = farm.groupId("web")
val mobile = farm.groupId("mobile")
val ana = farm.attachedChildId(listOf("forks"), "ana")

// Each free slot: lease it, run the work, charge what it really cost.
val queued = setOf(web, mobile, ana)
val first = heddle.acquire(waiting = queued, eligible = queued, startedAt = 0L)
check(first is AcquireOutcome.Acquired)
check(heddle.complete(first.lease.id, actualCost = 4L, waiting = emptySet()) is LeaseOutcome.Done)
val running = (heddle.acquire(queued, queued, startedAt = 4L) as AcquireOutcome.Acquired).lease
val lost = (heddle.acquire(queued, queued, startedAt = 4L) as AcquireOutcome.Acquired).lease

// Under the scheduling lock, persist. Encode and store the text exactly as it comes.
val stored = Json.encodeToString(LocalHeddleSnapshot.serializer(), heddle.persist())

// After the restart: decode, restore under the raised counter, and settle what did not survive.
val decoded = LocalHeddle.decode(stored, Json)
check(decoded is RestoreResult.Restored)
val restored = LocalHeddle.restore(decoded.snapshot, policy, epoch = 1L)
val orphan = restored.reconcileLeases(live = setOf(running.id), now = 10L).orphaned.single()
check(orphan.lease == lost)
check(restored.complete(orphan.lease.id, orphan.defaultSettlement, waiting = emptySet()) == LeaseOutcome.Done(charged = 3L, overrun = 0L, unrecorded = 0L))
check(restored.complete(running.id, actualCost = 3L, waiting = emptySet()) == LeaseOutcome.Done(charged = 3L, overrun = 0L, unrecorded = 0L))
val tree = draft("farm") {
    group("builds") {
        lane("fast")
        lane("fine", Weight.of(1, 30_720))
    }
    lane("docs")
}
val (heddle, booted) = LocalHeddle.fromTopology(tree, LocalPolicy(defaultReservation = 3L), epoch = 0L)
val left = setOf(tree.groupId("builds"), tree.groupId("builds", "fast"), tree.groupId("builds", "fine"))
check(booted.refusals.map { it.child }.toSet() == left)
check(booted.refusals.all { it is StepRefusal.Refused })

// The rest of the tree runs.
heddle.reconcileCapacity(totalSlots = 1L)
val docs = tree.groupId("docs")
val lease = (heddle.acquire(setOf(docs), setOf(docs), startedAt = 0L) as AcquireOutcome.Acquired).lease
heddle.complete(lease.id, actualCost = 3L, waiting = emptySet())

// A tree whose weights fit brings `builds` back, as any reload would.
val fits = draft("farm") {
    group("builds") {
        lane("fast")
        lane("fine", Weight.of(1, 30_000))
    }
    lane("docs")
}
check(heddle.apply(fits, generation = 1L).refusals.isEmpty())
val fast = fits.groupId("builds", "fast")
check(heddle.acquire(setOf(fast), setOf(fast), startedAt = 1L) is AcquireOutcome.Acquired)