| | 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.Globalization; |
| | 7 | | using System.IO; |
| | 8 | | using System.Runtime.CompilerServices; |
| | 9 | | using System.Text.RegularExpressions; |
| | 10 | | using System.Threading; |
| | 11 | | using System.Threading.Tasks; |
| | 12 | | using Azure.Core; |
| | 13 | | using Azure.Messaging.EventHubs.Consumer; |
| | 14 | | using Azure.Messaging.EventHubs.Core; |
| | 15 | | using Azure.Messaging.EventHubs.Primitives; |
| | 16 | | using Azure.Messaging.EventHubs.Processor.Diagnostics; |
| | 17 | | using Azure.Storage.Blobs; |
| | 18 | | using Azure.Storage.Blobs.Models; |
| | 19 | |
|
| | 20 | | namespace Azure.Messaging.EventHubs.Processor |
| | 21 | | { |
| | 22 | | /// <summary> |
| | 23 | | /// A storage blob service that keeps track of checkpoints and ownership. |
| | 24 | | /// </summary> |
| | 25 | | /// |
| | 26 | | internal sealed class BlobsCheckpointStore : StorageManager |
| | 27 | | { |
| | 28 | | /// <summary>A regular expression used to capture strings enclosed in double quotes.</summary> |
| 2 | 29 | | private static readonly Regex DoubleQuotesExpression = new Regex("\"(.*)\"", RegexOptions.Compiled); |
| | 30 | |
|
| | 31 | | /// <summary>An ETag value to be used for permissive matching when querying Storage.</summary> |
| 2 | 32 | | private static readonly ETag IfNoneMatchAllTag = new ETag("*"); |
| | 33 | |
|
| | 34 | | /// <summary> |
| | 35 | | /// Specifies a string that filters the results to return only checkpoint blobs whose name begins |
| | 36 | | /// with the specified prefix. |
| | 37 | | /// </summary> |
| | 38 | | /// |
| | 39 | | private const string CheckpointPrefix = "{0}/{1}/{2}/checkpoint/"; |
| | 40 | |
|
| | 41 | | /// <summary> |
| | 42 | | /// Specifies a string that filters the results to return only ownership blobs whose name begins |
| | 43 | | /// with the specified prefix. |
| | 44 | | /// </summary> |
| | 45 | | /// |
| | 46 | | private const string OwnershipPrefix = "{0}/{1}/{2}/ownership/"; |
| | 47 | |
|
| | 48 | | /// <summary> |
| | 49 | | /// The client used to interact with the Azure Blob Storage service. |
| | 50 | | /// </summary> |
| | 51 | | /// |
| 120 | 52 | | private BlobContainerClient ContainerClient { get; } |
| | 53 | |
|
| | 54 | | /// <summary> |
| | 55 | | /// The active policy which governs retry attempts for the |
| | 56 | | /// <see cref="BlobsCheckpointStore" />. |
| | 57 | | /// </summary> |
| | 58 | | /// |
| 282 | 59 | | private EventHubsRetryPolicy RetryPolicy { get; } |
| | 60 | |
|
| | 61 | | /// <summary> |
| | 62 | | /// The instance of <see cref="BlobEventStoreEventSource" /> which can be mocked for testing. |
| | 63 | | /// </summary> |
| | 64 | | /// |
| 610 | 65 | | internal BlobEventStoreEventSource Logger { get; set; } = BlobEventStoreEventSource.Log; |
| | 66 | |
|
| | 67 | | /// <summary> |
| | 68 | | /// Initializes a new instance of the <see cref="BlobsCheckpointStore"/> class. |
| | 69 | | /// </summary> |
| | 70 | | /// |
| | 71 | | /// <param name="blobContainerClient">The client used to interact with the Azure Blob Storage service.</param> |
| | 72 | | /// <param name="retryPolicy">The retry policy to use as the basis for interacting with the Storage Blobs servic |
| | 73 | | /// |
| 136 | 74 | | public BlobsCheckpointStore(BlobContainerClient blobContainerClient, |
| 136 | 75 | | EventHubsRetryPolicy retryPolicy) |
| | 76 | | { |
| 136 | 77 | | Argument.AssertNotNull(blobContainerClient, nameof(blobContainerClient)); |
| 134 | 78 | | Argument.AssertNotNull(retryPolicy, nameof(retryPolicy)); |
| | 79 | |
|
| 132 | 80 | | ContainerClient = blobContainerClient; |
| 132 | 81 | | RetryPolicy = retryPolicy; |
| 132 | 82 | | Logger.BlobsCheckpointStoreCreated(nameof(BlobsCheckpointStore), blobContainerClient.AccountName, blobContai |
| 132 | 83 | | } |
| | 84 | |
|
| | 85 | | /// <summary> |
| | 86 | | /// Retrieves a complete ownership list from the storage blob service. |
| | 87 | | /// </summary> |
| | 88 | | /// |
| | 89 | | /// <param name="fullyQualifiedNamespace">The fully qualified Event Hubs namespace the ownership are associated |
| | 90 | | /// <param name="eventHubName">The name of the specific Event Hub the ownership are associated with, relative to |
| | 91 | | /// <param name="consumerGroup">The name of the consumer group the ownership are associated with.</param> |
| | 92 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> instance to signal the request to cancel t |
| | 93 | | /// |
| | 94 | | /// <returns>An enumerable containing all the existing ownership for the associated Event Hub and consumer group |
| | 95 | | /// |
| | 96 | | public override async Task<IEnumerable<EventProcessorPartitionOwnership>> ListOwnershipAsync(string fullyQualifi |
| | 97 | | string eventHubName |
| | 98 | | string consumerGrou |
| | 99 | | CancellationToken c |
| | 100 | | { |
| 18 | 101 | | cancellationToken.ThrowIfCancellationRequested<TaskCanceledException>(); |
| 16 | 102 | | Logger.ListOwnershipStart(fullyQualifiedNamespace, eventHubName, consumerGroup); |
| | 103 | |
|
| 16 | 104 | | List<EventProcessorPartitionOwnership> result = null; |
| | 105 | |
|
| | 106 | | try |
| | 107 | | { |
| 16 | 108 | | var prefix = string.Format(CultureInfo.InvariantCulture, OwnershipPrefix, fullyQualifiedNamespace.ToLowe |
| | 109 | |
|
| | 110 | | async Task<List<EventProcessorPartitionOwnership>> listOwnershipAsync(CancellationToken listOwnershipTok |
| | 111 | | { |
| 20 | 112 | | var ownershipList = new List<EventProcessorPartitionOwnership>(); |
| | 113 | |
|
| 28 | 114 | | await foreach (BlobItem blob in ContainerClient.GetBlobsAsync(traits: BlobTraits.Metadata, prefix: p |
| | 115 | | { |
| | 116 | | // In case this key does not exist, ownerIdentifier is set to null. This will force the Partiti |
| | 117 | | // to throw an exception. |
| | 118 | |
|
| 2 | 119 | | blob.Metadata.TryGetValue(BlobMetadataKey.OwnerIdentifier, out var ownerIdentifier); |
| | 120 | |
|
| 2 | 121 | | ownershipList.Add(new EventProcessorPartitionOwnership |
| 2 | 122 | | { |
| 2 | 123 | | FullyQualifiedNamespace = fullyQualifiedNamespace, |
| 2 | 124 | | EventHubName = eventHubName, |
| 2 | 125 | | ConsumerGroup = consumerGroup, |
| 2 | 126 | | OwnerIdentifier = ownerIdentifier, |
| 2 | 127 | | PartitionId = blob.Name.Substring(prefix.Length), |
| 2 | 128 | | LastModifiedTime = blob.Properties.LastModified.GetValueOrDefault(), |
| 2 | 129 | | Version = blob.Properties.ETag.ToString() |
| 2 | 130 | | }); |
| | 131 | | } |
| | 132 | |
|
| 4 | 133 | | return ownershipList; |
| 4 | 134 | | }; |
| | 135 | |
|
| 16 | 136 | | result = await ApplyRetryPolicy(listOwnershipAsync, cancellationToken).ConfigureAwait(false); |
| 4 | 137 | | return result; |
| | 138 | | } |
| 2 | 139 | | catch (RequestFailedException ex) when (ex.ErrorCode == BlobErrorCode.ContainerNotFound) |
| | 140 | | { |
| 2 | 141 | | Logger.ListOwnershipError(fullyQualifiedNamespace, eventHubName, consumerGroup, ex.Message); |
| 2 | 142 | | throw new RequestFailedException(Resources.BlobsResourceDoesNotExist); |
| | 143 | | } |
| | 144 | | finally |
| | 145 | | { |
| 16 | 146 | | Logger.ListOwnershipComplete(fullyQualifiedNamespace, eventHubName, consumerGroup, result?.Count ?? 0); |
| | 147 | | } |
| 4 | 148 | | } |
| | 149 | |
|
| | 150 | | /// <summary> |
| | 151 | | /// Attempts to claim ownership of partitions for processing. |
| | 152 | | /// </summary> |
| | 153 | | /// |
| | 154 | | /// <param name="partitionOwnership">An enumerable containing all the ownership to claim.</param> |
| | 155 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> instance to signal the request to cancel t |
| | 156 | | /// |
| | 157 | | /// <returns>An enumerable containing the successfully claimed ownership.</returns> |
| | 158 | | /// |
| | 159 | | public override async Task<IEnumerable<EventProcessorPartitionOwnership>> ClaimOwnershipAsync(IEnumerable<EventP |
| | 160 | | CancellationToken |
| | 161 | | { |
| 38 | 162 | | cancellationToken.ThrowIfCancellationRequested<TaskCanceledException>(); |
| | 163 | |
|
| 36 | 164 | | var claimedOwnership = new List<EventProcessorPartitionOwnership>(); |
| 36 | 165 | | var metadata = new Dictionary<string, string>(); |
| | 166 | |
|
| | 167 | | Response<BlobContentInfo> contentInfoResponse; |
| | 168 | | Response<BlobInfo> infoResponse; |
| | 169 | |
|
| 120 | 170 | | foreach (EventProcessorPartitionOwnership ownership in partitionOwnership) |
| | 171 | | { |
| 36 | 172 | | Logger.ClaimOwnershipStart(ownership.PartitionId, ownership.FullyQualifiedNamespace, ownership.EventHubN |
| 36 | 173 | | metadata[BlobMetadataKey.OwnerIdentifier] = ownership.OwnerIdentifier; |
| | 174 | |
|
| 36 | 175 | | var blobRequestConditions = new BlobRequestConditions(); |
| 36 | 176 | | var blobName = string.Format(CultureInfo.InvariantCulture, OwnershipPrefix + ownership.PartitionId, owne |
| 36 | 177 | | var blobClient = ContainerClient.GetBlobClient(blobName); |
| | 178 | |
|
| | 179 | | try |
| | 180 | | { |
| | 181 | | // Even though documentation states otherwise, we cannot use UploadAsync when the blob already exist |
| | 182 | | // the current storage SDK. For this reason, we are using the specified ETag as an indication of wh |
| | 183 | | // method to use. |
| | 184 | |
|
| 36 | 185 | | if (ownership.Version == null) |
| | 186 | | { |
| 18 | 187 | | blobRequestConditions.IfNoneMatch = IfNoneMatchAllTag; |
| | 188 | |
|
| | 189 | | async Task<Response<BlobContentInfo>> uploadBlobAsync(CancellationToken uploadToken) |
| | 190 | | { |
| 22 | 191 | | using var blobContent = new MemoryStream(Array.Empty<byte>()); |
| | 192 | |
|
| | 193 | | try |
| | 194 | | { |
| 22 | 195 | | return await blobClient.UploadAsync(blobContent, metadata: metadata, conditions: blobReq |
| | 196 | | } |
| 0 | 197 | | catch (RequestFailedException ex) when (ex.ErrorCode == BlobErrorCode.BlobAlreadyExists) |
| | 198 | | { |
| | 199 | | // A blob could have just been created by another Event Processor that claimed ownership |
| | 200 | | // partition. In this case, there's no point in retrying because we don't have the corr |
| | 201 | |
|
| 0 | 202 | | Logger.OwnershipNotClaimable(ownership.PartitionId, ownership.FullyQualifiedNamespace, o |
| 0 | 203 | | return null; |
| | 204 | | } |
| 6 | 205 | | }; |
| | 206 | |
|
| 18 | 207 | | contentInfoResponse = await ApplyRetryPolicy(uploadBlobAsync, cancellationToken).ConfigureAwait( |
| | 208 | |
|
| 6 | 209 | | if (contentInfoResponse == null) |
| | 210 | | { |
| 0 | 211 | | continue; |
| | 212 | | } |
| | 213 | |
|
| 6 | 214 | | ownership.LastModifiedTime = contentInfoResponse.Value.LastModified; |
| 6 | 215 | | ownership.Version = contentInfoResponse.Value.ETag.ToString(); |
| | 216 | | } |
| | 217 | | else |
| | 218 | | { |
| 18 | 219 | | blobRequestConditions.IfMatch = new ETag(ownership.Version); |
| 40 | 220 | | infoResponse = await ApplyRetryPolicy(uploadToken => blobClient.SetMetadataAsync(metadata, blobR |
| | 221 | |
|
| 2 | 222 | | ownership.LastModifiedTime = infoResponse.Value.LastModified; |
| 2 | 223 | | ownership.Version = infoResponse.Value.ETag.ToString(); |
| | 224 | | } |
| | 225 | |
|
| | 226 | | // Small workaround to retrieve the eTag. The current storage SDK returns it enclosed in |
| | 227 | | // double quotes ('"ETAG_VALUE"' instead of 'ETAG_VALUE'). |
| | 228 | |
|
| 8 | 229 | | var match = DoubleQuotesExpression.Match(ownership.Version); |
| | 230 | |
|
| 8 | 231 | | if (match.Success) |
| | 232 | | { |
| 2 | 233 | | ownership.Version = match.Groups[1].ToString(); |
| | 234 | | } |
| | 235 | |
|
| 8 | 236 | | claimedOwnership.Add(ownership); |
| 8 | 237 | | Logger.OwnershipClaimed(ownership.PartitionId, ownership.FullyQualifiedNamespace, ownership.EventHub |
| 8 | 238 | | } |
| 6 | 239 | | catch (RequestFailedException ex) when (ex.ErrorCode == BlobErrorCode.ConditionNotMet) |
| | 240 | | { |
| 4 | 241 | | Logger.OwnershipNotClaimable(ownership.PartitionId, ownership.FullyQualifiedNamespace, ownership.Eve |
| 4 | 242 | | } |
| 2 | 243 | | catch (RequestFailedException ex) when (ex.ErrorCode == BlobErrorCode.ContainerNotFound || ex.ErrorCode |
| | 244 | | { |
| 2 | 245 | | Logger.ClaimOwnershipError(ownership.PartitionId, ownership.FullyQualifiedNamespace, ownership.Event |
| 2 | 246 | | throw new RequestFailedException(Resources.BlobsResourceDoesNotExist); |
| | 247 | | } |
| 22 | 248 | | catch (Exception ex) |
| | 249 | | { |
| 22 | 250 | | Logger.ClaimOwnershipError(ownership.PartitionId, ownership.FullyQualifiedNamespace, ownership.Event |
| 22 | 251 | | throw; |
| | 252 | | } |
| | 253 | | finally |
| | 254 | | { |
| 36 | 255 | | Logger.ClaimOwnershipComplete(ownership.PartitionId, ownership.FullyQualifiedNamespace, ownership.Ev |
| | 256 | | } |
| 12 | 257 | | } |
| | 258 | |
|
| 12 | 259 | | return claimedOwnership; |
| 12 | 260 | | } |
| | 261 | |
|
| | 262 | | /// <summary> |
| | 263 | | /// Retrieves a list of all the checkpoints in a data store for a given namespace, Event Hub and consumer grou |
| | 264 | | /// </summary> |
| | 265 | | /// |
| | 266 | | /// <param name="fullyQualifiedNamespace">The fully qualified Event Hubs namespace the ownership are associated |
| | 267 | | /// <param name="eventHubName">The name of the specific Event Hub the ownership are associated with, relative to |
| | 268 | | /// <param name="consumerGroup">The name of the consumer group the ownership are associated with.</param> |
| | 269 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> instance to signal the request to cancel t |
| | 270 | | /// |
| | 271 | | /// <returns>An enumerable containing all the existing checkpoints for the associated Event Hub and consumer gro |
| | 272 | | /// |
| | 273 | | public override async Task<IEnumerable<EventProcessorCheckpoint>> ListCheckpointsAsync(string fullyQualifiedName |
| | 274 | | string eventHubName, |
| | 275 | | string consumerGroup, |
| | 276 | | CancellationToken cancell |
| | 277 | | { |
| 28 | 278 | | cancellationToken.ThrowIfCancellationRequested<TaskCanceledException>(); |
| 26 | 279 | | Logger.ListCheckpointsStart(fullyQualifiedNamespace, eventHubName, consumerGroup); |
| | 280 | |
|
| 26 | 281 | | var prefix = string.Format(CultureInfo.InvariantCulture, CheckpointPrefix, fullyQualifiedNamespace.ToLowerIn |
| 26 | 282 | | var checkpointCount = 0; |
| | 283 | |
|
| | 284 | | async Task<IEnumerable<EventProcessorCheckpoint>> listCheckpointsAsync(CancellationToken listCheckpointsToke |
| | 285 | | { |
| 30 | 286 | | var checkpoints = new List<EventProcessorCheckpoint>(); |
| | 287 | |
|
| 62 | 288 | | await foreach (BlobItem blob in ContainerClient.GetBlobsAsync(traits: BlobTraits.Metadata, prefix: prefi |
| | 289 | | { |
| 10 | 290 | | var partitionId = blob.Name.Substring(prefix.Length); |
| 10 | 291 | | var startingPosition = default(EventPosition?); |
| | 292 | |
|
| 10 | 293 | | if (blob.Metadata.TryGetValue(BlobMetadataKey.Offset, out var str) && long.TryParse(str, NumberStyle |
| | 294 | | { |
| 4 | 295 | | startingPosition = EventPosition.FromOffset(result, false); |
| | 296 | | } |
| 6 | 297 | | else if (blob.Metadata.TryGetValue(BlobMetadataKey.SequenceNumber, out str) && long.TryParse(str, Nu |
| | 298 | | { |
| 2 | 299 | | startingPosition = EventPosition.FromSequenceNumber(result, false); |
| | 300 | | } |
| | 301 | |
|
| | 302 | | // If either the offset or the sequence number was not populated, |
| | 303 | | // this is not a valid checkpoint. |
| | 304 | |
|
| 10 | 305 | | if (startingPosition.HasValue) |
| | 306 | | { |
| 6 | 307 | | checkpoints.Add(new EventProcessorCheckpoint |
| 6 | 308 | | { |
| 6 | 309 | | FullyQualifiedNamespace = fullyQualifiedNamespace, |
| 6 | 310 | | EventHubName = eventHubName, |
| 6 | 311 | | ConsumerGroup = consumerGroup, |
| 6 | 312 | | PartitionId = partitionId, |
| 6 | 313 | | StartingPosition = startingPosition.Value |
| 6 | 314 | | }); |
| | 315 | | } |
| | 316 | | else |
| | 317 | | { |
| 4 | 318 | | Logger.InvalidCheckpointFound(partitionId, fullyQualifiedNamespace, eventHubName, consumerGroup) |
| | 319 | | } |
| | 320 | | } |
| | 321 | |
|
| 12 | 322 | | checkpointCount = checkpoints.Count; |
| 12 | 323 | | return checkpoints; |
| 12 | 324 | | }; |
| | 325 | |
|
| | 326 | | try |
| | 327 | | { |
| 26 | 328 | | return await ApplyRetryPolicy(listCheckpointsAsync, cancellationToken).ConfigureAwait(false); |
| | 329 | | } |
| 2 | 330 | | catch (RequestFailedException ex) when (ex.ErrorCode == BlobErrorCode.ContainerNotFound) |
| | 331 | | { |
| 2 | 332 | | Logger.ListCheckpointsError(fullyQualifiedNamespace, eventHubName, consumerGroup, ex.Message); |
| 2 | 333 | | throw new RequestFailedException(Resources.BlobsResourceDoesNotExist); |
| | 334 | | } |
| 12 | 335 | | catch (Exception ex) |
| | 336 | | { |
| 12 | 337 | | Logger.ListCheckpointsError(fullyQualifiedNamespace, eventHubName, consumerGroup, ex.Message); |
| 12 | 338 | | throw; |
| | 339 | | } |
| | 340 | | finally |
| | 341 | | { |
| 26 | 342 | | Logger.ListCheckpointsComplete(fullyQualifiedNamespace, eventHubName, consumerGroup, checkpointCount); |
| | 343 | | } |
| 12 | 344 | | } |
| | 345 | |
|
| | 346 | | /// <summary> |
| | 347 | | /// Updates the checkpoint using the given information for the associated partition and consumer group in the |
| | 348 | | /// </summary> |
| | 349 | | /// |
| | 350 | | /// <param name="checkpoint">The checkpoint containing the information to be stored.</param> |
| | 351 | | /// <param name="eventData">The event to use as the basis for the checkpoint's starting position.</param> |
| | 352 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> instance to signal the request to cancel t |
| | 353 | | /// |
| | 354 | | /// <returns>A task to be resolved on when the operation has completed.</returns> |
| | 355 | | /// |
| | 356 | | public override async Task UpdateCheckpointAsync(EventProcessorCheckpoint checkpoint, |
| | 357 | | EventData eventData, |
| | 358 | | CancellationToken cancellationToken) |
| | 359 | | { |
| 36 | 360 | | cancellationToken.ThrowIfCancellationRequested<TaskCanceledException>(); |
| 34 | 361 | | Logger.UpdateCheckpointStart(checkpoint.PartitionId, checkpoint.FullyQualifiedNamespace, checkpoint.EventHub |
| | 362 | |
|
| 34 | 363 | | var blobName = string.Format(CultureInfo.InvariantCulture, CheckpointPrefix + checkpoint.PartitionId, checkp |
| 34 | 364 | | var blobClient = ContainerClient.GetBlobClient(blobName); |
| | 365 | |
|
| 34 | 366 | | var metadata = new Dictionary<string, string>() |
| 34 | 367 | | { |
| 34 | 368 | | { BlobMetadataKey.Offset, eventData.Offset.ToString(CultureInfo.InvariantCulture) }, |
| 34 | 369 | | { BlobMetadataKey.SequenceNumber, eventData.SequenceNumber.ToString(CultureInfo.InvariantCulture) } |
| 34 | 370 | | }; |
| | 371 | |
|
| | 372 | | try |
| | 373 | | { |
| | 374 | | try |
| | 375 | | { |
| | 376 | | // Assume the blob is present and attempt to set the metadata. |
| | 377 | |
|
| 72 | 378 | | await ApplyRetryPolicy(token => blobClient.SetMetadataAsync(metadata, cancellationToken: token), can |
| 4 | 379 | | } |
| 18 | 380 | | catch (RequestFailedException ex) when ((ex.ErrorCode == BlobErrorCode.BlobNotFound) || (ex.ErrorCode == |
| | 381 | | { |
| | 382 | | // If the blob wasn't present, fall-back to trying to create a new one. |
| | 383 | |
|
| 18 | 384 | | await ApplyRetryPolicy(async token => |
| 18 | 385 | | { |
| 40 | 386 | | using var blobContent = new MemoryStream(Array.Empty<byte>()); |
| 40 | 387 | | await blobClient.UploadAsync(blobContent, metadata: metadata, cancellationToken: token).Configur |
| 18 | 388 | |
|
| 22 | 389 | | }, cancellationToken).ConfigureAwait(false); |
| | 390 | | } |
| 8 | 391 | | } |
| 2 | 392 | | catch (RequestFailedException ex) when (ex.ErrorCode == BlobErrorCode.ContainerNotFound) |
| | 393 | | { |
| 2 | 394 | | Logger.UpdateCheckpointError(checkpoint.PartitionId, checkpoint.FullyQualifiedNamespace, checkpoint.Even |
| 2 | 395 | | throw new RequestFailedException(Resources.BlobsResourceDoesNotExist); |
| | 396 | | } |
| 24 | 397 | | catch (Exception ex) |
| | 398 | | { |
| 24 | 399 | | Logger.UpdateCheckpointError(checkpoint.PartitionId, checkpoint.FullyQualifiedNamespace, checkpoint.Even |
| 24 | 400 | | throw; |
| | 401 | | } |
| | 402 | | finally |
| | 403 | | { |
| 34 | 404 | | Logger.UpdateCheckpointComplete(checkpoint.PartitionId, checkpoint.FullyQualifiedNamespace, checkpoint.E |
| | 405 | | } |
| 8 | 406 | | } |
| | 407 | |
|
| | 408 | | /// <summary> |
| | 409 | | /// Applies the checkpoint store's <see cref="RetryPolicy" /> to a specified function. |
| | 410 | | /// </summary> |
| | 411 | | /// |
| | 412 | | /// <param name="functionToRetry">The function to which the retry policy should be applied.</param> |
| | 413 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> instance to signal the request to cancel t |
| | 414 | | /// |
| | 415 | | /// <returns>The value returned by the function to which the retry policy has been applied.</returns> |
| | 416 | | /// |
| | 417 | | private async Task ApplyRetryPolicy(Func<CancellationToken, Task> functionToRetry, |
| | 418 | | CancellationToken cancellationToken) |
| | 419 | | { |
| | 420 | | TimeSpan? retryDelay; |
| | 421 | |
|
| 130 | 422 | | var failedAttemptCount = 0; |
| 130 | 423 | | var tryTimeout = RetryPolicy.CalculateTryTimeout(0); |
| 130 | 424 | | var timeoutTokenSource = default(CancellationTokenSource); |
| 130 | 425 | | var linkedTokenSource = default(CancellationTokenSource); |
| | 426 | |
|
| 154 | 427 | | while (!cancellationToken.IsCancellationRequested) |
| | 428 | | { |
| | 429 | | try |
| | 430 | | { |
| 154 | 431 | | timeoutTokenSource = new CancellationTokenSource(tryTimeout); |
| 154 | 432 | | linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutTokenS |
| | 433 | |
|
| 154 | 434 | | await functionToRetry(linkedTokenSource.Token).ConfigureAwait(false); |
| 32 | 435 | | return; |
| | 436 | | } |
| | 437 | | catch (Exception ex) |
| | 438 | | { |
| | 439 | | // Determine if there should be a retry for the next attempt; if so enforce the delay but do not qui |
| | 440 | | // Otherwise, mark the exception as active and break out of the loop. |
| | 441 | |
|
| 122 | 442 | | ++failedAttemptCount; |
| 122 | 443 | | retryDelay = RetryPolicy.CalculateRetryDelay(ex, failedAttemptCount); |
| | 444 | |
|
| 122 | 445 | | if ((retryDelay.HasValue) && (!cancellationToken.IsCancellationRequested)) |
| | 446 | | { |
| 24 | 447 | | await Task.Delay(retryDelay.Value, cancellationToken).ConfigureAwait(false); |
| 24 | 448 | | tryTimeout = RetryPolicy.CalculateTryTimeout(failedAttemptCount); |
| | 449 | | } |
| | 450 | | else |
| | 451 | | { |
| 98 | 452 | | timeoutTokenSource?.Token.ThrowIfCancellationRequested<TimeoutException>(); |
| 94 | 453 | | throw; |
| | 454 | | } |
| | 455 | | } |
| | 456 | | finally |
| | 457 | | { |
| 154 | 458 | | timeoutTokenSource?.Dispose(); |
| 154 | 459 | | linkedTokenSource?.Dispose(); |
| | 460 | | } |
| | 461 | | } |
| | 462 | |
|
| | 463 | | // If no value has been returned nor exception thrown by this point, |
| | 464 | | // then cancellation has been requested. |
| | 465 | |
|
| 0 | 466 | | throw new TaskCanceledException(); |
| 32 | 467 | | } |
| | 468 | |
|
| | 469 | | /// <summary> |
| | 470 | | /// Applies the checkpoint store's <see cref="RetryPolicy" /> to a specified function. |
| | 471 | | /// </summary> |
| | 472 | | /// |
| | 473 | | /// <param name="functionToRetry">The function to which the retry policy should be applied.</param> |
| | 474 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> instance to signal the request to cancel t |
| | 475 | | /// |
| | 476 | | /// <typeparam name="T">The type returned by the function to be executed.</typeparam> |
| | 477 | | /// |
| | 478 | | /// <returns>The value returned by the function to which the retry policy has been applied.</returns> |
| | 479 | | /// |
| | 480 | | private async Task<T> ApplyRetryPolicy<T>(Func<CancellationToken, Task<T>> functionToRetry, |
| | 481 | | CancellationToken cancellationToken) |
| | 482 | | { |
| 112 | 483 | | var result = default(T); |
| | 484 | |
|
| | 485 | | async Task wrapper(CancellationToken token) |
| | 486 | | { |
| 132 | 487 | | result = await functionToRetry(token).ConfigureAwait(false); |
| 28 | 488 | | }; |
| | 489 | |
|
| 112 | 490 | | await ApplyRetryPolicy(wrapper, cancellationToken).ConfigureAwait(false); |
| 28 | 491 | | return result; |
| 28 | 492 | | } |
| | 493 | | } |
| | 494 | | } |