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:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
+81
@@ -0,0 +1,81 @@
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
using ZB.MOM.WW.OtOpcUa.Runtime.DataConnections;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.DataConnections;
|
||||
|
||||
/// <summary>
|
||||
/// #1026/#1115: an in-flight snapshot must not be reset because the partition's bounded change history
|
||||
/// (<see cref="DataResourceLimits.MaxSnapshotCatchUpItems"/>) rolled past its capture watermark. The live scale gate
|
||||
/// (2048 projected items) showed every cohort snapshot reset ("Snapshot catch-up expired") while ordinary traffic of
|
||||
/// OTHER sources — or the cohort's own first values — filled the 1024-entry history. #994 already made interest leases
|
||||
/// catch up from each own source's last change in that case; the snapshot kept the old reset.
|
||||
/// </summary>
|
||||
[Trait("Feature", "GrpcData")]
|
||||
[Trait("Category", "Unit")]
|
||||
public sealed class GrpcData1115SnapshotHistoryTests
|
||||
{
|
||||
[Fact]
|
||||
public void Unrelated_changes_rolling_the_history_do_not_reset_a_snapshot()
|
||||
{
|
||||
using var hub = HubTest.Create(partitions: 1); // MaxSnapshotCatchUpItems = 1
|
||||
var captured = HubTest.Ticket("driver", "captured");
|
||||
var busy = HubTest.Ticket("driver", "busy");
|
||||
HubTest.BindAndPublish(hub, captured, 1, 1);
|
||||
HubTest.BindAndPublish(hub, busy, 1, 1);
|
||||
hub.ProcessPending();
|
||||
hub.TryCaptureSnapshot([captured], HubTest.Budget, CancellationToken.None, out var snapshot).ShouldBeTrue();
|
||||
using (snapshot!)
|
||||
{
|
||||
for (var version = 2; version <= 6; version++)
|
||||
{
|
||||
HubTest.Publish(hub, busy, version, version);
|
||||
hub.ProcessPending();
|
||||
}
|
||||
snapshot!.IsReset.ShouldBeFalse("a sibling's traffic rolling the bounded history is not this snapshot's loss");
|
||||
snapshot.IsItemCurrent(captured).ShouldBeTrue();
|
||||
snapshot.TryReadDelta(captured, out var none).ShouldBeFalse(); // nothing newer of its own
|
||||
none.ShouldBeNull();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Own_changes_past_the_history_are_caught_up_as_the_latest_delta()
|
||||
{
|
||||
using var hub = HubTest.Create(partitions: 1);
|
||||
var captured = HubTest.Ticket("driver", "own");
|
||||
HubTest.BindAndPublish(hub, captured, 1, 1);
|
||||
hub.ProcessPending();
|
||||
hub.TryCaptureSnapshot([captured], HubTest.Budget, CancellationToken.None, out var snapshot).ShouldBeTrue();
|
||||
using (snapshot!)
|
||||
{
|
||||
for (var version = 2; version <= 6; version++)
|
||||
{
|
||||
HubTest.Publish(hub, captured, version, version * 10);
|
||||
hub.ProcessPending();
|
||||
}
|
||||
snapshot!.IsReset.ShouldBeFalse();
|
||||
snapshot.TryReadDelta(captured, out var delta).ShouldBeTrue();
|
||||
using (delta!) delta!.Sample!.Value.GetScalar().ShouldBe(60);
|
||||
snapshot.IsReset.ShouldBeFalse();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_status_barrier_on_a_captured_source_still_resets_after_the_history_rolled()
|
||||
{
|
||||
using var hub = HubTest.Create(partitions: 1);
|
||||
var captured = HubTest.Ticket("driver", "barrier");
|
||||
var busy = HubTest.Ticket("driver", "busy-barrier");
|
||||
HubTest.BindAndPublish(hub, captured, 1, 1);
|
||||
HubTest.BindAndPublish(hub, busy, 1, 1);
|
||||
hub.ProcessPending();
|
||||
hub.TryCaptureSnapshot([captured], HubTest.Budget, CancellationToken.None, out var snapshot).ShouldBeTrue();
|
||||
using (snapshot!)
|
||||
{
|
||||
for (var version = 2; version <= 4; version++) { HubTest.Publish(hub, busy, version, version); hub.ProcessPending(); }
|
||||
hub.TryPublishStatus(captured, 2, 0x80000000, null, null).ShouldBeTrue();
|
||||
snapshot!.IsReset.ShouldBeTrue("an own source's status barrier is still a reset");
|
||||
}
|
||||
}
|
||||
}
|
||||
+10
-4
@@ -563,21 +563,27 @@ public sealed class GrpcDataSnapshotTests
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Catch_up_overflow_resets_snapshot_instead_of_silently_losing_deltas()
|
||||
public void Catch_up_overflow_keeps_the_snapshot_and_its_interest_still_reports_every_delta()
|
||||
{
|
||||
using var hub = HubTest.Create(partitions: 1);
|
||||
// #1115: this used to assert a reset ("instead of silently losing deltas"). Every production snapshot is captured
|
||||
// with its interest lease, and that lease catches up from each own source's last change when the bounded history
|
||||
// rolled past it (#994) — so nothing is lost, and the reset only made large cohorts unopenable on a busy origin.
|
||||
using var hub = HubTest.Create(partitions: 1); // MaxSnapshotCatchUpItems = 1
|
||||
var first = HubTest.Ticket("driver", "first");
|
||||
var second = HubTest.Ticket("driver", "second");
|
||||
HubTest.BindAndPublish(hub, first, 1, 1);
|
||||
HubTest.BindAndPublish(hub, second, 1, 1);
|
||||
hub.ProcessPending();
|
||||
hub.TryCaptureSnapshot([first, second], HubTest.Budget, CancellationToken.None, out var snapshot).ShouldBeTrue();
|
||||
hub.TryCaptureInterestSnapshot([first, second], HubTest.Budget, CancellationToken.None, out var snapshot, out var interest).ShouldBeTrue();
|
||||
using (interest!)
|
||||
using (snapshot!)
|
||||
{
|
||||
HubTest.Publish(hub, first, 2, 2);
|
||||
HubTest.Publish(hub, second, 2, 2);
|
||||
hub.ProcessPending();
|
||||
snapshot!.IsReset.ShouldBeTrue();
|
||||
snapshot!.IsReset.ShouldBeFalse();
|
||||
interest!.TryReadChanges(16, out var changes).ShouldBeTrue();
|
||||
changes.Select(change => change.Ticket).ShouldBe([first, second], ignoreOrder: true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user