fix(grpc-data): a re-delivered, already-consumed acquisition leaves no orphan proof candidate (#948)

ProjectedMonitoredItem remembers the acquisition its last SDK notification consumed (key only,
no value retained). A complete snapshot of that same acquisition, arriving while nothing else is
queued (no candidate, no queued loss, no reset since), renews health evidence but adds no
candidate: the node's value does not change, so the SDK never raises a notification to consume it,
and the candidate held its shared budget charge until the binding changed. Cleared on any queue
reset (rebind, loss, client mode change, unmatched notification), so a fail-safe miss only costs
an unproven notification + baseline request.

Red/green: A_redelivered_already_consumed_acquisition_leaves_no_orphan_candidate (red: 1 orphan).
This commit is contained in:
Joseph Doherty
2026-12-01 19:27:09 -05:00
parent 6b60845931
commit 4fcabdf24a
2 changed files with 90 additions and 4 deletions
@@ -180,6 +180,58 @@ public sealed partial class GrpcDataConfiguredDiscoveryWireTests
}, onPublished: update => published.Writer.TryWrite(update));
}
/// <summary>
/// #948: an acquisition an item already DELIVERED (its proof candidate consumed by an SDK notification) that reaches the
/// item again as a complete snapshot — a recovery re-publish, a duplicate baseline, a stricter demand's snapshot — must
/// not leave a new candidate behind: the node's value does not change, so no SDK notification will ever consume it, and
/// it would hold its shared budget charge until the item's binding changed. A NEW acquisition still gets one (control).
/// </summary>
[Fact(Timeout = 90000), Trait("Issue", "948")]
public async Task A_redelivered_already_consumed_acquisition_leaves_no_orphan_candidate()
{
var ct = TestContext.Current.CancellationToken;
LocalDataFixture? peer = null;
var subscriptions = new FakeSubscriptions(() => peer!.Serving);
using var origin = new LocalDataFixture(servingGeneration: 1, targetSubscriptions: (_, _) => subscriptions);
peer = origin; subscriptions.SampleClock = origin.Clock;
await WithComposedServer(origin, origin.Raw, async (session, sink, _, _, _, _, anchor) =>
{
var client = (SessionClient)session;
await BrowseAll(client, anchor, 1);
var sub = (await client.CreateSubscriptionAsync(new RequestHeader(), 100, 300, 1, 0, true, 0, ct)).SubscriptionId;
await AddComposedItem(client, sub, anchor, 1, 100, ct);
await subscriptions.ItemsAdded.Reader.ReadAsync(ct).AsTask().WaitAsync(TimeSpan.FromSeconds(10), ct);
(await PublishComposedValue(client, 1, ct)).StatusCode.ShouldBe(StatusCodes.Good);
var flags = System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic;
var manager = sink.GetType().GetField("_nodeManager", flags)!.GetValue(sink)!;
var samples = (System.Collections.IDictionary)manager.GetType().GetField("_projectedSamples", flags)!.GetValue(manager)!;
var live = manager.GetType().GetField("_liveItems", flags)!.GetValue(manager)!;
var keys = (System.Collections.IEnumerable)live.GetType().GetProperty("Keys")!.GetValue(live)!;
var existing = keys.Cast<object>().OfType<Opc.Ua.Server.MonitoredItem>().Single(x => x.ClientHandle == 1);
int Candidates() => ((System.Collections.IEnumerable)existing.GetType().GetField("_candidates", flags)!.GetValue(existing)!).Cast<object>().Count();
var budget = manager.GetType().GetField("_projectedCandidateBudget", flags)!.GetValue(manager)!;
var entries = budget.GetType().GetProperty("RetainedEntries").ShouldNotBeNull();
Candidates().ShouldBe(0, "precondition: the delivered baseline consumed its candidate");
var before = (int)entries.GetValue(budget)!;
var delivered = (ProjectedValueWrite)samples[anchor].ShouldNotBeNull();
delivered.IsCompleteSnapshot.ShouldBeTrue("precondition: the baseline is a complete snapshot");
// The same acquisition again (a distinct object, as every publication detaches one).
((IProjectedValueSink)manager).TryPublish([delivered with { }]).ShouldBeTrue();
Candidates().ShouldBe(0, "an already-delivered acquisition cannot produce another SDK notification");
((int)entries.GetValue(budget)!).ShouldBe(before, "no orphan candidate charge is held");
// Positive control: a NEW acquisition is still admitted, delivered and consumed.
((IProjectedValueSink)manager).TryPublish([delivered with { SourceVersion = delivered.SourceVersion + 1,
Snapshot = delivered.Snapshot with { Value = 8 } }]).ShouldBeTrue();
Candidates().ShouldBe(1, "a new acquisition holds a candidate until delivered");
var next = await PublishComposedValue(client, 1, ct);
next.StatusCode.ShouldBe(StatusCodes.Good);
next.WrappedValue.AsBoxedObject(Variant.BoxingBehavior.Legacy).ShouldBe(8);
Candidates().ShouldBe(0);
});
}
private sealed class ThrowingSourceProof(IProjectedSamplePublicationProof inner) : IProjectedSamplePublicationProof
{
public bool IsValid => inner.IsValid;