fix(historian-gateway): #1103 IsSystemicFailure shares the client-fabricated-status classification with IsGatewayAnswer

This commit is contained in:
Joseph Doherty
2026-12-04 20:16:15 -05:00
parent ce0133b03c
commit f7a0c4aef5
2 changed files with 45 additions and 13 deletions
@@ -101,16 +101,21 @@ public sealed class GatewayAlarmHistorianWriter : IAlarmHistorianWriter, IDispos
/// </summary> /// </summary>
internal static bool IsGatewayAnswer(Exception exception) internal static bool IsGatewayAnswer(Exception exception)
{ {
// One authority: IsSystemicFailure owns the whole classification, including a client-fabricated status (#1103).
if (IsSystemicFailure(exception)) return false; if (IsSystemicFailure(exception)) return false;
for (Exception? e = exception; e is not null; e = e.InnerException) return StatusOf(exception) is not null;
{ }
if (e is not RpcException rpc) continue;
if (rpc.Status.DebugException is not null) return false; /// <summary>
var detail = rpc.Status.Detail ?? string.Empty; /// True when the status was fabricated client-side (Grpc.Net.Client transport exception attached as
foreach (var prefix in TransportDetailPrefixes) /// <c>DebugException</c>, or a client/proxy detail prefix) rather than sent by the gateway (#1096/#1103).
if (detail.StartsWith(prefix, StringComparison.Ordinal)) return false; /// </summary>
return true; private static bool IsClientFabricated(RpcException rpc)
} {
if (rpc.Status.DebugException is not null) return true;
var detail = rpc.Status.Detail ?? string.Empty;
foreach (var prefix in TransportDetailPrefixes)
if (detail.StartsWith(prefix, StringComparison.Ordinal)) return true;
return false; return false;
} }
@@ -314,27 +319,34 @@ public sealed class GatewayAlarmHistorianWriter : IAlarmHistorianWriter, IDispos
} }
/// <summary>The gRPC status of a gateway failure (raw, or wrapped by the package client); null when it has none.</summary> /// <summary>The gRPC status of a gateway failure (raw, or wrapped by the package client); null when it has none.</summary>
private static StatusCode? StatusOf(Exception exception) private static StatusCode? StatusOf(Exception exception) => FirstRpc(exception)?.StatusCode;
private static RpcException? FirstRpc(Exception exception)
{ {
for (Exception? e = exception; e is not null; e = e.InnerException) for (Exception? e = exception; e is not null; e = e.InnerException)
if (e is RpcException rpc) return rpc.StatusCode; if (e is RpcException rpc) return rpc;
return null; return null;
} }
/// <summary> /// <summary>
/// Whether a RetryPlease-class failure is a KNOWN outage of the whole gateway path (so re-sending event by event would /// Whether a RetryPlease-class failure is a KNOWN outage of the whole gateway path (so re-sending event by event would
/// only multiply the round trips): the transient / back-pressure / auth / deadline / server-cancellation codes, and a /// only multiply the round trips): the transient / back-pressure / auth / deadline / server-cancellation codes, and a
/// status-less failure whose chain holds a transport fault (socket, I/O, HTTP, a disposed channel, a timeout or an /// client-fabricated status (#1103), and a status-less failure whose chain holds a transport fault (socket, I/O, HTTP, a disposed channel, a timeout or an
/// internal cancellation). Everything else — <c>Unknown</c>, <c>Internal</c>, <c>DataLoss</c>, unlisted codes, a /// internal cancellation). Everything else — <c>Unknown</c>, <c>Internal</c>, <c>DataLoss</c>, unlisted codes, a
/// client exception with no status and no transport cause, any other exception — is unrecognised and gets the chunk /// client exception with no status and no transport cause, any other exception — is unrecognised and gets the chunk
/// isolated per event. /// isolated per event.
/// </summary> /// </summary>
internal static bool IsSystemicFailure(Exception exception) internal static bool IsSystemicFailure(Exception exception)
{ {
if (StatusOf(exception) is { } code) if (FirstRpc(exception) is { } rpc)
{
// #1103: a proxy/transport-fabricated status of ANY code is an outage of the path, not an event-level fault.
if (IsClientFabricated(rpc)) return true;
var code = rpc.StatusCode;
return code is StatusCode.Unavailable or StatusCode.DeadlineExceeded or StatusCode.Cancelled return code is StatusCode.Unavailable or StatusCode.DeadlineExceeded or StatusCode.Cancelled
or StatusCode.ResourceExhausted or StatusCode.Aborted or StatusCode.Unauthenticated or StatusCode.ResourceExhausted or StatusCode.Aborted or StatusCode.Unauthenticated
or StatusCode.PermissionDenied; or StatusCode.PermissionDenied;
}
for (Exception? e = exception; e is not null; e = e.InnerException) for (Exception? e = exception; e is not null; e = e.InnerException)
if (e is HistorianGatewayUnavailableException or HistorianGatewayAuthenticationException if (e is HistorianGatewayUnavailableException or HistorianGatewayAuthenticationException
or HistorianGatewayAuthorizationException or HistorianGatewayAuthorizationException
@@ -77,6 +77,26 @@ public sealed class GatewayAlarmWriterReachabilityTests
GatewayAlarmHistorianWriter.IsGatewayAnswer(new HistorianGatewayException("x")).ShouldBeFalse(); GatewayAlarmHistorianWriter.IsGatewayAnswer(new HistorianGatewayException("x")).ShouldBeFalse();
} }
// #1103: IsSystemicFailure shares the same classification.
[Theory]
[InlineData(StatusCode.Internal)]
[InlineData(StatusCode.Unknown)]
public void A_genuine_gateway_Internal_or_Unknown_is_not_systemic(StatusCode code) =>
GatewayAlarmHistorianWriter.IsSystemicFailure(Wrapped(code)).ShouldBeFalse();
[Theory]
[InlineData(StatusCode.Internal, "Bad gRPC response. HTTP status code: 500")]
[InlineData(StatusCode.Unknown, "Error reading next message. IOException: reset")]
public void A_fabricated_Internal_or_Unknown_is_systemic(StatusCode code, string detail) =>
GatewayAlarmHistorianWriter.IsSystemicFailure(WrappedRpc(code, detail)).ShouldBeTrue();
[Theory]
[InlineData(StatusCode.Internal)]
[InlineData(StatusCode.Unknown)]
public void A_status_with_a_transport_debug_exception_is_systemic(StatusCode code) =>
GatewayAlarmHistorianWriter.IsSystemicFailure(
WrappedRpc(code, "Received RST_STREAM with error code 2", new IOException("reset"))).ShouldBeTrue();
[Theory] [Theory]
[InlineData(StatusCode.Internal)] [InlineData(StatusCode.Internal)]
[InlineData(StatusCode.Unknown)] [InlineData(StatusCode.Unknown)]