11// Copyright (c) Microsoft Corporation.
22// Licensed under the MIT license.
33
4- import { CheckpointStore , PartitionOwnership , Checkpoint } from "@azure/event-hubs" ;
4+ import {
5+ CheckpointStore ,
6+ PartitionOwnership ,
7+ Checkpoint ,
8+ OperationOptions
9+ } from "@azure/event-hubs" ;
510import { ContainerClient , Metadata , RestError , BlobSetMetadataResponse } from "@azure/storage-blob" ;
611import { logger , logErrorStackTrace } from "./log" ;
712import { throwTypeErrorIfParameterMissing } from "./util/error" ;
@@ -24,14 +29,19 @@ export class BlobCheckpointStore implements CheckpointStore {
2429 * <yournamespace>.servicebus.windows.net.
2530 * @param eventHubName - The event hub name.
2631 * @param consumerGroup - The consumer group name.
32+ * @param options - A set of options that can be specified to influence the behavior of this method.
33+ * - `abortSignal`: A signal used to request operation cancellation.
34+ * - `tracingOptions`: Options for configuring tracing.
2735 * @returns Partition ownership details of all the partitions that have had an owner.
2836 */
2937 async listOwnership (
3038 fullyQualifiedNamespace : string ,
3139 eventHubName : string ,
32- consumerGroup : string
40+ consumerGroup : string ,
41+ options : OperationOptions = { }
3342 ) : Promise < PartitionOwnership [ ] > {
3443 const partitionOwnershipArray : PartitionOwnership [ ] = [ ] ;
44+ const { abortSignal, tracingOptions } = options ;
3545
3646 const blobPrefix = BlobCheckpointStore . getBlobPrefix ( {
3747 type : "ownership" ,
@@ -42,8 +52,10 @@ export class BlobCheckpointStore implements CheckpointStore {
4252
4353 try {
4454 const blobs = this . _containerClient . listBlobsFlat ( {
55+ abortSignal,
4556 includeMetadata : true ,
46- prefix : blobPrefix
57+ prefix : blobPrefix ,
58+ tracingOptions
4759 } ) ;
4860
4961 for await ( const blob of blobs ) {
@@ -72,6 +84,9 @@ export class BlobCheckpointStore implements CheckpointStore {
7284 } catch ( err ) {
7385 logger . warning ( `Error occurred while fetching the list of blobs` , err . message ) ;
7486 logErrorStackTrace ( err ) ;
87+
88+ if ( err ?. name === "AbortError" ) throw err ;
89+
7590 throw new Error ( `Error occurred while fetching the list of blobs. \n${ err } ` ) ;
7691 }
7792 }
@@ -81,9 +96,15 @@ export class BlobCheckpointStore implements CheckpointStore {
8196 * successfully claimed.
8297 *
8398 * @param partitionOwnership - The list of partition ownership this instance is claiming to own.
99+ * @param options - A set of options that can be specified to influence the behavior of this method.
100+ * - `abortSignal`: A signal used to request operation cancellation.
101+ * - `tracingOptions`: Options for configuring tracing.
84102 * @returns A list partitions this instance successfully claimed ownership.
85103 */
86- async claimOwnership ( partitionOwnership : PartitionOwnership [ ] ) : Promise < PartitionOwnership [ ] > {
104+ async claimOwnership (
105+ partitionOwnership : PartitionOwnership [ ] ,
106+ options : OperationOptions = { }
107+ ) : Promise < PartitionOwnership [ ] > {
87108 const partitionOwnershipArray : PartitionOwnership [ ] = [ ] ;
88109 for ( const ownership of partitionOwnership ) {
89110 const blobName = BlobCheckpointStore . getBlobPrefix ( { type : "ownership" , ...ownership } ) ;
@@ -93,7 +114,8 @@ export class BlobCheckpointStore implements CheckpointStore {
93114 {
94115 ownerid : ownership . ownerId
95116 } ,
96- ownership . etag
117+ ownership . etag ,
118+ options
97119 ) ;
98120
99121 if ( updatedBlobResponse . lastModified ) {
@@ -138,12 +160,17 @@ export class BlobCheckpointStore implements CheckpointStore {
138160 * <yournamespace>.servicebus.windows.net.
139161 * @param eventHubName - The event hub name.
140162 * @param consumerGroup - The consumer group name.
163+ * @param options - A set of options that can be specified to influence the behavior of this method.
164+ * - `abortSignal`: A signal used to request operation cancellation.
165+ * - `tracingOptions`: Options for configuring tracing.
141166 */
142167 async listCheckpoints (
143168 fullyQualifiedNamespace : string ,
144169 eventHubName : string ,
145- consumerGroup : string
170+ consumerGroup : string ,
171+ options : OperationOptions = { }
146172 ) : Promise < Checkpoint [ ] > {
173+ const { abortSignal, tracingOptions } = options ;
147174 const blobPrefix = BlobCheckpointStore . getBlobPrefix ( {
148175 type : "checkpoint" ,
149176 fullyQualifiedNamespace,
@@ -152,8 +179,10 @@ export class BlobCheckpointStore implements CheckpointStore {
152179 } ) ;
153180
154181 const blobs = this . _containerClient . listBlobsFlat ( {
182+ abortSignal,
155183 includeMetadata : true ,
156- prefix : blobPrefix
184+ prefix : blobPrefix ,
185+ tracingOptions
157186 } ) ;
158187
159188 const checkpoints : Checkpoint [ ] = [ ] ;
@@ -188,9 +217,12 @@ export class BlobCheckpointStore implements CheckpointStore {
188217 * Updates the checkpoint in the data store for a partition.
189218 *
190219 * @param checkpoint - The checkpoint.
220+ * @param options - A set of options that can be specified to influence the behavior of this method.
221+ * - `abortSignal`: A signal used to request operation cancellation.
222+ * - `tracingOptions`: Options for configuring tracing.
191223 * @returns The new etag on successful update.
192224 */
193- async updateCheckpoint ( checkpoint : Checkpoint ) : Promise < void > {
225+ async updateCheckpoint ( checkpoint : Checkpoint , options : OperationOptions = { } ) : Promise < void > {
194226 throwTypeErrorIfParameterMissing (
195227 "updateCheckpoint" ,
196228 "sequenceNumber" ,
@@ -206,7 +238,8 @@ export class BlobCheckpointStore implements CheckpointStore {
206238 sequencenumber : checkpoint . sequenceNumber . toString ( ) ,
207239 offset : checkpoint . offset . toString ( )
208240 } ,
209- undefined
241+ undefined ,
242+ options
210243 ) ;
211244
212245 logger . verbose (
@@ -222,6 +255,9 @@ export class BlobCheckpointStore implements CheckpointStore {
222255 err . message
223256 ) ;
224257 logErrorStackTrace ( err ) ;
258+
259+ if ( err ?. name === "AbortError" ) throw err ;
260+
225261 throw new Error (
226262 `Error occurred while upating the checkpoint for partition: ${ checkpoint . partitionId } , ${ err } `
227263 ) ;
@@ -251,24 +287,31 @@ export class BlobCheckpointStore implements CheckpointStore {
251287 private async _setBlobMetadata (
252288 blobName : string ,
253289 metadata : OwnershipMetadata | CheckpointMetadata ,
254- etag : string | undefined
290+ etag : string | undefined ,
291+ options : OperationOptions = { }
255292 ) : Promise < BlobSetMetadataResponse > {
293+ const { abortSignal, tracingOptions } = options ;
256294 const blockBlobClient = this . _containerClient . getBlobClient ( blobName ) . getBlockBlobClient ( ) ;
257295
258296 // When we have an etag, we know the blob existed.
259297 // If we encounter an error we should fail.
260298 if ( etag ) {
261299 return blockBlobClient . setMetadata ( metadata as Metadata , {
300+ abortSignal,
262301 conditions : {
263302 ifMatch : etag
264- }
303+ } ,
304+ tracingOptions
265305 } ) ;
266306 } else {
267307 try {
268308 // Attempt to set metadata, and fallback to upload if the blob doesn't already exist.
269309 // This avoids poor performance in storage accounts with soft-delete or blob versioning enabled.
270310 // https://github.com/Azure/azure-sdk-for-js/issues/10132
271- return await blockBlobClient . setMetadata ( metadata as Metadata ) ;
311+ return await blockBlobClient . setMetadata ( metadata as Metadata , {
312+ abortSignal,
313+ tracingOptions
314+ } ) ;
272315 } catch ( err ) {
273316 // Check if the error is `BlobNotFound` and fallback to `upload` if it is.
274317 if ( err ?. name !== "RestError" ) {
@@ -281,7 +324,9 @@ export class BlobCheckpointStore implements CheckpointStore {
281324 }
282325
283326 return blockBlobClient . upload ( "" , 0 , {
284- metadata : metadata as Metadata
327+ abortSignal,
328+ metadata : metadata as Metadata ,
329+ tracingOptions
285330 } ) ;
286331 }
287332 }
0 commit comments