fix(sessions): drop per-session event metrics after the session dispose (WRK-008, #253)
This commit is contained in:
+1
-1
@@ -106,7 +106,7 @@ The scalar fields mirror the counters and gauges. The three dictionaries provide
|
||||
|
||||
- `CommandFailuresByMethod` keys by gRPC method name.
|
||||
- `EventsByFamily` keys by event family (the `Family` enum on a worker event).
|
||||
- `EventsBySession` keys by `sessionId`; entries are removed via `RemoveSessionEvents` when a session closes so the map does not grow without bound.
|
||||
- `EventsBySession` keys by `sessionId`; entries are removed via `RemoveSessionEvents` when a session closes so the map does not grow without bound. The removal runs after the session (and with it the worker client, whose dispose waits for its read loop) is disposed: on the kill path the read loop is cancelled but not awaited, and a frame it had already read would otherwise re-create the entry after the removal.
|
||||
|
||||
`EventsReceived` is read with `Interlocked.Read(ref _eventsReceived)` because `EventReceived` increments it via `Interlocked.Increment` outside the lock to keep the event-ingestion path non-blocking. `CommandsStarted`, `CommandsSucceeded`, `CommandsFailed`, and `CommandFailuresByMethod` are read the same way: the command counters run two-to-three times per gRPC call, so they are recorded with `Interlocked` and a `ConcurrentDictionary` rather than under `_syncRoot` (PERF-02). The two queue depths are pulled from their registered sources before the lock is taken, since those delegates reach into subscriber channels and worker clients.
|
||||
|
||||
|
||||
+1
-1
@@ -196,7 +196,7 @@ catch (Exception exception)
|
||||
}
|
||||
```
|
||||
|
||||
`RollBackOpenAsync` faults the session and, when it had been registered, routes it through `RemoveSessionAsync` — the single removal path every teardown uses, which deregisters, drops the session's per-session event metrics, releases the slot and disposes, in that order. The order matters because releasing the semaphore before disposal would let the next open race the worker process tear-down on the same machine. A session that was never registered (id collision) releases the slot and disposes directly. The open-session gauge is not touched by the rollback: `SessionOpened` is recorded inside the attach gate described next, after which nothing in the open sequence can fail.
|
||||
`RollBackOpenAsync` faults the session and, when it had been registered, routes it through `RemoveSessionAsync` — the single removal path every teardown uses, which deregisters, releases the slot, disposes, and then drops the session's per-session event metrics (after the dispose, so a worker read loop still finishing a frame cannot re-create the entry). The order matters because releasing the semaphore before disposal would let the next open race the worker process tear-down on the same machine. A session that was never registered (id collision) releases the slot and disposes directly. The open-session gauge is not touched by the rollback: `SessionOpened` is recorded inside the attach gate described next, after which nothing in the open sequence can fail.
|
||||
|
||||
#### Close or kill during worker startup
|
||||
|
||||
|
||||
@@ -746,9 +746,20 @@ public sealed class SessionManager : ISessionManager
|
||||
return;
|
||||
}
|
||||
|
||||
_metrics.RemoveSessionEvents(session.SessionId);
|
||||
ReleaseSessionSlot();
|
||||
await removedSession.DisposeAsync().ConfigureAwait(false);
|
||||
try
|
||||
{
|
||||
await removedSession.DisposeAsync().ConfigureAwait(false);
|
||||
}
|
||||
finally
|
||||
{
|
||||
// After the dispose, not before (WRK-008): on the kill path the worker client's read
|
||||
// loop is cancelled but not awaited, so it can still be inside DispatchEnvelope for a
|
||||
// frame it already read, and its EventReceived would re-create the per-session entry
|
||||
// that nothing removes again. Disposing the session disposes its worker client, which
|
||||
// waits (bounded) for that loop to stop.
|
||||
_metrics.RemoveSessionEvents(session.SessionId);
|
||||
}
|
||||
}
|
||||
|
||||
private void ReleaseSessionSlot()
|
||||
|
||||
@@ -1939,6 +1939,28 @@ public sealed class SessionManagerTests
|
||||
Assert.Equal(0, metrics.GetSnapshot().OpenSessions);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// WRK-008: a worker read loop that records one more event while the killed session's
|
||||
/// worker client is being disposed must not leave a per-session metrics entry behind; the
|
||||
/// entry is dropped after the dispose.
|
||||
/// </summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
public async Task KillWorkerAsync_EventRecordedDuringWorkerDispose_LeavesNoPerSessionEntry()
|
||||
{
|
||||
using GatewayMetrics metrics = new();
|
||||
string? sessionId = null;
|
||||
FakeWorkerClient workerClient = new() { OnDispose = () => metrics.EventReceived(sessionId!, "OnDataChange") };
|
||||
SessionManager manager = CreateManager(new FakeSessionWorkerClientFactory(workerClient), metrics: metrics);
|
||||
GatewaySession session = await manager.OpenSessionAsync(CreateOpenRequest(), "client-1", ownerKeyId: null, CancellationToken.None);
|
||||
sessionId = session.SessionId;
|
||||
|
||||
await manager.KillWorkerAsync(session.SessionId, "test-kill", CancellationToken.None);
|
||||
|
||||
Assert.Equal(1, workerClient.DisposeCount);
|
||||
Assert.False(metrics.GetSnapshot().EventsBySession.ContainsKey(session.SessionId));
|
||||
}
|
||||
|
||||
/// <summary>Creates a session manager for testing.</summary>
|
||||
/// <param name="factory">Worker client factory.</param>
|
||||
/// <param name="registry">Session registry; defaults to a new registry.</param>
|
||||
@@ -2467,9 +2489,16 @@ public sealed class SessionManagerTests
|
||||
public ValueTask DisposeAsync()
|
||||
{
|
||||
DisposeCount++;
|
||||
OnDispose?.Invoke();
|
||||
return ValueTask.CompletedTask;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a hook run inside <see cref="DisposeAsync"/>, standing in for a worker read loop
|
||||
/// that is still finishing a frame while the client is disposed.
|
||||
/// </summary>
|
||||
public Action? OnDispose { get; init; }
|
||||
|
||||
/// <summary>Waits for shutdown to start on the fake worker client.</summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
public Task WaitForShutdownStartAsync()
|
||||
|
||||
Reference in New Issue
Block a user