Gateway: cut per-event relay hops (staging channel, dashboard mirror, pipe reader, gRPC BufferHint) #235

Closed
opened 2026-11-21 08:01:06 -05:00 by dohertj2 · 3 comments
Owner

Follow-up from the worker CPU investigation (240 ev/s on wonder-app-vd03; branch feat/worker-perf-prototype, commit 659bb831). The gateway's .NET 10 PortableThreadPool spin-waits after each per-event work item; each event currently costs ~6 of our pool work items plus 2–3 of Kestrel's (~2.1 spin spells per frame). The spin limit (UnfairSemaphoreSpinLimit=10, in 659bb831) trims the cost; these remove the hops themselves.

Ranked:

  1. Drop the staging hop. AllowSynchronousContinuations=true on _eventStaging and _events, or run mapping + fan-out inline on the read loop (WorkerClient.cs:723,753-768,791). Saves ~2 pool items/event.
  2. Lazy dashboard mirror. Attach the mirror (GatewaySession.cs:656,851, 942-960) only while a viewer exists; consider a per-subscriber predicate at the distributor so the alarm monitor (GatewayAlarmMonitor.cs:285-305) stops waking on every data change. Saves ~1/event.
  3. Buffer the pipe reader (WorkerFrameReader.cs:46,72) with a PipeReader or a 64 KiB pooled buffer — currently two read completions per frame.
  4. Batch gRPC writes. WriteOptions(WriteFlags.BufferHint) while EventStreamService.cs:224 has more buffered, flush on the last. Verify grpc-dotnet BufferHint semantics first.

Measure with the windev bench (C:\build\perfbench\bench-run.ps1, ~242 ev/s): gateway CPU baseline 8.1% of a core, 6.9% with the spin limit. Lower priority: per-event metrics dictionary lookups (GatewayMetrics.cs:253-260), send-latency histogram sampling (MxAccessGatewayService.cs:187-206), and a WorkerEventBatch contract change only if per-frame cost still shows.

Follow-up from the worker CPU investigation (240 ev/s on wonder-app-vd03; branch `feat/worker-perf-prototype`, commit 659bb831). The gateway's .NET 10 PortableThreadPool spin-waits after each per-event work item; each event currently costs ~6 of our pool work items plus 2–3 of Kestrel's (~2.1 spin spells per frame). The spin limit (`UnfairSemaphoreSpinLimit=10`, in 659bb831) trims the cost; these remove the hops themselves. Ranked: 1. **Drop the staging hop.** `AllowSynchronousContinuations=true` on `_eventStaging` and `_events`, or run mapping + fan-out inline on the read loop (`WorkerClient.cs:723,753-768,791`). Saves ~2 pool items/event. 2. **Lazy dashboard mirror.** Attach the mirror (`GatewaySession.cs:656,851`, `942-960`) only while a viewer exists; consider a per-subscriber predicate at the distributor so the alarm monitor (`GatewayAlarmMonitor.cs:285-305`) stops waking on every data change. Saves ~1/event. 3. **Buffer the pipe reader** (`WorkerFrameReader.cs:46,72`) with a `PipeReader` or a 64 KiB pooled buffer — currently two read completions per frame. 4. **Batch gRPC writes.** `WriteOptions(WriteFlags.BufferHint)` while `EventStreamService.cs:224` has more buffered, flush on the last. Verify grpc-dotnet `BufferHint` semantics first. Measure with the windev bench (`C:\build\perfbench\bench-run.ps1`, ~242 ev/s): gateway CPU baseline 8.1% of a core, 6.9% with the spin limit. Lower priority: per-event metrics dictionary lookups (`GatewayMetrics.cs:253-260`), send-latency histogram sampling (`MxAccessGatewayService.cs:187-206`), and a `WorkerEventBatch` contract change only if per-frame cost still shows.
dohertj2 added the area:gatewaypriority:p2type:followup labels 2026-11-21 08:01:06 -05:00
Author
Owner

Implemented, not merged or deployed. Branch feat/gateway-relay-hops, three commits stacked on R1 (feat/worker-perf-prototype):

  1. Staging hop removed. The worker event channels run their continuations synchronously, so an event goes from the pipe read straight into the per-subscriber channels on one thread instead of taking two pool hops. Ordering, both overflow faults, the 3× buffering bound, replay and fan-out are unchanged. Subscriber channels still hand their consumers to the pool, so no gRPC, SignalR or alarm code runs on the read loop. The new rule ("a ReadEventsAsync reader must never block synchronously") is documented in IWorkerClient and in docs/GatewayProcessDesign.md ("Event path threading").
  2. Lazy dashboard mirror plus alarm-monitor filter. The mirror is attached only while the session has a dashboard viewer (IDashboardViewerPresence). Internal subscribers can pass a filter, so the alarm monitor no longer wakes on data changes. ACL and redaction are unchanged.
  3. 64 KiB read-ahead pipe reader. One read usually serves one or more whole frames. Size checks still run on the prefix before any allocation, and an EOF anywhere is still EndOfStream.
  4. gRPC BufferHint: skipped. The semantics work in grpc-dotnet, but at ~4 ms event spacing the next event is almost never waiting, and doing it safely needs manual flushes (for example on resume-filtered events).

