-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
Copy pathBlobsCheckpointStore.cs
835 lines (738 loc) · 47.8 KB
/
BlobsCheckpointStore.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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Azure.Core;
using Azure.Messaging.EventHubs.Consumer;
using Azure.Messaging.EventHubs.Core;
using Azure.Messaging.EventHubs.Primitives;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using Azure.Storage.Blobs.Specialized;
namespace Azure.Messaging.EventHubs.Processor
{
/// <summary>
/// A storage blob service that keeps track of checkpoints and ownership.
/// </summary>
///
internal partial class BlobsCheckpointStore : StorageManager
{
#pragma warning disable CA1802 // Use a constant field
/// <summary>A message to use when throwing exception when checkpoint container or blob does not exists.</summary>
private static readonly string BlobsResourceDoesNotExist = "The Azure Storage Blobs container or blob used by the Event Processor Client does not exist.";
#pragma warning restore CA1810
/// <summary>A regular expression used to capture strings enclosed in double quotes.</summary>
private static readonly Regex DoubleQuotesExpression = new Regex("\"(.*)\"", RegexOptions.Compiled);
/// <summary>An ETag value to be used for permissive matching when querying Storage.</summary>
private static readonly ETag IfNoneMatchAllTag = new ETag("*");
/// <summary>
/// Specifies a string that filters the results to return only checkpoint blobs whose name begins
/// with the specified prefix.
/// </summary>
///
private const string CheckpointPrefix = "{0}/{1}/{2}/checkpoint/";
/// <summary>
/// Specifies a string that filters the results to return only legacy checkpoint blobs whose name begins
/// with the specified prefix.
/// </summary>
///
private const string LegacyCheckpointPrefix = "{0}/{1}/{2}/";
/// <summary>
/// Specifies a string that filters the results to return only ownership blobs whose name begins
/// with the specified prefix.
/// </summary>
///
private const string OwnershipPrefix = "{0}/{1}/{2}/ownership/";
/// <summary>
/// The client used to interact with the Azure Blob Storage service.
/// </summary>
///
private BlobContainerClient ContainerClient { get; }
/// <summary>
/// The active policy which governs retry attempts for the
/// <see cref="BlobsCheckpointStore" />.
/// </summary>
///
private EventHubsRetryPolicy RetryPolicy { get; }
/// <summary>
/// Indicates whether to read legacy checkpoints when no current version checkpoints are available.
/// </summary>
///
private bool ReadLegacyCheckpoints { get; }
/// <summary>
/// Initializes a new instance of the <see cref="BlobsCheckpointStore" /> class.
/// </summary>
///
/// <param name="blobContainerClient">The client used to interact with the Azure Blob Storage service.</param>
/// <param name="retryPolicy">The retry policy to use as the basis for interacting with the Storage Blobs service.</param>
/// <param name="readLegacyCheckpoints">Indicates whether to read legacy checkpoint when no current version checkpoint is available for a partition.</param>
///
public BlobsCheckpointStore(BlobContainerClient blobContainerClient,
EventHubsRetryPolicy retryPolicy,
bool readLegacyCheckpoints = false)
{
Argument.AssertNotNull(blobContainerClient, nameof(blobContainerClient));
Argument.AssertNotNull(retryPolicy, nameof(retryPolicy));
ContainerClient = blobContainerClient;
RetryPolicy = retryPolicy;
ReadLegacyCheckpoints = readLegacyCheckpoints;
BlobsCheckpointStoreCreated(nameof(BlobsCheckpointStore), blobContainerClient.AccountName, blobContainerClient.Name);
}
/// <summary>
/// Retrieves a complete ownership list from the storage blob service.
/// </summary>
///
/// <param name="fullyQualifiedNamespace">The fully qualified Event Hubs namespace the ownership are associated with. This is likely to be similar to <c>{yournamespace}.servicebus.windows.net</c>.</param>
/// <param name="eventHubName">The name of the specific Event Hub the ownership are associated with, relative to the Event Hubs namespace that contains it.</param>
/// <param name="consumerGroup">The name of the consumer group the ownership are associated with.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken" /> instance to signal the request to cancel the operation.</param>
///
/// <returns>An enumerable containing all the existing ownership for the associated Event Hub and consumer group.</returns>
///
public override async Task<IEnumerable<EventProcessorPartitionOwnership>> ListOwnershipAsync(string fullyQualifiedNamespace,
string eventHubName,
string consumerGroup,
CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested<TaskCanceledException>();
ListOwnershipStart(fullyQualifiedNamespace, eventHubName, consumerGroup);
List<EventProcessorPartitionOwnership> result = null;
try
{
var prefix = string.Format(CultureInfo.InvariantCulture, OwnershipPrefix, fullyQualifiedNamespace.ToLowerInvariant(), eventHubName.ToLowerInvariant(), consumerGroup.ToLowerInvariant());
async Task<List<EventProcessorPartitionOwnership>> listOwnershipAsync(CancellationToken listOwnershipToken)
{
var ownershipList = new List<EventProcessorPartitionOwnership>();
await foreach (BlobItem blob in ContainerClient.GetBlobsAsync(traits: BlobTraits.Metadata, prefix: prefix, cancellationToken: listOwnershipToken).ConfigureAwait(false))
{
// In case this key does not exist, ownerIdentifier is set to null. This will force the PartitionOwnership constructor
// to throw an exception.
blob.Metadata.TryGetValue(BlobMetadataKey.OwnerIdentifier, out var ownerIdentifier);
ownershipList.Add(new EventProcessorPartitionOwnership
{
FullyQualifiedNamespace = fullyQualifiedNamespace,
EventHubName = eventHubName,
ConsumerGroup = consumerGroup,
OwnerIdentifier = ownerIdentifier,
PartitionId = blob.Name.Substring(prefix.Length),
LastModifiedTime = blob.Properties.LastModified.GetValueOrDefault(),
Version = blob.Properties.ETag.ToString()
});
}
return ownershipList;
};
result = await ApplyRetryPolicy(listOwnershipAsync, cancellationToken).ConfigureAwait(false);
return result;
}
catch (RequestFailedException ex) when (ex.ErrorCode == BlobErrorCode.ContainerNotFound)
{
ListOwnershipError(fullyQualifiedNamespace, eventHubName, consumerGroup, ex);
throw new RequestFailedException(BlobsResourceDoesNotExist);
}
finally
{
ListOwnershipComplete(fullyQualifiedNamespace, eventHubName, consumerGroup, result?.Count ?? 0);
}
}
/// <summary>
/// Attempts to claim ownership of partitions for processing.
/// </summary>
///
/// <param name="partitionOwnership">An enumerable containing all the ownership to claim.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken" /> instance to signal the request to cancel the operation.</param>
///
/// <returns>An enumerable containing the successfully claimed ownership.</returns>
///
public override async Task<IEnumerable<EventProcessorPartitionOwnership>> ClaimOwnershipAsync(IEnumerable<EventProcessorPartitionOwnership> partitionOwnership,
CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested<TaskCanceledException>();
var claimedOwnership = new List<EventProcessorPartitionOwnership>();
var metadata = new Dictionary<string, string>();
Response<BlobContentInfo> contentInfoResponse;
Response<BlobInfo> infoResponse;
foreach (EventProcessorPartitionOwnership ownership in partitionOwnership)
{
ClaimOwnershipStart(ownership.PartitionId, ownership.FullyQualifiedNamespace, ownership.EventHubName, ownership.ConsumerGroup, ownership.OwnerIdentifier);
metadata[BlobMetadataKey.OwnerIdentifier] = ownership.OwnerIdentifier;
var blobRequestConditions = new BlobRequestConditions();
var blobName = string.Format(CultureInfo.InvariantCulture, OwnershipPrefix + ownership.PartitionId, ownership.FullyQualifiedNamespace.ToLowerInvariant(), ownership.EventHubName.ToLowerInvariant(), ownership.ConsumerGroup.ToLowerInvariant());
var blobClient = ContainerClient.GetBlobClient(blobName);
try
{
// Even though documentation states otherwise, we cannot use UploadAsync when the blob already exists in
// the current storage SDK. For this reason, we are using the specified ETag as an indication of what
// method to use.
if (ownership.Version == null)
{
blobRequestConditions.IfNoneMatch = IfNoneMatchAllTag;
async Task<Response<BlobContentInfo>> uploadBlobAsync(CancellationToken uploadToken)
{
using var blobContent = new MemoryStream(Array.Empty<byte>());
try
{
return await blobClient.UploadAsync(blobContent, metadata: metadata, conditions: blobRequestConditions, cancellationToken: uploadToken).ConfigureAwait(false);
}
catch (RequestFailedException ex) when (ex.ErrorCode == BlobErrorCode.BlobAlreadyExists)
{
// A blob could have just been created by another Event Processor that claimed ownership of this
// partition. In this case, there's no point in retrying because we don't have the correct ETag.
OwnershipNotClaimable(ownership.PartitionId, ownership.FullyQualifiedNamespace, ownership.EventHubName, ownership.ConsumerGroup, ownership.OwnerIdentifier, ex.Message);
return null;
}
};
contentInfoResponse = await ApplyRetryPolicy(uploadBlobAsync, cancellationToken).ConfigureAwait(false);
if (contentInfoResponse == null)
{
continue;
}
ownership.LastModifiedTime = contentInfoResponse.Value.LastModified;
ownership.Version = contentInfoResponse.Value.ETag.ToString();
}
else
{
blobRequestConditions.IfMatch = new ETag(ownership.Version);
infoResponse = await ApplyRetryPolicy(uploadToken => blobClient.SetMetadataAsync(metadata, blobRequestConditions, uploadToken), cancellationToken).ConfigureAwait(false);
ownership.LastModifiedTime = infoResponse.Value.LastModified;
ownership.Version = infoResponse.Value.ETag.ToString();
}
// Small workaround to retrieve the eTag. The current storage SDK returns it enclosed in
// double quotes ('"ETAG_VALUE"' instead of 'ETAG_VALUE').
var match = DoubleQuotesExpression.Match(ownership.Version);
if (match.Success)
{
ownership.Version = match.Groups[1].ToString();
}
claimedOwnership.Add(ownership);
OwnershipClaimed(ownership.PartitionId, ownership.FullyQualifiedNamespace, ownership.EventHubName, ownership.ConsumerGroup, ownership.OwnerIdentifier);
}
catch (RequestFailedException ex) when (ex.ErrorCode == BlobErrorCode.ConditionNotMet)
{
OwnershipNotClaimable(ownership.PartitionId, ownership.FullyQualifiedNamespace, ownership.EventHubName, ownership.ConsumerGroup, ownership.OwnerIdentifier, ex.ToString());
}
catch (RequestFailedException ex) when (ex.ErrorCode == BlobErrorCode.ContainerNotFound || ex.ErrorCode == BlobErrorCode.BlobNotFound)
{
ClaimOwnershipError(ownership.PartitionId, ownership.FullyQualifiedNamespace, ownership.EventHubName, ownership.ConsumerGroup, ownership.OwnerIdentifier, ex);
throw new RequestFailedException(BlobsResourceDoesNotExist);
}
catch (Exception ex)
{
ClaimOwnershipError(ownership.PartitionId, ownership.FullyQualifiedNamespace, ownership.EventHubName, ownership.ConsumerGroup, ownership.OwnerIdentifier, ex);
throw;
}
finally
{
ClaimOwnershipComplete(ownership.PartitionId, ownership.FullyQualifiedNamespace, ownership.EventHubName, ownership.ConsumerGroup, ownership.OwnerIdentifier);
}
}
return claimedOwnership;
}
/// <summary>
/// Retrieves a list of all the checkpoints in a data store for a given namespace, Event Hub and consumer group.
/// </summary>
///
/// <param name="fullyQualifiedNamespace">The fully qualified Event Hubs namespace the ownership are associated with. This is likely to be similar to <c>{yournamespace}.servicebus.windows.net</c>.</param>
/// <param name="eventHubName">The name of the specific Event Hub the ownership are associated with, relative to the Event Hubs namespace that contains it.</param>
/// <param name="consumerGroup">The name of the consumer group the ownership are associated with.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken" /> instance to signal the request to cancel the operation.</param>
///
/// <returns>An enumerable containing all the existing checkpoints for the associated Event Hub and consumer group.</returns>
///
public override async Task<IEnumerable<EventProcessorCheckpoint>> ListCheckpointsAsync(string fullyQualifiedNamespace,
string eventHubName,
string consumerGroup,
CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested<TaskCanceledException>();
ListCheckpointsStart(fullyQualifiedNamespace, eventHubName, consumerGroup);
async Task<List<EventProcessorCheckpoint>> listCheckpointsAsync(CancellationToken listCheckpointsToken)
{
var prefix = string.Format(CultureInfo.InvariantCulture, CheckpointPrefix, fullyQualifiedNamespace.ToLowerInvariant(), eventHubName.ToLowerInvariant(), consumerGroup.ToLowerInvariant());
var checkpoints = new List<EventProcessorCheckpoint>();
await foreach (BlobItem blob in ContainerClient.GetBlobsAsync(traits: BlobTraits.Metadata, prefix: prefix, cancellationToken: listCheckpointsToken).ConfigureAwait(false))
{
var partitionId = blob.Name.Substring(prefix.Length);
var startingPosition = default(EventPosition?);
var offset = default(long?);
var sequenceNumber = default(long?);
if (blob.Metadata.TryGetValue(BlobMetadataKey.Offset, out var str) && long.TryParse(str, NumberStyles.Integer, CultureInfo.InvariantCulture, out var result))
{
offset = result;
startingPosition = EventPosition.FromOffset(result, false);
}
else if (blob.Metadata.TryGetValue(BlobMetadataKey.SequenceNumber, out str) && long.TryParse(str, NumberStyles.Integer, CultureInfo.InvariantCulture, out result))
{
sequenceNumber = result;
startingPosition = EventPosition.FromSequenceNumber(result, false);
}
// If either the offset or the sequence number was not populated,
// this is not a valid checkpoint.
if (startingPosition.HasValue)
{
checkpoints.Add(new BlobStorageCheckpoint
{
FullyQualifiedNamespace = fullyQualifiedNamespace,
EventHubName = eventHubName,
ConsumerGroup = consumerGroup,
PartitionId = partitionId,
StartingPosition = startingPosition.Value,
Offset = offset,
SequenceNumber = sequenceNumber
});
}
else
{
InvalidCheckpointFound(partitionId, fullyQualifiedNamespace, eventHubName, consumerGroup);
}
}
return checkpoints;
};
async Task<List<EventProcessorCheckpoint>> listLegacyCheckpointsAsync(List<EventProcessorCheckpoint> existingCheckpoints, CancellationToken listCheckpointsToken)
{
// Legacy checkpoints are not normalized to lowercase
var legacyPrefix = string.Format(CultureInfo.InvariantCulture, LegacyCheckpointPrefix, fullyQualifiedNamespace, eventHubName, consumerGroup);
var checkpoints = new List<EventProcessorCheckpoint>();
await foreach (BlobItem blob in ContainerClient.GetBlobsAsync(prefix: legacyPrefix, cancellationToken: listCheckpointsToken).ConfigureAwait(false))
{
// Skip new checkpoints and empty blobs
if (blob.Properties.ContentLength == 0)
{
continue;
}
var partitionId = blob.Name.Substring(legacyPrefix.Length);
// Check whether there is already a checkpoint for this partition id
if (existingCheckpoints.Any(existingCheckpoint => string.Equals(existingCheckpoint.PartitionId, partitionId, StringComparison.Ordinal)))
{
continue;
}
var startingPosition = default(EventPosition?);
BlobBaseClient blobClient = ContainerClient.GetBlobClient(blob.Name);
using var memoryStream = new MemoryStream();
await blobClient.DownloadToAsync(memoryStream, listCheckpointsToken).ConfigureAwait(false);
TryReadLegacyCheckpoint(
memoryStream.GetBuffer().AsSpan(0, (int)memoryStream.Length),
out long? offset,
out long? sequenceNumber);
if (offset.HasValue)
{
startingPosition = EventPosition.FromOffset(offset.Value, false);
}
else if (sequenceNumber.HasValue)
{
startingPosition = EventPosition.FromSequenceNumber(sequenceNumber.Value, false);
}
if (startingPosition.HasValue)
{
checkpoints.Add(new BlobStorageCheckpoint
{
FullyQualifiedNamespace = fullyQualifiedNamespace,
EventHubName = eventHubName,
ConsumerGroup = consumerGroup,
PartitionId = partitionId,
StartingPosition = startingPosition.Value,
Offset = offset,
SequenceNumber = sequenceNumber
});
}
else
{
InvalidCheckpointFound(partitionId, fullyQualifiedNamespace, eventHubName, consumerGroup);
}
}
return checkpoints;
};
List<EventProcessorCheckpoint> checkpoints = null;
try
{
checkpoints = await ApplyRetryPolicy(listCheckpointsAsync, cancellationToken).ConfigureAwait(false);
if (ReadLegacyCheckpoints)
{
checkpoints.AddRange(await ApplyRetryPolicy(ct => listLegacyCheckpointsAsync(checkpoints, ct), cancellationToken).ConfigureAwait(false));
}
return checkpoints;
}
catch (RequestFailedException ex) when (ex.ErrorCode == BlobErrorCode.ContainerNotFound)
{
ListCheckpointsError(fullyQualifiedNamespace, eventHubName, consumerGroup, ex);
throw new RequestFailedException(BlobsResourceDoesNotExist);
}
catch (Exception ex)
{
ListCheckpointsError(fullyQualifiedNamespace, eventHubName, consumerGroup, ex);
throw;
}
finally
{
ListCheckpointsComplete(fullyQualifiedNamespace, eventHubName, consumerGroup, checkpoints?.Count ?? 0);
}
}
/// <summary>
/// Updates the checkpoint using the given information for the associated partition and consumer group in the storage blob service.
/// </summary>
///
/// <param name="checkpoint">The checkpoint containing the information to be stored.</param>
/// <param name="eventData">The event to use as the basis for the checkpoint's starting position.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken" /> instance to signal the request to cancel the operation.</param>
///
/// <returns>A task to be resolved on when the operation has completed.</returns>
///
public override async Task UpdateCheckpointAsync(EventProcessorCheckpoint checkpoint,
EventData eventData,
CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested<TaskCanceledException>();
UpdateCheckpointStart(checkpoint.PartitionId, checkpoint.FullyQualifiedNamespace, checkpoint.EventHubName, checkpoint.ConsumerGroup);
var blobName = string.Format(CultureInfo.InvariantCulture, CheckpointPrefix + checkpoint.PartitionId, checkpoint.FullyQualifiedNamespace.ToLowerInvariant(), checkpoint.EventHubName.ToLowerInvariant(), checkpoint.ConsumerGroup.ToLowerInvariant());
var blobClient = ContainerClient.GetBlobClient(blobName);
var metadata = new Dictionary<string, string>()
{
{ BlobMetadataKey.Offset, eventData.Offset.ToString(CultureInfo.InvariantCulture) },
{ BlobMetadataKey.SequenceNumber, eventData.SequenceNumber.ToString(CultureInfo.InvariantCulture) }
};
try
{
try
{
// Assume the blob is present and attempt to set the metadata.
await ApplyRetryPolicy(token => blobClient.SetMetadataAsync(metadata, cancellationToken: token), cancellationToken).ConfigureAwait(false);
}
catch (RequestFailedException ex) when ((ex.ErrorCode == BlobErrorCode.BlobNotFound) || (ex.ErrorCode == BlobErrorCode.ContainerNotFound))
{
// If the blob wasn't present, fall-back to trying to create a new one.
await ApplyRetryPolicy(async token =>
{
using var blobContent = new MemoryStream(Array.Empty<byte>());
await blobClient.UploadAsync(blobContent, metadata: metadata, cancellationToken: token).ConfigureAwait(false);
}, cancellationToken).ConfigureAwait(false);
}
}
catch (RequestFailedException ex) when (ex.ErrorCode == BlobErrorCode.ContainerNotFound)
{
UpdateCheckpointError(checkpoint.PartitionId, checkpoint.FullyQualifiedNamespace, checkpoint.EventHubName, checkpoint.ConsumerGroup, ex);
throw new RequestFailedException(BlobsResourceDoesNotExist);
}
catch (Exception ex)
{
UpdateCheckpointError(checkpoint.PartitionId, checkpoint.FullyQualifiedNamespace, checkpoint.EventHubName, checkpoint.ConsumerGroup, ex);
throw;
}
finally
{
UpdateCheckpointComplete(checkpoint.PartitionId, checkpoint.FullyQualifiedNamespace, checkpoint.EventHubName, checkpoint.ConsumerGroup);
}
}
/// <summary>
/// Applies the checkpoint store's <see cref="RetryPolicy" /> to a specified function.
/// </summary>
///
/// <param name="functionToRetry">The function to which the retry policy should be applied.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken" /> instance to signal the request to cancel the operation.</param>
///
/// <returns>The value returned by the function to which the retry policy has been applied.</returns>
///
private async Task ApplyRetryPolicy(Func<CancellationToken, Task> functionToRetry,
CancellationToken cancellationToken)
{
TimeSpan? retryDelay;
var failedAttemptCount = 0;
var tryTimeout = RetryPolicy.CalculateTryTimeout(0);
var timeoutTokenSource = default(CancellationTokenSource);
var linkedTokenSource = default(CancellationTokenSource);
while (!cancellationToken.IsCancellationRequested)
{
try
{
timeoutTokenSource = new CancellationTokenSource(tryTimeout);
linkedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutTokenSource.Token);
await functionToRetry(linkedTokenSource.Token).ConfigureAwait(false);
return;
}
catch (Exception ex)
{
// Determine if there should be a retry for the next attempt; if so enforce the delay but do not quit the loop.
// Otherwise, mark the exception as active and break out of the loop.
++failedAttemptCount;
retryDelay = RetryPolicy.CalculateRetryDelay(ex, failedAttemptCount);
if ((retryDelay.HasValue) && (!cancellationToken.IsCancellationRequested))
{
await Task.Delay(retryDelay.Value, cancellationToken).ConfigureAwait(false);
tryTimeout = RetryPolicy.CalculateTryTimeout(failedAttemptCount);
}
else
{
timeoutTokenSource?.Token.ThrowIfCancellationRequested<TimeoutException>();
throw;
}
}
finally
{
timeoutTokenSource?.Dispose();
linkedTokenSource?.Dispose();
}
}
// If no value has been returned nor exception thrown by this point,
// then cancellation has been requested.
throw new TaskCanceledException();
}
/// <summary>
/// Applies the checkpoint store's <see cref="RetryPolicy" /> to a specified function.
/// </summary>
///
/// <param name="functionToRetry">The function to which the retry policy should be applied.</param>
/// <param name="cancellationToken">A <see cref="CancellationToken" /> instance to signal the request to cancel the operation.</param>
///
/// <typeparam name="T">The type returned by the function to be executed.</typeparam>
///
/// <returns>The value returned by the function to which the retry policy has been applied.</returns>
///
private async Task<T> ApplyRetryPolicy<T>(Func<CancellationToken, Task<T>> functionToRetry,
CancellationToken cancellationToken)
{
var result = default(T);
async Task wrapper(CancellationToken token)
{
result = await functionToRetry(token).ConfigureAwait(false);
};
await ApplyRetryPolicy(wrapper, cancellationToken).ConfigureAwait(false);
return result;
}
/// <summary>
/// Attempts to read a legacy checkpoint JSON format and extract an offset and a sequence number
/// </summary>
/// <param name="data">The binary representation of the checkpoint JSON.</param>
/// <param name="offset">The parsed offset. null if not found.</param>
/// <param name="sequenceNumber">The parsed sequence number. null if not found.</param>
/// <remarks>
/// Sample checkpoint JSON:
/// {
/// "PartitionId":"0",
/// "Owner":"681d365b-de1b-4288-9733-76294e17daf0",
/// "Token":"2d0c4276-827d-4ca4-a345-729caeca3b82",
/// "Epoch":386,
/// "Offset":"8591964920",
/// "SequenceNumber":960180
/// }
/// </remarks>
private static void TryReadLegacyCheckpoint(Span<byte> data, out long? offset, out long? sequenceNumber)
{
offset = null;
sequenceNumber = null;
var jsonReader = new Utf8JsonReader(data);
try
{
if (!jsonReader.Read() || jsonReader.TokenType != JsonTokenType.StartObject) return;
while (jsonReader.Read() && jsonReader.TokenType == JsonTokenType.PropertyName)
{
switch (jsonReader.GetString())
{
case "Offset":
if (!jsonReader.Read() ||
jsonReader.GetString() is not string offsetString ||
!long.TryParse(offsetString, out long offsetValue))
{
return;
}
offset = offsetValue;
break;
case "SequenceNumber":
if (!jsonReader.Read() ||
!jsonReader.TryGetInt64(out long sequenceNumberValue))
{
return;
}
sequenceNumber = sequenceNumberValue;
break;
default:
jsonReader.Skip();
break;
}
}
}
catch (JsonException)
{
// Ignore this because if the data is malformed, it will be treated as if the checkpoint didn't exist.
}
}
/// <summary>
/// Indicates that an attempt to retrieve a list of ownership has completed.
/// </summary>
///
/// <param name="fullyQualifiedNamespace">The fully qualified Event Hubs namespace the ownership are associated with. This is likely to be similar to <c>{yournamespace}.servicebus.windows.net</c>.</param>
/// <param name="eventHubName">The name of the specific Event Hub the ownership are associated with, relative to the Event Hubs namespace that contains it.</param>
/// <param name="consumerGroup">The name of the consumer group the ownership are associated with.</param>
/// <param name="ownershipCount">The amount of ownership received from the storage service.</param>
///
partial void ListOwnershipComplete(string fullyQualifiedNamespace, string eventHubName, string consumerGroup, int ownershipCount);
/// <summary>
/// Indicates that an unhandled exception was encountered while retrieving a list of ownership.
/// </summary>
///
/// <param name="fullyQualifiedNamespace">The fully qualified Event Hubs namespace the ownership are associated with. This is likely to be similar to <c>{yournamespace}.servicebus.windows.net</c>.</param>
/// <param name="eventHubName">The name of the specific Event Hub the ownership are associated with, relative to the Event Hubs namespace that contains it.</param>
/// <param name="consumerGroup">The name of the consumer group the ownership are associated with.</param>
/// <param name="exception">The message for the exception that occurred.</param>
///
partial void ListOwnershipError(string fullyQualifiedNamespace, string eventHubName, string consumerGroup, Exception exception);
/// <summary>
/// Indicates that an attempt to retrieve a list of ownership has started.
/// </summary>
///
/// <param name="fullyQualifiedNamespace">The fully qualified Event Hubs namespace the ownership are associated with. This is likely to be similar to <c>{yournamespace}.servicebus.windows.net</c>.</param>
/// <param name="eventHubName">The name of the specific Event Hub the ownership are associated with, relative to the Event Hubs namespace that contains it.</param>
/// <param name="consumerGroup">The name of the consumer group the ownership are associated with.</param>
///
partial void ListOwnershipStart(string fullyQualifiedNamespace, string eventHubName, string consumerGroup);
/// <summary>
/// Indicates that an attempt to retrieve a list of checkpoints has completed.
/// </summary>
///
/// <param name="fullyQualifiedNamespace">The fully qualified Event Hubs namespace the checkpoints are associated with. This is likely to be similar to <c>{yournamespace}.servicebus.windows.net</c>.</param>
/// <param name="eventHubName">The name of the specific Event Hub the checkpoints are associated with, relative to the Event Hubs namespace that contains it.</param>
/// <param name="consumerGroup">The name of the consumer group the checkpoints are associated with.</param>
/// <param name="checkpointCount">The amount of checkpoints received from the storage service.</param>
///
partial void ListCheckpointsComplete(string fullyQualifiedNamespace, string eventHubName, string consumerGroup, int checkpointCount);
/// <summary>
/// Indicates that an unhandled exception was encountered while retrieving a list of checkpoints.
/// </summary>
///
/// <param name="fullyQualifiedNamespace">The fully qualified Event Hubs namespace the checkpoints are associated with. This is likely to be similar to <c>{yournamespace}.servicebus.windows.net</c>.</param>
/// <param name="eventHubName">The name of the specific Event Hub the checkpoints are associated with, relative to the Event Hubs namespace that contains it.</param>
/// <param name="consumerGroup">The name of the consumer group the ownership are associated with.</param>
/// <param name="exception">The message for the exception that occurred.</param>
///
partial void ListCheckpointsError(string fullyQualifiedNamespace, string eventHubName, string consumerGroup, Exception exception);
/// <summary>
/// Indicates that invalid checkpoint data was found during an attempt to retrieve a list of checkpoints.
/// </summary>
///
/// <param name="partitionId">The identifier of the partition the data is associated with.</param>
/// <param name="fullyQualifiedNamespace">The fully qualified Event Hubs namespace the data is associated with. This is likely to be similar to <c>{yournamespace}.servicebus.windows.net</c>.</param>
/// <param name="eventHubName">The name of the specific Event Hub the data is associated with, relative to the Event Hubs namespace that contains it.</param>
/// <param name="consumerGroup">The name of the consumer group the data is associated with.</param>
///
partial void InvalidCheckpointFound(string partitionId, string fullyQualifiedNamespace, string eventHubName, string consumerGroup);
/// <summary>
/// Indicates that an attempt to retrieve a list of checkpoints has started.
/// </summary>
///
/// <param name="fullyQualifiedNamespace">The fully qualified Event Hubs namespace the checkpoints are associated with. This is likely to be similar to <c>{yournamespace}.servicebus.windows.net</c>.</param>
/// <param name="eventHubName">The name of the specific Event Hub the checkpoints are associated with, relative to the Event Hubs namespace that contains it.</param>
/// <param name="consumerGroup">The name of the consumer group the checkpoints are associated with.</param>
///
partial void ListCheckpointsStart(string fullyQualifiedNamespace, string eventHubName, string consumerGroup);
/// <summary>
/// Indicates that an unhandled exception was encountered while updating a checkpoint.
/// </summary>
///
/// <param name="partitionId">The identifier of the partition being checkpointed.</param>
/// <param name="fullyQualifiedNamespace">The fully qualified Event Hubs namespace the checkpoint is associated with. This is likely to be similar to <c>{yournamespace}.servicebus.windows.net</c>.</param>
/// <param name="eventHubName">The name of the specific Event Hub the checkpoint is associated with, relative to the Event Hubs namespace that contains it.</param>
/// <param name="consumerGroup">The name of the consumer group the checkpoint is associated with.</param>
/// <param name="exception">The message for the exception that occurred.</param>
///
partial void UpdateCheckpointError(string partitionId, string fullyQualifiedNamespace, string eventHubName, string consumerGroup, Exception exception);
/// <summary>
/// Indicates that an attempt to update a checkpoint has completed.
/// </summary>
///
/// <param name="partitionId">The identifier of the partition being checkpointed.</param>
/// <param name="fullyQualifiedNamespace">The fully qualified Event Hubs namespace the checkpoint is associated with. This is likely to be similar to <c>{yournamespace}.servicebus.windows.net</c>.</param>
/// <param name="eventHubName">The name of the specific Event Hub the checkpoint is associated with, relative to the Event Hubs namespace that contains it.</param>
/// <param name="consumerGroup">The name of the consumer group the checkpoint is associated with.</param>
///
partial void UpdateCheckpointComplete(string partitionId, string fullyQualifiedNamespace, string eventHubName, string consumerGroup);
/// <summary>
/// Indicates that an attempt to create/update a checkpoint has started.
/// </summary>
///
/// <param name="partitionId">The identifier of the partition being checkpointed.</param>
/// <param name="fullyQualifiedNamespace">The fully qualified Event Hubs namespace the checkpoint is associated with. This is likely to be similar to <c>{yournamespace}.servicebus.windows.net</c>.</param>
/// <param name="eventHubName">The name of the specific Event Hub the checkpoint is associated with, relative to the Event Hubs namespace that contains it.</param>
/// <param name="consumerGroup">The name of the consumer group the checkpoint is associated with.</param>
///
partial void UpdateCheckpointStart(string partitionId, string fullyQualifiedNamespace, string eventHubName, string consumerGroup);
/// <summary>
/// Indicates that an attempt to retrieve claim partition ownership has completed.
/// </summary>
///
/// <param name="partitionId">The identifier of the partition being claimed.</param>
/// <param name="fullyQualifiedNamespace">The fully qualified Event Hubs namespace the ownership is associated with. This is likely to be similar to <c>{yournamespace}.servicebus.windows.net</c>.</param>
/// <param name="eventHubName">The name of the specific Event Hub the ownership is associated with, relative to the Event Hubs namespace that contains it.</param>
/// <param name="consumerGroup">The name of the consumer group the ownership is associated with.</param>
/// <param name="ownerIdentifier">The identifier of the processor that attempted to claim the ownership for.</param>
///
partial void ClaimOwnershipComplete(string partitionId, string fullyQualifiedNamespace, string eventHubName, string consumerGroup, string ownerIdentifier);
/// <summary>
/// Indicates that an exception was encountered while attempting to retrieve claim partition ownership.
/// </summary>
///
/// <param name="partitionId">The identifier of the partition being claimed.</param>
/// <param name="fullyQualifiedNamespace">The fully qualified Event Hubs namespace the ownership is associated with. This is likely to be similar to <c>{yournamespace}.servicebus.windows.net</c>.</param>
/// <param name="eventHubName">The name of the specific Event Hub the ownership is associated with, relative to the Event Hubs namespace that contains it.</param>
/// <param name="consumerGroup">The name of the consumer group the ownership is associated with.</param>
/// <param name="ownerIdentifier">The identifier of the processor that attempted to claim the ownership for.</param>
/// <param name="exception">The message for the exception that occurred.</param>
///
partial void ClaimOwnershipError(string partitionId, string fullyQualifiedNamespace, string eventHubName, string consumerGroup, string ownerIdentifier, Exception exception);
/// <summary>
/// Indicates that ownership was unable to be claimed.
/// </summary>
///
/// <param name="partitionId">The identifier of the partition being claimed.</param>
/// <param name="fullyQualifiedNamespace">The fully qualified Event Hubs namespace the ownership is associated with. This is likely to be similar to <c>{yournamespace}.servicebus.windows.net</c>.</param>
/// <param name="eventHubName">The name of the specific Event Hub the ownership is associated with, relative to the Event Hubs namespace that contains it.</param>
/// <param name="consumerGroup">The name of the consumer group the ownership is associated with.</param>
/// <param name="ownerIdentifier">The identifier of the processor that attempted to claim the ownership for.</param>
/// <param name="message">The message for the failure.</param>
///
partial void OwnershipNotClaimable(string partitionId, string fullyQualifiedNamespace, string eventHubName, string consumerGroup, string ownerIdentifier, string message);
/// <summary>
/// Indicates that ownership was successfully claimed.
/// </summary>
///
/// <param name="partitionId">The identifier of the partition being claimed.</param>
/// <param name="fullyQualifiedNamespace">The fully qualified Event Hubs namespace the ownership is associated with. This is likely to be similar to <c>{yournamespace}.servicebus.windows.net</c>.</param>
/// <param name="eventHubName">The name of the specific Event Hub the ownership is associated with, relative to the Event Hubs namespace that contains it.</param>
/// <param name="consumerGroup">The name of the consumer group the ownership is associated with.</param>
/// <param name="ownerIdentifier">The identifier of the processor that attempted to claim the ownership for.</param>
///
partial void OwnershipClaimed(string partitionId, string fullyQualifiedNamespace, string eventHubName, string consumerGroup, string ownerIdentifier);
/// <summary>
/// Indicates that an attempt to claim a partition ownership has started.
/// </summary>
///
/// <param name="partitionId">The identifier of the partition being claimed.</param>
/// <param name="fullyQualifiedNamespace">The fully qualified Event Hubs namespace the ownership is associated with. This is likely to be similar to <c>{yournamespace}.servicebus.windows.net</c>.</param>
/// <param name="eventHubName">The name of the specific Event Hub the ownership is associated with, relative to the Event Hubs namespace that contains it.</param>
/// <param name="consumerGroup">The name of the consumer group the ownership is associated with.</param>
/// <param name="ownerIdentifier">The identifier of the processor that attempted to claim the ownership for.</param>
///
partial void ClaimOwnershipStart(string partitionId, string fullyQualifiedNamespace, string eventHubName, string consumerGroup, string ownerIdentifier);
/// <summary>
/// Indicates that a <see cref="BlobsCheckpointStore" /> was created.
/// </summary>
///
/// <param name="typeName">The type name for the checkpoint store.</param>
/// <param name="accountName">The Storage account name corresponding to the associated container client.</param>
/// <param name="containerName">The name of the associated container client.</param>
///
partial void BlobsCheckpointStoreCreated(string typeName, string accountName, string containerName);
/// <summary>
/// Contains the information to reflect the state of event processing for a given Event Hub partition.
/// Provides access to the offset and the sequence number retrieved from the blob.
/// </summary>
public class BlobStorageCheckpoint : EventProcessorCheckpoint
{
public long? Offset { get; set; }
public long? SequenceNumber { get; set; }
}
}
}