azure-sdk-for-js/sdk/eventhub/eventhubs-checkpointstore-blob
Daniel Rodríguez b8f46a9175
Ensuring that the build script also cleans (#17123)
* [Identity] Ensuring that the build script also cleans

* ...everywhere!

* removing double cleans
2021-08-26 14:25:14 -04:00
..
.vscode Verifying VS Code debugger with default configuration works on tests in our SDKs (#10145) 2020-08-11 14:24:30 -04:00
review [eventhubs-checkpointstore-blob] Updates CheckpointStore methods to support cancellation (#13862) 2021-02-19 11:43:03 -08:00
samples/v1 Rename master to main 2021-06-24 09:14:06 -07:00
samples-dev Rename master to main 2021-06-24 09:14:06 -07:00
src [eventhubs-checkpointstore-blob] fixes eslint errors (#14677) 2021-04-02 17:22:46 +00:00
test Rename hardcoded master links to use main 2021-06-22 15:51:16 -07:00
.nycrc [Service Bus] [Event Hubs] Fix code coverage for node tests (#6744) 2020-01-08 12:04:31 -08:00
CHANGELOG.md Update engines, @types/node to Node.js v12 (#15996) 2021-06-28 15:54:47 -07:00
License Use inlineSources to bundle source file contents inside map files (#7615) 2020-03-11 10:34:37 -07:00
README.md Dispaly links as list (#16117) 2021-07-06 12:30:32 -04:00
api-extractor.json Set docModel to true in api-extractor.json config (#10509) 2020-08-10 10:42:04 -07:00
karma.conf.js Remove IE related doc and tests (#15760) 2021-06-16 14:20:49 -07:00
package.json Ensuring that the build script also cleans (#17123) 2021-08-26 14:25:14 -04:00
rollup.base.config.js update clean commands (#14693) 2021-04-07 18:31:38 +00:00
rollup.config.js [Event Hubs][eventhubs-checkpoint-blob] Rename storage package name to "eventhubs-checkpointstore-blob" (#5048) 2019-09-06 18:16:21 -07:00
rollup.test.config.js [Event Hubs][eventhubs-checkpoint-blob] Rename storage package name to "eventhubs-checkpointstore-blob" (#5048) 2019-09-06 18:16:21 -07:00
sample.env [Event Hubs Checkpoint Store] Move to samples v2 (#15137) 2021-06-17 19:21:46 +00:00
tests.yml Update live test yaml to use cloud and matrix configuration (#13886) 2021-03-01 19:25:57 -05:00
tsconfig.json [Event Hubs Checkpoint Store] Move to samples v2 (#15137) 2021-06-17 19:21:46 +00:00
tsdoc.json replace @ignore with @hidden (#12963) 2021-01-05 04:17:33 +00:00

README.md

Azure Event Hubs Checkpoint Store client library for JavaScript

An Azure Blob storage based solution to store checkpoints and to aid in load balancing when using EventHubConsumerClient from the @azure/event-hubs library

Key links:

Getting started

Currently supported environments

See our support policy for more details.

Prerequisites

Install the package

Install the Azure Event Hubs Checkpoint Store Blob library using npm

npm install @azure/eventhubs-checkpointstore-blob

Configure Typescript

TypeScript users need to have Node type definitions installed:

npm install @types/node

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 for more information.

Key concepts

  • Scale: Create multiple consumers, with each consumer taking ownership of reading from a few Event Hubs partitions.

  • Load balance: Applications that support load balancing consist of one or more instances of EventHubConsumerClient which have been configured to consume events from the same Event Hub and consumer group and the same CheckpointStore. They balance the workload across different instances by distributing the partitions to be processed among themselves.

  • 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 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 in the event stream, and can inform the service when it considers the data stream complete.

    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. 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, 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. Through this mechanism, checkpointing enables both failover resiliency and event stream replay.

    A BlobCheckpointStore is a class that implements key methods required by the EventHubConsumerClient to balance load and update checkpoints.

Examples

Create a CheckpointStore using Azure Blob Storage

Use the below code snippet to create a CheckpointStore. You will need to provide the connection string to your storage account.

import { ContainerClient } from "@azure/storage-blob",
import { BlobCheckpointStore } from "@azure/eventhubs-checkpointstore-blob"

const containerClient = new ContainerClient("storage-connection-string", "container-name");

if (!containerClient.exists()) {
  await containerClient.create(); // This can be skipped if the container already exists
}

const checkpointStore =  new BlobCheckpointStore(containerClient);

Checkpoint events using Azure Blob storage

To checkpoint events received using Azure Blob Storage, you will need to pass an object that is compatible with the SubscriptionEventHandlers interface along with code to call the updateCheckpoint() method.

In this example, SubscriptionHandlers implements SubscriptionEventHandlers and also handles checkpointing.

const { EventHubConsumerClient } = require("@azure/event-hubs");
const { ContainerClient } = require("@azure/storage-blob");
const { BlobCheckpointStore } = require("@azure/eventhubs-checkpointstore-blob");

const storageAccountConnectionString = "storage-account-connection-string";
const containerName = "container-name";
const eventHubConnectionString = "eventhub-connection-string";
const consumerGroup = "my-consumer-group";
const eventHubName = "eventHubName";

async function main() {
  const blobContainerClient = new ContainerClient(storageAccountConnectionString, containerName);

  if (!(await blobContainerClient.exists())) {
    await blobContainerClient.create();
  }

  const checkpointStore = new BlobCheckpointStore(blobContainerClient);
  const consumerClient = new EventHubConsumerClient(
    consumerGroup,
    eventHubConnectionString,
    eventHubName,
    checkpointStore
  );

  const subscription = consumerClient.subscribe({
    processEvents: async (events, context) => {
      // event processing code goes here
      if (events.length === 0) {
        // If the wait time expires (configured via options in maxWaitTimeInSeconds) Event Hubs
        // will pass you an empty array.
        return;
      }

      // Checkpointing will allow your service to pick up from
      // where it left off when restarting.
      //
      // You'll want to balance how often you checkpoint with the
      // performance of your underlying checkpoint store.
      await context.updateCheckpoint(events[events.length - 1]);
    },
    processError: async (err, context) => {
      // handle any errors that occur during the course of
      // this subscription
      console.log(`Errors in subscription to partition ${context.partitionId}: ${err}`);
    }
  });

  // Wait for a few seconds to receive events before closing
  await new Promise((resolve) => setTimeout(resolve, 10 * 1000));

  await subscription.close();
  await consumerClient.close();
  console.log(`Exiting sample`);
}

main();

Troubleshooting

Logging

You can set the AZURE_LOG_LEVEL environment variable to one of the following values to enable logging to stderr:

  • verbose
  • info
  • warning
  • error

You can also set the log level programatically by importing the @azure/logger package and calling the setLogLevel function with one of the log level values.

When setting a log level either programatically or via the AZURE_LOG_LEVEL environment variable, any logs that are written using a log level equal to or less than the one you choose will be emitted. For example, when you set the log level to info, the logs that are written for levels warning and error are also emitted. This SDK follows the Azure SDK for TypeScript guidelines when determining which level to log to.

You can alternatively set the DEBUG environment variable to get logs when using this library. This can be useful if you also want to emit logs from the dependencies rhea-promise and rhea as well.

Note: AZURE_LOG_LEVEL, if set, takes precedence over DEBUG. Do not specify any azure libraries via DEBUG when also specifying AZURE_LOG_LEVEL or calling setLogLevel.

You can set the following environment variable to get the debug logs when using this library.

  • Getting only info level debug logs from the Eventhubs Checkpointstore Blob.
export DEBUG=azure:eventhubs-checkpointstore-blob:info

Logging to a file

  • Enable logging as shown above and then run your test script as follows:

    • Logging statements from your test script go to out.log and logging statements from the sdk go to debug.log.

      node your-test-script.js > out.log 2>debug.log
      
    • 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:

      node your-test-script.js >out.log 2>&1
      
    • Logging statements from your test script and the sdk go to the same file out.log.

      node your-test-script.js &> out.log
      

Next steps

Please take a look at the samples directory for detailed example.

Contributing

If you'd like to contribute to this library, please read the contributing guide to learn more about how to build and test the code.

Impressions