persist

Everything a restart needs, as one value to encode and store: see LocalHeddleSnapshot. Like snapshot, it is taken under the caller's scheduling lock, since it reads the engine's own maps; the value it returns is immutable, so encoding and writing it can happen off the lock.

Costs a sort of the open leases and of the wake memory, and a walk of the whole ledger: O(edges + groups × writers + the edges of every open lease's path). The walk is the check LocalHeddle.decode runs, that the books balance (see LocalHeddleSnapshot), so what this returns always decodes.

Throws

when this engine's own books break a bound the check holds them to. Every call keeps them, so that is a bug in the engine, never in what the caller passed.

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)