fix(grpc-data): #969 concurrent cadence renewals of one predecessor share one metadata call and one commit

Two Subscribe cadences of one node reach the renewal window on the same tick
and each ran its own overlap renewal of the same predecessor: two origin
metadata calls, and the second commit adopted the first's successor yet
replaced the node's committed definition again (harmless since #968, wasted).

ProjectedDataAccess now keeps in-flight cadence renewals per predecessor proof
(reference identity, same node). A cadence that finds one waits for it; a
committed shared renewal is its success too (it adopts the committed successor
exactly as the existing already-committed shortcut does, its own caller
re-authorized by TryPrepareBinding). A shared renewal that did not commit is no
verdict: the joiner renews for itself, so one cadence stopping or being refused
only delays the other. No sink interface change.

Tests: Concurrent_cadence_renewals_of_one_predecessor_share_one_metadata_call_and_one_commit
(red: 2 metadata calls; green: 1 call, 1 definition replacement, both streams
kept) plus a variant where the shared renewal fails and the joiner renews
itself. The #968 adoption wire test keeps exercising the unshared path via a
test switch.
This commit is contained in:
Joseph Doherty
2026-12-02 15:10:02 -05:00
parent 708a4889ca
commit a571a0fc99
4 changed files with 170 additions and 1 deletions
@@ -23,6 +23,14 @@ public sealed class ProjectedDataAccess : IProjectedDataAccess, IDisposable
// until they are equal so a tick is always evaluated at the fake instant that fired it, never at a later one under load.
private long _renewalTicksQueued, _renewalTicksHandled;
internal bool RenewalTicksQuiescentForTests => Volatile.Read(ref _renewalTicksHandled) == Volatile.Read(ref _renewalTicksQueued);
// #969: cadence (overlap) renewals in flight, per predecessor proof. Two Subscribe cadences of one node reach the renewal
// window on the same tick; the second joins the first's renewal instead of running its own metadata call and replacing
// the node's committed definition a second time. A joiner whose shared renewal did not commit renews for itself, so one
// cadence stopping (or its caller being refused) only delays the other's renewal, never fails it.
private readonly Dictionary<ProjectedNodeProof, (NodeId NodeId, Task<bool> Renewal)> _overlapRenewals = new(System.Collections.Generic.ReferenceEqualityComparer.Instance);
internal bool DeduplicateOverlapRenewalsForTests { get; set; } = true;
private int _overlapRenewalsJoined;
internal int OverlapRenewalsJoinedForTests => Volatile.Read(ref _overlapRenewalsJoined);
/// <summary>#920: runs on the stream pump after a publish passed its demand check, before the duplicate-baseline check.</summary>
internal Action<IProjectedDemandLease>? AfterPublishDemandCheckForTests { get; set; }
private readonly IFederatedProjectionCatalog _catalog;
@@ -415,6 +423,53 @@ public sealed class ProjectedDataAccess : IProjectedDataAccess, IDisposable
DataOperations.HistoryRead => OpcUaOperation.HistoryRead,
_ => throw new ArgumentOutOfRangeException(nameof(operation))
};
/// <summary>
/// #969: runs <paramref name="renew"/> (a cadence's overlap renewal of <paramref name="predecessor"/>) unless another
/// cadence is already renewing the same predecessor of the same node, in which case it waits for that renewal. A
/// shared renewal that committed is this cadence's success too: its successor is then the node's committed proof,
/// which the cadence adopts exactly as when it finds the successor already committed (its own caller is re-authorized
/// by <see cref="Demand.TryPrepareBinding"/>). One that did not commit is no verdict for this cadence, which renews for
/// itself.
/// </summary>
private async Task<bool> RenewOverlapOnceAsync(ProjectedNodeProof predecessor, NodeId nodeId, Func<Task<bool>> renew,
CancellationToken cancellationToken)
{
Task<bool>? shared = null;
TaskCompletionSource<bool>? owned = null;
lock (_sync)
{
if (DeduplicateOverlapRenewalsForTests)
{
if (_overlapRenewals.TryGetValue(predecessor, out var running))
{
if (running.NodeId == nodeId) shared = running.Renewal;
}
else
{
owned = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
_overlapRenewals.Add(predecessor, (nodeId, owned.Task));
}
}
}
if (shared is not null)
{
Interlocked.Increment(ref _overlapRenewalsJoined);
bool sharedRenewed;
// Cancelled while waiting: the same verdict as this cadence's own renewal being cancelled.
try { sharedRenewed = await shared.WaitAsync(cancellationToken).ConfigureAwait(false); }
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) { return false; }
return sharedRenewed || await renew().ConfigureAwait(false);
}
if (owned is null) return await renew().ConfigureAwait(false);
var renewed = false;
try { renewed = await renew().ConfigureAwait(false); return renewed; }
finally
{
lock (_sync) _overlapRenewals.Remove(predecessor);
owned.TrySetResult(renewed);
}
}
private sealed class Demand(ProjectedDataAccess owner, NodeId nodeId, ProjectedVariableDefinition definition,
ProjectedNodeProof proof, DataCallerIdentity? caller, DataOperations operation, IDisposable pin, IProjectedNodePin nodePin, string connectionId, long writeRoleGeneration, ConfiguredProjectionCapability? configured) : IProjectedDemandLease
{
@@ -1529,7 +1584,8 @@ public sealed class ProjectedDataAccess : IProjectedDataAccess, IDisposable
else if (_owner._catalog is RemoteNamespaceResolver resolver && _owner._renewalSink is { } sink)
{
using var linked = CancellationTokenSource.CreateLinkedTokenSource(token, demand.CancellationToken);
success = await RenewOwnedAsync(demand, expected, resolver, sink, linked.Token, overlap: true).ConfigureAwait(false);
success = await _owner.RenewOverlapOnceAsync(expected.Proof, demand.NodeId,
() => RenewOwnedAsync(demand, expected, resolver, sink, linked.Token, overlap: true), linked.Token).ConfigureAwait(false);
}
await _events.Writer.WriteAsync(new RenewedEvent(demand, expected, success), token).ConfigureAwait(false);
}