This repository was archived by the owner on Jul 13, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathSampleChangeFeedEstimator.java
More file actions
228 lines (195 loc) · 11.4 KB
/
Copy pathSampleChangeFeedEstimator.java
File metadata and controls
228 lines (195 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.cosmos.examples.changefeed;
import com.azure.cosmos.ChangeFeedProcessor;
import com.azure.cosmos.ChangeFeedProcessorBuilder;
import com.azure.cosmos.CosmosAsyncClient;
import com.azure.cosmos.CosmosAsyncContainer;
import com.azure.cosmos.CosmosAsyncDatabase;
import com.azure.cosmos.examples.common.CustomPOJO2;
import com.azure.cosmos.implementation.Utils;
import com.azure.cosmos.models.ChangeFeedProcessorState;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;
import java.time.Duration;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import static com.azure.cosmos.examples.changefeed.SampleChangeFeedProcessor.createNewCollection;
import static com.azure.cosmos.examples.changefeed.SampleChangeFeedProcessor.createNewDatabase;
import static com.azure.cosmos.examples.changefeed.SampleChangeFeedProcessor.createNewLeaseCollection;
import static com.azure.cosmos.examples.changefeed.SampleChangeFeedProcessor.deleteDatabase;
import static com.azure.cosmos.examples.changefeed.SampleChangeFeedProcessor.getCosmosClient;
/**
* Sample for Change Feed Estimator.
* This sample models an application where documents are being inserted into one container (the "feed container"),
* and meanwhile another worker thread or worker application is pulling inserted documents from the feed container's Change Feed
* and operating on them in some way. For one or more workers to process the Change Feed of a container, the workers must first contact the server
* and "lease" access to monitor one or more partitions of the feed container. The Change Feed Processor Library
* handles leasing automatically for you, however you must create a separate "lease container" where the Change Feed
* Processor Library can store and track leases container partitions.
*/
public class SampleChangeFeedEstimator {
public static final String DATABASE_NAME = "db_" + UUID.randomUUID();
public static final String COLLECTION_NAME = "coll_" + UUID.randomUUID();
private static final ObjectMapper OBJECT_MAPPER = Utils.getSimpleObjectMapper();
protected static Logger logger = LoggerFactory.getLogger(SampleChangeFeedEstimator.class);
public static void main(String[] args) {
logger.info("Begin Sample");
try {
//Summary of the next four commands:
//-Create an asynchronous Azure Cosmos DB client and database so that we can issue async requests to the DB
//-Create a "feed container" and a "lease container" in the DB
logger.info("Create CosmosClient");
CosmosAsyncClient client = getCosmosClient();
logger.info("Create sample's database: " + DATABASE_NAME);
CosmosAsyncDatabase cosmosDatabase = createNewDatabase(client, DATABASE_NAME);
logger.info("Create container for documents: " + COLLECTION_NAME);
CosmosAsyncContainer feedContainer = createNewCollection(client, DATABASE_NAME, COLLECTION_NAME);
logger.info("Create container for lease: " + COLLECTION_NAME + "-leases");
CosmosAsyncContainer leaseContainer = createNewLeaseCollection(client, DATABASE_NAME, COLLECTION_NAME + "-leases");
//Model of a worker thread or application which leases access to monitor one or more feed container
//partitions via the Change Feed. In a real-world application you might deploy this code in an Azure function.
//The next line causes the worker to create and start an instance of the Change Feed Processor. See the implementation of getChangeFeedProcessor() for guidance
//on creating a handler for Change Feed events. In this stream, we also trigger the insertion of 10 documents on a separate
//thread.
logger.info("Start Change Feed Processor on worker (handles changes asynchronously)");
// <ChangeFeedProcessorBuilder>
ChangeFeedProcessor changeFeedProcessorMainInstance = new ChangeFeedProcessorBuilder()
.hostName("SampleHost_1")
.feedContainer(feedContainer)
.leaseContainer(leaseContainer)
.handleChanges(handleChangesWithLag())
.buildChangeFeedProcessor();
try {
changeFeedProcessorMainInstance
.start()
.subscribeOn(Schedulers.boundedElastic())
.timeout(Duration.ofSeconds(10))
.then(Mono.just(changeFeedProcessorMainInstance)
.delayElement(Duration.ofSeconds(10))
.flatMap(value -> changeFeedProcessorMainInstance
.stop()
.subscribeOn(Schedulers.boundedElastic())
.timeout(Duration.ofSeconds(10))))
.subscribe();
} catch (Exception ex) {
logger.error("Change feed processor did not start and stopped in the expected time", ex);
throw ex;
}
// </ChangeFeedProcessorBuilder>
// Sleeping here because the CFP instance above requires some time to initialize the lease container
// with all the documents that will keep track of the ongoing work.
Thread.sleep(Duration.ofSeconds(20).toMillis());
// CFP lag checking can be performed on a separate application (like a health monitor)
// as long as the same input containers (feedContainer and leaseContainer) and the exact same lease prefix are used.
// The estimator code requires that the CFP had an opportunity to fully initialize the leaseContainer's documents.
ChangeFeedProcessor changeFeedProcessorSideCart = new ChangeFeedProcessorBuilder()
.hostName("side-cart")
.feedContainer(feedContainer)
.leaseContainer(leaseContainer)
.handleChanges(nodes -> {
logger.error("This never needs to be called, as this is just monitoring the state");
})
.buildChangeFeedProcessor();
// <EstimatedLag>
AtomicInteger totalLag = new AtomicInteger();
Mono<List<ChangeFeedProcessorState>> currentState = changeFeedProcessorMainInstance.getCurrentState();
currentState.map(state -> {
for (ChangeFeedProcessorState changeFeedProcessorState : state) {
totalLag.addAndGet(changeFeedProcessorState.getEstimatedLag());
}
return state;
}).subscribe();
// Initially totalLag should be zero
logger.info("Initially total lag is : {}", totalLag.get());
totalLag.set(0);
currentState = changeFeedProcessorSideCart.getCurrentState();
currentState.map(state -> {
for (ChangeFeedProcessorState changeFeedProcessorState : state) {
totalLag.addAndGet(changeFeedProcessorState.getEstimatedLag());
}
return state;
}).subscribe();
// Initially totalLag should be zero
logger.info("Initially total lag is : {}", totalLag.get());
// </EstimatedLag>
//These two lines model an application which is inserting ten documents into the feed container
logger.info("Start application that inserts documents into feed container");
createNewDocumentsCustomPOJO(feedContainer, 10);
currentState = changeFeedProcessorMainInstance.getCurrentState();
currentState.map(state -> {
for (ChangeFeedProcessorState changeFeedProcessorState : state) {
totalLag.addAndGet(changeFeedProcessorState.getEstimatedLag());
}
return state;
}).block();
// Finally, totalLag should be greater or equal to the number of documents created
logger.info("Finally total lag is : {}", totalLag.get());
// <FinalLag>
totalLag.set(0);
changeFeedProcessorSideCart.start().block();
currentState = changeFeedProcessorSideCart.getCurrentState();
currentState.map(state -> {
for (ChangeFeedProcessorState changeFeedProcessorState : state) {
totalLag.addAndGet(changeFeedProcessorState.getEstimatedLag());
}
return state;
}).block();
// Finally, totalLag should be greater or equal to the number of documents created
logger.info("Finally total lag is : {}", totalLag.get());
// </FinalLag>
Thread.sleep(Duration.ofSeconds(30).toMillis());
logger.info("Delete sample's database: " + DATABASE_NAME);
deleteDatabase(cosmosDatabase);
Thread.sleep(500);
} catch (Exception e) {
e.printStackTrace();
}
logger.info("End Sample");
}
// <HandleChangesWithLag>
private static Consumer<List<JsonNode>> handleChangesWithLag() {
return (List<JsonNode> docs) -> {
logger.info("Start handleChangesWithLag()");
try {
Thread.sleep(Duration.ofSeconds(5).toMillis());
} catch (InterruptedException e) {
e.printStackTrace();
}
for (JsonNode document : docs) {
try {
//Change Feed hands the document to you in the form of a JsonNode
//As a developer you have two options for handling the JsonNode document provided to you by Change Feed
//One option is to operate on the document in the form of a JsonNode, as shown below. This is great
//especially if you do not have a single uniform data model for all documents.
logger.info("Document received: " + OBJECT_MAPPER.writerWithDefaultPrettyPrinter()
.writeValueAsString(document));
//You can also transform the JsonNode to a POJO having the same structure as the JsonNode,
//as shown below. Then you can operate on the POJO.
CustomPOJO2 pojo_doc = OBJECT_MAPPER.treeToValue(document, CustomPOJO2.class);
logger.info("id: " + pojo_doc.getId());
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
logger.info("End handleChangesWithLag()");
};
}
// </HandleChangesWithLag>
public static void createNewDocumentsCustomPOJO(CosmosAsyncContainer containerClient, int count) {
String suffix = UUID.randomUUID().toString();
for (int i = 0; i < count; i++) {
CustomPOJO2 document = new CustomPOJO2();
document.setId(String.format("0%d-%s", i, suffix));
document.setPk(document.getId()); // This is a very simple example, so we'll just have a partition key (/pk) field that we set equal to id
containerClient.createItem(document).block();
}
}
}