feat(historian-gateway): alarm-write cutover — AddAlarmHistorian drains to GatewayAlarmHistorianWriter

Claude-Session: https://claude.ai/code/session_012SDSQ3AcaXqPcBtDESBRii
This commit is contained in:
Joseph Doherty
2026-06-26 17:40:23 -04:00
parent 8559905e8a
commit 0be79219fc
3 changed files with 87 additions and 15 deletions
@@ -2,16 +2,17 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
using ZB.MOM.WW.OtOpcUa.Core.AlarmHistorian;
using ZB.MOM.WW.OtOpcUa.Runtime.Historian;
namespace ZB.MOM.WW.OtOpcUa.Driver.Historian.Gateway;
/// <summary>
/// Host-callable factory that builds the gateway-backed server-side HistoryRead data source. The
/// Host's <c>AddServerHistorian</c> wiring supplies <see cref="CreateDataSource"/> as its
/// <c>Func&lt;ServerHistorianOptions, IServiceProvider, IHistorianDataSource&gt;</c>, keeping the
/// concrete package-client dependency inside this driver project (the Host references only the
/// driver, not the package client directly).
/// Host-callable factories that build the gateway-backed historian seams against the single
/// <c>ServerHistorian</c> gateway: <see cref="CreateDataSource"/> for the read path (the Host's
/// <c>AddServerHistorian</c> wiring) and <see cref="CreateAlarmWriter"/> for the alarm-write path
/// (the Host's <c>AddAlarmHistorian</c> wiring). Both keep the concrete package-client dependency
/// inside this driver project — the Host references only the driver, not the package client directly.
/// </summary>
public static class GatewayHistorian
{
@@ -39,4 +40,42 @@ public static class GatewayHistorian
HistorianGatewayClientAdapter.Create(options, loggerFactory),
logger);
}
/// <summary>
/// Builds a <see cref="GatewayAlarmHistorianWriter"/> over a lazily connected
/// <see cref="HistorianGatewayClientAdapter"/> mapped from the bound
/// <see cref="ServerHistorianOptions"/> — the <b>same single gateway</b> the read path
/// (<see cref="CreateDataSource"/>) targets. The Host's <c>AddAlarmHistorian</c> wiring supplies
/// this as the concrete <see cref="IAlarmHistorianWriter"/> the durable
/// <c>SqliteStoreAndForwardSink</c> drain worker delegates to, sourcing the connection from the
/// <c>ServerHistorian</c> section (endpoint/key/TLS) rather than the legacy Wonderware-shaped
/// <c>AlarmHistorian</c> host/port. Resolves an <see cref="ILoggerFactory"/> and the writer's
/// <see cref="ILogger{TCategoryName}"/> from <paramref name="services"/>, falling back to the null
/// implementations when absent. Performs no network I/O — the underlying channel dials on first send.
/// </summary>
/// <remarks>
/// This deliberately constructs its <b>own</b> <see cref="HistorianGatewayClientAdapter"/> — a
/// second gRPC channel to the same gateway as the read path. Collapsing the two onto one shared
/// channel would require the container to own a singleton <see cref="IHistorianGatewayClient"/> and
/// the read-side <see cref="GatewayHistorianDataSource"/> to stop owning + disposing its client,
/// regressing the read cutover's dispose ownership (and its tests). A second channel to a co-located
/// sidecar is cheap — the gateway pools and amortizes the underlying historian sessions server-side —
/// so each path keeps its own channel with a clean, independent lifetime.
/// </remarks>
/// <param name="options">The bound <c>ServerHistorian</c> configuration (endpoint, key, TLS posture).</param>
/// <param name="services">The resolving service provider (used only to locate logging services).</param>
/// <returns>The gateway-backed <see cref="IAlarmHistorianWriter"/>.</returns>
public static IAlarmHistorianWriter CreateAlarmWriter(ServerHistorianOptions options, IServiceProvider services)
{
ArgumentNullException.ThrowIfNull(options);
ArgumentNullException.ThrowIfNull(services);
var loggerFactory = services.GetService<ILoggerFactory>() ?? NullLoggerFactory.Instance;
var logger = services.GetService<ILogger<GatewayAlarmHistorianWriter>>()
?? NullLogger<GatewayAlarmHistorianWriter>.Instance;
return new GatewayAlarmHistorianWriter(
HistorianGatewayClientAdapter.Create(options, loggerFactory),
logger);
}
}