Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't drain from session processor as it is not necessary for FIFO #41834

Merged
merged 3 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +369,10 @@ public override int PrefetchCount
messagesReceived as IReadOnlyCollection<AmqpMessage> ?? messagesReceived?.ToList() ?? s_emptyAmqpMessageList;

// If this is a session receiver and we didn't receive all requested messages, we need to drain the credits
// to ensure FIFO ordering within each session.
if (_isSessionReceiver && messageList.Count < maxMessages)
// to ensure FIFO ordering within each session. We exclude session processor since those will always receive a single message
// at a time, and if there are no messages, the session will be closed. The session won't be closed in the case that
// MaxConcurrentCallsPerSession > 1, but in that case FIFO is not possible to guarantee.
if (_isSessionReceiver && !_isProcessor && messageList.Count < maxMessages)
{
await link.DrainAsyc(cancellationToken).ConfigureAwait(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2679,6 +2679,54 @@ async Task ProcessMessage(ProcessSessionMessageEventArgs args)
}
}

[Test]
public async Task SessionOrderingIsGuaranteedProcessor()
{
await using (var scope = await ServiceBusScope.CreateWithQueue(enablePartitioning: false, enableSession: true))
{
await using var client = new ServiceBusClient(TestEnvironment.ServiceBusConnectionString);
long lastSequenceNumber = 0;

await using var processor = client.CreateSessionProcessor(scope.QueueName,
new ServiceBusSessionProcessorOptions
{
MaxConcurrentCallsPerSession = 1, MaxConcurrentSessions = 1
});
processor.ProcessMessageAsync += ProcessMessage;
processor.ProcessErrorAsync += SessionErrorHandler;

var sender = client.CreateSender(scope.QueueName);

CancellationTokenSource cts = new CancellationTokenSource();
cts.CancelAfter(TimeSpan.FromSeconds(60));
await processor.StartProcessingAsync();
await SendMessagesAsync();

await processor.StopProcessingAsync();

async Task SendMessagesAsync()
{
while (!cts.IsCancellationRequested)
{
await sender.SendMessageAsync(ServiceBusTestUtilities.GetMessage("session"));
await Task.Delay(TimeSpan.FromMilliseconds(100));
}
}

Task ProcessMessage(ProcessSessionMessageEventArgs args)
{
if (args.Message.SequenceNumber != lastSequenceNumber + 1)
JoshLove-msft marked this conversation as resolved.
Show resolved Hide resolved
{
Assert.Fail(
$"Last sequence number: {lastSequenceNumber}, current sequence number: {args.Message.SequenceNumber}");
}

lastSequenceNumber = args.Message.SequenceNumber;
return Task.CompletedTask;
}
}
}

private Task SessionErrorHandler(ProcessErrorEventArgs args)
{
// If the connection drops due to network flakiness
Expand Down
Loading