fix(grpc-data): #1057 a sibling source's invalidation no longer resets an in-flight snapshot on the same partition

#994 (R-9) taught the interest lease to tell its own sources' invalidation from a sibling's, but SnapshotLease still
compared the partition-wide control stamp, so any sibling invalidation, status barrier or plain lock contention reset
the snapshot and with it the stream. Each snapshot cursor now adopts a newer stamp once it proves none of ITS captured
sources moved (quiet version, invalidation version, binding/fence/gate retirement, same source state), read inside a
captured control window; an in-progress bracket is spun on briefly. Own-source invalidation, a partition-wide epoch bump,
a poisoned signal and history roll-off still reset.
This commit is contained in:
Joseph Doherty
2026-12-03 15:13:31 -05:00
parent 258634c12b
commit 7436e1af95
3 changed files with 152 additions and 7 deletions
@@ -575,7 +575,8 @@ internal sealed class SamplePartition : IDisposable
items.Add(ticket, new SnapshotItem(exists ? state.Readiness : DataReadiness.Unavailable,
exists ? (ready ? DataItemStatus.Good : DataItemStatus.Unavailable) : DataItemStatus.NotFound, sample,
exists ? state.InvalidationVersion : -1, ready ? state.Received : null)
{ Owner = exists ? this : null, Source = exists ? state : null });
{ Owner = exists ? this : null, Source = exists ? state : null,
QuietVersion = exists ? Interlocked.Read(ref state.QuietVersion) : -1 });
}
return true;
}
@@ -668,6 +669,16 @@ internal sealed class SamplePartition : IDisposable
return Control.Matches(control);
}
}
/// <summary>
/// #1057: lock-free — is a snapshot's captured source still the one it captured, with no invalidation, status barrier,
/// removal or retirement since? Every one of those bumps <see cref="SourceState.QuietVersion"/> or
/// <see cref="SourceState.InvalidationVersion"/> inside a control bracket, or retires the binding/fence/gate. The caller
/// reads it inside a captured control window, so a racing bracket shows up as a moved stamp and is decided again.
/// </summary>
internal bool IsSnapshotSourceUnchanged(SourceTicket ticket, SnapshotItem item) =>
item.Source is SourceState state && _sources.TryGetValue(ticket.Key, out var current) && ReferenceEquals(current, state) &&
Interlocked.Read(ref state.QuietVersion) == item.QuietVersion &&
Interlocked.Read(ref state.InvalidationVersion) == item.InvalidationVersion && IsTicketCurrentNonBlocking(ticket);
internal bool IsSnapshotItemCurrent(SourceTicket ticket, SnapshotItem item, long control)
{
if (!Control.Matches(control)) return false;
@@ -9,16 +9,22 @@ public sealed record SnapshotItem(DataReadiness Readiness, DataItemStatus Status
{
internal SamplePartition? Owner { get; init; }
internal object? Source { get; init; }
/// <summary>#1057: the source's quiet version at capture (see <see cref="SamplePartition.IsSnapshotSourceUnchanged"/>).</summary>
internal long QuietVersion { get; init; }
internal void ReleaseSource() { if (Owner is not null && Source is not null) Owner.ReleaseSnapshot(Source); }
}
/// <summary>Retained immutable snapshots and per-partition catch-up watermarks.</summary>
public sealed class SnapshotLease : IDisposable
{
private sealed class PartitionCursor(SamplePartition partition, long epoch, long control, long watermark, int count)
private sealed class PartitionCursor(SamplePartition partition, long epoch, long control, long watermark, int count,
SourceTicket[] tickets)
{
internal readonly SamplePartition Partition = partition;
internal readonly long Epoch = epoch, Control = control;
internal readonly long Epoch = epoch;
/// <summary>#1057: advanced (under the lease gate) once a partition control bump is proven to concern only OTHER sources.</summary>
internal long Control = control;
internal readonly SourceTicket[] Tickets = tickets;
private readonly SortedDictionary<long, int> _positions = new() { [watermark] = count };
private long _minimum = watermark;
internal long Minimum => _minimum;
@@ -54,8 +60,9 @@ public sealed class SnapshotLease : IDisposable
Items = new ReadOnlyDictionary<SourceTicket, SnapshotItem>(items);
Watermarks = new ReadOnlyDictionary<int, long>(watermarks);
_owners = items.Keys.ToDictionary(t => t, t => PartitionedSampleHub.GetPartitionFor(t.Key, partitions.Length));
_cursors = _owners.Values.GroupBy(i => i).ToDictionary(g => g.Key,
g => new PartitionCursor(partitions[g.Key], epochs[g.Key], controls[g.Key], watermarks[g.Key], g.Count()));
_cursors = _owners.GroupBy(pair => pair.Value).ToDictionary(g => g.Key,
g => new PartitionCursor(partitions[g.Key], epochs[g.Key], controls[g.Key], watermarks[g.Key], g.Count(),
g.Select(pair => pair.Key).ToArray()));
_positions = items.Keys.ToDictionary(t => t, t => watermarks[_owners[t]]);
_versions = items.ToDictionary(p => p.Key, p => p.Value.Sample?.Version ?? 0);
_timer = hub.Clock.CreateTimer(static o => ((SnapshotLease)o!).Dispose(), this, lifetime, Timeout.InfiniteTimeSpan);
@@ -75,7 +82,7 @@ public sealed class SnapshotLease : IDisposable
{
_hub.BeforeSnapshotValidationForTests?.Invoke();
_hub.CountSnapshotValidationForTests();
if (!cursor.Partition.IsSnapshotCurrent(cursor.Epoch, cursor.Minimum, cursor.Control)) { Release(); return true; }
if (!IsCursorCurrent(cursor, null, null)) { Release(); return true; }
}
if (_hub.IsDisposed || _hub.RoutingEpoch != _routingEpoch) { Release(); return true; }
return false;
@@ -89,11 +96,55 @@ public sealed class SnapshotLease : IDisposable
{
if (IsReset || !_owners.TryGetValue(ticket, out var owner) || !Items.TryGetValue(ticket, out var item)) return false;
var cursor = _cursors[owner];
if (!cursor.Partition.IsSnapshotItemCurrent(ticket, item, cursor.Control) ||
if (!IsCursorCurrent(cursor, ticket, item) ||
_hub.IsDisposed || _hub.RoutingEpoch != _routingEpoch) { Release(); return false; }
return true;
}
}
/// <summary>
/// #1057: the partition check (or, with <paramref name="ticket"/>, the item check) against this cursor's control stamp,
/// adopting a newer stamp first when the bump concerned only OTHER sources of the partition. A check refused while the
/// stamp still matches is refused on its own evidence; one refused because a racing bump moved the stamp is decided again.
/// </summary>
private bool IsCursorCurrent(PartitionCursor cursor, SourceTicket? ticket, SnapshotItem? item)
{
for (var attempt = 0; attempt < 4; attempt++)
{
if (!TryAdoptControl(cursor)) return false;
var current = ticket is { } own
? cursor.Partition.IsSnapshotItemCurrent(own, item!, cursor.Control)
: cursor.Partition.IsSnapshotCurrent(cursor.Epoch, cursor.Minimum, cursor.Control);
if (current) return true;
if (cursor.Partition.Control.Matches(cursor.Control)) return false;
}
return false;
}
/// <summary>
/// #1057: a partition's control stamp moves for ANY source's invalidation, status barrier, removal or retirement —
/// including a sibling's plain lock contention. It resets this snapshot only when one of its OWN captured sources changed
/// (as #994 R-9 did for interest leases); otherwise the cursor adopts the new stamp. Partition-wide invalidation still
/// resets through the partition epoch, and a poisoned signal never captures, so it still resets too. An in-progress
/// bracket is spun on briefly (brackets are a few interlocked operations) and is a reset only if it does not end.
/// </summary>
private bool TryAdoptControl(PartitionCursor cursor)
{
var signal = cursor.Partition.Control;
if (signal.Matches(cursor.Control)) return true;
var spin = new SpinWait();
for (var attempt = 0; attempt < 64; attempt++)
{
if (signal.TryCapture(out var now))
{
foreach (var ticket in cursor.Tickets)
if (!cursor.Partition.IsSnapshotSourceUnchanged(ticket, Items[ticket])) return false;
if (signal.Matches(now)) { cursor.Control = now; return true; }
}
spin.SpinOnce(sleep1Threshold: -1);
}
return false;
}
/// <summary>Returns the latest newer sample as an independently disposable, globally charged read.</summary>
public bool TryReadDelta(SourceTicket ticket, out CachedSourceRead? delta)
{