fix(grpc-data): removal RefreshRequired reaches the holders after their mapping left the deployed set (#936)

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).
This commit is contained in:
Joseph Doherty
2026-11-25 11:01:01 -05:00
parent 5d946fde55
commit 18154eeff4
3 changed files with 83 additions and 9 deletions
@@ -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; }
/// <summary>Native (driver-fed) at the origin: Confirm / shelve / enable have no upstream path.</summary>
public bool IsNative { get; init; }
/// <summary>
/// #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.
/// </summary>
private readonly ConditionalWeakTable<object, object> _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 _);
}
/// <summary>
@@ -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;
});
}
/// <summary>The marker a queued imported event carries: re-checked at every Publish and Republish (§5.4).</summary>
@@ -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);
}
/// <inheritdoc />
@@ -412,14 +429,23 @@ public sealed partial class OtOpcUaNodeManager : IProjectedConditionSink
QueueRefreshRequired(targets, conditions);
}
/// <summary>Off Lock: one RefreshRequired per item that may see at least one of the conditions (§5.4 gate).</summary>
private void QueueRefreshRequired(RefreshTargets targets, IReadOnlyList<ImportedCondition> conditions)
/// <summary>
/// Off Lock: one RefreshRequired per item that may see at least one of the conditions (§5.4 gate). For a
/// <paramref name="removal"/> (#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.
/// </summary>
private void QueueRefreshRequired(RefreshTargets targets, IReadOnlyList<ImportedCondition> conditions, bool removal = false)
{
Func<IEventMonitoredItem, ImportedCondition, bool> holds = removal
? (item, c) => item.EffectiveIdentity is { } identity && c.WasAdmitted(identity)
: ImportedVisibleTo;
var send = new List<IEventMonitoredItem>();
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);