Repository navigation
feat(relay): the director that booked a join writes its row about a second later (off) - #27048
Conversation
6208f72 to
f642623
Compare
03c8e63 to
d94b447
Compare
f642623 to
e9de1f6
Compare
d94b447 to
d4853e9
Compare
e9de1f6 to
581a5d8
Compare
79b08ef to
92822cd
Compare
…econd later (off) Rebased onto main as one commit after its parent squash-merged.
da6c37b to
680e462
Compare
📝 Walkthrough
Priority: ⬇️ Low Merge Risk: 🔵 Low · up to These failures require unusual queue pressure or a director configuration without rehome support. The change is mergeable with owner awareness, though both issues should be fixed. Pre-merge checks |
|
There was a problem hiding this comment.
Actionable comments posted: 2
ℹ️ Review info
⚙️ Run configuration
- Configuration used: Repository UI
- Review profile: CHILL
- Plan: Advanced
- Run ID:
0ca1adda-5e04-4c20-9352-631ca232efea
📒 Files selected for processing (8)
cloud/apps/relay/src/floating-promise-census.test.tscloud/apps/relay/src/relay-server.tscloud/apps/relay/src/reserve-assignment.test.tscloud/apps/relay/src/reserve-assignment.tscloud/apps/relay/src/reserve-ledger-writer-postgres.test.tscloud/apps/relay/src/reserve-ledger-writer.test.tscloud/apps/relay/src/reserve-ledger-writer.tscloud/apps/relay/src/shadow-seat-directory.ts
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
| // Only a director that books writes rows, and only for the joins it booked. | ||
| const reserveLedger = | ||
| config.role === 'director' && shadowSeatPoller && config.reservePlacement === 'on' | ||
| ? new ReserveLedgerWriter(observedDatabase, { now: options.now }) | ||
| : undefined |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
A director without rehomeAudience creates the writer, but it cannot demote seats.
reserveLedger is created without a rehomeAudience check. reservePlacement does require rehomeAudience. If rehomeAudience is missing, reservePlacement is undefined. The demote callback then does nothing except log a warning, and reconcileReserveLedger still counts the seat as demoted. Such a director also books nothing, so the writer has no purpose there. Add the same condition: create the writer only when reservePlacement?.placementMode === 'on'.
| this.queue.splice(0, excess) | ||
| this.dropped += excess | ||
| } | ||
| } | ||
|
|
||
| pending(): number { | ||
| return this.queue.length | ||
| } | ||
|
|
||
| // Single-flight; resolves once the queued rows are written or the attempt failed. | ||
| async flush(): Promise<void> { | ||
| if (this.flushing || this.queue.length === 0 || this.now() < this.retryAt) { | ||
| this.logSummary() | ||
| return | ||
| } | ||
| this.flushing = true | ||
| const batch = this.queue.slice(0, RESERVE_LEDGER_BATCH) | ||
| try { | ||
| await this.write(batch) | ||
| this.queue.splice(0, batch.length) | ||
| this.written += batch.length | ||
| this.failures = 0 | ||
| this.retryAt = 0 | ||
| } catch { | ||
| this.failures += 1 | ||
| this.retryAt = this.now() + Math.min(RETRY_MAX_MS, RETRY_BASE_MS * 2 ** (this.failures - 1)) | ||
| } finally { | ||
| this.flushing = false | ||
| this.logSummary() | ||
| } | ||
| } |
There was a problem hiding this comment.
🗄️ Data Integrity & Integration | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
sed -n '27,105p' cloud/apps/relay/src/reserve-ledger-writer.ts
sed -n '60,80p' cloud/apps/relay/src/reserve-ledger-writer.test.tsRepository: stablyai/orca
Length of output: 3355
🏁 Script executed:
set -o pipefail
printf '%s\n' '--- writer ---'
nl -ba cloud/apps/relay/src/reserve-ledger-writer.ts | sed -n '1,180p'
printf '%s\n' '--- writer references ---'
rg -n -F --glob '*.ts' -- 'new ReserveLedgerWriter' cloud/apps/relay/src || true
rg -n -F --glob '*.ts' -- '.enqueue(' cloud/apps/relay/src | rg 'writer|ledger|reserve' || true
printf '%s\n' '--- queue/flush tests ---'
rg -n -F --glob 'reserve-ledger-writer.test.ts' -- 'flush|enqueue|queueMax|pending|in flight|Promise' cloud/apps/relay/src || true
nl -ba cloud/apps/relay/src/reserve-ledger-writer.test.ts | sed -n '1,150p'Repository: stablyai/orca
Length of output: 16612
🏁 Script executed:
nl -ba cloud/apps/relay/src/relay-server.ts | sed -n '175,260p'
rg -n -F --glob 'relay-server.ts' -- 'reserveLedger.start' 'reserveLedger.flush' 'ORCA_RELAY_RESERVE_PLACEMENT' cloud/apps/relay/src || trueRepository: stablyai/orca
Length of output: 4582
Remove only the entries that the successful write actually used.
flush() snapshots a batch, awaits write(batch), then removes batch.length entries from the current queue. If enqueue() overflows during that await, it can already remove the batch’s oldest entries. The later count-based splice() can then remove newer entries that were never written.
Use stable queue-entry identities and remove only the snapshot entries still present after the write.
Suggested fix
+type QueuedLedgerEntry = { id: symbol; entry: LedgerEntry }
+
export class ReserveLedgerWriter {
- private queue: LedgerEntry[] = []
+ private queue: QueuedLedgerEntry[] = []
...
enqueue(entry: LedgerEntry): void {
- this.queue.push(entry)
+ this.queue.push({ id: Symbol(), entry })
...
- const batch = this.queue.slice(0, RESERVE_LEDGER_BATCH)
+ const batch = this.queue.slice(0, RESERVE_LEDGER_BATCH)
try {
- await this.write(batch)
- this.queue.splice(0, batch.length)
+ await this.write(batch.map(({ entry }) => entry))
+ for (const queued of batch) {
+ const index = this.queue.findIndex(({ id }) => id === queued.id)
+ if (index >= 0) this.queue.splice(index, 1)
+ }
ELI5
A switched-on cell lets a booked desktop in without the database. The database still needs a record of where each desktop is (phones look it up until step 6), so the director that booked the desktop writes that record about a second later, and keeps trying through a database stall.
What Changed
ReserveLedgerWriter: when the seat feed shows a join withreservedByequal to this director's id, the director queues the host's(cell, epoch). Every 250 ms it writes up to 500 queued rows in one multi-row upsert ofrelay_assignments, guarded byWHERE assignment_epoch < excluded.assignment_epoch. A demotion winner is written with<=, the only path that can replace an equal epoch (every director names the same winner). It touches norelay_cellsrow and no activity lease.orca_relay_reserve_ledger_summaryline reports written, pending, dropped and failures.Only with
ORCA_RELAY_RESERVE_PLACEMENT=on, and only for joins this director booked, so nothing is written while every cell is off.Why
Design §6.2, default 2 (≤ 1 s). Nothing on a switched-on cell's connect path reads the row; phones (
/v1/resolve) and a flip back (#27043) do.Linked Issue
N/A (internal relay redesign, step 5).
Visual Proof
N/A: server-side, no UI.
Testing
reserve-ledger-writer.test.ts(SQLite): only a higher epoch replaces a row; only a demotion winner replaces an equal one; a moved row drops its counters and a same-cell row keeps them; a stall keeps the queue and backs off; the bound drops the oldest; reconcile upserts settled seats, skips young ones and corrects an orphaned row; a row naming a cell that may still seat the host is left alone.