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
+7
View File
@@ -2380,6 +2380,13 @@ catalog/provisioner and the alarm writer (`QuestDbWalVisibility`): after the INS
reported `suspended` (Warning, stop), or `VisibilityTimeout` elapses (Warning). A timeout never changes a result code and never discards data;
the rows simply become visible shortly after. Measured: median 0 ms, p95 6 ms.
**A wait that gives up is a metric, not only a log line (#1046).** Besides the Warning, every timeout / suspended-table give-up increments the counter
`otopcua.historian.questdb.wal_visibility.failures{table, reason=timeout|suspended}` and holds the gauge
`otopcua.historian.questdb.wal_visibility.recent_failure` at 1 for the following 5 minutes (alert on it). A sustained non-zero rate means
read-your-writes is not holding — QuestDB's WAL apply is lagging (an overloaded host) or a table is suspended — so a HistoryRead or an
immediate redelivery can see fewer rows than were written; the writes themselves are committed. This is also how the shared test fixture's
"read back 0 rows" symptom (#1008, host load average ~50 on 22 cores) is diagnosed: it is the designed timeout, not a writer/reader defect.
**The live writer waits once per drain pass, not once per tag group.** The recorder writes every group of a pass through the writer's
`IHistorianPassWriter` path and then calls `FlushAsync` once — after every group has reported, before the ack, whenever any group wrote —
so the pass's rows are visible to the next pass's insert-if-absent check. With `DrainConcurrency = 8` the per-group wait used to run up to
@@ -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;
@@ -1,4 +1,6 @@
using System.Diagnostics.Metrics;
using Shouldly;
using ZB.MOM.WW.OtOpcUa.Commons.Observability;
using Xunit;
namespace ZB.MOM.WW.OtOpcUa.Driver.Historian.QuestDb.Tests;
@@ -38,4 +40,46 @@ public sealed class QuestDbWalVisibilityTests
while (delays.Count < 8) delays.Add(QuestDbWalVisibility.NextPollInterval(delays[^1]));
delays.Select(d => (int)d.TotalMilliseconds).ShouldBe([10, 20, 40, 80, 160, 200, 200, 200]);
}
[Fact]
public void A_failed_wait_is_a_metric_with_table_and_reason_not_only_a_log_line()
{
var table = "t" + Guid.NewGuid().ToString("N")[..12] + "_sample"; // unique: the meter is process-wide and tests run in parallel
var seen = new List<(string Reason, long Value)>();
using var listener = new MeterListener();
listener.InstrumentPublished = (inst, l) =>
{
if (inst.Meter.Name == OtOpcUaTelemetry.MeterName && inst.Name == QuestDbWalVisibility.FailuresCounterName)
l.EnableMeasurementEvents(inst);
};
listener.SetMeasurementEventCallback<long>((_, value, tags, _) =>
{
string? t = null, reason = null;
foreach (var tag in tags)
{
if (tag.Key == "table") t = (string?)tag.Value;
if (tag.Key == "reason") reason = (string?)tag.Value;
}
if (t == table) lock (seen) seen.Add((reason!, value));
});
listener.Start();
QuestDbWalVisibility.RecordFailure(table, "timeout");
QuestDbWalVisibility.RecordFailure(table, "suspended");
lock (seen) seen.ShouldBe([("timeout", 1L), ("suspended", 1L)]);
}
[Fact]
public void The_recent_failure_gauge_reads_1_for_five_minutes_after_a_failed_wait_then_0()
{
QuestDbWalVisibility.ResetRecentFailure();
QuestDbWalVisibility.RecentFailureGauge(DateTime.UtcNow).ShouldBe(0, "no failure yet");
QuestDbWalVisibility.RecordFailure("t_x_sample", "timeout");
QuestDbWalVisibility.RecentFailureGauge(DateTime.UtcNow).ShouldBe(1);
QuestDbWalVisibility.RecentFailureGauge(DateTime.UtcNow + QuestDbWalVisibility.RecentFailureWindow - TimeSpan.FromSeconds(5)).ShouldBe(1);
QuestDbWalVisibility.RecentFailureGauge(DateTime.UtcNow + QuestDbWalVisibility.RecentFailureWindow + TimeSpan.FromSeconds(5)).ShouldBe(0);
QuestDbWalVisibility.ResetRecentFailure();
}
}