feat(worker,gateway): report record-info pin outcome in WorkerReady; drain-thread completion tests

The worker logged its MXSTATUS_PROXY record-info pin outcome only to its own
stderr, which the gateway's launcher does not capture, so operators never saw
whether the pin took.

- Contract (additive, internal IPC): WorkerReady.record_info_pin_status = 5.
  No protocol version bump; an older peer ignores it or sends it empty.
  Regenerated Contracts/Generated, the client protoset, the Rust vendored
  proto, and the Go/Python/Java worker bindings (real delta only).
- Worker: the handshake copies MxStatusProxyRecordInfoPin.Status into
  WorkerReady unless the runtime already set it.
- Gateway: WorkerClient records it (IWorkerClient.RecordInfoPinStatus, default
  null) and logs it at Information when pinned, Warning otherwise; empty is
  not logged. Shown as "Record-info pin" in the dashboard session details
  Worker table via DashboardSessionSummary.WorkerRecordInfoPinStatus.
- Tests: drain-thread cancellation (task ends Canceled; RunAsync cancels,
  runtime disposed, thread exits, no EventDrain stop-failure log) and fault
  (exception on the thread faults RunAsync with the same instance) paths;
  WorkerReady carries the pin status; gateway logging/recording and the
  dashboard projection.
- Docs: gateway.md STA thread model (drain thread, pin step in COM lifetime),
  MxAccessWorkerInstanceDesign, GatewayProcessDesign, GatewayDashboardDesign,
  WorkerFrameProtocol.
This commit is contained in:
Joseph Doherty
2026-11-21 08:11:31 -05:00
parent f6a3e13bd3
commit e811b93eb8
23 changed files with 878 additions and 145 deletions
@@ -102,6 +102,7 @@ else
<tr><th scope="row">Process id</th><td>@(CurrentSession.WorkerProcessId?.ToString(System.Globalization.CultureInfo.InvariantCulture) ?? "-")</td></tr>
<tr><th scope="row">State</th><td><StatusBadge Text="@(CurrentSession.WorkerState?.ToString() ?? "-")" /></td></tr>
<tr><th scope="row">Last heartbeat</th><td>@DashboardDisplay.DateTime(CurrentSession.LastWorkerHeartbeatAt)</td></tr>
<tr><th scope="row">Record-info pin</th><td>@DashboardDisplay.Text(CurrentSession.WorkerRecordInfoPinStatus)</td></tr>
</tbody>
</table>
</div>
@@ -17,4 +17,5 @@ public sealed record DashboardSessionSummary(
WorkerClientState? WorkerState,
DateTimeOffset? LastWorkerHeartbeatAt,
long EventsReceived,
string? LastFault);
string? LastFault,
string? WorkerRecordInfoPinStatus = null);
@@ -285,7 +285,8 @@ public sealed class DashboardSnapshotService : IDashboardSnapshotService
WorkerState: workerClient?.State,
LastWorkerHeartbeatAt: workerClient?.LastHeartbeatAt,
EventsReceived: eventsReceived,
LastFault: DashboardRedactor.Redact(session.FinalFault));
LastFault: DashboardRedactor.Redact(session.FinalFault),
WorkerRecordInfoPinStatus: workerClient?.RecordInfoPinStatus);
}
private static DashboardWorkerSummary CreateWorkerSummary(GatewaySession session)
@@ -27,6 +27,14 @@ public interface IWorkerClient : IAsyncDisposable
/// </returns>
Task? GetStateChangedSignal() => null;
/// <summary>
/// Outcome of the worker's <c>MXSTATUS_PROXY</c> record-info pin, as reported in
/// <see cref="WorkerReady"/>, or <see langword="null"/> before the handshake or when the
/// worker did not report one. Diagnostic only; the default implementation returns
/// <see langword="null"/>.
/// </summary>
string? RecordInfoPinStatus => null;
/// <summary>UTC timestamp of the most recent heartbeat from the worker.</summary>
DateTimeOffset LastHeartbeatAt { get; }
@@ -13,6 +13,8 @@ namespace ZB.MOM.WW.MxGateway.Server.Workers;
public sealed class WorkerClient : IWorkerClient
{
private const string GatewayVersionFallback = "unknown";
// Prefix of a successful MxStatusProxyRecordInfoPin.Status in the worker; anything else is a failure.
private const string RecordInfoPinnedPrefix = "pinned ";
private static readonly TimeSpan DisposeTaskTimeout = TimeSpan.FromSeconds(5);
private readonly object _syncRoot = new();
private readonly WorkerClientConnection _connection;
@@ -55,6 +57,7 @@ public sealed class WorkerClient : IWorkerClient
private TaskCompletionSource _stateChanged = new(TaskCreationOptions.RunContinuationsAsynchronously);
private DateTimeOffset _lastHeartbeatAt;
private int? _processId;
private string? _recordInfoPinStatus;
private int _eventQueueDepth;
private int _eventsReaderClaimed;
private Task? _readLoopTask;
@@ -161,6 +164,9 @@ public sealed class WorkerClient : IWorkerClient
}
}
/// <inheritdoc />
public string? RecordInfoPinStatus => Volatile.Read(ref _recordInfoPinStatus);
/// <inheritdoc />
public DateTimeOffset LastHeartbeatAt
{
@@ -1074,6 +1080,45 @@ public sealed class WorkerClient : IWorkerClient
DateTimeOffset readyAt = _timeProvider.GetUtcNow();
DateTimeOffset launchedAt = _connection.ProcessHandle?.LaunchedAt ?? readyAt;
_metrics?.WorkerStarted(readyAt - launchedAt);
RecordRecordInfoPinStatus(ready.RecordInfoPinStatus);
}
/// <summary>
/// Records and logs the worker's <c>MXSTATUS_PROXY</c> record-info pin outcome from
/// <see cref="WorkerReady"/>.
/// </summary>
/// <remarks>
/// The worker logs the outcome too, but to its own stderr, which the gateway does not
/// capture; this is the line operators actually see. A failed pin is a warning rather than a
/// fault: MXAccess still works, but every data change then pays a type-library reload, which
/// is the dominant worker CPU cost at high event rates. An empty status means an older
/// worker, so nothing is logged.
/// </remarks>
/// <param name="status">The status string the worker reported; empty when absent.</param>
private void RecordRecordInfoPinStatus(string status)
{
if (string.IsNullOrEmpty(status))
{
return;
}
Volatile.Write(ref _recordInfoPinStatus, status);
if (status.StartsWith(RecordInfoPinnedPrefix, StringComparison.Ordinal))
{
_logger.LogInformation(
"Worker {ProcessId} for session {SessionId} pinned the MXSTATUS_PROXY record info: {RecordInfoPinStatus}",
ProcessId,
SessionId,
status);
}
else
{
_logger.LogWarning(
"Worker {ProcessId} for session {SessionId} could not pin the MXSTATUS_PROXY record info; each data change will reload the MXAccess type library: {RecordInfoPinStatus}",
ProcessId,
SessionId,
status);
}
}
/// <summary>Updates the heartbeat timestamp and process ID.</summary>