Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit eba47da

Browse files
ShivangiRejaramya-rao-a
authored andcommitted
[eventhubs-checkpointstore-blob] Add README and changelog (Azure#5077)
1 parent c2b39e4 commit eba47da

File tree

4 files changed

+234
-7
lines changed

4 files changed

+234
-7
lines changed
Lines changed: 132 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,145 @@
1-
# Azure Event Hubs checkpoint blob client library for Javascript
1+
# Azure Event Hubs Checkpoint Store library for Javascript using Storage Blobs
22

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
44

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)
66

77
## Getting started
88

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.
1047

1148
## Examples
1249

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+
13100
## Troubleshooting
14101

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+
```
16111

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.
18140

19141
## 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+
![Impressions](https://azure-sdk-impressions.azurewebsites.net/api/impressions/azure-sdk-for-js/sdk/eventhub/eventhubs-checkpointstore-blob/README.png)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
### 2019-09-10 1.0.0-preview.1
2+
3+
This is the first preview of the `@azure/eventhubs-checkpointstore-blob` library which provides the implementation for the `PartitionManager` interface from the `@azure/event-hubs` library which is required to store checkpoints & aid in load balancing of multiple instances of an EventProcessor.
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
Copyright (c) Microsoft Corporation. All rights reserved.
3+
Licensed under the MIT Licence.
4+
5+
This sample demonstrates how to use the EventProcessor to process events from all partitions
6+
of a consumer group in an Event Hubs instance. It also demonstrates the process of checkpointing an event
7+
which helps new instances of Event Processors that may have spun up for scaling or for crash recovery.
8+
9+
If your Event Hub instance doesn't have any events, then please run https://github.com/Azure/azure-sdk-for-js/blob/master/sdk/eventhub/event-hubs/samples/sendEvents.ts sample
10+
to populate it before running this sample.
11+
*/
12+
13+
import {
14+
EventHubClient,
15+
ReceivedEventData,
16+
delay,
17+
EventProcessor,
18+
PartitionContext,
19+
PartitionProcessor,
20+
CloseReason
21+
} from "@azure/event-hubs";
22+
23+
import { ContainerClient } from "@azure/storage-blob";
24+
import { BlobPartitionManager } from "@azure/eventhubs-checkpointstore-blob";
25+
26+
// A Sample Partition Processor that keeps track of the number of events processed.
27+
class SamplePartitionProcessor extends PartitionProcessor {
28+
private _messageCount = 0;
29+
30+
async processEvents(events: ReceivedEventData[], partitionContext: PartitionContext) {
31+
// events can be empty if no events were recevied in the last 60 seconds.
32+
// This interval can be configured when creating the EventProcessor
33+
if (events.length === 0) {
34+
return;
35+
}
36+
37+
for (const event of events) {
38+
console.log(
39+
`Received event: '${event.body}' from partition: '${partitionContext.partitionId}' and consumer group: '${partitionContext.consumerGroupName}'`
40+
);
41+
this._messageCount++;
42+
}
43+
44+
// checkpoint using the last event in the batch
45+
const lastEvent = events[events.length - 1];
46+
await partitionContext.updateCheckpoint(lastEvent).catch((err) => {
47+
console.log(`Error when checkpointing on partition ${partitionContext.partitionId}: `, err);
48+
});
49+
console.log(
50+
`Successfully checkpointed event with sequence number: ${lastEvent.sequenceNumber} from partition: 'partitionContext.partitionId'`
51+
);
52+
}
53+
54+
async processError(error: Error, partitionContext: PartitionContext) {
55+
console.log(`Encountered an error: ${error.message} when processing partition ${partitionContext.partitionId}`);
56+
}
57+
58+
async initialize(partitionContext: PartitionContext) {
59+
console.log(`Started processing partition: ${partitionContext.partitionId}`);
60+
}
61+
62+
async close(reason: CloseReason, partitionContext: PartitionContext) {
63+
console.log(`Stopped processing for reason ${reason}`);
64+
console.log(`Processed ${this._messageCount} from partition ${partitionContext.partitionId}.`);
65+
}
66+
}
67+
68+
const connectionString = "";
69+
const eventHubName = "";
70+
const storageConnectionString = "";
71+
const containerName = "";
72+
73+
async function main() {
74+
const client = new EventHubClient(connectionString, eventHubName);
75+
const containerClient = new ContainerClient(storageConnectionString, containerName);
76+
await containerClient.create();
77+
78+
const processor = new EventProcessor(
79+
EventHubClient.defaultConsumerGroupName,
80+
client,
81+
SamplePartitionProcessor,
82+
new BlobPartitionManager(containerClient),
83+
{
84+
maxBatchSize: 10,
85+
maxWaitTimeInSeconds: 20
86+
}
87+
);
88+
await processor.start();
89+
// after 30 seconds, stop processing
90+
await delay(30000);
91+
92+
await processor.stop();
93+
await client.close();
94+
}
95+
96+
main().catch((err) => {
97+
console.log("Error occurred: ", err);
98+
});

sdk/eventhub/eventhubs-checkpointstore-blob/samples/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
],
99
"exclude": [
1010
"../node_modules",
11-
"../typings/**",
11+
"../typings/**"
1212
]
1313
}

0 commit comments

Comments
 (0)