fix(controlplane): audit writer drops only the rejected row, not the whole batch
AuditWriterActor documented per-row dedup on the UX_ConfigAuditLog_EventId index but flushed one statement batch: a single duplicate EventId (any at-least-once producer across a singleton hand-over) threw for all 500 rows (R03:2.6). SaveChanges now detaches the entries a DbUpdateException names (2601/2627 logged at Debug as the expected duplicate, anything else at Warning) and re-saves, bounded by the batch size; a failure naming no entry stays a whole-batch failure. Refs #733 Claude-Session: https://claude.ai/code/session_01Ub9hGmYLQvn9XETzw7CMsT
This commit is contained in:
@@ -17,8 +17,8 @@ namespace ZB.MOM.WW.OtOpcUa.ControlPlane.Audit;
|
||||
/// Dedup is two-layer: in-buffer (the <see cref="Dictionary{TKey, TValue}"/> below collapses
|
||||
/// duplicate EventIds before flush) and at the database via the filtered unique index
|
||||
/// <c>UX_ConfigAuditLog_EventId</c> (cross-restart safety — a retry of an already-flushed
|
||||
/// batch hits the constraint and we drop the duplicate insert without losing the rest of
|
||||
/// the batch).
|
||||
/// batch hits the constraint; <see cref="SaveDetachingRejectedRows"/> detaches the rejected row
|
||||
/// and re-saves, so the duplicate is dropped without losing the rest of the batch).
|
||||
///
|
||||
/// Implements the shared <see cref="IAuditWriter"/> seam: <see cref="WriteAsync"/> is a
|
||||
/// best-effort, never-throwing entry point that simply <c>Tell</c>s this actor and returns
|
||||
@@ -107,8 +107,8 @@ public sealed class AuditWriterActor : ReceiveActor, IWithTimers, IAuditWriter
|
||||
Outcome = evt.Outcome.ToString(),
|
||||
});
|
||||
}
|
||||
db.SaveChanges();
|
||||
_log.Debug("AuditWriter flushed {Count} events", snapshot.Count);
|
||||
var dropped = SaveDetachingRejectedRows(db);
|
||||
_log.Debug("AuditWriter flushed {Count} events ({Dropped} rejected rows dropped)", snapshot.Count - dropped, dropped);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -116,6 +116,55 @@ public sealed class AuditWriterActor : ReceiveActor, IWithTimers, IAuditWriter
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves the batch, detaching and retrying past any row the database rejects (R03:2.6). One
|
||||
/// statement batch used to carry the whole flush: a single duplicate <c>EventId</c> (any at-least-once
|
||||
/// producer — the audit seam is Tell-based through a singleton proxy that re-delivers across a
|
||||
/// hand-over) threw for all 500 rows and the class remarks' per-row dedup claim was not what ran.
|
||||
/// </summary>
|
||||
/// <param name="db">The context holding the batch.</param>
|
||||
/// <returns>How many rows were dropped.</returns>
|
||||
/// <remarks>
|
||||
/// Only a <see cref="DbUpdateException"/> that NAMES its entries is handled here — that is the shape a
|
||||
/// constraint violation takes (SQL 2601/2627 for the unique index). A failure with no entries (the
|
||||
/// connection died, a deadlock) is the whole-batch failure it always was and propagates to the
|
||||
/// caller's catch. Bounded by the batch size so a store that rejects every row still terminates.
|
||||
/// </remarks>
|
||||
private int SaveDetachingRejectedRows(OtOpcUaConfigDbContext db)
|
||||
{
|
||||
var dropped = 0;
|
||||
var attempts = 0;
|
||||
var maxAttempts = db.ChangeTracker.Entries<ConfigAuditLog>().Count() + 1;
|
||||
while (true)
|
||||
{
|
||||
try
|
||||
{
|
||||
db.SaveChanges();
|
||||
return dropped;
|
||||
}
|
||||
catch (DbUpdateException ex) when (ex.Entries.Count > 0 && ++attempts < maxAttempts)
|
||||
{
|
||||
var duplicate = IsUniqueViolation(ex);
|
||||
foreach (var entry in ex.Entries)
|
||||
{
|
||||
var eventId = (entry.Entity as ConfigAuditLog)?.EventId;
|
||||
if (duplicate)
|
||||
_log.Debug("AuditWriter: EventId {EventId} already persisted (unique index); dropping the duplicate", eventId);
|
||||
else
|
||||
_log.Warning(ex, "AuditWriter: the database rejected EventId {EventId}; dropping that row and keeping the batch", eventId);
|
||||
entry.State = EntityState.Detached;
|
||||
dropped++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>SQL Server 2601 (unique index) / 2627 (unique constraint), the dedup contract's expected rejection.</summary>
|
||||
/// <param name="ex">The update failure.</param>
|
||||
/// <returns><see langword="true"/> for a duplicate-key rejection.</returns>
|
||||
private static bool IsUniqueViolation(DbUpdateException ex) =>
|
||||
ex.InnerException is Microsoft.Data.SqlClient.SqlException { Number: 2601 or 2627 };
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void PreRestart(Exception reason, object message)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user