| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Threading; |
| | 7 | | using System.Threading.Tasks; |
| | 8 | | using Azure.Messaging.ServiceBus.Diagnostics; |
| | 9 | | using Azure.Messaging.ServiceBus.Plugins; |
| | 10 | |
|
| | 11 | | namespace Azure.Messaging.ServiceBus |
| | 12 | | { |
| | 13 | | /// <summary> |
| | 14 | | /// Represents a thread-safe abstraction around a single session receiver that threads |
| | 15 | | /// spawned by the ServiceBusProcessor use to receive and process messages. Depending on how the |
| | 16 | | /// MaxConcurrentCalls and SessionIds options are configured, there may be multiple threads using the same |
| | 17 | | /// SessionReceiverManager (i.e. if MaxConcurrentCalls is greater than the number of specified sessions). |
| | 18 | | /// The manager will delegate to the user provided callbacks and handle automatic locking of sessions. |
| | 19 | | /// The receiver instance will only be closed when no other threads are using it, or when the user has |
| | 20 | | /// called StopProcessingAsync. |
| | 21 | | /// </summary> |
| | 22 | | #pragma warning disable CA1001 // Types that own disposable fields should be disposable. |
| | 23 | | // Doesn't own _concurrentAcceptSessionsSemaphore |
| | 24 | | internal class SessionReceiverManager : ReceiverManager |
| | 25 | | #pragma warning restore CA1001 // Types that own disposable fields should be disposable |
| | 26 | | { |
| | 27 | | private int _threadCount = 0; |
| | 28 | | private readonly Func<ProcessSessionEventArgs, Task> _sessionInitHandler; |
| | 29 | | private readonly Func<ProcessSessionEventArgs, Task> _sessionCloseHandler; |
| | 30 | | private readonly Func<ProcessSessionMessageEventArgs, Task> _messageHandler; |
| | 31 | | private readonly SemaphoreSlim _concurrentAcceptSessionsSemaphore; |
| | 32 | | private readonly int _maxCallsPerSession; |
| | 33 | | private readonly ServiceBusSessionReceiverOptions _sessionReceiverOptions; |
| | 34 | | private readonly bool _keepOpenOnReceiveTimeout; |
| | 35 | | private ServiceBusSessionReceiver _receiver; |
| | 36 | | private CancellationTokenSource _sessionLockRenewalCancellationSource; |
| | 37 | | private Task _sessionLockRenewalTask; |
| 0 | 38 | | private CancellationTokenSource _sessionCancellationSource = new CancellationTokenSource(); |
| 0 | 39 | | protected override ServiceBusReceiver Receiver => _receiver; |
| | 40 | |
|
| 0 | 41 | | private readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1); |
| | 42 | |
|
| | 43 | | public SessionReceiverManager( |
| | 44 | | ServiceBusConnection connection, |
| | 45 | | string fullyQualifiedNamespace, |
| | 46 | | string entityPath, |
| | 47 | | string identifier, |
| | 48 | | string sessionId, |
| | 49 | | ServiceBusProcessorOptions processorOptions, |
| | 50 | | Func<ProcessSessionEventArgs, Task> sessionInitHandler, |
| | 51 | | Func<ProcessSessionEventArgs, Task> sessionCloseHandler, |
| | 52 | | Func<ProcessSessionMessageEventArgs, Task> messageHandler, |
| | 53 | | Func<ProcessErrorEventArgs, Task> errorHandler, |
| | 54 | | SemaphoreSlim concurrentAcceptSessionsSemaphore, |
| | 55 | | EntityScopeFactory scopeFactory, |
| | 56 | | IList<ServiceBusPlugin> plugins, |
| | 57 | | int maxCallsPerSession, |
| | 58 | | bool keepOpenOnReceiveTimeout) |
| 0 | 59 | | : base(connection, fullyQualifiedNamespace, entityPath, identifier, processorOptions, default, errorHandler, |
| 0 | 60 | | scopeFactory, plugins) |
| | 61 | | { |
| 0 | 62 | | _sessionInitHandler = sessionInitHandler; |
| 0 | 63 | | _sessionCloseHandler = sessionCloseHandler; |
| 0 | 64 | | _messageHandler = messageHandler; |
| 0 | 65 | | _concurrentAcceptSessionsSemaphore = concurrentAcceptSessionsSemaphore; |
| 0 | 66 | | _maxCallsPerSession = maxCallsPerSession; |
| 0 | 67 | | _sessionReceiverOptions = new ServiceBusSessionReceiverOptions |
| 0 | 68 | | { |
| 0 | 69 | | ReceiveMode = _processorOptions.ReceiveMode, |
| 0 | 70 | | PrefetchCount = _processorOptions.PrefetchCount, |
| 0 | 71 | | SessionId = sessionId |
| 0 | 72 | | }; |
| 0 | 73 | | _keepOpenOnReceiveTimeout = keepOpenOnReceiveTimeout; |
| 0 | 74 | | } |
| | 75 | |
|
| | 76 | | private async Task<bool> EnsureCanProcess(CancellationToken cancellationToken) |
| | 77 | | { |
| 0 | 78 | | bool releaseSemaphore = false; |
| | 79 | | try |
| | 80 | | { |
| 0 | 81 | | await WaitSemaphore(cancellationToken).ConfigureAwait(false); |
| 0 | 82 | | releaseSemaphore = true; |
| 0 | 83 | | if (_threadCount >= _maxCallsPerSession) |
| | 84 | | { |
| 0 | 85 | | return false; |
| | 86 | | } |
| 0 | 87 | | if (_receiver == null) |
| | 88 | | { |
| 0 | 89 | | await CreateAndInitializeSessionReceiver(cancellationToken).ConfigureAwait(false); |
| | 90 | | } |
| 0 | 91 | | _threadCount++; |
| 0 | 92 | | return true; |
| | 93 | | } |
| | 94 | | finally |
| | 95 | | { |
| 0 | 96 | | if (releaseSemaphore) |
| | 97 | | { |
| 0 | 98 | | _semaphore.Release(); |
| | 99 | | } |
| | 100 | | } |
| 0 | 101 | | } |
| | 102 | |
|
| | 103 | | private async Task WaitSemaphore(CancellationToken cancellationToken) |
| | 104 | | { |
| | 105 | | try |
| | 106 | | { |
| 0 | 107 | | await _semaphore.WaitAsync(cancellationToken).ConfigureAwait(false); |
| 0 | 108 | | } |
| 0 | 109 | | catch (OperationCanceledException) |
| | 110 | | { |
| | 111 | | // propagate as TCE so can be handled by |
| | 112 | | // caller |
| 0 | 113 | | throw new TaskCanceledException(); |
| | 114 | | } |
| 0 | 115 | | } |
| | 116 | |
|
| | 117 | | private async Task CreateAndInitializeSessionReceiver( |
| | 118 | | CancellationToken processorCancellationToken) |
| | 119 | | { |
| 0 | 120 | | await CreateReceiver(processorCancellationToken).ConfigureAwait(false); |
| 0 | 121 | | _sessionCancellationSource = CancellationTokenSource.CreateLinkedTokenSource(processorCancellationToken); |
| | 122 | |
|
| 0 | 123 | | if (AutoRenewLock) |
| | 124 | | { |
| 0 | 125 | | _sessionLockRenewalTask = RenewSessionLock(); |
| | 126 | | } |
| | 127 | |
|
| 0 | 128 | | if (_sessionInitHandler != null) |
| | 129 | | { |
| 0 | 130 | | var args = new ProcessSessionEventArgs(_receiver, processorCancellationToken); |
| 0 | 131 | | await _sessionInitHandler(args).ConfigureAwait(false); |
| | 132 | | } |
| 0 | 133 | | } |
| | 134 | |
|
| | 135 | | private async Task CreateReceiver(CancellationToken processorCancellationToken) |
| | 136 | | { |
| 0 | 137 | | bool releaseSemaphore = false; |
| | 138 | | try |
| | 139 | | { |
| 0 | 140 | | await _concurrentAcceptSessionsSemaphore.WaitAsync(processorCancellationToken).ConfigureAwait(false); |
| | 141 | | // only attempt to release semaphore if WaitAsync is successful, |
| | 142 | | // otherwise SemaphoreFullException can occur. |
| 0 | 143 | | releaseSemaphore = true; |
| 0 | 144 | | _receiver = await ServiceBusSessionReceiver.CreateSessionReceiverAsync( |
| 0 | 145 | | entityPath: _entityPath, |
| 0 | 146 | | connection: _connection, |
| 0 | 147 | | plugins: _plugins, |
| 0 | 148 | | options: _sessionReceiverOptions, |
| 0 | 149 | | cancellationToken: processorCancellationToken).ConfigureAwait(false); |
| 0 | 150 | | } |
| 0 | 151 | | catch (OperationCanceledException) |
| | 152 | | { |
| | 153 | | // propagate as TCE so it will be handled by the outer catch block |
| 0 | 154 | | throw new TaskCanceledException(); |
| | 155 | | } |
| | 156 | | finally |
| | 157 | | { |
| 0 | 158 | | if (releaseSemaphore) |
| | 159 | | { |
| 0 | 160 | | _concurrentAcceptSessionsSemaphore.Release(); |
| | 161 | | } |
| | 162 | | } |
| 0 | 163 | | } |
| | 164 | |
|
| | 165 | | public override async Task CloseReceiverIfNeeded( |
| | 166 | | CancellationToken processorCancellationToken) |
| | 167 | | { |
| 0 | 168 | | bool releaseSemaphore = false; |
| | 169 | | try |
| | 170 | | { |
| | 171 | | // Intentionally not including processor cancellation token as |
| | 172 | | // we need to ensure that we at least attempt to close the receiver if needed. |
| 0 | 173 | | await WaitSemaphore(CancellationToken.None).ConfigureAwait(false); |
| 0 | 174 | | releaseSemaphore = true; |
| 0 | 175 | | if (_receiver == null) |
| | 176 | | { |
| 0 | 177 | | return; |
| | 178 | | } |
| 0 | 179 | | _threadCount--; |
| 0 | 180 | | if (_threadCount == 0) |
| | 181 | | { |
| 0 | 182 | | if (!_keepOpenOnReceiveTimeout || |
| 0 | 183 | | !AutoRenewLock || |
| 0 | 184 | | _sessionLockRenewalCancellationSource.IsCancellationRequested) |
| | 185 | | { |
| 0 | 186 | | await CloseReceiver(processorCancellationToken).ConfigureAwait(false); |
| | 187 | | } |
| | 188 | | } |
| 0 | 189 | | } |
| | 190 | | finally |
| | 191 | | { |
| 0 | 192 | | if (releaseSemaphore) |
| | 193 | | { |
| 0 | 194 | | _semaphore.Release(); |
| | 195 | | } |
| | 196 | | } |
| 0 | 197 | | } |
| | 198 | |
|
| | 199 | | private async Task CloseReceiver(CancellationToken cancellationToken) |
| | 200 | | { |
| 0 | 201 | | if (_receiver == null || _receiver.IsDisposed) |
| | 202 | | { |
| 0 | 203 | | return; |
| | 204 | | } |
| | 205 | | try |
| | 206 | | { |
| 0 | 207 | | if (_sessionCloseHandler != null) |
| | 208 | | { |
| 0 | 209 | | var args = new ProcessSessionEventArgs(_receiver, cancellationToken); |
| 0 | 210 | | await _sessionCloseHandler(args).ConfigureAwait(false); |
| | 211 | | } |
| 0 | 212 | | } |
| 0 | 213 | | catch (Exception exception) |
| | 214 | | { |
| 0 | 215 | | await RaiseExceptionReceived( |
| 0 | 216 | | new ProcessErrorEventArgs( |
| 0 | 217 | | exception, |
| 0 | 218 | | ServiceBusErrorSource.CloseMessageSession, |
| 0 | 219 | | _fullyQualifiedNamespace, |
| 0 | 220 | | _entityPath)) |
| 0 | 221 | | .ConfigureAwait(false); |
| | 222 | | } |
| | 223 | | finally |
| | 224 | | { |
| | 225 | | // cancel the automatic session lock renewal |
| 0 | 226 | | await CancelTask(_sessionLockRenewalCancellationSource, _sessionLockRenewalTask).ConfigureAwait(false); |
| | 227 | |
|
| | 228 | | // Always at least attempt to dispose. If this fails, it won't be retried. |
| 0 | 229 | | await _receiver.DisposeAsync().ConfigureAwait(false); |
| 0 | 230 | | _receiver = null; |
| | 231 | | } |
| 0 | 232 | | } |
| | 233 | |
|
| | 234 | | public override async Task ReceiveAndProcessMessagesAsync(CancellationToken processorCancellationToken) |
| | 235 | | { |
| 0 | 236 | | ServiceBusErrorSource errorSource = ServiceBusErrorSource.AcceptMessageSession; |
| 0 | 237 | | bool canProcess = false; |
| | 238 | | try |
| | 239 | | { |
| | 240 | | try |
| | 241 | | { |
| 0 | 242 | | canProcess = await EnsureCanProcess(processorCancellationToken).ConfigureAwait(false); |
| 0 | 243 | | if (!canProcess) |
| | 244 | | { |
| 0 | 245 | | return; |
| | 246 | | } |
| 0 | 247 | | } |
| | 248 | | catch (ServiceBusException ex) |
| 0 | 249 | | when (ex.Reason == ServiceBusFailureReason.ServiceTimeout) |
| | 250 | | { |
| | 251 | | // these exceptions are expected when no messages are available |
| | 252 | | // so simply return and allow this to be tried again on next thread |
| 0 | 253 | | return; |
| | 254 | | } |
| | 255 | | // loop within the context of this thread |
| 0 | 256 | | while (!_sessionCancellationSource.Token.IsCancellationRequested) |
| | 257 | | { |
| 0 | 258 | | errorSource = ServiceBusErrorSource.Receive; |
| 0 | 259 | | ServiceBusReceivedMessage message = await _receiver.ReceiveMessageAsync( |
| 0 | 260 | | _maxReceiveWaitTime, |
| 0 | 261 | | _sessionCancellationSource.Token).ConfigureAwait(false); |
| 0 | 262 | | if (message == null) |
| | 263 | | { |
| | 264 | | // Break out of the loop to allow a new session to |
| | 265 | | // be processed. |
| | 266 | | break; |
| | 267 | | } |
| 0 | 268 | | await ProcessOneMessageWithinScopeAsync( |
| 0 | 269 | | message, |
| 0 | 270 | | DiagnosticProperty.ProcessSessionMessageActivityName, |
| 0 | 271 | | _sessionCancellationSource.Token).ConfigureAwait(false); |
| | 272 | | } |
| 0 | 273 | | } |
| | 274 | | catch (Exception ex) |
| 0 | 275 | | when (!(ex is TaskCanceledException) || |
| 0 | 276 | | // If the user manually throws a TCE, then we should log it. |
| 0 | 277 | | (!_sessionCancellationSource.IsCancellationRequested && |
| 0 | 278 | | // Even though the _sessionCancellationSource is linked to processorCancellationToken, |
| 0 | 279 | | // we need to check both here in case the processor token gets cancelled before the |
| 0 | 280 | | // session token is linked. |
| 0 | 281 | | !processorCancellationToken.IsCancellationRequested)) |
| | 282 | | { |
| 0 | 283 | | if (ex is ServiceBusException sbException && sbException.ProcessorErrorSource.HasValue) |
| | 284 | | { |
| 0 | 285 | | errorSource = sbException.ProcessorErrorSource.Value; |
| | 286 | |
|
| | 287 | | // Signal cancellation so user event handlers can stop whatever processing they are doing |
| | 288 | | // as soon as we know the session lock has been lost. Note, we don't have analogous handling |
| | 289 | | // for message locks in ReceiverManager, because there is only ever one thread processing a |
| | 290 | | // single message at one time, so cancelling the token there would serve no purpose. |
| 0 | 291 | | if (sbException.Reason == ServiceBusFailureReason.SessionLockLost) |
| | 292 | | { |
| 0 | 293 | | _sessionCancellationSource.Cancel(); |
| | 294 | | } |
| | 295 | | } |
| 0 | 296 | | await RaiseExceptionReceived( |
| 0 | 297 | | new ProcessErrorEventArgs( |
| 0 | 298 | | ex, |
| 0 | 299 | | errorSource, |
| 0 | 300 | | _fullyQualifiedNamespace, |
| 0 | 301 | | _entityPath)) |
| 0 | 302 | | .ConfigureAwait(false); |
| | 303 | | } |
| | 304 | | finally |
| | 305 | | { |
| 0 | 306 | | if (canProcess) |
| | 307 | | { |
| 0 | 308 | | await CloseReceiverIfNeeded( |
| 0 | 309 | | processorCancellationToken).ConfigureAwait(false); |
| | 310 | | } |
| | 311 | | } |
| 0 | 312 | | } |
| | 313 | |
|
| | 314 | | /// <summary> |
| | 315 | | /// |
| | 316 | | /// </summary> |
| | 317 | | /// <returns></returns> |
| | 318 | | private async Task RenewSessionLock() |
| | 319 | | { |
| 0 | 320 | | _sessionLockRenewalCancellationSource = CancellationTokenSource.CreateLinkedTokenSource( |
| 0 | 321 | | _sessionCancellationSource.Token); |
| 0 | 322 | | _sessionLockRenewalCancellationSource.CancelAfter(_processorOptions.MaxAutoLockRenewalDuration); |
| 0 | 323 | | CancellationToken sessionLockRenewalCancellationToken = _sessionLockRenewalCancellationSource.Token; |
| 0 | 324 | | while (!sessionLockRenewalCancellationToken.IsCancellationRequested) |
| | 325 | | { |
| | 326 | | try |
| | 327 | | { |
| 0 | 328 | | ServiceBusEventSource.Log.ProcessorRenewSessionLockStart(_identifier, _receiver.SessionId); |
| 0 | 329 | | TimeSpan delay = CalculateRenewDelay(_receiver.SessionLockedUntil); |
| | 330 | |
|
| 0 | 331 | | await Task.Delay(delay, sessionLockRenewalCancellationToken).ConfigureAwait(false); |
| 0 | 332 | | if (_receiver.IsDisposed) |
| | 333 | | { |
| 0 | 334 | | break; |
| | 335 | | } |
| 0 | 336 | | await _receiver.RenewSessionLockAsync(sessionLockRenewalCancellationToken).ConfigureAwait(false); |
| 0 | 337 | | ServiceBusEventSource.Log.ProcessorRenewSessionLockComplete(_identifier); |
| 0 | 338 | | } |
| | 339 | |
|
| 0 | 340 | | catch (Exception ex) when (!(ex is TaskCanceledException)) |
| | 341 | | { |
| 0 | 342 | | ServiceBusEventSource.Log.ProcessorRenewSessionLockException(_identifier, ex.ToString()); |
| 0 | 343 | | await HandleRenewLockException(ex, sessionLockRenewalCancellationToken).ConfigureAwait(false); |
| | 344 | |
|
| | 345 | | // if the error was not transient, break out of the loop |
| 0 | 346 | | if (!(ex as ServiceBusException)?.IsTransient == true) |
| | 347 | | { |
| | 348 | | break; |
| | 349 | | } |
| | 350 | | } |
| | 351 | | } |
| 0 | 352 | | } |
| | 353 | |
|
| | 354 | | protected override async Task OnMessageHandler( |
| | 355 | | ServiceBusReceivedMessage message, |
| | 356 | | CancellationToken cancellationToken) |
| | 357 | | { |
| 0 | 358 | | var args = new ProcessSessionMessageEventArgs( |
| 0 | 359 | | message, |
| 0 | 360 | | _receiver, |
| 0 | 361 | | cancellationToken); |
| 0 | 362 | | await _messageHandler(args).ConfigureAwait(false); |
| 0 | 363 | | } |
| | 364 | | } |
| | 365 | | } |