refactor(server): SparseArrayExpander throws a domain exception, mapped to InvalidArgument in the gRPC layer (mxaccessgw#190)
Adds SparseArrayException so the Sessions layer no longer throws Grpc.Core.RpcException; MxAccessGatewayService.MapException maps it to StatusCode.InvalidArgument (client-visible behaviour unchanged). Adds a service-level mapping test.
This commit is contained in:
@@ -1082,6 +1082,11 @@ public sealed class MxAccessGatewayService(
|
|||||||
return MapSessionException(sessionException);
|
return MapSessionException(sessionException);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (exception is SparseArrayException sparseException)
|
||||||
|
{
|
||||||
|
return new RpcException(new Status(StatusCode.InvalidArgument, sparseException.Message));
|
||||||
|
}
|
||||||
|
|
||||||
if (exception is WorkerClientException workerClientException)
|
if (exception is WorkerClientException workerClientException)
|
||||||
{
|
{
|
||||||
return MapWorkerClientException(workerClientException);
|
return MapWorkerClientException(workerClientException);
|
||||||
|
|||||||
@@ -1392,9 +1392,9 @@ public sealed class GatewaySession
|
|||||||
// lands on the same MxCommand instance forwarded to the worker.
|
// lands on the same MxCommand instance forwarded to the worker.
|
||||||
// 2. Sparse array write values are expanded to whole-array values, because MXAccess has no
|
// 2. Sparse array write values are expanded to whole-array values, because MXAccess has no
|
||||||
// partial-array write primitive — the worker only ever sees a full MxArray.
|
// partial-array write primitive — the worker only ever sees a full MxArray.
|
||||||
// SparseArrayExpander.Expand throws RpcException(InvalidArgument) for an invalid sparse payload;
|
// SparseArrayExpander.Expand throws SparseArrayException for an invalid sparse payload; that
|
||||||
// that propagates out of InvokeAsync as the desired client-facing error and is deliberately not
|
// propagates out of InvokeAsync and the gRPC layer maps it to InvalidArgument. It is
|
||||||
// caught here.
|
// deliberately not caught here.
|
||||||
private void NormalizeOutboundCommand(MxCommand command)
|
private void NormalizeOutboundCommand(MxCommand command)
|
||||||
{
|
{
|
||||||
switch (command.PayloadCase)
|
switch (command.PayloadCase)
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
namespace ZB.MOM.WW.MxGateway.Server.Sessions;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Thrown by <see cref="SparseArrayExpander"/> when a client-supplied sparse array write is invalid.
|
||||||
|
/// A domain exception so the Sessions layer stays free of gRPC types; the gRPC layer maps it to
|
||||||
|
/// <c>InvalidArgument</c> (see <c>MxAccessGatewayService.MapException</c>).
|
||||||
|
/// </summary>
|
||||||
|
internal sealed class SparseArrayException : Exception
|
||||||
|
{
|
||||||
|
/// <summary>Initializes a new instance of the <see cref="SparseArrayException"/> class.</summary>
|
||||||
|
/// <param name="message">A description of why the sparse payload is invalid.</param>
|
||||||
|
public SparseArrayException(string message)
|
||||||
|
: base(message)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
using Google.Protobuf.WellKnownTypes;
|
using Google.Protobuf.WellKnownTypes;
|
||||||
using Grpc.Core;
|
|
||||||
using ZB.MOM.WW.MxGateway.Contracts.Proto;
|
using ZB.MOM.WW.MxGateway.Contracts.Proto;
|
||||||
|
|
||||||
namespace ZB.MOM.WW.MxGateway.Server.Sessions;
|
namespace ZB.MOM.WW.MxGateway.Server.Sessions;
|
||||||
@@ -40,8 +39,8 @@ internal static class SparseArrayExpander
|
|||||||
/// so the <see cref="Array.MaxLength"/> backstop is the only bound in test/unit-construction
|
/// so the <see cref="Array.MaxLength"/> backstop is the only bound in test/unit-construction
|
||||||
/// paths that do not thread the configured cap.
|
/// paths that do not thread the configured cap.
|
||||||
/// </param>
|
/// </param>
|
||||||
/// <exception cref="RpcException">
|
/// <exception cref="SparseArrayException">
|
||||||
/// <see cref="StatusCode.InvalidArgument"/> when the sparse payload is invalid: zero
|
/// Thrown (mapped to gRPC <c>InvalidArgument</c> by the gRPC layer) when the sparse payload is invalid: zero
|
||||||
/// total length, a total length exceeding <paramref name="maxSparseArrayLength"/>, an index
|
/// total length, a total length exceeding <paramref name="maxSparseArrayLength"/>, an index
|
||||||
/// at or beyond the total length, a duplicate index, an unsupported element type, or an
|
/// at or beyond the total length, a duplicate index, an unsupported element type, or an
|
||||||
/// element value whose kind does not match the declared element type.
|
/// element value whose kind does not match the declared element type.
|
||||||
@@ -303,6 +302,5 @@ internal static class SparseArrayExpander
|
|||||||
private static long ReadInt64(MxValue value) =>
|
private static long ReadInt64(MxValue value) =>
|
||||||
value.KindCase == MxValue.KindOneofCase.Int64Value ? value.Int64Value : value.Int32Value;
|
value.KindCase == MxValue.KindOneofCase.Int64Value ? value.Int64Value : value.Int32Value;
|
||||||
|
|
||||||
private static RpcException Invalid(string message) =>
|
private static SparseArrayException Invalid(string message) => new(message);
|
||||||
new(new Status(StatusCode.InvalidArgument, message));
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -161,6 +161,28 @@ public sealed class MxAccessGatewayServiceTests
|
|||||||
Assert.Equal(0, sessionManager.InvokeCount);
|
Assert.Equal(0, sessionManager.InvokeCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Verifies that an invalid sparse-array write (a Sessions-layer <c>SparseArrayException</c>) surfaces
|
||||||
|
/// to gRPC clients as <c>InvalidArgument</c> rather than <c>Unavailable</c> (mxaccessgw#190).
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||||
|
[Fact]
|
||||||
|
public async Task Invoke_WhenSparseArrayInvalid_MapsToInvalidArgument()
|
||||||
|
{
|
||||||
|
FakeSessionManager sessionManager = new()
|
||||||
|
{
|
||||||
|
InvokeException = new SparseArrayException("Sparse array total_length must be greater than zero."),
|
||||||
|
};
|
||||||
|
sessionManager.SeedSession(CreateSession("session-1", processId: 1234));
|
||||||
|
MxAccessGatewayService service = CreateService(sessionManager);
|
||||||
|
|
||||||
|
RpcException exception = await Assert.ThrowsAsync<RpcException>(
|
||||||
|
async () => await service.Invoke(CreatePingRequest("session-1"), new TestServerCallContext()));
|
||||||
|
|
||||||
|
Assert.Equal(StatusCode.InvalidArgument, exception.StatusCode);
|
||||||
|
Assert.Contains("total_length", exception.Status.Detail);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>Verifies that Invoke returns HResult status and method payload from worker reply.</summary>
|
/// <summary>Verifies that Invoke returns HResult status and method payload from worker reply.</summary>
|
||||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||||
[Fact]
|
[Fact]
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
using Google.Protobuf.WellKnownTypes;
|
using Google.Protobuf.WellKnownTypes;
|
||||||
using Grpc.Core;
|
|
||||||
using ZB.MOM.WW.MxGateway.Contracts.Proto;
|
using ZB.MOM.WW.MxGateway.Contracts.Proto;
|
||||||
using ZB.MOM.WW.MxGateway.Server.Sessions;
|
using ZB.MOM.WW.MxGateway.Server.Sessions;
|
||||||
|
|
||||||
@@ -56,17 +55,16 @@ public sealed class SparseArrayExpanderTests
|
|||||||
Assert.Equal(new[] { false, false, false }, value.ArrayValue.BoolValues.Values);
|
Assert.Equal(new[] { false, false, false }, value.ArrayValue.BoolValues.Values);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Verifies that expanding a sparse array with a zero total length throws an invalid-argument <see cref="RpcException"/>.</summary>
|
/// <summary>Verifies that expanding a sparse array with a zero total length throws a <see cref="SparseArrayException"/>.</summary>
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Expand_ZeroTotalLength_Throws()
|
public void Expand_ZeroTotalLength_Throws()
|
||||||
{
|
{
|
||||||
MxValue value = SparseValue(MxDataType.Integer, 0);
|
MxValue value = SparseValue(MxDataType.Integer, 0);
|
||||||
|
|
||||||
RpcException ex = Assert.Throws<RpcException>(() => SparseArrayExpander.Expand(value));
|
SparseArrayException ex = Assert.Throws<SparseArrayException>(() => SparseArrayExpander.Expand(value));
|
||||||
Assert.Equal(StatusCode.InvalidArgument, ex.StatusCode);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Verifies that an element index at or beyond the total length throws an invalid-argument <see cref="RpcException"/>.</summary>
|
/// <summary>Verifies that an element index at or beyond the total length throws a <see cref="SparseArrayException"/>.</summary>
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Expand_IndexOutOfRange_Throws()
|
public void Expand_IndexOutOfRange_Throws()
|
||||||
{
|
{
|
||||||
@@ -75,11 +73,10 @@ public sealed class SparseArrayExpanderTests
|
|||||||
2,
|
2,
|
||||||
(5, new MxValue { Int32Value = 1 }));
|
(5, new MxValue { Int32Value = 1 }));
|
||||||
|
|
||||||
RpcException ex = Assert.Throws<RpcException>(() => SparseArrayExpander.Expand(value));
|
SparseArrayException ex = Assert.Throws<SparseArrayException>(() => SparseArrayExpander.Expand(value));
|
||||||
Assert.Equal(StatusCode.InvalidArgument, ex.StatusCode);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Verifies that two sparse elements sharing the same index throw an invalid-argument <see cref="RpcException"/>.</summary>
|
/// <summary>Verifies that two sparse elements sharing the same index throw a <see cref="SparseArrayException"/>.</summary>
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Expand_DuplicateIndex_Throws()
|
public void Expand_DuplicateIndex_Throws()
|
||||||
{
|
{
|
||||||
@@ -89,21 +86,19 @@ public sealed class SparseArrayExpanderTests
|
|||||||
(1, new MxValue { Int32Value = 1 }),
|
(1, new MxValue { Int32Value = 1 }),
|
||||||
(1, new MxValue { Int32Value = 2 }));
|
(1, new MxValue { Int32Value = 2 }));
|
||||||
|
|
||||||
RpcException ex = Assert.Throws<RpcException>(() => SparseArrayExpander.Expand(value));
|
SparseArrayException ex = Assert.Throws<SparseArrayException>(() => SparseArrayExpander.Expand(value));
|
||||||
Assert.Equal(StatusCode.InvalidArgument, ex.StatusCode);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Verifies that an unspecified element data type throws an invalid-argument <see cref="RpcException"/>.</summary>
|
/// <summary>Verifies that an unspecified element data type throws a <see cref="SparseArrayException"/>.</summary>
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Expand_UnsupportedElementType_Throws()
|
public void Expand_UnsupportedElementType_Throws()
|
||||||
{
|
{
|
||||||
MxValue value = SparseValue(MxDataType.Unspecified, 2);
|
MxValue value = SparseValue(MxDataType.Unspecified, 2);
|
||||||
|
|
||||||
RpcException ex = Assert.Throws<RpcException>(() => SparseArrayExpander.Expand(value));
|
SparseArrayException ex = Assert.Throws<SparseArrayException>(() => SparseArrayExpander.Expand(value));
|
||||||
Assert.Equal(StatusCode.InvalidArgument, ex.StatusCode);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Verifies that an element value whose kind does not match the declared element data type throws an invalid-argument <see cref="RpcException"/>.</summary>
|
/// <summary>Verifies that an element value whose kind does not match the declared element data type throws a <see cref="SparseArrayException"/>.</summary>
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Expand_ElementValueKindMismatch_Throws()
|
public void Expand_ElementValueKindMismatch_Throws()
|
||||||
{
|
{
|
||||||
@@ -112,8 +107,7 @@ public sealed class SparseArrayExpanderTests
|
|||||||
2,
|
2,
|
||||||
(0, new MxValue { StringValue = "nope" }));
|
(0, new MxValue { StringValue = "nope" }));
|
||||||
|
|
||||||
RpcException ex = Assert.Throws<RpcException>(() => SparseArrayExpander.Expand(value));
|
SparseArrayException ex = Assert.Throws<SparseArrayException>(() => SparseArrayExpander.Expand(value));
|
||||||
Assert.Equal(StatusCode.InvalidArgument, ex.StatusCode);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Verifies that expanding a String sparse array fills unset indices with an empty string default.</summary>
|
/// <summary>Verifies that expanding a String sparse array fills unset indices with an empty string default.</summary>
|
||||||
@@ -213,14 +207,13 @@ public sealed class SparseArrayExpanderTests
|
|||||||
Assert.Throws<ArgumentNullException>(() => SparseArrayExpander.Expand(null!));
|
Assert.Throws<ArgumentNullException>(() => SparseArrayExpander.Expand(null!));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Verifies that a total length exceeding the maximum supported array length throws an invalid-argument <see cref="RpcException"/>.</summary>
|
/// <summary>Verifies that a total length exceeding the maximum supported array length throws a <see cref="SparseArrayException"/>.</summary>
|
||||||
[Fact]
|
[Fact]
|
||||||
public void Expand_TotalLengthExceedsMaxArrayLength_Throws()
|
public void Expand_TotalLengthExceedsMaxArrayLength_Throws()
|
||||||
{
|
{
|
||||||
MxValue value = SparseValue(MxDataType.Integer, 2_147_483_648u);
|
MxValue value = SparseValue(MxDataType.Integer, 2_147_483_648u);
|
||||||
|
|
||||||
RpcException ex = Assert.Throws<RpcException>(() => SparseArrayExpander.Expand(value));
|
SparseArrayException ex = Assert.Throws<SparseArrayException>(() => SparseArrayExpander.Expand(value));
|
||||||
Assert.Equal(StatusCode.InvalidArgument, ex.StatusCode);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Verifies that a total length above the configured cap throws <see cref="StatusCode.InvalidArgument"/> before the full array is allocated.</summary>
|
/// <summary>Verifies that a total length above the configured cap throws <see cref="StatusCode.InvalidArgument"/> before the full array is allocated.</summary>
|
||||||
@@ -231,9 +224,8 @@ public sealed class SparseArrayExpanderTests
|
|||||||
// so only the configured cap can reject it (the Array.MaxLength backstop would not).
|
// so only the configured cap can reject it (the Array.MaxLength backstop would not).
|
||||||
MxValue value = SparseValue(MxDataType.Integer, 500_000_000u);
|
MxValue value = SparseValue(MxDataType.Integer, 500_000_000u);
|
||||||
|
|
||||||
RpcException ex = Assert.Throws<RpcException>(() => SparseArrayExpander.Expand(value, maxSparseArrayLength: 1_000_000));
|
SparseArrayException ex = Assert.Throws<SparseArrayException>(() => SparseArrayExpander.Expand(value, maxSparseArrayLength: 1_000_000));
|
||||||
Assert.Equal(StatusCode.InvalidArgument, ex.StatusCode);
|
Assert.Contains("MaxSparseArrayLength", ex.Message, StringComparison.Ordinal);
|
||||||
Assert.Contains("MaxSparseArrayLength", ex.Status.Detail, StringComparison.Ordinal);
|
|
||||||
|
|
||||||
// The value must be untouched — expansion (allocation) never ran.
|
// The value must be untouched — expansion (allocation) never ran.
|
||||||
Assert.Equal(MxValue.KindOneofCase.SparseArrayValue, value.KindCase);
|
Assert.Equal(MxValue.KindOneofCase.SparseArrayValue, value.KindCase);
|
||||||
|
|||||||
Reference in New Issue
Block a user