56bb4855bb
M1: the time-weighted family (incl. Total, formerly a pushed-down SUM) streams raw window rows into the C# evaluator. New shared knob ServerHistorian:MaxTimeWeightedRawRows (default 1,000,000; <=0 unbounded with a Validate warning) flows through HistorianBackendRegistry into both providers. The window query is capped at cap+1 (SQL Server TOP (@maxRows), QuestDB LIMIT $4); one row past the cap throws the new Core.Abstractions HistoryReadLimitExceededException, which the node manager maps to BadTooManyOperations for that node. Not counted as a backend health failure; bound rows are not counted; per-sample aggregates are not capped. M2: reverse Interpolative is anchored at the later time (Part 13): points at End - i*interval, emitted latest first. 10:00->09:00 @1h yields 10:00. L1: Partial bit (0x4, = Opc.Ua.AggregateBits.Partial) on non-Bad results of a truncated last interval (time-weighted family; per-sample family is a follow-up). L2/L3: aligned with the SDK AggregateCalculator.GetTimeBasedStatusCode (decompiled from Opc.Ua.Server 1.5.378.106): TreatUncertainAsBad=false counts Uncertain time (Uncertain samples, held-into-outage, extrapolation) as Good time; data but no non-Bad time => generic Bad (0x80000000); BadNoData only when there is no data at all. Consequence: a live tag's latest interval reads Good (documented). L5: release note no longer claims Total matches the Gateway Integral exactly (AVEVA's divisor is unverified); it notes Total is now evaluated in C# and subject to the cap. Tests: unit (reverse Interpolative x3, Partial, Bad vs BadNoData incl. an open-ended outage, cap helpers, SDK bit oracles); conformance (reverse Interpolative, raw-row cap at/over/0, bounds not counted, health unaffected); node manager BadTooManyOperations; options default + warning. Each new behaviour falsified by mutation (aggregator x9, data-source cap checks on both backends against the real DBs, node-manager catch).
139 lines
6.0 KiB
C#
139 lines
6.0 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.QuestDb;
|
|
|
|
/// <summary>
|
|
/// <see cref="IHistorianBackendProvider"/> for the QuestDB historian. Supports every
|
|
/// <see cref="HistorianValueKind"/> and HistoryUpdate (soft delete). All seams share ONE connection helper, ONE
|
|
/// schema bootstrapper, ONE tag catalog (R7: the only "known tag" authority) and ONE WAL visibility wait, so the
|
|
/// schema is ensured once and known tags 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 shared pieces log through.
|
|
/// <para>The provider owns the shared connection pool and disposes it (IAsyncDisposable/IDisposable); the host
|
|
/// registers the provider through a DI factory so the container disposes it at shutdown.</para>
|
|
/// </remarks>
|
|
public sealed class QuestDbHistorianProvider : IHistorianBackendProvider, IAsyncDisposable, IDisposable
|
|
{
|
|
private static readonly FrozenSet<HistorianValueKind> Kinds =
|
|
Enum.GetValues<HistorianValueKind>().ToFrozenSet();
|
|
|
|
private readonly QuestDbHistorianOptions _options;
|
|
private readonly object _initLock = new();
|
|
private Shared? _shared;
|
|
private bool _disposed;
|
|
|
|
/// <summary>Creates the provider over the <c>ServerHistorian:QuestDb</c> options (validated elsewhere).</summary>
|
|
/// <param name="options">The <c>ServerHistorian:QuestDb</c> options.</param>
|
|
/// <param name="maxTimeWeightedRawRows">The shared <c>ServerHistorian:MaxTimeWeightedRawRows</c> cap (<= 0 = unbounded).</param>
|
|
public QuestDbHistorianProvider(QuestDbHistorianOptions options,
|
|
int maxTimeWeightedRawRows = TimeWeightedAggregator.DefaultMaxWindowRows)
|
|
{
|
|
_options = options ?? throw new ArgumentNullException(nameof(options));
|
|
_maxTimeWeightedRawRows = maxTimeWeightedRawRows;
|
|
}
|
|
|
|
private readonly int _maxTimeWeightedRawRows;
|
|
|
|
/// <inheritdoc />
|
|
public string Name => "QuestDb";
|
|
|
|
/// <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 loggers = Loggers(sp);
|
|
ObjectDisposedException.ThrowIf(_disposed, this);
|
|
var conn = new QuestDbConnection(_options, loggers.CreateLogger<QuestDbConnection>());
|
|
var bootstrap = new QuestDbSchemaBootstrapper(conn, _options, loggers.CreateLogger<QuestDbSchemaBootstrapper>());
|
|
var wal = new QuestDbWalVisibility(conn, _options.VisibilityTimeout, loggers.CreateLogger<QuestDbWalVisibility>());
|
|
var shared = new Shared(conn, bootstrap, new QuestDbTagCatalog(conn, bootstrap, wal), wal);
|
|
Volatile.Write(ref _shared, shared);
|
|
return shared;
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public IHistorianDataSource CreateDataSource(IServiceProvider sp)
|
|
{
|
|
var s = Get(sp);
|
|
return new QuestDbHistorianDataSource(s.Conn, s.Bootstrap, s.Catalog,
|
|
Loggers(sp).CreateLogger<QuestDbHistorianDataSource>(), maxTimeWeightedRawRows: _maxTimeWeightedRawRows);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public IHistorianValueWriter CreateValueWriter(IServiceProvider sp)
|
|
{
|
|
var s = Get(sp);
|
|
return new QuestDbHistorianValueWriter(s.Conn, s.Catalog, s.Wal,
|
|
Loggers(sp).CreateLogger<QuestDbHistorianValueWriter>());
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public IAlarmHistorianWriter CreateAlarmWriter(IServiceProvider sp)
|
|
{
|
|
var s = Get(sp);
|
|
return new QuestDbAlarmHistorianWriter(s.Conn, s.Bootstrap, s.Wal,
|
|
Loggers(sp).CreateLogger<QuestDbAlarmHistorianWriter>());
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public IHistorianProvisioning CreateProvisioning(IServiceProvider sp)
|
|
{
|
|
var s = Get(sp);
|
|
return new QuestDbTagProvisioner(s.Conn, s.Bootstrap, s.Catalog, s.Wal,
|
|
Loggers(sp).CreateLogger<QuestDbTagProvisioner>());
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
/// <remarks>The soft-delete updater: deletes write tombstones (INSERT only, never QuestDB UPDATE/DELETE).</remarks>
|
|
public IHistorianDataUpdater? CreateUpdater(IServiceProvider sp)
|
|
{
|
|
var s = Get(sp);
|
|
return new QuestDbHistorianDataUpdater(s.Conn, s.Catalog, s.Wal,
|
|
Loggers(sp).CreateLogger<QuestDbHistorianDataUpdater>());
|
|
}
|
|
|
|
/// <summary>Disposes the shared connection pool, when one was built. Idempotent.</summary>
|
|
/// <returns>A task completing when the pool is disposed.</returns>
|
|
public async ValueTask DisposeAsync()
|
|
{
|
|
if (TakeForDispose() is { } conn) await conn.DisposeAsync().ConfigureAwait(false);
|
|
}
|
|
|
|
/// <summary>Disposes the shared connection pool, when one was built. Idempotent.</summary>
|
|
public void Dispose() => TakeForDispose()?.Dispose();
|
|
|
|
private QuestDbConnection? TakeForDispose()
|
|
{
|
|
lock (_initLock)
|
|
{
|
|
if (_disposed) return null;
|
|
_disposed = true;
|
|
var conn = _shared?.Conn;
|
|
_shared = null;
|
|
return conn;
|
|
}
|
|
}
|
|
|
|
private sealed record Shared(
|
|
QuestDbConnection Conn, QuestDbSchemaBootstrapper Bootstrap, QuestDbTagCatalog Catalog, QuestDbWalVisibility Wal);
|
|
}
|