diff --git a/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/DataConnections/SamplePartition.cs b/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/DataConnections/SamplePartition.cs
index c3b84a16e..1d4e1c5a3 100644
--- a/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/DataConnections/SamplePartition.cs
+++ b/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/DataConnections/SamplePartition.cs
@@ -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);
}
}
+ ///
+ /// #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 or
+ /// 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.
+ ///
+ 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;
diff --git a/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/DataConnections/SnapshotLease.cs b/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/DataConnections/SnapshotLease.cs
index 4837c52cd..8e1aeb96c 100644
--- a/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/DataConnections/SnapshotLease.cs
+++ b/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/DataConnections/SnapshotLease.cs
@@ -9,16 +9,22 @@ public sealed record SnapshotItem(DataReadiness Readiness, DataItemStatus Status
{
internal SamplePartition? Owner { get; init; }
internal object? Source { get; init; }
+ /// #1057: the source's quiet version at capture (see ).
+ internal long QuietVersion { get; init; }
internal void ReleaseSource() { if (Owner is not null && Source is not null) Owner.ReleaseSnapshot(Source); }
}
/// Retained immutable snapshots and per-partition catch-up watermarks.
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;
+ /// #1057: advanced (under the lease gate) once a partition control bump is proven to concern only OTHER sources.
+ internal long Control = control;
+ internal readonly SourceTicket[] Tickets = tickets;
private readonly SortedDictionary _positions = new() { [watermark] = count };
private long _minimum = watermark;
internal long Minimum => _minimum;
@@ -54,8 +60,9 @@ public sealed class SnapshotLease : IDisposable
Items = new ReadOnlyDictionary(items);
Watermarks = new ReadOnlyDictionary(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;
}
}
+
+ ///
+ /// #1057: the partition check (or, with , 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.
+ ///
+ 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;
+ }
+
+ ///
+ /// #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.
+ ///
+ 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;
+ }
/// Returns the latest newer sample as an independently disposable, globally charged read.
public bool TryReadDelta(SourceTicket ticket, out CachedSourceRead? delta)
{
diff --git a/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcData1057SnapshotValidityTests.cs b/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcData1057SnapshotValidityTests.cs
new file mode 100644
index 000000000..f5c54854a
--- /dev/null
+++ b/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcData1057SnapshotValidityTests.cs
@@ -0,0 +1,83 @@
+using Shouldly;
+using Xunit;
+using ZB.MOM.WW.OtOpcUa.Runtime.DataConnections;
+
+namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.DataConnections;
+
+///
+/// #1057: #994 (R-9) made an interest lease tell its own sources' invalidation from a sibling's, but the snapshot lease still
+/// compared the partition-wide control stamp, so any sibling invalidation (including plain lock contention) reset an in-flight
+/// snapshot — and with it the stream ("Snapshot catch-up expired; retry later.").
+///
+[Trait("Category", "Unit"), Trait("Feature", "GrpcData"), Trait("Issue", "1057")]
+public sealed class GrpcData1057SnapshotValidityTests
+{
+ [Fact]
+ public void A_sibling_sources_invalidation_does_not_reset_an_in_flight_snapshot_of_the_same_partition()
+ {
+ using var hub = HubTest.Create(partitions: 1, bytes: 1_000_000, ingress: 100, dirty: 100);
+ var mine = HubTest.Ticket("driver", "mine");
+ var sibling = HubTest.Ticket("driver", "sibling");
+ HubTest.BindAndPublish(hub, mine, 1, 1);
+ HubTest.BindAndPublish(hub, sibling, 1, 1);
+ hub.ProcessPending();
+ hub.TryCaptureInterestSnapshot([mine], HubTest.Budget, CancellationToken.None, out var snapshot, out var interest).ShouldBeTrue();
+ using (snapshot!) using (interest!)
+ {
+ hub.Invalidate(sibling, "source serialization contention");
+ snapshot!.IsReset.ShouldBeFalse("a sibling's invalidation is not this snapshot's");
+ snapshot.IsItemCurrent(mine).ShouldBeTrue();
+ // A sibling STATUS change (an outer-control bracket, no quiet bump) is not this snapshot's either.
+ hub.TryPublishStatus(sibling, 2, 0x80000000u, null, null).ShouldBeTrue();
+ hub.ProcessPending();
+ snapshot.IsReset.ShouldBeFalse();
+ snapshot.IsItemCurrent(mine).ShouldBeTrue();
+ // Positive control: this snapshot's OWN source's invalidation still resets it.
+ hub.Invalidate(mine, "contention");
+ snapshot.IsReset.ShouldBeTrue();
+ }
+ }
+
+ [Fact]
+ public void An_own_source_status_change_still_invalidates_its_snapshot_item()
+ {
+ using var hub = HubTest.Create(partitions: 1, bytes: 1_000_000, ingress: 100, dirty: 100);
+ var mine = HubTest.Ticket("driver", "mine");
+ var sibling = HubTest.Ticket("driver", "sibling");
+ HubTest.BindAndPublish(hub, mine, 1, 1);
+ HubTest.BindAndPublish(hub, sibling, 1, 1);
+ hub.ProcessPending();
+ hub.TryCaptureSnapshot([mine, sibling], HubTest.Budget, CancellationToken.None, out var snapshot).ShouldBeTrue();
+ using (snapshot!)
+ {
+ hub.TryPublishStatus(mine, 2, 0x80000000u, null, null).ShouldBeTrue();
+ snapshot!.IsItemCurrent(mine).ShouldBeFalse("its captured value was invalidated by a status change");
+ }
+ }
+
+ [Fact]
+ public void A_partition_wide_invalidation_and_a_retired_binding_still_reset_the_snapshot()
+ {
+ using var hub = HubTest.Create(partitions: 1, bytes: 1_000_000, ingress: 100, dirty: 100);
+ var mine = HubTest.Ticket("driver", "mine");
+ HubTest.BindAndPublish(hub, mine, 1, 1);
+ hub.ProcessPending();
+ hub.TryCaptureSnapshot([mine], HubTest.Budget, CancellationToken.None, out var first).ShouldBeTrue();
+ using (first!)
+ {
+ hub.InvalidatePartitionFor(mine.Key, "partition overflow");
+ first!.IsReset.ShouldBeTrue();
+ }
+ using var other = HubTest.Create(partitions: 1, bytes: 1_000_000, ingress: 100, dirty: 100);
+ var bound = HubTest.Ticket("driver", "bound");
+ HubTest.BindAndPublish(other, bound, 1, 1);
+ other.ProcessPending();
+ other.TryCaptureSnapshot([bound], HubTest.Budget, CancellationToken.None, out var second).ShouldBeTrue();
+ using (second!)
+ {
+ var newer = new SourceTicket(bound.Key, new(1, 2));
+ other.TryBind(newer).ShouldBeTrue(); // retires the captured binding
+ second!.IsReset.ShouldBeTrue();
+ }
+ }
+}