fix(grpc-data): #922 review r1 — own renewal pool + queue, bounded HistoryRead wait, two-authority denial, per-op keys

This commit is contained in:
Joseph Doherty
2026-11-28 10:54:10 -05:00
parent b569d5c5dd
commit 7e924deda9
6 changed files with 351 additions and 24 deletions
@@ -22,6 +22,10 @@ public sealed class RemoteNamespaceResolver : IFederatedProjectionCatalog, IDisp
private readonly DataOperationPolicy _policy;
private readonly int _maxMappings;
private readonly SemaphoreSlim _fetchSlots;
// #922 review: on-demand renewals of lapsed nodes (a Read / HistoryRead) run on their OWN small pool, never on _fetchSlots,
// so a burst of them cannot crowd out browse pages or the subscription renewals that keep live items from lapsing.
private readonly SemaphoreSlim _onDemandSlots;
internal int OnDemandRenewalSlots { get; }
private readonly int _maximumWaiters;
private readonly Func<CancellationToken, Task>? _beforeFetch;
private int _waiterCount;
@@ -44,6 +48,8 @@ public sealed class RemoteNamespaceResolver : IFederatedProjectionCatalog, IDisp
_source = source ?? throw new ArgumentNullException(nameof(source)); _policy = policy ?? throw new ArgumentNullException(nameof(policy));
if (maxMappings <= 0 || maxConcurrentFetches <= 0) throw new ArgumentOutOfRangeException(nameof(maxMappings));
_maxMappings = maxMappings; _fetchSlots = new(maxConcurrentFetches, maxConcurrentFetches);
OnDemandRenewalSlots = Math.Max(1, maxConcurrentFetches / 2);
_onDemandSlots = new(OnDemandRenewalSlots, OnDemandRenewalSlots);
_maximumWaiters = checked(maxConcurrentFetches * 16);
_cache.Invalidated += OnInvalidated;
}
@@ -304,7 +310,8 @@ public sealed class RemoteNamespaceResolver : IFederatedProjectionCatalog, IDisp
internal async Task<bool> RenewExactAsync(ProjectedNodeProof previous, ProjectedVariableDefinition definition,
NodeId nodeId, DataCallerIdentity? caller, ConfiguredProjectionCapability? configured,
IProjectedNamespacePageSink sink, DataBudget budget, Func<bool> demandActive, CancellationToken cancellationToken, bool overlap = false,
OpcUaOperation authorizeAs = OpcUaOperation.CreateMonitoredItems, DataOperations required = DataOperations.Subscribe)
OpcUaOperation authorizeAs = OpcUaOperation.CreateMonitoredItems, DataOperations required = DataOperations.Subscribe,
bool onDemand = false)
{
// #922: a subscription renews for CreateMonitoredItems/Subscribe (the default); an on-demand renewal of a lapsed node
// renews for the operation the caller is performing (Read / HistoryRead) and requires exactly that bit at the origin.
@@ -329,7 +336,17 @@ public sealed class RemoteNamespaceResolver : IFederatedProjectionCatalog, IDisp
previous.Subject.Scope.Segments.Count < binding.LocalScope.Segments.Count ||
!previous.Subject.Scope.Segments.Take(binding.LocalScope.Segments.Count).SequenceEqual(binding.LocalScope.Segments, StringComparer.Ordinal) ||
!previous.Subject.LocalAncestry.SequenceEqual(binding.LocalAncestry, StringComparer.OrdinalIgnoreCase)) return false;
if (!await _fetchSlots.WaitAsync(0, cancellationToken).ConfigureAwait(false)) return false;
// A subscription renewal never queues (it retries on its own cadence); an on-demand renewal waits for its own pool within
// its budget, and the wait is charged to that budget.
var slots = onDemand ? _onDemandSlots : _fetchSlots;
var queued = System.Diagnostics.Stopwatch.GetTimestamp();
if (!await slots.WaitAsync(onDemand ? budget.Timeout : TimeSpan.Zero, cancellationToken).ConfigureAwait(false)) return false;
if (onDemand)
{
var left = budget.Timeout - System.Diagnostics.Stopwatch.GetElapsedTime(queued);
if (left <= TimeSpan.Zero) { slots.Release(); return false; }
budget = budget with { Timeout = left };
}
IGrpcDataAdapterConnectionLease? connection = null;
try
{
@@ -407,7 +424,7 @@ public sealed class RemoteNamespaceResolver : IFederatedProjectionCatalog, IDisp
{
try { if (connection is not null) await connection.DisposeAsync().ConfigureAwait(false); }
catch (Exception) { }
finally { _fetchSlots.Release(); }
finally { slots.Release(); }
}
}
public void Dispose()