Skip to content

Commit 3436043

Browse files
author
Michael Li
committed
Changing how nextBatchEntry works to be more intuitive. This removes need for a separate BatchUtils
1 parent 81248cf commit 3436043

File tree

3 files changed

+19
-139
lines changed

3 files changed

+19
-139
lines changed

core/sdk-core/src/main/java/software/amazon/awssdk/core/internal/batchmanager/BatchBuffer.java

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,28 @@
2525
import java.util.function.BiConsumer;
2626
import java.util.stream.Collectors;
2727
import software.amazon.awssdk.annotations.SdkInternalApi;
28+
import software.amazon.awssdk.utils.Logger;
2829

2930
@SdkInternalApi
3031
public final class BatchBuffer<RequestT, ResponseT> {
3132
private final Object flushLock = new Object();
3233

3334
private final Map<String, BatchingExecutionContext<RequestT, ResponseT>> idToBatchContext;
3435

36+
// TODO: Figure out better name for nextId and nextBatchEntry.
3537
/**
3638
* Batch entries in a batch request require a unique ID so nextId keeps track of the ID to assign to the next
3739
* BatchingExecutionContext. For simplicity, the ID is just an integer that is incremented everytime a new request and
3840
* response pair is received.
3941
*/
40-
private final AtomicInteger nextId;
42+
private int nextId;
4143

4244
/**
4345
* Keeps track of the ID of the next entry to be added in a batch request. This ID does not necessarily correlate to a
4446
* request that already exists in the idToBatchContext map since it refers to the next entry (ex. if the last entry added
4547
* to idToBatchContext had an id of 22, nextBatchEntry will have a value of 23).
4648
*/
47-
private final AtomicInteger nextBatchEntry;
49+
private int nextBatchEntry;
4850

4951
/**
5052
* The scheduled flush tasks associated with this batchBuffer.
@@ -53,8 +55,8 @@ public final class BatchBuffer<RequestT, ResponseT> {
5355

5456
public BatchBuffer(ScheduledFuture<?> scheduledFlush) {
5557
this.idToBatchContext = new ConcurrentHashMap<>();
56-
this.nextId = new AtomicInteger(0);
57-
this.nextBatchEntry = new AtomicInteger(0);
58+
this.nextId = 0;
59+
this.nextBatchEntry = 0;
5860
this.scheduledFlush = scheduledFlush;
5961
}
6062

@@ -79,7 +81,8 @@ public Map<String, BatchingExecutionContext<RequestT, ResponseT>> flushableSched
7981
private Map<String, BatchingExecutionContext<RequestT, ResponseT>> extractFlushedEntries(int maxBatchItems) {
8082
LinkedHashMap<String, BatchingExecutionContext<RequestT, ResponseT>> requestEntries = new LinkedHashMap<>();
8183
String nextEntry;
82-
while (requestEntries.size() < maxBatchItems && (nextEntry = nextBatchEntry()) != null) {
84+
while (requestEntries.size() < maxBatchItems && hasNextBatchEntry()) {
85+
nextEntry = nextBatchEntry();
8386
requestEntries.put(nextEntry, idToBatchContext.get(nextEntry));
8487
idToBatchContext.remove(nextEntry);
8588
}
@@ -94,30 +97,25 @@ public CompletableFuture<ResponseT> getResponse(String key) {
9497
return idToBatchContext.get(key).response();
9598
}
9699

97-
// TODO: Needs to be in a lock to maintain insertion order. Not sure if there is any other way to accomplish this. I tried to
98-
// do this in a do while loop but it still ended up resulting in an incorrect insertion order.
99100
public BatchingExecutionContext<RequestT, ResponseT> put(RequestT request, CompletableFuture<ResponseT> response) {
100101
synchronized (this) {
101-
String id = BatchUtils.getAndIncrementId(nextId);
102+
if (nextId == Integer.MAX_VALUE) {
103+
nextId = 0;
104+
}
105+
String id = Integer.toString(nextId++);
102106
return idToBatchContext.put(id, new BatchingExecutionContext<>(request, response));
103107
}
104108
}
105109

106-
private String nextBatchEntry() {
107-
int currentNextBatchEntry;
108-
int newNextBatchEntry;
109-
do {
110-
currentNextBatchEntry = nextBatchEntry.get();
111-
newNextBatchEntry = currentNextBatchEntry + 1;
112-
if (!idToBatchContext.containsKey(Integer.toString(currentNextBatchEntry))) {
113-
newNextBatchEntry = currentNextBatchEntry;
114-
}
115-
} while (!nextBatchEntry.compareAndSet(currentNextBatchEntry, newNextBatchEntry));
110+
private boolean hasNextBatchEntry() {
111+
return idToBatchContext.containsKey(Integer.toString(nextBatchEntry));
112+
}
116113

117-
if (currentNextBatchEntry != newNextBatchEntry) {
118-
return Integer.toString(currentNextBatchEntry);
114+
private String nextBatchEntry() {
115+
if (nextBatchEntry == Integer.MAX_VALUE) {
116+
nextBatchEntry = 0;
119117
}
120-
return null;
118+
return Integer.toString(nextBatchEntry++);
121119
}
122120

123121
public void putScheduledFlush(ScheduledFuture<?> scheduledFlush) {
@@ -138,9 +136,4 @@ public Collection<CompletableFuture<ResponseT>> responses() {
138136
public void clear() {
139137
idToBatchContext.clear();
140138
}
141-
142-
// TODO: Only for debugging
143-
public void forEach(BiConsumer<String, BatchingExecutionContext<RequestT, ResponseT>> action) {
144-
idToBatchContext.forEach(action);
145-
}
146139
}

core/sdk-core/src/main/java/software/amazon/awssdk/core/internal/batchmanager/BatchUtils.java

Lines changed: 0 additions & 36 deletions
This file was deleted.

core/sdk-core/src/test/java/software/amazon/awssdk/core/internal/batchmanager/BatchUtilsTest.java

Lines changed: 0 additions & 77 deletions
This file was deleted.

0 commit comments

Comments
 (0)