From 2a8bac5bf5c5cb36c12f3347a00d7e516fa416ca Mon Sep 17 00:00:00 2001 From: Joseph Doherty Date: Thu, 3 Dec 2026 15:02:09 -0500 Subject: [PATCH] feat(audit,install): #1030 audit overflow drops exported as otopcua.audit.dropped; scripted SCM-restart-after-cluster-exit gate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - AuditWriterActor: every buffer-full drop is added to OtOpcUaTelemetry.AuditDropped (otopcua.audit.dropped{reason=buffer_full, actor}) — the count used to be readable only through the actor's own property. - scripts/install/tests/Test-ScmRestartAfterClusterExit.Windows.ps1: the windev manual gate of 2026-12-03 as a script (qfailure restart action; failover/command trigger => non-zero exit, 7031, Running under a new pid; Stop-Service stays stopped; optional kill control). Parse-checked with pwsh on macOS; must be run elevated on a Windows box hosting OtOpcUaHost. Co-Authored-By: Claude --- docs/Configuration.md | 2 +- docs/Redundancy.md | 9 + docs/ServiceHosting.md | 3 + ...est-ScmRestartAfterClusterExit.Windows.ps1 | 187 ++++++++++++++++++ .../Observability/OtOpcUaTelemetry.cs | 14 ++ .../Audit/AuditWriterActor.cs | 12 +- .../AuditWriterActorRetryTests.cs | 41 ++++ 7 files changed, 266 insertions(+), 2 deletions(-) create mode 100644 scripts/install/tests/Test-ScmRestartAfterClusterExit.Windows.ps1 diff --git a/docs/Configuration.md b/docs/Configuration.md index 99c1c6210..4cfe5c5d5 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -435,7 +435,7 @@ per cluster at `/clusters/{id}/audit`. It has no `appsettings` section — it is has a `ConfigDb` — but one schema property is worth recording here because it constrains how callers may write to it: -*Flush behaviour (#980 / review 05 H-2):* `AuditWriterActor` batches events and flushes every 5 s (or at 500). A flush that fails because the database did not answer **keeps** the batch and retries on the next tick; the buffer is bounded at 10 000 events (drop-oldest, with `OverflowDroppedCount` and a Warning). Rows the database rejects outright (e.g. a duplicate `EventId`) are still dropped individually. Events are lost only if the actor stops while the database is still down (logged at Error with the count). +*Flush behaviour (#980 / review 05 H-2):* `AuditWriterActor` batches events and flushes every 5 s (or at 500). A flush that fails because the database did not answer **keeps** the batch and retries on the next tick; the buffer is bounded at 10 000 events (drop-oldest, with `OverflowDroppedCount` and a Warning; since #1030 every drop is also exported as the meter counter `otopcua.audit.dropped{reason=buffer_full}` on the `ZB.MOM.WW.OtOpcUa` meter — alert on any non-zero rate, it means `ConfigAuditLog` is missing rows). Rows the database rejects outright (e.g. a duplicate `EventId`) are still dropped individually. Events are lost only if the actor stops while the database is still down (logged at Error with the count). - **`EventId` gives cross-restart deduplication, and the DB enforces it.** Each row carries the stable per-event `AuditEvent.EventId`, under a *filtered* unique index diff --git a/docs/Redundancy.md b/docs/Redundancy.md index af189f357..50f69a94f 100644 --- a/docs/Redundancy.md +++ b/docs/Redundancy.md @@ -504,6 +504,15 @@ two-node pair that choice means *any* crash of the oldest node is a full outage. 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. + + **Scripted form of that gate (#1030):** `scripts/install/tests/Test-ScmRestartAfterClusterExit.Windows.ps1`, run + elevated on the box hosting an installed `OtOpcUaHost` (the pair's current Primary for the default + `-Trigger Failover`, which pauses while the operator presses *Trigger failover* on the cluster redundancy page; + `-Trigger Command -TriggerCommand '…'` for an unattended trigger). It checks `sc.exe qfailure` has a restart action, + that the leave ends the process non-zero, logs **7031** and comes back `Running` under a new pid, and — as the + negative control — that `Stop-Service` stays `Stopped` with no 7031; `-KillControl` adds a hard-kill leg that + proves the SCM recovery config on its own. It never reconfigures the service and always leaves it running. Not + for the wonder boxes while remote access is on hold. 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 diff --git a/docs/ServiceHosting.md b/docs/ServiceHosting.md index 9c743f65a..159a2d4a2 100644 --- a/docs/ServiceHosting.md +++ b/docs/ServiceHosting.md @@ -186,6 +186,9 @@ steps inside the window (a `data\` backup) and the host start-up to `/health/rea - `scripts/install/Install-Services.ps1 -Roles admin,driver` — installs `OtOpcUaHost`. - `scripts/install/Install-ScriptWorkerIsolation.ps1` — the ScriptWorker identity, ACLs and firewall rule (above); only for `Scripting:Runner` ≠ `InProcess`. - `scripts/install/Invoke-OtOpcUaInstallSwap.ps1` — upgrade/rollback folder swap with the isolation `-Reapply` + `-Verify` gate built in (lmxopcua#901), scoped so only the swapped-in folder is handled while the service is stopped (lmxopcua#905, above). +- `scripts/install/tests/Test-ScmRestartAfterClusterExit.Windows.ps1` — elevated, on the node's own box: checks that a node + leaving its cluster (manual failover) is restarted by the `sc.exe failure` recovery `Install-Services.ps1` sets, and that a + clean `Stop-Service` is not (#980/#1030; `docs/Redundancy.md` §"Recovery still depends on supervision"). - `scripts/install/Uninstall-Services.ps1` — stops + removes the host service. (The historian backend is the external HistorianGateway — not installed/removed by these scripts.) ## Logging diff --git a/scripts/install/tests/Test-ScmRestartAfterClusterExit.Windows.ps1 b/scripts/install/tests/Test-ScmRestartAfterClusterExit.Windows.ps1 new file mode 100644 index 000000000..f45c3f50d --- /dev/null +++ b/scripts/install/tests/Test-ScmRestartAfterClusterExit.Windows.ps1 @@ -0,0 +1,187 @@ +<# +.SYNOPSIS + Windows-executed gate for lmxopcua#980 / #1030: an OtOpcUaHost node whose ActorSystem terminates because it LEFT or was + DOWNED from its cluster (manual failover, endpoint step-down, auto-down) must be RESTARTED by Windows SCM recovery + (sc.exe failure), while an operator `sc stop` must stay stopped. + +.DESCRIPTION + Background (docs/Redundancy.md "Recovery still depends on supervision"): ActorSystemTerminationWatchdog exits the process + with code 1 on an unexpected termination, so SCM sees the service process die WITHOUT reporting SERVICE_STOPPED, logs + System event 7031 ("terminated unexpectedly") and runs the configured recovery action. Before #980 the node ended in a + clean stop (SERVICE_STOPPED, win32 exit 0) and SCM never restarted it. This script is the scripted form of the manual + windev gate of 2026-12-03. + + It runs ON the Windows box that hosts the service under test, elevated, against an INSTALLED OtOpcUaHost + (scripts\install\Install-Services.ps1). It never installs, uninstalls or reconfigures anything; it stops/starts only the + named service and always leaves it Running (finally block). + + Legs (each prints PASS/FAIL lines; exit 0 = all passed, 1 = a check failed, 2 = cannot run here): + 1. Recovery config sc.exe qfailure lists at least one RESTART action (what Install-Services.ps1 sets). + 2. Cluster exit the trigger makes this node leave / be downed. Expected: the service process exits (exit code 1 + when readable), SCM logs 7031 for the service, and the service is Running again under a NEW pid + within the recovery delay + -TimeoutSeconds. + 3. Clean stop (unless -SkipStopControl) Stop-Service: the service reports Stopped, NO new 7031 is logged and it + is NOT restarted within the first recovery delay + 15 s; then the script starts it again. + 4. Kill control (only with -KillControl) Stop-Process -Force: a crash is restarted (7031/7034). This proves the + SCM recovery config, not the watchdog — use it to tell "recovery not configured" from "the + node did not exit non-zero" when leg 2 fails. + + Triggers (-Trigger): + Failover (default) the script pauses and asks the operator to press "Trigger failover" on the AdminUI cluster + redundancy page (/clusters/{id}/redundancy). Manual failover asks the driver PRIMARY to leave, so run this on + the box hosting the current Primary of a two-node pair. Needs an interactive console. + Command runs -TriggerCommand (a PowerShell script block, as text) that makes THIS node leave or be downed, e.g. a + local admin tool; the script does not interpret it. + + Do NOT run this on the wonder boxes (vd03 / vq03 / vq04) while remote access is on hold — it is meant for windev or a QA + pair, run locally by an operator. It restarts the node it targets: a two-node pair fails over once (and again for + -KillControl); a single-node cluster has an outage for each leg. + + Run once per PowerShell host if in doubt; it uses nothing host-specific: + + pwsh -NoProfile -File scripts\install\tests\Test-ScmRestartAfterClusterExit.Windows.ps1 + powershell -NoProfile -ExecutionPolicy Bypass -File scripts\install\tests\Test-ScmRestartAfterClusterExit.Windows.ps1 -KillControl + pwsh -NoProfile -File ...\Test-ScmRestartAfterClusterExit.Windows.ps1 -Trigger Command -TriggerCommand '