This repository was archived by the owner on Oct 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 116
[WIP] Batching #539
Open
SeanFeldman
wants to merge
22
commits into
Azure:dev
Choose a base branch
from
SeanFeldman:batching
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[WIP] Batching #539
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
4b7aecc
Initial (simple) batching implementation
SeanFeldman 56c346e
Updating public API
SeanFeldman 19e50df
Modify test to remove reliance on order of messages
SeanFeldman b03695e
Adding batched send to QueueClient and TopicClient
SeanFeldman 76d127a
Remove order implied by Assert.Collection()
SeanFeldman 369a78f
Validate received messages cannot be added to Batch
SeanFeldman 04f1489
Cleanup
SeanFeldman 156dce4
Enable MessagingEventSource logging to comply with the rest of SendAs…
SeanFeldman 51da1ae
Verify custom properties affect Batch size (serialized bytes)
SeanFeldman 7a60434
Provide an API to create Batch initiated with supported maximum messa…
SeanFeldman 306e57e
Pass messages in Batch through outgoing plugins
SeanFeldman a0933fe
Report exception via diagnostics
SeanFeldman 3f50da5
Add tracking TODO
SeanFeldman a7b425d
Rename Batch to MessageBatch
SeanFeldman 8f98ee2
Move extension method into appropreate class
SeanFeldman 8b4e31c
Use correct exception type
SeanFeldman 0aca4b0
Minor tweaks
SeanFeldman 484e303
Ensure properties from the first message needed for a batch are inclu…
SeanFeldman c02b754
Fix and rename batch test
SeanFeldman eff85a7
Approving API
SeanFeldman eabe617
Uncomment log statement
SeanFeldman 6e20439
Resolved conflict from merging v3.4.0
SeanFeldman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
| // Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
|
||
| namespace Microsoft.Azure.ServiceBus.Core | ||
| { | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Azure.Amqp; | ||
| using Microsoft.Azure.Amqp.Framing; | ||
| using Microsoft.Azure.ServiceBus.Amqp; | ||
| using Microsoft.Azure.ServiceBus.Diagnostics; | ||
|
|
||
| [DebuggerDisplay("{" + nameof(DebuggerDisplay) + ",nq}")] | ||
| public class MessageBatch : IDisposable | ||
| { | ||
| internal readonly ulong maximumBatchSize; | ||
| private readonly Func<Message, Task<Message>> pluginsCallback; | ||
| private AmqpMessage firstMessage; | ||
| private readonly List<Data> datas; | ||
| private AmqpMessage result; | ||
|
|
||
| /// <summary> | ||
| /// Construct a new batch with a maximum batch size and outgoing plugins callback. | ||
| /// <remarks> | ||
| /// To construct a batch at run-time, use <see cref="MessageSender"/>, <see cref="QueueClient"/>, or <see cref="TopicClient"/>. | ||
| /// Use this constructor for testing and custom implementations. | ||
| /// </remarks> | ||
| /// </summary> | ||
| /// <param name="maximumBatchSize">Maximum batch size allowed for batch.</param> | ||
| /// <param name="pluginsCallback">Plugins callback to invoke on outgoing messages regisered with batch.</param> | ||
| internal MessageBatch(ulong maximumBatchSize, Func<Message, Task<Message>> pluginsCallback) | ||
| { | ||
| this.maximumBatchSize = maximumBatchSize; | ||
| this.pluginsCallback = pluginsCallback; | ||
| this.datas = new List<Data>(); | ||
| this.result = AmqpMessage.Create(datas); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Add <see cref="Message"/> to the batch if the overall size of the batch with the added message is not exceeding the batch maximum. | ||
| /// </summary> | ||
| /// <param name="message"><see cref="Message"/> to add to the batch.</param> | ||
| /// <returns></returns> | ||
| public async Task<bool> TryAdd(Message message) | ||
| { | ||
| ThrowIfDisposed(); | ||
|
|
||
| message.VerifyMessageIsNotPreviouslyReceived(); | ||
|
|
||
| var processedMessage = await pluginsCallback(message); | ||
|
|
||
| var amqpMessage = AmqpMessageConverter.SBMessageToAmqpMessage(processedMessage); | ||
|
|
||
| if (firstMessage == null) | ||
| { | ||
| firstMessage = amqpMessage; | ||
|
|
||
| if (processedMessage.MessageId != null) | ||
| { | ||
| result.Properties.MessageId = processedMessage.MessageId; | ||
| } | ||
|
|
||
| if (processedMessage.SessionId != null) | ||
| { | ||
| result.Properties.GroupId = processedMessage.SessionId; | ||
| } | ||
|
|
||
| if (processedMessage.PartitionKey != null) | ||
| { | ||
| result.MessageAnnotations.Map[AmqpMessageConverter.PartitionKeyName] = processedMessage.PartitionKey; | ||
| } | ||
|
|
||
| if (processedMessage.ViaPartitionKey != null) | ||
| { | ||
| result.MessageAnnotations.Map[AmqpMessageConverter.ViaPartitionKeyName] = processedMessage.ViaPartitionKey; | ||
| } | ||
| } | ||
|
|
||
| var data = AmqpMessageConverter.ToData(amqpMessage); | ||
| datas.Add(data); | ||
|
|
||
| if (Size <= maximumBatchSize) | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| datas.Remove(data); | ||
| return false; | ||
|
|
||
| } | ||
|
|
||
| /// <summary> | ||
| /// Number of messages in batch. | ||
| /// </summary> | ||
| public int Length => datas.Count; | ||
|
|
||
| internal ulong Size => (ulong) result.SerializedMessageSize; | ||
|
|
||
|
|
||
| /// <summary> | ||
| /// Convert batch to AMQP message. | ||
| /// </summary> | ||
| /// <returns></returns> | ||
| internal AmqpMessage ToAmqpMessage() | ||
| { | ||
| ThrowIfDisposed(); | ||
|
|
||
| if (datas.Count == 1) | ||
| { | ||
| firstMessage.Batchable = true; | ||
| return firstMessage; | ||
| } | ||
|
|
||
| result.MessageFormat = AmqpConstants.AmqpBatchedMessageFormat; | ||
| result.Batchable = true; | ||
| return result; | ||
| } | ||
|
|
||
| public void Dispose() | ||
| { | ||
| // TODO: review if there's anything else to do | ||
| firstMessage?.Dispose(); | ||
| result?.Dispose(); | ||
|
|
||
| firstMessage = null; | ||
| result = null; | ||
| } | ||
|
|
||
| private void ThrowIfDisposed() | ||
| { | ||
| if (result == null) | ||
| { | ||
| throw new ObjectDisposedException("MessageBatch has been disposed and cannot be re-used."); | ||
| } | ||
| } | ||
|
|
||
| private string DebuggerDisplay => $"MessageBatch: size={Size}; message count={datas.Count}; maximum size={maximumBatchSize}."; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nemakam I'm contemplating to make this public.
Reason: to be able to know what batch size was sent.