perf(opcua): #855 — shared per-report projection for condition events under Lock
Condition events delivered to many event items (folder and Server-object) no longer pay the SDK's per-item QueueEvent(IFilterTarget) work under the node-manager Lock: the role-permission verdict is shared per (session, identity), the #826 grant check per identity per hop, and the where clause + select-clause projection per (value-equal EventFilter, locales); each item gets its own EventFieldList over the shared read-only fields. The SDK's QueueEvent body is reproduced under the item's own lock (three private SDK members, guard-tested); anything outside that shape falls back to the SDK. The per-report duplicate check replaces the SDK's O(queue) scan. ProjectedEventDeliveryWireTests compares both paths over the wire. Claude-Session: https://claude.ai/code/session_01GT67AD99zQQqpLCVm3p7xk
This commit is contained in:
@@ -1520,7 +1520,7 @@ public sealed class OtOpcUaNodeManager : CustomNodeManager2, INodeManager3
|
||||
ValueTask delivery;
|
||||
try
|
||||
{
|
||||
delivery = DeliverConditionEventAsync(condition, condition, e);
|
||||
delivery = DeliverReportAsync(condition, e);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -1611,7 +1611,7 @@ public sealed class OtOpcUaNodeManager : CustomNodeManager2, INodeManager3
|
||||
|
||||
try
|
||||
{
|
||||
current = DeliverConditionEventAsync(condition, condition, next).AsTask();
|
||||
current = DeliverReportAsync(condition, next).AsTask();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -1689,6 +1689,58 @@ public sealed class OtOpcUaNodeManager : CustomNodeManager2, INodeManager3
|
||||
await DeliverConditionEventAsync(condition, parent, e).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// One report's whole walk, then its per-report delivery cache is dropped (#855 perf): the cache only serves the
|
||||
/// walk, and the snapshot it hangs on stays referenced from every item queue the event reached.
|
||||
/// </summary>
|
||||
private async ValueTask DeliverReportAsync(AlarmConditionState condition, IFilterTarget e)
|
||||
{
|
||||
try
|
||||
{
|
||||
await DeliverConditionEventAsync(condition, condition, e).ConfigureAwait(false);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (e is ConditionEventSnapshot snapshot) snapshot.ReleaseDeliveryCache();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Queues a condition event to one item: through the report's shared projection (#855 perf) for a condition
|
||||
/// snapshot, else through the SDK's own <c>QueueEvent</c>.
|
||||
/// </summary>
|
||||
private void QueueToItem(IEventMonitoredItem item, IFilterTarget e)
|
||||
{
|
||||
if (e is ConditionEventSnapshot snapshot) snapshot.DeliveryCache.Queue(Server, item, e, UseProjectedEventDelivery);
|
||||
else item.QueueEvent(e);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Whether condition events are queued through the report's shared projection (#855 perf, default) or through the
|
||||
/// SDK's own <c>QueueEvent(IFilterTarget)</c> per item. A test switch: the two must deliver identical events.
|
||||
/// </summary>
|
||||
internal bool UseProjectedEventDelivery { get; set; } = true;
|
||||
|
||||
/// <summary>The SDK's role-permission verdict for one item, shared per (session, identity) within a report (#855 perf).</summary>
|
||||
private bool RoleAllows(IEventMonitoredItem item, IFilterTarget e) =>
|
||||
e is ConditionEventSnapshot snapshot
|
||||
? snapshot.DeliveryCache.RoleAllows(item, i => !ServiceResult.IsBad(ValidateEventRolePermissions(i, e)))
|
||||
: !ServiceResult.IsBad(ValidateEventRolePermissions(item, e));
|
||||
|
||||
/// <summary>
|
||||
/// <see cref="IsConditionVisibleTo"/> per effective identity within one delivery (#855 perf): the decision reads the
|
||||
/// item's identity only, and a delivery's scopes are fixed.
|
||||
/// </summary>
|
||||
private bool IsConditionVisibleTo(
|
||||
IEventMonitoredItem item, List<NodeId> scopes, Dictionary<object, bool> perIdentity)
|
||||
{
|
||||
if (item.EffectiveIdentity is not { } identity) return IsConditionVisibleTo(item, scopes);
|
||||
if (perIdentity.TryGetValue(identity, out var cached)) return cached;
|
||||
var visible = IsConditionVisibleTo(item, scopes);
|
||||
perIdentity[identity] = visible;
|
||||
return visible;
|
||||
}
|
||||
|
||||
/// <summary>Marks an event whose remaining delivery will run off <c>Lock</c> (#855) — before the walk suspends.</summary>
|
||||
private static void MarkDeferred(IFilterTarget e)
|
||||
{
|
||||
@@ -1756,14 +1808,15 @@ public sealed class OtOpcUaNodeManager : CustomNodeManager2, INodeManager3
|
||||
scopes = ConditionVisibilityScopes(live);
|
||||
}
|
||||
|
||||
var visibleByIdentity = scopes is null ? null : new Dictionary<object, bool>(System.Collections.Generic.ReferenceEqualityComparer.Instance);
|
||||
foreach (var item in monitored.EventMonitoredItems.Values)
|
||||
{
|
||||
if (item is null) continue;
|
||||
try
|
||||
{
|
||||
if (IsAuditEvent(e) && !IsAuditDeliverableTo(item)) continue;
|
||||
if (ServiceResult.IsBad(ValidateEventRolePermissions(item, e))) continue;
|
||||
if (scopes is not null && !IsConditionVisibleTo(item, scopes)) continue;
|
||||
if (!RoleAllows(item, e)) continue;
|
||||
if (scopes is not null && !IsConditionVisibleTo(item, scopes, visibleByIdentity!)) continue;
|
||||
QueueConditionEventToItem(item, e, deferred, DeferredFolderDeliveryProbe);
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -1877,6 +1930,21 @@ public sealed class OtOpcUaNodeManager : CustomNodeManager2, INodeManager3
|
||||
get => _deferred;
|
||||
set => _deferred = value;
|
||||
}
|
||||
|
||||
private EventDeliveryCache? _deliveryCache;
|
||||
|
||||
/// <summary>
|
||||
/// The report's shared delivery state (#855 perf) — projections, role verdicts, the items it was queued to. Only
|
||||
/// the report's own walk uses it (sequentially, even when part of the walk runs off <c>Lock</c>); dropped when the
|
||||
/// walk ends.
|
||||
/// </summary>
|
||||
public EventDeliveryCache DeliveryCache =>
|
||||
Volatile.Read(ref _deliveryCache)
|
||||
?? Interlocked.CompareExchange(ref _deliveryCache, new EventDeliveryCache(), null)
|
||||
?? _deliveryCache!;
|
||||
|
||||
/// <summary>Drops the delivery cache once the walk is over (the snapshot stays referenced from the item queues).</summary>
|
||||
public void ReleaseDeliveryCache() => Volatile.Write(ref _deliveryCache, null);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -5841,14 +5909,15 @@ public sealed class OtOpcUaNodeManager : CustomNodeManager2, INodeManager3
|
||||
|
||||
var reportingSession = (context as ISessionSystemContext)?.SessionId;
|
||||
var deferred = e is ConditionEventSnapshot { Deferred: true } d ? d : null;
|
||||
var visibleByIdentity = scopes is null ? null : new Dictionary<object, bool>(System.Collections.Generic.ReferenceEqualityComparer.Instance);
|
||||
|
||||
foreach (var item in _allEventsItems.Keys)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (e is AuditEventState && !IsAuditDeliverableTo(item)) continue;
|
||||
if (scopes is not null && !IsConditionVisibleTo(item, scopes)) continue;
|
||||
if (ServiceResult.IsBad(ValidateEventRolePermissions(item, e))) continue;
|
||||
if (scopes is not null && !IsConditionVisibleTo(item, scopes, visibleByIdentity!)) continue;
|
||||
if (!RoleAllows(item, e)) continue;
|
||||
if (reportingSession is { IsNull: false } reportingId
|
||||
&& item.Session?.Id is { } own
|
||||
&& !own.Equals(reportingId))
|
||||
@@ -5884,7 +5953,7 @@ public sealed class OtOpcUaNodeManager : CustomNodeManager2, INodeManager3
|
||||
if (deferred is not null) state = EventItemStateOf(item);
|
||||
else if (!_eventItemStates.TryGetValue(item, out state) || state.PendingReplay == 0 || e is not ConditionEventSnapshot)
|
||||
{
|
||||
item.QueueEvent(e);
|
||||
QueueToItem(item, e);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -5907,7 +5976,7 @@ public sealed class OtOpcUaNodeManager : CustomNodeManager2, INodeManager3
|
||||
state.NewerSinceCut.Add(ce.ConditionId);
|
||||
|
||||
if (deferred is not null) probe?.Invoke();
|
||||
item.QueueEvent(e);
|
||||
QueueToItem(item, e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user