Tests: full gateway suite on macOS 1478 passed / 0 failed / 6 skipped; affected tests re-run on the combined R1+R2 stack 616/616.
windev bench (~242 ev/s, dashboard off, spin limit on in both): gateway 7.26% → 5.99% of a core (−17.5%), context switches about −⅓. Item 2's saving isn't in that number.
Possible pre-existing flake: EventStreamServiceTests.StreamEventsAsync_WhenWorkerFaultsUnderTwoSubscribers_CountsTheFaultOnce failed once in a large filtered run (the pump's readiness check lost a race with the fault) and passed on six reruns.

Rollout: this is R2 in docs/plans/2026-11-21-worker-perf-rollout.md. It deploys to vd03 after R1 has soaked; see #236.

**Implemented, not merged or deployed.** Branch `feat/gateway-relay-hops`, three commits stacked on R1 (`feat/worker-perf-prototype`): 1. **Staging hop removed.** The worker event channels run their continuations synchronously, so an event goes from the pipe read straight into the per-subscriber channels on one thread instead of taking two pool hops. Ordering, both overflow faults, the 3× buffering bound, replay and fan-out are unchanged. Subscriber channels still hand their consumers to the pool, so no gRPC, SignalR or alarm code runs on the read loop. The new rule ("a `ReadEventsAsync` reader must never block synchronously") is documented in `IWorkerClient` and in `docs/GatewayProcessDesign.md` ("Event path threading"). 2. **Lazy dashboard mirror plus alarm-monitor filter.** The mirror is attached only while the session has a dashboard viewer (`IDashboardViewerPresence`). Internal subscribers can pass a filter, so the alarm monitor no longer wakes on data changes. ACL and redaction are unchanged. 3. **64 KiB read-ahead pipe reader.** One read usually serves one or more whole frames. Size checks still run on the prefix before any allocation, and an EOF anywhere is still `EndOfStream`. 4. **gRPC `BufferHint`: skipped.** The semantics work in grpc-dotnet, but at ~4 ms event spacing the next event is almost never waiting, and doing it safely needs manual flushes (for example on resume-filtered events). **Tests:** full gateway suite on macOS 1478 passed / 0 failed / 6 skipped; affected tests re-run on the combined R1+R2 stack 616/616. **windev bench** (~242 ev/s, dashboard off, spin limit on in both): gateway 7.26% → 5.99% of a core (−17.5%), context switches about −⅓. Item 2's saving isn't in that number. **Possible pre-existing flake:** `EventStreamServiceTests.StreamEventsAsync_WhenWorkerFaultsUnderTwoSubscribers_CountsTheFaultOnce` failed once in a large filtered run (the pump's readiness check lost a race with the fault) and passed on six reruns. Rollout: this is **R2** in `docs/plans/2026-11-21-worker-perf-rollout.md`. It deploys to vd03 after R1 has soaked; see #236.
Author
Owner

The 2026-12-02 bug + perf review (MXA-002 / WIP-002) confirmed that none of the event-path CPU fixes are on main at 54367e74. They live only on feat/worker-perf-prototype → feat/gateway-relay-hops → fix/readbulk-subscription-teardown, each 2 commits behind main. Landing them is Phase 1 of the fix plan: #252.

The 2026-12-02 bug + perf review (MXA-002 / WIP-002) confirmed that none of the event-path CPU fixes are on `main` at `54367e74`. They live only on `feat/worker-perf-prototype` → `feat/gateway-relay-hops` → `fix/readbulk-subscription-teardown`, each 2 commits behind main. Landing them is Phase 1 of the fix plan: #252.
Author
Owner

Merged to main at 68043411 (pushed 2026-12-03). Gateway suite 1610 passed / 0 failed (macOS); windev worker CI 836 passed / 0 failed on that SHA.

The relay-hop commits landed with Phase 1 (#252). Deployment state is tracked on #236.

Merged to `main` at `68043411` (pushed 2026-12-03). Gateway suite 1610 passed / 0 failed (macOS); windev worker CI 836 passed / 0 failed on that SHA. The relay-hop commits landed with Phase 1 (#252). Deployment state is tracked on #236.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: dohertj2/mxaccessgw#235