Skip to content

Keep a controller token's rows until a signal says they changed (CHOO-3667) - #1259

Closed
abeldantas wants to merge 4 commits into
mainfrom
hold-auth-rows
Closed

abeldantas wants to merge 4 commits into
mainfrom
hold-auth-rows

Conversation

@abeldantas

Copy link
Copy Markdown
Collaborator

Every request a controller makes reads its controller row (to refuse a revoked one) and, when it acts as an agent, that agent's row. Both were kept for 5 seconds. A controller's requests are spread over every core process, so a process sees a given controller or agent too rarely for 5 seconds to help: with 12 processes and 10,000 agents each sending a message every 2 minutes, a process sees a given agent about once every 24 minutes, and the cache missed on nearly every request.

In this PR we:

  • keep an entry for as long as the signal connection it was read under keeps listening, and drop it when a signal names it. Triggers on agents, and on a controller's owner, credential and revocation, send agent.changed and controller.auth_changed, so no writer can forget to
  • fall back to the 5 second TTL for anything read while the bus was not listening, or kept past a reconnect, since a signal sent between two connections reached neither
  • read all of a controller's bound agents on a miss, so one read serves the rest.

Locally (2 processes, 400 agents, 20 messages a second), this and #1258 take CPU per message from 13.8 to 13.2 ms. With more processes the saving should be larger; not measured yet.

…-3667)

Every request a controller makes reads its controller row, to refuse a
revoked one, and acting as an agent, that agent's row. Both were kept
for 5 seconds. A controller's requests are spread over every process, so
with 12 processes each saw a given controller about once a minute and a
given agent less often: the cache missed on nearly every request, at two
transactions each.

An entry read while the signal bus listens is now kept for as long as
that same connection listens, and dropped when a signal names it.
Triggers on agents, and on a controller's owner, credential and
revocation, send agent.changed and controller.auth_changed, so no writer
can skip them. An entry read without a listening bus, or kept past a
reconnect, lasts the TTL as before. A miss reads every agent bound to
the controller at once (AgentStore.get_many), and the agent rows have a
bound of their own, CONTROLLER_AUTH_CACHE_MAX_AGENTS.

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 change spans authorization caching across PostgreSQL trigger/NOTIFY delivery, reconnect gaps, and migration-created DDL, requiring end-to-end validation of multi-process races and rollout behavior that automated review cannot provide.

No open issues 🎉

What changed in this PR

This PR replaces the fixed five-second controller-auth cache TTL with signal-driven invalidation and batches agent authorization reads for improved performance across processes.

Changes:

  • Adds signal-aware cache retention with TTL fallback across disconnects and reconnects.
  • Adds PostgreSQL notification triggers, migration support, and cache wiring.
  • Adds bounded bulk agent lookups, configuration, tests, and changelog documentation.
  • Review finding: when AGENT_AUTH_CACHE_TTL_SECONDS=0, the authentication path still performs the bulk agent lookup on every request; it should retain the single-agent lookup when caching is disabled.
File Summary
core/​tests/​switch_core/​management/​test_controller_auth_cache.py Adds integration coverage for batched reads, signal invalidation, reconnect behavior, and trigger payloads.
core/​tests/​switch_core/​bridges/​agent/​test_controller_auth_cache.py Tests cache tickets, signal lifetimes, bounds, reconnect handling, and invalidation.
core/​switch_core/​signals.py Tracks signal-listener connection generations.
core/​switch_core/​migrations/​versions/​bad5099ef32b_auth_rows_notify.py Installs authorization-row notification functions and triggers.
core/​switch_core/​management/​wiring.py Connects the authorization cache to the signal bus and configures its agent bound.
core/​switch_core/​management/​auth.py Uses read tickets when caching controller authorization rows.
core/​switch_core/​db/​stores/​agent_store.py Adds a bulk agent lookup method.
core/​switch_core/​db/​models.py Registers authorization notification DDL for test schema creation.
core/​switch_core/​db/​auth_rows_notify_ddl.py Defines authorization-row notification functions, triggers, and signal constants.
core/​switch_core/​config.py Adds configuration for the maximum cached controller-agent rows.
core/​switch_core/​bridges/​agent/​controller_auth_cache.py Implements signal-aware, bounded controller and agent row caching.
core/​switch_core/​bridges/​agent/​auth.py Loads and caches agents bound to a controller on cache misses; the disabled-cache path requires adjustment noted above.
CHANGELOG.md Documents the improved controller authentication caching behavior and configuration.

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