fix(galaxy): park data-change events that overtake their SubscribeBulk reply (#859)
mxaccessgw gives no reply-before-event ordering: the worker completes the STA
command via a RunContinuationsAsynchronously TCS, so the reply is framed on a
thread-pool continuation while the STA already pumps the OnDataChange Advise
queued (drained/framed by a separate loop), and the reply (unary Invoke) and
the event (StreamEvents) are separate HTTP/2 streams. The driver then also
registers handles concurrently with the pump's dispatch loop. MXAccess's
initial value could therefore be dropped as "stale after unsubscribe",
leaving the node at its pre-subscribe value until the next change.
SubscriptionRegistry gains an early-event window: every SubscribeBulk call
site (SubscribeAsync, ResubscribeAllAsync, PerPlatformProbeWatcher) opens
BeginPendingSubscribe() before the RPC and closes it after registering.
Inside the window unknown-handle events are parked (bounded, 100k); the
Register/Rebind that binds a handle promotes them, and the EventPump replays
them on its single dispatch thread before any later event for that handle
(woken by a sentinel when no further event arrives). Closing the last window
discards unclaimed events, so the stale drop is unchanged outside it.
New counters galaxy.events.early_{parked,replayed,overflow}.
Claude-Session: https://claude.ai/code/session_01GT67AD99zQQqpLCVm3p7xk
This commit is contained in:
@@ -53,6 +53,15 @@ internal sealed class EventPump : IAsyncDisposable
|
||||
private static readonly Counter<long> ValuesUndecodable =
|
||||
Meter.CreateCounter<long>("galaxy.events.undecodable", unit: "{event}",
|
||||
description: "Data-change events carrying a value kind this driver cannot represent.");
|
||||
private static readonly Counter<long> EarlyEventsParked =
|
||||
Meter.CreateCounter<long>("galaxy.events.early_parked", unit: "{event}",
|
||||
description: "Data-change events that arrived before their SubscribeBulk reply registered the item handle, parked for replay (#859).");
|
||||
private static readonly Counter<long> EarlyEventsReplayed =
|
||||
Meter.CreateCounter<long>("galaxy.events.early_replayed", unit: "{event}",
|
||||
description: "Parked early data-change events replayed once their item handle was registered (#859).");
|
||||
private static readonly Counter<long> EarlyEventsOverflowed =
|
||||
Meter.CreateCounter<long>("galaxy.events.early_overflow", unit: "{event}",
|
||||
description: "Early data-change events dropped because the parking bound was reached (#859).");
|
||||
|
||||
private readonly IGalaxySubscriber _subscriber;
|
||||
private readonly SubscriptionRegistry _registry;
|
||||
@@ -124,11 +133,28 @@ internal sealed class EventPump : IAsyncDisposable
|
||||
// value consistent).
|
||||
FullMode = BoundedChannelFullMode.Wait,
|
||||
SingleReader = true,
|
||||
SingleWriter = true,
|
||||
// Not single-writer: besides the stream read loop, a registration that promotes
|
||||
// parked early events (#859) writes the WakeSentinel from the subscribing thread.
|
||||
SingleWriter = false,
|
||||
});
|
||||
_clientTag = new KeyValuePair<string, object?>("galaxy.client", clientName ?? "<unknown>");
|
||||
_registry.ParkedEventsReady += WakeForReadyEvents;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Marker written into the channel to wake the dispatch loop after a registration promoted
|
||||
/// parked early events (#859). Never dispatched itself; compared by reference.
|
||||
/// </summary>
|
||||
private static readonly MxEvent WakeSentinel = new();
|
||||
|
||||
/// <summary>
|
||||
/// Wakes the dispatch loop so promoted early events replay even if no further event arrives.
|
||||
/// A failed write is harmless: the channel is either full (the loop is busy and replays the
|
||||
/// ready queue before its next data-change dispatch) or completed (the pump is dead, and the
|
||||
/// next pump on this registry replays them).
|
||||
/// </summary>
|
||||
private void WakeForReadyEvents() => _channel.Writer.TryWrite(WakeSentinel);
|
||||
|
||||
/// <summary>
|
||||
/// Start consuming the event stream on a background task. Idempotent — second
|
||||
/// calls are no-ops while the loop is running.
|
||||
@@ -281,6 +307,11 @@ internal sealed class EventPump : IAsyncDisposable
|
||||
{
|
||||
await foreach (var ev in _channel.Reader.ReadAllAsync(ct).ConfigureAwait(false))
|
||||
{
|
||||
if (ReferenceEquals(ev, WakeSentinel))
|
||||
{
|
||||
DispatchReadyEvents();
|
||||
continue;
|
||||
}
|
||||
Dispatch(ev);
|
||||
EventsDispatched.Add(1, _clientTag);
|
||||
}
|
||||
@@ -313,16 +344,61 @@ internal sealed class EventPump : IAsyncDisposable
|
||||
// — alarms come from the gateway's session-less StreamAlarms feed
|
||||
// (GatewayGalaxyAlarmFeed). OnWriteComplete / OperationComplete /
|
||||
// OnBufferedDataChange are filtered out: write callers get their reply
|
||||
// via the InvokeAsync round-trip, not via the event stream.
|
||||
// via the InvokeAsync round-trip, not via the event stream. Still replay any
|
||||
// promoted early events: a wake lost to a full channel relies on the next item.
|
||||
DispatchReadyEvents();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private void DispatchDataChange(MxEvent ev)
|
||||
{
|
||||
var subscribers = _registry.ResolveSubscribers(ev.ItemHandle);
|
||||
if (subscribers.Count == 0) return; // stale event after unsubscribe — drop quietly
|
||||
var outcome = _registry.ResolveOrPark(ev, out var subscribers);
|
||||
|
||||
// Replay promoted early events (#859) AFTER the resolve and BEFORE fanning this event out:
|
||||
// any registration that let this event resolve has already moved its parked (older) events
|
||||
// to the ready queue, so they reach the handler first and per-handle order is preserved.
|
||||
DispatchReadyEvents();
|
||||
|
||||
switch (outcome)
|
||||
{
|
||||
case EarlyEventOutcome.Resolved:
|
||||
break;
|
||||
case EarlyEventOutcome.Parked:
|
||||
// The handle's SubscribeBulk reply has not been registered yet — MXAccess's initial
|
||||
// value can overtake it. Replayed when the registration lands.
|
||||
EarlyEventsParked.Add(1, _clientTag);
|
||||
return;
|
||||
case EarlyEventOutcome.Overflow:
|
||||
EarlyEventsOverflowed.Add(1, _clientTag);
|
||||
return;
|
||||
default:
|
||||
return; // stale event after unsubscribe (no subscribe in flight) — drop quietly
|
||||
}
|
||||
|
||||
FanOut(ev, subscribers);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Dispatch every early event a registration promoted (#859). Runs only on the dispatch
|
||||
/// thread, so it is serialised with live dispatch. A promoted event whose subscription was
|
||||
/// removed again before the replay resolves to nobody and is dropped like any stale event.
|
||||
/// </summary>
|
||||
private void DispatchReadyEvents()
|
||||
{
|
||||
var ready = _registry.TakeReadyEvents();
|
||||
if (ready.Count == 0) return;
|
||||
foreach (var early in ready)
|
||||
{
|
||||
var subscribers = _registry.ResolveSubscribers(early.ItemHandle);
|
||||
if (subscribers.Count == 0) continue;
|
||||
EarlyEventsReplayed.Add(1, _clientTag);
|
||||
FanOut(early, subscribers);
|
||||
}
|
||||
}
|
||||
|
||||
private void FanOut(MxEvent ev, IReadOnlyList<(long SubscriptionId, string FullReference)> subscribers)
|
||||
{
|
||||
var snapshot = ToSnapshot(ev);
|
||||
foreach (var (subscriptionId, fullReference) in subscribers)
|
||||
{
|
||||
@@ -377,6 +453,7 @@ internal sealed class EventPump : IAsyncDisposable
|
||||
{
|
||||
if (_disposed) return;
|
||||
_disposed = true;
|
||||
_registry.ParkedEventsReady -= WakeForReadyEvents;
|
||||
_cts.Cancel();
|
||||
_channel.Writer.TryComplete();
|
||||
if (_loop is not null)
|
||||
|
||||
Reference in New Issue
Block a user