Files
parking_solution/packages/db/drizzle/0032_hot_path_indexes.sql
julian 73ada89a13
Build & push images / images (push) Successful in 3m45s
fix(server): bound active-sessions cost, index hot paths, no phantom relay timeouts
park-2 (2026-10-24): GET /api/sessions/active ran ~2 s synchronously — PayStation.activeSessions
called quote() per open unpaid transient (664 rows), each 3-4 unindexed ledger_events scans by
identity — 2234x/day. The frozen event loop made the Dingtian driver's 2 s timer fire before the
already-received UDP reply (timers run before poll I/O), so healthy pulses logged "timeout"; the
subscription flow then unwound before firing the camera or writing the sessions row, leaving
signed entries with no snapshot. Cost grew ~120 ms/day with the operator's open-ticket count.

- pay-station: price every list row from the one ledger scan (rows grouped per identity ->
  #quoteFromRows); tariff resolver built once per request; quote(identity) wraps the same core.
  validations: sessionValidationsFromRows / liveValidationsFromRows. Test pins list == quote().
- db 0032_hot_path_indexes: ledger_events(identity,index), device_events(category,kind,
  occurred_at), device_events(kind,occurred_at), snapshots(identity), sessions(state),
  sessions(subscription_id,state). Additive.
- access-dingtian binaryUdp: defer the timeout rejection with setImmediate so a datagram that
  already arrived wins over an expired timer.
- subscription-flow: snapshot fired and session cache written/closed BEFORE the pulse.

wiki/concepts/park-2-event-loop-stall.md has the full analysis.
2026-10-24 11:17:30 +02:00

19 lines
1.3 KiB
SQL

-- Hot-path indexes (park-2, 2026-10-24). Every per-session read of the ledger was a full
-- scan (no index on identity), and the camera/relay telemetry queries scanned
-- device_events (no index beyond the PK). Both tables grow with the operator's traffic,
-- so request time grew with it: the booth's active-sessions call reached ~2 s and froze
-- the event loop, which surfaced as relay "timeouts". The handler was also rewritten to
-- avoid per-row reads; these indexes make the remaining identity/telemetry lookups O(log n)
-- whatever the data volume. All additive; the previous image ignores them on rollback.
CREATE INDEX IF NOT EXISTS `ledger_events_identity_idx` ON `ledger_events` (`identity`,`index`);
--> statement-breakpoint
CREATE INDEX IF NOT EXISTS `device_events_cat_kind_time_idx` ON `device_events` (`category`,`kind`,`occurred_at`);
--> statement-breakpoint
CREATE INDEX IF NOT EXISTS `device_events_kind_time_idx` ON `device_events` (`kind`,`occurred_at`);
--> statement-breakpoint
CREATE INDEX IF NOT EXISTS `snapshots_identity_idx` ON `snapshots` (`identity`);
--> statement-breakpoint
CREATE INDEX IF NOT EXISTS `sessions_state_idx` ON `sessions` (`state`);
--> statement-breakpoint
CREATE INDEX IF NOT EXISTS `sessions_subscription_idx` ON `sessions` (`subscription_id`,`state`);