fix(dataconn): #1115 a snapshot is not reset by the partition change history rolling past it

The #1026 scale gate (2048 projected items, rig seed --scale 2048) never opened: every
cohort snapshot reset with 'Snapshot catch-up expired' while ~1024 changes of any source
(even the cohort's own first values) landed on its partition. SnapshotLease required the
bounded change history (MaxSnapshotCatchUpItems) to still reach its capture watermark,
but nothing reads that history for a snapshot: every production capture comes with its
interest lease, which already catches up from each own source's last change when the
history rolled past it (#994). Live: with the history bound raised to 262144 the same
cohort opened all 2048 items Good in 4.6 s; at the default it never got past ~120.

Drops the reach check (epoch/control/own-source checks unchanged) and the now-unread
per-item cursor positions. The old pin that asserted the reset now asserts no reset and
that the interest still reports both changed sources.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Joseph Doherty
2026-12-05 03:34:02 -05:00
parent 8ddae2f43d
commit 0dfe8371b7
4 changed files with 108 additions and 28 deletions
@@ -672,12 +672,21 @@ internal sealed class SamplePartition : IDisposable
ReleaseMetadataOwner(state);
if (lastSnapshot && Volatile.Read(ref state.Gate.Retired) != 0 && !Volatile.Read(ref _disposed)) RequestReclaim();
}
internal bool IsSnapshotCurrent(long epoch, long watermark, long control)
/// <summary>
/// Whether a snapshot's partition cursor is still valid: same partition epoch and control stamp. #1115: it no longer
/// also requires the bounded change history (<see cref="DataResourceLimits.MaxSnapshotCatchUpItems"/>) to reach back
/// to the cursor. A snapshot's catch-up never walks that history — <see cref="SnapshotLease.TryReadDelta"/> reads each
/// own source's LATEST sample by version — and the interest lease it hands over to already catches up from each own
/// source's last change when the history rolled past it (#994). Requiring the reach reset every snapshot that lived
/// while ~1024 changes of ANY source landed on its partition, so a 2048-item projected cohort could never finish
/// opening ("Snapshot catch-up expired") on a busy origin, or even from its own first values.
/// </summary>
internal bool IsSnapshotCurrent(long epoch, long control)
{
if (!Control.Matches(control)) return false;
lock (_gate)
{
if (_disposed || epoch != _epoch || (_changes.Count > 0 && watermark < _changes.First!.Value.Version - 1)) return false;
if (_disposed || epoch != _epoch) return false;
return Control.Matches(control);
}
}
@@ -17,26 +17,15 @@ public sealed record SnapshotItem(DataReadiness Readiness, DataItemStatus Status
/// <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,
SourceTicket[] tickets)
// #1115: the cursor no longer tracks per-item history positions — a snapshot is not reset by the partition's bounded
// change history rolling past it (SamplePartition.IsSnapshotCurrent), so the minimum position had no remaining reader.
private sealed class PartitionCursor(SamplePartition partition, long epoch, long control, SourceTicket[] tickets)
{
internal readonly SamplePartition Partition = partition;
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;
internal void Advance(long oldPosition, long newPosition)
{
if (oldPosition == newPosition) return;
var removed = --_positions[oldPosition] == 0;
if (removed) _positions.Remove(oldPosition);
_positions.TryGetValue(newPosition, out var count);
_positions[newPosition] = count + 1;
if (removed && oldPosition == _minimum) _minimum = _positions.First().Key;
}
}
private readonly PartitionedSampleHub _hub;
private readonly SamplePartition[] _partitions;
@@ -45,7 +34,6 @@ public sealed class SnapshotLease : IDisposable
private readonly int _itemCount;
private readonly Dictionary<int, PartitionCursor> _cursors;
private readonly Dictionary<SourceTicket, int> _owners;
private readonly Dictionary<SourceTicket, long> _positions;
private readonly Dictionary<SourceTicket, long> _versions;
private readonly object _gate = new();
private CancellationTokenRegistration _cancellation;
@@ -61,9 +49,7 @@ public sealed class SnapshotLease : IDisposable
Watermarks = new ReadOnlyDictionary<int, long>(watermarks);
_owners = items.Keys.ToDictionary(t => t, t => PartitionedSampleHub.GetPartitionFor(t.Key, partitions.Length));
_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]]);
g => new PartitionCursor(partitions[g.Key], epochs[g.Key], controls[g.Key], g.Select(pair => pair.Key).ToArray()));
_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);
_cancellation = cancellationToken.Register(static o => ((SnapshotLease)o!).Dispose(), this);
@@ -114,7 +100,7 @@ public sealed class SnapshotLease : IDisposable
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);
: cursor.Partition.IsSnapshotCurrent(cursor.Epoch, cursor.Control);
if (current) return true;
if (cursor.Partition.Control.Matches(cursor.Control)) return false;
}
@@ -152,15 +138,13 @@ public sealed class SnapshotLease : IDisposable
lock (_gate)
{
if (IsReset || !_owners.TryGetValue(ticket, out var owner)) return false;
var read = _partitions[owner].Read(ticket, out var readWatermark);
var read = _partitions[owner].Read(ticket, out _);
if (read.Status == DataItemStatus.ResourceExhausted) { read.Dispose(); Release(); return false; }
if (read.Readiness != DataReadiness.Ready || read.Sample is null) { read.Dispose(); Release(); return false; }
if (IsReset) { read.Dispose(); return false; }
var version = read.Sample.Version;
if (version <= _versions[ticket]) { read.Dispose(); return false; }
_versions[ticket] = version;
_cursors[owner].Advance(_positions[ticket], readWatermark);
_positions[ticket] = readWatermark;
delta = read; return true;
}
}