perf(grpc-data): H2 review 6 - Implemented(target) once per node; history serving cached per source

Metadata no longer rebuilds the history view per node x operation (and checks the ACL before the tagname, the
ingress order); OriginHistoryPort caches its served-aggregate set per historian source instance.
This commit is contained in:
Joseph Doherty
2026-11-23 22:06:02 -05:00
parent 3c5cbea931
commit 6f1faaeb0b
3 changed files with 33 additions and 8 deletions
@@ -88,15 +88,24 @@ public sealed class OriginHistoryPort(OtOpcUaNodeManager nodeManager) : IOriginH
{
private readonly OtOpcUaNodeManager _nm = nodeManager ?? throw new ArgumentNullException(nameof(nodeManager));
private (IHistorianDataSource Source, OriginHistoryServing? Serving)? _serving;
/// <inheritdoc />
/// <remarks>Cached per historian source instance: the catalog asks per node, so this is a reference compare, not a rebuild.</remarks>
public OriginHistoryServing? Serving
{
get
{
var source = _nm.HistorianDataSource;
if (source is NullHistorianDataSource) return null;
var served = AdvertisedAggregateFunctions.ServedBy(source).Select(x => x.ToString()).Order(StringComparer.Ordinal).ToArray();
return new OriginHistoryServing(served, Math.Max(1, source.TimestampResolutionTicks));
if (_serving is { } cached && ReferenceEquals(cached.Source, source)) return cached.Serving;
OriginHistoryServing? serving = null;
if (source is not NullHistorianDataSource)
{
var served = AdvertisedAggregateFunctions.ServedBy(source).Select(x => x.ToString()).Order(StringComparer.Ordinal).ToArray();
serving = new OriginHistoryServing(served, Math.Max(1, source.TimestampResolutionTicks));
}
_serving = (source, serving);
return serving;
}
}
@@ -207,17 +207,18 @@ public sealed partial class LocalDataServices : IDataConnectionFactory
private DataMetadata Metadata(LocalExportCatalog catalog, DataAuthenticatedContext context, DataTarget target, LocalDataNode node)
{
var operations = DataOperations.None;
var implemented = Implemented(target); // once per node (H2 review: no per-operation rebuild of the history view)
foreach (var operation in new[] { DataOperations.Browse, DataOperations.Metadata, DataOperations.Read, DataOperations.Write,
DataOperations.Subscribe, DataOperations.HistoryRead })
if ((node.Supported || operation == DataOperations.Browse)
&& (operation != DataOperations.HistoryRead || IsHistorized(node))
&& (node.SourceEnabled || operation is DataOperations.Browse or DataOperations.Metadata)
&& (Implemented(target) & operation) != 0 && (node.Metadata.AllowedOperations & operation) != 0
&& (implemented & operation) != 0 && (node.Metadata.AllowedOperations & operation) != 0
&& (operation != DataOperations.Write || node.Metadata.Writable
&& string.Equals(_capabilities.Serving.Role, "Primary", StringComparison.OrdinalIgnoreCase))
&& Allowed(catalog, context, node, operation)) operations |= operation;
&& Allowed(catalog, context, node, operation)
&& (operation != DataOperations.HistoryRead || IsHistorized(node))) operations |= operation;
// H2: historizing = this caller may HistoryRead it (historized AND granted), so it never separates denied from unhistorized.
var history = node.Source is not null && (Implemented(target) & DataOperations.HistoryRead) != 0
var history = node.Source is not null && (implemented & DataOperations.HistoryRead) != 0
? new DataHistoryInfo(operations.HasFlag(DataOperations.HistoryRead)) : null;
return node.Metadata with { AllowedOperations = operations, Writable = node.Metadata.Writable && operations.HasFlag(DataOperations.Write),
Serving = catalog.Serving, History = history };
@@ -284,6 +284,17 @@ public sealed class GrpcDataHistoryWireTests
denied.StatusCode.ShouldBe(StatusCode.PermissionDenied);
}
[Fact]
public async Task A_v1_metadata_call_does_not_rebuild_the_history_view_per_node_and_operation()
{
await using var origin = new RealOrigin();
var before = origin.History!.ServingReads;
var lookups = origin.History.HistorizedLookups;
await origin.DriveAsync("GetMetadata", new Metadata()); // 11 views, header-less
(origin.History.ServingReads - before).ShouldBeLessThanOrEqualTo(16, "at most about one per node, not one per node and operation");
(origin.History.HistorizedLookups - lookups).ShouldBeLessThanOrEqualTo(11);
}
[Fact]
public async Task Origin_memory_is_bounded_per_request_and_per_principal_and_nothing_is_lost()
{
@@ -381,7 +392,11 @@ internal sealed class FakeHistoryPort : IOriginHistoryPort
private readonly TaskCompletionSource _entered = new(TaskCreationOptions.RunContinuationsAsynchronously);
private readonly ConcurrentDictionary<string, bool> _issued = new();
public OriginHistoryServing? Serving { get; set; } = new(["Average", "Count"], 10);
private OriginHistoryServing? _serving = new(["Average", "Count"], 10);
private int _servingReads;
public OriginHistoryServing? Serving { get { Interlocked.Increment(ref _servingReads); return _serving; } set => _serving = value; }
/// <summary>How often the origin asked what it can serve.</summary>
public int ServingReads => Volatile.Read(ref _servingReads);
public IReadOnlyList<OriginHistorySample> Samples { get; set; } = [];
public byte[]? Continuation { get; set; }
public bool WiringFault { get; set; }