fix(grpc-data): H1 history logs keep {NodeId} a NodeId on the local path (review 5)

This commit is contained in:
Joseph Doherty
2026-11-23 19:51:06 -05:00
parent 19a7c7af01
commit 111c9c3566
2 changed files with 64 additions and 8 deletions
@@ -10031,7 +10031,7 @@ public sealed partial class OtOpcUaNodeManager : CustomNodeManager2, INodeManage
{
// The backend cannot serve this Part 13 aggregate — the spec status for THIS node,
// not a logged server fault.
_logger.LogDebug(ex, "OtOpcUaNodeManager: backend does not support aggregate for node {NodeId}", handle);
_logger.LogDebug(ex, "OtOpcUaNodeManager: backend does not support aggregate for node {NodeId}", handle.LogNodeId);
outcome.Error = StatusCodes.BadAggregateNotSupported;
outcome.Result = new SdkHistoryReadResult { StatusCode = StatusCodes.BadAggregateNotSupported };
}
@@ -10039,7 +10039,7 @@ public sealed partial class OtOpcUaNodeManager : CustomNodeManager2, INodeManage
{
// The read would exceed a backend size limit (the time-weighted raw-row cap) — refuse THIS node
// explicitly instead of a generic fault or a deadline-elapsed BadTimeout.
_logger.LogWarning("OtOpcUaNodeManager: HistoryRead limit exceeded for node {NodeId}: {Message}", handle, ex.Message);
_logger.LogWarning("OtOpcUaNodeManager: HistoryRead limit exceeded for node {NodeId}: {Message}", handle.LogNodeId, ex.Message);
outcome.Error = StatusCodes.BadTooManyOperations;
outcome.Result = new SdkHistoryReadResult { StatusCode = StatusCodes.BadTooManyOperations };
}
@@ -10048,7 +10048,7 @@ public sealed partial class OtOpcUaNodeManager : CustomNodeManager2, INodeManage
// One node's backend failure (throw / timeout) must not throw out of the batch — surface a
// Bad status for THIS node only. Logged through the injected ILogger
// rather than swallowed silently.
_logger.LogError(ex, "OtOpcUaNodeManager: HistoryRead failed for node {NodeId}", handle);
_logger.LogError(ex, "OtOpcUaNodeManager: HistoryRead failed for node {NodeId}", handle.LogNodeId);
outcome.Error = HistoryReadFailureStatus(ex);
outcome.Result = new SdkHistoryReadResult { StatusCode = HistoryReadFailureStatus(ex) };
}
@@ -10540,14 +10540,14 @@ public sealed partial class OtOpcUaNodeManager : CustomNodeManager2, INodeManage
{
// A request limit (a reverse read walked back from forward reads met a tie cluster over the row
// budget) — BadTooManyOperations for THIS node, as on the processed path; not a backend fault.
_logger.LogWarning("OtOpcUaNodeManager: HistoryReadRaw limit exceeded for node {NodeId}: {Message}", handle, ex.Message);
_logger.LogWarning("OtOpcUaNodeManager: HistoryReadRaw limit exceeded for node {NodeId}: {Message}", handle.LogNodeId, ex.Message);
outcome.Error = StatusCodes.BadTooManyOperations;
outcome.Result = new SdkHistoryReadResult { StatusCode = StatusCodes.BadTooManyOperations };
}
catch (Exception ex)
{
// One node's backend failure must not throw out of the batch — Bad for THIS node only.
_logger.LogError(ex, "OtOpcUaNodeManager: HistoryReadRaw (paged) failed for node {NodeId}", handle);
_logger.LogError(ex, "OtOpcUaNodeManager: HistoryReadRaw (paged) failed for node {NodeId}", handle.LogNodeId);
outcome.Error = HistoryReadFailureStatus(ex);
outcome.Result = new SdkHistoryReadResult { StatusCode = HistoryReadFailureStatus(ex) };
}
@@ -10648,13 +10648,13 @@ public sealed partial class OtOpcUaNodeManager : CustomNodeManager2, INodeManage
catch (NotSupportedException ex)
{
// The backend keeps no modification trail (Gateway / Null) — the spec status, not a server fault.
_logger.LogDebug(ex, "OtOpcUaNodeManager: backend serves no modified-value history for node {NodeId}", handle);
_logger.LogDebug(ex, "OtOpcUaNodeManager: backend serves no modified-value history for node {NodeId}", handle.LogNodeId);
outcome.Error = StatusCodes.BadHistoryOperationUnsupported;
outcome.Result = new SdkHistoryReadResult { StatusCode = StatusCodes.BadHistoryOperationUnsupported };
}
catch (Exception ex)
{
_logger.LogError(ex, "OtOpcUaNodeManager: HistoryRead (modified) failed for node {NodeId}", handle);
_logger.LogError(ex, "OtOpcUaNodeManager: HistoryRead (modified) failed for node {NodeId}", handle.LogNodeId);
outcome.Error = HistoryReadFailureStatus(ex);
outcome.Result = new SdkHistoryReadResult { StatusCode = HistoryReadFailureStatus(ex) };
}
@@ -19,6 +19,9 @@ public sealed class HistoryReadPortServerFixture : IAsyncLifetime
/// <summary>The node manager under test.</summary>
public OtOpcUaNodeManager Nm { get; private set; } = null!;
/// <summary>Every node-manager log entry's structured properties.</summary>
public CapturingLogger Log { get; } = new();
/// <summary>Serialises the cases (they share the node manager's source and continuation store).</summary>
public SemaphoreSlim Gate { get; } = new(1, 1);
@@ -38,7 +41,7 @@ public sealed class HistoryReadPortServerFixture : IAsyncLifetime
PkiStoreRoot = _pkiRoot,
},
NullLogger<OpcUaApplicationHost>.Instance),
() => new OtOpcUaSdkServer(), CancellationToken.None);
() => new OtOpcUaSdkServer(Log), CancellationToken.None);
_host = started.Host;
Nm = started.Server.NodeManager!;
Nm.HistoryContinuationStore = new InMemoryHistoryContinuationStore();
@@ -71,6 +74,24 @@ public sealed class HistoryReadPortServerFixture : IAsyncLifetime
}
}
/// <summary>Records each log entry's level and structured properties.</summary>
public sealed class CapturingLogger : Microsoft.Extensions.Logging.ILogger<OtOpcUaNodeManager>
{
private readonly System.Collections.Concurrent.ConcurrentQueue<(Microsoft.Extensions.Logging.LogLevel Level, IReadOnlyList<KeyValuePair<string, object?>> Properties)> _entries = new();
public IReadOnlyList<(Microsoft.Extensions.Logging.LogLevel Level, IReadOnlyList<KeyValuePair<string, object?>> Properties)> Entries => _entries.ToArray();
public void Clear() => _entries.Clear();
public IDisposable? BeginScope<TState>(TState state) where TState : notnull => null;
public bool IsEnabled(Microsoft.Extensions.Logging.LogLevel logLevel) => true;
public void Log<TState>(Microsoft.Extensions.Logging.LogLevel logLevel, Microsoft.Extensions.Logging.EventId eventId, TState state,
Exception? exception, Func<TState, Exception?, string> formatter) =>
_entries.Enqueue((logLevel, state as IReadOnlyList<KeyValuePair<string, object?>> ?? []));
}
/// <summary>
/// H1 (gRPC data history plan §3.1): the override-level history port is the ONE pipeline the SDK overrides and a remote
/// peer's HistoryRead (H2) are served through. Every case runs the same request two ways against the same fake backend —
@@ -442,6 +463,38 @@ public sealed class HistoryReadPortParityTests(HistoryReadPortServerFixture fx)
});
}
[Fact]
public async Task A_backend_failure_logs_the_original_NodeId_on_the_local_path()
{
await RunAsync(async () =>
{
fx.Nm.HistorianDataSource = new ParitySource { Throw = true };
var nodeId = fx.Nodes["dbl"].NodeId;
static object? NodeIdProperty(CapturingLogger log) => log.Entries
.Where(e => e.Level == Microsoft.Extensions.Logging.LogLevel.Error)
.SelectMany(e => e.Properties).Single(p => p.Key == "NodeId").Value;
foreach (var details in new HistoryReadDetails[]
{
Raw(T(0), T(10), 5, bounds: false),
new ReadAtTimeDetails { ReqTimes = new[] { (DateTimeUtc)T(0) } },
})
{
fx.Log.Clear();
Override(details, "dbl").Single().Error.ShouldBe(StatusCodes.BadHistoryOperationUnsupported.Code);
NodeIdProperty(fx.Log).ShouldBe(nodeId, "the {NodeId} property keeps meaning a NodeId");
// A peer item carries no SDK NodeId: it logs as Realm:Identifier.
fx.Log.Clear();
fx.Nm.ReadHistory(HistoryReadVariant.From(details), [new HistoryReadPortItem(AddressSpaceRealm.Uns, "port/dbl")],
fx.Nm.SessionHistoryContinuationScope(session: null), CancellationToken.None);
NodeIdProperty(fx.Log).ShouldBe("Uns:port/dbl");
}
await Task.CompletedTask;
});
}
[Fact]
public async Task The_outcome_status_carries_GoodNoData_and_Bad_operation_statuses_verbatim()
{
@@ -604,6 +657,7 @@ public sealed class HistoryReadPortParityTests(HistoryReadPortServerFixture fx)
public IReadOnlyList<DataValueSnapshot> AtTimeResult { get; set; } = [];
public bool NativeReverse { get; init; } = true;
public int Ceiling { get; init; }
public bool Throw { get; init; }
public int ProcessedCalls;
public int ReverseOnForwardOnly;
public bool RefusedAggregateAsked { get; private set; }
@@ -628,6 +682,7 @@ public sealed class HistoryReadPortParityTests(HistoryReadPortServerFixture fx)
string fullReference, DateTime startUtc, DateTime endUtc, uint maxValuesPerNode, CancellationToken cancellationToken)
{
static DateTime Ts(DataValueSnapshot s) => s.SourceTimestampUtc ?? DateTime.MinValue;
if (Throw) throw new InvalidOperationException("backend down");
var series = Series.TryGetValue(fullReference, out var s) ? s : [];
List<DataValueSnapshot> window;
if (startUtc > endUtc)
@@ -665,6 +720,7 @@ public sealed class HistoryReadPortParityTests(HistoryReadPortServerFixture fx)
string fullReference, IReadOnlyList<DateTime> timestampsUtc, TypeCode? valueType, CancellationToken cancellationToken)
{
LastAtTimeType = valueType;
if (Throw) throw new InvalidOperationException("backend down");
return Task.FromResult(new HistorianRead(AtTimeResult, null));
}