fix(site-runtime): move synchronous SQLite work off the Akka dispatcher (SRT-5, WP6 task 6.1)
Follows the InstanceActor static-override pattern: the storage call runs in
Task.Run, flushes chain on a persist-tail task so batches stay ordered, and
reads PipeTo(Self).
- NativeAlarmActor: FlushDirtyUpserts runs ApplyNativeAlarmChangesAsync on an
ordered persist tail; the PreStart rehydration read runs in Task.Run and
seeds the tail, so no flush can commit before the read.
- InstanceActor: the ungated PreStart override read runs in Task.Run; the
List-override normalising re-persist joins the static-override persist tail
(off-dispatcher and ordered behind/before batched flushes).
Evidence (NativeAlarmActorTests.Flush_BlockedOnWriteLock_DoesNotBlockTheActorThread,
another connection holding BEGIN IMMEDIATE for ~1.6 s, busy_timeout 5 s):
before: the next transition's emit stalled until the lock was released
(ExpectMsg 500 ms timed out; actor thread parked in the flush)
after: next transition emitted while the flush waits; both rows commit
once the lock is released.
This commit is contained in:
@@ -265,6 +265,50 @@ public class NativeAlarmActorTests : TestKit, IDisposable
|
||||
Assert.Contains(rows, r => r.ConditionJson.Contains("49"));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// SRT-5 (WP6 6.1): a persist flush that is waiting on the shared LocalDb write lock must not
|
||||
/// park the actor's dispatcher thread. While another connection holds the write lock the
|
||||
/// actor keeps processing transitions, and the blocked batch still commits (in order) once
|
||||
/// the lock is released.
|
||||
/// </summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
public async Task Flush_BlockedOnWriteLock_DoesNotBlockTheActorThread()
|
||||
{
|
||||
var instance = CreateTestProbe();
|
||||
var dcl = CreateTestProbe();
|
||||
var actor = SpawnWithFlush(instance.Ref, dcl.Ref, TimeSpan.FromMilliseconds(50));
|
||||
dcl.ExpectMsg<SubscribeAlarmsRequest>();
|
||||
|
||||
await using var locker = _localDb.Db.CreateConnection();
|
||||
await using (var begin = locker.CreateCommand())
|
||||
{
|
||||
begin.CommandText = "BEGIN IMMEDIATE;";
|
||||
await begin.ExecuteNonQueryAsync();
|
||||
}
|
||||
|
||||
var t0 = DateTimeOffset.UtcNow;
|
||||
DeliverLiveTransition(actor, "ref-a", severity: 800, t0);
|
||||
instance.ExpectMsg<AlarmStateChanged>();
|
||||
await Task.Delay(300); // the 50 ms flush has started and is waiting on the lock
|
||||
|
||||
var sw = System.Diagnostics.Stopwatch.StartNew();
|
||||
DeliverLiveTransition(actor, "ref-b", severity: 801, t0.AddMilliseconds(1));
|
||||
instance.ExpectMsg<AlarmStateChanged>(TimeSpan.FromMilliseconds(500));
|
||||
sw.Stop();
|
||||
|
||||
await using (var rollback = locker.CreateCommand())
|
||||
{
|
||||
rollback.CommandText = "ROLLBACK;";
|
||||
await rollback.ExecuteNonQueryAsync();
|
||||
}
|
||||
|
||||
Assert.True(sw.ElapsedMilliseconds < 500, $"actor stalled {sw.ElapsedMilliseconds} ms behind a blocked flush");
|
||||
await AwaitAssertAsync(async () =>
|
||||
Assert.Equal(2, (await _storage.GetNativeAlarmsAsync("inst", Source().CanonicalName)).Count),
|
||||
TimeSpan.FromSeconds(5));
|
||||
}
|
||||
|
||||
/// <summary>A snapshot swap that drops several conditions deletes their rows via the batched flush, and a delete cancels a pending upsert (review 2026-10-21 01/SR-P2).</summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
|
||||
Reference in New Issue
Block a user