Merge #958: scripted-alarm refused broadcast copy settles the expired ask's LateOutcome -> mutation-guarded revert (30 s window, 1024 cap)
This commit is contained in:
@@ -206,6 +206,40 @@ public sealed class ScriptedAlarmHostActor : ReceiveActor
|
||||
private readonly Queue<string> _recentCommandOrder = new();
|
||||
internal const int RecentCommandCapacity = 1024;
|
||||
|
||||
/// <summary>
|
||||
/// #958: how long after this host answered a direct ask "the broadcast copy decides" it waits for that copy before
|
||||
/// settling the ask's <see cref="AlarmCommandOutcome.LateOutcome"/> as unknown. The copy is published right after the
|
||||
/// asker's bound, from this process, so one that is coming at all is queued here long before this.
|
||||
/// </summary>
|
||||
public static readonly TimeSpan DefaultBroadcastDecisionWindow = TimeSpan.FromSeconds(30);
|
||||
|
||||
/// <summary>
|
||||
/// #958: direct asks answered "the broadcast copy decides" (expired before the engine reached them, or never started
|
||||
/// before their asker's deadline), keyed by <see cref="AlarmCommand.CommandId"/>. The answer's
|
||||
/// <see cref="AlarmCommandOutcome.LateOutcome"/> completes with what this host's engine does with the broadcast copy,
|
||||
/// so the node manager can revert the method's node change when the engine refuses that copy — before #958 nobody
|
||||
/// awaited a broadcast copy and the node kept the change until the next good deploy.
|
||||
/// </summary>
|
||||
private readonly Dictionary<string, PendingBroadcastDecision> _broadcastDecisions = new(StringComparer.Ordinal);
|
||||
private readonly TimeSpan _broadcastDecisionWindow;
|
||||
|
||||
/// <summary>#958: one direct ask waiting on its broadcast copy.</summary>
|
||||
private sealed class PendingBroadcastDecision(AlarmCommand command)
|
||||
{
|
||||
/// <summary>The command the ask carried.</summary>
|
||||
public AlarmCommand Command { get; } = command;
|
||||
|
||||
/// <summary>Completed with the broadcast copy's local outcome. Continuations run asynchronously: the node manager's
|
||||
/// revert takes the SDK Lock, which must never run on this actor's thread.</summary>
|
||||
public TaskCompletionSource<AlarmCommandOutcome> Outcome { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
|
||||
/// <summary>The expiry timer, cancelled once the copy decided.</summary>
|
||||
public ICancelable? Expiry { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>#958: the broadcast copy of a pending direct ask did not arrive within the window.</summary>
|
||||
private sealed record BroadcastDecisionExpired(string CommandId, PendingBroadcastDecision Pending);
|
||||
|
||||
/// <summary>Cached cluster DistributedPubSub mediator, resolved once in <see cref="PreStart"/> (on the
|
||||
/// actor thread) and reused for every emission instead of re-resolving it per-publish.</summary>
|
||||
private IActorRef _mediator = null!;
|
||||
@@ -262,8 +296,9 @@ public sealed class ScriptedAlarmHostActor : ReceiveActor
|
||||
Func<int>? driverMemberCountProvider = null,
|
||||
ITelemetryLocalHub? telemetryHub = null,
|
||||
TimeSpan? commandTimeout = null,
|
||||
ScriptedAlarmCommandReadinessView? readiness = null) =>
|
||||
Akka.Actor.Props.Create(() => new ScriptedAlarmHostActor(publishActor, mux, engineFactory, localNode, driverMemberCountProvider, telemetryHub, commandTimeout, readiness));
|
||||
ScriptedAlarmCommandReadinessView? readiness = null,
|
||||
TimeSpan? broadcastDecisionWindow = null) =>
|
||||
Akka.Actor.Props.Create(() => new ScriptedAlarmHostActor(publishActor, mux, engineFactory, localNode, driverMemberCountProvider, telemetryHub, commandTimeout, readiness, broadcastDecisionWindow));
|
||||
|
||||
/// <summary>Factory-built incarnation: owns (and on every stop disposes) the engine it builds.</summary>
|
||||
/// <param name="publishActor">The OPC UA publish actor emissions are bridged to.</param>
|
||||
@@ -275,6 +310,8 @@ public sealed class ScriptedAlarmHostActor : ReceiveActor
|
||||
/// <param name="commandTimeout">Bound on one inbound alarm command's engine op.</param>
|
||||
/// <param name="readiness">#937: where the outcome of every completed load is published for the node manager's
|
||||
/// synchronous pre-check. Null ⇒ none (tests).</param>
|
||||
/// <param name="broadcastDecisionWindow">#958: how long a direct ask answered "the broadcast copy decides" waits for that
|
||||
/// copy; null ⇒ <see cref="DefaultBroadcastDecisionWindow"/>.</param>
|
||||
public ScriptedAlarmHostActor(
|
||||
IActorRef publishActor,
|
||||
IActorRef? mux,
|
||||
@@ -283,8 +320,10 @@ public sealed class ScriptedAlarmHostActor : ReceiveActor
|
||||
Func<int>? driverMemberCountProvider = null,
|
||||
ITelemetryLocalHub? telemetryHub = null,
|
||||
TimeSpan? commandTimeout = null,
|
||||
ScriptedAlarmCommandReadinessView? readiness = null)
|
||||
: this(publishActor, mux, Build(engineFactory), localNode, driverMemberCountProvider, telemetryHub, ownsEngine: true, commandTimeout, readiness)
|
||||
ScriptedAlarmCommandReadinessView? readiness = null,
|
||||
TimeSpan? broadcastDecisionWindow = null)
|
||||
: this(publishActor, mux, Build(engineFactory), localNode, driverMemberCountProvider, telemetryHub, ownsEngine: true, commandTimeout, readiness,
|
||||
broadcastDecisionWindow)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -319,7 +358,8 @@ public sealed class ScriptedAlarmHostActor : ReceiveActor
|
||||
NodeId? localNode = null,
|
||||
Func<int>? driverMemberCountProvider = null,
|
||||
ITelemetryLocalHub? telemetryHub = null)
|
||||
: this(publishActor, mux, (upstream, engine), localNode, driverMemberCountProvider, telemetryHub, ownsEngine: false, commandTimeout: null, readiness: null)
|
||||
: this(publishActor, mux, (upstream, engine), localNode, driverMemberCountProvider, telemetryHub, ownsEngine: false, commandTimeout: null, readiness: null,
|
||||
broadcastDecisionWindow: null)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -332,7 +372,8 @@ public sealed class ScriptedAlarmHostActor : ReceiveActor
|
||||
ITelemetryLocalHub? telemetryHub,
|
||||
bool ownsEngine,
|
||||
TimeSpan? commandTimeout,
|
||||
ScriptedAlarmCommandReadinessView? readiness)
|
||||
ScriptedAlarmCommandReadinessView? readiness,
|
||||
TimeSpan? broadcastDecisionWindow)
|
||||
{
|
||||
_readiness = readiness;
|
||||
// A new incarnation has not loaded yet: whatever the previous one published no longer describes this engine.
|
||||
@@ -346,6 +387,7 @@ public sealed class ScriptedAlarmHostActor : ReceiveActor
|
||||
_engine = pair.Engine;
|
||||
_ownsEngine = ownsEngine;
|
||||
_commandTimeout = commandTimeout is { } t && t > TimeSpan.Zero ? t : DefaultCommandTimeout;
|
||||
_broadcastDecisionWindow = broadcastDecisionWindow is { } w && w > TimeSpan.Zero ? w : DefaultBroadcastDecisionWindow;
|
||||
_localNode = localNode;
|
||||
_driverMemberCountProvider = driverMemberCountProvider;
|
||||
_telemetryHub = telemetryHub;
|
||||
@@ -382,6 +424,8 @@ public sealed class ScriptedAlarmHostActor : ReceiveActor
|
||||
ReceiveAsync<AlarmCommand>(OnAlarmCommand);
|
||||
// #937: the node-local ask behind an OPC UA Part 9 method — same op, plus a reply carrying the verdict.
|
||||
ReceiveAsync<ExecuteAlarmCommand>(OnExecuteAlarmCommand);
|
||||
// #958: a direct ask's broadcast copy never arrived — settle what the ask's answer still promises.
|
||||
Receive<BroadcastDecisionExpired>(OnBroadcastDecisionExpired);
|
||||
// A faulted LoadAsync pipes back an AlarmsLoadFailed carrying its generation (see OnApply) — log it, publish
|
||||
// readiness if it is the latest load (#953), and stay inert.
|
||||
Receive<AlarmsLoadFailed>(OnLoadFailed);
|
||||
@@ -820,6 +864,7 @@ public sealed class ScriptedAlarmHostActor : ReceiveActor
|
||||
{
|
||||
_log.Debug("ScriptedAlarmHost: skipping duplicate AlarmCommand {Op} for {AlarmId} (command {CommandId})",
|
||||
cmd.Operation, cmd.AlarmId, id);
|
||||
SettleBroadcastDecision(cmd, known);
|
||||
replyTo?.Tell(known);
|
||||
return;
|
||||
}
|
||||
@@ -830,16 +875,64 @@ public sealed class ScriptedAlarmHostActor : ReceiveActor
|
||||
{
|
||||
_log.Debug("ScriptedAlarmHost: ignoring AlarmCommand {Op} for unowned alarm {AlarmId}",
|
||||
cmd.Operation, cmd.AlarmId);
|
||||
replyTo?.Tell(new AlarmCommandOutcome(cmd.AlarmId, cmd.Operation, AlarmCommandVerdict.NotApplied,
|
||||
NotLoadedRefusal, NotOwned: true));
|
||||
var notOwned = new AlarmCommandOutcome(cmd.AlarmId, cmd.Operation, AlarmCommandVerdict.NotApplied,
|
||||
NotLoadedRefusal, NotOwned: true);
|
||||
// #958: a direct ask of this node waiting on this copy — nothing here applied it (e.g. a deploy removed the alarm).
|
||||
SettleBroadcastDecision(cmd, notOwned);
|
||||
replyTo?.Tell(notOwned);
|
||||
return;
|
||||
}
|
||||
|
||||
var outcome = await ApplyCommandAsync(cmd);
|
||||
Remember(cmd, outcome);
|
||||
SettleBroadcastDecision(cmd, outcome);
|
||||
replyTo?.Tell(outcome);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// #958: registers a direct ask answered "the broadcast copy decides" and returns what its answer's
|
||||
/// <see cref="AlarmCommandOutcome.LateOutcome"/> carries — the broadcast copy's local outcome once
|
||||
/// <see cref="OnAlarmCommand"/> has decided it, or an unknown one if it never arrives (window) or this host stops.
|
||||
/// Null ⇒ nothing to wait on (no CommandId, or the bounded table is full).
|
||||
/// </summary>
|
||||
private Task<AlarmCommandOutcome>? AwaitBroadcastCopy(AlarmCommand cmd)
|
||||
{
|
||||
if (cmd.CommandId is not { } id) return null;
|
||||
if (_broadcastDecisions.TryGetValue(id, out var existing)) return existing.Outcome.Task;
|
||||
if (_broadcastDecisions.Count >= RecentCommandCapacity)
|
||||
{
|
||||
_log.Warning("ScriptedAlarmHost: {Op} for alarm {AlarmId} — too many commands waiting on their broadcast copy; its outcome is not reported back",
|
||||
cmd.Operation, cmd.AlarmId);
|
||||
return null;
|
||||
}
|
||||
|
||||
var pending = new PendingBroadcastDecision(cmd);
|
||||
pending.Expiry = Context.System.Scheduler.ScheduleTellOnceCancelable(_broadcastDecisionWindow, Self,
|
||||
new BroadcastDecisionExpired(id, pending), Self);
|
||||
_broadcastDecisions[id] = pending;
|
||||
return pending.Outcome.Task;
|
||||
}
|
||||
|
||||
/// <summary>#958: hands the broadcast copy's local outcome to the direct ask waiting on it, if any.</summary>
|
||||
private void SettleBroadcastDecision(AlarmCommand cmd, AlarmCommandOutcome outcome)
|
||||
{
|
||||
if (cmd.CommandId is not { } id || !_broadcastDecisions.Remove(id, out var pending)) return;
|
||||
pending.Expiry?.Cancel();
|
||||
pending.Outcome.TrySetResult(outcome);
|
||||
}
|
||||
|
||||
private void OnBroadcastDecisionExpired(BroadcastDecisionExpired msg)
|
||||
{
|
||||
// Reference check: only the registration this timer was armed for.
|
||||
if (!_broadcastDecisions.TryGetValue(msg.CommandId, out var pending) || !ReferenceEquals(pending, msg.Pending)) return;
|
||||
_broadcastDecisions.Remove(msg.CommandId);
|
||||
_log.Warning("ScriptedAlarmHost: the broadcast copy of command {CommandId} did not arrive within {Window}; its outcome stays unknown",
|
||||
msg.CommandId, _broadcastDecisionWindow);
|
||||
pending.Outcome.TrySetResult(new AlarmCommandOutcome(pending.Command.AlarmId, pending.Command.Operation, AlarmCommandVerdict.OutcomeUnknown,
|
||||
$"OutcomeUnknown: the broadcast copy did not reach this server's scripted-alarm engine within {_broadcastDecisionWindow.TotalSeconds:0.#} s.",
|
||||
TimedOut: true));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// #937: the node-local ask behind an OPC UA Part 9 method. Applies the command exactly as
|
||||
/// <see cref="OnAlarmCommand"/> does (once — a broadcast copy that already arrived is not re-applied; its recorded
|
||||
@@ -863,8 +956,9 @@ public sealed class ScriptedAlarmHostActor : ReceiveActor
|
||||
{
|
||||
_log.Warning("ScriptedAlarmHost: {Op} for alarm {AlarmId} skipped on the direct path — its asker stopped waiting; the broadcast copy applies",
|
||||
cmd.Operation, cmd.AlarmId);
|
||||
// #958: the answer still carries what the broadcast copy does here, so a refusal of it reverts the node.
|
||||
replyTo.Tell(new AlarmCommandOutcome(cmd.AlarmId, cmd.Operation, AlarmCommandVerdict.OutcomeUnknown,
|
||||
"OutcomeUnknown: the ask expired before the engine reached it.", TimedOut: true));
|
||||
"OutcomeUnknown: the ask expired before the engine reached it.", TimedOut: true) { LateOutcome = AwaitBroadcastCopy(cmd) });
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -882,7 +976,7 @@ public sealed class ScriptedAlarmHostActor : ReceiveActor
|
||||
{
|
||||
replyTo.Tell(new AlarmCommandOutcome(cmd.AlarmId, cmd.Operation, AlarmCommandVerdict.OutcomeUnknown,
|
||||
"OutcomeUnknown: the engine did not start the command before its asker stopped waiting; the broadcast copy decides.",
|
||||
TimedOut: true));
|
||||
TimedOut: true) { LateOutcome = AwaitBroadcastCopy(cmd) });
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -1076,6 +1170,14 @@ public sealed class ScriptedAlarmHostActor : ReceiveActor
|
||||
// unless this is a RESTART around a legacy Props-captured engine, which the next incarnation will
|
||||
// re-attach to (R02:H1).
|
||||
_cts.Cancel();
|
||||
// #958: asks waiting on a broadcast copy this incarnation will never decide — their outcome stays unknown.
|
||||
foreach (var pending in _broadcastDecisions.Values)
|
||||
{
|
||||
pending.Expiry?.Cancel();
|
||||
pending.Outcome.TrySetResult(new AlarmCommandOutcome(pending.Command.AlarmId, pending.Command.Operation, AlarmCommandVerdict.OutcomeUnknown,
|
||||
"OutcomeUnknown: the scripted-alarm host stopped before the broadcast copy was decided."));
|
||||
}
|
||||
_broadcastDecisions.Clear();
|
||||
_engine.OnEvent -= _onEngineEvent;
|
||||
_engine.OnInputsEstablished -= _onInputsEstablished;
|
||||
if (_ownsEngine || !_restarting) _engine.Dispose();
|
||||
|
||||
Reference in New Issue
Block a user