diff --git a/src/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Historian.Gateway/GatewayAlarmHistorianWriter.cs b/src/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Historian.Gateway/GatewayAlarmHistorianWriter.cs index 32600475f..271a8a3a5 100644 --- a/src/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Historian.Gateway/GatewayAlarmHistorianWriter.cs +++ b/src/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Historian.Gateway/GatewayAlarmHistorianWriter.cs @@ -35,7 +35,8 @@ namespace ZB.MOM.WW.OtOpcUa.Driver.Historian.Gateway; /// which the sink counts toward MaxAttempts 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); -/// Unimplemented (a gateway older than the RPC) ⇒ the writer stops trying batches for its lifetime and +/// Unimplemented 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; /// any other refusal (InvalidArgument — 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. @@ -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). /// 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; /// /// Only the codes that prove the EVENT is bad dead-letter; every other code — transient, auth, Cancelled diff --git a/tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Historian.Gateway.Tests/GatewayAlarmWriterReachabilityTests.cs b/tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Historian.Gateway.Tests/GatewayAlarmWriterReachabilityTests.cs index 589d0a939..6183ba4a1 100644 --- a/tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Historian.Gateway.Tests/GatewayAlarmWriterReachabilityTests.cs +++ b/tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Historian.Gateway.Tests/GatewayAlarmWriterReachabilityTests.cs @@ -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(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())));