fix(alarms): #937 review r1 — revert a refused scripted method, broadcast to the peer only after the local verdict, settle a batch per request, in-flight cap
1. A post-dispatch refusal now puts the node back to its captured pre-call state with one corrective event (skipped when the node changed since) — the SDK had already applied and reported the change. Blank scripted AddComment is refused in the handler before the SDK runs. 2. ScriptedAlarmCommandDispatch: ask the local engine first, broadcast to the peer only on Applied/OutcomeUnknown — a refusal now reached no engine anywhere. 3. ApplyAsync no longer checks 'loaded' before _evalGate: a command racing the first load queues behind it; never loaded => refusal (nothing applied). 4. One Call's requests are settled sequentially (no request runs against a node state the engine refused); <= 64 asks in flight node-wide, else BadTooManyOperations before anything applies; per-request bound starts at send. Measured 300 acks through one host + LocalDb: 59 ms sequential, max 24 ms/ask with 64 outstanding. 5. Mixed-batch wire test (scripted refused + native + imported + scripted applied + core-manager method): per-item status in order, one scripted ask at a time. L6: readiness from the engine's HasCompleteGeneration on a load fault. L7: the verdict slot is closed when its SDK call returns (timer-captured AsyncLocal can no longer park an ask). L10: remote budget never stretched. Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -215,7 +215,6 @@ public sealed class ScriptedAlarmEngine : IDisposable
|
||||
private readonly List<IDisposable> _upstreamSubscriptions = [];
|
||||
private readonly SemaphoreSlim _evalGate = new(1, 1);
|
||||
private Timer? _shelvingTimer;
|
||||
private bool _loaded;
|
||||
|
||||
/// <summary>
|
||||
/// #935: whether <see cref="_alarms"/> holds a COMPLETED generation — set at the end of a successful
|
||||
@@ -230,6 +229,12 @@ public sealed class ScriptedAlarmEngine : IDisposable
|
||||
/// </summary>
|
||||
private bool _generationComplete;
|
||||
|
||||
/// <summary>
|
||||
/// #937: whether the engine currently holds a COMPLETED generation (operator commands are accepted). A lock-free
|
||||
/// read of the flag <c>_evalGate</c> guards: false while a load is in flight and after a failed one.
|
||||
/// </summary>
|
||||
public bool HasCompleteGeneration => Volatile.Read(ref _generationComplete);
|
||||
|
||||
// volatile: written by Dispose and read unsynchronized from every background
|
||||
// path (LoadAsync's guard, OnUpstreamChange, the drain loop, ReevaluateAsync /
|
||||
// ShelvingCheckAsync's post-gate re-checks, TrackBackgroundTask's registration
|
||||
@@ -402,7 +407,7 @@ public sealed class ScriptedAlarmEngine : IDisposable
|
||||
carryOver = new Dictionary<string, AlarmConditionState>(_alarms.Count, StringComparer.Ordinal);
|
||||
foreach (var (alarmId, prior) in _alarms) carryOver[alarmId] = prior.Condition;
|
||||
}
|
||||
_generationComplete = false;
|
||||
Volatile.Write(ref _generationComplete, false);
|
||||
|
||||
UnsubscribeFromUpstream();
|
||||
_alarms.Clear();
|
||||
@@ -484,9 +489,9 @@ public sealed class ScriptedAlarmEngine : IDisposable
|
||||
// Seed the value cache with current tag values before subscribing. The
|
||||
// ReadTag calls happen first so that the initial predicate evaluation below
|
||||
// (startup recovery) uses a consistent snapshot.
|
||||
// Subscriptions are established AFTER _loaded = true so that any synchronous
|
||||
// Subscriptions are established AFTER the generation completes so that any synchronous
|
||||
// initial-push an ITagUpstreamSource delivers from inside SubscribeTag arrives
|
||||
// when _alarms is fully initialised. Before _loaded = true, a synchronous push
|
||||
// when _alarms is fully initialised. Before then, a synchronous push
|
||||
// would race the in-progress state restore and could overwrite the carefully
|
||||
// seeded cache with a push that has no defined ordering relative to ReadTag.
|
||||
foreach (var path in _alarmsReferencing.Keys)
|
||||
@@ -531,10 +536,9 @@ public sealed class ScriptedAlarmEngine : IDisposable
|
||||
}
|
||||
}
|
||||
|
||||
_loaded = true;
|
||||
_generationComplete = true;
|
||||
Volatile.Write(ref _generationComplete, true);
|
||||
|
||||
// Subscribe after _loaded = true and full state restore. If an upstream
|
||||
// Subscribe after the generation completed and full state restore. If an upstream
|
||||
// implementation pushes its initial value synchronously from inside
|
||||
// SubscribeTag, OnUpstreamChange will queue a ReevaluateAsync that acquires
|
||||
// _evalGate — it will correctly block until LoadAsync releases the gate, then
|
||||
@@ -740,12 +744,13 @@ public sealed class ScriptedAlarmEngine : IDisposable
|
||||
|
||||
private async Task ApplyAsync(string alarmId, CancellationToken ct, Func<AlarmConditionState, TransitionResult> op)
|
||||
{
|
||||
EnsureLoaded();
|
||||
|
||||
ScriptedAlarmEvent? pending = null;
|
||||
await _evalGate.WaitAsync(ct).ConfigureAwait(false);
|
||||
try
|
||||
{
|
||||
// #937 review: no separate "not loaded" check before the gate any more — a command racing the FIRST load used
|
||||
// to throw InvalidOperationException there (the host could only classify it as unknown). It now queues behind
|
||||
// the load; never loaded ⇒ no completed generation ⇒ the refusal below (nothing applied).
|
||||
// #935 review S1: look the alarm up only once the gate is held. Looked up before it, a command racing a
|
||||
// LoadAsync captured either the old generation's entry (and wrote its state over the new one) or the
|
||||
// load's interim Fresh entry (and, an ack being a no-op on Fresh, saved Fresh over the carried Active
|
||||
@@ -1784,12 +1789,6 @@ public sealed class ScriptedAlarmEngine : IDisposable
|
||||
_upstreamSubscriptions.Clear();
|
||||
}
|
||||
|
||||
private void EnsureLoaded()
|
||||
{
|
||||
if (!_loaded) throw new InvalidOperationException(
|
||||
"ScriptedAlarmEngine not loaded. Call LoadAsync first.");
|
||||
}
|
||||
|
||||
/// <summary>Disposes the engine, cleaning up resources and waiting for in-flight background tasks.</summary>
|
||||
public void Dispose()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user