fix(historian): incremental batch-append bookkeeping; genuine group-commit durability pin

This commit is contained in:
Joseph Doherty
2026-08-16 04:18:38 -04:00
parent fbe8aaad9d
commit 7320586512
3 changed files with 98 additions and 14 deletions
@@ -1,4 +1,5 @@
using System.Linq;
using FASTER.core;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
using ZB.MOM.WW.OtOpcUa.Driver.Historian.Gateway.Recorder;
@@ -143,8 +144,11 @@ public sealed class FasterLogHistorizationOutboxTests : IDisposable
[Fact]
public async Task AppendBatch_then_peek_returns_fifo_in_batch_order()
{
// Group commit: one fsync for the whole batch. The reopen leg is the load-bearing half — it
// proves the single per-batch commit actually made every entry durable, not just the last one.
// Group commit: one fsync for the whole batch, and FIFO order is preserved across a restart.
// The reopen leg here proves ORDER and COUNT survive recovery — it deliberately does NOT
// prove the batch committed itself, because Dispose() unconditionally runs
// _log.Commit(spinWait: true) and would rescue a batch that never committed. That claim is
// pinned separately by AppendBatch_group_commit_is_durable_before_dispose.
var dir = NewTempDir();
var a = E("A", 1);
var b = E("B", 2);
@@ -164,6 +168,51 @@ public sealed class FasterLogHistorizationOutboxTests : IDisposable
Assert.Equal(new[] { a.Id, b.Id, c.Id }, survived.Select(e => e.Id));
}
[Fact]
public async Task AppendBatch_group_commit_is_durable_before_dispose()
{
// THE group-commit pin. Every other durability test disposes the writer before reopening, and
// Dispose() unconditionally commits — so an AppendBatchAsync that never called CommitAsync
// would still pass them. This one never disposes the writer before observing durability:
// it opens a SECOND, read-only FasterLog over the same directory while the writing outbox is
// still alive, and reads only up to CommittedUntilAddress. Anything visible there was made
// durable by AppendBatchAsync's own group commit and nothing else. (PerEntry mode, so no
// periodic-commit loop exists to commit behind the batch's back either.)
var dir = NewTempDir();
var a = E("A", 1);
var b = E("B", 2);
var c = E("C", 3);
var writer = new FasterLogHistorizationOutbox(dir, HistorizationCommitMode.PerEntry);
try
{
await writer.AppendBatchAsync([a, b, c], TestContext.Current.CancellationToken);
Assert.Equal(new[] { a.Id, b.Id, c.Id }, ReadCommitted(dir));
}
finally
{
writer.Dispose();
}
}
// Opens an independent read-only FasterLog over the outbox's directory and returns the ids of
// every record the writer has COMMITTED (BeginAddress .. CommittedUntilAddress). Uncommitted
// enqueues sit past CommittedUntilAddress and are therefore invisible here — which is exactly
// what makes this a durability observation rather than a memory one.
private static Guid[] ReadCommitted(string dir)
{
using var device = new ManagedLocalStorageDevice(Path.Combine(dir, "hlog.log"));
using var reader = new FasterLog(new FasterLogSettings { LogDevice = device, ReadOnlyMode = true });
var ids = new List<Guid>();
using FasterLogScanIterator iter = reader.Scan(reader.BeginAddress, reader.CommittedUntilAddress, recover: false);
while (iter.GetNext(out byte[] bytes, out int len, out _, out _))
ids.Add(HistorizationOutboxEntrySerializer.Deserialize(bytes.AsSpan(0, len)).Id);
return ids.ToArray();
}
[Fact]
public async Task AppendBatch_at_capacity_drops_oldest_across_the_whole_batch()
{