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:
@@ -472,6 +472,27 @@ internal sealed class ProjectedMonitoredItem : MonitoredItem
|
||||
private LossControl? _queuedLoss;
|
||||
internal Action? AfterLossDequeueForTests;
|
||||
private long _candidateBytes;
|
||||
// #948: the acquisition whose candidate an SDK notification last consumed, while that is still the value the SDK item last
|
||||
// queued (no other candidate, loss or queue reset since). Kept without the sample (no value retained): a false match only
|
||||
// costs an unproven notification + a baseline request, never a borrowed proof.
|
||||
private ConsumedAcquisition? _lastConsumed;
|
||||
private sealed record ConsumedAcquisition(SourceBinding Binding, object SourceIdentity, TimeProvider Clock, long SourceVersion,
|
||||
DataRealm Realm, string LocalIdentifier, long ProjectionGeneration, ServingIdentity Serving, uint StatusCode,
|
||||
DateTime? SourceTimestampUtc, DateTime? ServerTimestampUtc)
|
||||
{
|
||||
internal static ConsumedAcquisition? Of(SourceBinding binding, ProjectedValueWrite sample) =>
|
||||
sample.SourceVersion is > 0 and var version
|
||||
? new(binding, sample.Proof.SourceIdentity, sample.Proof.Clock, version, sample.Realm, sample.LocalIdentifier,
|
||||
sample.ProjectionGeneration, sample.Serving, sample.Snapshot.StatusCode, sample.Snapshot.SourceTimestampUtc,
|
||||
sample.Snapshot.ServerTimestampUtc)
|
||||
: null;
|
||||
internal bool Matches(SourceBinding? binding, ProjectedValueWrite sample) =>
|
||||
ReferenceEquals(Binding, binding) && sample.SourceVersion == SourceVersion &&
|
||||
ReferenceEquals(sample.Proof.SourceIdentity, SourceIdentity) && ReferenceEquals(sample.Proof.Clock, Clock) &&
|
||||
sample.Realm == Realm && sample.LocalIdentifier == LocalIdentifier && sample.ProjectionGeneration == ProjectionGeneration &&
|
||||
sample.Serving == Serving && sample.Snapshot.StatusCode == StatusCode &&
|
||||
sample.Snapshot.SourceTimestampUtc == SourceTimestampUtc && sample.Snapshot.ServerTimestampUtc == ServerTimestampUtc;
|
||||
}
|
||||
private bool _needsSnapshot;
|
||||
private int _snapshotRequested;
|
||||
private SourceBinding? _binding;
|
||||
@@ -527,6 +548,17 @@ internal sealed class ProjectedMonitoredItem : MonitoredItem
|
||||
if (!FreshForDemand(sample)) return true;
|
||||
_needsSnapshot = true;
|
||||
}
|
||||
else if (_candidates.Count == 0 && _queuedLoss is null && _lastConsumed is { } consumed && consumed.Matches(original, sample))
|
||||
{
|
||||
// #948: this item already DELIVERED this acquisition and nothing has been queued since, so the SDK item's last
|
||||
// value is this one: re-applying it raises no notification, and a candidate for it would never be consumed
|
||||
// (it would hold its shared budget charge until the binding changed). Renew health evidence only.
|
||||
if (!FreshForDemand(sample)) return true;
|
||||
var successor = new SampleEvidence(original!, sample.Proof, sample.OriginAgeBounds, sample.ReceivedTimestamp);
|
||||
_latestEvidence?.Clear();
|
||||
_latestEvidence = successor; _reportedLoss = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (!sameSource || (_needsSnapshot && sample.IsCompleteSnapshot))
|
||||
{
|
||||
@@ -606,7 +638,7 @@ internal sealed class ProjectedMonitoredItem : MonitoredItem
|
||||
{
|
||||
if (MonitoringMode == MonitoringMode.Disabled || _queuedLoss is not null || ReferenceEquals(_reportedLoss, loss.Evidence) || _retired != 0 || !ReferenceEquals(_latestEvidence, loss.Evidence) ||
|
||||
!ReferenceEquals(_binding, loss.Evidence.Binding)) return;
|
||||
_queuedLoss = loss;
|
||||
_queuedLoss = loss; _lastConsumed = null;
|
||||
try { QueueValue(new DataValue(Variant.Null, StatusCodes.BadNoCommunication, DateTimeUtc.MinValue, DateTimeUtc.MinValue),
|
||||
new ServiceResult(StatusCodes.BadNoCommunication), ignoreFilters: true); }
|
||||
catch (Exception) { _queuedLoss = null; }
|
||||
@@ -670,7 +702,7 @@ internal sealed class ProjectedMonitoredItem : MonitoredItem
|
||||
private void ClearCandidates()
|
||||
{
|
||||
foreach (var candidate in _candidates) candidate.Charge.Dispose();
|
||||
_candidates.Clear(); _candidateBytes = 0;
|
||||
_candidates.Clear(); _candidateBytes = 0; _lastConsumed = null;
|
||||
}
|
||||
internal void RequestBaseline()
|
||||
{
|
||||
@@ -704,7 +736,8 @@ internal sealed class ProjectedMonitoredItem : MonitoredItem
|
||||
_definition is { } definition && _owner.IsProjectedDefinitionCurrent(_nodeId, definition);
|
||||
internal void ResetHealthMonitoringMode()
|
||||
{
|
||||
lock (_bindingSync) { _queuedLoss = null; _reportedLoss = null; }
|
||||
// A client mode change can reset the SDK item's last value: a later re-delivery may notify again, so it needs a candidate.
|
||||
lock (_bindingSync) { _queuedLoss = null; _reportedLoss = null; _lastConsumed = null; }
|
||||
}
|
||||
internal void Retire()
|
||||
{
|
||||
@@ -785,10 +818,11 @@ internal sealed class ProjectedMonitoredItem : MonitoredItem
|
||||
{
|
||||
ProjectedDeliveryDiagnostics.Count(ProjectedDeliveryDiagnostics.Event.CandidateUnique);
|
||||
var matched = matching[0]; _candidates.Remove(matched); _candidateBytes -= matched.Bytes;
|
||||
_lastConsumed = ConsumedAcquisition.Of(matched.Binding, matched.Sample);
|
||||
evidence = matched.Binding.Capture(matched.Sample.Proof, matched.Sample.OriginAgeBounds, matched.Sample.ReceivedTimestamp);
|
||||
matched.Charge.Dispose();
|
||||
}
|
||||
else { ProjectedDeliveryDiagnostics.Count(matching.Length == 0 ? ProjectedDeliveryDiagnostics.Event.CandidateMissing : ProjectedDeliveryDiagnostics.Event.CandidateAmbiguous); _needsSnapshot = true; }
|
||||
else { ProjectedDeliveryDiagnostics.Count(matching.Length == 0 ? ProjectedDeliveryDiagnostics.Event.CandidateMissing : ProjectedDeliveryDiagnostics.Event.CandidateAmbiguous); _needsSnapshot = true; _lastConsumed = null; }
|
||||
}
|
||||
if (evidence is null) RequestBaseline();
|
||||
var originalSample = evidence;
|
||||
|
||||
+52
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user