1c7e06502f
#976 made StatusCode.Unknown (the gateway's handler threw) and non-RPC non-transport exceptions RetryPlease. On the SendEvents path a RetryPlease marked the whole chunk and every later chunk RetryPlease; with no Ack the sink treats the drain as an outage and counts nothing, so ONE such event at the queue head stalled all Gateway alarm history until the 1,000,000-row capacity evicted it silently. The SQL writers already isolate unrecognised failures (AlarmBatchWrite); the Gateway writer had no equivalent. Gateway: a RetryPlease failure that is not a known outage (IsSystemicFailure: Unavailable, DeadlineExceeded, Cancelled, ResourceExhausted, Aborted, auth codes, typed Unavailable/auth client exceptions, or a socket/IO/HTTP/disposed/timeout/cancellation cause) re-sends the chunk singly at once: siblings Ack, the culprit keeps RetryPlease and dead-letters via MaxAttempts. If no single Acks it is treated as systemic (later chunks not sent) — bounded to one chunk of singles per drain. SQL (LOW): AlarmBatchWrite stops isolating at the first single that fails with a known outage error; the SQL Server alarm writer passes the wider SqlHistorianConnection.IsOutage (login failed 18456/18452, unreachable/refused 2/40/53/64/121/10061/10065/-1, severity >= 20, pool timeout) — IsTransient is unchanged for its retry callers. Fixes the false claim in docs/AlarmHistorian.md. Tests: GatewayAlarmPoisonDrainTests (real LocalDb sink + real writer: poison at the queue head ⇒ siblings Ack on the first drain, poison dead-letters via MaxAttempts; positive control: Unavailable outage ⇒ no single sends, nothing dead-lettered), writer-level isolation + bounded fan-out tests, AlarmBatchWrite early-stop + no-stop-on-adjacent-poison control, SqlOutageClassificationTests.
171 lines
7.7 KiB
C#
171 lines
7.7 KiB
C#
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.OtOpcUa.Core.AlarmHistorian;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Driver.Historian.Sql.Common.Tests;
|
|
|
|
/// <summary>
|
|
/// The shared batched alarm write: one statement per chunk instead of one round trip per event, with the per-event
|
|
/// outcomes the store-and-forward drain needs kept intact (a poison event is isolated, an outage retries everything,
|
|
/// a cancellation never dead-letters).
|
|
/// </summary>
|
|
public sealed class AlarmBatchWriteTests
|
|
{
|
|
private sealed class PoisonException : Exception;
|
|
private sealed class OutageException : Exception;
|
|
private sealed class UnrecognisedException : Exception;
|
|
|
|
private static HistorianWriteOutcome Classify(Exception ex) =>
|
|
ex is PoisonException ? HistorianWriteOutcome.PermanentFail : HistorianWriteOutcome.RetryPlease;
|
|
|
|
private static bool IsTransient(Exception ex) => ex is OutageException;
|
|
|
|
private static HistorianWriteOutcome?[] Undecided(int n) => new HistorianWriteOutcome?[n];
|
|
|
|
[Fact]
|
|
public async Task A_clean_batch_is_one_statement_per_chunk_and_all_Ack()
|
|
{
|
|
var statements = new List<int[]>();
|
|
var r = await AlarmBatchWrite.RunAsync(Undecided(25), 10,
|
|
(ix, _) => { statements.Add(ix.ToArray()); return Task.CompletedTask; }, Classify, IsTransient, null, CancellationToken.None);
|
|
|
|
r.ShouldAllBe(o => o == HistorianWriteOutcome.Ack);
|
|
statements.Select(s => s.Length).ShouldBe([10, 10, 5]);
|
|
statements.SelectMany(s => s).ShouldBe(Enumerable.Range(0, 25));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task A_poison_event_is_isolated_and_its_siblings_still_Ack()
|
|
{
|
|
var statements = 0;
|
|
var r = await AlarmBatchWrite.RunAsync(Undecided(6), 10, (ix, _) =>
|
|
{
|
|
statements++;
|
|
if (ix.Contains(3)) throw new PoisonException();
|
|
return Task.CompletedTask;
|
|
}, Classify, IsTransient, null, CancellationToken.None);
|
|
|
|
r.ShouldBe([HistorianWriteOutcome.Ack, HistorianWriteOutcome.Ack, HistorianWriteOutcome.Ack,
|
|
HistorianWriteOutcome.PermanentFail, HistorianWriteOutcome.Ack, HistorianWriteOutcome.Ack]);
|
|
statements.ShouldBe(1 + 6, "the failed chunk, then one statement per event");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task An_outage_retries_the_whole_chunk_without_re_sending_event_by_event()
|
|
{
|
|
var statements = 0;
|
|
var r = await AlarmBatchWrite.RunAsync(Undecided(6), 10,
|
|
(_, _) => { statements++; throw new OutageException(); }, Classify, IsTransient, null, CancellationToken.None);
|
|
|
|
r.ShouldAllBe(o => o == HistorianWriteOutcome.RetryPlease);
|
|
statements.ShouldBe(1);
|
|
}
|
|
|
|
/// <summary>
|
|
/// #976 review: an unrecognised chunk failure is isolated, but once a single send hits a KNOWN outage the rest of the
|
|
/// chunk is RetryPlease — an outage the chunk-level classifier missed must not cost one round trip per event.
|
|
/// </summary>
|
|
[Fact]
|
|
public async Task Isolation_stops_at_the_first_single_that_hits_a_known_outage()
|
|
{
|
|
var statements = 0;
|
|
var r = await AlarmBatchWrite.RunAsync(Undecided(150), 150, (ix, _) =>
|
|
{
|
|
statements++;
|
|
if (ix.Count > 1) throw new UnrecognisedException();
|
|
if (ix[0] == 0) return Task.CompletedTask;
|
|
throw new OutageException();
|
|
}, Classify, IsTransient, null, CancellationToken.None);
|
|
|
|
r[0].ShouldBe(HistorianWriteOutcome.Ack);
|
|
r.Skip(1).ShouldAllBe(o => o == HistorianWriteOutcome.RetryPlease);
|
|
statements.ShouldBe(1 + 2, "the chunk, the Ack'd single, the single that met the outage — then stop");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Positive_control_isolation_continues_past_unrecognised_single_failures()
|
|
{
|
|
var r = await AlarmBatchWrite.RunAsync(Undecided(5), 10, (ix, _) =>
|
|
{
|
|
if (ix.Count > 1 || ix[0] is 1 or 2) throw new UnrecognisedException();
|
|
return Task.CompletedTask;
|
|
}, Classify, IsTransient, null, CancellationToken.None);
|
|
|
|
r.ShouldBe([HistorianWriteOutcome.Ack, HistorianWriteOutcome.RetryPlease, HistorianWriteOutcome.RetryPlease,
|
|
HistorianWriteOutcome.Ack, HistorianWriteOutcome.Ack], "two adjacent poison events never stop the isolation");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Pre_decided_slots_are_kept_and_never_sent()
|
|
{
|
|
var sent = new List<int>();
|
|
var outcomes = Undecided(4);
|
|
outcomes[1] = HistorianWriteOutcome.PermanentFail; // e.g. a pre-1970 time on QuestDB
|
|
var r = await AlarmBatchWrite.RunAsync(outcomes, 10,
|
|
(ix, _) => { sent.AddRange(ix); return Task.CompletedTask; }, Classify, IsTransient, null, CancellationToken.None);
|
|
|
|
r.ShouldBe([HistorianWriteOutcome.Ack, HistorianWriteOutcome.PermanentFail, HistorianWriteOutcome.Ack, HistorianWriteOutcome.Ack]);
|
|
sent.ShouldBe([0, 2, 3]);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task A_cancellation_mid_batch_retries_everything_undecided_and_dead_letters_nothing()
|
|
{
|
|
using var cts = new CancellationTokenSource();
|
|
var r = await AlarmBatchWrite.RunAsync(Undecided(5), 2, (ix, ct) =>
|
|
{
|
|
if (ix.Contains(2)) { cts.Cancel(); ct.ThrowIfCancellationRequested(); }
|
|
return Task.CompletedTask;
|
|
}, Classify, IsTransient, null, cts.Token);
|
|
|
|
r.ShouldBe([HistorianWriteOutcome.Ack, HistorianWriteOutcome.Ack,
|
|
HistorianWriteOutcome.RetryPlease, HistorianWriteOutcome.RetryPlease, HistorianWriteOutcome.RetryPlease]);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task A_cancellation_during_poison_isolation_retries_the_rest()
|
|
{
|
|
using var cts = new CancellationTokenSource();
|
|
var r = await AlarmBatchWrite.RunAsync(Undecided(4), 10, (ix, ct) =>
|
|
{
|
|
if (ix.Count > 1) throw new PoisonException();
|
|
if (ix[0] == 1) { cts.Cancel(); ct.ThrowIfCancellationRequested(); }
|
|
return Task.CompletedTask;
|
|
}, Classify, IsTransient, null, cts.Token);
|
|
|
|
r.ShouldBe([HistorianWriteOutcome.Ack, HistorianWriteOutcome.RetryPlease, HistorianWriteOutcome.RetryPlease,
|
|
HistorianWriteOutcome.RetryPlease]);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task An_unrecognised_chunk_failure_is_isolated_so_its_siblings_still_Ack()
|
|
{
|
|
// G-16 secondary effect (#976): an error the classifier does not recognise is RetryPlease — not proven permanent —
|
|
// but it is not a known outage either. Retrying the whole chunk tied every sibling to the poison event; isolating it
|
|
// lets the siblings land, and the store-and-forward sink (which counts a RetryPlease only when the same drain shows
|
|
// the historian reachable) dead-letters the poison event alone once it keeps failing.
|
|
var statements = 0;
|
|
var r = await AlarmBatchWrite.RunAsync(Undecided(3), 10, (ix, _) =>
|
|
{
|
|
statements++;
|
|
if (ix.Contains(1)) throw new UnrecognisedException();
|
|
return Task.CompletedTask;
|
|
}, Classify, IsTransient, null, CancellationToken.None);
|
|
|
|
r.ShouldBe([HistorianWriteOutcome.Ack, HistorianWriteOutcome.RetryPlease, HistorianWriteOutcome.Ack]);
|
|
statements.ShouldBe(1 + 3, "the failed chunk, then one statement per event");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task A_known_outage_is_never_isolated_even_though_an_unrecognised_failure_is()
|
|
{
|
|
// Control for the test above: isolation keys on "not a KNOWN transient", not on RetryPlease.
|
|
var statements = 0;
|
|
var r = await AlarmBatchWrite.RunAsync(Undecided(3), 10,
|
|
(_, _) => { statements++; throw new OutageException(); }, Classify, IsTransient, null, CancellationToken.None);
|
|
|
|
r.ShouldAllBe(o => o == HistorianWriteOutcome.RetryPlease);
|
|
statements.ShouldBe(1);
|
|
}
|
|
}
|