restore
An engine that carries on from snapshot, issuing new lease ids under epoch.
Every lease open in snapshot is open again under its own id, from whatever epoch issued it, and charges down the path it was delegated on. The wake memory and the capacity come back too, so the next picks and top-ups are the ones the persisted engine would have made, and so does the topology it last applied, so it leases exactly the groups that one did. policy is taken afresh: pass the one the persisted engine ran with for that to hold.
epoch must be above snapshot's. Treat it as a boot counter you persist and only ever raise: lease ids are numbered from zero in each epoch, so restoring under an epoch an earlier run already used could hand out an id that run handed out too. An older lease id that is no longer open is then answered LeaseOutcome.Stale, never LeaseOutcome.Unknown.
Throws
when epoch is not above LocalHeddleSnapshot.epoch, or when policy cannot size LocalHeddleSnapshot.totalSlots (see reconcileCapacity).
Samples
val tree = twoLanes()
val heavy = tree.groupId("heavy")
val light = tree.groupId("light")
val policy = LocalPolicy(defaultReservation = 3L)
val (heddle, booted) = LocalHeddle.fromTopology(tree, policy, epoch = 0L)
check(booted.refusals.isEmpty())
heddle.reconcileCapacity(totalSlots = 2L)
val queued = setOf(heavy, light)
val running = (heddle.acquire(queued, queued, startedAt = 100L) as AcquireOutcome.Acquired).lease
// Under your scheduling lock: take the snapshot. Encoding and storing it can happen off the lock.
val stored = Json.encodeToString(LocalHeddleSnapshot.serializer(), heddle.persist())
// After the restart: decode, then restore under a later epoch, your persisted boot counter.
val decoded = LocalHeddle.decode(stored, Json)
check(decoded is RestoreResult.Restored)
val restarted = LocalHeddle.restore(decoded.snapshot, policy, epoch = decoded.snapshot.epoch + 1)
// The lease that was running is still open, and settles as it would have.
check(restarted.complete(running.id, actualCost = 3L, waiting = queued) == LeaseOutcome.Done(charged = 3L, overrun = 0L, unrecorded = 0L))
// New leases are named under the new epoch, so none can repeat an old id.
check((restarted.acquire(queued, queued, startedAt = 120L) as AcquireOutcome.Acquired).lease.id.epoch == 1L)