Measured on the shipped image with writes to block_entries aborted by SQLite triggers while reads kept working: five refusals from one source at a threshold of 3 answered 404 five times, no ban reached storage or /state, /readyz answered 200, and the log said "BlockStore: concurrent ban on 203.0.113.12; keeping the winner" four times over an inner "SQLite Error 19: 'gate: ban write blocked'". Enforcement reads the snapshot and the snapshot is built from storage, so a ban that cannot be written is a ban that exists nowhere - and the only sentence the log offered said the opposite. BlockStore now asks storage which case it is: re-read the row. A live engine ban for that source means somebody won the unique-index race and nothing is wrong; no row, or a read that cannot run either, means the write never happened. The second case is counted (DurabilityHealth.Bans), logged with its real reason and its consequence, and gates /readyz - the enforcement path is the path that gates. That also moved the boundary drawn last cycle. Observations, alerts and retention are reporting pillars: counted, shown on /state and on the console, and no longer gating readiness. The measurement that forced it: with the gate in place, one lost alert left alertWriteFailures at 3 and /readyz at 503; the triggers were removed and the stand left idle for 120 s, and it answered 503 the whole time with the write path healthy. An alert pillar writes only when an alert happens to be raised, so "not yet proven healthy" had become "unready forever". ObservationWriter now retries every tracked source while it has an outstanding failure: the write-amplification bound protects against load, not against retries, and without this the lost rows waited for each source to be due again a full interval later. Teeth: BanWriteFailureTests (3) - 3 failed / 0 passed before the fix; two WriteFailureVisibilityTests assertions re-drawn for the new boundary. Gates: compose-smoke 63 -> 72 (the ban scene asserts 404x5, no row in storage, 503 naming bans, race lines 0/0 and the honest line present, then the ban landing on a later refusal), smoke.sh counters now include bans, console-check 57 -> 58.
156 lines
6.0 KiB
C#
156 lines
6.0 KiB
C#
namespace OpenWaf.Infrastructure.Persistence;
|
|
|
|
/// <summary>
|
|
/// Counts what each background pillar's durable writes actually lost.
|
|
///
|
|
/// The write side had the same failure posture as the read side — catch
|
|
/// everything, log one line, keep the process alive — and the same blindness.
|
|
/// Measured on the shipped image (openwaf:ci, the head tree) with SQLite
|
|
/// triggers that abort every INSERT and UPDATE on <c>source_observations</c>
|
|
/// and <c>edge_alerts</c> while leaving reads alone:
|
|
/// before the break GET /admin-api/observations → X-Total-Count: 4
|
|
/// GET /admin-api/alerts → 1 row
|
|
/// triggers installed, three new sources and six more 502s from one address
|
|
/// GET /admin-api/observations → 200, X-Total-Count: 4
|
|
/// GET /admin-api/alerts → 200, still 1 row
|
|
/// /healthz 200, /readyz 200 {"status":"ready"}
|
|
/// /admin-api/state → no write-side field at all
|
|
/// the log: "ObservationWriter: flush failed, 4 entries
|
|
/// dropped." and two "AlertWriter: could not persist…"
|
|
/// triggers dropped X-Total-Count: 9 again
|
|
///
|
|
/// So the edge lost four sources of streak history and every alert a 5xx storm
|
|
/// raised, and the operator's surface — the alert table being "the whole point
|
|
/// of the alerting pillar" — reported a quiet, healthy edge. A dropped write is
|
|
/// not recoverable by a retry from the UI: the row never existed.
|
|
///
|
|
/// The counters are per pillar and mean "consecutive failed writes since the
|
|
/// last successful one", plus a cumulative count of entries lost, because lost
|
|
/// observations and lost alerts stay lost. A counter clears only on a write
|
|
/// that actually succeeded: a flush that had nothing to write is not evidence
|
|
/// that writing works, and a stand that has never written since a blip has not
|
|
/// earned a clean answer.
|
|
/// </summary>
|
|
public sealed class DurabilityHealth
|
|
{
|
|
public const string Observations = "observations";
|
|
public const string Alerts = "alerts";
|
|
public const string Retention = "retention";
|
|
|
|
/// <summary>
|
|
/// The engine's ban writes. Counted separately from the reporting pillars
|
|
/// because it is the only write on the enforcement path: a ban that never
|
|
/// reaches storage is not enforced by this edge either (enforcement reads
|
|
/// the snapshot), and it is invisible to every other edge sharing the
|
|
/// database until the write is retried.
|
|
/// </summary>
|
|
public const string Bans = "bans";
|
|
|
|
/// <summary>
|
|
/// One pillar's write state. <paramref name="Failures"/> is consecutive
|
|
/// failures since the last success; <paramref name="EntriesLost"/> is
|
|
/// cumulative, because a dropped row is not brought back by a later write.
|
|
/// </summary>
|
|
public sealed record Pillar(
|
|
string Name,
|
|
int Failures,
|
|
long EntriesLost,
|
|
long EntriesWritten,
|
|
string? LastError,
|
|
DateTimeOffset? LastSuccessAt)
|
|
{
|
|
public bool Failing => Failures > 0;
|
|
}
|
|
|
|
private sealed class State
|
|
{
|
|
public int Failures;
|
|
public long EntriesLost;
|
|
public long EntriesWritten;
|
|
public string? LastError;
|
|
public DateTimeOffset? LastSuccessAt;
|
|
|
|
public Pillar Snapshot(string name) =>
|
|
new(name, Failures, EntriesLost, EntriesWritten, LastError, LastSuccessAt);
|
|
}
|
|
|
|
private readonly Dictionary<string, State> _pillars = new(StringComparer.Ordinal);
|
|
private readonly object _gate = new();
|
|
|
|
/// <summary>
|
|
/// Records a write that went through. Only call this after SaveChanges
|
|
/// actually returned: a flush that decided nothing was due, or a prune that
|
|
/// deleted zero rows, says nothing about whether the write path works.
|
|
/// </summary>
|
|
public void RecordSuccess(string name, int entries = 0)
|
|
{
|
|
lock (_gate)
|
|
{
|
|
var s = For(name);
|
|
s.Failures = 0;
|
|
s.LastError = null;
|
|
s.LastSuccessAt = DateTimeOffset.UtcNow;
|
|
if (entries > 0) s.EntriesWritten += entries;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Records a write that did not. <paramref name="entriesLost"/> is how many
|
|
/// rows this attempt was carrying, so the surface can say what was lost
|
|
/// rather than only that something failed.
|
|
/// </summary>
|
|
public void RecordFailure(string name, string error, int entriesLost = 0)
|
|
{
|
|
lock (_gate)
|
|
{
|
|
var s = For(name);
|
|
s.Failures++;
|
|
s.LastError = error;
|
|
s.EntriesLost += entriesLost;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// The exception form, because the exception an EF save throws is a wrapper
|
|
/// and the operator needs the reason underneath it: measured in a test,
|
|
/// <c>DbUpdateException.Message</c> is "An error occurred while saving the
|
|
/// entity changes…" while the actual reason — "gate: durable write blocked",
|
|
/// "no such table: policy_settings" — sits on the innermost exception. A
|
|
/// surface that quotes the wrapper tells the reader nothing to act on.
|
|
/// </summary>
|
|
public void RecordFailure(string name, Exception ex, int entriesLost = 0) =>
|
|
RecordFailure(name, RootCause(ex).Message, entriesLost);
|
|
|
|
private static Exception RootCause(Exception ex)
|
|
{
|
|
var current = ex;
|
|
while (current.InnerException is not null) current = current.InnerException;
|
|
return current;
|
|
}
|
|
|
|
public Pillar Get(string name)
|
|
{
|
|
lock (_gate) return For(name).Snapshot(name);
|
|
}
|
|
|
|
public IReadOnlyList<Pillar> All()
|
|
{
|
|
lock (_gate)
|
|
{
|
|
return _pillars.Select(kv => kv.Value.Snapshot(kv.Key))
|
|
.OrderBy(p => p.Name, StringComparer.Ordinal)
|
|
.ToList();
|
|
}
|
|
}
|
|
|
|
private State For(string name)
|
|
{
|
|
if (!_pillars.TryGetValue(name, out var s))
|
|
{
|
|
s = new State();
|
|
_pillars[name] = s;
|
|
}
|
|
return s;
|
|
}
|
|
}
|