Merge historian follow-up (#1089): HistorianUnreachable on /alarms-historian + otopcua.alarm_historian.unreachable gauge; per-writer reachability tests; SqlServer writer no longer counts login/connection failures as answered

This commit is contained in:
Joseph Doherty
2026-12-04 14:34:06 -05:00
12 changed files with 371 additions and 6 deletions
@@ -145,10 +145,22 @@ internal sealed class QuestDbAlarmHistorianWriter : IAlarmHistorianWriter
// #1071: a server-side refusal (not a transport/transient fault) proves the historian answered.
private bool NoteAnswered(Exception ex)
{
if (!QuestDbConnection.IsTransient(ex)) _reach.Touch();
if (Answered(ex)) _reach.Touch();
return false;
}
/// <summary>
/// Whether a failed statement proves QuestDB answered (#1089): a server rejection of the statement does; a transient
/// transport fault, or a connection/authentication-level refusal (SQLSTATE class 08 / 28 / 53 / 57) does not.
/// </summary>
/// <param name="ex">The failure.</param>
/// <returns>Whether the server demonstrably answered.</returns>
internal static bool Answered(Exception ex) =>
!QuestDbConnection.IsTransient(ex)
&& !(ex is PostgresException pg && pg.SqlState is { Length: >= 2 } st
&& (st.StartsWith("08", StringComparison.Ordinal) || st.StartsWith("28", StringComparison.Ordinal)
|| st.StartsWith("53", StringComparison.Ordinal) || st.StartsWith("57", StringComparison.Ordinal)));
/// <summary>Classifies a write exception as retryable or a permanent dead-letter failure.</summary>
/// <param name="ex">The exception raised by the write.</param>
/// <returns>The write outcome the exception maps to.</returns>
@@ -151,10 +151,20 @@ internal sealed class SqlServerAlarmHistorianWriter : IAlarmHistorianWriter
// #1071: a server-side refusal (not a transport/transient fault) proves the historian answered.
private bool NoteAnswered(Exception ex)
{
if (!SqlHistorianConnection.IsTransient(ex)) _reach.Touch();
if (Answered(ex)) _reach.Touch();
return false;
}
/// <summary>
/// Whether a failed statement proves the SQL Server answered (#1089): a statement-level refusal (data error, constraint)
/// does; a transient fault, a timeout or a connection-level failure (login failed, server unreachable, pool exhausted)
/// does not. It used <c>!IsTransient</c>, which counted a login failure or a refused connection as an answer and so
/// reported a dead historian reachable.
/// </summary>
/// <param name="ex">The failure.</param>
/// <returns>Whether the server demonstrably answered.</returns>
internal static bool Answered(Exception ex) => !SqlHistorianConnection.IsOutage(ex);
/// <summary>Classifies a write failure. Only a data error SQL Server will reject forever dead-letters; anything unknown or non-SQL retries (never lose alarm history).</summary>
/// <param name="ex">The exception raised by the write.</param>
/// <returns>The write outcome the retry queue should act on.</returns>