From 8d3e1493cf0c363525bd70fd6539fc2f92a98478 Mon Sep 17 00:00:00 2001 From: Warwick Schroeder Date: Wed, 25 Nov 2026 16:05:05 +0800 Subject: [PATCH] Fix retry handling --- docs/bulk-retries-design.md | 2 +- .../FailedMessageLifecycleDataStore.cs | 25 ++++++++++++++----- .../Recoverability/RetryStagingStoreTests.cs | 19 ++++++++++++++ .../Infrastructure/ReturnToSenderDequeuer.cs | 3 ++- 4 files changed, 41 insertions(+), 8 deletions(-) diff --git a/docs/bulk-retries-design.md b/docs/bulk-retries-design.md index c218ce40fc..2d51fb7123 100644 --- a/docs/bulk-retries-design.md +++ b/docs/bulk-retries-design.md @@ -82,7 +82,7 @@ When all batches of a retry operation complete, ServiceControl adds two entries ## Other notes -1. A message can be part of only one batch at a time. The `FailedMessageRetry/{messageId}` document prevents a message from being added to a second batch. ServiceControl removes this document only when the message comes back through the error queue. +1. A message can be part of only one batch at a time. The `FailedMessageRetry/{messageId}` document prevents a message from being added to a second batch. ServiceControl removes this document when the message no longer belongs to a retry, for example when the message comes back through the error queue, when the retry is confirmed, or when forwarding the message fails. 2. A batch that is created is eventually forwarded. If the ServiceControl process stops while the batch is in `Marking Documents`, the Adopt Orphan Batches process picks it up and moves it into `Staging`. Once a batch is in `Staging`, the Retry Processor repeatedly attempts to stage it until it succeeds. ServiceControl then selects the batch for `Forwarding`. 3. ServiceControl processes only one batch at a time in the `Forwarding` or `Staging` status. A batch in `Forwarding` must be fully forwarded and deleted before ServiceControl can stage a new batch. A batch selected for staging moves to the `Forwarding` status once it is fully staged. All of this happens on a single background thread that sleeps for 30 seconds if it finds nothing to do. 4. A batch can be forwarded only if it is completely staged. diff --git a/src/ServiceControl.Persistence.EFCore/Implementation/FailedMessageLifecycleDataStore.cs b/src/ServiceControl.Persistence.EFCore/Implementation/FailedMessageLifecycleDataStore.cs index bdb0201b1b..fcab5cb00d 100644 --- a/src/ServiceControl.Persistence.EFCore/Implementation/FailedMessageLifecycleDataStore.cs +++ b/src/ServiceControl.Persistence.EFCore/Implementation/FailedMessageLifecycleDataStore.cs @@ -130,13 +130,26 @@ await ExecuteWithDbContext(async (dbContext, token) => } var now = timeProvider.GetUtcNow().UtcDateTime; + var strategy = dbContext.Database.CreateExecutionStrategy(); - await dbContext.FailedMessages - .Where(fm => fm.UniqueMessageId == uniqueMessageId && fm.Status == FailedMessageStatus.RetryIssued) - .ExecuteUpdateAsync(s => s - .SetProperty(fm => fm.Status, FailedMessageStatus.Unresolved) - .SetProperty(fm => fm.StatusChangedAt, now) - .SetProperty(fm => fm.LastModified, now), token); + await strategy.ExecuteAsync(async () => + { + await using var transaction = await dbContext.Database.BeginTransactionAsync(token); + + await dbContext.FailedMessages + .Where(fm => fm.UniqueMessageId == uniqueMessageId && fm.Status == FailedMessageStatus.RetryIssued) + .ExecuteUpdateAsync(s => s + .SetProperty(fm => fm.Status, FailedMessageStatus.Unresolved) + .SetProperty(fm => fm.StatusChangedAt, now) + .SetProperty(fm => fm.LastModified, now), token); + + // The claim would otherwise stop every later retry from staging the message. + await dbContext.FailedMessageRetries + .Where(retry => retry.UniqueMessageId == uniqueMessageId) + .ExecuteDeleteAsync(token); + + await transaction.CommitAsync(token); + }); }, cancellationToken); } } \ No newline at end of file diff --git a/src/ServiceControl.Persistence.Tests/Recoverability/RetryStagingStoreTests.cs b/src/ServiceControl.Persistence.Tests/Recoverability/RetryStagingStoreTests.cs index ea576afa94..5f3861c2f9 100644 --- a/src/ServiceControl.Persistence.Tests/Recoverability/RetryStagingStoreTests.cs +++ b/src/ServiceControl.Persistence.Tests/Recoverability/RetryStagingStoreTests.cs @@ -225,6 +225,25 @@ public async Task Removing_a_message_from_the_batch_leaves_it_out_of_staging() Assert.That(messages.Single().UniqueMessageId, Is.EqualTo(kept)); } + [Test] + public async Task The_next_retry_stages_a_message_whose_send_failed_but_not_one_that_was_sent() + { + var failedToSend = await Insert(new IngestedFailure()); + var sent = await Insert(new IngestedFailure()); + + var firstBatch = await StageBatch(failedToSend, sent); + await RetryStagingStore.MarkBatchAsForwarding(firstBatch, "staging-1", [failedToSend, sent]); + await FailedMessageLifecycleStore.RevertRetry(failedToSend); + await RetryStagingStore.CompleteForwarding(firstBatch); + await CompleteDatabaseOperation(); + + var secondBatch = await StageBatch(failedToSend, sent); + + var messages = await RetryStagingStore.GetMessagesToStage(secondBatch); + + Assert.That(messages.Select(message => message.UniqueMessageId), Is.EquivalentTo(new[] { failedToSend })); + } + async Task Insert(IngestedFailure failure) { var message = failure.ToFailedMessage(); diff --git a/src/ServiceControl/Recoverability/Retrying/Infrastructure/ReturnToSenderDequeuer.cs b/src/ServiceControl/Recoverability/Retrying/Infrastructure/ReturnToSenderDequeuer.cs index 2c4d975e6a..61ff7acb51 100644 --- a/src/ServiceControl/Recoverability/Retrying/Infrastructure/ReturnToSenderDequeuer.cs +++ b/src/ServiceControl/Recoverability/Retrying/Infrastructure/ReturnToSenderDequeuer.cs @@ -1,6 +1,7 @@ namespace ServiceControl.Recoverability; using System; +using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using Infrastructure.DomainEvents; @@ -222,7 +223,7 @@ await domainEvents.Raise(new MessagesSubmittedForRetryFailed catch (Exception ex) { // If something goes wrong here we just ignore, not the end of the world! - logger.LogError(ex, "A failure occurred when trying to handle a retry failure"); + logger.LogError(ex, "A failure occurred when trying to handle a retry failure of message '{UniqueMessageId}'", errorContext.Headers.GetValueOrDefault("ServiceControl.Retry.UniqueMessageId")); } finally {