fix(grpc-data): H3 review D2/D3/D5 — claimed cursors never leak; projected slot sub-limit
D2: Release() after TryClaimForResume() was a no-op, so a claimed cursor leaked its origin continuation on every exit that never sent it (no gateway/identity, deadline spent after the local half, limiter rejection, a gateway fault). New ReleaseEvenIfClaimed() (at most once) on every post-claim exit; any exception from the gateway now fails the projected half per node instead of escaping. D3: projected reads hold at most N - max(1, N/4) of the N shared history batch slots (OriginHistoryPort.RemoteSlots), Wait(0) => BadTooManyOperations for the projected half, so a slow origin cannot starve local HistoryRead/HistoryUpdate. D5: a Remote cursor presented to a local node (raw/modified/events take paths) is released when it is taken. Red/green: each test fails with its fix reverted.
This commit is contained in:
+122
-3
@@ -206,6 +206,115 @@ public sealed class ProjectedHistoryReadTests : IAsyncDisposable
|
||||
atTime.UseSimpleBounds.ShouldBeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task A_claimed_cursor_is_released_on_every_exit_that_never_sends_it()
|
||||
{
|
||||
// H3 review D2: Release() after a claim is a no-op, so each post-claim exit must use ReleaseEvenIfClaimed — exactly once.
|
||||
var (nm, f) = await BootAsync(aclEnabled: false, NodePermissions.Browse | NodePermissions.Read | NodePermissions.HistoryRead);
|
||||
var store = new InMemoryHistoryContinuationStore();
|
||||
nm.HistoryContinuationStore = store;
|
||||
var node = Project(nm, f, "Leak", historizing: true);
|
||||
var paging = new FakeAccess { Paged = true };
|
||||
nm.ProjectedHistoryAccess = paging;
|
||||
|
||||
(FakeCursor Cursor, ByteString Cp) HeldPoint()
|
||||
{
|
||||
var (results, _) = Raw(nm, f.Identity, [node]);
|
||||
results[0].ContinuationPoint.IsEmpty.ShouldBeFalse();
|
||||
return (paging.Issued[^1], results[0].ContinuationPoint);
|
||||
}
|
||||
|
||||
// (a) the gateway disappears between the issue and the resume.
|
||||
var (cursor, cp) = HeldPoint();
|
||||
nm.ProjectedHistoryAccess = null;
|
||||
Raw(nm, f.Identity, [node], cp).Errors[0].StatusCode.Code.ShouldBe(StatusCodes.BadHistoryOperationUnsupported.Code);
|
||||
cursor.Released.ShouldBe(1);
|
||||
nm.ProjectedHistoryAccess = paging;
|
||||
|
||||
// (b) the gateway throws (anything): BadNoCommunication, the resume released, nothing escapes.
|
||||
(cursor, cp) = HeldPoint();
|
||||
nm.ProjectedHistoryAccess = new FakeAccess { Throw = new InvalidCastException("boom") };
|
||||
Raw(nm, f.Identity, [node], cp).Errors[0].StatusCode.Code.ShouldBe(StatusCodes.BadNoCommunication.Code);
|
||||
cursor.Released.ShouldBe(1);
|
||||
nm.ProjectedHistoryAccess = paging;
|
||||
|
||||
// (c) the request deadline was spent by the time the projected half starts.
|
||||
(cursor, cp) = HeldPoint();
|
||||
nm.HistoryReadDeadline = TimeSpan.FromTicks(1);
|
||||
Raw(nm, f.Identity, [node], cp).Errors[0].StatusCode.Code.ShouldBe(StatusCodes.BadTimeout.Code);
|
||||
cursor.Released.ShouldBe(1);
|
||||
nm.HistoryReadDeadline = TimeSpan.FromSeconds(60);
|
||||
|
||||
// (d) the projected sub-limit is exhausted (limiter rejection).
|
||||
(cursor, cp) = HeldPoint();
|
||||
var limiter = nm.GetProjectedHistoryLimiter();
|
||||
var taken = 0;
|
||||
while (limiter.Wait(0, TestContext.Current.CancellationToken)) taken++;
|
||||
try
|
||||
{
|
||||
Raw(nm, f.Identity, [node], cp).Errors[0].StatusCode.Code.ShouldBe(StatusCodes.BadTooManyOperations.Code);
|
||||
cursor.Released.ShouldBe(1);
|
||||
}
|
||||
finally { limiter.Release(taken); }
|
||||
|
||||
// Positive control: a normal resume claims the cursor and does NOT release it (the read owns the token).
|
||||
(cursor, cp) = HeldPoint();
|
||||
Raw(nm, f.Identity, [node], cp).Errors[0].StatusCode.Code.ShouldBe(StatusCodes.Good.Code);
|
||||
cursor.Released.ShouldBe(0);
|
||||
paging.Issued.Where(c => !ReferenceEquals(c, paging.Issued[^1])).ShouldAllBe(c => c.Released <= 1, "never released twice");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Projected_reads_cannot_take_every_history_slot_local_history_is_still_served()
|
||||
{
|
||||
// H3 review D3: projected reads hold at most N - max(1, N/4) of the N shared batch slots.
|
||||
var (nm, f) = await BootAsync(aclEnabled: false, NodePermissions.Browse | NodePermissions.Read | NodePermissions.HistoryRead);
|
||||
nm.MaxConcurrentHistoryReadBatches = 4; // => 3 projected slots, 1 always left for local history
|
||||
nm.EnsureVariable("Plant/LocalSlot", "Plant", "LocalSlot", "Double", false, AddressSpaceRealm.Raw, historianTagname: "L.slot");
|
||||
var local = nm.TryGetVariable("Plant/LocalSlot", AddressSpaceRealm.Raw)!.NodeId;
|
||||
var node = Project(nm, f, "Slow", historizing: true);
|
||||
var hold = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
var slow = new FakeAccess { Hold = hold };
|
||||
nm.ProjectedHistoryAccess = slow;
|
||||
var held = Enumerable.Range(0, 3).Select(_ => Task.Run(() => Raw(nm, f.Identity, [node]))).ToArray();
|
||||
var deadline = DateTime.UtcNow.AddSeconds(10);
|
||||
while (Volatile.Read(ref slow.Entries) < 3 && DateTime.UtcNow < deadline) await Task.Delay(10, TestContext.Current.CancellationToken);
|
||||
slow.Entries.ShouldBe(3, "three projected reads hold the three projected slots");
|
||||
try
|
||||
{
|
||||
// A fourth projected read is refused at once, unsent; local history is still served.
|
||||
// (Bounded: without the sub-limit this read would take the 4th shared slot and wait on the held gateway.)
|
||||
var (_, refused) = await Task.Run(() => Raw(nm, f.Identity, [node]))
|
||||
.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
|
||||
refused[0].StatusCode.Code.ShouldBe(StatusCodes.BadTooManyOperations.Code);
|
||||
slow.Entries.ShouldBe(3);
|
||||
var (localResults, localErrors) = Raw(nm, f.Identity, [local]);
|
||||
StatusCode.IsGood(localErrors[0].StatusCode).ShouldBeTrue(localErrors[0].StatusCode.ToString());
|
||||
localResults[0].StatusCode.Code.ShouldBe(StatusCodes.GoodNoData.Code);
|
||||
}
|
||||
finally { hold.SetResult(); }
|
||||
foreach (var (_, errors) in await Task.WhenAll(held)) StatusCode.IsGood(errors[0].StatusCode).ShouldBeTrue();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task A_remote_cursor_presented_to_a_local_node_is_invalid_and_released()
|
||||
{
|
||||
// H3 review D5: the local take removes the SDK slot, so it must release the origin continuation.
|
||||
var (nm, f) = await BootAsync(aclEnabled: false, NodePermissions.Browse | NodePermissions.Read | NodePermissions.HistoryRead);
|
||||
var store = new InMemoryHistoryContinuationStore();
|
||||
nm.HistoryContinuationStore = store;
|
||||
nm.EnsureVariable("Plant/LocalCp", "Plant", "LocalCp", "Double", false, AddressSpaceRealm.Raw, historianTagname: "L.cp");
|
||||
var local = nm.TryGetVariable("Plant/LocalCp", AddressSpaceRealm.Raw)!.NodeId;
|
||||
var node = Project(nm, f, "Remote", historizing: true);
|
||||
var access = new FakeAccess { Paged = true };
|
||||
nm.ProjectedHistoryAccess = access;
|
||||
var (results, _) = Raw(nm, f.Identity, [node]);
|
||||
var (_, errors) = Raw(nm, f.Identity, [local], results[0].ContinuationPoint);
|
||||
errors[0].StatusCode.Code.ShouldBe(StatusCodes.BadContinuationPointInvalid.Code);
|
||||
access.Issued.Single().Released.ShouldBe(1);
|
||||
store.Count.ShouldBe(0);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
private static (List<SdkHistoryReadResult> Results, List<ServiceResult> Errors) Raw(OtOpcUaNodeManager nm, IUserIdentity? identity,
|
||||
@@ -326,12 +435,22 @@ public sealed class ProjectedHistoryReadTests : IAsyncDisposable
|
||||
public uint Status { get; init; }
|
||||
public Func<ProjectedHistoryNode, double[]> Answer { get; init; } = _ => [1.0, 2.0];
|
||||
|
||||
public Task<IReadOnlyList<ProjectedHistoryOutcome>> ReadAsync(RoleCarryingUserIdentity caller, DataHistoryDetails details,
|
||||
/// <summary>When set, every call waits on it (a projected read holding its slot).</summary>
|
||||
public TaskCompletionSource? Hold { get; set; }
|
||||
public TaskCompletionSource Entered { get; } = new(TaskCreationOptions.RunContinuationsAsynchronously);
|
||||
/// <summary>When set, every call throws it (after claiming nothing — the cursors are the caller's to release).</summary>
|
||||
public Exception? Throw { get; set; }
|
||||
public int Entries;
|
||||
|
||||
public async Task<IReadOnlyList<ProjectedHistoryOutcome>> ReadAsync(RoleCarryingUserIdentity caller, DataHistoryDetails details,
|
||||
IReadOnlyList<ProjectedHistoryNode> nodes, TimeSpan deadline, CancellationToken cancellationToken)
|
||||
{
|
||||
Calls++;
|
||||
Interlocked.Increment(ref Calls);
|
||||
LastNodes = nodes;
|
||||
return Task.FromResult<IReadOnlyList<ProjectedHistoryOutcome>>(nodes.Select(node =>
|
||||
if (Interlocked.Increment(ref Entries) > 0) Entered.TrySetResult();
|
||||
if (Hold is { } hold) await hold.Task.ConfigureAwait(false);
|
||||
if (Throw is { } error) throw error;
|
||||
return (nodes.Select(node =>
|
||||
{
|
||||
FakeCursor? next = null;
|
||||
if (Paged)
|
||||
|
||||
Reference in New Issue
Block a user