feat(audit,install): #1030 audit overflow drops exported as otopcua.audit.dropped; scripted SCM-restart-after-cluster-exit gate

- AuditWriterActor: every buffer-full drop is added to OtOpcUaTelemetry.AuditDropped
  (otopcua.audit.dropped{reason=buffer_full, actor}) — the count used to be readable only
  through the actor's own property.
- scripts/install/tests/Test-ScmRestartAfterClusterExit.Windows.ps1: the windev manual gate
  of 2026-12-03 as a script (qfailure restart action; failover/command trigger => non-zero exit,
  7031, Running under a new pid; Stop-Service stays stopped; optional kill control). Parse-checked
  with pwsh on macOS; must be run elevated on a Windows box hosting OtOpcUaHost.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Joseph Doherty
2026-12-03 15:02:09 -05:00
parent 6b19f4add6
commit 2a8bac5bf5
7 changed files with 266 additions and 2 deletions
@@ -2,6 +2,7 @@ using Akka.Actor;
using Akka.Event;
using Microsoft.EntityFrameworkCore;
using ZB.MOM.WW.Audit;
using ZB.MOM.WW.OtOpcUa.Commons.Observability;
using ZB.MOM.WW.OtOpcUa.Configuration;
using ZB.MOM.WW.OtOpcUa.Configuration.Entities;
@@ -18,7 +19,8 @@ namespace ZB.MOM.WW.OtOpcUa.ControlPlane.Audit;
/// opposed to the database rejecting specific rows) <b>keeps</b> the batch: the events go back into the buffer
/// and the next timer tick retries them (#980 / review 05 H-2). The buffer is bounded by
/// <see cref="MaxBufferedEvents"/>; past that the OLDEST events are dropped, counted
/// (<see cref="OverflowDroppedCount"/>) and logged, so a long outage cannot grow memory without bound.
/// (<see cref="OverflowDroppedCount"/>, exported as the <c>otopcua.audit.dropped</c> meter counter — #1030) and
/// logged, so a long outage cannot grow memory without bound.
/// While flushes are failing, the count-threshold trigger is suppressed (each attempt blocks the actor for a
/// connect timeout) and only the timer retries.
///
@@ -41,12 +43,16 @@ public sealed class AuditWriterActor : ReceiveActor, IWithTimers, IAuditWriter
/// <summary>Upper bound on events held in memory awaiting a successful save; past it the oldest are dropped.</summary>
public const int MaxBufferedEvents = 10_000;
/// <summary>The exported counter overflow drops are metered on (#1030): <see cref="OtOpcUaTelemetry.AuditDropped"/>.</summary>
public const string DroppedCounterName = OtOpcUaTelemetry.AuditDroppedName;
private readonly IDbContextFactory<OtOpcUaConfigDbContext> _dbFactory;
private readonly ILoggingAdapter _log = Context.GetLogger();
// Insertion-ordered so the overflow policy can drop the OLDEST events first.
private readonly OrderedDictionary<Guid, AuditEvent> _buffer = new();
private bool _lastFlushFailed;
private long _overflowDropped;
private string? _actorTag;
/// <summary>Total events dropped (oldest first) because the retry buffer hit <see cref="MaxBufferedEvents"/>.</summary>
public long OverflowDroppedCount => Interlocked.Read(ref _overflowDropped);
@@ -116,6 +122,10 @@ public sealed class AuditWriterActor : ReceiveActor, IWithTimers, IAuditWriter
for (var i = 0; i < overflow; i++) _buffer.RemoveAt(0);
var before = Interlocked.Read(ref _overflowDropped);
var total = Interlocked.Add(ref _overflowDropped, overflow);
// #1030: exported, so a scrape sees lost audit rows without reading this actor's property.
OtOpcUaTelemetry.AuditDropped.Add(overflow,
new KeyValuePair<string, object?>("reason", "buffer_full"),
new KeyValuePair<string, object?>("actor", _actorTag ??= Self.Path.ToStringWithoutAddress()));
// Once per 1000 drops (and on the first), so a sustained outage does not log per event.
if (before == 0 || before / 1000 != total / 1000)
_log.Warning(