fix(alarms): #937 review r1 — revert a refused scripted method, broadcast to the peer only after the local verdict, settle a batch per request, in-flight cap

1. A post-dispatch refusal now puts the node back to its captured pre-call state
   with one corrective event (skipped when the node changed since) — the SDK had
   already applied and reported the change. Blank scripted AddComment is refused
   in the handler before the SDK runs.
2. ScriptedAlarmCommandDispatch: ask the local engine first, broadcast to the peer
   only on Applied/OutcomeUnknown — a refusal now reached no engine anywhere.
3. ApplyAsync no longer checks 'loaded' before _evalGate: a command racing the
   first load queues behind it; never loaded => refusal (nothing applied).
4. One Call's requests are settled sequentially (no request runs against a node
   state the engine refused); <= 64 asks in flight node-wide, else
   BadTooManyOperations before anything applies; per-request bound starts at send.
   Measured 300 acks through one host + LocalDb: 59 ms sequential, max 24 ms/ask
   with 64 outstanding.
5. Mixed-batch wire test (scripted refused + native + imported + scripted applied
   + core-manager method): per-item status in order, one scripted ask at a time.
L6: readiness from the engine's HasCompleteGeneration on a load fault.
L7: the verdict slot is closed when its SDK call returns (timer-captured
AsyncLocal can no longer park an ask). L10: remote budget never stretched.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Joseph Doherty
2026-11-28 11:51:28 -05:00
parent 66b75aed0f
commit 6004a5a565
14 changed files with 670 additions and 125 deletions
+24 -13
View File
@@ -189,7 +189,8 @@ after a full rebuild, carrying the unchanged `LastTransitionUtc`. Decisions:
and shelving checks are dropped. **Since #937 an OPC UA Part 9 method (and a remote gRPC-data condition method) on a
scripted condition reports this**: while the last load failed the node manager refuses the call `BadInvalidState` before the
SDK applies anything (no event, nothing routed); a call that passed that pre-check and then met a rejection (it was queued
behind a load that failed) also returns `BadInvalidState` — see [Operator command verdicts](#operator-command-verdicts-937).
behind a load that failed) also returns `BadInvalidState` and the node is reverted — see
[Operator command verdicts](#operator-command-verdicts-937).
The AdminUI `/alerts` buttons still publish fire-and-forget ("Ack sent"), so for them the only record remains the host's
Warning `… NOT applied — …`. The next good load re-asserts every node from the store.
**Accepted trade-off:** because the next good load after a failed one reads the store (which has no `Active` column), each
@@ -425,17 +426,26 @@ of which blocks under the node manager's `Lock`:
1. **Pre-check** — `ScriptedAlarmCommandReadinessView` (Runtime singleton) holds the outcome of the host's latest completed
load; the handler reads it (one volatile read) after the role/ACL gate and the Warm-standby check. While the last load
failed it returns `BadInvalidState` and nothing is applied, routed or reported. It never refuses before the first load.
2. **Bounded ask** — inside the Call service (`OtOpcUaNodeManager.CallAsync`, which the SDK invokes off `Lock`), each local
request runs through the SDK's `base.Call` on its own; the handler, under `Lock`, only *starts* the ask
(`AlarmCommandAsker`): the hosted service broadcasts the command on `alarm-commands` for the redundant peer (as before)
and asks **this node's** `ScriptedAlarmHostActor` (via `DriverHostActor`, `ExecuteAlarmCommand`) for its verdict, with a
fresh `CommandId` so the local host applies whichever copy reaches it first, once. The SDK applies its node state and
reports the one method event exactly as before; then the Call awaits the verdict with no lock held.
2. **Bounded ask** — inside the Call service (`OtOpcUaNodeManager.CallAsync`, which the SDK invokes off `Lock`; SDK 2.0.1's
`StandardServer.CallAsync` awaits it, so no request thread is held), each local request runs through the SDK's
`base.Call` on its own and is SETTLED before the next request of the batch runs. The handler, under `Lock`, only
*starts* the ask (`AlarmCommandAsker`): the hosted service asks **this node's** `ScriptedAlarmHostActor` (via
`DriverHostActor`, `ExecuteAlarmCommand`) and broadcasts the command on `alarm-commands` for the redundant peer **only
after the local verdict is applied or unknown** (`ScriptedAlarmCommandDispatch`), with a fresh `CommandId` so the local
host applies its two copies once. The SDK applies its node state and reports the one method event exactly as before;
then the Call awaits the verdict with no lock held. **A refusal reverts the node** to its captured pre-call state with
one corrective event — unless something else changed the node since (newer truth wins) — mirroring the #760 native-ack
revert. Cheap refusals the engine would make (a blank scripted AddComment ⇒ `BadInvalidArgument`) happen in the
handler, before the SDK applies anything.
3. **Load bound** — at most 64 asks outstanding node-wide (`MaxScriptedCommandsInFlight`); past it the call is refused
`BadTooManyOperations` before anything is applied or sent. Each request's 5 s bound starts when its own ask is sent.
Measured through one host with the production LocalDb store (`ScriptedAlarmCommandThroughputTests`): 300 sequential
acks in 59 ms (max 11 ms each); 300 AddComments with 64 outstanding at once in 49 ms (max 24 ms each).
| Engine verdict | Method status |
|---|---|
| applied (incl. an idempotent no-op) | `Good` |
| refused — last load failed, alarm not loaded on this node, malformed command, no engine on this node | `BadInvalidState` |
| refused — last load failed, alarm not loaded on this node (incl. the window after a host supervision restart before its reload), malformed command, no engine on this node | `BadInvalidState` (node reverted; the peer never received it) |
| no answer within min(5 s, request deadline − 250 ms), or the engine op timed out / failed in the store | `BadTimeout` / `BadInternalError`, diagnostic "OutcomeUnknown" — the command **may still apply**, never reported as refused |
`BadInvalidState` is Part 4's general "object … uninitialized or in some other invalid state" code; Part 9's method result
@@ -443,14 +453,15 @@ codes (bad EventId, already acked/confirmed/shelved, disabled, shelving time out
cannot act", and the server already uses `BadInvalidState` for a native ack off-Primary. The status is the contract: a plain
SDK client sees only the code (the reason text travels in diagnostics, returned only when requested).
**Redundancy.** The verdict is the LOCAL engine's — the one that projects this node's condition node. The peer still applies
its broadcast copy independently (and both engines' saves converge through the replicated LocalDb row). A remote
**Redundancy.** The verdict is the LOCAL engine's — the one that projects this node's condition node. The peer applies its
broadcast copy independently once the local engine applied it (or did not answer); a locally refused command is never
broadcast, so no engine anywhere applied it and the revert plus the definite failure are true pair-wide. A remote
gRPC-data `CallConditionMethod` on a scripted condition carries the same verdict (`ExecuteConditionMethodAsync`): applied ⇒
`DriverConfirmedSuccess`, refused after dispatch ⇒ `DriverConfirmedFailure`, no answer ⇒ `OutcomeUnknown`.
**Residual.** Only the pre-check stops the SDK from applying the node change. A refusal that arrives through the ask (a load
in flight when the command queued behind it, which then failed) leaves the node showing the change — and its one event
reported — until the next good deploy re-asserts the node from the store; the caller gets `BadInvalidState`.
**Residual.** Only the pre-check stops the SDK from applying the node change, so a refusal that arrives through the ask
shows subscribers two events: the method's, then the corrective one. An OutcomeUnknown leaves the node changed (the engine
may still apply it). A command racing the engine's first load queues behind it rather than failing.
**Not covered:** the AdminUI `/alerts` buttons are still fire-and-forget across the central→node transport (they report
"Ack sent", not "applied").