The push queue was a channel in one process: three attempts inside one request of work, then the row was simply no longer owed to anybody. An endpoint down for ten minutes swallowed every alert raised in that window, and a restart swallowed everything still waiting. The alert table stayed correct the whole time, which is exactly what made the hole invisible from the surface. What is owed is now a row in alert_pushes: denormalised payload (retention prunes edge_alerts, and a delivery still owed must not go with the row it was copied from), attempts, next-attempt time, last error, and a claim so two edges over one database send each row once. Attempts are configuration (OpenWaf:Alerts:MaxPushAttempts, PushRetrySeconds), validated at startup like the other bounds; when they run out the row records that and the surface says how many were given up, because a counter that cannot tell 'still retrying' from 'stopped retrying' is the blindness this item removes. Surfaces: /admin-api/state answers alertPushPending/Delivered/GivenUp/OldestPendingAt/LastError from the table, /metrics publishes the same as gauges from a cache no older than 5s (the metrics endpoint's rule is that it never leans on the database per scrape). Retention prunes only rows that are finished. What the run caught, in order: (1) SchemaUpgradeTests hardcoded the migration count as a literal, so the new migration turned a working upgrade into a red test - the count is now read from the migrations assembly. (2) Two of the new queue tests asserted 'pending == 0' after waiting only for 'delivered >= 1'; one scene raises more than one alert, so the read landed between two rows and failed on a correct product - the wait now requires the queue to have emptied. (3) The whole integration suite answered 9 failures of 251, every one 'Timed out waiting for the entry point to build the IHost after 00:05:00', while the identical build minutes later answered 251/251 in 34s: a test host that cannot bind a loopback socket fails as a timeout, and the README now says so next to the scheduling note that used to be the only explanation on file. Gates on this tree: build 0 warnings / 0 errors, unit 277/277, integration 251/251 in 34s, smoke 68 passed / 0 failed (3 new), image 261 MB, compose-smoke 232 passed / 0 failed with 0 containers left behind (2 new, including the push-queue table being added by the edge to a volume that predates it), console 90 assertions / 0 failed. Refs OPEN-60.
69 lines
3.4 KiB
C#
69 lines
3.4 KiB
C#
namespace OpenWaf.Infrastructure.Persistence;
|
|
|
|
/// <summary>
|
|
/// When the next attempt at a delivery is allowed, and when to stop (OPEN-60).
|
|
///
|
|
/// Pure, because the two failure modes of a retry loop are both arithmetic: a
|
|
/// loop that never stops turns an outage at the receiver into load here, and a
|
|
/// loop that stops after one try is the at-most-once behaviour this item exists
|
|
/// to remove. Neither needs a running edge to check.
|
|
///
|
|
/// The bound is deliberate. An endpoint that has refused for an hour is broken,
|
|
/// and the honest answer at that point is to say so on the surface rather than to
|
|
/// keep asking — which is why giving up is a recorded state and not a dropped row.
|
|
/// </summary>
|
|
public static class PushRetryPolicy
|
|
{
|
|
public const int MaxAttemptsFloor = 1, MaxAttemptsCeiling = 20;
|
|
public const int RetrySecondsFloor = 1, RetrySecondsCeiling = 3600;
|
|
|
|
/// <summary>The cap on a single backoff step. Exponential growth from a
|
|
/// 30-second base reaches a day in eight steps, and a day is not a retry
|
|
/// interval for a page that was supposed to fire at 03:00.</summary>
|
|
public static readonly TimeSpan MaxDelay = TimeSpan.FromMinutes(30);
|
|
|
|
/// <summary>How long a claim may be held before another process may take the
|
|
/// row over. Longer than one push timeout times the attempts inside one push,
|
|
/// shorter than the retry interval, so a crashed process delays a delivery
|
|
/// rather than losing it.</summary>
|
|
public static readonly TimeSpan ClaimReclaimWindow = TimeSpan.FromMinutes(5);
|
|
|
|
public static int ParseMaxAttempts(string? configured)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(configured)) return 6;
|
|
if (!int.TryParse(configured, out var attempts)
|
|
|| attempts < MaxAttemptsFloor || attempts > MaxAttemptsCeiling)
|
|
throw new InvalidOperationException(
|
|
$"OpenWaf:Alerts:MaxPushAttempts must be a whole number of attempts between "
|
|
+ $"{MaxAttemptsFloor} and {MaxAttemptsCeiling}; got {configured}");
|
|
return attempts;
|
|
}
|
|
|
|
public static int ParseRetrySeconds(string? configured)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(configured)) return 30;
|
|
if (!int.TryParse(configured, out var seconds)
|
|
|| seconds < RetrySecondsFloor || seconds > RetrySecondsCeiling)
|
|
throw new InvalidOperationException(
|
|
$"OpenWaf:Alerts:PushRetrySeconds must be a whole number of seconds between "
|
|
+ $"{RetrySecondsFloor} and {RetrySecondsCeiling}; got {configured}");
|
|
return seconds;
|
|
}
|
|
|
|
/// <summary>The delay before attempt number <c>attempts + 1</c>, given that
|
|
/// <c>attempts</c> have already failed. Exponential from the base, capped, and
|
|
/// never zero — a zero delay against a dead endpoint is a busy loop.</summary>
|
|
public static TimeSpan NextDelay(int attempts, int baseSeconds)
|
|
{
|
|
if (attempts <= 0) return TimeSpan.Zero;
|
|
var exponent = Math.Min(attempts, 16);
|
|
var seconds = (double)baseSeconds * Math.Pow(2, exponent - 1);
|
|
return TimeSpan.FromSeconds(Math.Min(seconds, MaxDelay.TotalSeconds));
|
|
}
|
|
|
|
/// <summary>Has this delivery run out of attempts? Compared against the
|
|
/// configured bound rather than a constant, because the bound is
|
|
/// configuration and a test has to be able to ask for a small one.</summary>
|
|
public static bool HasGivenUp(int attempts, int maxAttempts) => attempts >= maxAttempts;
|
|
}
|