Files
lmxopcua/src/Server/ZB.MOM.WW.OtOpcUa.Host/OpcUa/ScriptedAlarmCommandDispatch.cs
T
Joseph Doherty 6004a5a565 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>
2026-11-28 11:51:28 -05:00

80 lines
3.9 KiB
C#

using Akka.Actor;
using Microsoft.Extensions.Logging;
using ZB.MOM.WW.OtOpcUa.Commons.OpcUa;
namespace ZB.MOM.WW.OtOpcUa.Host.OpcUa;
/// <summary>
/// #937: how an OPC UA Part 9 method on a scripted condition reaches the engines — this node's first, the redundant peer's
/// only after this node's engine applied it (or did not answer).
/// </summary>
/// <remarks>
/// <para>
/// <b>Why the peer goes second (review r1).</b> Broadcasting first let the peer apply a command the local engine then
/// refused: the caller was told a definite failure, the local node was reverted, and the peer's save came back to this
/// node through the last-writer-wins condition-state replication. Asking first makes a refusal true pair-wide
/// (no engine saw the command) — which is what lets the node manager revert the node and answer
/// <c>BadInvalidState</c>. An applied or unknown verdict is broadcast so the peer converges exactly as before (an
/// unknown local outcome may still apply locally, so the peer must not be left behind).
/// </para>
/// <para>
/// No local engine (no driver host, or it runs no scripted-alarm host) ⇒ <see cref="AlarmCommandVerdict.NotApplied"/>
/// and nothing is broadcast: this node's condition node is driven by no engine, and a command for it is refused.
/// </para>
/// </remarks>
internal static class ScriptedAlarmCommandDispatch
{
/// <summary>
/// Asks the local engine, then broadcasts unless it refused. Starts the ask synchronously (safe under the SDK's Lock);
/// never faults — no answer within <paramref name="timeout"/> is an OutcomeUnknown outcome.
/// </summary>
/// <param name="cmd">The command (with its <c>CommandId</c>, which de-duplicates the broadcast copy on the local host).</param>
/// <param name="timeout">The caller's bound.</param>
/// <param name="askLocal">The ask to this node's scripted-alarm host; null ⇒ none on this node.</param>
/// <param name="broadcast">Publishes the command on the <c>alarm-commands</c> topic.</param>
/// <param name="logger">Logger.</param>
/// <returns>The local verdict.</returns>
internal static async Task<AlarmCommandOutcome> AskThenBroadcastAsync(AlarmCommand cmd, TimeSpan timeout,
Func<AlarmCommand, TimeSpan, Task<AlarmCommandOutcome>>? askLocal, Action<AlarmCommand> broadcast, ILogger logger)
{
if (askLocal is null)
{
return new AlarmCommandOutcome(cmd.AlarmId, cmd.Operation, AlarmCommandVerdict.NotApplied,
"This server runs no scripted-alarm engine; the command was not applied.");
}
AlarmCommandOutcome outcome;
try
{
outcome = await askLocal(cmd, timeout).ConfigureAwait(false);
}
catch (Exception ex) when (ex is AskTimeoutException or TaskCanceledException or TimeoutException)
{
outcome = new AlarmCommandOutcome(cmd.AlarmId, cmd.Operation, AlarmCommandVerdict.OutcomeUnknown,
$"OutcomeUnknown: the scripted-alarm engine did not answer within {timeout.TotalSeconds:0.#} s; the command may still apply.",
TimedOut: true);
}
catch (Exception ex)
{
outcome = new AlarmCommandOutcome(cmd.AlarmId, cmd.Operation, AlarmCommandVerdict.OutcomeUnknown,
"OutcomeUnknown: " + ex.GetType().Name);
}
if (outcome.Verdict != AlarmCommandVerdict.NotApplied)
{
try
{
broadcast(cmd);
}
catch (Exception ex)
{
// The peer's copy only; the caller's answer is the local verdict.
logger.LogWarning(ex, "Failed to broadcast alarm command {Operation} for {AlarmId} to the redundant peer",
cmd.Operation, cmd.AlarmId);
}
}
return outcome;
}
}