-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathMessageReceiver.cs
More file actions
294 lines (254 loc) · 13.1 KB
/
MessageReceiver.cs
File metadata and controls
294 lines (254 loc) · 13.1 KB
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
namespace NServiceBus.Transport.SqlServer
{
using System;
#if SYSTEMDATASQLCLIENT
using System.Data.SqlClient;
#else
using Microsoft.Data.SqlClient;
#endif
using System.Threading;
using System.Threading.Tasks;
using Logging;
class MessageReceiver : IMessageReceiver
{
public MessageReceiver(
SqlServerTransport transport,
string receiverId,
string receiveAddress,
string errorQueueAddress,
Action<string, Exception, CancellationToken> criticalErrorAction,
Func<TransportTransactionMode, ProcessStrategy> processStrategyFactory,
Func<string, TableBasedQueue> queueFactory,
IPurgeQueues queuePurger,
IExpiredMessagesPurger expiredMessagesPurger,
IPeekMessagesInQueue queuePeeker,
QueuePeekerOptions queuePeekerOptions,
SchemaInspector schemaInspector,
TimeSpan waitTimeCircuitBreaker,
ISubscriptionManager subscriptionManager,
bool purgeAllMessagesOnStartup)
{
this.transport = transport;
this.processStrategyFactory = processStrategyFactory;
this.queuePurger = queuePurger;
this.queueFactory = queueFactory;
this.expiredMessagesPurger = expiredMessagesPurger;
this.queuePeeker = queuePeeker;
this.queuePeekerOptions = queuePeekerOptions;
this.schemaInspector = schemaInspector;
this.waitTimeCircuitBreaker = waitTimeCircuitBreaker;
this.errorQueueAddress = errorQueueAddress;
this.criticalErrorAction = criticalErrorAction;
this.purgeAllMessagesOnStartup = purgeAllMessagesOnStartup;
Subscriptions = subscriptionManager;
Id = receiverId;
ReceiveAddress = receiveAddress;
}
public async Task Initialize(PushRuntimeSettings limitations, OnMessage onMessage, OnError onError, CancellationToken cancellationToken = default)
{
this.limitations = limitations;
messageReceivingCancellationTokenSource = new CancellationTokenSource();
messageProcessingCancellationTokenSource = new CancellationTokenSource();
processStrategy = processStrategyFactory(transport.TransportTransactionMode);
messageReceivingCircuitBreaker = new RepeatedFailuresOverTimeCircuitBreaker("message receiving", waitTimeCircuitBreaker, ex => criticalErrorAction("Failed to peek " + ReceiveAddress, ex, messageProcessingCancellationTokenSource.Token));
messageProcessingCircuitBreaker = new RepeatedFailuresOverTimeCircuitBreaker("message processing", waitTimeCircuitBreaker, ex => criticalErrorAction("Failed to receive from " + ReceiveAddress, ex, messageProcessingCancellationTokenSource.Token));
inputQueue = queueFactory(ReceiveAddress);
errorQueue = queueFactory(errorQueueAddress);
processStrategy.Init(inputQueue, errorQueue, onMessage, onError, criticalErrorAction);
if (purgeAllMessagesOnStartup)
{
try
{
var purgedRowsCount = await queuePurger.Purge(inputQueue, cancellationToken).ConfigureAwait(false);
Logger.InfoFormat("{0:N0} messages purged from queue {1}", purgedRowsCount, ReceiveAddress);
}
catch (Exception ex) when (!ex.IsCausedBy(cancellationToken))
{
Logger.Warn("Failed to purge input queue on startup.", ex);
}
}
await PurgeExpiredMessages(cancellationToken).ConfigureAwait(false);
await schemaInspector.PerformInspection(inputQueue, cancellationToken).ConfigureAwait(false);
}
public Task StartReceive(CancellationToken cancellationToken = default)
{
inputQueue.FormatPeekCommand(queuePeekerOptions.MaxRecordsToPeek ?? Math.Min(100, 10 * limitations.MaxConcurrency));
maxConcurrency = limitations.MaxConcurrency;
concurrencyLimiter = new SemaphoreSlim(limitations.MaxConcurrency);
// Task.Run() so the call returns immediately instead of waiting for the first await or return down the call stack
messageReceivingTask = Task.Run(() => ReceiveMessagesAndSwallowExceptions(messageReceivingCancellationTokenSource.Token), CancellationToken.None);
return Task.CompletedTask;
}
public async Task ChangeConcurrency(PushRuntimeSettings newLimitations, CancellationToken cancellationToken = default)
{
SemaphoreSlim oldLimiter;
int oldMaxConcurrency;
lock (lockObject)
{
oldLimiter = concurrencyLimiter;
oldMaxConcurrency = maxConcurrency;
concurrencyLimiter = new SemaphoreSlim(newLimitations.MaxConcurrency);
limitations = newLimitations;
maxConcurrency = limitations.MaxConcurrency;
}
try
{
//Drain and dispose of the old semaphore
while (oldLimiter.CurrentCount != oldMaxConcurrency)
{
await Task.Delay(50, cancellationToken).ConfigureAwait(false);
}
oldLimiter.Dispose();
}
catch (Exception ex) when (ex.IsCausedBy(cancellationToken))
{
//Ignore, we are stopping anyway
}
}
public async Task StopReceive(CancellationToken cancellationToken = default)
{
messageReceivingCancellationTokenSource?.Cancel();
using (cancellationToken.Register(() => messageProcessingCancellationTokenSource?.Cancel()))
{
await messageReceivingTask.ConfigureAwait(false);
while (concurrencyLimiter.CurrentCount != maxConcurrency)
{
// Pass CancellationToken.None so that no exceptions will be thrown while waiting
// for the message receiver to gracefully shut down. The cancellation tokens passed to
// ProcessMessages (and thus the message processing pipelines) will be responsible
// for more forcefully shutting down message processing after the user's shutdown SLA
// is reached
await Task.Delay(50, CancellationToken.None).ConfigureAwait(false);
}
}
messageReceivingCircuitBreaker.Dispose();
messageProcessingCircuitBreaker.Dispose();
concurrencyLimiter.Dispose();
messageReceivingCancellationTokenSource?.Dispose();
messageProcessingCancellationTokenSource?.Dispose();
}
async Task ReceiveMessagesAndSwallowExceptions(CancellationToken messageReceivingCancellationToken)
{
while (!messageReceivingCancellationToken.IsCancellationRequested)
{
try
{
try
{
await ReceiveMessages(messageReceivingCancellationToken).ConfigureAwait(false);
}
catch (Exception ex) when (!ex.IsCausedBy(messageReceivingCancellationToken))
{
Logger.Error("Message receiving failed", ex);
await messageReceivingCircuitBreaker.Failure(ex, messageReceivingCancellationToken).ConfigureAwait(false);
}
}
catch (Exception ex) when (ex.IsCausedBy(messageReceivingCancellationToken))
{
// private token, receiver is being stopped, log the exception in case the stack trace is ever needed for debugging
Logger.Debug("Operation canceled while stopping the message receiver.", ex);
break;
}
}
}
async Task ReceiveMessages(CancellationToken messageReceivingCancellationToken)
{
var messageCount = await queuePeeker.Peek(inputQueue, messageReceivingCircuitBreaker, messageReceivingCancellationToken).ConfigureAwait(false);
if (messageCount == 0)
{
return;
}
messageReceivingCancellationToken.ThrowIfCancellationRequested();
// We cannot dispose this token source because of potential race conditions of concurrent processing
var stopBatchCancellationSource = new CancellationTokenSource();
// If either the receiving or processing circuit breakers are triggered, start only one message processing task at a time.
var maximumConcurrentProcessing =
messageProcessingCircuitBreaker.IsTriggered || messageReceivingCircuitBreaker.IsTriggered
? 1
: messageCount;
for (var i = 0; i < maximumConcurrentProcessing; i++)
{
if (stopBatchCancellationSource.IsCancellationRequested)
{
break;
}
var localConcurrencyLimiter = concurrencyLimiter;
await localConcurrencyLimiter.WaitAsync(messageReceivingCancellationToken).ConfigureAwait(false);
_ = ProcessMessagesSwallowExceptionsAndReleaseConcurrencyLimiter(stopBatchCancellationSource, localConcurrencyLimiter, messageProcessingCancellationTokenSource.Token);
}
}
async Task ProcessMessagesSwallowExceptionsAndReleaseConcurrencyLimiter(CancellationTokenSource stopBatchCancellationTokenSource, SemaphoreSlim localConcurrencyLimiter, CancellationToken messageProcessingCancellationToken)
{
try
{
try
{
// We need to force the method to continue asynchronously because SqlConnection
// in combination with TransactionScope will apply connection pooling and enlistment synchronous in ctor.
await Task.Yield();
await processStrategy.ProcessMessage(stopBatchCancellationTokenSource, messageProcessingCancellationToken)
.ConfigureAwait(false);
messageProcessingCircuitBreaker.Success();
}
catch (SqlException ex) when (ex.Number == 1205)
{
// getting the message was the victim of a lock resolution
Logger.Warn("Message processing failed", ex);
}
catch (Exception ex) when (!ex.IsCausedBy(messageProcessingCancellationToken))
{
Logger.Warn("Message processing failed", ex);
await messageProcessingCircuitBreaker.Failure(ex, messageProcessingCancellationToken).ConfigureAwait(false);
}
}
catch (Exception ex) when (ex.IsCausedBy(messageProcessingCancellationToken))
{
Logger.Debug("Message processing canceled.", ex);
}
finally
{
localConcurrencyLimiter.Release();
}
}
async Task PurgeExpiredMessages(CancellationToken cancellationToken)
{
try
{
await expiredMessagesPurger.Purge(inputQueue, cancellationToken).ConfigureAwait(false);
}
catch (SqlException e) when (e.Number == 1205)
{
//Purge has been victim of a lock resolution
Logger.Warn("Purger has been selected as a lock victim.", e);
}
}
TableBasedQueue inputQueue;
TableBasedQueue errorQueue;
readonly SqlServerTransport transport;
readonly string errorQueueAddress;
readonly Action<string, Exception, CancellationToken> criticalErrorAction;
readonly Func<TransportTransactionMode, ProcessStrategy> processStrategyFactory;
readonly IPurgeQueues queuePurger;
readonly Func<string, TableBasedQueue> queueFactory;
readonly IExpiredMessagesPurger expiredMessagesPurger;
readonly IPeekMessagesInQueue queuePeeker;
readonly QueuePeekerOptions queuePeekerOptions;
readonly SchemaInspector schemaInspector;
readonly bool purgeAllMessagesOnStartup;
TimeSpan waitTimeCircuitBreaker;
volatile SemaphoreSlim concurrencyLimiter;
CancellationTokenSource messageReceivingCancellationTokenSource;
CancellationTokenSource messageProcessingCancellationTokenSource;
int maxConcurrency;
RepeatedFailuresOverTimeCircuitBreaker messageReceivingCircuitBreaker;
RepeatedFailuresOverTimeCircuitBreaker messageProcessingCircuitBreaker;
Task messageReceivingTask;
ProcessStrategy processStrategy;
static readonly ILog Logger = LogManager.GetLogger<MessageReceiver>();
PushRuntimeSettings limitations;
public ISubscriptionManager Subscriptions { get; }
public string Id { get; }
public string ReceiveAddress { get; }
public static object lockObject = new();
}
}