fix(audit): purge/archive also cover partition 1 (rows before the first boundary) (AUD-6, WP4 task 4.11)
This commit is contained in:
@@ -730,6 +730,15 @@ MS SQL for direct-write events). Unredacted secrets never persist.
|
||||
and emits an `AuditLogPurgedEvent` (partition range, rowcount, duration) per
|
||||
switched partition. A partition-maintenance step rolls forward each month,
|
||||
creating the next month's partition ahead of time.
|
||||
- **Partition 1 is purged and archived too (AUD-6, 2026-12).** Rows stamped before
|
||||
the first boundary (a site whose clock was reset, a back-dated row) land in
|
||||
partition 1, which has no lower boundary, so the per-boundary enumeration never
|
||||
offered it and those rows outlived retention and the cold archive indefinitely.
|
||||
`GetPartitionBoundariesOlderThanAsync` now also returns partition 1 — once its
|
||||
newest row is older than the window — as the **month start of its newest row**, a
|
||||
value inside partition 1: the switch-out and the archive resolve the partition
|
||||
with `$partition.pf_AuditLog_Month(value)`, so they handle it like any month, and
|
||||
the archive is labelled (and its `RetentionDays` counted) by the age of that data.
|
||||
- **Purge failure is a health metric, not just a log line:** every purge step that
|
||||
throws — a partition switch-out, a per-channel override DELETE, or the pre-purge
|
||||
boundary enumeration — publishes an `AuditLogPurgeFailedEvent` (month boundary,
|
||||
|
||||
@@ -203,6 +203,13 @@ public interface IAuditLogRepository
|
||||
/// <paramref name="threshold"/>. Boundaries whose partition is empty are
|
||||
/// excluded (a no-op switch is wasted work). Used by the purge actor
|
||||
/// to enumerate retention-eligible months on every tick.
|
||||
/// <para>
|
||||
/// Partition 1 — everything before the first boundary, which has no lower boundary of its
|
||||
/// own — is included too (AUD-6): it is reported as the month start of its newest row, a
|
||||
/// value that lies inside partition 1, so <see cref="SwitchOutPartitionAsync"/> and the cold
|
||||
/// archive (both resolve the partition with <c>$partition.pf_AuditLog_Month(value)</c>)
|
||||
/// purge and archive it like any other month.
|
||||
/// </para>
|
||||
/// </summary>
|
||||
/// <param name="threshold">Only partitions whose data is entirely older than this UTC datetime are returned.</param>
|
||||
/// <param name="ct">Cancellation token.</param>
|
||||
|
||||
@@ -827,7 +827,23 @@ VALUES
|
||||
) x
|
||||
WHERE x.MaxOccurredAt IS NOT NULL
|
||||
AND x.MaxOccurredAt < CAST('{thresholdStr}' AS datetime2(7))
|
||||
ORDER BY b.BoundaryValue;";
|
||||
UNION ALL
|
||||
-- AUD-6: partition 1 (everything before the FIRST boundary — back-dated rows from a
|
||||
-- site whose clock was reset, or test rows) has no boundary of its own, so the
|
||||
-- per-boundary enumeration above never offered it and its rows outlived retention
|
||||
-- and the cold archive forever. It is identified by a stand-in value inside it: the
|
||||
-- month start of its newest row (always below the first boundary, so
|
||||
-- $partition.pf_AuditLog_Month maps it to partition 1 for the switch-out and the
|
||||
-- archive, and it labels the archive by the age of the data).
|
||||
SELECT CAST(DATEFROMPARTS(YEAR(p1.MaxOccurredAt), MONTH(p1.MaxOccurredAt), 1) AS datetime2(7))
|
||||
FROM (
|
||||
SELECT MAX(a.OccurredAtUtc) AS MaxOccurredAt
|
||||
FROM dbo.AuditLog a
|
||||
WHERE $PARTITION.pf_AuditLog_Month(a.OccurredAtUtc) = 1
|
||||
) p1
|
||||
WHERE p1.MaxOccurredAt IS NOT NULL
|
||||
AND p1.MaxOccurredAt < CAST('{thresholdStr}' AS datetime2(7))
|
||||
ORDER BY 1;";
|
||||
|
||||
var conn = _context.Database.GetDbConnection();
|
||||
var openedHere = false;
|
||||
|
||||
+48
@@ -1209,6 +1209,54 @@ public class AuditLogRepositoryTests : IClassFixture<MsSqlMigrationFixture>
|
||||
Assert.DoesNotContain(new DateTime(2026, 8, 1, 0, 0, 0, DateTimeKind.Utc), boundaries);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// AUD-6 regression: rows stamped before the FIRST partition boundary (partition 1 — a site
|
||||
/// with a reset clock, a back-dated row) used to be invisible to the purge enumeration, so
|
||||
/// they outlived retention and the archive forever. Partition 1 is now offered as the month
|
||||
/// start of its newest row, and switching that value out purges exactly partition 1.
|
||||
/// </summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[SkippableFact]
|
||||
public async Task GetPartitionBoundariesOlderThanAsync_IncludesPartitionOne_AndItSwitchesOut()
|
||||
{
|
||||
Skip.IfNot(_fixture.Available, _fixture.SkipReason);
|
||||
|
||||
var siteId = NewSiteId();
|
||||
await using var context = CreateContext();
|
||||
var repo = new AuditLogRepository(context);
|
||||
|
||||
// Two rows before the first boundary (2026-01-01) and one inside a regular partition.
|
||||
await repo.InsertIfNotExistsAsync(NewEvent(siteId, occurredAtUtc: new DateTime(2019, 3, 10, 0, 0, 0, DateTimeKind.Utc)));
|
||||
await repo.InsertIfNotExistsAsync(NewEvent(siteId, occurredAtUtc: new DateTime(2019, 5, 15, 12, 0, 0, DateTimeKind.Utc)));
|
||||
var survivor = NewEvent(siteId, occurredAtUtc: new DateTime(2026, 7, 12, 0, 0, 0, DateTimeKind.Utc));
|
||||
await repo.InsertIfNotExistsAsync(survivor);
|
||||
|
||||
var boundaries = await repo.GetPartitionBoundariesOlderThanAsync(new DateTime(2026, 7, 1, 0, 0, 0, DateTimeKind.Utc));
|
||||
|
||||
// Partition 1 is offered as the month start of its newest row.
|
||||
var partitionOne = new DateTime(2019, 5, 1, 0, 0, 0, DateTimeKind.Utc);
|
||||
Assert.Contains(partitionOne, boundaries);
|
||||
Assert.Equal(partitionOne, boundaries[0]);
|
||||
|
||||
var purged = await repo.SwitchOutPartitionAsync(partitionOne);
|
||||
Assert.True(purged >= 2, $"expected the two partition-1 rows to be purged, got {purged}");
|
||||
|
||||
await using var read = CreateContext();
|
||||
var left = await read.Database
|
||||
.SqlQueryRaw<int>("SELECT COUNT(*) AS Value FROM dbo.AuditLog WHERE OccurredAtUtc < '2026-01-01'")
|
||||
.SingleAsync();
|
||||
Assert.Equal(0, left);
|
||||
var survivorId = survivor.EventId;
|
||||
var survived = await read.Database
|
||||
.SqlQuery<int>($"SELECT COUNT(*) AS Value FROM dbo.AuditLog WHERE EventId = {survivorId}")
|
||||
.SingleAsync();
|
||||
Assert.Equal(1, survived);
|
||||
|
||||
// Nothing is left in partition 1, so it is no longer offered.
|
||||
var after = await repo.GetPartitionBoundariesOlderThanAsync(new DateTime(2026, 7, 1, 0, 0, 0, DateTimeKind.Utc));
|
||||
Assert.DoesNotContain(after, b => b < new DateTime(2026, 1, 1, 0, 0, 0, DateTimeKind.Utc));
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
// GetKpiSnapshotAsync — Health-dashboard Audit KPI tiles
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user