From bd973f2ea7ca42b2f63dd4ec6d6c2a7ed142610a Mon Sep 17 00:00:00 2001 From: Joseph Doherty Date: Thu, 3 Dec 2026 14:56:16 -0500 Subject: [PATCH] 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. --- docs/Historian.md | 7 +++ .../QuestDbWalVisibility.cs | 62 +++++++++++++++++++ .../QuestDbWalVisibilityTests.cs | 44 +++++++++++++ 3 files changed, 113 insertions(+) diff --git a/docs/Historian.md b/docs/Historian.md index 9f3c2c702..1f70a7d16 100644 --- a/docs/Historian.md +++ b/docs/Historian.md @@ -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 diff --git a/src/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Historian.QuestDb/QuestDbWalVisibility.cs b/src/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Historian.QuestDb/QuestDbWalVisibility.cs index a6712a178..d416d737f 100644 --- a/src/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Historian.QuestDb/QuestDbWalVisibility.cs +++ b/src/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Historian.QuestDb/QuestDbWalVisibility.cs @@ -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 ) so a lagging WAL apply is not hammered with ~100 queries/s per /// waiter — until it reaches the target, the table is suspended, or /// elapses. +/// +/// 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 (tags table, reason = timeout|suspended) and +/// the gauge (1 while a failure happened within +/// , 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. +/// /// internal sealed class QuestDbWalVisibility { + /// Counter of waits that gave up (table, reason = timeout|suspended). + internal const string FailuresCounterName = "otopcua.historian.questdb.wal_visibility.failures"; + + /// Gauge: 1 while a wait gave up within , else 0 (process-wide). + internal const string RecentFailureGaugeName = "otopcua.historian.questdb.wal_visibility.recent_failure"; + + /// How long after a failed wait the gauge keeps reading 1. + internal static readonly TimeSpan RecentFailureWindow = TimeSpan.FromMinutes(5); + + private static readonly Counter FailuresCounter = OtOpcUaTelemetry.Meter.CreateCounter( + 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."); + + /// The gauge value at . + /// The evaluation time. + /// 1 when a wait failed within , else 0. + internal static int RecentFailureGauge(DateTime nowUtc) + { + var ticks = Volatile.Read(ref _lastFailureTicks); + return ticks != 0 && nowUtc.Ticks - ticks <= RecentFailureWindow.Ticks ? 1 : 0; + } + + /// Test-only: clears the recent-failure memory. + internal static void ResetRecentFailure() => Volatile.Write(ref _lastFailureTicks, 0); + + /// Records one failed wait on the counter + the recent-failure gauge. + /// The table waited on. + /// timeout or suspended. + internal static void RecordFailure(string table, string reason) + { + Volatile.Write(ref _lastFailureTicks, DateTime.UtcNow.Ticks); + FailuresCounter.Add(1, new KeyValuePair("table", table), + new KeyValuePair("reason", reason)); + } + + private int _failures; + + /// Waits that gave up so far (lifetime, this instance) — test/diagnostic. + internal int Failures => Volatile.Read(ref _failures); + /// The first poll delay. 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; diff --git a/tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Historian.QuestDb.Tests/QuestDbWalVisibilityTests.cs b/tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Historian.QuestDb.Tests/QuestDbWalVisibilityTests.cs index 0ed543c09..0f1dfa8f8 100644 --- a/tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Historian.QuestDb.Tests/QuestDbWalVisibilityTests.cs +++ b/tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Historian.QuestDb.Tests/QuestDbWalVisibilityTests.cs @@ -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((_, 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(); + } }