perf(historian): scan the historization outbox outside its state lock (lmxopcua#746)
FasterLogHistorizationOutbox.PeekBatchAsync held the state lock across the FasterLog scan. Once a backlog's head ages out of FasterLog's in-memory window (any multi-minute historian outage), GetNext reads pages from disk synchronously, and the recorder's appends (which index under the same lock) blocked on an Akka dispatcher thread once per drain pass, exactly during outage recovery. The peek now snapshots the head under the lock, scans unlocked, and re-takes the lock only when it met undecodable records, to reconcile them with the index in O(records scanned) and drop one still at the head (re-checked, since an ack or drop-oldest may have moved it). A test seam pauses the scan and proves an append completes meanwhile; wrapping the scan in the lock again fails it. Rebased from the unpushed remed/lane-H2 branch (originally authored 2026-10-22 in session_01Ub9hGmYLQvn9XETzw7CMsT) onto current master. Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
This commit is contained in:
+27
@@ -27,6 +27,33 @@ public sealed class FasterLogHistorizationOutboxTests : IDisposable
|
||||
private static HistorizationOutboxEntry E(string tag, double v) =>
|
||||
new(Guid.NewGuid(), tag, HistorianValue.FromDouble(v), 0u, new DateTime(2026, 1, 1, 0, 0, 0, DateTimeKind.Utc));
|
||||
|
||||
[Fact]
|
||||
public async Task A_peek_scan_does_not_hold_the_state_lock_so_appends_proceed_meanwhile()
|
||||
{
|
||||
// The scan may read pages from disk (a backlog aged out of the in-memory window); holding the state lock across it
|
||||
// blocked the recorder's appends on an Akka dispatcher thread.
|
||||
var dir = NewTempDir();
|
||||
using var o = new FasterLogHistorizationOutbox(dir, HistorizationCommitMode.PerEntry);
|
||||
for (var i = 0; i < 5; i++) await o.AppendAsync(E("A", i), TestContext.Current.CancellationToken);
|
||||
|
||||
bool? appendCompletedDuringScan = null;
|
||||
o.ScanStepForTests = () =>
|
||||
{
|
||||
if (appendCompletedDuringScan is not null) return;
|
||||
var append = Task.Run(async () => await o.AppendAsync(E("B", 99), CancellationToken.None));
|
||||
appendCompletedDuringScan = append.Wait(TimeSpan.FromSeconds(5));
|
||||
};
|
||||
|
||||
var batch = await o.PeekBatchAsync(10, TestContext.Current.CancellationToken);
|
||||
|
||||
Assert.True(appendCompletedDuringScan, "an append must not wait for a peek's scan");
|
||||
Assert.Equal(5, batch.Count(e => e.Tag == "A"));
|
||||
Assert.Equal(6, await o.CountAsync(TestContext.Current.CancellationToken));
|
||||
// The concurrent append is FIFO-tracked: the next peek returns it after the five.
|
||||
o.ScanStepForTests = null;
|
||||
Assert.Equal("B", (await o.PeekBatchAsync(10, TestContext.Current.CancellationToken))[^1].Tag);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Append_then_peek_returns_fifo()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user