Merge review fixes: termination watchdog exits 1 on cluster leave/down under Akka.Hosting (#980, windev SCM gate passed); Gateway alarm writer isolates poison events (#976)

This commit is contained in:
Joseph Doherty
2026-12-03 12:01:50 -05:00
16 changed files with 946 additions and 82 deletions
+34 -10
View File
@@ -216,7 +216,7 @@ an unobserved task exception.
Every production writer answers a systemic failure — historian, HistorianGateway or
database down, an auth blip, a failed schema bootstrap, a gateway restart answering
`Cancelled`/`Unknown`, a dropped channel — with `RetryPlease` for every event of the
`Cancelled`, a dropped channel — with `RetryPlease` for every event of the
batch. Until #976 the sink counted each of those against `MaxAttempts`, so an outage
longer than ~6.5 minutes (1 + 2 + 5 + 15 + 60 s, then 60 s per drain, 10 attempts)
dead-lettered the oldest 100 queued alarm events, and another 100 every ~10 minutes
@@ -228,18 +228,42 @@ after that, with no automatic retry. Now:
unavailable: N event(s) deferred …". `PermanentFail` rows still dead-letter. When the
historian returns, the queue drains in `enqueued_at_utc` order.
- `MaxAttempts` therefore bounds only a row that keeps failing **while its neighbours
land** — the finding-002 poison case it exists for.
land** — the finding-002 poison case it exists for. That only works if the writer lets
the neighbours land, i.e. isolates an event it cannot classify instead of retrying its
whole batch with it — which both writers now do (below).
- Writers: `GatewayAlarmHistorianWriter` dead-letters only `InvalidArgument`,
`FailedPrecondition`, `OutOfRange`, `NotFound`, `AlreadyExists`, `Unimplemented` (on
a single send) and events that cannot be mapped to the wire; every other gRPC code and
every non-RPC exception retries (the default used to be dead-letter). The SQL writers
dead-letter only recognised data errors, and `AlarmBatchWrite` re-sends a multi-event
chunk **event by event** when it fails with an error that is neither a recognised data
error nor a known transient, so one unrecognised poison event cannot hold its
chunk-mates (they `Ack`, which is what lets the sink count the poison row's attempts).
- Residual: a batch made up **entirely** of rows that always answer `RetryPlease` looks
like an outage and is retried at the 60 s cap rather than dead-lettered. It shows as a
queue that does not drain while `LastError` reports the deferral.
every non-RPC exception retries (the default used to be dead-letter). On the batch
(`SendEvents`) path a retryable failure is split two ways (#976 review):
- a **known outage** — `Unavailable`, `DeadlineExceeded`, `Cancelled`,
`ResourceExhausted`, `Aborted`, `Unauthenticated`, `PermissionDenied`, or a
status-less transport fault (socket / I/O / HTTP, a disposed channel, a timeout) —
retries the chunk and every later chunk without sending more;
- anything else — `Unknown` (the gateway's handler threw, e.g. on one bad event),
`Internal`, `DataLoss`, an unlisted code, a non-transport exception — **re-sends the
chunk event by event**, so the siblings `Ack` and only the culprit keeps
`RetryPlease` (and dead-letters via `MaxAttempts` on those Ack'd drains). If not one
single send `Ack`s, it was not the event: later chunks retry as for an outage, so such
an outage costs at most one chunk of single sends per drain. Before this, the gateway
writer retried the whole batch on `Unknown`, the drain saw no `Ack`, and one such
event at the queue head stalled **all** Gateway alarm history until capacity eviction
dropped it silently. (The earlier version of this section said the writer behaved like
the SQL writers here; it did not.)
The SQL writers dead-letter only recognised data errors, and `AlarmBatchWrite`
re-sends a multi-event chunk **event by event** when it fails with an error that is
neither a recognised data error nor a known outage, so one unrecognised poison event
cannot hold its chunk-mates. The isolation stops at the first single that fails with a
known outage error (the rest of the chunk retries), and the SQL Server writer's outage
set (`SqlHistorianConnection.IsOutage`) adds login failures (18456/18452), server
unreachable / refusing connections (2, 40, 53, 64, 121, 10061, 10065, -1), fatal
severity ≥ 20 and an exhausted connection pool to the retry-transient numbers — so an
outage of that kind costs one round trip per chunk, not one per event.
- Residual: a batch (Gateway: an isolated chunk) made up **entirely** of rows that always
answer `RetryPlease` looks like an outage and is retried at the 60 s cap rather than
dead-lettered. It shows as a queue that does not drain while `LastError` reports the
deferral.
- Rows dead-lettered as `max attempts (N) exceeded` before #976 may be outage
casualties; `RetryDeadLettered()` revives them. Rows dead-lettered by a post-#976 node
read `… exceeded while the historian accepted other events`.
+26 -4
View File
@@ -478,10 +478,32 @@ two-node pair that choice means *any* crash of the oldest node is a full outage.
a dead actor system. It deliberately does **not** call `StopApplication()` (#980 / review 05 H-1): under
`UseWindowsService` that is a *clean* service stop (`SERVICE_STOPPED`, exit 0), which `sc.exe failure`
recovery does not act on, so a node that left the cluster stayed stopped on Windows (docker's
`restart: unless-stopped` masked it). A graceful host stop (SCM stop, Ctrl-C) is still silent. The
real-SCM check (leave a node on a Windows pair, assert the service returns to `Running` within the first
recovery delay) is a live gate on `windev`. Under `auto-down` the *survivor* no longer needs this
(it is never the one downed); the **downed** node still does.
`restart: unless-stopped` masked it). A graceful host stop (SCM stop, Ctrl-C) is still silent.
**How "graceful" is decided (#980 review).** *Not* from "is the host already stopping": Akka.Hosting's
`AkkaHostedService` hooks `WhenTerminated` first and, for a `ClusterLeavingReason` / `ClusterDowningReason`
shutdown, sets `Environment.ExitCode = -1` and calls `StopApplication()` **before** the watchdog's
continuation runs — so the first #980 fix saw every leave/down as "already stopping" and stayed quiet, and
the node still ended in a clean stop. (The -1 does not help: SCM is told `ServiceBase.ExitCode`, which the
generic host leaves at 0, in `SERVICE_STOPPED`; recovery never runs.) The watchdog now treats a termination
as expected only when (a) the host asked to stop **while the ActorSystem was still running** (SCM
stop / Ctrl-C / `GracefulClusterLeaveService` — Akka.Hosting's hook only calls `StopApplication` after
termination, so it can never set this), (b) its own `StopAsync` ran, or (c) `CoordinatedShutdown`'s reason is
`ClrExitReason` (what both `GracefulClusterLeaveService` and Akka.Hosting's `StopAsync` run). Anything else —
`ClusterLeavingReason`, `ClusterDowningReason`, `ActorSystemTerminateReason`, no reason — exits 1. Once it has
decided to exit, its `StopAsync` holds the host's shutdown (bounded by `ShutdownTimeout`) until the exit is
issued, so the service host cannot report `SERVICE_STOPPED` first. Pinned by
`TerminationWatchdogAkkaHostingTests` (real `AddAkka` host + clustering, `Cluster.Leave`/`Down` of self ⇒
exit 1; host `StopAsync` / `StopApplication`-while-running ⇒ no exit).
**Real-SCM live gate (windev, 2026-12-03): PASSED.** A throwaway service (`AddWindowsService` + `AddAkka`
clustering + `GracefulClusterLeaveService` + this watchdog, `sc failure … actions= restart/3000`) that
`Leave`s its own cluster: the fixed watchdog exited 1 → SCM event **7031** "terminated unexpectedly … Restart
the service" → back to `Running` 3 s later. Control with the pre-review logic: watchdog quiet, host returned
with process exit code **-1**, service **Stopped, no 7031, never restarted** (confirms both the defect and
that Akka's -1 is invisible to SCM recovery). Positive control: `sc stop` on the fixed build → clean
`Stopped`, exit 0, no recovery. Under `auto-down` the *survivor* no longer needs this (it is never the one
downed); the **downed** node still does.
3. **Both peers listed in `SeedNodes`** on every node, **each node listing itself first**, so a restarted
node can re-join via either peer and a node cold-starting while its peer is dead still forms the
cluster. Without the self-first ordering it waits in `InitJoin` forever (auto-down removes the crash