LocalHeddleSnapshot

@Serializable(with = LocalHeddleSnapshotSerializer::class)
class LocalHeddleSnapshot

Everything a LocalHeddle needs to carry on after a restart, as one value you can encode, as JSON or CBOR, and store: the books, the open leases, how many slots the engine was sized for, what the fair-share allocator remembers, and the topology it last applied. Take one with LocalHeddle.persist, and start an engine from it with LocalHeddle.restore.

A snapshot that exists is consistent. The engine is the only thing that builds one, and decoding one runs the same check, so a snapshot that breaks any of these never decodes: LocalHeddle.decode reports it as RestoreResult.Corrupt.

  • Each lease names only edges the ledger has, down a path that leads from root to the lease's leaf across no retired edge, and none is from a later epoch than epoch.

  • Each edge is charged, returned plus spent, no more than was issued across it, and its effective spend is not below zero.

  • Each group holds, for each writer, no less than nothing.

  • What the leaves have spent, forgotten edges included, and what every group holds come to no more than was ever minted.

  • The open leases across an edge reserve no more than is outstanding on it.

  • Each parent's active children's weights fit the weight budget LocalHeddle.apply keeps.

  • Every total the engine reads stays within Long.

Each is a bound the engine keeps after every call, and none is a fixed cap: a lease that LocalHeddle.extend has grown past LocalHeddle.MAX_RESERVATION is one an engine writes, so it restores.

What is kept, and why

  • The open leases, each with its own id, its path and its startedAt. A restored engine keeps answering the ids it handed out before the restart, charges each lease down the very path it was delegated on, and still knows when each one started, so LocalHeddle.reconcileLeases can say how long an orphan ran.

  • The allocator's wake memory (AllocatorMemory). Without it a group that went idle just before the restart and is busy just after would keep the idle credit the wake clamp exists to deny, and the next picks would not be the ones this engine would have made.

  • The capacity last reconciled, totalSlots. So a restored engine tops up its own supply from its first operation, exactly as the one it replaces did. Calling LocalHeddle.reconcileCapacity again after a restore is harmless.

  • The topology last applied, lastApplied. Only the topology tells a lane from a group that has nothing attached right now, such as one with a template before its first child is attached: both read as leaves in the ledger. A restored engine leases exactly what the persisted one did, and LocalHeddle.attach and a later LocalHeddle.apply diff against it. It is the topology of the last LocalHeddle.apply, even one with refusals: applying it again after a restore lands what was left.

What is not kept: the policy, which LocalHeddle.restore takes afresh, and the counter lease ids are numbered from. A restored engine runs under a later epoch, and lease ids are scoped by epoch, so it counts from zero again without reusing an id.

The encoded form

Encoded, a snapshot is a two-element sequence, [formatVersion, state]: a JSON array, a CBOR array. It is a sequence and not an object so that a store which reorders an object's keys, as Postgres jsonb does, cannot put state ahead of formatVersion. JSON and CBOR are both checked. ProtoBuf is not: kotlinx ProtoBuf writes an EntitlementLedger but cannot read it back, because it fails on the ledger's maps whose values are maps, such as transfers. formatVersion is FORMAT_VERSION, 1, for every snapshot this release writes, and it is the only format this release reads (SUPPORTED_FORMAT_VERSIONS). LocalHeddle.decode reads formatVersion before it reads anything else, so bytes in a format this release does not read are refused by name, as RestoreResult.UnsupportedVersion, rather than half-read: format 2, which only development builds of 0.7.5 wrote, is one of them. A snapshot v0.7.4 wrote also says format 1, in a layout this release does not read, and is not told apart: it fails to decode, or misreads, so a consumer coming from v0.7.4 starts from its topology again rather than restoring (#3489).

Equal snapshots encode to equal bytes: every map inside goes through a canonical serializer and every list is in id order.

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)

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard

the fair-share allocator's wake memory.

Link copied to clipboard
val epoch: Long

the epoch of the engine that took this snapshot. A restore must run under a later one.

Link copied to clipboard

the topology the engine last applied: the one it booted from, or the last one passed to LocalHeddle.apply.

Link copied to clipboard

Every open lease, in LeaseId order.

Link copied to clipboard

the books: every edge, every grant, every charge.

Link copied to clipboard

The tree's root, where supply is minted and every pick starts: lastApplied's, since a reload never changes the root. It is not stored separately, so the two cannot disagree.

Link copied to clipboard

the capacity last given to LocalHeddle.reconcileCapacity, 0 when none was.

Functions

Link copied to clipboard
open operator override fun equals(other: Any?): Boolean
Link copied to clipboard
open override fun hashCode(): Int
Link copied to clipboard
fun previewApply(policy: LocalPolicy, new: Topology, generation: Long): TopologyPreview

What LocalHeddle.apply of new at generation would do to the engine that took this snapshot, with no engine: the same TopologyPreview as LocalHeddle.previewApply on that engine as it stood, but with no TopologyPreview.basis, since a snapshot keeps no revision. Changes nothing, here or anywhere. The leases it holds move as that engine's would, whatever epoch opened them.

Link copied to clipboard
open override fun toString(): String
Link copied to clipboard

Ask this snapshot what the engine that took it would answer, with no engine: who goes next, why a leaf is waiting, what share it is owed, and what share every group is owed (see LocalHeddleView). Changes nothing, here or anywhere.