Skip to content

feat(relay): flip a reserve-mode cell back without disconnecting anyone (E-mix) - #27043

Merged
Jinwoo-H merged 1 commit into
mainfrom
relay-step5-rollback
Oct 10, 2026
Merged

Jinwoo-H merged 1 commit into
mainfrom
relay-step5-rollback

Conversation

@Jinwoo-H

@Jinwoo-H Jinwoo-H commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 3 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​387 $\color{#cf222e}{\Huge{\mathbf{−}}}$​1 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​386
Prod 8 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​630 $\color{#cf222e}{\Huge{\mathbf{−}}}$​17 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​613

ELI5

If anything looks wrong on a cell running step 5, an operator flips that one cell back to the database. This PR makes the flip back safe: no desktop is disconnected, and within about a minute the database again holds a record for every desktop on the cell.

What Changed

  • Every second the cell compares its applied admitMode with the last one. On leaving reserve it voids its bookings and starts re-registering every control it admitted from memory (rules 1–3 of the parent PR), which has no database lease.
  • Re-registration calls today's activateControl for each such control at 50 a second, with at most 20 in flight while the database is slow. A control whose row is not written yet (the ledger writer lags by up to a second) retries with backoff for about a minute; one whose row never names it (a duplicate the ledger did not follow) is logged as orca_relay_cell_reregistration_gave_up and keeps its connection.
  • A control that moved on while its lease was being taken (rebound, replaced, or the cell flipped to reserve again) has the lease released at once. Flipping back to reserve stops the run.
  • A memory-admitted control later rebound through the database keeps the lease that rebind took.

Why

Design §7 (E-mix): a flip back must take effect in seconds, disconnect nobody, and rebuild the cell's rows. activateControl is the one path that already writes a control's lease, capability row and cell counter together, so reusing it rebuilds exactly what a database-mode admission writes.

Deviation: the design ends with "one counter reconcile". It is not needed: memory-admitted controls never moved the cell counter, so each activateControl adds exactly the unit it owes. The existing reconcile is a multi-row locking transaction meant for drains, which a cell should not run on every flip.

Linked Issue

N/A (internal relay redesign, step 5).

Visual Proof

N/A: server-side, no UI.

Testing

  • cell-reserve-rollback.blackbox.test.ts: a real cell on SQLite admits three hosts from bookings (no leases), flips to db, and one host's row lands only after the flip. All three sockets stay open; the leases equal the seat report (cell, control:<cell>:<generation>); a second flip back has nothing to do.
  • The relay suite passes locally.

The staging E-mix loop (10 flips each way under load, design §7) is a separate staging-harness item.

…ne (E-mix)

Rebased onto main as one commit after its parent squash-merged.
@Jinwoo-H
Jinwoo-H force-pushed the relay-step5-rollback branch from 9e4cc29 to 2e86afa Compare October 10, 2026 06:11
@Jinwoo-H
Jinwoo-H changed the base branch from relay-step5-cell to main October 10, 2026 06:11
@coderabbitai

coderabbitai Bot commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →Review in Change Stack →

📝 Walkthrough
📝 Walkthrough
📝 Walkthrough

Walkthrough

The relay now tracks effective reserve admission with a per-cell dead-man window. When reserve mode ends, it re-registers database activity leases for active controls admitted from memory, with bounded concurrency, batched reservation updates, and retry handling. The seat-feed route reports effective admission and database pool capacity. New fixtures and tests cover reserve-mode rollback, lease registration, and dead-man expiry.





Priority: ➖ Normal

Merge Risk: 🔵 Low · up to 2e86a

A rapid reserve-to-database transition can leave the cell’s reservation count below its active leases. The issue is localized, but the delta should have a stable owner before merging.

Pre-merge checks | Passed 4 | Failed 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 12 functions across 11 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Title check Passed The title clearly identifies the main change: safely flipping a reserve-mode relay cell back without disconnecting controls.
Description check Passed The description covers the user impact, implementation, rationale, linked-issue context, visual-proof rationale, and testing. It is sufficiently complete for the core change, although the review, note…

  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Commit to this branch
  • Create a new PR





  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 66fa23c6-61a1-4ed0-8cfc-48bfb3f982f0
📥 Commits

Reviewing files that changed from the base of the PR and between d488ce5 and 2e86afa.

📒 Files selected for processing (11)
  • cloud/apps/relay/src/app.ts
  • cloud/apps/relay/src/assignment-store.ts
  • cloud/apps/relay/src/cell-flags.ts
  • cloud/apps/relay/src/cell-reserve-dead-man.ts
  • cloud/apps/relay/src/cell-reserve-flip-back-postgres.test.ts
  • cloud/apps/relay/src/cell-reserve-rollback.blackbox.test.ts
  • cloud/apps/relay/src/cell-seat-feed-route.ts
  • cloud/apps/relay/src/cell-seat-feed.test.ts
  • cloud/apps/relay/src/host-session-registry.ts
  • cloud/apps/relay/src/relay-server.ts
  • cloud/apps/relay/src/test-fixtures/reserve-mode-cell.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review.

Comment on lines +502 to +516
private flushReregistrationCellDelta(state: NonNullable<HostSessionRegistry['reregistration']>): void {
if (state.cellDelta === 0 || state.cellWriteInFlight) return
const delta = state.cellDelta
state.cellDelta = 0
state.cellWriteInFlight = true
void this.assignments
.commitCellReservationDelta(this.config.cellId, delta)
.catch(() => {
// Retried next tick: the leases exist, only the cell's count is behind.
state.cellDelta += delta
})
.finally(() => {
state.cellWriteInFlight = false
})
}

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.

🗄️ Data Integrity & Integration | 🟡 Minor | ⚡ Quick win

Fix the lost-delta case when a newer flip back replaces the state.

reregisterMemoryControls() replaces this.reregistration with a new state object. The old state's in-flight commitCellReservationDelta can fail after that. Its .catch then adds the delta back to the old state.cellDelta. No timer flushes the old state, so the relay_cells.reserved_requests increment is lost. The same loss applies to any cellDelta that stays unflushed on the old state when it is replaced, and to activations that finish after the replacement and add their reservationDelta to the old state. Leases exist, but the cell counter stays below them until a reconcile runs.

The trigger is realistic. A switch flip goes reserve → db → reserve → db within a few seconds. Another trigger is a dead-man trip during an active pass.

Carry any unflushed delta into the new state when you replace it. Alternatively, keep cellDelta on the registry instead of on the per-pass state.

Proposed fix
-    if (this.reregistration) clearInterval(this.reregistration.timer)
+    const carried = this.reregistration?.cellDelta ?? 0
+    if (this.reregistration) clearInterval(this.reregistration.timer)
     this.reregistration = null
     const state: NonNullable<HostSessionRegistry['reregistration']> = {
       queue: [],
       seen: new Set(),
       inFlight: 0,
-      cellDelta: 0,
+      cellDelta: carried,

This fix does not cover late completions on the old state. To cover those, move the delta accumulator to a registry field.

@Jinwoo-H
Jinwoo-H merged commit abff7ab into main Oct 10, 2026
28 of 46 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