Merge #1103: gateway alarm writer's IsSystemicFailure treats a client/proxy-fabricated status as a path outage — one IsClientFabricated authority shared with IsGatewayAnswer

This commit is contained in:
Joseph Doherty
2026-12-04 20:16:25 -05:00
2 changed files with 45 additions and 13 deletions
@@ -101,16 +101,21 @@ public sealed class GatewayAlarmHistorianWriter : IAlarmHistorianWriter, IDispos
/// </summary>
internal static bool IsGatewayAnswer(Exception exception)
{
// One authority: IsSystemicFailure owns the whole classification, including a client-fabricated status (#1103).
if (IsSystemicFailure(exception)) return false;
for (Exception? e = exception; e is not null; e = e.InnerException)
{
if (e is not RpcException rpc) continue;
if (rpc.Status.DebugException is not null) return false;
var detail = rpc.Status.Detail ?? string.Empty;
foreach (var prefix in TransportDetailPrefixes)
if (detail.StartsWith(prefix, StringComparison.Ordinal)) return false;
return true;
}
return StatusOf(exception) is not null;
}
/// <summary>
/// True when the status was fabricated client-side (Grpc.Net.Client transport exception attached as
/// <c>DebugException</c>, or a client/proxy detail prefix) rather than sent by the gateway (#1096/#1103).
/// </summary>
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;
}
@@ -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>
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)
if (e is RpcException rpc) return rpc.StatusCode;
if (e is RpcException rpc) return rpc;
return null;
}
/// <summary>
/// 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
/// 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
/// client exception with no status and no transport cause, any other exception — is unrecognised and gets the chunk
/// isolated per event.
/// </summary>
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
or StatusCode.ResourceExhausted or StatusCode.Aborted or StatusCode.Unauthenticated
or StatusCode.PermissionDenied;
}
for (Exception? e = exception; e is not null; e = e.InnerException)
if (e is HistorianGatewayUnavailableException or HistorianGatewayAuthenticationException
or HistorianGatewayAuthorizationException
@@ -77,6 +77,26 @@ public sealed class GatewayAlarmWriterReachabilityTests
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]
[InlineData(StatusCode.Internal)]
[InlineData(StatusCode.Unknown)]