FairShareAllocator

One peer's fair-share scheduler: it hands this peer's holdings at a parent down to the children that want them, one quantum at a time, in the order HeddlePolicy picks (design §7.3).

It needs no network. Give it a LedgerCell and the current demand, and schedule runs allocation rounds until nothing more can move. HeddleNode is this plus replication, a demand board and reservations.

What it remembers between calls

The §10.6 wake clamps this peer has computed, and the demanding state each was derived from (design §7.2; issue #1695). Beside them, each parent's net-service clock, and where it stood when this peer saw each child stop asking, which is what moves a child that comes back (#3376). All of it is scheduler-local: never replicated, never in the ledger. A divergent offset reorders one peer's grants but can never create entitlement.

The demanding state is what makes the clamp an edge detector rather than a level one: false means this peer has seen the child not competing, so the next round in which it competes is a wake. An id that is absent has never been observed, and a seated one found competing is a wake too, the restart rule (design §7.2, #3472): a first sighting says nothing about how long the child idled, so this peer clamps it to the front, at the cost of its lag, once. That is every child of a restarted HeddleNode, and a child another peer seated and ran. Seating a genuinely new generation is the seat bump's job (schedule), not the clamp's.

Two histories a first sighting cannot tell apart

A clamped waker lands past the front by what its edge still has outstanding (#3721), so the grants it holds stay counted as its service. What a first sighting cannot know is what the child's siblings were doing when those grants were made. Take a child at L + 8 holding 8, the front, and a sibling at L holding nothing, both first seen asking. That ledger arises two ways:

  • The sibling was competing when the 8 was granted. An allocator that watched leaves a gap of 8.

  • The sibling was idle when it was granted. An allocator that watched leaves a gap of 0.

This one watched neither, and lands them 8 apart: the front child past itself by its 8, the sibling at the front. That is right for the first history. In the second, the idle sibling keeps up to the front child's outstanding of lag it should have forfeited. Clamping the front child by less would credit the holder its unspent grants instead, the #3238 defect, and is ruled out (#3721). So a clamp still never lowers a position, and forgives a waker only history, but it can leave a sibling lag that kept memory would have taken: at most the outstanding of the child it is clamped beside.

That memory is a value too: memory exports it as an AllocatorMemory and the secondary constructor takes one back, so a single-process caller can persist it beside its ledger, and can ask "who would go next" of a throwaway allocator built over a copy without moving this one.

Two ways to drive it

  • schedule runs grants until nothing more can move, against one Demand view, and writes them through a LedgerCell. That is the replicated node's shape: it tops children up to a target.

  • pickChild makes one pick on a ledger value, with the seat and wake step reading one view (who is waiting) and the pick reading another (who is eligible for this slot). That is a lease's shape: a slot that only some children can run on must not make the others look idle, or the next time they are eligible they would be clamped as wakers they never were.

Thread-safety

Not thread-safe. That state is plain mutable maps, so one allocator belongs to one peer and is called by one thread at a time. HeddleNode calls it only under its own lock.

Parameters

self

the replica whose holdings this allocator delegates.

policy

the quantum and sleeper credit the policy runs with.

Samples

val root = GroupId("root")
val self = ReplicaId("self")
val heavy = AttachmentRecord(AttachmentId("heavy"), root, GroupId("heavy"), Weight.of(3))
val light = AttachmentRecord(AttachmentId("light"), root, GroupId("light"), Weight.ONE)

// The whole "network": the latest ledger, and one write step that decides on what it lands on.
val ledger = object : LedgerCell {
    override var current = EntitlementLedger.bootstrap(root, mapOf(self to 400L), nonce = "genesis")
    override fun mutateOrSkip(transform: (EntitlementLedger) -> Patch<EntitlementLedger>?): Boolean {
        val patch = transform(current) ?: return false
        current = current.piece(patch.delta)
        return true
    }
}
for (record in listOf(heavy, light)) {
    ledger.mutateOrSkip { it.prepare(record) }
    ledger.mutateOrSkip { it.activate(record.id) }
}

val allocator = FairShareAllocator(self, PolicyConfig(quantum = 10L))
val wantsEverything = Demand(targetOutstanding = 1_000L, maximumUsefulGrant = 10L)
allocator.schedule(root, ledger, earmarked = 0L, demand = { wantsEverything })

check(ledger.current.edge(heavy.id)?.issued == 300L)
check(ledger.current.edge(light.id)?.issued == 100L)

Constructors

Link copied to clipboard
constructor(self: ReplicaId, policy: PolicyConfig)
constructor(self: ReplicaId, policy: PolicyConfig, memory: AllocatorMemory)

An allocator that resumes from memory, as if it were the allocator memory was exported from. Building one over a copy of a live allocator's FairShareAllocator.memory gives a throwaway that picks exactly as the live one would next.

Properties

Link copied to clipboard

What this allocator remembers right now, as an immutable value with its maps in key order. A copy: later calls on this allocator do not change it. Costs one sort of each map, so read it to persist or to preview, not on every pick.

Functions

Link copied to clipboard

How far parent has climbed: the highest effective position among its active children in ledger, each divided by its own position ceiling, the largest position a Long could hold on that child's grid. A plain ratio with no unit, 0.0 at the start and 1.0 at the ceiling.

Link copied to clipboard
fun pickChild(parent: GroupId, ledger: EntitlementLedger, waiting: (AttachmentId) -> Demand, eligible: (AttachmentId) -> Demand): ChildPick?

Make one pick at parent: seat the newborns and clamp the wakers exactly as schedule does, reading waiting, then choose one child among those eligible wants, on the seated state. Nothing is delegated: the caller delegates ChildPick.grant on ChildPick.ledger, which every child of parent is already seated on, so that delegation cannot hand a newborn the lifetime credit an unseated delegation would (#1752).

Link copied to clipboard
fun schedule(parent: GroupId, ledger: LedgerCell, earmarked: Long, demand: (AttachmentId) -> Demand): AllocationOutcome

Run allocation rounds at parent until nothing more can be delegated: build the policy input from the active children, their EdgeSummarys and the current demand, then apply each HeddlePolicy.pick grant to the ledger before the next round (design §7.3). A bad local decision only misplaces entitlement — it can never create any, so partitioned peers may schedule divergently and still converge on heal.

Link copied to clipboard
fun virtualTime(ledger: EntitlementLedger, parent: GroupId, demand: (AttachmentId) -> Demand): Rational?

parent's current virtual time V in ledger — this allocator's front (design §7.2; issue #1688). null when parent has no active children in ledger, and null too when their weights fail weightsFitTheBudget, where no front can be computed and schedule answers AllocationOutcome.WeightBudget (#3353).

Link copied to clipboard

virtualTime as a VirtualTime, exact and never held (design §7.1): the demanding children's weighted mean, or the furthest-ahead child when none is demanding. null as virtualTime answers it, and over a merged load whose sum leaves 128 bits.