Files
lmxopcua/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcDataInterestScalingTests.cs
T
Joseph Doherty 5cccfd75ce fix(grpc-data): #994 projected cohorts batch, pace and scale past one item; busy origins no longer reset quiet streams
Wave 3 of the 2026-12-02 review (docs/reviews/2026-12-02/01-runtime.md R-2/3/7/8/9/10/12/13/21/22/23/24,
02-opcuaserver.md U-10/11/12/13).

Consumer (ProjectedDataAccess cohort):
- R-2: a cohort's OpenSubscription declared a snapshot budget of 1 item, which the origin uses to size the stream's
  handle table, so the 2nd item was refused LimitExceeded and the cohort re-opened in a loop (two projected items of
  one interval broke each other). Opens/adds/removes are now batched (IDataSubscriptionCommandLimits, <=256 items,
  bounded by estimated snapshot bytes) and paced (32 burst, 32/s, <=4 commands or snapshots outstanding).
- R-7: control events no longer share the bounded frame queue; a failed Remove can no longer Stop() the cohort.
- R-10: a local capacity refusal (TryPublishOrBackpressure -> CapacityExhausted) is retried for up to 2 s instead of
  invalidating healthy evidence and retiring the route.
- R-13: RemoteSubscription mutates its handle/version tables in place; the cohort caches the source per binding and
  drops the per-sample LINQ in EvidenceFor.
- R-8: only an unselectable endpoint arms the discovery backoff; a caller cancel is OperationCanceledException.
- R-24: the condition stream cancels and observes a pending frame read before disposing the enumerator.

Origin (DataSubscriptionSession / SamplePartition / SourceInterestLease):
- R-2 (found by the perf harness): the delivery index was sized MaxPendingFrames x 16 = 1024, so the 1025th item of a
  stream stopped the session after its ack; it now covers MaxMonitoredItems.
- R-3: interest reads count only own-ticket matches (scan budget separate), re-queue a lagging tick, and catch up from
  per-source last-change versions when the bounded history rolled off - no more "Source change history expired".
- R-9: a partition quiet bump resets a lease only when one of ITS sources was invalidated (per-source quiet version)
  or its binding retired; partition-wide invalidation still resets all.
- R-12: the per-item publish verdict is memoised per (catalog instance, authority generation) for <=1 s; Commit
  under the session lock re-checks identity only.
- R-21/R-22/R-23: registry prune is interval/capacity driven; a cleared history session refuses chunks; the snapshot
  barrier is decided and armed under the same gate its release is decided under.

Node manager: U-10 projected live items indexed by node; U-11 anchor removal / root-leaf swap tombstone the anchor's
own monitored node; U-12 per-parent child count; U-13 per-condition verdict index.

Also: Issue934_r1_N1 test clears its metadata failure before ending the stream (test-only sequencing race,
reproduced with a 200 ms delay; production behaviour correct).

Perf (Category=Perf, OTOPCUA_GRPCDATA_PERF=1): 10k-item cohort opens in 3.3 s / 80 commands / 1 stream (before: never
opens); 100 source versions at 1k/s -> 11,036 item values in 569 ms.
2026-12-03 11:59:26 -05:00

99 lines
5.2 KiB
C#

