Files
lmxopcua/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcDataCohortScaleTests.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

141 lines
8.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections;
using ZB.MOM.WW.OtOpcUa.Runtime.DataConnections;
namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.DataConnections;
/// <summary>#994: projected-cohort scaling against a real origin session (review 2026-12-02 R-2, R-7, R-13).</summary>
public sealed class GrpcDataCohortScaleTests
{
[Fact(Timeout = 60000), Trait("Issue", "994")]
public async Task R2_a_300_item_cohort_opens_against_the_origin_command_rate_and_every_item_gets_its_baseline()
{
using var fixture = new CohortScaleFixture(300);
using var access = fixture.Create(1024);
var demands = fixture.AcquireAll(access);
try
{
foreach (var demand in demands) access.StartSubscription(demand, TimeSpan.FromSeconds(1)).ShouldBeTrue();
await fixture.WaitForAsync(() => fixture.Baselines.Count == 300, TimeSpan.FromSeconds(30),
$"every item must reach its first value; baselines={fixture.Baselines.Count} opens={fixture.Opens} commands={fixture.Commands.Count}");
fixture.Opens.ShouldBe(1, "one physical stream carries the whole cohort");
fixture.Commands.Count.ShouldBeLessThan(128, "the open stays inside the origin's per-principal command rate");
}
finally { foreach (var demand in demands) demand.Dispose(); }
}
[Fact(Timeout = 60000), Trait("Issue", "994")]
public async Task R2_a_large_cohort_re_establishes_after_a_stream_drop()
{
using var fixture = new CohortScaleFixture(300);
using var access = fixture.Create(1024);
var demands = fixture.AcquireAll(access);
try
{
foreach (var demand in demands) access.StartSubscription(demand, TimeSpan.FromSeconds(1)).ShouldBeTrue();
await fixture.WaitForAsync(() => fixture.Baselines.Count == 300, TimeSpan.FromSeconds(30), "initial open");
fixture.Baselines.Clear();
// The origin's rate window is per second of the fixture clock: the re-open happens in the same window.
await fixture.DropStreamAsync();
await fixture.WaitForAsync(() => fixture.Baselines.Count == 300, TimeSpan.FromSeconds(30),
$"every item must be re-baselined after the drop; baselines={fixture.Baselines.Count} opens={fixture.Opens}");
fixture.Opens.ShouldBeGreaterThanOrEqualTo(2, "the origin may still hold the dropped session's interests for one retry");
}
finally { foreach (var demand in demands) demand.Dispose(); }
}
[Fact(Timeout = 60000), Trait("Issue", "994")]
public async Task R2_two_items_in_one_cohort_both_reach_the_origin()
{
// Positive control for the batch path at its smallest: two items added after the open (separate StartSubscription calls).
using var fixture = new CohortScaleFixture(2);
using var access = fixture.Create(16);
var demands = fixture.AcquireAll(access);
try
{
access.StartSubscription(demands[0], TimeSpan.FromSeconds(1)).ShouldBeTrue();
await fixture.WaitForAsync(() => fixture.Baselines.Count == 1, TimeSpan.FromSeconds(10), "first item");
access.StartSubscription(demands[1], TimeSpan.FromSeconds(1)).ShouldBeTrue();
await fixture.WaitForAsync(() => fixture.Baselines.Count == 2, TimeSpan.FromSeconds(10),
$"the second item joins the same origin stream; opens={fixture.Opens} commands={string.Join(",", fixture.Commands.Select(c => c.GetType().Name))}");
fixture.Opens.ShouldBe(1);
}
finally { foreach (var demand in demands) demand.Dispose(); }
}
[Fact(Timeout = 60000), Trait("Issue", "994")]
public async Task R7_removing_one_item_while_the_frame_backlog_is_full_keeps_every_sibling_demand_live()
{
using var fixture = new CohortScaleFixture(8);
using var access = fixture.Create(16);
var demands = fixture.AcquireAll(access);
using var blocked = new ManualResetEventSlim(true);
var entered = 0;
fixture.PublishGate = _ => { Interlocked.Increment(ref entered); blocked.Wait(TimeSpan.FromSeconds(20)); return true; };
try
{
foreach (var demand in demands) access.StartSubscription(demand, TimeSpan.FromMilliseconds(100)).ShouldBeTrue();
await fixture.WaitForAsync(() => fixture.Baselines.Count == 8, TimeSpan.FromSeconds(20), "initial baselines");
// Stall the cohort's event loop inside a publish, then let the origin keep producing frames so the pump fills
// the cohort's frame backlog behind it.
blocked.Reset();
var stalled = Volatile.Read(ref entered);
fixture.PublishVersion(2, 1000); fixture.Origin.Clock.Advance(TimeSpan.FromMilliseconds(100));
await fixture.WaitForAsync(() => Volatile.Read(ref entered) > stalled, TimeSpan.FromSeconds(10), "the loop is stalled in a publish");
for (var version = 3; version < 80; version++)
{ fixture.PublishVersion(version, 1000 + version); fixture.Origin.Clock.Advance(TimeSpan.FromMilliseconds(40)); await Task.Delay(5, TestContext.Current.CancellationToken); }
await Task.Delay(500, TestContext.Current.CancellationToken);
demands[0].Dispose();
await Task.Delay(200, TestContext.Current.CancellationToken);
blocked.Set();
foreach (var sibling in demands.Skip(1)) sibling.IsValid.ShouldBeTrue("a sibling demand must survive one item's removal");
// ...and keeps delivering: a later value reaches every sibling.
fixture.Latest.Clear();
fixture.PublishVersion(100, 5000); fixture.Origin.Clock.Advance(TimeSpan.FromMilliseconds(200));
await fixture.WaitForAsync(() => demands.Skip(1).All(d => fixture.Latest.ContainsKey(d.LocalIdentifier)),
TimeSpan.FromSeconds(20), $"siblings keep receiving values; latest={fixture.Latest.Count}");
}
finally { blocked.Set(); foreach (var demand in demands) demand.Dispose(); }
}
[Fact(Timeout = 60000), Trait("Issue", "994")]
public async Task R10_a_capacity_refused_sample_is_retried_without_invalidating_the_source_or_reopening_the_stream()
{
using var fixture = new CohortScaleFixture(4);
using var access = fixture.Create(16);
var demands = fixture.AcquireAll(access);
try
{
fixture.CapacityRefusals = 20; // the local sink is full for the first 20 publications (about 200 ms of backpressure)
foreach (var demand in demands) access.StartSubscription(demand, TimeSpan.FromSeconds(1)).ShouldBeTrue();
await fixture.WaitForAsync(() => fixture.Baselines.Count == 4, TimeSpan.FromSeconds(20),
$"every baseline is delivered once the sink drains; refusals left={fixture.CapacityRefusals}");
fixture.CapacityRefusals.ShouldBeLessThanOrEqualTo(0);
fixture.Retired.ShouldBe(0, "local backpressure must not retire a healthy route");
fixture.Opens.ShouldBe(1, "nor force a re-snapshot");
foreach (var demand in demands) demand.IsValid.ShouldBeTrue();
}
finally { foreach (var demand in demands) demand.Dispose(); }
}
// R-2 (found by the 10k perf harness): the origin session's delivery index was sized from its frame window
// (MaxPendingFrames × 16 = 1024), so the 1025th item of one stream stopped the session after its acknowledgement and
// a cohort above 1024 items could never open.
[Fact(Timeout = 120000), Trait("Issue", "994")]
public async Task R2_a_cohort_above_1024_items_opens_on_one_origin_stream()
{
using var fixture = new CohortScaleFixture(1100, maxBatchItems: 1000);
using var access = fixture.Create(2048);
var demands = fixture.AcquireAll(access);
try
{
foreach (var demand in demands) access.StartSubscription(demand, TimeSpan.FromSeconds(1)).ShouldBeTrue();
await fixture.WaitForAsync(() => fixture.Baselines.Count == 1100, TimeSpan.FromSeconds(60),
$"every item must reach its first value; baselines={fixture.Baselines.Count} opens={fixture.Opens}");
fixture.Opens.ShouldBe(1);
}
finally { foreach (var demand in demands) demand.Dispose(); }
}
}