|
1 |
| -# Azure Event Hubs checkpoint blob client library for Javascript |
| 1 | +# Azure Event Hubs Checkpoint Store library for Javascript using Storage Blobs |
2 | 2 |
|
3 |
| -This template serves as a starting point for JavaScript libraries targeting both Node and the Browser and implemented in TypeScript. |
| 3 | +An Azure Blob storage based solution to store checkpoints and to aid in load balancing when using `EventProcessor` from the [@azure/event-hubs](https://www.npmjs.com/package/@azure/event-hubs) library |
4 | 4 |
|
5 |
| -[Source code](https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/eventhub/event-hubs/eventhubs-checkpointstore-blob) | [Package (npm)](https://www.npmjs.com/package/@azure/eventhubs-checkpointstore-blob) | [API Reference Documentation](https://azure.github.io/azure-sdk-for-js/eventhubs-checkpointstore-blob/index.html) | [Product documentation](https://docs.microsoft.com/en-us/azure/event-hubs/event-hubs-event-processor-host) | [Samples](https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/eventhub/event-hubs/eventhubs-checkpointstore-blob/samples) |
| 5 | +[Source code](https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/eventhub/eventhubs-checkpointstore-blob) | [Package (npm)](https://www.npmjs.com/package/@azure/eventhubs-checkpointstore-blob) | [API Reference Documentation](https://azure.github.io/azure-sdk-for-js/eventhubs-checkpointstore-blob/index.html) | [Samples](https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/eventhub/eventhubs-checkpointstore-blob/samples) |
6 | 6 |
|
7 | 7 | ## Getting started
|
8 | 8 |
|
9 |
| -## Key concepts |
| 9 | +### Install the package |
| 10 | + |
| 11 | +Install the Azure Event Hubs Checkpoint Store Blob library using npm |
| 12 | + |
| 13 | +`npm install @azure/eventhubs-checkpointstore-blob` |
| 14 | + |
| 15 | +**Prerequisites**: You must have an [Azure subscription](https://azure.microsoft.com/free/), an |
| 16 | +[Event Hubs Namespace](https://docs.microsoft.com/en-us/azure/event-hubs/) to use this package, and a [Storage account](https://docs.microsoft.com/en-us/azure/storage/blobs/storage-blobs-introduction) |
| 17 | +If you are using this package in a Node.js application, then use Node.js 8.x or higher. |
| 18 | + |
| 19 | +### Configure Typescript |
| 20 | + |
| 21 | +TypeScript users need to have Node type definitions installed: |
| 22 | + |
| 23 | +```bash |
| 24 | +npm install @types/node |
| 25 | +``` |
| 26 | + |
| 27 | +You also need to enable `compilerOptions.allowSyntheticDefaultImports` in your tsconfig.json. Note that if you have enabled `compilerOptions.esModuleInterop`, `allowSyntheticDefaultImports` is enabled by default. See [TypeScript's compiler options handbook](https://www.typescriptlang.org/docs/handbook/compiler-options.html) for more information. |
| 28 | + |
| 29 | +### Key concepts |
| 30 | + |
| 31 | +- **Scale:** Create multiple consumers, with each consumer taking ownership of reading from a few Event Hubs partitions. |
| 32 | + |
| 33 | +- **Load balance:** Event Processor based application consists of one or more instances of `EventProcessor` which have been |
| 34 | + configured to consume events from the same Event Hub and consumer group. They balance the |
| 35 | + workload across different instances by distributing the partitions to be processed among themselves. |
| 36 | + |
| 37 | +- **Checkpointing:** It is a process by which readers mark or commit their position within a partition event sequence. Checkpointing is the responsibility of the consumer and |
| 38 | + occurs on a per-partition basis within a consumer group. This responsibility means that for each consumer group, each partition reader must keep track of its current position |
| 39 | + in the event stream, and can inform the service when it considers the data stream complete. |
| 40 | + |
| 41 | + If a reader disconnects from a partition, when it reconnects it begins reading at the checkpoint that was previously submitted by the last reader of that partition in that consumer group. |
| 42 | + When the reader connects, it passes the offset to the event hub to specify the location at which to start reading. In this way, you can use checkpointing to both mark events as "complete" by downstream applications, |
| 43 | + and to provide resiliency if a failover between readers running on different machines occurs. It is possible to return to older data by specifying a lower offset from this checkpointing process. |
| 44 | + Through this mechanism, checkpointing enables both failover resiliency and event stream replay. |
| 45 | + |
| 46 | + A [BlobPartitionManager](https://azure.github.io/azure-sdk-for-js/eventhubs-checkpointstore-blob/classes/blobpartitionmanager.html) is a class that implements key methods required by the Event Processor to balance load and update checkpoints. |
10 | 47 |
|
11 | 48 | ## Examples
|
12 | 49 |
|
| 50 | +- [Create a Partition Manager using Azure Blob Storage](#create-a-partition-manager-using-azure-blob-storage) |
| 51 | +- [Checkpoint events using Azure Blob storage](#checkpoint-events-using-azure-blob-storage) |
| 52 | + |
| 53 | +### Create a Partition Manager using Azure Blob Storage |
| 54 | + |
| 55 | +Use the below code snippet to create a Partition Manager. You will need to provide the connection string to your storage account. |
| 56 | + |
| 57 | +```javascript |
| 58 | +import { ContainerClient } from "@azure/storage-blob", |
| 59 | +import { BlobPartitionManager } from "@azure/eventhubs-checkpointstore-blob" |
| 60 | + |
| 61 | +const containerClient = new ContainerClient("storage-connection-string", "container-name"); |
| 62 | +await containerClient.create(); // This can be skipped if the container already exists |
| 63 | + |
| 64 | +const partitionManager = new BlobPartitionManager(containerClient); |
| 65 | +``` |
| 66 | + |
| 67 | +### Checkpoint events using Azure Blob storage |
| 68 | + |
| 69 | +To checkpoint events received using an EventProcessor to Azure Blob Storage, you will need to pass an instance of the Partition Manager to the Event Processor along with the code to call the `updateCheckpoint()` method. |
| 70 | + |
| 71 | +In this example, we will use a `SamplePartitionProcessor` that extends the [PartitionProcessor](https://azure.github.io/azure-sdk-for-js/event-hubs/classes/partitionprocessor.html) class in order to checkpoint the last event in the batch. |
| 72 | + |
| 73 | +```javascript |
| 74 | +import { ContainerClient } from "@azure/storage-blob", |
| 75 | +import { BlobPartitionManager } from "@azure/eventhubs-checkpointstore-blob" |
| 76 | +import { EventHubClient, PartitionProcessor } from "@azure/event-hubs" |
| 77 | + |
| 78 | +const eventhubClient = new EventHubClient("event-hub-connectionstring") |
| 79 | +const containerClient = new ContainerClient("storage-connection-string", "container-name"); |
| 80 | +await containerClient.create(); // This can be skipped if the container already exists |
| 81 | +const partitionManager = new BlobPartitionManager(containerClient); |
| 82 | + |
| 83 | +SamplePartitionProcessor extends PartitionProcessor { |
| 84 | + async processEvents(events, partitionContext) { |
| 85 | + if (events.length) { |
| 86 | + /* custom logic for processing events, then checkpoint*/ |
| 87 | + await partitionContext.updateCheckpoint(events[events.length - 1]); |
| 88 | + } |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +const eventProcessor = new EventProcessor( |
| 93 | + EventHubClient.defaultConsumerGroupName, |
| 94 | + client, |
| 95 | + SamplePartitionProcessor, |
| 96 | + partitionManager); |
| 97 | +eventProcessor.start(); |
| 98 | +``` |
| 99 | + |
13 | 100 | ## Troubleshooting
|
14 | 101 |
|
15 |
| -## Enable logs |
| 102 | +### Enable logs |
| 103 | + |
| 104 | +You can set the following environment variable to get the debug logs when using this library. |
| 105 | + |
| 106 | +- Getting debug logs from the Eventhubs Checkpointstore Blob |
| 107 | + |
| 108 | +```bash |
| 109 | +export DEBUG=azure:eventhubs-checkpointstore-blob* |
| 110 | +``` |
16 | 111 |
|
17 |
| -## Next steps |
| 112 | +- If you are interested only in **errors**, then you can set the `DEBUG` environment variable as follows: |
| 113 | + |
| 114 | +```bash |
| 115 | +export DEBUG=azure:event-hubs:error,azure:eventhubs-checkpointstore-blob:error |
| 116 | +``` |
| 117 | + |
| 118 | +### Logging to a file |
| 119 | + |
| 120 | +- Set the `DEBUG` environment variable as shown above and then run your test script as follows: |
| 121 | + |
| 122 | + - Logging statements from your test script go to `out.log` and logging statements from the sdk go to `debug.log`. |
| 123 | + ```bash |
| 124 | + node your-test-script.js > out.log 2>debug.log |
| 125 | + ``` |
| 126 | + - Logging statements from your test script and the sdk go to the same file `out.log` by redirecting stderr to stdout (&1), and then redirect stdout to a file: |
| 127 | + ```bash |
| 128 | + node your-test-script.js >out.log 2>&1 |
| 129 | + ``` |
| 130 | + - Logging statements from your test script and the sdk go to the same file `out.log`. |
| 131 | + |
| 132 | + ```bash |
| 133 | + node your-test-script.js &> out.log |
| 134 | + ``` |
| 135 | + |
| 136 | +## Next Steps |
| 137 | + |
| 138 | +Please take a look at the [samples](https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/eventhub/eventhubs-checkpointstore-blob/samples) |
| 139 | +directory for detailed example. |
18 | 140 |
|
19 | 141 | ## Contributing
|
| 142 | + |
| 143 | +If you'd like to contribute to this library, please read the [contributing guide](../../../CONTRIBUTING.md) to learn more about how to build and test the code. |
| 144 | +
|
| 145 | + |
0 commit comments