fix(grpc-data): H4 review 4 — an origin chunk holding its page cursor is one open continuation, not two

This commit is contained in:
Joseph Doherty
2026-11-24 08:39:02 -05:00
parent b5509dfc48
commit 79cf50d50e
4 changed files with 55 additions and 2 deletions
@@ -58,6 +58,9 @@ public sealed class OriginHistoryCursors
/// <summary>Releases a token (idempotent).</summary>
public bool Release(byte[]? token) => Store.Release(token);
/// <summary>Whether a token names a live page cursor (diagnostics).</summary>
public bool Contains(byte[]? token) => Store.Contains(token);
/// <summary>Drops every record (the channel closed).</summary>
public void Clear() => Store.Clear();
}
@@ -185,6 +185,17 @@ internal sealed class PeerHistoryContinuationStore
}
}
/// <summary>Whether <paramref name="token"/> names a live (unexpired) record (diagnostics: de-duplicating open continuations).</summary>
public bool Contains(byte[]? token)
{
if (token is not { Length: TokenLength }) return false;
lock (_gate)
{
Sweep(_clock.GetTimestamp());
return _entries.ContainsKey(new Guid(token));
}
}
/// <summary>Drops a record without reading it (<c>release_continuations</c>). Idempotent.</summary>
/// <param name="token">The token to release.</param>
/// <returns>Whether a live record was dropped.</returns>
@@ -44,6 +44,27 @@ internal sealed class OriginHistorySession
get { lock (_gate) { Sweep(_clock.GetTimestamp()); return _chunks.Count; } }
}
/// <summary>
/// H4 review: open continuations of this session, each counted once — a chunk that still holds its page cursor (a
/// deferred node, or a remainder that ends with a continuation) is one open continuation, not two.
/// </summary>
public int OpenContinuations
{
get
{
byte[]?[] behindChunks;
int chunks;
lock (_gate)
{
Sweep(_clock.GetTimestamp());
chunks = _chunks.Count;
behindChunks = _chunks.Values.Select(x => x.Continuation).ToArray();
}
var shared = behindChunks.Count(token => token is not null && Cursors.Contains(token));
return chunks + Cursors.Count - shared;
}
}
/// <summary>
/// A stored part of one node's answer. <see cref="Deferred"/> = the node was not read yet because the response budget
/// was spent: resuming reads it then, from <see cref="Continuation"/> (its original page token, or null = fresh).
@@ -194,14 +215,14 @@ internal sealed class OriginHistorySessions(TimeProvider clock, int maxSessions
/// <summary>Live sessions (diagnostics/tests).</summary>
public int Count { get { lock (_gate) return _sessions.Count; } }
/// <summary>Open page cursors plus chunk remainders across every session (diagnostics/tests).</summary>
/// <summary>Open continuations across every session, each once (a chunk holding its page cursor counts once).</summary>
public int OpenContinuations
{
get
{
OriginHistorySession[] sessions;
lock (_gate) sessions = _sessions.Values.Select(x => x.Session).ToArray();
return sessions.Sum(x => x.Cursors.Count + x.ChunkCount);
return sessions.Sum(x => x.OpenContinuations);
}
}
@@ -73,6 +73,24 @@ public sealed class GrpcDataOriginHistorySessionTests
sessions.RetainedBytes("a").ShouldBe(0);
}
[Fact]
[Trait("Phase", "H4")]
public void Open_continuations_count_a_chunk_holding_its_page_cursor_once()
{
// H4 review item 4: Cursors.Count + ChunkCount counted a chunk AND the page cursor behind it.
var sessions = new OriginHistorySessions(TimeProvider.System);
var session = sessions.Get(Caller("a"), Channel("1"), 8);
var view = new DataView(new("c", "u", "string", "x"), DataRealm.Raw, null, 0, "x");
byte[] Page() => session.Cursors.Store.Save(new ZB.MOM.WW.OtOpcUa.OpcUaServer.HistoryContinuationState("t", DateTime.UnixEpoch,
DateTime.UnixEpoch, 0, 1) { BoundIdentifier = "x" }, "a", "inc", "h");
session.SaveChunk(new OriginHistorySession.Chunk(view, "k", "i", [], 0, Page(), null, 0, 10), 1000).ShouldNotBeNull();
session.SaveChunk(new OriginHistorySession.Chunk(view, "k", "i", [], 0, null, null, 0, 10), 1000).ShouldNotBeNull();
Page(); // a plain page cursor, no chunk
(session.Cursors.Count, session.ChunkCount).ShouldBe((2, 2));
session.OpenContinuations.ShouldBe(3, "chunk+its cursor, a chunk without one, a plain cursor");
sessions.OpenContinuations.ShouldBe(3);
}
[Fact]
public void Remainder_bytes_are_budgeted_per_principal_across_sessions()
{