diff --git a/src/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer/OriginHistoryPort.cs b/src/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer/OriginHistoryPort.cs index e655f0fff..63a0787ba 100644 --- a/src/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer/OriginHistoryPort.cs +++ b/src/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer/OriginHistoryPort.cs @@ -58,6 +58,9 @@ public sealed class OriginHistoryCursors /// Releases a token (idempotent). public bool Release(byte[]? token) => Store.Release(token); + /// Whether a token names a live page cursor (diagnostics). + public bool Contains(byte[]? token) => Store.Contains(token); + /// Drops every record (the channel closed). public void Clear() => Store.Clear(); } diff --git a/src/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer/PeerHistoryContinuationStore.cs b/src/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer/PeerHistoryContinuationStore.cs index 74d0aff4b..0aac055f5 100644 --- a/src/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer/PeerHistoryContinuationStore.cs +++ b/src/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer/PeerHistoryContinuationStore.cs @@ -185,6 +185,17 @@ internal sealed class PeerHistoryContinuationStore } } + /// Whether names a live (unexpired) record (diagnostics: de-duplicating open continuations). + public bool Contains(byte[]? token) + { + if (token is not { Length: TokenLength }) return false; + lock (_gate) + { + Sweep(_clock.GetTimestamp()); + return _entries.ContainsKey(new Guid(token)); + } + } + /// Drops a record without reading it (release_continuations). Idempotent. /// The token to release. /// Whether a live record was dropped. diff --git a/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/DataConnections/OriginHistorySessions.cs b/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/DataConnections/OriginHistorySessions.cs index aed8567d5..d732203e5 100644 --- a/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/DataConnections/OriginHistorySessions.cs +++ b/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/DataConnections/OriginHistorySessions.cs @@ -44,6 +44,27 @@ internal sealed class OriginHistorySession get { lock (_gate) { Sweep(_clock.GetTimestamp()); return _chunks.Count; } } } + /// + /// 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. + /// + 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; + } + } + /// /// A stored part of one node's answer. = the node was not read yet because the response budget /// was spent: resuming reads it then, from (its original page token, or null = fresh). @@ -194,14 +215,14 @@ internal sealed class OriginHistorySessions(TimeProvider clock, int maxSessions /// Live sessions (diagnostics/tests). public int Count { get { lock (_gate) return _sessions.Count; } } - /// Open page cursors plus chunk remainders across every session (diagnostics/tests). + /// Open continuations across every session, each once (a chunk holding its page cursor counts once). 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); } } diff --git a/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcDataOriginHistorySessionTests.cs b/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcDataOriginHistorySessionTests.cs index acaf1157d..a4c82a88e 100644 --- a/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcDataOriginHistorySessionTests.cs +++ b/tests/Server/ZB.MOM.WW.OtOpcUa.Runtime.Tests/DataConnections/GrpcDataOriginHistorySessionTests.cs @@ -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() {