Repository navigation
test(relay): deterministic step-5 simulation with invariants 1-8 - #27040
Conversation
914a01c to
ccbcac9
Compare
2f27605 to
2feef9e
Compare
Rebased onto main as one commit after its parent squash-merged.
cdc579d to
529f5a4
Compare
📝 Walkthrough
Priority: ⬇️ Low Merge Risk: ⚪ Minimal · up to This change adds a deterministic test simulation for reserve placement and does not change production behavior. The remaining suggestions would improve test diagnostics and confidence in reproducibility, but they do not block merging. Pre-merge checks |
|
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
cloud/apps/relay/src/reserve-placement-simulation.ts (1)
18-27: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winAdd a test that one seed gives one result.
The suite relies on
seededRandomand on the claim that the simulation is "deterministic". No test checks that two runs with the same seed give the sameSimulationReport. Hidden nondeterminism can make seeded failures impossible to reproduce. Examples are a strayMath.random,Date.nowin a dependency, or the order of async continuations. Add a short-duration test that runs one config twice and compares the reports withtoEqual. The short duration keeps the extra runtime small. Also check thatseededRandom(seed)gives the same sequence from two separate instances.♻️ Proposed test
it('is deterministic for a seed', async () => { const config = { ...mixed({}), durationMs: 2 * 60_000 } const first = await runReservePlacementSimulation(config) const second = await runReservePlacementSimulation(config) expect(second).toEqual(first) const a = seededRandom(11) const b = seededRandom(11) expect(Array.from({ length: 100 }, a)).toEqual(Array.from({ length: 100 }, b)) })Based on learnings: "For any seeded pseudo-random number generator (PRNG) implementation, require a determinism test verifying that initializing with the same seed produces an identical output sequence across separate instances/runs."
Source: Learnings
ℹ️ Review info
⚙️ Run configuration
- Configuration used: Repository UI
- Review profile: CHILL
- Plan: Advanced
- Run ID:
0f86b362-f915-4c7d-9b51-67219a4a16fa
📒 Files selected for processing (2)
cloud/apps/relay/src/reserve-placement-simulation.test.tscloud/apps/relay/src/reserve-placement-simulation.ts
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 4 remain after this review.
| const violate = (invariant: number, detail: string): void => { | ||
| if (violations.length < 50) violations.push({ invariant, at: clock.now, detail }) | ||
| } |
There was a problem hiding this comment.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Cap violations per invariant, not across all invariants.
The comment at Line 15-16 says one run "reports every class it hit". violate stops recording after 50 violations of any kind. After that, a different invariant that fires later is dropped without notice.
This has two effects:
- The fault tests check
toContain(invariant). If an early run of another invariant fills the cap, the target invariant is lost and the test fails, even though the check worked. - Clean-run failures show only the first 50 entries. A second, independent invariant class can be hidden.
Keep a separate counter for each invariant number.
🐛 Proposed fix
--- "a/cloud/apps/relay/src/reserve-placement-simulation.ts"
+++ "b/cloud/apps/relay/src/reserve-placement-simulation.ts"
@@ -502,10 +502,13 @@
): Promise<SimulationReport> {
const random = seededRandom(config.seed)
const clock = new Clock()
const violations: SimulationViolation[] = []
+ const violationCounts = new Map<number, number>()
const violate = (invariant: number, detail: string): void => {
- if (violations.length < 50) violations.push({ invariant, at: clock.now, detail })
+ const count = violationCounts.get(invariant) ?? 0
+ violationCounts.set(invariant, count + 1)
+ if (count < 20) violations.push({ invariant, at: clock.now, detail })
}
const faults = config.faults ?? {}
const database = new SimDatabase(violate, faults)
const cells = config.cells.map(
ELI5
Before any cell runs step 5 for real, this test plays out thousands of desktops, five directors and thirty cells, with things breaking on purpose, and checks that no cell ever goes over its limit and no desktop ends up owned by two cells.
What Changed
reserve-placement-simulation.tsis a seeded discrete-event model that drives the realCellReserveBookandReservePlacer(from the parent PR). Sockets, Postgres and desktops are modelled. Scenarios, 3 seeds each:Invariants checked: (1) seats + bookings never over the hard cap, after every event; (2) ok bookings in any window ≤ burst + rate × window, per incarnation; (3) no host holds two live seats past poll lag + grace; (4) no demotion ever lands on the desktop's live control, and no host loses every seat; (5) the ledger epoch never falls, and after a reconcile each seat's row is that seat; (6) ≤ 3 reserve calls per request; (7) a database mint is above every epoch the map knew; (8) near full, extra calls ≤ 10 % and exhausted requests ≤ 1 %; (9) once a reserve cell's dead-man has tripped (while Postgres still says reserve), today's database path takes it within 20 s by the row the cell writes itself (raw mode, current process, under 45 s old). This must hold whether the directors' feed polls come back after 150 s or never come back; fault
pgReserveBlocksDatabaseignores the row and must trip it. A tripped cell reports reserve while it re-registers (modelled delay), but placing directors read its raw mode and book nothing there. FaultnoRawFeedModesends it 118 bookings that it refuses.Each invariant except 6 also has a fault run that breaks its rule (book past the cap, a 200-token burst with greedy directors, no demotion, a director that keeps the oldest seat, an unguarded ledger, no epoch floor) and must trip it, so a pass is not vacuous.
Why
The design (§9) gates every production flip on this simulation. Driving the production modules rather than a separate model means a bug in either decision-maker shows up here.
Three design gaps the simulation found, fixed in the parent PR and here:
joinedAttoo.Linked Issue
N/A (internal relay redesign, step 5).
Visual Proof
N/A: test only.
Testing
vitest run src/reserve-placement-simulation.test.ts: 24 tests, ~25 s locally.Row-ahead repair (invariant 8), measured
Run: 25 min, 5 directors (one old), 6,600 hosts, 10% duplicate assigns, seed 11. Re-measured after
observePollwas removed.A director whose map has not yet seen the leave can re-place level with the row; the next reconcile repairs it. The hard invariant is at most 2 demotions behind one divergence (the same row); fault
mintAtFloor(placement never rises above the row) breaks it ("demoted 3 times behind its row"). Re-seats that miss are reported, not violations.Feed outage that never ends: the first fresh placement lands 63 s in, when the first cell trips (60-120 s per cell); before that, a region where every cell is reserve has no new placement.
Cold start: a restarted director's per-cell estimates start empty and refill at its share of the cell's intake, so it books nothing on a cell for 1 / (intakePerSec / directors) s and paces the rest; the startup gate holds hosts that may be on an unheard cell for up to 30 s.