Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/bulk-retries-design.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> Insert(IngestedFailure failure)
{
var message = failure.ToFailedMessage();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace ServiceControl.Recoverability;

using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Infrastructure.DomainEvents;
Expand Down Expand Up @@ -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
{
Expand Down
Loading