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
@@ -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;