From 18154eeff487709654f4c1371752af597dbf36a0 Mon Sep 17 00:00:00 2001 From: Joseph Doherty Date: Wed, 25 Nov 2026 11:01:01 -0500 Subject: [PATCH] fix(grpc-data): removal RefreshRequired reaches the holders after their mapping left the deployed set (#936) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit QueueRefreshRequired decided per item with the CURRENT gate (CanSee over the current mapping set), so when a deploy removed the mapping or its connection nobody passed and the §5.2 removal + RefreshRequired was never delivered. Each imported condition instance now records (weakly, per identity) every identity the §5.4 gate admitted to it — the verdict cache's compute is the only place a positive verdict is made, and every delivery, refresh replay and Read/Browse goes through it. A removal's RefreshRequired goes to the collected targets whose identity was admitted to the removed instance. The event carries no condition data (SourceNode=Server); a never-admitted session still gets nothing. OI test pins both: holders told after the gate admits nobody, the ungranted session not (with a must-arrive RefreshEnd control). --- docs/AlarmTracking.md | 7 ++- .../ProjectedConditionRegistry.cs | 42 ++++++++++++++---- .../ImportedConditionEventWireTests.cs | 43 +++++++++++++++++++ 3 files changed, 83 insertions(+), 9 deletions(-) diff --git a/docs/AlarmTracking.md b/docs/AlarmTracking.md index ecb22b25b..8904ae7b1 100644 --- a/docs/AlarmTracking.md +++ b/docs/AlarmTracking.md @@ -863,7 +863,12 @@ This section is what an OPC UA client of the **consumer** sees. Code: `OpcUaServ mappings differs from the previous barrier's (#930: a mapping that stays refused across streams does not repeat it; one that completes leaves the set, so a relapse is announced again); - an imported condition is removed (a complete re-snapshot no longer lists it, a `removed` record, a capacity - eviction, or a secret rotation). Removal emits **no** `NodeDeleted` model change; + eviction, a secret rotation, its mapping or connection leaving the deployed set, a demand linger, or #933's + `MaxUnknownHold`). Removal emits **no** `NodeDeleted` model change. For a removal, "may see" means "its identity was + admitted by the gate to that condition instance" (a delivery, a refresh replay or a Read/Browse of it), recorded per + instance — not the current mapping set, which no longer admits anybody once the removal was caused by the mapping + leaving it (#936). The event carries no condition field (`SourceNode = Server`, `Message = "RefreshRequired"`), and a + session never admitted to the condition is not sent one; - a resync leaves retained EventIds un-re-bindable (see EventIds below). - **Before the first barrier, a refresh returns nothing imported**, because nothing is materialised yet. Conditions appear as their records apply. A RefreshRequired follows only if that barrier is incomplete; a refresh that merely preceded a diff --git a/src/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer/ProjectedConditionRegistry.cs b/src/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer/ProjectedConditionRegistry.cs index cf76bfe5e..4a3d7fd06 100644 --- a/src/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer/ProjectedConditionRegistry.cs +++ b/src/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer/ProjectedConditionRegistry.cs @@ -1,4 +1,5 @@ using System.Collections.Concurrent; +using System.Runtime.CompilerServices; using Microsoft.Extensions.Logging; using Opc.Ua; using Opc.Ua.Server; @@ -54,6 +55,17 @@ internal sealed class ImportedCondition(AlarmConditionState alarm, string connec public long PlacementVersion { get; set; } /// Native (driver-fed) at the origin: Confirm / shelve / enable have no upstream path. public bool IsNative { get; init; } + + /// + /// #936: every identity that passed the §5.4 gate for THIS condition instance (each delivery, refresh replay and + /// Read/Browse of it is gated, so any client that may hold it is here). Weak keys: a closed session's identity is + /// collected with it. Lets the removal RefreshRequired reach the holders even after the mapping that admitted them + /// left the deployed set — the current gate then admits nobody. + /// + private readonly ConditionalWeakTable _admitted = new(); + private static readonly object AdmittedMark = new(); + public void Admit(object identity) => _admitted.AddOrUpdate(identity, AdmittedMark); + public bool WasAdmitted(object identity) => _admitted.TryGetValue(identity, out _); } /// @@ -216,8 +228,12 @@ public sealed partial class OtOpcUaNodeManager : IProjectedConditionSink if (ProjectedConditionAccess is not { } access || identity is not RoleCarryingUserIdentity caller) return false; var generation = access.AuthorityGeneration; var placements = condition.Placements; - return _importedConditions.Verdict(caller, condition, generation, DateTimeOffset.UtcNow, caller.CredentialExpiresUtc, - () => access.CanSee(caller, placements)); + return _importedConditions.Verdict(caller, condition, generation, DateTimeOffset.UtcNow, caller.CredentialExpiresUtc, () => + { + var allowed = access.CanSee(caller, placements); + if (allowed) condition.Admit(caller); // #936: a cached positive verdict was computed (and recorded) here first + return allowed; + }); } /// The marker a queued imported event carries: re-checked at every Publish and Republish (§5.4). @@ -336,8 +352,9 @@ public sealed partial class OtOpcUaNodeManager : IProjectedConditionSink } } // §5.2 "removal + RefreshRequired": a client holding a removed (e.g. Active/Retain) condition is told to refresh, after - // which it no longer lists it. Outside Lock. - if (removed.Count > 0) QueueRefreshRequired(targets, removed); + // which it no longer lists it. Outside Lock. #936: decided by who was ADMITTED to the condition, not by the current + // mapping set — a removal caused by the mapping (or its connection) leaving the deployed set must still tell them. + if (removed.Count > 0) QueueRefreshRequired(targets, removed, removal: true); } /// @@ -412,14 +429,23 @@ public sealed partial class OtOpcUaNodeManager : IProjectedConditionSink QueueRefreshRequired(targets, conditions); } - /// Off Lock: one RefreshRequired per item that may see at least one of the conditions (§5.4 gate). - private void QueueRefreshRequired(RefreshTargets targets, IReadOnlyList conditions) + /// + /// Off Lock: one RefreshRequired per item that may see at least one of the conditions (§5.4 gate). For a + /// (#936) the test is instead "its identity was admitted to the removed instance" — the + /// current gate cannot answer once the mapping is gone. The event carries no condition data (EventType + /// RefreshRequired, SourceNode = Server, SourceName "Server", Message "RefreshRequired", Severity Low), and it still + /// goes only to identities that could see the condition, so an ungranted session learns nothing. + /// + private void QueueRefreshRequired(RefreshTargets targets, IReadOnlyList conditions, bool removal = false) { + Func holds = removal + ? (item, c) => item.EffectiveIdentity is { } identity && c.WasAdmitted(identity) + : ImportedVisibleTo; var send = new List(); foreach (var item in targets.ServerItems) - if (conditions.Any(c => ImportedVisibleTo(item, c))) send.Add(item); + if (conditions.Any(c => holds(item, c))) send.Add(item); foreach (var (item, candidates) in targets.Direct) - if (!targets.ServerItems.Contains(item) && candidates.Any(c => ImportedVisibleTo(item, c))) send.Add(item); + if (!targets.ServerItems.Contains(item) && candidates.Any(c => holds(item, c))) send.Add(item); foreach (var item in send) { var e = new RefreshRequiredEventState(null); diff --git a/tests/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer.IntegrationTests/ImportedConditionEventWireTests.cs b/tests/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer.IntegrationTests/ImportedConditionEventWireTests.cs index 086e3407c..162fb35da 100644 --- a/tests/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer.IntegrationTests/ImportedConditionEventWireTests.cs +++ b/tests/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer.IntegrationTests/ImportedConditionEventWireTests.cs @@ -403,6 +403,49 @@ public sealed class ImportedConditionEventWireTests events.Of(other).Any(IsRefreshRequired).ShouldBeFalse("an item that never saw the removed condition is not told to refresh"); } + [Fact] + public async Task Issue936_a_removal_after_the_mapping_left_the_deployed_set_still_tells_its_holders_and_only_them() + { + var ct = TestContext.Current.CancellationToken; + await using var rig = await ServerEventRig.StartAsync("fu-936-remove", encrypted: false, ct); + var f = Fixture.Install(rig, Granted); + f.Apply(Update(Key1, version: 3, active: true, acked: false, f.TwoPlacements())); + using var granted = await rig.ConnectAsync(anonymous: false, ct); + using var denied = await rig.ConnectAsync(anonymous: true, ct); + var events = new Events(); + var deniedEvents = new Events(); + var grantedSub = await SubscribeAsync(granted, events, ct); + var deniedSub = await SubscribeAsync(denied, deniedEvents, ct); + var server = AddItem(grantedSub, ObjectIds.Server); + var device = AddItem(grantedSub, f.DeviceFolder); + var deniedServer = AddItem(deniedSub, ObjectIds.Server); + await grantedSub.ApplyChangesAsync(ct); + await deniedSub.ApplyChangesAsync(ct); + f.Apply(Update(Key1, version: 4, active: true, acked: true, f.TwoPlacements(), message: "held")); + await Events.WaitUntilAsync(() => events.Of(server).Any(e => Message(e) == "held") && events.Of(device).Any(e => Message(e) == "held")); + + // The deploy removed the mapping: the CURRENT gate now admits nobody (as CanSee does with the mapping gone). + f.Access.Allowed.Clear(); + Interlocked.Increment(ref f.Access.Generation); + var before = rig.Nm.ImportedRefreshRequiredQueued; + rig.Nm.RemoveConditions(Connection, [Key1]); + rig.Nm.ImportedConditionCount.ShouldBe(0); + await Events.WaitUntilAsync(() => events.Of(server).Any(IsRefreshRequired) && events.Of(device).Any(IsRefreshRequired)); + (rig.Nm.ImportedRefreshRequiredQueued - before).ShouldBe(2, "one per item that held it"); + // Content-free: the event is the Server's, with no condition field. + var refresh = events.Of(server).First(IsRefreshRequired); + refresh[SourceNodeF].GetNodeId().ShouldBe(ObjectIds.Server); + refresh[ConditionNameF].TryGetValue(out string _).ShouldBeFalse("no ConditionName"); + refresh[ActiveF].TryGetValue(out bool _).ShouldBeFalse("no ActiveState"); + refresh[AckedF].TryGetValue(out bool _).ShouldBeFalse("no AckedState"); + refresh[ConditionIdF].TryGetValue(out NodeId _).ShouldBeFalse("no ConditionId"); + + // Must-arrive control on the never-admitted session, queued after the removal: its own refresh's RefreshEnd. + await deniedSub.ConditionRefreshAsync(ct); + await Events.WaitUntilAsync(() => deniedEvents.Of(deniedServer).Any(e => e[EventTypeF].GetNodeId() == ObjectTypeIds.RefreshEndEventType)); + deniedEvents.Of(deniedServer).Any(IsRefreshRequired).ShouldBeFalse("an identity never admitted to the condition is not told"); + } + [Fact] public async Task Deviation8_ConditionRefresh_replays_hundreds_of_imported_conditions_in_chunks_between_RefreshStart_and_RefreshEnd() {