Repository navigation
fix(relay): skip the dead-cell sweep's host scan when no cell qualifies - #25350
Conversation
evacuateDeadCells ran one query that joined every assignment to its cell's liveness and fence state, ordered by primary key with LIMIT 100. In production no row ever matched (the only dead cells are stale existing-only cells without a committed fence), so the planner walked the whole relay_assignments primary key every call: 551 ms mean over 85k calls, ~12% of relay database time. A cell-level pre-check now evaluates the predicate's cell-only half over the ~33-row cell tables (3 ms in production). It is a superset of the cells the host query can act on, so the sweep returns early when it is empty and otherwise restricts the host query to those cells. The host predicate is unchanged.
|
Navigate logical layers of code changes, visualize relationships, and explore their blast radius. 🧰 Additional context used📚 Code guidelines (1)No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configuration
📒 Files selected for processing (2)
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 5 remain after this review. 📝 WalkthroughWalkthroughDead-cell evacuation now selects eligible candidate cells before querying assignments. It returns zero without an assignment query when no candidates exist. When candidates exist, the assignment query is limited to those cell IDs. New tests cover the no-query case and assignment evacuation on SQLite and optional PostgreSQL. Priority: ➖ Normal Merge Risk: ⚪ Minimal · up to The change skips the host scan when no cells qualify while preserving the evacuation path for eligible cells. No actionable merge-blocking risk is established; normal checks remain appropriate. Security Architecture ReviewSecurity architecture risk: 🔵 Low · up to The existing fencing and assignment controls remain in place, with no demonstrated security bypass. A cell becoming eligible between the two reads can wait for a later sweep. Concurrent-transition coverage and the acceptable recovery delay remain unspecified. Retained concerns Security review detailsSecurity Blast Radius
Trust Boundaries and Controls
Resilience and Maintainability Implications
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches 💡 1📝 Generate docstrings 💡
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes
- Dead-cell sweep pre-check:
evacuateDeadCellsnow calls the new privatedeadCellEvacuationCandidates(cutoff)first and returns0without issuing the host query when no cell qualifies; when candidates exist, the host query runs unchanged plusAND assignment.cell_id IN (...). - New test:
dead-cell-evacuation-precheck.test.tspins that an unfenced existing-only dead cell skips the host query and leaves the host in place, and that a dead uncapped admitting cell still evacuates, on SQLite and (when configured) Postgres.
The candidate query is the cell-level half of the host predicate minus the per-host pin/limits disjunctions, so it is a strict superset of the cells the host query can act on; the set of moved hosts is unchanged. The skip is guarded by the cellIds.length === 0 early return, and the param order [1, cutoff, now, now, ...cellIds, limit] matches the placeholder order. I independently traced the three-valued logic, the 1:1 LEFT JOINs (all cell tables keyed by cell_id), and the relay_cells inner-join domain against the host query, and separately had it verified — no counterexample.
Two benign observations, neither actionable: the pre-check and host query are separate statements with separately sampled now, so a cell that becomes actionable between them is caught on the next tick (errs safe); and the IN (...) parameter count grows with fleet size (the empty case is guarded, and the fleet is ~33 cells).
deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏

ELI5
Every 30 seconds each relay director asks the database "are there hosts stuck on a dead cell that I should move?". The answer is almost always "no", but working that out cost over half a second each time, because the question was asked host by host. Now the director first asks the cheap question, "is there any dead cell I could move hosts off?", and only asks the expensive one when the answer is yes.
What Changed
evacuateDeadCellsruns a cell-level pre-check before its host query. The pre-check evaluates the parts of the existing predicate that depend only on the cell: liveness, admission, the connection-limits row, and the committed fence. It reads only the small cell tables (~33 rows each).AND assignment.cell_id IN (...)for those cells.Nothing changes for users. Hosts on a dead cell that can be evacuated are still moved by the same sweep tick.
Why
Production evidence, read-only, 2026-10-04/05:
pg_stat_statements: this statement has 85,459 calls at a 551 ms mean, 47,102 s in total, about 12% of relay database time (phase-0 measurement: 12.5%, 574 ms).EXPLAIN(plain, read-only session):ORDER BY user_id, relay_host_id LIMIT 100lets the planner walk the wholerelay_assignmentsprimary key. No row ever matches, so it reaches the end every call.enabled = 0) with no committed fence, so no host on them qualifies. 2,706 assignments still point at c1/c2/c3/c6.EXPLAIN ANALYZEon the same read-only session (a plain SELECT), takes 3.1 ms and returns 0 rows.Alternatives considered:
relay_assignments(cell_id). It needs an out-of-bandCREATE INDEX CONCURRENTLY(see the RULE besideSCHEMA), and it adds write cost to a table with ~390M updates a week. It still would not stop the per-row subplans from running.Linked Issue
N/A. Relay redesign step 0 (internal).
Visual Proof
N/A. Database sweep only; there is no UI or client-visible change.
Testing
dead-cell-evacuation-precheck.test.ts, run on SQLite and on local Postgres 16 (port 55440):origin/main's implementation.cloud/apps/relay:pnpm typecheckpasses, and the full relay suite with Postgres passes (111 files, 992 tests).Review
Superset argument: a host row qualifies only if (a) its cell is dead, and (b) either branch 1 holds (
enabled = 1, no limits row, no active pin) or branch 2 holds (enabled = 0, limits row or active pin, valid committed fence). The pre-check keeps (a), and keeps (b) without the pin/limits disjunction of branch 2 and the pin clause of branch 1. So every cell with a qualifying host is in the pre-check result.Notes