Skip to content
This repository was archived by the owner on Jul 13, 2026. It is now read-only.

Commit d63da2d

Browse files
committed
updates to address Abhijeet review comments
1 parent ede861e commit d63da2d

2 files changed

Lines changed: 27 additions & 19 deletions

File tree

src/main/java/com/azure/cosmos/examples/bulk/async/BulkWriter.java

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@
1717
import org.slf4j.Logger;
1818
import org.slf4j.LoggerFactory;
1919
import reactor.core.publisher.Flux;
20+
import reactor.core.publisher.Mono;
2021
import reactor.core.publisher.Sinks;
2122
import reactor.core.scheduler.Schedulers;
2223

24+
import java.time.Duration;
2325
import java.util.concurrent.Semaphore;
2426

2527
public class BulkWriter {
@@ -111,21 +113,17 @@ private void processResponseCode(
111113
"The operation for Item ID: [{}] Item PartitionKey Value: [{}] will be retried",
112114
itemOperation.getId(),
113115
itemOperation.getPartitionKeyValue());
114-
if (itemResponse.getRetryAfterDuration() != null) {
115-
logger.info(
116-
"Retrying after [{}] milliseconds",
117-
itemResponse.getRetryAfterDuration().toMillis());
118-
try {
119-
Thread.sleep(itemResponse.getRetryAfterDuration().toMillis());
120-
} catch (InterruptedException e) {
121-
throw new RuntimeException(e);
122-
}
123-
} else {
124-
logger.info("Retrying without delay");
125-
}
116+
long delay = itemResponse.getRetryAfterDuration() != null
117+
? itemResponse.getRetryAfterDuration().toMillis()
118+
: 0;
119+
120+
logger.info("Retrying after [{}] ms", delay);
121+
122+
Mono.delay(Duration.ofMillis(delay))
123+
// re-scheduling
124+
.doOnNext(t -> scheduleWrites(itemOperation))
125+
.subscribe();
126126

127-
//re-scheduling
128-
scheduleWrites(itemOperation);
129127
} else {
130128
logger.info(
131129
"The operation for Item ID: [{}] Item PartitionKey Value: [{}] did not complete successfully " +
@@ -156,7 +154,11 @@ private void handleException(CosmosItemOperation itemOperation, Exception except
156154
}
157155

158156
private boolean shouldRetry(int statusCode) {
159-
return statusCode == HttpConstants.StatusCodes.REQUEST_TIMEOUT ||
160-
statusCode == HttpConstants.StatusCodes.TOO_MANY_REQUESTS;
157+
return statusCode == 408 ||
158+
statusCode == 429 ||
159+
statusCode == 503 ||
160+
statusCode == 500 ||
161+
statusCode == 449 ||
162+
statusCode == 410;
161163
}
162164
}

src/main/java/com/azure/cosmos/examples/bulk/sync/BulkWriter.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
import java.util.ArrayList;
2121
import java.util.List;
22+
import java.util.Queue;
23+
import java.util.concurrent.ConcurrentLinkedQueue;
2224
import java.util.concurrent.Semaphore;
2325

2426
public class BulkWriter {
@@ -27,7 +29,7 @@ public class BulkWriter {
2729
private final CosmosContainer cosmosContainer;
2830
private final int cpuCount = Runtime.getRuntime().availableProcessors();
2931
private final Semaphore semaphore = new Semaphore(1024 * 167 / cpuCount);
30-
private final List<CosmosItemOperation> bufferedOperations = new ArrayList<>();
32+
private final Queue<CosmosItemOperation> bufferedOperations = new ConcurrentLinkedQueue<>();
3133
private final int maxRetries = 5;
3234

3335
public BulkWriter(CosmosContainer cosmosContainer) {
@@ -168,7 +170,11 @@ private void handleException(
168170
}
169171

170172
private boolean shouldRetry(int statusCode) {
171-
return statusCode == HttpConstants.StatusCodes.REQUEST_TIMEOUT ||
172-
statusCode == HttpConstants.StatusCodes.TOO_MANY_REQUESTS;
173+
return statusCode == 408 ||
174+
statusCode == 429 ||
175+
statusCode == 503 ||
176+
statusCode == 500 ||
177+
statusCode == 449 ||
178+
statusCode == 410;
173179
}
174180
}

0 commit comments

Comments
 (0)