fix(sf): per-lane due-ness keeps per-target FIFO; post-delivery storage error is not a delivery failure (SF-1, SF-2, WP3 task 3.5)
This commit is contained in:
@@ -842,6 +842,105 @@ public class StoreAndForwardServiceTests : IAsyncLifetime, IDisposable
|
||||
Assert.Equal(1, maxConcurrent); // one (category,target) lane: strictly sequential
|
||||
}
|
||||
|
||||
// ── SF-1: per-target FIFO across sweeps ──
|
||||
|
||||
private async Task ExecSqlAsync(string sql)
|
||||
{
|
||||
await using var conn = _localDb.Db.CreateConnection();
|
||||
await using var cmd = conn.CreateCommand();
|
||||
cmd.CommandText = sql;
|
||||
await cmd.ExecuteNonQueryAsync();
|
||||
}
|
||||
|
||||
/// <summary>Back-dates a buffered row's last attempt so it is due on the next sweep.</summary>
|
||||
private Task MakeDueAsync(string id) => ExecSqlAsync(
|
||||
$"UPDATE sf_messages SET last_attempt_at_ms = {DateTimeOffset.UtcNow.AddMinutes(-5).ToUnixTimeMilliseconds()} WHERE id = '{id}'");
|
||||
|
||||
/// <summary>
|
||||
/// SF-1 (the review's exact scenario): A then B to one target. The head A fails, so its
|
||||
/// last attempt moves to now while B keeps its older stamp. On the next sweep B is due and
|
||||
/// A is not — pre-fix B was delivered first and A's later delivery overwrote B's newer
|
||||
/// value. The lane must wait for its head.
|
||||
/// </summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
public async Task RetrySweep_YoungerRow_NeverPassesBackingOffHead()
|
||||
{
|
||||
var delivered = new List<string>();
|
||||
var targetUp = false;
|
||||
_service.RegisterDeliveryHandler(StoreAndForwardCategory.CachedDbWrite, msg =>
|
||||
{
|
||||
if (!targetUp) throw new TimeoutException("Mes down");
|
||||
delivered.Add(msg.Id);
|
||||
return Task.FromResult(true);
|
||||
});
|
||||
|
||||
await _service.EnqueueAsync(StoreAndForwardCategory.CachedDbWrite, "Mes", "{}",
|
||||
attemptImmediateDelivery: false, retryInterval: TimeSpan.FromSeconds(30), messageId: "A");
|
||||
await _service.EnqueueAsync(StoreAndForwardCategory.CachedDbWrite, "Mes", "{}",
|
||||
attemptImmediateDelivery: false, retryInterval: TimeSpan.FromSeconds(30), messageId: "B");
|
||||
await MakeDueAsync("A");
|
||||
await MakeDueAsync("B");
|
||||
|
||||
// Sweep 1: A is attempted and fails (its last attempt is now "now"); B is skipped.
|
||||
await _service.RetryPendingMessagesAsync();
|
||||
Assert.Empty(delivered);
|
||||
|
||||
// The target recovers. B is still due by its own stamp, A is not.
|
||||
targetUp = true;
|
||||
await _service.RetryPendingMessagesAsync();
|
||||
Assert.Empty(delivered); // pre-fix: ["B"]
|
||||
|
||||
// Once the head is due, the lane drains in enqueue order.
|
||||
await MakeDueAsync("A");
|
||||
await _service.RetryPendingMessagesAsync();
|
||||
Assert.Equal(new[] { "A", "B" }, delivered);
|
||||
}
|
||||
|
||||
// ── SF-2: storage failure after a successful delivery ──
|
||||
|
||||
/// <summary>
|
||||
/// SF-2: the handler succeeded but the row delete fails. The delivery must not be counted
|
||||
/// as a failed retry (pre-fix: RetryCount++ and, on the last allowed retry, Parked — an
|
||||
/// operator Retry then re-sent a call that had succeeded). The row stays Pending with its
|
||||
/// counters untouched, and the lane stops so the younger row is not delivered ahead of
|
||||
/// the head's re-delivery.
|
||||
/// </summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
public async Task RetrySweep_DeleteFailsAfterDelivery_IsNotCountedAsFailure()
|
||||
{
|
||||
var delivered = new List<string>();
|
||||
_service.RegisterDeliveryHandler(StoreAndForwardCategory.ExternalSystem, msg =>
|
||||
{
|
||||
delivered.Add(msg.Id);
|
||||
return Task.FromResult(true);
|
||||
});
|
||||
await _service.EnqueueAsync(StoreAndForwardCategory.ExternalSystem, "api", "{}",
|
||||
attemptImmediateDelivery: false, retryInterval: TimeSpan.Zero, maxRetries: 1, messageId: "head");
|
||||
await _service.EnqueueAsync(StoreAndForwardCategory.ExternalSystem, "api", "{}",
|
||||
attemptImmediateDelivery: false, retryInterval: TimeSpan.Zero, maxRetries: 1, messageId: "next");
|
||||
|
||||
await ExecSqlAsync(
|
||||
"CREATE TRIGGER fail_delete BEFORE DELETE ON sf_messages BEGIN SELECT RAISE(ABORT, 'disk I/O error'); END;");
|
||||
|
||||
await _service.RetryPendingMessagesAsync();
|
||||
|
||||
Assert.Equal(new[] { "head" }, delivered); // lane stopped after the head
|
||||
var head = await _service.GetMessageByIdAsync("head");
|
||||
Assert.NotNull(head);
|
||||
Assert.Equal(StoreAndForwardMessageStatus.Pending, head!.Status); // pre-fix: Parked
|
||||
Assert.Equal(0, head.RetryCount); // pre-fix: 1
|
||||
Assert.Null(head.LastError);
|
||||
|
||||
// Storage recovers: the head is re-delivered (at-least-once) and the lane drains in order.
|
||||
await ExecSqlAsync("DROP TRIGGER fail_delete;");
|
||||
await _service.RetryPendingMessagesAsync();
|
||||
Assert.Equal(new[] { "head", "head", "next" }, delivered);
|
||||
Assert.Null(await _service.GetMessageByIdAsync("head"));
|
||||
Assert.Null(await _service.GetMessageByIdAsync("next"));
|
||||
}
|
||||
|
||||
private static void InterlockedMax(ref int target, int value)
|
||||
{
|
||||
int current;
|
||||
|
||||
@@ -135,18 +135,23 @@ public class StoreAndForwardStorageTests : IAsyncLifetime, IDisposable
|
||||
{
|
||||
// Exercise the julianday elapsed-time comparison with a
|
||||
// non-zero retry interval. A message attempted just now must NOT be due; one
|
||||
// attempted long ago must be due.
|
||||
// attempted long ago must be due. Each row is its own (category, target) lane:
|
||||
// within ONE lane a not-due older row would hold back the rows behind it (SF-1,
|
||||
// covered by GetMessagesForRetry_RowBehindNotDueHead_IsNotReturned).
|
||||
var notDue = CreateMessage("notdue", StoreAndForwardCategory.ExternalSystem);
|
||||
notDue.Target = "lane-notdue";
|
||||
notDue.RetryIntervalMs = (long)TimeSpan.FromHours(1).TotalMilliseconds;
|
||||
notDue.LastAttemptAt = DateTimeOffset.UtcNow;
|
||||
await _storage.EnqueueAsync(notDue);
|
||||
|
||||
var due = CreateMessage("due", StoreAndForwardCategory.ExternalSystem);
|
||||
due.Target = "lane-due";
|
||||
due.RetryIntervalMs = (long)TimeSpan.FromMinutes(5).TotalMilliseconds;
|
||||
due.LastAttemptAt = DateTimeOffset.UtcNow.AddHours(-2);
|
||||
await _storage.EnqueueAsync(due);
|
||||
|
||||
var neverAttempted = CreateMessage("never", StoreAndForwardCategory.ExternalSystem);
|
||||
neverAttempted.Target = "lane-never";
|
||||
neverAttempted.RetryIntervalMs = (long)TimeSpan.FromHours(1).TotalMilliseconds;
|
||||
neverAttempted.LastAttemptAt = null;
|
||||
await _storage.EnqueueAsync(neverAttempted);
|
||||
@@ -853,6 +858,60 @@ public class StoreAndForwardStorageTests : IAsyncLifetime, IDisposable
|
||||
Assert.Equal("a000", dead[0]);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// SF-1: due-ness is per lane. A younger row whose own stamp is due must not be returned
|
||||
/// while an older pending row of the SAME lane is still backing off; another lane is
|
||||
/// unaffected.
|
||||
/// </summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
public async Task GetMessagesForRetry_RowBehindNotDueHead_IsNotReturned()
|
||||
{
|
||||
var now = DateTimeOffset.UtcNow;
|
||||
var head = NewMsg("head", StoreAndForwardMessageStatus.Pending, now.AddMinutes(-10));
|
||||
head.LastAttemptAt = now; // just failed: not due for 30 s
|
||||
var behind = NewMsg("behind", StoreAndForwardMessageStatus.Pending, now.AddMinutes(-9));
|
||||
behind.LastAttemptAt = now.AddMinutes(-5); // due by its own stamp
|
||||
var other = NewMsg("other", StoreAndForwardMessageStatus.Pending, now.AddMinutes(-8));
|
||||
other.Target = "other-target";
|
||||
other.LastAttemptAt = now.AddMinutes(-5);
|
||||
await _storage.EnqueueAsync(head);
|
||||
await _storage.EnqueueAsync(behind);
|
||||
await _storage.EnqueueAsync(other);
|
||||
|
||||
foreach (var limit in new[] { 0, 500 })
|
||||
{
|
||||
var ids = (await _storage.GetMessagesForRetryAsync(limit)).Select(m => m.Id).ToList();
|
||||
Assert.Equal(new[] { "other" }, ids); // pre-fix: behind, other
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// SF-1: an older PARKED row does not block its lane (only pending rows hold a lane), and
|
||||
/// a due head releases the rows behind it in enqueue order.
|
||||
/// </summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
public async Task GetMessagesForRetry_ParkedOlderRow_DoesNotBlockLane()
|
||||
{
|
||||
var now = DateTimeOffset.UtcNow;
|
||||
var parked = NewMsg("parked", StoreAndForwardMessageStatus.Parked, now.AddMinutes(-10));
|
||||
parked.LastAttemptAt = now;
|
||||
var a = NewMsg("a", StoreAndForwardMessageStatus.Pending, now.AddMinutes(-9));
|
||||
a.LastAttemptAt = now.AddMinutes(-5);
|
||||
var b = NewMsg("b", StoreAndForwardMessageStatus.Pending, now.AddMinutes(-8));
|
||||
b.LastAttemptAt = now.AddMinutes(-5);
|
||||
var c = NewMsg("c", StoreAndForwardMessageStatus.Pending, now.AddMinutes(-7));
|
||||
c.LastAttemptAt = now; // not due: blocks nothing older, holds itself back
|
||||
await _storage.EnqueueAsync(parked);
|
||||
await _storage.EnqueueAsync(a);
|
||||
await _storage.EnqueueAsync(b);
|
||||
await _storage.EnqueueAsync(c);
|
||||
|
||||
var ids = (await _storage.GetMessagesForRetryAsync(500)).Select(m => m.Id).ToList();
|
||||
Assert.Equal(new[] { "a", "b" }, ids);
|
||||
}
|
||||
|
||||
// ReplaceAll_SwapsTheEntireBuffer_Atomically was DELETED with ReplaceAllAsync in
|
||||
// LocalDb Phase 2, and deliberately not replaced. It asserted a destructive
|
||||
// delete-all-then-insert-all, which was the standby's anti-entropy apply: a resync
|
||||
|
||||
Reference in New Issue
Block a user