Skip to content

feat(hosting): let a finished job free its slot before its outcome write with UnwrittenOutcomeSlots - #47

Merged
pdevito3 merged 36 commits into
mainfrom
feat/unwritten-outcome-bound
Nov 27, 2026
Merged

pdevito3 merged 36 commits into
mainfrom
feat/unwritten-outcome-bound

Conversation

@pdevito3

Copy link
Copy Markdown
Contributor

Summary

Adds the opt-in WorkerGroupOptions.UnwrittenOutcomeSlots (B, default 0). With B > 0, a finished job frees its slot before its outcome write lands, so the Pump can claim new work while up to B outcomes wait in the buffer or in the write. The job keeps its Lease until the write lands, so At-Least-Once Execution and Concurrency Limits hold. At most PoolSize handlers run.

Part of issue 0296 (BackWave on Postgres against Acta). Design: ADR 0062 in backwave-admin.

Measured (local mode, M1 Max, indicative): Postgres drain of no-op jobs, B = 40 against B = 0, median +27.5% (rounds +24.7% to +33.0%).

Costs, all with B > 0 only

  • After a crash or a store outage, up to PoolSize + B jobs per Pump run again, against PoolSize at B = 0.
  • While outcome writes fail, the B extra slots close, so a failing store claims as today.
  • A Wake-Up Hint that arrives during a claim can wait up to one PollInterval while slots are free, until the jobs of that claim finish. A rule that paid the poll sooner cut the gain to about +12%, so this one keeps the wait.

Behavior at B = 0

The simulator dump at B = 0 is byte-identical to main. One small difference: a heartbeat result for a job that already finished is no longer acted on (no abandon or cancel for a job the node does not run). This applies at every B.

What else changes

  • New metric backwave.worker.outcomes.unwritten, also emitted by the deterministic test harness.
  • CONTEXT.md gets the term Unwritten Outcome.
  • The Driver answers each outcome write as landed, faulted, or never issued.
  • The simulator draws B and checks both per-node caps, including under store faults. A sabotaged Driver proves the check fails.

Review

Three review rounds (bugs and code quality). Round 3 had no M or L findings. A throwaway fuzzer drove the real NodeDriver through 60,000 random event orders with 0 violations of Running + Unwritten <= PoolSize + B.

Tests

Release build 0 warnings. BackWave.Tests 1029/1029, Hosting.Tests 124/124, Testing.Tests 30/30. Each commit of the round 3 fixes was built and tested on its own.

Site docs: backwave-site branch docs/0296-outcome-slots-and-auto-prepare, to merge after this PR and the Postgres round-trip PR.

Checklist

  • I was invited to submit this contribution.
  • I have signed the Contributor License Agreement.
  • The change is surgical - it touches only what's necessary.
  • Public members of shipped assemblies have complete XML documentation.
  • I added or updated tests, and dotnet test passes.
  • dotnet build is warning-free.

…outcome bound

NodeOptions.UnwrittenOutcomeSlots (B, default 0) gives each node B extra
slots that only a finished job whose outcome write has not answered can
hold. Free slots are PoolSize - Running - max(0, Unwritten - B), so B = 0
is the old PoolSize - LeasedSlots. A finish re-polls once when it raises
the free slots to a full claim from below. A write that answers with no
rows closes the headroom until a write applies rows again. With B > 0 a
heartbeat also renews the buffered outcomes, never the write in flight.
…nwritten-outcomes metric

The option passes to the Driver, defaults to 0, and AddWorkerGroup rejects
a negative value. Each pump reports its Unwritten count after every Driver
step on the new backwave.worker.outcomes.unwritten up-down counter, tagged
by consumer group, and takes it back to zero when the pump ends.
…-node caps

SimulationOptions.UnwrittenOutcomeSlots passes to each node's Driver. The
per-node cap now checks two things every step: executions stay at most
PoolSize, and executions plus Unwritten outcomes (buffered plus the rows of
the write in flight whose job is not running) at most PoolSize plus the
bound. A new sabotage builds the Driver with unbounded headroom to prove the
second check trips.

The swarm draws the bound, 0 included, for finite-pool regimes from its own
stream, so every other knob of every seed is unchanged. Confine clamps it to
0..PoolSize and the config mutator toggles it. A new coverage Situation
records a claim sized into the headroom.
… bound

With UnwrittenOutcomeSlots 1 and PoolSize 1, a second job runs while the
first job's write is held and a third waits. The unwritten metric reaches 2
and returns to 0 after the stop. A negative bound is rejected.
…r issued

The Driver read an empty result list as a faulted write, but the Shell also
sends one for a batch it never issued, so a never-issued batch closed the
unwritten-outcome headroom. Two new events, OutcomeBatchFaulted and
OutcomeBatchNotIssued, make the answer explicit. Only a faulted write closes
the headroom now.
The outcome buffer, the rows of the write in flight, the deferred flush, the
fault flag, and the configured headroom were five loose fields that Finish,
the flush, the write answers, and the slot count each kept in step by hand.
UnwrittenOutcomes now owns them, and the free slots read
PoolSize - Running - SlotsHeld.
… doc

Finish and the waiting-pass guard each spelled out "the free slots reach a
full claim" by hand. CanTakeFullClaim names it, Finish builds one command
list instead of a four-way ternary, and the guard that claimed a finish will
re-poll is renamed RunningJobsCanFreeAFullClaim, which is what it checks.
The free-slots summary now sits on FreeSlots instead of stacked on the guard.
…e their slots back

