using Akka.Actor; using ZB.MOM.WW.OtOpcUa.Core.Authorization; using ZB.MOM.WW.OtOpcUa.Runtime.Drivers; namespace ZB.MOM.WW.OtOpcUa.Runtime.DataConnections; /// Bounds shared physical demand and downstream subscription resources for one process. /// A2b: condition-method operations the origin remembers at once (all principals). /// A2b security review: the same, per principal. public sealed record LocalDataRuntimeLimits(int MaxDrivers, int MaxInterests, int MaxOperations, int MaxOperationsPerPrincipal, long MaxTransportBytes, int MaxCommands, int MaxPendingFrames, long MaxPendingBytes, TimeSpan MaxSessionLifetime, TimeSpan DriverAskTimeout, int MaxConditionMethodOperations = LocalDataServices.DefaultConditionMethodMaxOperations, int MaxConditionMethodOperationsPerPrincipal = LocalDataServices.DefaultConditionMethodMaxOperationsPerPrincipal); /// Owns the local caller-bound services and the one shared physical-demand bridge. public sealed class LocalDataRuntimeComposition : IDisposable { private readonly DriverHostSubscriptionDemandSource _demand; private readonly PartitionedSampleHub _hub; private int _disposed; public LocalDataRuntimeComposition(LocalCatalogCoordinator catalog, PartitionedSampleHub hub, DataOperationPolicy policy, DataCapabilitiesProvider capabilities, TimeProvider clock, DataWriteRuntime writes, IActorRef host, SubscriptionAcquisitionCapacity acquisitionCapacity, LocalDataRuntimeLimits limits, ZB.MOM.WW.OtOpcUa.OpcUaServer.IOriginHistoryPort? history = null, Microsoft.Extensions.Logging.ILogger? logger = null, DataConnectionHistoryDiagnostics? historyDiagnostics = null, ConditionStateHub? conditionHub = null, ZB.MOM.WW.OtOpcUa.OpcUaServer.IOriginConditionSource? conditionSource = null, IDataConnectionAuditLog? audit = null, DataConnectionConditionDiagnostics? conditionDiagnostics = null) { ArgumentNullException.ThrowIfNull(catalog); ArgumentNullException.ThrowIfNull(hub); ArgumentNullException.ThrowIfNull(policy); ArgumentNullException.ThrowIfNull(capabilities); ArgumentNullException.ThrowIfNull(clock); ArgumentNullException.ThrowIfNull(writes); ArgumentNullException.ThrowIfNull(host); ArgumentNullException.ThrowIfNull(acquisitionCapacity); ArgumentNullException.ThrowIfNull(limits); _hub = hub; if (!ReferenceEquals(hub.Clock, clock)) throw new ArgumentException("Local hub and services must share one clock.", nameof(clock)); var sessionLimits = new SubscriptionSessionLimits(limits.MaxCommands, limits.MaxPendingFrames, limits.MaxPendingBytes, limits.MaxSessionLifetime); sessionLimits.Validate(); var registry = new UpstreamDemandRegistry(limits.MaxInterests, limits.MaxInterests); var capacity = new SubscriptionTransportCapacity(limits.MaxOperations, limits.MaxOperationsPerPrincipal, limits.MaxTransportBytes, clock); _demand = new DriverHostSubscriptionDemandSource(host, registry, limits.MaxDrivers, limits.MaxInterests, limits.DriverAskTimeout, acquisitionCapacity); AcquisitionCapacity = acquisitionCapacity; LocalDataServices? services = null; services = new LocalDataServices(() => catalog.Current, hub, policy, capabilities, clock, writes: context => writes.ForCaller(context, () => host), targetSubscriptions: (context, target) => new LocalSubscriptionService(services!, context, target, _demand, sessionLimits, capacity), subscriptionFreshnessChallenges: true, history: history) { Logger = logger ?? Microsoft.Extensions.Logging.Abstractions.NullLogger.Instance, HistoryDiagnostics = historyDiagnostics, // A2: condition streams over the A1 hub and the node manager's snapshot port; keys from the coordinator. ConditionHub = conditionHub, ConditionSource = conditionSource, ConditionKeysSource = () => catalog.ConditionKeys, ConditionDiagnostics = conditionDiagnostics, // A2b security review: the audit trail on its own logger category (the Host passes it), never the services' logger. Audit = audit ?? new LoggerDataConnectionAuditLog(logger ?? Microsoft.Extensions.Logging.Abstractions.NullLogger.Instance), ConditionMethodMaxOperations = limits.MaxConditionMethodOperations, ConditionMethodMaxOperationsPerPrincipal = limits.MaxConditionMethodOperationsPerPrincipal, }; Services = services; } public LocalDataServices Services { get; } public SubscriptionAcquisitionCapacity AcquisitionCapacity { get; } /// Samples actual demand and cache owners; component reads are not cross-component atomic. public GrpcDataWorkloadAccounting CaptureWorkloadAccounting() => new(_hub.Clock.GetUtcNow(), _demand.CaptureAccounting(), _hub.CaptureAccounting(), AcquisitionCapacity.ReservedDriverCount, AcquisitionCapacity.MaxReservations); public void Dispose() { if (Interlocked.Exchange(ref _disposed, 1) == 0) _demand.Dispose(); } }