068124d1ae
#951: the hosted-service Ask lives LateVerdictWindow (30 s) past the caller's bound (ExecuteAlarmCommand deadline unchanged); the OutcomeUnknown it returns carries the still-running ask as AlarmCommandOutcome.LateOutcome (process-local). The node manager watches it (or the asker task itself when its backstop expired, following unknown chains) and reverts the node on a late NotApplied, under the existing per-condition mutation-counter guard. Both CallAsync and remote ExecuteConditionMethodAsync paths. #952: ScriptedAlarmEngine throws ScriptedAlarmCommandNotStartedException (an OCE) when a command is cancelled before it acquires _evalGate; a cancellation inside SaveAsync stays a plain OCE (unknown). The host maps not-started to NotApplied and remembers it, except on the direct ask once its asker's deadline passed (asker already answered unknown and broadcast): then unknown + not remembered, so the broadcast copy decides (as review r2 M2). #953: a faulted load pipes back AlarmsLoadFailed(cause, generation); a failure from a generation a newer apply superseded publishes no readiness (it would refuse every command while the newer load is in flight). Red/green: each fix reverted individually turns its tests red (Core 1, Runtime 3, Host 1, OpcUaServer.IntegrationTests 4). Co-Authored-By: Claude <noreply@anthropic.com>
99 lines
5.4 KiB
C#
99 lines
5.4 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>
|
|
/// #951: how long past the caller's bound the local host's answer is still collected. The host bounds one engine op by
|
|
/// its command timeout (10 s) and skips an ask whose deadline passed before it started, so an answer that is coming at
|
|
/// all comes inside this window.
|
|
/// </summary>
|
|
internal static readonly TimeSpan LateVerdictWindow = TimeSpan.FromSeconds(30);
|
|
|
|
/// <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. Its second argument is the
|
|
/// caller's bound (the host skips the command once it has passed); the returned task may complete later than that — up to
|
|
/// <see cref="LateVerdictWindow"/> past it — and that late answer rides the timeout outcome's
|
|
/// <see cref="AlarmCommandOutcome.LateOutcome"/> (#951).</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;
|
|
Task<AlarmCommandOutcome>? ask = null;
|
|
try
|
|
{
|
|
ask = askLocal(cmd, timeout);
|
|
outcome = await ask.WaitAsync(timeout).ConfigureAwait(false);
|
|
}
|
|
catch (Exception ex) when (ex is AskTimeoutException or TaskCanceledException or TimeoutException)
|
|
{
|
|
// #951: the caller stops waiting, but the host's answer is still collected — a refusal arriving now lets the
|
|
// node manager put the node back. Only for the caller's bound expiring (an ask that faulted in time has no
|
|
// later answer); an ask completing in the same instant is still handed over, already complete.
|
|
var late = ex is TimeoutException ? ask : null;
|
|
// The late ask usually ends in an AskTimeoutException nobody may observe: observe it here.
|
|
late?.ContinueWith(static t => _ = t.Exception, CancellationToken.None,
|
|
TaskContinuationOptions.OnlyOnFaulted | TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);
|
|
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) { LateOutcome = late };
|
|
}
|
|
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;
|
|
}
|
|
}
|