Skip to content

Read a controller's agents' heads once per delivery, not three times (CHOO-3667) - #1220

Closed
abeldantas wants to merge 1 commit into
controller-idle-passfrom
stream-pass-once
Closed

abeldantas wants to merge 1 commit into
controller-idle-passfrom
stream-pass-once

Conversation

@abeldantas

Copy link
Copy Markdown
Collaborator

A delivery to an agent behind a controller made the controller's stream read the heads of all its agents, about 20, three times, each in a transaction of its own: to find the work, again after delivering, and again before waiting.

In this PR we:

  • clear the agents' doorbells before the pass's one read of the heads, so an event added after that read rings again and the wait returns at once
  • read again without waiting only when a delivery stopped short of the head (the batch size, or a gap)

Measured together with the rest of this set, in a load test of 3,000 agents behind 150 controllers on 12 pods, rooms of 4, before and after on the same day:

before after
pod CPU per message, 29 msg/s 124 ms 87 ms
pod CPU per message, 58 msg/s 127 ms 98 ms
transactions per message 24.0 19.9
reads of a controller's agents' heads per message 7.5 2.9
delivery p50 at 58 msg/s 497 ms 304 ms

Stack (merge order)

  1. Make idle controller agents and startup cheap on the database as deployments grow (CHOO-3667) #1200
  2. This PR ← you are here
  3. Next: the retry PR stacked on this one

…(CHOO-3667)

Each delivery made the controller's stream read the heads of all its
agents three times, each in its own transaction: to find the work, again
after delivering, and again before waiting. In a load test with 10,000
agents behind controllers that was 9 of the ~50 transactions a message
cost.

The pass now clears its agents' doorbells before its one read of the
heads, so an event added after that read rings again and the wait
returns at once. It reads again without waiting only when a delivery
stopped short of the head (the batch size, or a gap).

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The race-sensitive async/Postgres delivery-loop optimization depends on notification timing around the head snapshot and lacks runtime or load-test verification.

No open issues 🎉

What changed in this PR

This pull request reduces redundant agent-head reads during controller-backed deliveries while preserving prompt wakeups and backlog catch-up behavior.

Changes:

  • Clears doorbells before the consolidated head read.
  • Rechecks heads only when delivery remains behind the captured head.
  • Adds stream coverage for read counts, mid-pass arrivals, and multi-batch backlogs.
  • Documents the transaction reduction in the changelog.
File Review
core/​tests/​switch_core/​management/​test_stream.py Adds coverage for single-read delivery, concurrent arrivals, and backlog handling.
core/​switch_core/​bridges/​agent/​protocol/​controller_stream.py Updates delivery scheduling and wake handling. A moderate race remains: an event committed between doorbell clearing and the head snapshot can leave the bell set after delivery, causing a redundant pass and head read.
CHANGELOG.md Documents the reduced controller delivery transaction and head-read cost.

This pull request requires changes.

Review effort: Lite

@abeldantas

Copy link
Copy Markdown
Collaborator Author

Combined into #1307 with the other scaling PRs.

@abeldantas abeldantas closed this Nov 2, 2026
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.

2 participants