6669fae7e5
Review R08:1.1: an uncapped HistoryRead-Raw (NumValuesPerNode = 0) read a tag's whole window into memory on every backend (SQL TOP(int.MaxValue), QuestDB LIMIT, Gateway max_values = int.MaxValue + a second snapshot copy). New ServerHistorian:MaxRawValuesPerRead (default 10 000; <= 0 unbounded with a Validate() warning; 1 raised to 2). Each backend data source takes it, reports it as IHistorianDataSource.MaxRawValuesPerRead (the Gateway: min with a strict gateway's learned ceiling), so the node manager's existing RawPageLimit paging serves N = 0 and N > cap as pages of at most the cap with a continuation point. Independently, a backend raw read asking for 0 or more than the cap reads cap + 1 rows and is REFUSED (HistoryReadLimitExceededException => BadTooManyOperations) when the window holds more - never answered truncated, since the server's paging reads a short answer as the last page. Bound through the Host registry, the SqlServer/QuestDb providers and GatewayHistorian.CreateDataSource. Tests: options (default/binding/warnings), provider binding, Gateway unit + through a real SDK node manager (uncapped -> 999/998/998/605 + CPs), and a conformance case for the SQL backends. Refs #736 Claude-Session: https://claude.ai/code/session_01Ub9hGmYLQvn9XETzw7CMsT
120 lines
5.2 KiB
C#
120 lines
5.2 KiB
C#
using ZB.MOM.WW.OtOpcUa.Driver.Historian.Sql.Common;
|
|
using System.Collections.Frozen;
|
|
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.SqlServer;
|
|
|
|
/// <summary>
|
|
/// <see cref="IHistorianBackendProvider"/> for the SQL Server historian. Supports every
|
|
/// <see cref="HistorianValueKind"/> and HistoryUpdate. All seams share ONE connection helper, ONE schema
|
|
/// bootstrapper and ONE tag catalog, so the schema is ensured once and tag ids are cached once.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// The shared state is built on the first <c>Create*</c> call, not in the constructor: the constructor does no
|
|
/// validation and no I/O, so resolving the provider for a backend nobody consumes never fails on its options
|
|
/// (they are validated, consumer-gated, by the host's <c>ServerHistorianOptionsValidator</c>). The first
|
|
/// <c>Create*</c> call's service provider supplies the logger factory the bootstrapper logs through.
|
|
/// </remarks>
|
|
public sealed class SqlServerHistorianProvider : IHistorianBackendProvider
|
|
{
|
|
private static readonly FrozenSet<HistorianValueKind> Kinds =
|
|
Enum.GetValues<HistorianValueKind>().ToFrozenSet();
|
|
|
|
private readonly SqlServerHistorianOptions _options;
|
|
private readonly object _initLock = new();
|
|
private Shared? _shared;
|
|
|
|
/// <summary>Creates the provider over the <c>ServerHistorian:SqlServer</c> options (validated elsewhere).</summary>
|
|
/// <param name="options">The <c>ServerHistorian:SqlServer</c> options.</param>
|
|
/// <param name="maxTimeWeightedRawRows">The shared <c>ServerHistorian:MaxTimeWeightedRawRows</c> cap (<= 0 = unbounded).</param>
|
|
/// <param name="maxRawValuesPerRead">The shared <c>ServerHistorian:MaxRawValuesPerRead</c> raw cap (<= 0 = unbounded; R08:1.1).</param>
|
|
public SqlServerHistorianProvider(SqlServerHistorianOptions options,
|
|
int maxTimeWeightedRawRows = TimeWeightedAggregator.DefaultMaxWindowRows, int maxRawValuesPerRead = 0)
|
|
{
|
|
_options = options ?? throw new ArgumentNullException(nameof(options));
|
|
_maxTimeWeightedRawRows = maxTimeWeightedRawRows;
|
|
_maxRawValuesPerRead = maxRawValuesPerRead;
|
|
}
|
|
|
|
private readonly int _maxTimeWeightedRawRows;
|
|
private readonly int _maxRawValuesPerRead;
|
|
|
|
/// <inheritdoc />
|
|
public string Name => "SqlServer";
|
|
|
|
/// <inheritdoc />
|
|
public IReadOnlySet<HistorianValueKind> SupportedKinds => Kinds;
|
|
|
|
private static ILoggerFactory Loggers(IServiceProvider sp) =>
|
|
(sp.GetService(typeof(ILoggerFactory)) as ILoggerFactory) ?? NullLoggerFactory.Instance;
|
|
|
|
private Shared Get(IServiceProvider sp)
|
|
{
|
|
if (Volatile.Read(ref _shared) is { } built) return built;
|
|
lock (_initLock)
|
|
{
|
|
if (_shared is { } raced) return raced;
|
|
var conn = new SqlHistorianConnection(_options);
|
|
var bootstrap = new SqlServerSchemaBootstrapper(conn, _options,
|
|
Loggers(sp).CreateLogger<SqlServerSchemaBootstrapper>());
|
|
var shared = new Shared(conn, bootstrap, new SqlServerTagCatalog(conn, bootstrap));
|
|
Volatile.Write(ref _shared, shared);
|
|
return shared;
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public IHistorianDataSource CreateDataSource(IServiceProvider sp)
|
|
{
|
|
var s = Get(sp);
|
|
return new SqlServerHistorianDataSource(s.Conn, s.Bootstrap, s.Catalog,
|
|
Loggers(sp).CreateLogger<SqlServerHistorianDataSource>(), maxTimeWeightedRawRows: _maxTimeWeightedRawRows,
|
|
maxRawValuesPerRead: _maxRawValuesPerRead);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public IHistorianValueWriter CreateValueWriter(IServiceProvider sp)
|
|
{
|
|
var s = Get(sp);
|
|
return new SqlServerHistorianValueWriter(s.Conn, s.Bootstrap, s.Catalog,
|
|
Loggers(sp).CreateLogger<SqlServerHistorianValueWriter>());
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public IAlarmHistorianWriter CreateAlarmWriter(IServiceProvider sp)
|
|
{
|
|
var s = Get(sp);
|
|
return new SqlServerAlarmHistorianWriter(s.Conn, s.Bootstrap,
|
|
Loggers(sp).CreateLogger<SqlServerAlarmHistorianWriter>());
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public IHistorianProvisioning CreateProvisioning(IServiceProvider sp)
|
|
{
|
|
var s = Get(sp);
|
|
return new SqlServerTagProvisioner(s.Conn, s.Bootstrap, Loggers(sp).CreateLogger<SqlServerTagProvisioner>());
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public IHistorianDataUpdater? CreateUpdater(IServiceProvider sp)
|
|
{
|
|
var s = Get(sp);
|
|
return new SqlServerHistorianDataUpdater(s.Conn, s.Bootstrap, s.Catalog);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public IHistorianRetention? CreateRetention(IServiceProvider sp)
|
|
{
|
|
if (_options.RetentionDays is not { } days) return null;
|
|
var s = Get(sp);
|
|
return new SqlServerHistorianRetention(s.Conn, s.Bootstrap, _options.PurgeInterval, days,
|
|
_options.PurgeBatchSize, Loggers(sp).CreateLogger<SqlServerHistorianRetention>());
|
|
}
|
|
|
|
private sealed record Shared(SqlHistorianConnection Conn, SqlServerSchemaBootstrapper Bootstrap, SqlServerTagCatalog Catalog);
|
|
}
|