|
| 1 | +/* |
| 2 | + * Copyright 2018 Google LLC |
| 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 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.batchingexperimental; |
| 18 | + |
| 19 | +import com.google.api.core.InternalApi; |
| 20 | +import com.google.api.core.SettableApiFuture; |
| 21 | +import com.google.common.base.Preconditions; |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.List; |
| 24 | + |
| 25 | +/** |
| 26 | + * Accumulates requests, used to batch many requests into one RPC call. |
| 27 | + * |
| 28 | + * <p>Experimental and only meant to be used by other packages in google-cloud-java. |
| 29 | + * |
| 30 | + * <p>Methods are not thread-safe. |
| 31 | + * |
| 32 | + * <p>Sample usage: |
| 33 | + * |
| 34 | + * <pre>{@code |
| 35 | + * synchronized(accumulator) { |
| 36 | + * accumulator.add(item, future); |
| 37 | + * while (accumulator.hasBatch()) { |
| 38 | + * // If items and futures must be retained, they must be copied. |
| 39 | + * List<ElementT> items = new ArrayList<>(accumulator.batch()); |
| 40 | + * List<SettableApiFuture<ResponseT>> futures = new ArrayList<>(accumulator.futureBatch()); |
| 41 | + * |
| 42 | + * // ... |
| 43 | + * |
| 44 | + * accumulator.next(); |
| 45 | + * } |
| 46 | + * } |
| 47 | + * |
| 48 | + * @param <ElementT> The elements that, when batched, make up RPC calls. |
| 49 | + * @param <ResponseT> The RPC results for the individual elements. |
| 50 | + * }</pre> |
| 51 | + */ |
| 52 | +@InternalApi |
| 53 | +public class RequestAccumulator<ElementT, ResponseT> { |
| 54 | + private final ArrayList<ElementT> requests = new ArrayList<>(); |
| 55 | + private final ArrayList<SettableApiFuture<ResponseT>> futures = new ArrayList<>(); |
| 56 | + private long curBytes = 0; |
| 57 | + |
| 58 | + // If not 0, the last element of requests is oversized and |
| 59 | + // must be sent in its own batch. |
| 60 | + private long oversizedBytes = 0; |
| 61 | + |
| 62 | + private final int batchCount; |
| 63 | + private final long batchBytes; |
| 64 | + |
| 65 | + /** |
| 66 | + * Creates a new accumulator, holding {@code batchCount} items and {@code batchBytes} bytes in a |
| 67 | + * batch. |
| 68 | + * |
| 69 | + * <p>{@code batchBytes} is a soft limit. Once the current batch is larger than {@code |
| 70 | + * batchBytes}, no new items will be added to the batch. {@code batchCount} is a hard limit. Once |
| 71 | + * reached, no new items will be added. |
| 72 | + */ |
| 73 | + public RequestAccumulator(int batchCount, long batchBytes) { |
| 74 | + Preconditions.checkArgument(batchCount > 0, "batchCount must be positive"); |
| 75 | + Preconditions.checkArgument(batchBytes > 0, "batchBytes must be positive"); |
| 76 | + this.batchCount = batchCount; |
| 77 | + this.batchBytes = batchBytes; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Add a new item {@code e} with size {@code bytes} into the current batch. The item is associated |
| 82 | + * with a {@code future}. |
| 83 | + * |
| 84 | + * <p>Given a call {@code add(e, f)}, {@code e} and {@code f} are in the same index in {@link |
| 85 | + * #batch()} and {@link #futureBatch()}. |
| 86 | + * |
| 87 | + * @throws IllegalArgumentException if {@code bytes} is not positive. |
| 88 | + * @throws IllegalStateException if {@link #hasBatch()} would return true immediately before the |
| 89 | + * call. |
| 90 | + */ |
| 91 | + public void add(ElementT e, long bytes, SettableApiFuture<ResponseT> future) { |
| 92 | + Preconditions.checkArgument(bytes >= 0, "size of element must not be negative"); |
| 93 | + Preconditions.checkState(!hasBatch(), "invalid use of add; there's already a batch waiting"); |
| 94 | + |
| 95 | + if (!requests.isEmpty() && bytes >= batchBytes) { |
| 96 | + oversizedBytes = bytes; |
| 97 | + } |
| 98 | + requests.add(e); |
| 99 | + futures.add(future); |
| 100 | + curBytes += bytes; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Reports whether the current batch is ready to be sent. If this method returns true, it is |
| 105 | + * invalid to call {@link #add()}. See the class documentation for example usage. |
| 106 | + */ |
| 107 | + public boolean hasBatch() { |
| 108 | + return requests.size() >= batchCount || curBytes >= batchBytes; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Returns the previously added items. The returned list must not be modified and will become |
| 113 | + * invalid after the next call to {@link #next()}. |
| 114 | + */ |
| 115 | + public List<ElementT> batch() { |
| 116 | + if (oversizedBytes > 0) { |
| 117 | + return requests.subList(0, requests.size() - 1); |
| 118 | + } |
| 119 | + return requests; |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Returns the previously added futures. The returned list must not be modified and will become |
| 124 | + * invalid after the next call to {@link #next()}. |
| 125 | + */ |
| 126 | + public List<SettableApiFuture<ResponseT>> futureBatch() { |
| 127 | + if (oversizedBytes > 0) { |
| 128 | + return futures.subList(0, futures.size() - 1); |
| 129 | + } |
| 130 | + return futures; |
| 131 | + } |
| 132 | + |
| 133 | + /** Consumes the current batch. */ |
| 134 | + public void next() { |
| 135 | + batch().clear(); |
| 136 | + futureBatch().clear(); |
| 137 | + curBytes = oversizedBytes; |
| 138 | + oversizedBytes = 0; |
| 139 | + } |
| 140 | +} |
0 commit comments