With the headroom open, a claim pass that landed dropped the polls that waited on it whenever any job ran,
so a hint that arrived during an empty claim, or a short claim beside a long job, waited for the poll tick
while slots were free. An empty claim now chains its pass at once. A claim that returned jobs keeps the
waiting polls on those jobs and re-polls once when the last of them finishes or loses its Lease, even below
a full claim.
…runs

With the headroom open, a heartbeat renews the buffered outcomes as well, so a lost Lease or a cancel request
on a buffered row emitted AbandonExecution or SignalCancellation for a job that had already finished. The
same holds for a job that finished after the heartbeat went out. Those results are now skipped.
…store faults

The swarm draws a headroom only where it removes store faults, so the closed-headroom path ran in no
simulation. A directed test now runs a headroom under store faults and lane latency, and the per-node-cap
oracle checks each claim issued after a faulted write: it must not reach into the headroom. A sabotage that
answers the fault as a write never issued keeps the headroom open, and the oracle fails the run. The claim
check the oracle and the headroom counter share is one helper.
… headroom

The heartbeat's lease list now reads the configured headroom from the Unwritten outcomes and lists the
running jobs and the buffered outcomes as one union. With no headroom it lists the running jobs only, as
before.
…e Unwritten outcomes

Both places that step the Driver followed each step with the same metric update by hand. They now call one
Step method that does both, and the note on when the pump updates the gauge moves from the instrument to it.
…serting no claim

The check that a full headroom takes no third job slept for twenty poll intervals. It now runs maintenance on
every poll and waits until the store has seen two more lease-expiry sweeps after the enqueue, so at least one
poll ran after the third job was due, then asserts that no claim reached the store.
A Weighted pass sized every per-Queue batch from the free slots when it
started. A faulted outcome write between two batches closes the headroom,
but the next batch still claimed its planned size, so the running jobs plus
the Unwritten outcomes went past PoolSize while the headroom was closed.
Each later batch now claims at most the free slots, and the pass ends when
none is free. With no headroom the planned size never exceeds the free
slots, so the claims do not change.

A Driver test runs a Weighted pass with a fault between its batches, and a
directed simulation runs every node Weighted under store faults so the
per-node-cap oracle checks the later batches of a pass.
…t frees one

A poll that waited on a claim was kept as a nullable set of the landed
jobs with three meanings, and five sites paid it. It re-polled only when
every landed job was done, so a hint waited for the poll tick while slots
were free, and a claim that landed with slots free deferred instead of
claiming them.

A claim that lands under a waiting poll now chains a pass for the slots
it left, with or without headroom, as it does with none. Only when no slot
is free does a node with a headroom keep the poll, as one flag. Step pays
it in one place: after the first event that leaves a slot free it adds one
re-poll unless the event already re-polls, and a claim pass that starts
first clears it. NodeEvent now exposes Now for that re-poll.

The short-claim tests change with it: a short claim now chains a pass of
2 with headroom too, and the pool-full tests re-poll on the first finish
or the first lost Lease, below a full claim.
Stepping OutcomeBatchFaulted could free a slot, when the dropped rows
held one past the headroom, and so pay a waiting poll as a re-poll. The
production pump re-throws the write's store fault before it runs any
command of that step, so the re-poll was lost there while the simulator
ran it. A faulted write now never pays the poll, and the next event
after which a slot is free pays it in both.
… they now work

The poll comment spoke of a buffered outcome over-admitting past PoolSize,
and the deterministic pump said it lands an empty answer for the batch that
threw. Both now describe the free slots with the headroom, and the faulted
and never-issued write answers. The UnwrittenOutcomeSlots doc uses Lease and
Concurrency Limit.
…r issued keeps the headroom

Any answered write reopens the headroom, an all-stale one included, so the
faulted-write test is now named for a write that lands. A new test mirrors
it with a write never issued, which drops its rows and keeps the headroom
open.
The host builder picked between two option values for a maintenance interval.
It now sets MaintenanceInterval in the initializer, with the documented
5 second default when the test passes none.
…jobs again

Paying a waiting poll on the first event that frees a slot cut the noop
drain gain at B = 40 from about +31% to about +12%: each early re-poll
claims only the slots the landed jobs left, which brings back the
two-cohort split the headroom removes.

A claim that lands under a waiting poll again chains a pass at once when
it returned no jobs, and with the headroom open and jobs returned below a
full claim, leaves the poll waiting on those jobs. The state is the fixed
set of those job ids, or null when no poll waits. Step still pays it in
one place, as one re-poll after the first event that finds none of those
jobs running and a slot free, never on a faulted write. With no headroom
nothing waits, so B = 0 is unchanged.

The short-claim tests go back to this rule: with headroom a short claim
chains no pass, and the poll is paid after the last landed job finishes
or loses its Lease, once. The cost is documented on UnwrittenOutcomeSlots:
a Wake-Up Hint that arrives during a claim can wait up to one PollInterval
while slots are free.
…ng on jobs

The rule that leaves a waiting poll on a landed claim's jobs had three
conditions with no test of their own. Each new test fails when its
condition is removed from NodeDriver:

- A fault that closes the headroom stops the rule. With a full pool the
  poll is dropped, and a slot that those jobs free later pays nothing.
- A short claim that still leaves a full claim free chains a pass at once.
- A claim pass that starts first pays the poll, so the jobs it waited on
  re-poll nothing when they finish.

A Weighted test pins today's behavior when the pass cuts a later batch to
the free slots: the poll waits on the jobs of the last batch only.

The faulted-write test is renamed, because the poll now waits on jobs,
not for a slot.
@pdevito3
pdevito3 merged commit e2f3e66 into main Nov 27, 2026
2 checks passed
@pdevito3
pdevito3 deleted the feat/unwritten-outcome-bound branch November 27, 2026 05:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant