Skip to content

Commit 3b13df6

Browse files
authored
Merge pull request #180083 from ealsur/users/ealsur/cfperrors
[Cosmos DB] - Adds Change Feed Processor notification APIs
2 parents 02681c2 + 576d6b2 commit 3b13df6

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

articles/cosmos-db/TOC.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@
624624
href: sql/change-feed-design-patterns.md
625625
- name: Reading change feed
626626
href: sql/read-change-feed.md
627-
- name: sql/Change feed processor
627+
- name: Change feed processor
628628
href: sql/change-feed-processor.md
629629
- name: Trigger Azure Functions
630630
displayName: change feed

articles/cosmos-db/sql/change-feed-processor.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ ms.service: cosmos-db
77
ms.subservice: cosmosdb-sql
88
ms.devlang: dotnet
99
ms.topic: conceptual
10-
ms.date: 07/20/2021
10+
ms.date: 11/16/2021
1111
ms.reviewer: sngun
1212
ms.custom: devx-track-csharp
1313
---
@@ -65,14 +65,24 @@ The normal life cycle of a host instance is:
6565

6666
## Error handling
6767

68-
The change feed processor is resilient to user code errors. That means that if your delegate implementation has an unhandled exception (step #4), the thread processing that particular batch of changes will be stopped, and a new thread will be created. The new thread will check which was the latest point in time the lease store has for that range of partition key values, and restart from there, effectively sending the same batch of changes to the delegate. This behavior will continue until your delegate processes the changes correctly and it's the reason the change feed processor has an "at least once" guarantee, because if the delegate code throws an exception, it will retry that batch.
68+
The change feed processor is resilient to user code errors. That means that if your delegate implementation has an unhandled exception (step #4), the thread processing that particular batch of changes will be stopped, and a new thread will be created. The new thread will check which was the latest point in time the lease store has for that range of partition key values, and restart from there, effectively sending the same batch of changes to the delegate. This behavior will continue until your delegate processes the changes correctly and it's the reason the change feed processor has an "at least once" guarantee.
6969

7070
> [!NOTE]
7171
> There is only one scenario where a batch of changes will not be retried. If the failure happens on the first ever delegate execution, the lease store has no previous saved state to be used on the retry. On those cases, the retry would use the [initial starting configuration](#starting-time), which might or might not include the last batch.
7272
7373
To prevent your change feed processor from getting "stuck" continuously retrying the same batch of changes, you should add logic in your delegate code to write documents, upon exception, to a dead-letter queue. This design ensures that you can keep track of unprocessed changes while still being able to continue to process future changes. The dead-letter queue might be another Cosmos container. The exact data store does not matter, simply that the unprocessed changes are persisted.
7474

75-
In addition, you can use the [change feed estimator](how-to-use-change-feed-estimator.md) to monitor the progress of your change feed processor instances as they read the change feed. You can use this estimation to understand if your change feed processor is "stuck" or lagging behind due to available resources like CPU, memory, and network bandwidth.
75+
In addition, you can use the [change feed estimator](how-to-use-change-feed-estimator.md) to monitor the progress of your change feed processor instances as they read the change feed or use the [life cycle notifications](#life-cycle-notifications) to detect underlying failures.
76+
77+
## Life-cycle notifications
78+
79+
The change feed processor lets you hook to relevant events in its [life cycle](#processing-life-cycle), you can choose to be notified to one or all of them. The recommendation is to at least register the error notification:
80+
81+
* Register a handler for `WithLeaseAcquireNotification` to be notified when the current host acquires a lease to start processing it.
82+
* Register a handler for `WithLeaseReleaseNotification` to be notified when the current host releases a lease and stops processing it.
83+
* Register a handler for `WithErrorNotification` to be notified when the current host encounters an exception during processing, being able to distinguish if the source is the user delegate (unhandled exception) or an error the processor is encountering trying to access the monitored container (for example, networking issues).
84+
85+
[!code-csharp[Main](~/samples-cosmosdb-dotnet-v3/Microsoft.Azure.Cosmos.Samples/Usage/ChangeFeed/Program.cs?name=StartWithNotifications)]
7686

7787
## Deployment unit
7888

@@ -96,7 +106,7 @@ Moreover, the change feed processor can dynamically adjust to containers scale d
96106

97107
## Change feed and provisioned throughput
98108

99-
Change feed read operations on the monitored container will consume RUs.
109+
Change feed read operations on the monitored container will consume RUs.
100110

101111
Operations on the lease container consume RUs. The higher the number of instances using the same lease container, the higher the potential RU consumption will be. Remember to monitor your RU consumption on the leases container if you decide to scale and increment the number of instances.
102112

0 commit comments

Comments
 (0)