5e375f6d3d
Adds five new MXAccess command kinds (WriteBulk, Write2Bulk,
WriteSecuredBulk, WriteSecured2Bulk, ReadBulk) that ride the existing
"one round-trip, per-entry results" bulk shape used by AddItemBulk and
SubscribeBulk today. MXAccess COM has no native bulk API; the worker
runs each bulk operation as a sequential loop on its STA, returning
one BulkWriteResult / BulkReadResult per requested entry so per-item
MXAccess failures surface as was_successful=false rather than throwing.
ReadBulk has no MXAccess analogue. The worker satisfies it by:
- Returning the last cached OnDataChange payload (was_cached=true)
when the requested tag is already in the session''s item registry
AND advised — the existing subscription is NOT touched, since the
caller did not create it.
- Otherwise taking the AddItem + Advise + wait-for-OnDataChange +
UnAdvise + RemoveItem snapshot lifecycle itself (was_cached=false)
and leaving the session exactly as it was. The wait pumps Windows
messages on the STA so the inbound MXAccess event can dispatch
while the executor still holds the thread.
The new MxAccessValueCache lives on each MxAccessSession, shared with
MxAccessBaseEventSink which populates it on every OnDataChange after
the event clears the outbound queue. Eviction on RemoveItem keeps
reused MXAccess handles from serving stale values from a previous
lifetime.
Gateway-side authorization wires WriteBulk/Write2Bulk to invoke:write,
WriteSecuredBulk/WriteSecured2Bulk to invoke:secure, ReadBulk to
invoke:read. The constraint-filter pipeline is refactored from a single
BulkConstraintPlan record into an abstract base plus three concretes
(SubscribeBulk, WriteBulk, ReadBulk), each owning its own denied-entry
merge so the dispatch site never branches on reply shape. A new
FilterWriteBulkAsync<TEntry> generic over the four write-entry shapes
runs CheckWriteHandleAsync per entry; denied entries surface as the
BulkWriteResult shape, preserving original-index order.
All five language clients (.NET, Go, Rust, Python, Java) gained the
five new methods following their existing bulk pattern, with regenerated
protobufs.
Tests added:
- MxAccessValueCacheTests (6 cases) — Set/TryGet, Remove resets the
version, TryWaitForUpdate signals on Set, pump step fires each poll.
- MxAccessBaseEventSinkTests — OnDataChange populates the cache,
ValueCache property exposes the bound instance.
- MxAccessCommandExecutorTests — four bulk-write variants (per-entry
success/failure, value+timestamp forwarding, secured user ids),
ReadBulk snapshot lifecycle on uncached tag (timeout surfaces as
was_successful=false), invalid-payload reply.
- GatewayGrpcScopeResolverTests — five new MxCommandKind cases.
- SessionManagerTests — WriteBulk and ReadBulk forwarding through
FakeWorkerHarness; ReadBulk forwards timeout_ms.
- Per-client (.NET, Go, Rust, Python, Java) — WriteBulk builds the
right command and returns per-entry results, ReadBulk forwards the
timeout and unpacks the was_cached flag.
Cross-language e2e CLI subcommands for the new bulks are deliberately
scoped out of this change (each of the five client CLIs would need
five new subcommands plus matching phases in
scripts/run-client-e2e-tests.ps1); coverage equivalent to the existing
bulk-subscribe coverage is provided by worker + gateway + per-client
unit tests.
Docs updated in the same commit: gateway.md (Public MXAccess Command
Surface), docs/DesignDecisions.md (new "Bulk Command Family" section
with the ReadBulk cache-then-snapshot rationale), and every client
README.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
121 lines
5.5 KiB
C#
121 lines
5.5 KiB
C#
using Grpc.Core;
|
|
using MxGateway.Contracts.Proto;
|
|
|
|
namespace MxGateway.Server.Grpc;
|
|
|
|
public sealed class MxAccessGrpcRequestValidator
|
|
{
|
|
/// <summary>Validates an open session request.</summary>
|
|
/// <param name="request">The request to validate.</param>
|
|
public void ValidateOpenSession(OpenSessionRequest request)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(request);
|
|
|
|
if (request.CommandTimeout is not null && request.CommandTimeout.ToTimeSpan() <= TimeSpan.Zero)
|
|
{
|
|
throw InvalidArgument("Command timeout must be greater than zero when provided.");
|
|
}
|
|
}
|
|
|
|
/// <summary>Validates a close session request.</summary>
|
|
/// <param name="request">The request to validate.</param>
|
|
public void ValidateCloseSession(CloseSessionRequest request)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(request);
|
|
RequireSessionId(request.SessionId);
|
|
}
|
|
|
|
/// <summary>Validates a stream events request.</summary>
|
|
/// <param name="request">The request to validate.</param>
|
|
public void ValidateStreamEvents(StreamEventsRequest request)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(request);
|
|
RequireSessionId(request.SessionId);
|
|
}
|
|
|
|
/// <summary>Validates an invoke request with command payload.</summary>
|
|
/// <param name="request">The request to validate.</param>
|
|
public void ValidateInvoke(MxCommandRequest request)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(request);
|
|
RequireSessionId(request.SessionId);
|
|
|
|
if (request.Command is null)
|
|
{
|
|
throw InvalidArgument("Invoke requires a command payload.");
|
|
}
|
|
|
|
if (request.Command.Kind is MxCommandKind.Unspecified)
|
|
{
|
|
throw InvalidArgument("Invoke requires a command kind.");
|
|
}
|
|
|
|
ValidateCommandPayload(request.Command);
|
|
}
|
|
|
|
private static void RequireSessionId(string sessionId)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(sessionId))
|
|
{
|
|
throw InvalidArgument("Session id is required.");
|
|
}
|
|
}
|
|
|
|
private static void ValidateCommandPayload(MxCommand command)
|
|
{
|
|
MxCommand.PayloadOneofCase expectedPayload = ExpectedPayload(command.Kind);
|
|
if (command.PayloadCase != expectedPayload)
|
|
{
|
|
throw InvalidArgument(
|
|
$"Command kind {command.Kind} requires payload {expectedPayload} but received {command.PayloadCase}.");
|
|
}
|
|
}
|
|
|
|
private static MxCommand.PayloadOneofCase ExpectedPayload(MxCommandKind kind)
|
|
{
|
|
return kind switch
|
|
{
|
|
MxCommandKind.Register => MxCommand.PayloadOneofCase.Register,
|
|
MxCommandKind.Unregister => MxCommand.PayloadOneofCase.Unregister,
|
|
MxCommandKind.AddItem => MxCommand.PayloadOneofCase.AddItem,
|
|
MxCommandKind.AddItem2 => MxCommand.PayloadOneofCase.AddItem2,
|
|
MxCommandKind.RemoveItem => MxCommand.PayloadOneofCase.RemoveItem,
|
|
MxCommandKind.Advise => MxCommand.PayloadOneofCase.Advise,
|
|
MxCommandKind.UnAdvise => MxCommand.PayloadOneofCase.UnAdvise,
|
|
MxCommandKind.AdviseSupervisory => MxCommand.PayloadOneofCase.AdviseSupervisory,
|
|
MxCommandKind.AddBufferedItem => MxCommand.PayloadOneofCase.AddBufferedItem,
|
|
MxCommandKind.SetBufferedUpdateInterval => MxCommand.PayloadOneofCase.SetBufferedUpdateInterval,
|
|
MxCommandKind.Suspend => MxCommand.PayloadOneofCase.Suspend,
|
|
MxCommandKind.Activate => MxCommand.PayloadOneofCase.Activate,
|
|
MxCommandKind.Write => MxCommand.PayloadOneofCase.Write,
|
|
MxCommandKind.Write2 => MxCommand.PayloadOneofCase.Write2,
|
|
MxCommandKind.WriteSecured => MxCommand.PayloadOneofCase.WriteSecured,
|
|
MxCommandKind.WriteSecured2 => MxCommand.PayloadOneofCase.WriteSecured2,
|
|
MxCommandKind.AuthenticateUser => MxCommand.PayloadOneofCase.AuthenticateUser,
|
|
MxCommandKind.ArchestraUserToId => MxCommand.PayloadOneofCase.ArchestraUserToId,
|
|
MxCommandKind.AddItemBulk => MxCommand.PayloadOneofCase.AddItemBulk,
|
|
MxCommandKind.AdviseItemBulk => MxCommand.PayloadOneofCase.AdviseItemBulk,
|
|
MxCommandKind.RemoveItemBulk => MxCommand.PayloadOneofCase.RemoveItemBulk,
|
|
MxCommandKind.UnAdviseItemBulk => MxCommand.PayloadOneofCase.UnAdviseItemBulk,
|
|
MxCommandKind.SubscribeBulk => MxCommand.PayloadOneofCase.SubscribeBulk,
|
|
MxCommandKind.UnsubscribeBulk => MxCommand.PayloadOneofCase.UnsubscribeBulk,
|
|
MxCommandKind.WriteBulk => MxCommand.PayloadOneofCase.WriteBulk,
|
|
MxCommandKind.Write2Bulk => MxCommand.PayloadOneofCase.Write2Bulk,
|
|
MxCommandKind.WriteSecuredBulk => MxCommand.PayloadOneofCase.WriteSecuredBulk,
|
|
MxCommandKind.WriteSecured2Bulk => MxCommand.PayloadOneofCase.WriteSecured2Bulk,
|
|
MxCommandKind.ReadBulk => MxCommand.PayloadOneofCase.ReadBulk,
|
|
MxCommandKind.Ping => MxCommand.PayloadOneofCase.Ping,
|
|
MxCommandKind.GetSessionState => MxCommand.PayloadOneofCase.GetSessionState,
|
|
MxCommandKind.GetWorkerInfo => MxCommand.PayloadOneofCase.GetWorkerInfo,
|
|
MxCommandKind.DrainEvents => MxCommand.PayloadOneofCase.DrainEvents,
|
|
MxCommandKind.ShutdownWorker => MxCommand.PayloadOneofCase.ShutdownWorker,
|
|
_ => MxCommand.PayloadOneofCase.None,
|
|
};
|
|
}
|
|
|
|
private static RpcException InvalidArgument(string detail)
|
|
{
|
|
return new RpcException(new Status(StatusCode.InvalidArgument, detail));
|
|
}
|
|
}
|