Every operator write already changes a row; what was missing is the answer an operator actually asks at 03:00 - when did this rule get here, who cleared that ban, did a document bring this edge's config. Since the transfer landed, an edge can be different from yesterday with nothing on it saying how.
config_changes carries one row per deliberate change: the instant, the section (block, allow, rule, country, policy, config), the action (added, removed, changed, imported), the target the operator typed, their own sentence where one was given, and the surface the change came through. GET /admin-api/changes answers it newest first, capped and paged like every other listing, filterable by section and since, readable by the read-only token because it is a read. A policy record says what moved ("mode Enforce->Off, streakThreshold 3->4") rather than only that something did. /admin-api/state counts the rows and the write failures.
The rulings, because each is a decision and not a detail:
* Engine bans are not in the log. A ban the engine earned is not a decision somebody made, and the block row already carries its evidence - consecutive refusals, status code, path, user agent, country, city. Logging bans would answer a different question and grow the table at the rate of traffic.
* An import is one record per section, not one per row: "3 added, 1 already there, 0 past expiry, 0 rejected". A 2 000-row document would otherwise bury a table whose whole use is being readable.
* The record names the surface, never a person. The edge has one admin token; a log that implied per-person attribution it cannot provide would be a false record, which is worse than no record.
* A record that cannot be written never undoes the change it describes. Enforcement is the product, so the change stands and the failure is counted - changeWriteFailures, changesNotWritten, changeLastError - because "the config changed and the record of it did not land" has to be a sentence an operator can read rather than one that silently never happened.
* Bounded twice, both keys the operator's: ChangeRetentionDays (30) and ChangeLogMaxRows (5000, oldest dropped first). The age window alone would let a scripted churn grow the table without limit; the cap alone would drop last month's incident record the week someone did a lot of editing.
What the run caught, in order: (1) Two gate assertions were wrong rather than the product - a policy record was asserted in a scene where no policy write had happened yet, and "one imported record per section" was asserted against a stack that had already imported twice, so the assertion now reads the newest imported record instead of counting them. (2) DELETE /admin-api/countries/{code} answers 204, and the new assertion expected 200. (3) A shell trap in the new scene: a $VAR inside a single-quoted python -c is not interpolated, so the first version would have handed python a literal "$CHANGES_BEFORE" instead of a number.
Gates on this tree: build 0 warnings / 0 errors, unit 313/313, integration 298/298 in 23s, smoke 127 passed / 0 failed (14 new), image 262 MB, compose-smoke 278 passed / 0 failed with 0 containers left behind (11 new, against the shipped image), console 90 assertions / 0 failed.
Refs OPEN-64.
164 lines
6.3 KiB
C#
164 lines
6.3 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 change log (OPEN-64). Counted because a change whose record did not
|
|
/// land is a hole in the only answer the table exists to give, and counted
|
|
/// separately from the enforcement writes because the change itself still
|
|
/// applied — the loss is the memory, not the decision.
|
|
/// </summary>
|
|
public const string Changes = "changes";
|
|
|
|
/// <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;
|
|
}
|
|
}
|