fix(comm): fold post-snapshot debug events into the snapshot and fail fast on a snapshot-request failure (COMM-3, COMM-7, WP4 task 4.3)

This commit is contained in:
Joseph Doherty
2026-12-03 09:47:29 -05:00
parent 8a4b466a03
commit c1db7094eb
4 changed files with 278 additions and 7 deletions
@@ -441,6 +441,7 @@ Disconnect is detected at the **transport layer**, never via an application-leve
- **In-flight command/control messages (gRPC call + deadline)**: When a connection drops while a request is in flight (e.g., a deployment sent but no response received), the gRPC call fails or hits its deadline and the caller receives a failure. There is **no automatic retry or buffering at central** — the engineer sees the failure in the UI and re-initiates the action. This is consistent with the design principle that central does not buffer messages. An in-progress deployment whose round-trip exceeds the timeout (default 120 s at `CommunicationService.DeployInstanceAsync`) surfaces as `DeploymentStatus.Failed` to the caller.
- **Debug streams (gRPC)**: Any gRPC stream interruption is detected by the HTTP/2 keepalive PING (~25 s) and triggers reconnection logic in the `DebugStreamBridgeActor`. The bridge actor attempts to reconnect to the other site node endpoint (NodeB if NodeA failed, or vice versa), with up to 3 retries and 5-second backoff. If all retries fail, the consumer is notified via `OnStreamTerminated` and the bridge actor is stopped. Events during the reconnection gap are lost (acceptable for real-time debug view). On successful reconnection, the consumer can request a fresh snapshot to re-sync state.
- **Debug View snapshot hand-over (COMM-3/COMM-7, 2026-12).** The bridge actor buffers stream events until the snapshot arrives and flushes that gap-window buffer right after it; `DebugStreamService.StartStreamAsync` now **holds** every event that follows the snapshot until it hands the snapshot back, folds them into it (newer-or-equal timestamp per entity wins, unseen entities appended) and only then passes later events through to `onEvent`. Before this the gap-window events reached the consumer before the caller had applied the snapshot, so the SignalR hub sent `OnAttributeChanged` ahead of `OnSnapshot` and a client that replaces its state from the snapshot regressed to the older value. A fail-fast `Status.Failure` from the command transport for the snapshot request (no gRPC channel for the site, an immediate RPC error) now fails the start at once with that cause and tears the session down, instead of waiting out the 30 s snapshot timeout.
- **Only the active site node serves streams (COMM-2, 2026-12).** The site stream hub is node-local — Instance Actors, and therefore every attribute/alarm event, exist only on the active node (oldest Up member of the `site-{SiteId}` role, `ActiveNodeEvaluator.SelfIsOldestUp`). `SiteStreamGrpcServer` therefore refuses `SubscribeInstance`/`SubscribeSite` with **`Unavailable`** on a node that is not active, and a watchdog (2 s) ends every open stream with **`Unavailable`** once the node loses the active role. Both consumers treat `Unavailable` as a stream error and flip NodeA↔NodeB, so after a failover — and in the steady state after any NodeA restart, where NodeA rejoins as standby — the Alarm Summary aggregator and Debug View land on the node that produces events. Before this a subscription to a healthy standby succeeded (headers flushed, keepalives passed) and never delivered an event; nothing errored, so the consumer never flipped and `IsLive` stayed true. The ending deliberately uses an error status, not OK: a graceful completion does not flip the node (below). The reconciliation/trace pulls on the same service are not gated — they read node-local stores and must answer on either node.
- **Graceful end of stream is NOT a fault, but it IS a reconnect trigger.** When the site's `GrpcMaxStreamLifetime` (4 h) elapses — and on a graceful site shutdown — the server ends the RPC with status **OK**, so the client's read loop simply finishes with no exception. `SiteStreamGrpcClient` reports that through a dedicated `onCompleted` callback, distinct from `onError`, and both consuming actors treat it as a reconnect trigger that **does not spend (or refund) the error-retry budget** and does **not** flip the node — the peer that closed cleanly is healthy. `SiteAlarmAggregatorActor` marks the stream down and lets the periodic reconcile tick reopen it (which also re-seeds); `DebugStreamBridgeActor` reopens on the same endpoint after `ReconnectDelay`. Both stamp the stream generation on the callback so a completion racing out of an already-replaced stream is ignored, and both observe the subscription `Task` itself so a fault escaping the subscribe call can never go unobserved. Without the `onCompleted` leg an OK completion was invisible to the reconnect logic and the stream stayed silently dead until the central process restarted.