Skip to content

fix: close billing and batch shutdown races - #49

Merged
CMGS merged 6 commits into
mainfrom
codex/fix-billing-drain-closure
Sep 3, 2026
Merged

fix: close billing and batch shutdown races#49
CMGS merged 6 commits into
mainfrom
codex/fix-billing-drain-closure

Conversation

@CMGS

@CMGS CMGS commented Sep 3, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • keep attributed ledger writes batched while waiting for the shared-store commit before request completion, so per-user usage reads on another Gateway instance observe completed Instar calls
  • stop distributed batch claiming when shutdown begins, finish the claimed batch, and flush billing only after the drain task exits
  • cover the attributed repair acknowledgement path

Verification

  • static source review and cargo fmt --all
  • lint, tests, and compilation were not run locally; CI owns those gates for this review

Hot path

Attributed requests (a user id on the request) now wait for one batched ledger commit before completing; unattributed requests are unchanged. Measured on the testbed (pg, pool 40, oha c=256, 3 interleaved reps, 5d44be7 vs 628c802): attributed small rps 52152 -> 60873 (+16.7%), p99 11.31 -> 5.47 ms; attributed big rps 28802 -> 30815 (+7.0%), p99 21.37 -> 11.10 ms; unattributed unchanged. Under saturation the old try_send path fell through to one single-row insert per request, so the batched wait is cheaper; on an idle gateway a single attributed request pays the batch commit (0.87 ms vs 0.17 ms). With the store down the wait is bounded by the ledger worker retry schedule (8 attempts, 100 ms doubling to 30 s) plus one direct write.

Follow-ups on the branch

  • 5d44be7 the row ack carries the commit result; on a dropped batch the writer makes one direct write and only then hands the row to the best-effort repair queue; the three shutdown selects in drain_until fold into two helpers.
  • d87a394 tests for the attributed fallback (paused tokio clock) and the drain shutdown path.
  • 5fe45b4 only unacknowledged rows count as dropped.

CMGS added 6 commits September 3, 2026 16:27
The row ack fired after every batch attempt, so an attributed request returned confirmed even when the batch had been dropped after its retries. The ack now carries committed; on false the writer makes one direct ledger_add and only then hands the row to the repair queue, which stays best-effort for every row. The three shutdown selects in drain_until fold into stopping and pause_or_stop.

Hot path: an attributed request now waits for one batched ledger commit (shared with concurrent rows) before it completes; unattributed rows are unchanged. With the store down the wait is bounded by the worker's retry schedule.
The fallback test runs under a paused tokio clock so the worker's retry backoff completes instantly; the drain test now flips the shutdown watch and requires the loop to exit within two seconds instead of aborting it.
An attributed row whose batch exhausted its retries is written directly by its caller, so counting it as dropped made the shutdown flush report billing loss that did not happen.
01e6087 switched the mock transport's reply encoding from v.to_string() to serde_json::to_vec. A per-commit bisect of PR #48 on the testbed put the whole +88k instructions per 48 KB request of the big-body regression on that line (p3 4,039k vs p4 4,127k instr/req; restoring the file returns to 4,039k), while the four production groups of that commit run the same instruction stream within 0.01%. The mock transport is benchmark and demo code; production uses the HTTP transport. The remaining rps spread between builds is code placement, not added work.
@CMGS
CMGS merged commit 4e78c36 into main Sep 3, 2026
2 checks passed
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