Embedding Heddle
A CI service runs jobs for many teams on one pool of runners. Paying teams should get more of the pool than free ones, a team must not grab every runner at once, and only some teams can use the Mac runners. This page wires Heddle into that service, one call site at a time.
Heddle does not replace your queue, your runners or your database. It answers one question when a runner frees up, "whose job?", and keeps the books that the next answer depends on. Everything below is one compiled sample, sampleRunnerPool, quoted in order, and every result shown is a check in that sample.
Draw the pool
Teams sign up while the service runs, so the tree has two tiers with a template each, rather than a fixed list of teams:
The weights split the pool between tiers; inside a tier, every team gets the template's equal weight. The ceiling, maxConcurrent, caps each team on its own. reconcileCapacity tells the engine how many runners exist; call it again when the pool grows or shrinks. An admin-edited file works too: decode a TopologyDocument and pass it to Topology.from, which lists every problem with its address. Fair share in one program covers both forms.
epoch is a boot counter. Keep it in your database and raise it on every start of the service. Survive a restart shows why.
Sign teams up
attach adds a team under its tier's template. A new team starts level with the teams already running, not owed a backlog. detach removes one: it takes no new jobs at once, and retires when its running jobs finish. The engine raises generation past every one its books hold, so a removed team's id is never reused.
Two call sites
The whole integration is two functions your service calls. One runs when a runner frees up, the other when a job finishes:
Two sets steer each pick. queued (the waiting parameter) is every team with a job in your queue. eligible is the teams this particular runner can serve. Keep them apart: a team that is queued but cannot use this runner keeps its place in line, and is not treated as idle.
The lease names the team. Take that team's oldest job from your queue and run it on the runner. Keep the lease id beside the job in your database, so that a finished job can be completed and a crashed one can be found.
A Mac runner, and a ceiling
The third Mac runner gets nothing: acquire answers NothingEligible, because the only team it could serve is at its ceiling. explain says so in a value your status page can turn into words. Explaining a wait lists every answer.
A job that runs long
complete is never refused: work that already ran is charged in full, unless Done.unrecorded says otherwise. Guessing low cannot dodge the charge, though it can win a turn early, as How fair is it? explains. A job that finishes early is charged what it used, and the rest goes back.
Does the split hold?
The sample checks the exact split: 151 jobs for acme, 148 for apps and 101 for hobby. The paid tier took 299 of 400 against an ideal of 300. Inside it, apps ran fewer Linux jobs than acme because its Mac minutes were already on its books. Share is counted in time across every runner, not in jobs on one.
Survive a restart
Two kinds of restart reach a CI service. A runner host can die and take its jobs with it, and the service itself can restart.
A runner host dies. List the lease ids of the jobs still running and pass them to reconcileLeases. It reports orphans, leases whose work is gone, and leases it has never heard of. It changes nothing; you settle each orphan with complete, usually at its defaultSettlement: the time it ran, capped at what it was granted.
The service restarts. Under your scheduling lock, persist the engine and store the encoded text exactly as it comes. On start, decode it and restore under a raised boot counter. This is the same loop from sampleFairShareQueue, which also starts from an admin's config file:
Given the same policy, the restored engine picks exactly as the old one would have. Open leases come back under their old ids, so jobs that kept running complete as normal. An id from before the restart that is no longer open answers Stale, not Unknown. The saved form is a JSON or CBOR array, [formatVersion, state], frozen at format 1 in this release. When the engine restarts too gives the rules and what decode checks.
The checklist
Before you ship it, check each of these against your service.
One caller at a time.
LocalHeddleis not thread-safe and takes no locks of its own. Call it under the lock your scheduler already holds.snapshot()returns an immutable view you can hand to another thread.Pass time in. Heddle never reads a clock.
startedAt,nowand every cost are numbers you supply, in one unit. Minutes or seconds both work.eligiblemust be insidewaiting. OtherwiseacquirethrowsIllegalArgumentException. An exception here means a caller's mistake.Refusals are values.
SupplyExhausted,NothingEligible,NoLiveLineageandLifetimeExhaustedfromacquirechange nothing except, for the last two, the engine's memory of who was waiting. Leave the job queued and try on the next free runner.Work started elsewhere is charged with
admit, which makes no pick and is never refused for supply or a ceiling.Estimates matter. Set
defaultReservationat or a little below a typical job, and passreservationwhen you know better, such as the team's last run time. How fair is it? shows why.Keep the boot counter durable. Two engines under one epoch could hand out the same lease id.
restorethrows on an epoch that is not higher.
Limits
The engine counts in 64-bit whole numbers and refuses rather than wraps.
One reservation or extension is at most
LocalHeddle.MAX_RESERVATION, 2^44 units. An overrun charged atcompleteis not bound by it.The books fill up, eventually. Two lanes of the largest leases fill a counter after about half a million leases. Before that,
acquireanswersLifetimeExhaustedand keeps the remaining room for work already running.admit,extendandcompleteare never refused for it.A charge that still does not fit is recorded whole or not at all, and
LeaseOutcome.Done.unrecordedsays how much was left out. So does a single overrun aboveLocalHeddle.MAX_RECORDED_OVERRUN, so one wrong cost cannot stop the engine. Sumunrecordedover completions only.reconcileCapacityrefuses a pool whose runners × default estimate × target rounds would passLong.MAX_VALUE / 4.
Running it in a real program gives the exact rules.
One program, or many machines?
Everything above is LocalHeddle: one process makes every pick. That fits a CI service with one scheduler, even a busy one; the measured cost of an acquire is in How fast, how fair.
When the runners schedule themselves, with no central service, use the distributed node in :kuilt-heddle. Each machine spends from its own holdings, and a split network keeps working. Fair share with Heddle starts that story.