fix(historian): #1109 a proxy-fabricated Unimplemented on SendEvents is an outage, not a missing batch RPC

IsSystemicFailure (which covers client-fabricated statuses) now runs before the
Unimplemented probe, so a proxy 404 retries the batch instead of flipping the
writer to single sends for its lifetime. Classify also treats a client-fabricated
status as RetryPlease, so a fabricated Unimplemented/InvalidArgument on a single
SendEvent no longer dead-letters an event the gateway never judged. (#1103)
This commit is contained in:
Joseph Doherty
2026-12-04 22:02:14 -05:00
parent da23fd970d
commit e38b9030b7
2 changed files with 75 additions and 9 deletions
@@ -35,7 +35,8 @@ namespace ZB.MOM.WW.OtOpcUa.Driver.Historian.Gateway;
/// which the sink counts toward <c>MaxAttempts</c> on that Ack'd drain and dead-letters alone. If NO single send
/// Acks, it was not the event: later chunks are RetryPlease as for an outage (so such an outage costs at most one
/// chunk of single sends per drain);</item>
/// <item><c>Unimplemented</c> (a gateway older than the RPC) ⇒ the writer stops trying batches for its lifetime and
/// <item><c>Unimplemented</c> SENT BY the gateway (one older than the RPC — a proxy's client-fabricated 404
/// "Unimplemented" is an outage, above, #1103) ⇒ the writer stops trying batches for its lifetime and
/// sends every event individually;</item>
/// <item>any other refusal (<c>InvalidArgument</c> — a poison event or an over-limit batch, nothing sent — and the
/// other permanent codes) ⇒ that call's events are sent individually, so the poison event dead-letters alone.</item>
@@ -232,6 +233,15 @@ public sealed class GatewayAlarmHistorianWriter : IAlarmHistorianWriter, IDispos
catch (Exception exception)
{
NoteAnswered(exception);
// #1109/#1103: the outage check runs FIRST. A proxy's non-gRPC 404 surfaces as a client-FABRICATED
// Unimplemented ("Bad gRPC response. HTTP status code: 404"): a broken path, not a gateway that lacks
// SendEvents — it must not flip the writer to single sends for its lifetime.
if (IsSystemicFailure(exception))
{
_logger.LogDebug("Alarm SendEvents transiently failed ({Exception}); the batch will retry.", exception.GetType().Name);
foreach (var i in indexes) outcomes[i] = HistorianWriteOutcome.RetryPlease;
return false;
}
if (StatusOf(exception) == StatusCode.Unimplemented)
{
_batchUnsupported = true;
@@ -242,13 +252,6 @@ public sealed class GatewayAlarmHistorianWriter : IAlarmHistorianWriter, IDispos
}
if (Classify(exception) == HistorianWriteOutcome.RetryPlease)
{
if (IsSystemicFailure(exception))
{
_logger.LogDebug("Alarm SendEvents transiently failed ({Exception}); the batch will retry.", exception.GetType().Name);
foreach (var i in indexes) outcomes[i] = HistorianWriteOutcome.RetryPlease;
return false;
}
// #976 review: a RetryPlease the writer cannot vouch for as an outage (Unknown — the gateway handler threw —
// Internal, DataLoss, an unlisted code, a non-transport client exception) may be ONE event's fault. Retrying
// the whole chunk tied every sibling to it, and the sink counts a RetryPlease only on a drain with an Ack, so
@@ -424,7 +427,11 @@ public sealed class GatewayAlarmHistorianWriter : IAlarmHistorianWriter, IDispos
/// one is a transport-level fault and retries. Never dead-letter on what might be an outage (lmxopcua#976 G-8).
/// </summary>
private static HistorianWriteOutcome Classify(Exception exception) =>
StatusOf(exception) is { } code ? ClassifyStatus(code) : HistorianWriteOutcome.RetryPlease;
FirstRpc(exception) is { } rpc && !IsClientFabricated(rpc)
? ClassifyStatus(rpc.StatusCode)
// No status, or one Grpc.Net.Client fabricated (a proxy 404 => "Unimplemented", a reset => "Internal"): the
// gateway never judged the event, so it must not dead-letter (#1109/#1103).
: HistorianWriteOutcome.RetryPlease;
/// <summary>
/// Only the codes that prove the EVENT is bad dead-letter; every other code — transient, auth, <c>Cancelled</c>
@@ -151,6 +151,65 @@ public sealed class GatewayAlarmWriterReachabilityTests
w.LastReachableUtc.ShouldNotBeNull();
}
// #1109/#1103: a proxy's non-gRPC 404 is fabricated client-side as Unimplemented. It is an outage of the path, NOT a
// gateway lacking SendEvents: the writer must stay on batches, retry the whole batch and send nothing singly.
[Fact]
public async Task A_proxy_fabricated_Unimplemented_on_SendEvents_is_systemic_and_does_not_flip_to_single_sends()
{
var proxy404 = WrappedRpc(StatusCode.Unimplemented, "Bad gRPC response. HTTP status code: 404");
var batchCalls = 0;
var fake = new FakeHistorianGatewayClient
{
SendEventsSource = (_, _) =>
{
Interlocked.Increment(ref batchCalls);
return Task.FromException<SendEventsReply>(proxy404);
},
};
using var w = Writer(fake);
var outcomes = await w.WriteBatchAsync([Evt(0), Evt(1)], Ct);
outcomes.ShouldBe([HistorianWriteOutcome.RetryPlease, HistorianWriteOutcome.RetryPlease]);
w.BatchUnsupported.ShouldBeFalse();
fake.SendEventCallCount.ShouldBe(0);
w.LastReachableUtc.ShouldBeNull();
// Next drain still tries the batch RPC (and the path is healthy again).
fake.SendEventsSource = (events, _) =>
{
Interlocked.Increment(ref batchCalls);
var reply = new SendEventsReply();
foreach (var e in events) reply.Results.Add(new EventWriteResult { Id = e.Id, Success = true });
return Task.FromResult(reply);
};
await w.WriteBatchAsync([Evt(2)], Ct);
batchCalls.ShouldBe(2);
fake.SendEventCallCount.ShouldBe(0);
}
[Fact]
public void A_proxy_fabricated_Unimplemented_is_systemic_but_a_gateway_sent_one_is_not()
{
GatewayAlarmHistorianWriter.IsSystemicFailure(
WrappedRpc(StatusCode.Unimplemented, "Bad gRPC response. HTTP status code: 404")).ShouldBeTrue();
GatewayAlarmHistorianWriter.IsSystemicFailure(Wrapped(StatusCode.Unimplemented)).ShouldBeFalse();
}
// #1109/#1103: on the single-send path a fabricated Unimplemented (a PermanentFail code when the gateway sends it) must
// retry, never dead-letter — the gateway never judged the event.
[Theory]
[InlineData(StatusCode.Unimplemented, "Bad gRPC response. HTTP status code: 404")]
[InlineData(StatusCode.InvalidArgument, "Error starting gRPC call. HttpRequestException: reset")]
public async Task A_client_fabricated_permanent_code_on_a_single_send_retries(StatusCode code, string detail)
{
var fake = new FakeHistorianGatewayClient { SendEventThrows = WrappedRpc(code, detail) };
using var w = Writer(fake);
UseSingleSends(w);
var outcomes = await w.WriteBatchAsync([Evt()], Ct);
outcomes.ShouldBe([HistorianWriteOutcome.RetryPlease]);
}
private static Exception Wrapped(StatusCode code) =>
new HistorianGatewayException(code.ToString(), new RpcException(new Status(code, code.ToString())));