diff --git a/src/Core/ZB.MOM.WW.OtOpcUa.Commons/OpcUa/DeferredAddressSpaceSink.cs b/src/Core/ZB.MOM.WW.OtOpcUa.Commons/OpcUa/DeferredAddressSpaceSink.cs index 7b4bb5d30..10127c038 100644 --- a/src/Core/ZB.MOM.WW.OtOpcUa.Commons/OpcUa/DeferredAddressSpaceSink.cs +++ b/src/Core/ZB.MOM.WW.OtOpcUa.Commons/OpcUa/DeferredAddressSpaceSink.cs @@ -53,6 +53,10 @@ public sealed class DeferredAddressSpaceSink : IOpcUaAddressSpaceSink, ISurgical public void WriteAlarmCondition(string alarmNodeId, AlarmConditionSnapshot state, DateTime sourceTimestampUtc, AddressSpaceRealm realm) => _inner.WriteAlarmCondition(alarmNodeId, state, sourceTimestampUtc, realm); + /// + public void WriteAlarmCondition(string alarmNodeId, AlarmConditionSnapshot state, DateTime sourceTimestampUtc, AddressSpaceRealm realm, + bool establishesState) => _inner.WriteAlarmCondition(alarmNodeId, state, sourceTimestampUtc, realm, establishesState); + /// public void WriteAlarmQuality(string alarmNodeId, uint statusCode, DateTime sourceTimestampUtc, AddressSpaceRealm realm) => _inner.WriteAlarmQuality(alarmNodeId, statusCode, sourceTimestampUtc, realm); diff --git a/src/Core/ZB.MOM.WW.OtOpcUa.Commons/OpcUa/IOpcUaAddressSpaceSink.cs b/src/Core/ZB.MOM.WW.OtOpcUa.Commons/OpcUa/IOpcUaAddressSpaceSink.cs index fef6cfd6b..dc41b4118 100644 --- a/src/Core/ZB.MOM.WW.OtOpcUa.Commons/OpcUa/IOpcUaAddressSpaceSink.cs +++ b/src/Core/ZB.MOM.WW.OtOpcUa.Commons/OpcUa/IOpcUaAddressSpaceSink.cs @@ -81,6 +81,20 @@ public interface IOpcUaAddressSpaceSink /// the condition was materialised under). void WriteAlarmCondition(string alarmNodeId, AlarmConditionSnapshot state, DateTime sourceTimestampUtc, AddressSpaceRealm realm); + /// + /// that says + /// whether the projection ESTABLISHES the condition's state (A1 §5.1 T2). for a projection + /// no source decided — the #760 ack-revert re-projects the projector's cached prior state — so it never makes a + /// withdrawn condition known again (review round 2 F1). The default ignores the flag. + /// + /// The condition node id. + /// The full condition state. + /// The source timestamp in UTC. + /// The condition's realm. + /// Whether this projection establishes the condition's state. + void WriteAlarmCondition(string alarmNodeId, AlarmConditionSnapshot state, DateTime sourceTimestampUtc, AddressSpaceRealm realm, + bool establishesState) => WriteAlarmCondition(alarmNodeId, state, sourceTimestampUtc, realm); + /// Annotate a materialised condition's source-data quality OUT OF BAND from any alarm /// transition (used by the driver-connectivity path: comms lost → , /// restored → ). Sets ONLY the condition's Quality — never diff --git a/src/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer/OtOpcUaNodeManager.cs b/src/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer/OtOpcUaNodeManager.cs index e78841536..b3883eeca 100644 --- a/src/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer/OtOpcUaNodeManager.cs +++ b/src/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer/OtOpcUaNodeManager.cs @@ -1327,6 +1327,11 @@ public sealed partial class OtOpcUaNodeManager : CustomNodeManager2, INodeManage /// The timestamp of the alarm state change in UTC. /// The address-space realm (Raw or Uns) the alarm node lives in. public void WriteAlarmCondition(string alarmNodeId, AlarmConditionSnapshot state, DateTime sourceTimestampUtc, AddressSpaceRealm realm) + => WriteAlarmCondition(alarmNodeId, state, sourceTimestampUtc, realm, establishesState: true); + + /// + public void WriteAlarmCondition(string alarmNodeId, AlarmConditionSnapshot state, DateTime sourceTimestampUtc, AddressSpaceRealm realm, + bool establishesState) { ArgumentException.ThrowIfNullOrEmpty(alarmNodeId); ArgumentNullException.ThrowIfNull(state); @@ -1428,7 +1433,8 @@ public sealed partial class OtOpcUaNodeManager : CustomNodeManager2, INodeManage // scripted known-ness comes ONLY from MarkConditionsKnown (the engine's inputs-established flip). // The record is raised by ReportConditionEvent when the gate fires, otherwise here as a state-only // record (ConditionClass and the other silent fields). - if (_nativeAlarmNodeIds.Contains(key)) _conditionStateKnown.Add(key); + // Review round 2 F1: a projection no source decided (the #760 ack-revert) never establishes state either. + if (establishesState && _nativeAlarmNodeIds.Contains(key)) _conditionStateKnown.Add(key); if (fire) { ReportConditionEvent(condition, sourceTimestampUtc); diff --git a/src/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer/SdkAddressSpaceSink.cs b/src/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer/SdkAddressSpaceSink.cs index 655fed51f..9f7e7c5bf 100644 --- a/src/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer/SdkAddressSpaceSink.cs +++ b/src/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer/SdkAddressSpaceSink.cs @@ -91,6 +91,10 @@ public sealed class SdkAddressSpaceSink : IOpcUaAddressSpaceSink, ISurgicalAddre public void WriteAlarmCondition(string alarmNodeId, AlarmConditionSnapshot state, DateTime sourceTimestampUtc, AddressSpaceRealm realm) => _nodeManager.WriteAlarmCondition(alarmNodeId, state, sourceTimestampUtc, realm); + /// + public void WriteAlarmCondition(string alarmNodeId, AlarmConditionSnapshot state, DateTime sourceTimestampUtc, AddressSpaceRealm realm, + bool establishesState) => _nodeManager.WriteAlarmCondition(alarmNodeId, state, sourceTimestampUtc, realm, establishesState); + /// public void WriteAlarmQuality(string alarmNodeId, uint statusCode, DateTime sourceTimestampUtc, AddressSpaceRealm realm) => _nodeManager.WriteAlarmQuality(alarmNodeId, statusCode, sourceTimestampUtc, realm); diff --git a/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/Drivers/DriverHostActor.ConditionSignals.cs b/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/Drivers/DriverHostActor.ConditionSignals.cs index 4de53675f..c51d982b1 100644 --- a/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/Drivers/DriverHostActor.ConditionSignals.cs +++ b/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/Drivers/DriverHostActor.ConditionSignals.cs @@ -78,11 +78,11 @@ public sealed partial class DriverHostActor /// /// A connectivity edge restarts the F5 window (from quality Good) and the backstop; a Bad edge cancels both. - /// Review S2: the Bad edge withdraws "known" from the driver's conditions — the source can no longer vouch for - /// their state, and an alarm raised during the outage would otherwise read known-inactive, Good after the - /// reconnect until the replay. A Good edge withdraws too (ahead of the Good quality annotation) when no Bad edge - /// was seen since the previous Good one, or it comes from a new child incarnation. They become known again only - /// on the reconnected source's next signal (or transition). + /// Review S2 / round 2 F1: every edge withdraws "known" from the driver's conditions — on the Bad edge the source + /// can no longer vouch for their state (an alarm raised during the outage would otherwise read known-inactive, + /// Good after the reconnect until the replay), and the Good edge withdraws again, unconditionally. They become + /// known again only on the reconnected source's next signal (or transition). The Bad edge also forgets the + /// unsignalable set (F2). /// private void OnConditionSignalConnectivity(string driverInstanceId, bool connected, IActorRef sender, long? connectionEpoch) { @@ -99,17 +99,22 @@ public sealed partial class DriverHostActor } var t = Tracker(driverInstanceId); - // A Good edge with no Bad edge since the previous Good one (or from a new incarnation) — withdraw here instead. - var missedBadEdge = connected && (t.GoodSinceUtc is not null || (t.Owner is not null && !Equals(t.Owner, sender))); t.Owner = sender; t.Epoch++; Timers.Cancel(SignalTimeoutKey(driverInstanceId)); Timers.Cancel(BackstopKey(driverInstanceId)); t.Pending.Clear(); t.PendingSinceUtc = null; - if (!connected || missedBadEdge) WithdrawDriverConditionsKnown(driverInstanceId); + // Review round 2 F1: withdraw on EVERY edge — the Good one too, unconditionally (ahead of the Good quality + // annotation): whatever made a condition known since the Bad edge (an ack-revert projection, a missed Bad + // edge, a new incarnation) cannot vouch for the reconnected source's state. + WithdrawDriverConditionsKnown(driverInstanceId); if (!connected) { + // Review round 2 F2: an unsignalable report belongs to the subscription that made it; the reconnected + // subscription reports again if it still cannot signal. Kept, it would arm the backstop on the next Good + // edge and turn a later FAILED refresh into known-by-timer instead of F5's unknown. + t.Unsignalable.Clear(); t.GoodSinceUtc = null; t.ConnectionEpoch = null; return; diff --git a/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/Drivers/DriverHostActor.cs b/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/Drivers/DriverHostActor.cs index e61fa43cb..f1cf33605 100644 --- a/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/Drivers/DriverHostActor.cs +++ b/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/Drivers/DriverHostActor.cs @@ -2680,7 +2680,10 @@ public sealed partial class DriverHostActor : ReceiveActor, IWithTimers, IWithUn "reverting the condition to unacknowledged", _localNode, conditionNodeId, reason); // Native conditions live in the Raw realm at their bare NodeId (the apply pass's single raw condition). _opcUaPublishActor?.Tell(new ZB.MOM.WW.OtOpcUa.Runtime.OpcUa.OpcUaPublishActor.AlarmStateUpdate( - conditionNodeId, snapshot, DateTime.UtcNow, AddressSpaceRealm.Raw)); + conditionNodeId, snapshot, DateTime.UtcNow, AddressSpaceRealm.Raw, + // Review round 2 F1: the projector's cached prior state, not a driver transition — it establishes nothing + // (a condition withdrawn by a comms loss must stay unknown until its driver signals again). + EstablishesState: false)); } /// diff --git a/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/OpcUa/OpcUaPublishActor.cs b/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/OpcUa/OpcUaPublishActor.cs index 8429f9bad..e6f6428d9 100644 --- a/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/OpcUa/OpcUaPublishActor.cs +++ b/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/OpcUa/OpcUaPublishActor.cs @@ -79,8 +79,10 @@ public sealed class OpcUaPublishActor : ReceiveActor, IWithTimers /// A1 (plan §5.1 G3): the emitting source — the driver instance id for a native projection, /// for the scripted host, null when unattributed. Only used to discard a source's /// deferred when one of its deferred projections is dropped or skipped. + /// A1 review round 2 F1: false for a projection no source decided (the #760 ack-revert), + /// which must never make a withdrawn condition known again. public sealed record AlarmStateUpdate(string AlarmNodeId, AlarmConditionSnapshot State, DateTime TimestampUtc, AddressSpaceRealm Realm, - string? Source = null); + string? Source = null, bool EstablishesState = true); /// The / of the scripted host. public const string ScriptedSource = "scripted-alarms"; @@ -787,7 +789,7 @@ public sealed class OpcUaPublishActor : ReceiveActor, IWithTimers if (PlanPipelineBusy) DeferAlarm(msg); try { - _sink.WriteAlarmCondition(msg.AlarmNodeId, msg.State, msg.TimestampUtc, msg.Realm); + _sink.WriteAlarmCondition(msg.AlarmNodeId, msg.State, msg.TimestampUtc, msg.Realm, msg.EstablishesState); Interlocked.Increment(ref _writes); OtOpcUaTelemetry.OpcUaSinkWrite.Add(1, new KeyValuePair("kind", "alarm")); } @@ -1156,7 +1158,7 @@ public sealed class OpcUaPublishActor : ReceiveActor, IWithTimers } alarms++; - try { _sink.WriteAlarmCondition(alarm.AlarmNodeId, alarm.State, alarm.TimestampUtc, alarm.Realm); } + try { _sink.WriteAlarmCondition(alarm.AlarmNodeId, alarm.State, alarm.TimestampUtc, alarm.Realm, alarm.EstablishesState); } catch (Exception ex) { _log.Warning(ex, "OpcUaPublish: replaying a deferred alarm write threw for {Node}", alarm.AlarmNodeId); } } diff --git a/tests/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer.Tests/ConditionStateNodeManagerTests.cs b/tests/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer.Tests/ConditionStateNodeManagerTests.cs index 014b1d38e..609220580 100644 --- a/tests/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer.Tests/ConditionStateNodeManagerTests.cs +++ b/tests/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer.Tests/ConditionStateNodeManagerTests.cs @@ -230,6 +230,24 @@ public sealed class ConditionStateNodeManagerTests : IDisposable await host.DisposeAsync(); } + /// Review round 2 F1: a native projection flagged as not establishing (the #760 ack-revert) applies its state + /// but never makes a withdrawn condition known again. + [Fact] + public async Task F1_A_non_establishing_native_projection_does_not_mark_known() + { + var (host, nm, sink) = await BootAsync(); + MaterialiseAll(nm); + nm.WriteAlarmCondition(Native, State(true, false), DateTime.UtcNow, AddressSpaceRealm.Raw); + nm.MarkConditionsUnknown("drv", [Native], AddressSpaceRealm.Raw).ShouldBe(1); // the Bad edge + + nm.WriteAlarmCondition(Native, State(true, false, message: "reverted"), DateTime.UtcNow, AddressSpaceRealm.Raw, establishesState: false); + + nm.IsConditionStateKnown(Native, AddressSpaceRealm.Raw).ShouldBeFalse(); + sink.Records.Last().ShouldSatisfyAllConditions(r => r.Message.ShouldBe("reverted"), r => r.StateKnown.ShouldBeFalse()); + AssertParity(nm, sink); + await host.DisposeAsync(); + } + [Fact] public async Task T3_The_marker_moves_only_on_a_new_occurrence() { diff --git a/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/Drivers/NativeAlarmInitialStateSignalTests.cs b/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/Drivers/NativeAlarmInitialStateSignalTests.cs index e23e47d39..a184314d4 100644 --- a/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/Drivers/NativeAlarmInitialStateSignalTests.cs +++ b/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/Drivers/NativeAlarmInitialStateSignalTests.cs @@ -157,6 +157,61 @@ public sealed class NativeAlarmInitialStateSignalTests : NativeAlarmSignalTestBa .ConditionIds.ShouldBe([A, B], ignoreOrder: true); } + /// + /// Review round 2 F1 — the exact scenario: comms lost (Bad edge withdraws), an operator acks while the driver is + /// Reconnecting, so the ack fails and the host reverts the condition. That revert is the projector's cached state, + /// not a driver transition: it must not establish the condition (EstablishesState false). And the reconnect's Good + /// edge withdraws again, unconditionally, ahead of the Good quality annotation — so an alarm that re-raised + /// upstream during the outage cannot read known-inactive, Good until the replay. + /// + [Fact] + public void An_ack_revert_during_an_outage_does_not_establish_and_the_Good_edge_withdraws_again() + { + var rig = StartRig(DriverTypeNames.Galaxy, ["hi"]); + rig.Driver.Raise(A).ShouldBeTrue(); + rig.Driver.Signal([A]).ShouldBeTrue(); + rig.Publish.FishForMessage(m => m.Known, TimeSpan.FromSeconds(5), cancellationToken: Ct); + + rig.Host.Tell(new ReconnectDriver("c1", DriverId, "test", Guid.NewGuid()), TestActor); + rig.Publish.FishForMessage(m => !m.Known, TimeSpan.FromSeconds(5), cancellationToken: Ct); + + rig.Host.Tell(new DriverHostActor.RouteNativeAlarmAck(A, null, "operator"), TestActor); + var revert = rig.Publish.FishForMessage(u => u.AlarmNodeId == A && !u.State.Acknowledged, + TimeSpan.FromSeconds(5), cancellationToken: Ct); + revert.EstablishesState.ShouldBeFalse("an ack-revert re-projects cached state; it establishes nothing"); + + // The reconnect: a withdrawal must precede the Good quality annotation. + var withdrawnAgain = false; + var deadline = DateTime.UtcNow + TimeSpan.FromSeconds(30); + while (true) + { + var m = rig.Publish.ReceiveOne(deadline - DateTime.UtcNow, Ct); + m.ShouldNotBeNull("the reconnect's Good quality annotation never arrived"); + if (m is OpcUaPublishActor.MarkConditionsKnown { Known: false } w && w.ConditionIds.Contains(A)) withdrawnAgain = true; + if (m is OpcUaPublishActor.MarkConditionsKnown { Known: true }) throw new ShouldAssertException("marked known before the driver signalled"); + if (m is OpcUaPublishActor.AlarmQualityUpdate { StatusCode: OpcUaStatus.Good }) break; + } + + withdrawnAgain.ShouldBeTrue("the Good edge withdraws unconditionally"); + } + + /// Review round 2 F2: an unsignalable report belongs to the subscription that made it. After a Bad edge the + /// reconnected subscription has not reported, so the backstop must not arm from the stale set — a refresh that then + /// fails leaves the conditions unknown (F5), never known-by-timer. + [Fact] + public void An_unsignalable_report_does_not_survive_a_Bad_edge() + { + // The settle outlasts the window before the Bad edge, so the report is still pending (an elapsed backstop clears it). + var rig = StartRig(DriverTypeNames.OpcUaClient, ["hi"], o => o.WithOpcUaClientSettle(TimeSpan.FromSeconds(3))); + rig.Driver.Signal([A], unsignalable: true).ShouldBeTrue(); + + rig.Host.Tell(new ReconnectDriver("c1", DriverId, "test", Guid.NewGuid()), TestActor); + rig.Publish.FishForMessage(q => q.StatusCode != OpcUaStatus.Good, TimeSpan.FromSeconds(5), cancellationToken: Ct); + // The reconnect's Good edge (the child retries on its reconnect interval). + rig.Publish.FishForMessage(q => q.StatusCode == OpcUaStatus.Good, TimeSpan.FromSeconds(30), cancellationToken: Ct); + AssertNoMark(rig.Publish, TimeSpan.FromSeconds(4.5)); // a stale set would arm the 3 s backstop from this Good edge + } + /// Open question answered — re-arm F5 for refs a deploy adds while the driver is connected. Today every such /// deploy changes the driver's config (its authored RawTags are folded into it), so the driver is respawned: the /// stopped incarnation's conditions are withdrawn, and the new incarnation's Good edge puts EVERY ref — the added one diff --git a/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/Drivers/NativeAlarmSignalTestBase.cs b/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/Drivers/NativeAlarmSignalTestBase.cs index 7be327993..eeb65ca06 100644 --- a/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/Drivers/NativeAlarmSignalTestBase.cs +++ b/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/Drivers/NativeAlarmSignalTestBase.cs @@ -141,7 +141,7 @@ public abstract class NativeAlarmSignalTestBase : RuntimeActorTestBase } } - /// The publish-actor messages that are alarm-state updates or marks, in arrival order, until + /// The publish-actor messages that are alarm-state updates or KNOWN marks (withdrawals skipped), in arrival order, until /// matches (inclusive). /// The probe. /// Stop condition. @@ -156,6 +156,8 @@ public abstract class NativeAlarmSignalTestBase : RuntimeActorTestBase var m = publish.ReceiveOne(deadline - DateTime.UtcNow); if (m is null) break; if (m is not (OpcUaPublishActor.AlarmStateUpdate or OpcUaPublishActor.MarkConditionsKnown)) continue; + // Withdrawals (every connectivity edge sends one, review round 2 F1) are not marks. + if (m is OpcUaPublishActor.MarkConditionsKnown { Known: false }) continue; seen.Add(m); if (until(m)) return seen; }