Files
lmxopcua/src/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer/IProjectedHistoryAccess.cs
T
Joseph Doherty 585d5079eb 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.
2026-11-24 01:40:18 -05:00

101 lines
5.6 KiB
C#

using Opc.Ua;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections;
using ZB.MOM.WW.OtOpcUa.OpcUaServer.Security;
namespace ZB.MOM.WW.OtOpcUa.OpcUaServer;
/// <summary>
/// H3 — the remote cursor behind a <see cref="HistoryCursorKind.Remote"/> continuation point in the consumer's SDK session
/// store (gRPC data history plan §3.2). Opaque to the node manager: the projection side (Runtime) owns what it binds —
/// the origin token, mapping id/revision, serving incarnation, projection generation, caller key and the ORIGINAL request
/// details — and re-checks all of it on resume. The node manager only binds it to the projected node it was issued for.
/// </summary>
public abstract class ProjectedHistoryContinuation
{
private int _claimed, _released;
/// <summary>Creates a cursor bound to one projected node.</summary>
/// <param name="realm">The projected node's realm.</param>
/// <param name="localIdentifier">The projected node's opaque local identifier.</param>
protected ProjectedHistoryContinuation(DataRealm realm, string localIdentifier)
{
ArgumentException.ThrowIfNullOrWhiteSpace(localIdentifier);
Realm = realm;
LocalIdentifier = localIdentifier;
}
/// <summary>The projected node's realm.</summary>
public DataRealm Realm { get; }
/// <summary>The projected node's local identifier.</summary>
public string LocalIdentifier { get; }
/// <summary>True once the cursor was released or handed back for a resume (it is single-use either way).</summary>
public bool IsSpent => Volatile.Read(ref _released) != 0 || Volatile.Read(ref _claimed) != 0;
/// <summary>
/// Best-effort, idempotent, non-blocking release of the origin continuation (<c>releaseContinuationPoints</c>, session
/// close, eviction, a store refusal). A no-op once the cursor was claimed for a resume: the resume owns it from then on
/// (and uses <see cref="ReleaseEvenIfClaimed"/> on every exit that does not send it). The origin's idle expiry is the
/// backstop.
/// </summary>
public void Release()
{
if (Volatile.Read(ref _claimed) != 0) return;
ReleaseOnce();
}
/// <summary>
/// Releases the origin continuation whether or not the cursor was claimed for a resume — for every post-claim exit that
/// never sends it (refused, limiter-rejected, timed out, faulted). Never releases twice.
/// </summary>
public void ReleaseEvenIfClaimed() => ReleaseOnce();
/// <summary>Marks the cursor as handed back for a resume: the resume read owns the origin token from here on.</summary>
/// <returns><c>false</c> when it was already released or resumed.</returns>
public bool TryClaimForResume() => Volatile.Read(ref _released) == 0 && Interlocked.Exchange(ref _claimed, 1) == 0;
private void ReleaseOnce()
{
if (Interlocked.Exchange(ref _released, 1) == 0) OnRelease();
}
/// <summary>Forwards the release to the origin (called at most once).</summary>
protected abstract void OnRelease();
}
/// <summary>One projected node of a consumer HistoryRead (H3).</summary>
/// <param name="NodeId">The projected node.</param>
/// <param name="AggregateId">Processed only: the requested aggregate's ns=0 numeric id.</param>
/// <param name="Resume">The claimed remote cursor of a resumed read; null = a fresh read.</param>
public sealed record ProjectedHistoryNode(NodeId NodeId, uint? AggregateId = null, ProjectedHistoryContinuation? Resume = null);
/// <summary>One projected node's outcome: the origin's Part 11 status verbatim, its values, and the next cursor.</summary>
/// <param name="StatusCode">The full OPC UA status.</param>
/// <param name="Samples">The values (CLR values, verbatim status and timestamps).</param>
/// <param name="Continuation">The next remote cursor; null when the read is complete.</param>
public sealed record ProjectedHistoryOutcome(uint StatusCode, IReadOnlyList<OriginHistorySample> Samples, ProjectedHistoryContinuation? Continuation = null);
/// <summary>
/// H3 — HistoryRead on projected variables (plan §2.1, §3.2): resolves each projected node to its mapping, deployed
/// connection lease and origin view, authorizes it (the mapping's HistoryRead bit and a local
/// <c>NodePermissions.HistoryRead</c> grant on the mapping scope, ACLs on or off), and forwards through
/// <see cref="IDataHistoryService"/>. A fresh read may move endpoint within its deadline; a resumed read never moves.
/// </summary>
public interface IProjectedHistoryAccess
{
/// <summary>
/// Reads one batch of projected nodes for <paramref name="caller"/>. Never called under the node-manager <c>Lock</c>.
/// Results are in <paramref name="nodes"/> order. Every <see cref="ProjectedHistoryNode.Resume"/> cursor is owned by
/// the call (resumed or released), whatever the outcome.
/// </summary>
/// <param name="caller">The session identity.</param>
/// <param name="details">The request details (fresh nodes; a resumed node resends its ORIGINAL details).</param>
/// <param name="nodes">The projected nodes.</param>
/// <param name="deadline">The consumer request's remaining <c>HistoryReadDeadline</c>; non-positive = unbounded.</param>
/// <param name="cancellationToken">The SDK request lifetime.</param>
/// <returns>One outcome per node.</returns>
Task<IReadOnlyList<ProjectedHistoryOutcome>> ReadAsync(RoleCarryingUserIdentity caller, DataHistoryDetails details,
IReadOnlyList<ProjectedHistoryNode> nodes, TimeSpan deadline, CancellationToken cancellationToken);
}