using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Runtime.DataConnections;
namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.DataConnections;
/// <summary>#994: interest leases under unrelated partition traffic (review 2026-12-02 R-3, R-9).</summary>
[Trait("Feature", "GrpcData")]
[Trait("Category", "Unit")]
public sealed class GrpcDataInterestScalingTests
{
// HubTest keeps ONE history entry per partition, so any two unrelated changes between reads roll the cursor off it —
// the shape a busy node produces with the production 1024-entry history and thousands of changes per second.
[Fact, Trait("Issue", "994")]
public void R3_unrelated_traffic_that_rolls_the_history_off_does_not_reset_the_lease_and_own_changes_still_arrive()
{
using var hub = HubTest.Create(partitions: 1, bytes: 1_000_000, ingress: 100, dirty: 100);
var mine = HubTest.Ticket("driver", "mine");
var other = HubTest.Ticket("driver", "busy");
HubTest.BindAndPublish(hub, mine, 1, 1);
HubTest.BindAndPublish(hub, other, 1, 1);
hub.ProcessPending();
hub.TryCaptureInterestSnapshot([mine], HubTest.Budget, CancellationToken.None, out var snapshot, out var interest).ShouldBeTrue();
using (interest!)
{
snapshot!.Dispose();
for (var version = 2; version <= 5000; version++) { HubTest.Publish(hub, other, version, version); hub.ProcessPending(); }
interest!.TryReadChanges(32, out var changes).ShouldBeTrue("unrelated traffic is not this lease's reset");
changes.ShouldBeEmpty();
// Positive control: this lease's own source still reports, after the catch-up.
HubTest.Publish(hub, mine, 2, 2); HubTest.Publish(hub, other, 5001, 1); hub.ProcessPending();
interest.TryReadChanges(32, out changes).ShouldBeTrue();
changes.ShouldHaveSingleItem().Ticket.ShouldBe(mine);
}
}
[Fact, Trait("Issue", "994")]
public void R3_a_lease_behind_its_scan_budget_reports_more_and_its_own_changes_are_not_starved()
{
using var hub = HubTest.Create(partitions: 1, bytes: 1_000_000, ingress: 100, dirty: 100);
var mine = HubTest.Ticket("driver", "mine");
var other = HubTest.Ticket("driver", "busy");
HubTest.BindAndPublish(hub, mine, 1, 1);
HubTest.BindAndPublish(hub, other, 1, 1);
hub.ProcessPending();
hub.TryCaptureInterestSnapshot([mine], HubTest.Budget, CancellationToken.None, out var snapshot, out var interest).ShouldBeTrue();
using (interest!)
{
snapshot!.Dispose();
HubTest.Publish(hub, mine, 2, 2); hub.ProcessPending();
interest!.TryReadChanges(256, 4096, out var changes, out var more).ShouldBeTrue();
changes.ShouldHaveSingleItem().Ticket.ShouldBe(mine); more.ShouldBeFalse();
}
}
[Fact, Trait("Issue", "994")]
public void R9_invalidating_one_source_does_not_reset_a_lease_on_a_sibling_source_of_the_same_partition()
{
using var hub = HubTest.Create(partitions: 1, bytes: 1_000_000, ingress: 100, dirty: 100);
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.TryCaptureInterestSnapshot([first], HubTest.Budget, CancellationToken.None, out var firstSnapshot, out var firstInterest).ShouldBeTrue();
hub.TryCaptureInterestSnapshot([second], HubTest.Budget, CancellationToken.None, out var secondSnapshot, out var secondInterest).ShouldBeTrue();
using (firstInterest!) using (secondInterest!)
{
firstSnapshot!.Dispose(); secondSnapshot!.Dispose();
hub.Invalidate(first, "source serialization contention");
secondInterest!.IsReset.ShouldBeFalse("a sibling's invalidation is not this lease's");
secondInterest.TryReadChanges(32, out _).ShouldBeTrue();
// Positive control: the invalidated source's own lease still resets.
(firstInterest!.IsReset || !firstInterest.TryReadChanges(32, out _)).ShouldBeTrue();
// ...and the sibling lease keeps delivering afterwards.
HubTest.Publish(hub, second, 2, 2); hub.ProcessPending();
secondInterest.TryReadChanges(32, out var changes).ShouldBeTrue();
changes.ShouldContain(change => change.Ticket == second);
}
}
[Fact, Trait("Issue", "994")]
public void R9_a_partition_wide_invalidation_still_resets_every_lease()
{
using var hub = HubTest.Create(partitions: 1, bytes: 1_000_000, ingress: 100, dirty: 100);
var first = HubTest.Ticket("driver", "first");
HubTest.BindAndPublish(hub, first, 1, 1);
hub.ProcessPending();
hub.TryCaptureInterestSnapshot([first], HubTest.Budget, CancellationToken.None, out var snapshot, out var interest).ShouldBeTrue();
using (interest!)
{
snapshot!.Dispose();
hub.InvalidatePartitionFor(first.Key, "partition overflow");
hub.ProcessPending();
(interest!.IsReset || !interest.TryReadChanges(32, out _)).ShouldBeTrue();
}
}
}