fix(grpc-data): H3 review round 2 — one non-local history slot share; claim loop never leaks

Remote peers reading this node (OriginHistoryPort) and this node's projected
reads now draw on ONE N - max(1, N/4) semaphore, created together with the
N-slot batch limiter from the same N, so both together can never take the
last (local) slot (two independent first-use sizings could also disagree —
which made OriginHistoryLimiterTests order-dependent and hang the earlier
run). The projected claim loop releases every claimed cursor if anything
throws while the batch is assembled. Red/green: the shared-slot assertion
fails with a per-port semaphore.
This commit is contained in:
Joseph Doherty
2026-11-24 06:25:19 -05:00
parent 022b297836
commit 37cbeff480
5 changed files with 93 additions and 71 deletions
@@ -247,7 +247,7 @@ public sealed class ProjectedHistoryReadTests : IAsyncDisposable
// (d) the projected sub-limit is exhausted (limiter rejection).
(cursor, cp) = HeldPoint();
var limiter = nm.GetProjectedHistoryLimiter();
var limiter = nm.GetNonLocalHistoryLimiter();
var taken = 0;
while (limiter.Wait(0, TestContext.Current.CancellationToken)) taken++;
try
@@ -265,7 +265,7 @@ public sealed class ProjectedHistoryReadTests : IAsyncDisposable
}
[Fact]
public async Task Projected_reads_cannot_take_every_history_slot_local_history_is_still_served()
public async Task Projected_reads_and_remote_peers_together_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);
@@ -288,6 +288,14 @@ public sealed class ProjectedHistoryReadTests : IAsyncDisposable
.WaitAsync(TimeSpan.FromSeconds(5), TestContext.Current.CancellationToken);
refused[0].StatusCode.Code.ShouldBe(StatusCodes.BadTooManyOperations.Code);
slow.Entries.ShouldBe(3);
// H3 review round 2: remote peers reading THIS node share the same non-local slots — refused too, unsent — so the
// origin port and the projected path together can never take the last (local) slot.
var port = new OriginHistoryPort(nm);
port.AvailableRemoteSlots.ShouldBe(0);
var peer = await port.ReadAsync(new OriginHistoryCursors("peer", "channel", 4), "peer", () => "serving",
new DataHistoryRawDetails(new DateTimeOffset(T0), new DateTimeOffset(T0.AddHours(1)), 5, false),
[new OriginHistoryItem(AddressSpaceRealm.Raw, "Plant/LocalSlot", "tag", null, null)], TestContext.Current.CancellationToken);
peer.Single().StatusCode.ShouldBe(StatusCodes.BadTooManyOperations.Code);
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);
@@ -24,22 +24,25 @@ public sealed class OriginHistoryLimiterTests(HistoryReadPortServerFixture fx) :
var source = new BlockingSource();
try
{
fx.Nm.MaxConcurrentHistoryReadBatches = 4; // the limiter is sized at first use; this fixture is fresh
// The limiter is sized ONCE per node manager, at first use: with the shared fixture another test may have sized it
// already (H3 review round 2: the non-local share is now one node-manager semaphore), so saturate what exists.
fx.Nm.MaxConcurrentHistoryReadBatches = 4;
fx.Nm.HistorianDataSource = source;
var port = new OriginHistoryPort(fx.Nm);
OriginHistoryPort.RemoteSlots(4).ShouldBe(3);
var slots = port.AvailableRemoteSlots;
var details = new DataHistoryRawDetails(new DateTimeOffset(T0), new DateTimeOffset(T0.AddSeconds(5)), 1, false);
var item = new OriginHistoryItem(AddressSpaceRealm.Uns, "port/dbl", "peer:uns", null, null);
var remote = Enumerable.Range(0, 3).Select(i => port.ReadAsync(new OriginHistoryCursors("peer", "c" + i, 4), "peer", () => "inc",
var remote = Enumerable.Range(0, slots).Select(i => port.ReadAsync(new OriginHistoryCursors("peer", "c" + i, 4), "peer", () => "inc",
details, [item], CancellationToken.None)).ToArray();
await source.WaitForCalls(3);
await source.WaitForCalls(slots);
var refused = (await port.ReadAsync(new OriginHistoryCursors("peer", "c9", 4), "peer", () => "inc", details, [item], CancellationToken.None)).Single();
refused.StatusCode.ShouldBe(StatusCodes.BadTooManyOperations.Code, "the remote share is full");
// A local (SDK-path) read still gets the reserved slot.
var local = Task.Run(() => fx.Nm.ReadHistory(OriginHistoryPort.ToVariant(details),
[new HistoryReadPortItem(AddressSpaceRealm.Uns, "port/dbl", null, default, "urn:local")],
fx.Nm.SessionHistoryContinuationScope(null), CancellationToken.None));
await source.WaitForCalls(4);
await source.WaitForCalls(slots + 1);
source.Release.TrySetResult();
(await local).Single().Status.Code.ShouldNotBe(StatusCodes.BadTooManyOperations.Code);
foreach (var r in await Task.WhenAll(remote)) r.Single().StatusCode.ShouldNotBe(StatusCodes.BadTooManyOperations.Code);
@@ -64,7 +67,7 @@ public sealed class OriginHistoryLimiterTests(HistoryReadPortServerFixture fx) :
var port = new OriginHistoryPort(fx.Nm);
var details = new DataHistoryRawDetails(new DateTimeOffset(T0), new DateTimeOffset(T0.AddSeconds(5)), 1, false);
var item = new OriginHistoryItem(AddressSpaceRealm.Uns, "port/dbl", "peer:uns", null, null);
var slots = OriginHistoryPort.RemoteSlots(fx.Nm.MaxConcurrentHistoryReadBatches);
var slots = port.AvailableRemoteSlots; // the node manager's non-local share, sized once at first use
using var cancelled = new CancellationTokenSource();
await cancelled.CancelAsync();
for (var i = 0; i < slots + 3; i++)