This repository has been archived by the owner on Sep 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 119
/
BatcherImpl.java
452 lines (402 loc) · 16.3 KB
/
BatcherImpl.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/*
* Copyright 2019 Google LLC
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google LLC nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.google.api.gax.batching;
import static com.google.common.util.concurrent.MoreExecutors.directExecutor;
import com.google.api.core.ApiFuture;
import com.google.api.core.ApiFutureCallback;
import com.google.api.core.ApiFutures;
import com.google.api.core.BetaApi;
import com.google.api.core.InternalApi;
import com.google.api.core.SettableApiFuture;
import com.google.api.gax.rpc.ApiException;
import com.google.api.gax.rpc.StatusCode;
import com.google.api.gax.rpc.UnaryCallable;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.util.concurrent.Futures;
import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Queues up the elements until {@link #flush()} is called; once batching is over, returned future
* resolves.
*
* <p>This class is not thread-safe, and expects to be used from a single thread.
*
* @param <ElementT> The type of each individual element to be batched.
* @param <ElementResultT> The type of the result for each individual element.
* @param <RequestT> The type of the request that will contain the accumulated elements.
* @param <ResponseT> The type of the response that will unpack into individual element results.
*/
@BetaApi("The surface for batching is not stable yet and may change in the future.")
@InternalApi("For google-cloud-java client use only")
public class BatcherImpl<ElementT, ElementResultT, RequestT, ResponseT>
implements Batcher<ElementT, ElementResultT> {
private static final Logger LOG = Logger.getLogger(BatcherImpl.class.getName());
private final BatchingDescriptor<ElementT, ElementResultT, RequestT, ResponseT>
batchingDescriptor;
private final UnaryCallable<RequestT, ResponseT> unaryCallable;
private final RequestT prototype;
private final BatchingSettings batchingSettings;
private final BatcherReference currentBatcherReference;
private Batch<ElementT, ElementResultT, RequestT, ResponseT> currentOpenBatch;
private final AtomicInteger numOfOutstandingBatches = new AtomicInteger(0);
private final Object flushLock = new Object();
private final Object elementLock = new Object();
private final Object errorLock = new Object();
private final Future<?> scheduledFuture;
private volatile boolean isClosed = false;
private AtomicLong numOfFailure = new AtomicLong();
private final Map<Class, AtomicInteger> failuresTypeCount = new ConcurrentHashMap<>();
private final Map<StatusCode, AtomicInteger> failureStatusCodeCount = new ConcurrentHashMap<>();
/**
* @param batchingDescriptor a {@link BatchingDescriptor} for transforming individual elements
* into wrappers request and response.
* @param unaryCallable a {@link UnaryCallable} object.
* @param prototype a {@link RequestT} object.
* @param batchingSettings a {@link BatchingSettings} with configuration of thresholds.
*/
public BatcherImpl(
BatchingDescriptor<ElementT, ElementResultT, RequestT, ResponseT> batchingDescriptor,
UnaryCallable<RequestT, ResponseT> unaryCallable,
RequestT prototype,
BatchingSettings batchingSettings,
ScheduledExecutorService executor) {
this.batchingDescriptor =
Preconditions.checkNotNull(batchingDescriptor, "batching descriptor cannot be null");
this.unaryCallable = Preconditions.checkNotNull(unaryCallable, "callable cannot be null");
this.prototype = Preconditions.checkNotNull(prototype, "request prototype cannot be null");
this.batchingSettings =
Preconditions.checkNotNull(batchingSettings, "batching setting cannot be null");
Preconditions.checkNotNull(executor, "executor cannot be null");
currentOpenBatch = new Batch<>(prototype, batchingDescriptor, batchingSettings);
if (batchingSettings.getDelayThreshold() != null) {
long delay = batchingSettings.getDelayThreshold().toMillis();
PushCurrentBatchRunnable<ElementT, ElementResultT, RequestT, ResponseT> runnable =
new PushCurrentBatchRunnable<>(this);
scheduledFuture =
executor.scheduleWithFixedDelay(runnable, delay, delay, TimeUnit.MILLISECONDS);
} else {
scheduledFuture = Futures.immediateCancelledFuture();
}
currentBatcherReference = new BatcherReference(this);
}
/** {@inheritDoc} */
@Override
public ApiFuture<ElementResultT> add(ElementT element) {
Preconditions.checkState(!isClosed, "Cannot add elements on a closed batcher");
SettableApiFuture<ElementResultT> result = SettableApiFuture.create();
synchronized (elementLock) {
currentOpenBatch.add(element, result);
}
if (currentOpenBatch.hasAnyThresholdReached()) {
sendOutstanding();
}
return result;
}
/** {@inheritDoc} */
@Override
public void flush() throws InterruptedException {
sendOutstanding();
awaitAllOutstandingBatches();
}
/** {@inheritDoc} */
@Override
public void sendOutstanding() {
final Batch<ElementT, ElementResultT, RequestT, ResponseT> accumulatedBatch;
synchronized (elementLock) {
if (currentOpenBatch.isEmpty()) {
return;
}
accumulatedBatch = currentOpenBatch;
currentOpenBatch = new Batch<>(prototype, batchingDescriptor, batchingSettings);
}
final ApiFuture<ResponseT> batchResponse =
unaryCallable.futureCall(accumulatedBatch.builder.build());
numOfOutstandingBatches.incrementAndGet();
ApiFutures.addCallback(
batchResponse,
new ApiFutureCallback<ResponseT>() {
@Override
public void onSuccess(ResponseT response) {
try {
accumulatedBatch.onBatchSuccess(response);
} finally {
onBatchCompletion();
}
}
@Override
public void onFailure(Throwable throwable) {
try {
addException(throwable);
accumulatedBatch.onBatchFailure(throwable);
} finally {
onBatchCompletion();
}
}
},
directExecutor());
}
private void onBatchCompletion() {
if (numOfOutstandingBatches.decrementAndGet() == 0) {
synchronized (flushLock) {
flushLock.notifyAll();
}
}
}
private void awaitAllOutstandingBatches() throws InterruptedException {
while (numOfOutstandingBatches.get() > 0) {
synchronized (flushLock) {
flushLock.wait();
}
}
}
/**
* It keeps the count of number of failed RPCs. This method also tracks the count for exception
* type along with counts for different failed {@link StatusCode}s.
*/
private void addException(Throwable throwable) {
numOfFailure.incrementAndGet();
Class exceptionClass = throwable.getClass();
if (throwable instanceof ApiException) {
StatusCode code = ((ApiException) throwable).getStatusCode();
exceptionClass = ApiException.class;
synchronized (errorLock) {
if (failureStatusCodeCount.containsKey(code)) {
failureStatusCodeCount.get(code).incrementAndGet();
} else {
failureStatusCodeCount.put(code, new AtomicInteger(1));
}
}
}
synchronized (errorLock) {
if (failuresTypeCount.containsKey(exceptionClass)) {
failuresTypeCount.get(exceptionClass).incrementAndGet();
} else {
failuresTypeCount.put(exceptionClass, new AtomicInteger(1));
}
}
}
/** {@inheritDoc} */
@Override
public void close() throws InterruptedException {
if (isClosed) {
return;
}
flush();
scheduledFuture.cancel(true);
isClosed = true;
if (numOfFailure.get() > 0) {
throw new BatchingException(numOfFailure.get(), failuresTypeCount, failureStatusCodeCount);
}
currentBatcherReference.closed = true;
currentBatcherReference.clear();
}
/**
* This class represent one logical Batch. It accumulates all the elements and their corresponding
* future results for one batch.
*/
private static class Batch<ElementT, ElementResultT, RequestT, ResponseT> {
private final BatchingRequestBuilder<ElementT, RequestT> builder;
private final List<SettableApiFuture<ElementResultT>> results;
private final BatchingDescriptor<ElementT, ElementResultT, RequestT, ResponseT> descriptor;
private final long elementThreshold;
private final long bytesThreshold;
private long elementCounter = 0;
private long byteCounter = 0;
private Batch(
RequestT prototype,
BatchingDescriptor<ElementT, ElementResultT, RequestT, ResponseT> descriptor,
BatchingSettings batchingSettings) {
this.descriptor = descriptor;
this.builder = descriptor.newRequestBuilder(prototype);
this.results = new ArrayList<>();
Long elementCountThreshold = batchingSettings.getElementCountThreshold();
this.elementThreshold = elementCountThreshold == null ? 0 : elementCountThreshold;
Long requestByteThreshold = batchingSettings.getRequestByteThreshold();
this.bytesThreshold = requestByteThreshold == null ? 0 : requestByteThreshold;
}
void add(ElementT element, SettableApiFuture<ElementResultT> result) {
builder.add(element);
results.add(result);
elementCounter++;
byteCounter += descriptor.countBytes(element);
}
void onBatchSuccess(ResponseT response) {
try {
descriptor.splitResponse(response, results);
} catch (Exception ex) {
onBatchFailure(ex);
}
}
void onBatchFailure(Throwable throwable) {
try {
descriptor.splitException(throwable, results);
} catch (Exception ex) {
for (SettableApiFuture<ElementResultT> result : results) {
result.setException(ex);
}
}
}
boolean isEmpty() {
return elementCounter == 0;
}
boolean hasAnyThresholdReached() {
return elementCounter >= elementThreshold || byteCounter >= bytesThreshold;
}
}
/**
* Executes {@link #sendOutstanding()} on a periodic interval.
*
* <p>This class holds a weak reference to the Batcher instance and cancels polling if the target
* Batcher has been garbage collected.
*/
@VisibleForTesting
static class PushCurrentBatchRunnable<ElementT, ElementResultT, RequestT, ResponseT>
implements Runnable {
private Future<?> scheduledFuture;
private final WeakReference<BatcherImpl<ElementT, ElementResultT, RequestT, ResponseT>>
batcherReferent;
PushCurrentBatchRunnable(BatcherImpl<ElementT, ElementResultT, RequestT, ResponseT> batcher) {
this.batcherReferent = new WeakReference<>(batcher);
}
@Override
public void run() {
BatcherImpl<ElementT, ElementResultT, RequestT, ResponseT> batcher = batcherReferent.get();
if (batcher == null) {
scheduledFuture.cancel(true);
} else {
batcher.sendOutstanding();
}
}
void setScheduledFuture(Future<?> scheduledFuture) {
this.scheduledFuture = scheduledFuture;
}
boolean isCancelled() {
return scheduledFuture.isCancelled();
}
}
/**
* On every Batcher allocation this class will check for garbage collected batchers that were
* never closed and emit warning logs.
*/
@VisibleForTesting
static final class BatcherReference extends WeakReference<BatcherImpl> {
private static final ReferenceQueue<BatcherImpl> refQueue = new ReferenceQueue<>();
// Retain the References so they don't get GC'd
private static final ConcurrentMap<BatcherReference, BatcherReference> refs =
new ConcurrentHashMap<>();
private static final String ALLOCATION_SITE_PROPERTY_NAME =
"com.google.api.gax.batching.Batcher.enableAllocationTracking";
private static final boolean ENABLE_ALLOCATION_TRACKING =
Boolean.parseBoolean(System.getProperty(ALLOCATION_SITE_PROPERTY_NAME, "true"));
private static final RuntimeException missingCallSite = missingCallSite();
private final Reference<RuntimeException> allocationSite;
private volatile boolean closed;
BatcherReference(BatcherImpl referent) {
super(referent, refQueue);
// allocationSite is softReference to make it garbage collectible, but delay it as long as
// possible as BatcherReference can only be weakly referred.
allocationSite =
new SoftReference<>(
ENABLE_ALLOCATION_TRACKING
? new RuntimeException("Batcher allocation site")
: missingCallSite);
refs.put(this, this);
cleanQueue();
}
/**
* This clear() is *not* called automatically by the JVM. As this is a weak ref, the reference
* will be cleared automatically by the JVM, but will not be removed from {@link #refs}. We do
* it here to avoid this ending up on the reference queue.
*/
@Override
public void clear() {
clearInternal();
// We run this here to periodically clean up the queue if any Batcher is being
// closed properly.
cleanQueue();
}
private void clearInternal() {
super.clear();
refs.remove(this);
allocationSite.clear();
}
/**
* It performs below tasks:
*
* <ul>
* <li>Check each batcher registered on refQueue while initialization.
* <li>Unregister them from refQueue.
* <li>If close() is not called on the batcher, then emits log with possible allocationSite.
* <li>Keeps track of number of batcher on which close() is not called.
* </ul>
*/
@VisibleForTesting
static int cleanQueue() {
BatcherReference ref;
int orphanedBatchers = 0;
while ((ref = (BatcherReference) refQueue.poll()) != null) {
RuntimeException maybeAllocationSite = ref.allocationSite.get();
ref.clearInternal(); // technically the reference is gone already.
if (!ref.closed) {
orphanedBatchers++;
if (LOG.isLoggable(Level.SEVERE)) {
String message = "Batcher was not closed properly!!! Make sure to call close().";
LOG.log(Level.SEVERE, message, maybeAllocationSite);
}
}
}
return orphanedBatchers;
}
private static RuntimeException missingCallSite() {
RuntimeException e =
new RuntimeException(
"Batcher allocation site not recorded. Set -D"
+ ALLOCATION_SITE_PROPERTY_NAME
+ "=true to enable it");
e.setStackTrace(new StackTraceElement[0]);
return e;
}
}
}