diff --git a/src/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer/OriginHistoryPort.cs b/src/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer/OriginHistoryPort.cs index bdac9f01b..1baff2b39 100644 --- a/src/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer/OriginHistoryPort.cs +++ b/src/Server/ZB.MOM.WW.OtOpcUa.OpcUaServer/OriginHistoryPort.cs @@ -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; + /// + /// Cached per historian source instance: the catalog asks per node, so this is a reference compare, not a rebuild. 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; } } diff --git a/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/DataConnections/LocalDataServices.cs b/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/DataConnections/LocalDataServices.cs index ab9943875..38cf003de 100644 --- a/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/DataConnections/LocalDataServices.cs +++ b/src/Server/ZB.MOM.WW.OtOpcUa.Runtime/DataConnections/LocalDataServices.cs @@ -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 }; diff --git a/tests/Server/ZB.MOM.WW.OtOpcUa.Host.Tests/DataConnections/GrpcDataHistoryWireTests.cs b/tests/Server/ZB.MOM.WW.OtOpcUa.Host.Tests/DataConnections/GrpcDataHistoryWireTests.cs index 3dd02e08a..4cfe48fdb 100644 --- a/tests/Server/ZB.MOM.WW.OtOpcUa.Host.Tests/DataConnections/GrpcDataHistoryWireTests.cs +++ b/tests/Server/ZB.MOM.WW.OtOpcUa.Host.Tests/DataConnections/GrpcDataHistoryWireTests.cs @@ -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 _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; } + /// How often the origin asked what it can serve. + public int ServingReads => Volatile.Read(ref _servingReads); public IReadOnlyList Samples { get; set; } = []; public byte[]? Continuation { get; set; } public bool WiringFault { get; set; }