fix(grpc-data): H1 peer takes consume before verifying, scopes snapshot the incarnation per request, wiring faults are BadInternalError (verification 1-3, 7b)
- a take computes its expectation only after consuming the record, so a throwing incarnation source cannot leave a token replayable; - a PeerHistoryContinuationScope is per request and reads the serving incarnation once, so a request straddling a failover binds its next token to the incarnation it started under (Save never answers null); - a non-owner principal or a blank/throwing incarnation source raises PeerHistoryContinuationWiringException, answered BadInternalError (not BadHistoryOperationUnsupported); plan H2 note added; - ConnectionIncarnation documented as identity only.
This commit is contained in:
@@ -10708,6 +10708,8 @@ public sealed partial class OtOpcUaNodeManager : CustomNodeManager2, INodeManage
|
||||
/// <summary>Transport failures are retryable; unsupported operations retain their distinct status.</summary>
|
||||
private static StatusCode HistoryReadFailureStatus(Exception error)
|
||||
{
|
||||
// A miswired peer continuation scope (H1/H2) is a server fault, never "this node has no history".
|
||||
if (error is PeerHistoryContinuationWiringException) return StatusCodes.BadInternalError;
|
||||
for (Exception? ex = error; ex is not null; ex = ex.InnerException)
|
||||
if (ex is System.IO.IOException or System.Net.Sockets.SocketException or System.Net.Http.HttpRequestException or TimeoutException)
|
||||
return StatusCodes.BadNoCommunication;
|
||||
|
||||
@@ -78,7 +78,11 @@ internal sealed class PeerHistoryContinuationStore
|
||||
/// <summary>The principal this instance serves.</summary>
|
||||
public string Principal { get; }
|
||||
|
||||
/// <summary>The connection's incarnation (which connection this store belongs to).</summary>
|
||||
/// <summary>
|
||||
/// The connection's incarnation — IDENTITY ONLY (which connection this store belongs to, for diagnostics and H2's
|
||||
/// per-connection bookkeeping). It is never part of a binding: the serving incarnation a cursor binds is supplied per
|
||||
/// request by <see cref="PeerHistoryContinuationScope"/>.
|
||||
/// </summary>
|
||||
public string ConnectionIncarnation { get; }
|
||||
|
||||
/// <summary>The most live records.</summary>
|
||||
@@ -111,16 +115,18 @@ internal sealed class PeerHistoryContinuationStore
|
||||
/// <param name="servingIncarnation">The serving incarnation current at save.</param>
|
||||
/// <param name="detailsHash">The request's details hash.</param>
|
||||
/// <returns>The 16-byte token.</returns>
|
||||
/// <exception cref="ArgumentException">An unbound state, or a principal other than the owner.</exception>
|
||||
/// <exception cref="ArgumentException">An unbound state.</exception>
|
||||
/// <exception cref="PeerHistoryContinuationWiringException">A principal other than the owner, or a blank incarnation.</exception>
|
||||
public byte[] Save(HistoryContinuationState state, string principal, string servingIncarnation, string detailsHash)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(state);
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(servingIncarnation);
|
||||
ArgumentNullException.ThrowIfNull(detailsHash);
|
||||
if (state.BoundIdentifier is null)
|
||||
throw new ArgumentException("A peer continuation must be bound to its view.", nameof(state));
|
||||
if (string.IsNullOrWhiteSpace(servingIncarnation))
|
||||
throw new PeerHistoryContinuationWiringException("A peer continuation needs the serving incarnation it is issued under.");
|
||||
if (!string.Equals(principal, Principal, StringComparison.Ordinal))
|
||||
throw new ArgumentException("A peer continuation store holds only its owning principal's cursors.", nameof(principal));
|
||||
throw new PeerHistoryContinuationWiringException("A peer continuation store holds only its owning principal's cursors.");
|
||||
|
||||
var token = RandomNumberGenerator.GetBytes(TokenLength);
|
||||
var key = new PeerHistoryContinuationKey(
|
||||
@@ -144,16 +150,38 @@ internal sealed class PeerHistoryContinuationStore
|
||||
/// <param name="token">The token the peer handed back.</param>
|
||||
/// <param name="expected">What the resume must match.</param>
|
||||
/// <returns>The state, or null (unknown, expired, evicted, malformed or mismatched).</returns>
|
||||
public HistoryContinuationState? TryTake(byte[]? token, PeerHistoryContinuationKey expected)
|
||||
public HistoryContinuationState? TryTake(byte[]? token, PeerHistoryContinuationKey expected) =>
|
||||
TryTake(token, () => expected);
|
||||
|
||||
/// <summary>
|
||||
/// <see cref="TryTake(byte[], PeerHistoryContinuationKey)"/> with the expectation computed only AFTER the record is
|
||||
/// consumed: if <paramref name="expected"/> throws (the incarnation source is down), the token is already gone and the
|
||||
/// take is a miss — never a token left replayable.
|
||||
/// </summary>
|
||||
/// <param name="token">The token the peer handed back.</param>
|
||||
/// <param name="expected">Computes what the resume must match.</param>
|
||||
/// <returns>The state, or null.</returns>
|
||||
public HistoryContinuationState? TryTake(byte[]? token, Func<PeerHistoryContinuationKey> expected)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(expected);
|
||||
if (token is not { Length: TokenLength }) return null;
|
||||
Entry taken;
|
||||
lock (_gate)
|
||||
{
|
||||
var now = _clock.GetTimestamp();
|
||||
Sweep(now);
|
||||
if (!_entries.TryGetValue(new Guid(token), out var node)) return null;
|
||||
Remove(node);
|
||||
return node.Value.Key == expected ? node.Value.State : null;
|
||||
taken = node.Value;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
return taken.Key == expected() ? taken.State : null;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
return null; // consumed, unverifiable ⇒ a miss (BadContinuationPointInvalid at the port)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -196,11 +224,32 @@ internal sealed class PeerHistoryContinuationStore
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The port's continuation scope for one remote peer: saves and takes through that peer's
|
||||
/// <see cref="PeerHistoryContinuationStore"/>, binding the caller's principal, the serving incarnation CURRENT at the
|
||||
/// save and at the take (read live from <paramref name="currentServingIncarnation"/>, so an in-process failover or role
|
||||
/// change on a still-open connection invalidates its tokens), and the request's details hash, on top of the view and
|
||||
/// epoch the port itself re-checks.
|
||||
/// A peer continuation store or scope was wired wrongly — a scope for a principal the store does not belong to, or a
|
||||
/// serving-incarnation source that is blank or throws. A server fault, not a property of the request: the port answers
|
||||
/// <c>BadInternalError</c> for the node (never <c>BadHistoryOperationUnsupported</c>), and H2 must surface it distinctly.
|
||||
/// </summary>
|
||||
internal sealed class PeerHistoryContinuationWiringException : InvalidOperationException
|
||||
{
|
||||
/// <summary>Creates the exception.</summary>
|
||||
/// <param name="message">What is miswired.</param>
|
||||
/// <param name="inner">The incarnation source's own failure, if any.</param>
|
||||
public PeerHistoryContinuationWiringException(string message, Exception? inner = null) : base(message, inner) { }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The port's continuation scope for one remote peer's ONE request (create a new scope per <c>ReadHistory</c> call):
|
||||
/// saves and takes through that peer's <see cref="PeerHistoryContinuationStore"/>, binding the caller's principal, the
|
||||
/// request's details hash and the serving incarnation, on top of the view and epoch the port itself re-checks.
|
||||
/// <para>
|
||||
/// The serving incarnation is read from <paramref name="currentServingIncarnation"/> ONCE, at the scope's first use,
|
||||
/// and that snapshot is used for every take and save of the request: a request resumed under incarnation A binds its
|
||||
/// next token to A even when a failover lands mid-request (the resume under B then fails
|
||||
/// <c>BadContinuationPointInvalid</c>), and a later request reads the then-current incarnation, so an in-process
|
||||
/// failover or role change on a still-open connection invalidates the connection's tokens. A take whose snapshot
|
||||
/// cannot be read (blank / throwing source) is a miss with the token consumed; a save is a
|
||||
/// <see cref="PeerHistoryContinuationWiringException"/>. Save never answers <c>null</c> (that would read as a
|
||||
/// complete read and truncate).
|
||||
/// </para>
|
||||
/// </summary>
|
||||
/// <param name="store">The (principal, connection) store.</param>
|
||||
/// <param name="principal">The caller's authenticated principal.</param>
|
||||
@@ -208,13 +257,30 @@ internal sealed class PeerHistoryContinuationStore
|
||||
internal sealed class PeerHistoryContinuationScope(
|
||||
PeerHistoryContinuationStore store, string principal, Func<string> currentServingIncarnation) : IHistoryContinuationScope
|
||||
{
|
||||
private readonly Lazy<string> _incarnation = new(() =>
|
||||
{
|
||||
string value;
|
||||
try
|
||||
{
|
||||
value = currentServingIncarnation();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
throw new PeerHistoryContinuationWiringException("The serving-incarnation source failed.", ex);
|
||||
}
|
||||
|
||||
return string.IsNullOrWhiteSpace(value)
|
||||
? throw new PeerHistoryContinuationWiringException("The serving-incarnation source answered a blank incarnation.")
|
||||
: value;
|
||||
}, LazyThreadSafetyMode.ExecutionAndPublication);
|
||||
|
||||
/// <inheritdoc />
|
||||
public byte[]? Save(HistoryContinuationState state, HistoryContinuationBinding binding) =>
|
||||
store.Save(state, principal, currentServingIncarnation(), binding.Details?.ComputeDetailsHash() ?? string.Empty);
|
||||
store.Save(state, principal, _incarnation.Value, binding.Details?.ComputeDetailsHash() ?? string.Empty);
|
||||
|
||||
/// <inheritdoc />
|
||||
public HistoryContinuationState? TryTake(byte[] continuationPoint, HistoryContinuationBinding binding) =>
|
||||
store.TryTake(continuationPoint, new PeerHistoryContinuationKey(
|
||||
principal, currentServingIncarnation(), binding.Epoch, binding.Realm, binding.Identifier, binding.Tag,
|
||||
store.TryTake(continuationPoint, () => new PeerHistoryContinuationKey(
|
||||
principal, _incarnation.Value, binding.Epoch, binding.Realm, binding.Identifier, binding.Tag,
|
||||
binding.Details?.ComputeDetailsHash() ?? string.Empty));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user