|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.awssdk.core.internal.batchmanager; |
| 17 | + |
| 18 | +import java.util.Collection; |
| 19 | +import java.util.LinkedHashMap; |
| 20 | +import java.util.Map; |
| 21 | +import java.util.concurrent.CompletableFuture; |
| 22 | +import java.util.concurrent.ConcurrentHashMap; |
| 23 | +import java.util.concurrent.ScheduledFuture; |
| 24 | +import java.util.stream.Collectors; |
| 25 | +import software.amazon.awssdk.annotations.SdkInternalApi; |
| 26 | + |
| 27 | +@SdkInternalApi |
| 28 | +public final class BatchBuffer<RequestT, ResponseT> { |
| 29 | + private final Object flushLock = new Object(); |
| 30 | + |
| 31 | + private final Map<String, BatchingExecutionContext<RequestT, ResponseT>> idToBatchContext; |
| 32 | + |
| 33 | + // TODO: Figure out better name for nextId and nextBatchEntry. |
| 34 | + /** |
| 35 | + * Batch entries in a batch request require a unique ID so nextId keeps track of the ID to assign to the next |
| 36 | + * BatchingExecutionContext. For simplicity, the ID is just an integer that is incremented everytime a new request and |
| 37 | + * response pair is received. |
| 38 | + */ |
| 39 | + private int nextId; |
| 40 | + |
| 41 | + /** |
| 42 | + * Keeps track of the ID of the next entry to be added in a batch request. This ID does not necessarily correlate to a |
| 43 | + * request that already exists in the idToBatchContext map since it refers to the next entry (ex. if the last entry added |
| 44 | + * to idToBatchContext had an id of 22, nextBatchEntry will have a value of 23). |
| 45 | + */ |
| 46 | + private int nextBatchEntry; |
| 47 | + |
| 48 | + /** |
| 49 | + * The scheduled flush tasks associated with this batchBuffer. |
| 50 | + */ |
| 51 | + private ScheduledFuture<?> scheduledFlush; |
| 52 | + |
| 53 | + public BatchBuffer(ScheduledFuture<?> scheduledFlush) { |
| 54 | + this.idToBatchContext = new ConcurrentHashMap<>(); |
| 55 | + this.nextId = 0; |
| 56 | + this.nextBatchEntry = 0; |
| 57 | + this.scheduledFlush = scheduledFlush; |
| 58 | + } |
| 59 | + |
| 60 | + public Map<String, BatchingExecutionContext<RequestT, ResponseT>> flushableRequests(int maxBatchItems) { |
| 61 | + synchronized (flushLock) { |
| 62 | + if (idToBatchContext.size() >= maxBatchItems) { |
| 63 | + return extractFlushedEntries(maxBatchItems); |
| 64 | + } |
| 65 | + return new ConcurrentHashMap<>(); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + public Map<String, BatchingExecutionContext<RequestT, ResponseT>> flushableScheduledRequests(int maxBatchItems) { |
| 70 | + synchronized (flushLock) { |
| 71 | + if (idToBatchContext.size() > 0) { |
| 72 | + return extractFlushedEntries(maxBatchItems); |
| 73 | + } |
| 74 | + return new ConcurrentHashMap<>(); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + private Map<String, BatchingExecutionContext<RequestT, ResponseT>> extractFlushedEntries(int maxBatchItems) { |
| 79 | + LinkedHashMap<String, BatchingExecutionContext<RequestT, ResponseT>> requestEntries = new LinkedHashMap<>(); |
| 80 | + String nextEntry; |
| 81 | + while (requestEntries.size() < maxBatchItems && hasNextBatchEntry()) { |
| 82 | + nextEntry = nextBatchEntry(); |
| 83 | + requestEntries.put(nextEntry, idToBatchContext.get(nextEntry)); |
| 84 | + idToBatchContext.remove(nextEntry); |
| 85 | + } |
| 86 | + return requestEntries; |
| 87 | + } |
| 88 | + |
| 89 | + public RequestT getRequest(String key) { |
| 90 | + return idToBatchContext.get(key).request(); |
| 91 | + } |
| 92 | + |
| 93 | + public CompletableFuture<ResponseT> getResponse(String key) { |
| 94 | + return idToBatchContext.get(key).response(); |
| 95 | + } |
| 96 | + |
| 97 | + public BatchingExecutionContext<RequestT, ResponseT> put(RequestT request, CompletableFuture<ResponseT> response) { |
| 98 | + synchronized (this) { |
| 99 | + if (nextId == Integer.MAX_VALUE) { |
| 100 | + nextId = 0; |
| 101 | + } |
| 102 | + String id = Integer.toString(nextId++); |
| 103 | + return idToBatchContext.put(id, new BatchingExecutionContext<>(request, response)); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + private boolean hasNextBatchEntry() { |
| 108 | + return idToBatchContext.containsKey(Integer.toString(nextBatchEntry)); |
| 109 | + } |
| 110 | + |
| 111 | + private String nextBatchEntry() { |
| 112 | + if (nextBatchEntry == Integer.MAX_VALUE) { |
| 113 | + nextBatchEntry = 0; |
| 114 | + } |
| 115 | + return Integer.toString(nextBatchEntry++); |
| 116 | + } |
| 117 | + |
| 118 | + public void putScheduledFlush(ScheduledFuture<?> scheduledFlush) { |
| 119 | + this.scheduledFlush = scheduledFlush; |
| 120 | + } |
| 121 | + |
| 122 | + public void cancelScheduledFlush() { |
| 123 | + scheduledFlush.cancel(false); |
| 124 | + } |
| 125 | + |
| 126 | + public Collection<CompletableFuture<ResponseT>> responses() { |
| 127 | + return idToBatchContext.values() |
| 128 | + .stream() |
| 129 | + .map(BatchingExecutionContext::response) |
| 130 | + .collect(Collectors.toList()); |
| 131 | + } |
| 132 | + |
| 133 | + public void clear() { |
| 134 | + idToBatchContext.clear(); |
| 135 | + } |
| 136 | +} |
0 commit comments