-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathMessageDispatcher.cs
427 lines (357 loc) · 20.5 KB
/
MessageDispatcher.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
namespace NServiceBus.Transport.SQS
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Amazon.S3;
using Amazon.S3.Model;
using Amazon.SimpleNotificationService;
using Amazon.SQS;
using Amazon.SQS.Model;
using DelayedDelivery;
using Extensibility;
using Extensions;
using Logging;
using SimpleJson;
using Transport;
class MessageDispatcher : IDispatchMessages
{
public MessageDispatcher(TransportConfiguration configuration, IAmazonS3 s3Client, IAmazonSQS sqsClient, IAmazonSimpleNotificationService snsClient, QueueCache queueCache, TopicCache topicCache)
{
this.topicCache = topicCache;
this.snsClient = snsClient;
this.configuration = configuration;
this.s3Client = s3Client;
this.sqsClient = sqsClient;
this.queueCache = queueCache;
serializerStrategy = configuration.UseV1CompatiblePayload ? SimpleJson.PocoJsonSerializerStrategy : ReducedPayloadSerializerStrategy.Instance;
}
public async Task Dispatch(TransportOperations outgoingMessages, TransportTransaction transaction, ContextBag context)
{
var concurrentDispatchTasks = new List<Task>(3);
// in order to not enumerate multi cast operations multiple times this code assumes the hashset is filled on the synchronous path of the async method!
var messageIdsOfMulticastEvents = new HashSet<string>();
concurrentDispatchTasks.Add(DispatchMulticast(outgoingMessages.MulticastTransportOperations, messageIdsOfMulticastEvents, transaction));
foreach (var dispatchConsistencyGroup in outgoingMessages.UnicastTransportOperations
.GroupBy(o => o.RequiredDispatchConsistency))
{
switch (dispatchConsistencyGroup.Key)
{
case DispatchConsistency.Isolated:
concurrentDispatchTasks.Add(DispatchIsolated(dispatchConsistencyGroup, messageIdsOfMulticastEvents, transaction));
break;
case DispatchConsistency.Default:
concurrentDispatchTasks.Add(DispatchBatched(dispatchConsistencyGroup, messageIdsOfMulticastEvents, transaction));
break;
default:
throw new ArgumentOutOfRangeException();
}
}
try
{
await Task.WhenAll(concurrentDispatchTasks).ConfigureAwait(false);
}
catch (Exception e)
{
Logger.Error("Exception from Send.", e);
throw;
}
}
Task DispatchMulticast(List<MulticastTransportOperation> multicastTransportOperations, HashSet<string> messageIdsOfMulticastEvents, TransportTransaction transportTransaction)
{
List<Task> tasks = null;
// ReSharper disable once LoopCanBeConvertedToQuery
foreach (var operation in multicastTransportOperations)
{
messageIdsOfMulticastEvents.Add(operation.Message.MessageId);
tasks = tasks ?? new List<Task>(multicastTransportOperations.Count);
tasks.Add(Dispatch(operation, emptyHashset, transportTransaction));
}
return tasks != null ? Task.WhenAll(tasks) : TaskExtensions.Completed;
}
Task DispatchIsolated(IEnumerable<UnicastTransportOperation> isolatedTransportOperations, HashSet<string> messageIdsOfMulticastEvents, TransportTransaction transportTransaction)
{
List<Task> tasks = null;
// ReSharper disable once LoopCanBeConvertedToQuery
foreach (var operation in isolatedTransportOperations)
{
tasks = tasks ?? new List<Task>();
tasks.Add(Dispatch(operation, messageIdsOfMulticastEvents, transportTransaction));
}
return tasks != null ? Task.WhenAll(tasks) : TaskExtensions.Completed;
}
async Task DispatchBatched(IEnumerable<UnicastTransportOperation> toBeBatchedTransportOperations, HashSet<string> messageIdsOfMulticastEvents, TransportTransaction transportTransaction)
{
var tasks = new List<Task<SqsPreparedMessage>>();
// ReSharper disable once LoopCanBeConvertedToQuery
foreach (var operation in toBeBatchedTransportOperations)
{
tasks.Add(PrepareMessage<SqsPreparedMessage>(operation, messageIdsOfMulticastEvents, transportTransaction));
}
await Task.WhenAll(tasks).ConfigureAwait(false);
var batches = Batcher.Batch(tasks.Select(x => x.Result).Where(x => x != null));
var operationCount = batches.Count;
var batchTasks = new Task[operationCount];
for (var i = 0; i < operationCount; i++)
{
batchTasks[i] = SendBatch(batches[i], i + 1, operationCount);
}
await Task.WhenAll(batchTasks).ConfigureAwait(false);
}
async Task SendBatch(BatchEntry<SqsPreparedMessage> batch, int batchNumber, int totalBatches)
{
try
{
if (Logger.IsDebugEnabled)
{
var message = batch.PreparedMessagesBydId.Values.First();
Logger.Debug($"Sending batch '{batchNumber}/{totalBatches}' with message ids '{string.Join(", ", batch.PreparedMessagesBydId.Values.Select(v => v.MessageId))}' to destination {message.Destination}");
}
var result = await sqsClient.SendMessageBatchAsync(batch.BatchRequest).ConfigureAwait(false);
if (Logger.IsDebugEnabled)
{
var message = batch.PreparedMessagesBydId.Values.First();
Logger.Debug($"Sent batch '{batchNumber}/{totalBatches}' with message ids '{string.Join(", ", batch.PreparedMessagesBydId.Values.Select(v => v.MessageId))}' to destination {message.Destination}");
}
List<Task> redispatchTasks = null;
foreach (var errorEntry in result.Failed)
{
redispatchTasks = redispatchTasks ?? new List<Task>(result.Failed.Count);
var messageToRetry = batch.PreparedMessagesBydId[errorEntry.Id];
Logger.Info($"Retrying message with MessageId {messageToRetry.MessageId} that failed in batch '{batchNumber}/{totalBatches}' due to '{errorEntry.Message}'.");
redispatchTasks.Add(SendMessageForBatch(messageToRetry, batchNumber, totalBatches));
}
if (redispatchTasks != null)
{
await Task.WhenAll(redispatchTasks).ConfigureAwait(false);
}
}
catch (QueueDoesNotExistException e)
{
var message = batch.PreparedMessagesBydId.Values.First();
if (message.OriginalDestination != null)
{
throw new QueueDoesNotExistException($"Unable to send batch '{batchNumber}/{totalBatches}'. Destination '{message.OriginalDestination}' doesn't support delayed messages longer than {TimeSpan.FromSeconds(configuration.DelayedDeliveryQueueDelayTime)}. To enable support for longer delays, call '.UseTransport<SqsTransport>().UnrestrictedDelayedDelivery()' on the '{message.OriginalDestination}' endpoint.", e);
}
Logger.Error($"Error while sending batch '{batchNumber}/{totalBatches}', with message ids '{string.Join(", ", batch.PreparedMessagesBydId.Values.Select(v => v.MessageId))}', to '{message.Destination}'. The destination does not exist.", e);
throw;
}
catch (Exception ex)
{
var message = batch.PreparedMessagesBydId.Values.First();
Logger.Error($"Error while sending batch '{batchNumber}/{totalBatches}', with message ids '{string.Join(", ", batch.PreparedMessagesBydId.Values.Select(v => v.MessageId))}', to '{message.Destination}'", ex);
throw;
}
}
async Task Dispatch(MulticastTransportOperation transportOperation, HashSet<string> messageIdsOfMulticastedEvents, TransportTransaction transportTransaction)
{
var message = await PrepareMessage<SnsPreparedMessage>(transportOperation, messageIdsOfMulticastedEvents, transportTransaction)
.ConfigureAwait(false);
if (message == null)
{
return;
}
if (string.IsNullOrEmpty(message.Destination))
{
return;
}
var publishRequest = message.ToPublishRequest();
if (Logger.IsDebugEnabled)
{
Logger.Debug($"Publishing message with '{message.MessageId}' to topic '{publishRequest.TopicArn}'");
}
await snsClient.PublishAsync(publishRequest)
.ConfigureAwait(false);
if (Logger.IsDebugEnabled)
{
Logger.Debug($"Published message with '{message.MessageId}' to topic '{publishRequest.TopicArn}'");
}
}
async Task Dispatch(UnicastTransportOperation transportOperation, HashSet<string> messageIdsOfMulticastedEvents, TransportTransaction transportTransaction)
{
var message = await PrepareMessage<SqsPreparedMessage>(transportOperation, messageIdsOfMulticastedEvents, transportTransaction)
.ConfigureAwait(false);
if (message == null)
{
return;
}
await SendMessage(message)
.ConfigureAwait(false);
}
async Task SendMessageForBatch(SqsPreparedMessage message, int batchNumber, int totalBatches)
{
await SendMessage(message).ConfigureAwait(false);
Logger.Info($"Retried message with MessageId {message.MessageId} that failed in batch '{batchNumber}/{totalBatches}'.");
}
async Task SendMessage(SqsPreparedMessage message)
{
try
{
await sqsClient.SendMessageAsync(message.ToRequest())
.ConfigureAwait(false);
}
catch (QueueDoesNotExistException e) when (message.OriginalDestination != null)
{
throw new QueueDoesNotExistException($"Destination '{message.OriginalDestination}' doesn't support delayed messages longer than {TimeSpan.FromSeconds(configuration.DelayedDeliveryQueueDelayTime)}. To enable support for longer delays, call '.UseTransport<SqsTransport>().UnrestrictedDelayedDelivery()' on the '{message.OriginalDestination}' endpoint.", e);
}
catch (Exception ex)
{
Logger.Error($"Error while sending message, with MessageId '{message.MessageId}', to '{message.Destination}'", ex);
throw;
}
}
async Task<TMessage> PrepareMessage<TMessage>(IOutgoingTransportOperation transportOperation, HashSet<string> messageIdsOfMulticastedEvents, TransportTransaction transportTransaction)
where TMessage : PreparedMessage, new()
{
var unicastTransportOperation = transportOperation as UnicastTransportOperation;
// these conditions are carefully chosen to only execute the code if really necessary
if (unicastTransportOperation != null
&& messageIdsOfMulticastedEvents.Contains(unicastTransportOperation.Message.MessageId)
&& unicastTransportOperation.Message.GetMessageIntent() == MessageIntentEnum.Publish
&& unicastTransportOperation.Message.Headers.ContainsKey(Headers.EnclosedMessageTypes))
{
var mostConcreteEnclosedMessageType = unicastTransportOperation.Message.GetEnclosedMessageTypes()[0];
var existingTopic = await topicCache.GetTopicArn(mostConcreteEnclosedMessageType).ConfigureAwait(false);
if (existingTopic != null)
{
var matchingSubscriptionArn = await snsClient.FindMatchingSubscription(queueCache, existingTopic, unicastTransportOperation.Destination)
.ConfigureAwait(false);
if (matchingSubscriptionArn != null)
{
return null;
}
}
}
var delayDeliveryWith = transportOperation.DeliveryConstraints.OfType<DelayDeliveryWith>().SingleOrDefault();
var doNotDeliverBefore = transportOperation.DeliveryConstraints.OfType<DoNotDeliverBefore>().SingleOrDefault();
long delaySeconds = 0;
if (delayDeliveryWith != null)
{
delaySeconds = Convert.ToInt64(Math.Ceiling(delayDeliveryWith.Delay.TotalSeconds));
}
else if (doNotDeliverBefore != null)
{
delaySeconds = Convert.ToInt64(Math.Ceiling((doNotDeliverBefore.At - DateTime.UtcNow).TotalSeconds));
}
if (!configuration.IsDelayedDeliveryEnabled && delaySeconds > TransportConfiguration.AwsMaximumQueueDelayTime)
{
throw new NotSupportedException($"To send messages with a delay time greater than '{TimeSpan.FromSeconds(TransportConfiguration.AwsMaximumQueueDelayTime)}', call '.UseTransport<SqsTransport>().UnrestrictedDelayedDelivery()'.");
}
var sqsTransportMessage = new TransportMessage(transportOperation.Message, transportOperation.DeliveryConstraints);
var messageId = transportOperation.Message.MessageId;
var preparedMessage = new TMessage();
// In case we're handling a message of which the incoming message id equals the outgoing message id, we're essentially handling an error or audit scenario, in which case we want copy over the message attributes
// from the native message, so we don't lose part of the message
var forwardingANativeMessage = transportTransaction.TryGet<Message>(out var nativeMessage) &&
transportTransaction.TryGet<string>("IncomingMessageId", out var incomingMessageId) &&
incomingMessageId == transportOperation.Message.MessageId;
var nativeMessageAttributes = forwardingANativeMessage ? nativeMessage.MessageAttributes : null;
await ApplyUnicastOperationMappingIfNecessary(unicastTransportOperation, preparedMessage as SqsPreparedMessage, delaySeconds, messageId, nativeMessageAttributes).ConfigureAwait(false);
await ApplyMulticastOperationMappingIfNecessary(transportOperation as MulticastTransportOperation, preparedMessage as SnsPreparedMessage).ConfigureAwait(false);
preparedMessage.Body = SimpleJson.SerializeObject(sqsTransportMessage, serializerStrategy);
preparedMessage.MessageId = messageId;
preparedMessage.CalculateSize();
if (preparedMessage.Size <= TransportConfiguration.MaximumMessageSize)
{
return preparedMessage;
}
if (string.IsNullOrEmpty(configuration.S3BucketForLargeMessages))
{
throw new Exception("Cannot send large message because no S3 bucket was configured. Add an S3 bucket name to your configuration.");
}
var key = $"{configuration.S3KeyPrefix}/{messageId}";
using (var bodyStream = new MemoryStream(transportOperation.Message.Body))
{
var putObjectRequest = new PutObjectRequest
{
BucketName = configuration.S3BucketForLargeMessages,
InputStream = bodyStream,
Key = key
};
ApplyServerSideEncryptionConfiguration(putObjectRequest);
await s3Client.PutObjectAsync(putObjectRequest).ConfigureAwait(false);
}
sqsTransportMessage.S3BodyKey = key;
sqsTransportMessage.Body = string.Empty;
preparedMessage.Body = SimpleJson.SerializeObject(sqsTransportMessage, serializerStrategy);
preparedMessage.CalculateSize();
return preparedMessage;
}
async Task ApplyMulticastOperationMappingIfNecessary(MulticastTransportOperation transportOperation, SnsPreparedMessage snsPreparedMessage)
{
if (transportOperation == null || snsPreparedMessage == null)
{
return;
}
var existingTopicArn = await topicCache.GetTopicArn(transportOperation.MessageType).ConfigureAwait(false);
snsPreparedMessage.Destination = existingTopicArn;
}
async Task ApplyUnicastOperationMappingIfNecessary(UnicastTransportOperation transportOperation, SqsPreparedMessage sqsPreparedMessage, long delaySeconds, string messageId, Dictionary<string, MessageAttributeValue> nativeMessageAttributes)
{
if (transportOperation == null || sqsPreparedMessage == null)
{
return;
}
// copy over the message attributes that were set on the incoming message for error/audit scenario's if available
sqsPreparedMessage.CopyMessageAttributes(nativeMessageAttributes);
sqsPreparedMessage.RemoveNativeHeaders();
var delayLongerThanConfiguredDelayedDeliveryQueueDelayTime = configuration.IsDelayedDeliveryEnabled && delaySeconds > configuration.DelayedDeliveryQueueDelayTime;
if (delayLongerThanConfiguredDelayedDeliveryQueueDelayTime)
{
sqsPreparedMessage.OriginalDestination = transportOperation.Destination;
sqsPreparedMessage.Destination = $"{transportOperation.Destination}{TransportConfiguration.DelayedDeliveryQueueSuffix}";
sqsPreparedMessage.QueueUrl = await queueCache.GetQueueUrl(sqsPreparedMessage.Destination)
.ConfigureAwait(false);
sqsPreparedMessage.MessageDeduplicationId = messageId;
sqsPreparedMessage.MessageGroupId = messageId;
sqsPreparedMessage.MessageAttributes[TransportHeaders.DelaySeconds] = new MessageAttributeValue
{
StringValue = delaySeconds.ToString(),
DataType = "String"
};
}
else
{
sqsPreparedMessage.Destination = transportOperation.Destination;
sqsPreparedMessage.QueueUrl = await queueCache.GetQueueUrl(sqsPreparedMessage.Destination)
.ConfigureAwait(false);
if (delaySeconds > 0)
{
sqsPreparedMessage.DelaySeconds = Convert.ToInt32(delaySeconds);
}
}
}
void ApplyServerSideEncryptionConfiguration(PutObjectRequest putObjectRequest)
{
if (configuration.ServerSideEncryptionMethod != null)
{
putObjectRequest.ServerSideEncryptionMethod = configuration.ServerSideEncryptionMethod;
if (!string.IsNullOrEmpty(configuration.ServerSideEncryptionKeyManagementServiceKeyId))
{
putObjectRequest.ServerSideEncryptionKeyManagementServiceKeyId = configuration.ServerSideEncryptionKeyManagementServiceKeyId;
}
return;
}
if (configuration.ServerSideEncryptionCustomerMethod != null)
{
putObjectRequest.ServerSideEncryptionCustomerMethod = configuration.ServerSideEncryptionCustomerMethod;
putObjectRequest.ServerSideEncryptionCustomerProvidedKey = configuration.ServerSideEncryptionCustomerProvidedKey;
if (!string.IsNullOrEmpty(configuration.ServerSideEncryptionCustomerProvidedKeyMD5))
{
putObjectRequest.ServerSideEncryptionCustomerProvidedKeyMD5 = configuration.ServerSideEncryptionCustomerProvidedKeyMD5;
}
}
}
readonly IAmazonSimpleNotificationService snsClient;
readonly TopicCache topicCache;
TransportConfiguration configuration;
IAmazonSQS sqsClient;
IAmazonS3 s3Client;
QueueCache queueCache;
IJsonSerializerStrategy serializerStrategy;
static readonly HashSet<string> emptyHashset = new HashSet<string>();
static ILog Logger = LogManager.GetLogger(typeof(MessageDispatcher));
}
}