feat(historian): #1046 a QuestDB WAL visibility give-up is a metric (counter + recent-failure gauge), not only a log line

Also records the #1008 finding in docs: the fixture's 0-row readback was the designed visibility timeout under host load, not a writer/reader defect.
This commit is contained in:
Joseph Doherty
2026-12-03 14:56:16 -05:00
parent cc7155e5e9
commit bd973f2ea7
3 changed files with 113 additions and 0 deletions
@@ -1,6 +1,8 @@
using System.Diagnostics;
using System.Diagnostics.Metrics;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using ZB.MOM.WW.OtOpcUa.Commons.Observability;
namespace ZB.MOM.WW.OtOpcUa.Driver.Historian.QuestDb;
@@ -12,9 +14,65 @@ namespace ZB.MOM.WW.OtOpcUa.Driver.Historian.QuestDb;
/// (doubling, capped at <see cref="MaxPollInterval"/>) so a lagging WAL apply is not hammered with ~100 queries/s per
/// waiter — until it reaches the target, the table is suspended, or <see cref="QuestDbHistorianOptions.VisibilityTimeout"/>
/// elapses.
/// <para>
/// A wait that gives up (timeout or suspended table) never changes a write's outcome — the rows are committed and
/// become visible later — so it used to surface only as a Warning in the log (#1046). It is now also a metric:
/// the counter <see cref="FailuresCounterName"/> (tags <c>table</c>, <c>reason</c> = <c>timeout|suspended</c>) and
/// the gauge <see cref="RecentFailureGaugeName"/> (1 while a failure happened within
/// <see cref="RecentFailureWindow"/>, else 0) that an alert can watch. A sustained non-zero rate means the
/// read-your-writes guarantee is not holding (QuestDB WAL apply lagging or the table suspended): a following
/// HistoryRead or an immediate redelivery may see fewer rows than were written.
/// </para>
/// </summary>
internal sealed class QuestDbWalVisibility
{
/// <summary>Counter of waits that gave up (<c>table</c>, <c>reason</c> = <c>timeout|suspended</c>).</summary>
internal const string FailuresCounterName = "otopcua.historian.questdb.wal_visibility.failures";
/// <summary>Gauge: 1 while a wait gave up within <see cref="RecentFailureWindow"/>, else 0 (process-wide).</summary>
internal const string RecentFailureGaugeName = "otopcua.historian.questdb.wal_visibility.recent_failure";
/// <summary>How long after a failed wait the <see cref="RecentFailureGaugeName"/> gauge keeps reading 1.</summary>
internal static readonly TimeSpan RecentFailureWindow = TimeSpan.FromMinutes(5);
private static readonly Counter<long> FailuresCounter = OtOpcUaTelemetry.Meter.CreateCounter<long>(
FailuresCounterName, unit: "{wait}",
description: "QuestDB read-your-writes waits that gave up (reason=timeout|suspended). The writes themselves are committed.");
private static long _lastFailureTicks; // DateTime.UtcNow.Ticks of the last failed wait; 0 = never
static QuestDbWalVisibility() =>
OtOpcUaTelemetry.Meter.CreateObservableGauge(
RecentFailureGaugeName, () => RecentFailureGauge(DateTime.UtcNow), unit: "{state}",
description: "1 while a QuestDB WAL visibility wait gave up within the last 5 minutes (read-your-writes not holding), else 0.");
/// <summary>The gauge value at <paramref name="nowUtc"/>.</summary>
/// <param name="nowUtc">The evaluation time.</param>
/// <returns>1 when a wait failed within <see cref="RecentFailureWindow"/>, else 0.</returns>
internal static int RecentFailureGauge(DateTime nowUtc)
{
var ticks = Volatile.Read(ref _lastFailureTicks);
return ticks != 0 && nowUtc.Ticks - ticks <= RecentFailureWindow.Ticks ? 1 : 0;
}
/// <summary>Test-only: clears the recent-failure memory.</summary>
internal static void ResetRecentFailure() => Volatile.Write(ref _lastFailureTicks, 0);
/// <summary>Records one failed wait on the counter + the recent-failure gauge.</summary>
/// <param name="table">The table waited on.</param>
/// <param name="reason"><c>timeout</c> or <c>suspended</c>.</param>
internal static void RecordFailure(string table, string reason)
{
Volatile.Write(ref _lastFailureTicks, DateTime.UtcNow.Ticks);
FailuresCounter.Add(1, new KeyValuePair<string, object?>("table", table),
new KeyValuePair<string, object?>("reason", reason));
}
private int _failures;
/// <summary>Waits that gave up so far (lifetime, this instance) — test/diagnostic.</summary>
internal int Failures => Volatile.Read(ref _failures);
/// <summary>The first poll delay.</summary>
internal static readonly TimeSpan FirstPollInterval = TimeSpan.FromMilliseconds(10);
@@ -95,12 +153,16 @@ internal sealed class QuestDbWalVisibility
if (Reached(writer, target)) return true;
if (suspended)
{
Interlocked.Increment(ref _failures);
RecordFailure(table, "suspended");
_log.LogWarning("QuestDB table {Table} is suspended (writerTxn {Writer} < sequencerTxn {Target}); not waiting.",
table, writer, target);
return false;
}
if (sw.Elapsed >= _timeout)
{
Interlocked.Increment(ref _failures);
RecordFailure(table, "timeout");
_log.LogWarning("QuestDB table {Table} did not become visible within {Timeout} ms (writerTxn {Writer} < sequencerTxn {Target}).",
table, _timeout.TotalMilliseconds, writer, target);
return false;