7c5cc9c7e5
Every IHistorianValueWriter caught Exception and returned false, including
OperationCanceledException from the drain's own token. The recorder's guard
("a cancelled pass is not a tag failure") was therefore dead code: at shutdown
mid-pass each cancelled group could be booked as a consecutive failure and,
with a sibling that already wrote, tip a healthy tag toward quarantine.
SqlServer, QuestDb and Gateway writers now rethrow a cancellation of the
caller's token; a cancellation-shaped error on a live token is still an
ordinary failed write.
The Gateway writer relabelled a Local-kind timestamp as UTC instead of
converting it, so the same value could land at two different times on two
backends. Stamped samples now go through LiveWritePreparer (Local -> UTC,
non-finite doubles not sent, duplicate timestamps keep the first); unstamped
samples are still left for the gateway to stamp, in batch order.
Rebased from the unpushed remed/lane-H2 branch (originally authored 2026-10-22
in session_01Ub9hGmYLQvn9XETzw7CMsT) onto current master.
Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
176 lines
9.1 KiB
C#
176 lines
9.1 KiB
C#
using System.Collections.Concurrent;
|
|
using System.Text;
|
|
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using Npgsql;
|
|
using NpgsqlTypes;
|
|
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
|
|
using ZB.MOM.WW.OtOpcUa.Driver.Historian.Sql.Common;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Driver.Historian.QuestDb;
|
|
|
|
/// <summary>
|
|
/// <see cref="IHistorianValueWriter"/> over the QuestDB sample table. Redelivery-idempotent with first-wins
|
|
/// semantics: QuestDB's <c>DEDUP UPSERT KEYS(ts, tag)</c> would let a re-sent sample overwrite the stored one (and a
|
|
/// re-sent live sample resurrect a tombstone), so the writer reads which timestamps already exist for the tag in
|
|
/// the batch's range (tombstones count as present, T0 Q11 range form) and inserts only the absent rows.
|
|
/// Returns false on any failure so the recorder keeps its entries — except a cancellation of the caller's token, which
|
|
/// is rethrown (it ends the pass; it is not this tag's failure).
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Batch normalisation is <see cref="LiveWritePreparer.Prepare"/> at µs resolution (non-finite doubles become Empty
|
|
/// + Bad, an in-batch µs collision keeps the first). <b>RC1:</b> a sample whose time is before 1970 cannot be a
|
|
/// QuestDB designated timestamp and one such row would fail the whole multi-row INSERT, so those samples are dropped
|
|
/// (Warning at most once per tag per <see cref="Pre1970WarningInterval"/>; every one counted in <see cref="DroppedPre1970"/>) and the call still succeeds for the rest.
|
|
/// <b>R5:</b> after inserting, the writer waits for WAL visibility so an immediate redelivery sees the rows.
|
|
/// Inserts are chunked at <see cref="BatchRows"/> rows (8 bound parameters per row, far under the 32,767 limit).
|
|
/// </remarks>
|
|
internal sealed class QuestDbHistorianValueWriter : IHistorianValueWriter
|
|
{
|
|
/// <summary>Rows per multi-row INSERT (T0 Q2: 1,000 rows is reliable).</summary>
|
|
internal const int BatchRows = 1000;
|
|
|
|
/// <summary>
|
|
/// Minimum time between two pre-1970 drop Warnings for the same tag. A device with an unset clock sends such
|
|
/// times on every drain pass; <see cref="DroppedPre1970"/> still counts every dropped sample.
|
|
/// </summary>
|
|
internal static readonly TimeSpan Pre1970WarningInterval = TimeSpan.FromMinutes(10);
|
|
|
|
private readonly QuestDbConnection _conn;
|
|
private readonly QuestDbTagCatalog _catalog;
|
|
private readonly QuestDbWalVisibility _wal;
|
|
private readonly ILogger _log;
|
|
private readonly Func<DateTime> _utcNow;
|
|
private long _droppedPre1970;
|
|
private readonly ConcurrentDictionary<string, DateTime> _pre1970WarnedUtc = new(StringComparer.Ordinal);
|
|
|
|
/// <summary>Creates the writer.</summary>
|
|
/// <param name="conn">Connection helper.</param>
|
|
/// <param name="catalog">Tag catalog (the only tag authority).</param>
|
|
/// <param name="wal">WAL visibility wait.</param>
|
|
/// <param name="log">Optional logger.</param>
|
|
/// <param name="utcNow">Clock for null timestamps and <c>server_ts</c> (tests).</param>
|
|
public QuestDbHistorianValueWriter(QuestDbConnection conn, QuestDbTagCatalog catalog, QuestDbWalVisibility wal,
|
|
ILogger? log = null, Func<DateTime>? utcNow = null)
|
|
{
|
|
_conn = conn;
|
|
_catalog = catalog;
|
|
_wal = wal;
|
|
_log = log ?? NullLogger.Instance;
|
|
_utcNow = utcNow ?? (() => DateTime.UtcNow);
|
|
}
|
|
|
|
/// <summary>Samples dropped so far because their time is before 1970 (RC1).</summary>
|
|
internal long DroppedPre1970 => Interlocked.Read(ref _droppedPre1970);
|
|
|
|
/// <inheritdoc/>
|
|
public async Task<bool> WriteLiveValuesAsync(string tag, IReadOnlyList<HistorizationValue> values, CancellationToken ct)
|
|
{
|
|
try
|
|
{
|
|
if (!HistorianTagNames.IsValid(tag))
|
|
{
|
|
_log.LogWarning("QuestDB historian live-value write refused: tag name is missing or longer than {Max} characters.",
|
|
HistorianTagNames.MaxLength);
|
|
return false;
|
|
}
|
|
if (values is null || values.Count == 0) return true;
|
|
|
|
var prepared = LiveWritePreparer.Prepare(values, _utcNow, resolutionTicks: 10);
|
|
var rows = prepared.Where(r => QuestDbConnection.IsStorable(r.TimeUtc)).ToList();
|
|
var dropped = prepared.Count - rows.Count;
|
|
if (dropped > 0)
|
|
{
|
|
Interlocked.Add(ref _droppedPre1970, dropped);
|
|
if (ShouldWarnPre1970(tag))
|
|
_log.LogWarning("QuestDB historian dropped {Count} sample(s) for a tag because their time is before 1970-01-01 (not storable); "
|
|
+ "further drops for this tag are not logged for {Minutes} minutes.", dropped, Pre1970WarningInterval.TotalMinutes);
|
|
}
|
|
if (rows.Count == 0) return true;
|
|
|
|
var dataType = HistorianDataTypes.FromKind(rows.Select(r => r.Value.Kind).FirstOrDefault(k => k != HistorianValueKind.Empty));
|
|
await _catalog.EnsureAsync(tag, dataType, ct).ConfigureAwait(false);
|
|
|
|
var present = await ExistingTicksAsync(tag, rows, ct).ConfigureAwait(false);
|
|
var fresh = rows.Where(r => !present.Contains(QuestDbConnection.Round(r.TimeUtc).Ticks)).ToList();
|
|
if (fresh.Count == 0) return true;
|
|
|
|
var serverTs = _utcNow();
|
|
for (var i = 0; i < fresh.Count; i += BatchRows)
|
|
await InsertAsync(tag, fresh.GetRange(i, Math.Min(BatchRows, fresh.Count - i)), serverTs, ct).ConfigureAwait(false);
|
|
|
|
await _wal.WaitAsync(_conn.SampleTable, ct).ConfigureAwait(false); // timeout/suspension does not change the result (R5)
|
|
return true;
|
|
}
|
|
catch (OperationCanceledException) when (ct.IsCancellationRequested)
|
|
{
|
|
throw; // the drain pass was cancelled (shutdown) — not a write failure of this tag
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_log.LogWarning("QuestDB historian live-value write failed ({Exception}); will retry.", ex.GetType().Name);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// Once per tag per Pre1970WarningInterval, measured on the writer's clock.
|
|
private bool ShouldWarnPre1970(string tag)
|
|
{
|
|
var now = _utcNow();
|
|
var warn = false;
|
|
_pre1970WarnedUtc.AddOrUpdate(tag,
|
|
_ => { warn = true; return now; },
|
|
(_, last) =>
|
|
{
|
|
if (now - last < Pre1970WarningInterval) { warn = false; return last; }
|
|
warn = true;
|
|
return now;
|
|
});
|
|
return warn;
|
|
}
|
|
|
|
// T0 Q11 range form, one query per cluster: tombstones are NOT filtered out (a deleted row must not be resurrected).
|
|
private async Task<HashSet<long>> ExistingTicksAsync(string tag, List<HistorianSampleRow> rows, CancellationToken ct)
|
|
{
|
|
var times = rows.Select(r => QuestDbConnection.Round(r.TimeUtc)).OrderBy(t => t).ToList();
|
|
var set = new HashSet<long>();
|
|
await using var conn = await _conn.OpenAsync(ct).ConfigureAwait(false);
|
|
foreach (var (lo, hi) in QuestDbTimeClusters.Cluster(times, QuestDbTimeClusters.ExistenceGap, BatchRows))
|
|
{
|
|
await using var cmd = _conn.CreateCommand(conn, $"SELECT ts FROM {_conn.SampleTable} WHERE tag = $1 AND ts >= $2 AND ts <= $3");
|
|
QuestDbConnection.BindText(cmd, tag);
|
|
QuestDbConnection.BindTimestamp(cmd, lo);
|
|
QuestDbConnection.BindTimestamp(cmd, hi);
|
|
await using var r = await cmd.ExecuteReaderAsync(ct).ConfigureAwait(false);
|
|
while (await r.ReadAsync(ct).ConfigureAwait(false)) set.Add(QuestDbConnection.ReadUtc(r, 0).Ticks);
|
|
}
|
|
return set;
|
|
}
|
|
|
|
private async Task InsertAsync(string tag, List<HistorianSampleRow> chunk, DateTime serverTs, CancellationToken ct)
|
|
{
|
|
var sql = new StringBuilder($"INSERT INTO {_conn.SampleTable} (ts, tag, kind, num_value, int_value, str_value, status, deleted, server_ts) VALUES ");
|
|
await using var conn = await _conn.OpenAsync(ct).ConfigureAwait(false);
|
|
await using var cmd = _conn.CreateCommand(conn, "");
|
|
var n = 1;
|
|
for (var i = 0; i < chunk.Count; i++)
|
|
{
|
|
if (i > 0) sql.Append(',');
|
|
sql.Append($"(${n},${n + 1},${n + 2},${n + 3},${n + 4},${n + 5},${n + 6},false,${n + 7})");
|
|
n += 8;
|
|
var row = chunk[i];
|
|
var c = SampleColumnMapper.ToColumns(row.Value);
|
|
QuestDbConnection.BindTimestamp(cmd, row.TimeUtc);
|
|
QuestDbConnection.BindText(cmd, tag);
|
|
QuestDbConnection.BindByte(cmd, c.ValueKind);
|
|
cmd.Parameters.Add(new NpgsqlParameter { NpgsqlDbType = NpgsqlDbType.Double, Value = c.NumValue is { } d ? d : DBNull.Value });
|
|
cmd.Parameters.Add(new NpgsqlParameter { NpgsqlDbType = NpgsqlDbType.Bigint, Value = c.IntValue is { } l ? l : DBNull.Value });
|
|
QuestDbConnection.BindText(cmd, c.StrValue);
|
|
QuestDbConnection.BindLong(cmd, SampleColumnMapper.ToLongStatus(row.StatusCode));
|
|
QuestDbConnection.BindTimestamp(cmd, serverTs);
|
|
}
|
|
cmd.CommandText = sql.ToString();
|
|
await cmd.ExecuteNonQueryAsync(ct).ConfigureAwait(false);
|
|
}
|
|
}
|