fix(auditlog): page the site audit purge, bound incremental_vacuum per page, fail fast instead of parking writers on a full queue (review 2026-10-21 06/MEM-1)
This commit is contained in:
@@ -38,9 +38,15 @@ public class SqliteAuditWriterRetentionTests : IDisposable
|
||||
}
|
||||
}
|
||||
|
||||
private SqliteAuditWriter CreateWriter()
|
||||
private SqliteAuditWriter CreateWriter(int purgeBatchSize = 5000, int channelCapacity = 4096, int batchSize = 256)
|
||||
{
|
||||
var options = new SqliteAuditWriterOptions { DatabasePath = _dbPath };
|
||||
var options = new SqliteAuditWriterOptions
|
||||
{
|
||||
DatabasePath = _dbPath,
|
||||
PurgeBatchSize = purgeBatchSize,
|
||||
ChannelCapacity = channelCapacity,
|
||||
BatchSize = batchSize,
|
||||
};
|
||||
return new SqliteAuditWriter(
|
||||
Options.Create(options),
|
||||
NullLogger<SqliteAuditWriter>.Instance,
|
||||
@@ -95,4 +101,74 @@ public class SqliteAuditWriterRetentionTests : IDisposable
|
||||
Assert.Equal(1, await writer.PurgeExpiredAsync(DateTime.UtcNow.AddDays(-7)));
|
||||
Assert.Equal(0, await writer.PurgeExpiredAsync(DateTime.UtcNow.AddDays(-7)));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Review 2026-10-21 06/MEM-1: the purge deletes in bounded pages (one transaction
|
||||
/// each, write lock released between them) and still removes every eligible row.
|
||||
/// </summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
public async Task PurgeExpired_IsPaged_AndStillRemovesEveryEligibleRow()
|
||||
{
|
||||
await using var writer = CreateWriter(purgeBatchSize: 10);
|
||||
var ids = new List<Guid>();
|
||||
for (var i = 0; i < 35; i++)
|
||||
{
|
||||
ids.Add(await WriteEventAsync(writer, DateTime.UtcNow.AddDays(-10)));
|
||||
}
|
||||
var pendingId = await WriteEventAsync(writer, DateTime.UtcNow.AddDays(-10));
|
||||
await writer.MarkForwardedAsync(ids);
|
||||
|
||||
var purged = await writer.PurgeExpiredAsync(DateTime.UtcNow.AddDays(-7));
|
||||
|
||||
Assert.Equal(35, purged);
|
||||
Assert.Equal(4, writer.PurgePageCountForTests); // 10 + 10 + 10 + 5
|
||||
var pending = await writer.ReadPendingAsync(10);
|
||||
Assert.Equal(pendingId, Assert.Single(pending).EventId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Review 2026-10-21 06/MEM-1: with the background writer parked (as behind a long purge
|
||||
/// page) and the bounded queue full, <c>WriteAsync</c> must fail fast with
|
||||
/// <see cref="SqliteAuditWriterSaturatedException"/> instead of parking a waiter that
|
||||
/// holds its event for as long as the writer stays stuck.
|
||||
/// </summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
public async Task WriteAsync_QueueFull_FailsFast_InsteadOfParking()
|
||||
{
|
||||
await using var writer = CreateWriter(channelCapacity: 1, batchSize: 1);
|
||||
Task first, third;
|
||||
System.Threading.Monitor.Enter(writer.WriteLockForTests);
|
||||
try
|
||||
{
|
||||
first = writer.WriteAsync(NewEvent());
|
||||
// Wait until the writer loop has dequeued `first` and is blocked on the lock.
|
||||
Assert.True(SpinWait.SpinUntil(() =>
|
||||
{
|
||||
var probe = writer.WriteAsync(NewEvent());
|
||||
if (probe.IsFaulted) { probe.Exception!.Handle(_ => true); return false; }
|
||||
return true;
|
||||
}, TimeSpan.FromSeconds(5)));
|
||||
// The queue (capacity 1) now holds the probe; the next write cannot fit.
|
||||
third = writer.WriteAsync(NewEvent());
|
||||
Assert.True(third.IsFaulted, "a full queue must fault immediately, not park");
|
||||
Assert.IsType<SqliteAuditWriterSaturatedException>(third.Exception!.InnerException);
|
||||
Assert.True(writer.SaturatedRejectionCount >= 1);
|
||||
}
|
||||
finally
|
||||
{
|
||||
System.Threading.Monitor.Exit(writer.WriteLockForTests);
|
||||
}
|
||||
|
||||
// Lock released: the parked writer resumes and the accepted event lands.
|
||||
await first.WaitAsync(TimeSpan.FromSeconds(5));
|
||||
}
|
||||
|
||||
private static AuditEvent NewEvent() => ScadaBridgeAuditEventFactory.Create(
|
||||
eventId: Guid.NewGuid(),
|
||||
occurredAtUtc: DateTime.UtcNow,
|
||||
channel: AuditChannel.ApiOutbound,
|
||||
kind: AuditKind.ApiCall,
|
||||
status: AuditStatus.Delivered);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user