diff --git a/src/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Galaxy/GalaxyDriver.cs b/src/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Galaxy/GalaxyDriver.cs index 46cf7e968..dec26e1d6 100644 --- a/src/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Galaxy/GalaxyDriver.cs +++ b/src/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Galaxy/GalaxyDriver.cs @@ -165,6 +165,10 @@ public sealed class GalaxyDriver // grpc-data A1: the alarm refs (RawPaths) of the live subscription — what a sentinel signal covers. Under // _alarmHandlersLock, replaced with the handle. private IReadOnlyList _alarmSubscriptionRefs = Array.Empty(); + // A1 review round 2 F4: the live alarm subscription when the feed's current stream opened (guarded by + // _alarmHandlersLock). A sentinel signals only if it is still the live one — entries delivered before a later + // subscription (or while none was live) were not delivered under it. + private GalaxyAlarmSubscriptionHandle? _streamOpenedUnder; // Production runtime owned by InitializeAsync (BuildProductionRuntimeAsync). The // driver builds these when it opens a real gw session; tests bypass them by @@ -1083,6 +1087,7 @@ public sealed class GalaxyDriver { alarmFeed.OnAlarmTransition -= OnAlarmFeedTransition; alarmFeed.SnapshotDelivered -= OnAlarmFeedSnapshotDelivered; + alarmFeed.StreamOpened -= OnAlarmFeedStreamOpened; _alarmFeedWired = false; } if (releaseFeed) _alarmFeed = null; @@ -2740,6 +2745,7 @@ public sealed class GalaxyDriver if (_alarmFeedWired) return; _alarmFeed.OnAlarmTransition += OnAlarmFeedTransition; _alarmFeed.SnapshotDelivered += OnAlarmFeedSnapshotDelivered; + _alarmFeed.StreamOpened += OnAlarmFeedStreamOpened; _alarmFeed.Start(); _alarmFeedWired = true; } @@ -2757,7 +2763,7 @@ public sealed class GalaxyDriver // StreamAlarms feed, and the server filters by source node before raising Part 9 // conditions. The subscription handle is a sentinel the server uses for // symmetric Unsubscribe. Same shape AbCip uses. - EnsureAlarmFeedStarted(); + // A1 review round 2 F4: the handle is live BEFORE the feed starts, so the first stream opens under it. var handle = new GalaxyAlarmSubscriptionHandle(Guid.NewGuid().ToString("N")); lock (_alarmHandlersLock) { @@ -2773,6 +2779,16 @@ public sealed class GalaxyDriver _alarmSubscriptions.Add(handle); _alarmSubscriptionRefs = sourceNodeIds.ToArray(); } + try { EnsureAlarmFeedStarted(); } + catch + { + lock (_alarmHandlersLock) + { + _alarmSubscriptions.Remove(handle); + if (_alarmSubscriptions.Count == 0) _alarmSubscriptionRefs = Array.Empty(); + } + throw; + } return Task.FromResult(handle); } @@ -3020,6 +3036,12 @@ public sealed class GalaxyDriver } } + /// A1 review round 2 F4: a feed stream (re)opened — record the subscription its snapshot is delivered under. + private void OnAlarmFeedStreamOpened(object? sender, EventArgs e) + { + lock (_alarmHandlersLock) _streamOpenedUnder = _alarmSubscriptions.Count > 0 ? _alarmSubscriptions[0] : null; + } + /// /// grpc-data A1 (§5.1, Galaxy row): the feed's snapshot_complete sentinel, on the feed thread after /// the snapshot entries were bridged. Signals the live subscription's refs; nothing while unsubscribed (G5). @@ -3030,6 +3052,12 @@ public sealed class GalaxyDriver lock (_alarmHandlersLock) { if (_alarmSubscriptions.Count == 0) return; + // A1 review round 2 F4: only a stream whose whole snapshot was delivered under THIS subscription. + if (!ReferenceEquals(_alarmSubscriptions[0], _streamOpenedUnder)) + { + _logger.LogDebug("GalaxyDriver alarm sentinel for a stream opened under an earlier (or no) subscription — not signalled; the next stream's sentinel will."); + return; + } refs = _alarmSubscriptionRefs; } diff --git a/src/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Galaxy/Runtime/GatewayGalaxyAlarmFeed.cs b/src/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Galaxy/Runtime/GatewayGalaxyAlarmFeed.cs index 4a1e20e6e..010373705 100644 --- a/src/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Galaxy/Runtime/GatewayGalaxyAlarmFeed.cs +++ b/src/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Galaxy/Runtime/GatewayGalaxyAlarmFeed.cs @@ -77,6 +77,9 @@ internal sealed class GatewayGalaxyAlarmFeed : IGalaxyAlarmFeed /// public event EventHandler? SnapshotDelivered; + /// + public event EventHandler? StreamOpened; + /// Initializes a new instance of the class. /// A factory delegate that opens the alarm stream. /// An optional logger for diagnostic output. @@ -127,6 +130,10 @@ internal sealed class GatewayGalaxyAlarmFeed : IGalaxyAlarmFeed AlarmFilterPrefix = _alarmFilterPrefix, }; + // A1 review round 2 F4: before any message of this stream, on this (the dispatch) thread. + try { StreamOpened?.Invoke(this, EventArgs.Empty); } + catch (Exception ex) { _logger.LogWarning(ex, "Galaxy alarm feed StreamOpened handler threw — continuing."); } + await foreach (var message in _streamFactory(request, ct) .WithCancellation(ct).ConfigureAwait(false)) { diff --git a/src/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Galaxy/Runtime/IGalaxyAlarmFeed.cs b/src/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Galaxy/Runtime/IGalaxyAlarmFeed.cs index a68004b91..b651cbbb6 100644 --- a/src/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Galaxy/Runtime/IGalaxyAlarmFeed.cs +++ b/src/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Galaxy/Runtime/IGalaxyAlarmFeed.cs @@ -46,4 +46,11 @@ internal interface IGalaxyAlarmFeed : IAsyncDisposable /// The default accessors make it a no-op for feeds with no snapshot phase. /// event EventHandler? SnapshotDelivered { add { } remove { } } + + /// + /// Raised synchronously on the feed thread each time a stream is (re)opened, before any of its messages (A1 review + /// round 2 F4): the driver records which alarm subscription the stream's snapshot is delivered under, and its + /// sentinel signals only if that subscription is still the live one. The default accessors make it a no-op. + /// + event EventHandler? StreamOpened { add { } remove { } } } diff --git a/tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Galaxy.Tests/Runtime/GalaxyInitialAlarmStateSignalTests.cs b/tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Galaxy.Tests/Runtime/GalaxyInitialAlarmStateSignalTests.cs index df84cf397..c37bffc9f 100644 --- a/tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Galaxy.Tests/Runtime/GalaxyInitialAlarmStateSignalTests.cs +++ b/tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Galaxy.Tests/Runtime/GalaxyInitialAlarmStateSignalTests.cs @@ -88,6 +88,41 @@ public sealed class GalaxyInitialAlarmStateSignalTests trace.ShouldBe(["signal:" + Tank1]); } + /// + /// Review round 2 F4: a stream whose snapshot entries arrived while no subscription was live (they were dropped) + /// must not signal at its sentinel just because a subscription is live by then — the signal would cover refs whose + /// state never reached the host. The next stream, opened under the live subscription, signals. + /// + [Fact] + public async Task A_sentinel_for_a_stream_opened_under_an_earlier_subscription_does_not_signal() + { + await using var streams = new ScriptedStreams(); + using var driver = NewDriver(streams.Feed); + var trace = Record(driver); + + var first = await driver.SubscribeAlarmsAsync([Tank1], Ct); + var s = await streams.NextAsync(); + await driver.UnsubscribeAlarmsAsync(first, Ct); + var sentinelSeen = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + var entryHandled = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); + streams.Feed.OnAlarmTransition += (_, _) => entryHandled.TrySetResult(); + await s.WriteAsync(Snapshot(Tank1, AlarmConditionState.Active), Ct); // dropped: nothing is subscribed + await entryHandled.Task.WaitAsync(TimeSpan.FromSeconds(10), Ct); + await driver.SubscribeAlarmsAsync([Tank1], Ct); // live again BEFORE the sentinel + streams.Feed.SnapshotDelivered += (_, _) => sentinelSeen.TrySetResult(); + await s.WriteAsync(new AlarmFeedMessage { SnapshotComplete = true }, Ct); + await sentinelSeen.Task.WaitAsync(TimeSpan.FromSeconds(10), Ct); + trace.ShouldBeEmpty(); + + // Positive control: the next stream opens under the live subscription and its sentinel signals. + s.Complete(new InvalidOperationException("reopen")); + var s2 = await streams.NextAsync(); + await s2.WriteAsync(Snapshot(Tank1, AlarmConditionState.Active), Ct); + await s2.WriteAsync(new AlarmFeedMessage { SnapshotComplete = true }, Ct); + await TestWait.UntilAsync(() => trace.Count >= 2, "the reopened stream's event and signal", () => Dump(trace)); + trace.ShouldBe(["event:" + Tank1, "signal:" + Tank1]); + } + /// /// (vii) a stream reopen re-signals; a deploy that adds a condition (re-subscribe with the larger set) is /// covered by the NEXT sentinel, not before it.