Files
lmxopcua/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/Drivers/NativeAlarmAckPreflightView.cs
T
Joseph Doherty f54922f94c fix(alarms): revert a native ack that reaches no driver; refuse native acks while the host is Stale (lmxopcua#765)
Residuals of #760:

1. An ack routed to a disabled / not-yet-spawned driver, or to a stubbed child
   (driver does not run on this node, or its factory rejected the config), was
   dropped and left the condition Acknowledged. DriverHostActor now reverts it
   through the same NativeAlarmProjector.TryRevertAcknowledge path as a refused
   ack.

2. A native ack arriving while the host is Stale was dropped before the projector
   recorded it, so it could never be reverted. Stale now marks the whole
   NativeAlarmAckPreflightView unavailable (SetHostUnavailable), so the node
   manager refuses every native ack synchronously; BecomeSteady clears it.

3. (keyed sessions without a Galaxy identity) is not addressed.

Tests: DriverHostActorNativeAlarmTests' client-ack test and the FOCAS respawn
theory now start the host's own driver (AckingAlarmDriver; a factory building a
real FocasDriver per spawn over per-instance fake CNCs, with a real config-change
respawn) - both fail against the fix in their old shape. New: ack to a disabled
driver reverts, ack to a stubbed driver reverts, a Stale host refuses through the
pre-check view - all three fail with the fix mutated out. Runtime.Tests 943/0/3.
Docs: AlarmTracking.md, .claude/rules/alarms.md.

Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
2026-11-09 14:03:59 -05:00

63 lines
3.5 KiB
C#

using System.Collections.Immutable;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
namespace ZB.MOM.WW.OtOpcUa.Runtime.Drivers;
/// <summary>
/// The lock-free snapshot the OPC UA node manager consults to refuse, synchronously, a native-condition
/// Acknowledge the owning driver would refuse anyway (lmxopcua#760). <see cref="DriverHostActor"/> owns
/// the condition → driver map and the driver objects, and publishes a fresh immutable snapshot whenever
/// either changes; <see cref="Check"/> is a single volatile read, safe under the SDK node-manager lock.
/// </summary>
/// <remarks>
/// Racy by design against a change published a moment later — it covers static misconfiguration only. An
/// ack it lets through can still be refused or fail at the driver; that path reverts the condition to
/// unacknowledged (<see cref="DriverHostActor"/>'s <c>NativeAlarmAckFailed</c> handling).
/// </remarks>
public sealed class NativeAlarmAckPreflightView
{
private volatile ImmutableDictionary<string, IAlarmAcknowledgePreflight> _byConditionNodeId =
ImmutableDictionary<string, IAlarmAcknowledgePreflight>.Empty.WithComparers(StringComparer.Ordinal);
private volatile string? _hostUnavailableReason;
/// <summary>
/// Returns why the driver owning <paramref name="conditionNodeId"/> would refuse an acknowledge from a
/// session authenticated with <paramref name="apiKeyId"/>, or null to proceed (unknown condition, a
/// driver without a pre-check, or one that cannot tell without I/O).
/// </summary>
/// <param name="conditionNodeId">The bare condition NodeId identifier the node manager routes by.</param>
/// <param name="apiKeyId">The session's API key id, or null for anonymous.</param>
/// <returns>The refusal reason, or null.</returns>
public string? Check(string conditionNodeId, string? apiKeyId)
{
if (string.IsNullOrEmpty(conditionNodeId)) return null;
// A host that cannot route any native ack (Stale — lmxopcua#765) refuses every one, whatever its driver.
if (_hostUnavailableReason is { } unavailable) return unavailable;
if (!_byConditionNodeId.TryGetValue(conditionNodeId, out var preflight)) return null;
try
{
return preflight.GetAcknowledgeRefusal(apiKeyId);
}
catch
{
// A buggy pre-check must never break the Part 9 method path; let the ack proceed (the driver's own
// refusal + the host's revert still apply).
return null;
}
}
/// <summary>
/// Marks the whole driver host unable to route a native acknowledge (lmxopcua#765): while set,
/// <see cref="Check"/> refuses every condition with <paramref name="reason"/>. Null clears it. Called by
/// <see cref="DriverHostActor"/> only — set on entering <c>Stale</c>, cleared on returning to <c>Steady</c>.
/// </summary>
/// <param name="reason">Why no native ack can be routed, or null when acks are routable.</param>
internal void SetHostUnavailable(string? reason) => _hostUnavailableReason = reason;
/// <summary>Replaces the snapshot. Called by <see cref="DriverHostActor"/> only.</summary>
/// <param name="byConditionNodeId">Condition NodeId → the owning driver's pre-check.</param>
internal void Publish(IEnumerable<KeyValuePair<string, IAlarmAcknowledgePreflight>> byConditionNodeId) =>
_byConditionNodeId = ImmutableDictionary.CreateRange(StringComparer.Ordinal, byConditionNodeId);
}