/
SampleChangeFeedProcessorForAllVersionsAndDeletesMode.java
348 lines (285 loc) · 16.4 KB
/
SampleChangeFeedProcessorForAllVersionsAndDeletesMode.java
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// 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.ConsistencyLevel;
import com.azure.cosmos.CosmosAsyncClient;
import com.azure.cosmos.CosmosAsyncContainer;
import com.azure.cosmos.CosmosAsyncDatabase;
import com.azure.cosmos.CosmosClientBuilder;
import com.azure.cosmos.CosmosException;
import com.azure.cosmos.examples.common.AccountSettings;
import com.azure.cosmos.examples.common.CustomPOJO2;
import com.azure.cosmos.implementation.Utils;
import com.azure.cosmos.implementation.apachecommons.lang.RandomStringUtils;
import com.azure.cosmos.models.ChangeFeedProcessorItem;
import com.azure.cosmos.models.ChangeFeedProcessorOptions;
import com.azure.cosmos.models.CosmosContainerProperties;
import com.azure.cosmos.models.CosmosContainerRequestOptions;
import com.azure.cosmos.models.CosmosContainerResponse;
import com.azure.cosmos.models.CosmosDatabaseResponse;
import com.azure.cosmos.models.PartitionKey;
import com.azure.cosmos.models.ThroughputProperties;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import reactor.core.scheduler.Schedulers;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
/**
* Sample for Change Feed Processor.
* 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 SampleChangeFeedProcessorForAllVersionsAndDeletesMode {
public static int WAIT_FOR_WORK = 60000;
public static final String DATABASE_NAME = "db_" + RandomStringUtils.randomAlphabetic(7);
public static final String COLLECTION_NAME = "coll_" + RandomStringUtils.randomAlphabetic(7);
private static final ObjectMapper OBJECT_MAPPER = Utils.getSimpleObjectMapper();
protected static Logger logger = LoggerFactory.getLogger(SampleChangeFeedProcessorForAllVersionsAndDeletesMode.class);
private static ChangeFeedProcessor changeFeedProcessorInstance;
private static boolean isWorkCompleted = false;
private static ChangeFeedProcessorOptions options;
private static List<CustomPOJO2> documentList = new ArrayList<>();
public static void main(String[] args) {
logger.info("BEGIN Sample");
try {
// <ChangeFeedProcessorOptions>
options = new ChangeFeedProcessorOptions();
options.setStartFromBeginning(false);
options.setLeasePrefix("myChangeFeedDeploymentUnit");
options.setFeedPollDelay(Duration.ofSeconds(5));
// </ChangeFeedProcessorOptions>
//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 DocumentClient");
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 getChangeFeedProcessorForAllVersionsAndDeletesMode() 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.
// <StartChangeFeedProcessor>
logger.info("-->START Change Feed Processor on worker (handles changes asynchronously)");
changeFeedProcessorInstance = getChangeFeedProcessorForAllVersionsAndDeletesMode("SampleHost_1", feedContainer, leaseContainer);
changeFeedProcessorInstance.start()
.subscribeOn(Schedulers.boundedElastic())
.doOnSuccess(aVoid -> {
//pass
})
.subscribe();
// </StartChangeFeedProcessor>
//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, Duration.ofSeconds(3));
upsertDocumentsCustomPOJO(feedContainer, 10, Duration.ofSeconds(3));
deleteDocumentsCustomPOJO(feedContainer, 10, Duration.ofSeconds(3));
isWorkCompleted = true;
//This loop models the Worker main loop, which spins while its Change Feed Processor instance asynchronously
//handles incoming Change Feed events from the feed container. Of course in this sample, polling
//isWorkCompleted is unnecessary because items are being added to the feed container on the same thread, and you
//can see just above isWorkCompleted is set to true.
//But conceptually the worker is part of a different thread or application than the one which is inserting
//into the feed container; so this code illustrates the worker waiting and listening for changes to the feed container
long remainingWork = WAIT_FOR_WORK;
while (!isWorkCompleted && remainingWork > 0) {
Thread.sleep(100);
remainingWork -= 100;
}
//When all documents have been processed, clean up
if (isWorkCompleted) {
if (changeFeedProcessorInstance != null) {
changeFeedProcessorInstance.stop().subscribe();
}
} else {
throw new RuntimeException("The change feed processor initialization and automatic create document feeding process did not complete in the expected time");
}
logger.info("-->DELETE sample's database: " + DATABASE_NAME);
//deleteDatabase(cosmosDatabase);
Thread.sleep(500);
} catch (Exception e) {
e.printStackTrace();
}
logger.info("END Sample");
}
// <Delegate>
public static ChangeFeedProcessor getChangeFeedProcessorForAllVersionsAndDeletesMode(String hostName, CosmosAsyncContainer feedContainer, CosmosAsyncContainer leaseContainer) {
return new ChangeFeedProcessorBuilder()
.hostName(hostName)
.options(options)
.feedContainer(feedContainer)
.leaseContainer(leaseContainer)
.handleAllVersionsAndDeletesChanges((List<ChangeFeedProcessorItem> changeFeedProcessorItems) -> {
logger.info("--->handleAllVersionsAndDeletesChanges() START");
for (ChangeFeedProcessorItem item : changeFeedProcessorItems) {
try {
// AllVersionsAndDeletes Change Feed hands the document to you in the form of ChangeFeedProcessorItem
// As a developer you have two options for handling the ChangeFeedProcessorItem provided to you by Change Feed
// One option is to operate on the item as it is and call the different getters for different states, as shown below.
logger.info("---->DOCUMENT RECEIVED: {}", OBJECT_MAPPER.writerWithDefaultPrettyPrinter()
.writeValueAsString(item));
logger.info("---->CURRENT RECEIVED: {}", item.getCurrent());
logger.info("---->PREVIOUS RECEIVED: {}", item.getPrevious());
logger.info("---->METADATA RECEIVED: {}", item.getChangeFeedMetaData());
// You can also transform the ChangeFeedProcessorItem to JsonNode and work on the generic json structure.
// This is great especially if you do not have a single uniform data model for all documents.
logger.info("----=>JsonNode received: " + item.toJsonNode());
} catch (JsonProcessingException e) {
e.printStackTrace();
}
}
logger.info("--->handleAllVersionsAndDeletesChanges() END");
})
.buildChangeFeedProcessor();
}
// </Delegate>
public static CosmosAsyncClient getCosmosClient() {
return new CosmosClientBuilder()
.endpoint(AccountSettings.HOST)
.key(AccountSettings.MASTER_KEY)
.contentResponseOnWriteEnabled(true)
.consistencyLevel(ConsistencyLevel.SESSION)
.buildAsyncClient();
}
public static CosmosAsyncDatabase createNewDatabase(CosmosAsyncClient client, String databaseName) {
CosmosDatabaseResponse databaseResponse = client.createDatabaseIfNotExists(databaseName).block();
return client.getDatabase(databaseResponse.getProperties().getId());
}
public static void deleteDatabase(CosmosAsyncDatabase cosmosDatabase) {
cosmosDatabase.delete().block();
}
public static CosmosAsyncContainer createNewCollection(CosmosAsyncClient client, String databaseName, String collectionName) {
CosmosAsyncDatabase databaseLink = client.getDatabase(databaseName);
CosmosAsyncContainer collectionLink = databaseLink.getContainer(collectionName);
CosmosContainerResponse containerResponse = null;
try {
containerResponse = collectionLink.read().block();
if (containerResponse != null) {
throw new IllegalArgumentException(String.format("Collection %s already exists in database %s.", collectionName, databaseName));
}
} catch (RuntimeException ex) {
if (ex instanceof CosmosException) {
CosmosException CosmosException = (CosmosException) ex;
if (CosmosException.getStatusCode() != 404) {
throw ex;
}
} else {
throw ex;
}
}
CosmosContainerProperties containerSettings = new CosmosContainerProperties(collectionName, "/pk");
CosmosContainerRequestOptions requestOptions = new CosmosContainerRequestOptions();
ThroughputProperties throughputProperties = ThroughputProperties.createManualThroughput(10000);
containerResponse = databaseLink.createContainer(containerSettings, throughputProperties, requestOptions).block();
if (containerResponse == null) {
throw new RuntimeException(String.format("Failed to create collection %s in database %s.", collectionName, databaseName));
}
return databaseLink.getContainer(containerResponse.getProperties().getId());
}
public static CosmosAsyncContainer createNewLeaseCollection(CosmosAsyncClient client, String databaseName, String leaseCollectionName) {
CosmosAsyncDatabase databaseLink = client.getDatabase(databaseName);
CosmosAsyncContainer leaseCollectionLink = databaseLink.getContainer(leaseCollectionName);
CosmosContainerResponse leaseContainerResponse = null;
try {
leaseContainerResponse = leaseCollectionLink.read().block();
if (leaseContainerResponse != null) {
leaseCollectionLink.delete().block();
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
} catch (RuntimeException ex) {
if (ex instanceof CosmosException) {
CosmosException CosmosException = (CosmosException) ex;
if (CosmosException.getStatusCode() != 404) {
throw ex;
}
} else {
throw ex;
}
}
CosmosContainerProperties containerSettings = new CosmosContainerProperties(leaseCollectionName, "/id");
CosmosContainerRequestOptions requestOptions = new CosmosContainerRequestOptions();
ThroughputProperties throughputProperties = ThroughputProperties.createManualThroughput(400);
leaseContainerResponse = databaseLink.createContainer(containerSettings, throughputProperties, requestOptions).block();
if (leaseContainerResponse == null) {
throw new RuntimeException(String.format("Failed to create collection %s in database %s.", leaseCollectionName, databaseName));
}
return databaseLink.getContainer(leaseContainerResponse.getProperties().getId());
}
public static void createNewDocumentsCustomPOJO(CosmosAsyncContainer containerClient, int count, Duration delay) {
String suffix = RandomStringUtils.randomAlphabetic(10);
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).subscribe(doc -> {
logger.info("---->DOCUMENT INSERT: " + doc);
documentList.add(document);
});
long remainingWork = delay.toMillis();
try {
while (remainingWork > 0) {
Thread.sleep(100);
remainingWork -= 100;
}
} catch (InterruptedException iex) {
// exception caught
break;
}
}
}
public static void upsertDocumentsCustomPOJO(CosmosAsyncContainer containerClient, int count, Duration delay) {
for (CustomPOJO2 document : documentList){
document.setId(document.getId());
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.upsertItem(document).subscribe(doc -> {
logger.info("---->DOCUMENT UPSERT: " + doc);
});
long remainingWork = delay.toMillis();
try {
while (remainingWork > 0) {
Thread.sleep(100);
remainingWork -= 100;
}
} catch (InterruptedException iex) {
// exception caught
break;
}
}
}
public static void deleteDocumentsCustomPOJO(CosmosAsyncContainer containerClient, int count, Duration delay) {
for (int i = 0; i <= count; i++) {
CustomPOJO2 document = documentList.get(i);
containerClient.deleteItem(document.getId(), new PartitionKey(document.getPk())).subscribe(doc -> {
logger.info("---->DOCUMENT DELETE: " + doc);
});
long remainingWork = delay.toMillis();
try {
while (remainingWork > 0) {
Thread.sleep(100);
remainingWork -= 100;
}
} catch (InterruptedException iex) {
// exception caught
break;
}
}
}
}