Repository navigation
Expand file tree
/
Copy pathGcpChannelPrimeControllerTest.java
More file actions
480 lines (425 loc) · 16.4 KB
/
Copy pathGcpChannelPrimeControllerTest.java
File metadata and controls
480 lines (425 loc) · 16.4 KB
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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/*
* Copyright 2026 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.grpc;
import static com.google.common.truth.Truth.assertThat;
import static org.awaitility.Awaitility.await;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.SettableFuture;
import io.grpc.ManagedChannel;
import java.time.Duration;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import javax.annotation.Nullable;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
/**
* Direct {@link GcpChannelPrimeController} tests for the interleavings a pool-level test cannot
* force: they hand the controller a scheduler whose timers never fire on their own, so a test can
* run the timeout of an attempt at a moment of its choosing.
*/
@RunWith(JUnit4.class)
public final class GcpChannelPrimeControllerTest {
private final ExecutorService stateExecutor = Executors.newSingleThreadExecutor();
private final RecordingScheduler scheduler = new RecordingScheduler();
private final Object lock = new Object();
private final FakePool pool = new FakePool(lock);
@After
public void tearDown() {
scheduler.shutdownNow();
stateExecutor.shutdownNow();
}
@Test
public void poolCallbacksObserveTheLockContract() throws Exception {
AtomicInteger primeCalls = new AtomicInteger();
GcpChannelPrimer primer =
channel ->
primeCalls.incrementAndGet() == 1
? Futures.immediateFailedFuture(new IllegalStateException("prime failed"))
: Futures.immediateVoidFuture();
GcpChannelPrimeController controller = newController(primer, 1);
controller.startPrime(newChannel());
awaitCondition(() -> pool.failures.size() == 1);
controller.startPrime(newChannel());
awaitCondition(() -> pool.published.size() == 1);
assertThat(pool.lockViolations).isEmpty();
assertThat(pool.abandonReports.get()).isEqualTo(0);
synchronized (lock) {
assertThat(controller.inFlightCount()).isEqualTo(0);
}
}
@Test
public void timeoutAfterPrimeReturnsCancelsTheLateFutureAndRetries() throws Exception {
CountDownLatch primerEntered = new CountDownLatch(1);
CountDownLatch releasePrimer = new CountDownLatch(1);
SettableFuture<Void> lateFuture = SettableFuture.create();
AtomicInteger primeCalls = new AtomicInteger();
AtomicReference<Thread> primerThread = new AtomicReference<>();
GcpChannelPrimer primer =
channel -> {
if (primeCalls.incrementAndGet() > 1) {
return Futures.immediateVoidFuture();
}
primerThread.set(Thread.currentThread());
primerEntered.countDown();
awaitUninterruptibly(releasePrimer);
return lateFuture;
};
GcpChannelPrimeController controller = newController(primer, 3);
FakeChannel channel = newChannel();
controller.startPrime(channel);
assertThat(primerEntered.await(5, TimeUnit.SECONDS)).isTrue();
assertThat(scheduler.tasks).hasSize(1);
Runnable timeout = scheduler.tasks.get(0);
try {
synchronized (lock) {
// prime() returns while this thread holds the pool lock, so the primer thread parks on
// the monitor before it can record the returned future.
releasePrimer.countDown();
awaitCondition(() -> primerThread.get().getState() == Thread.State.BLOCKED);
// The timeout wins that race. prime() has returned, so this is an ordinary timed-out
// attempt: it must not be abandoned without a retry.
timeout.run();
assertThat(controller.abandonedCount()).isEqualTo(0);
assertThat(controller.inFlightCount()).isEqualTo(1);
}
} finally {
releasePrimer.countDown();
}
// The primer thread then sees the attempt has moved on and cancels the late future.
awaitCondition(lateFuture::isCancelled);
assertThat(pool.abandonReports.get()).isEqualTo(0);
assertThat(pool.failures).isEmpty();
assertThat(channel.isShutdown()).isFalse();
awaitCondition(() -> scheduler.tasks.size() == 2);
Runnable retry = scheduler.tasks.get(1);
retry.run();
awaitCondition(() -> pool.published.size() == 1);
assertThat(pool.published.get(0)).isSameInstanceAs(channel);
assertThat(primeCalls.get()).isEqualTo(2);
assertThat(pool.lockViolations).isEmpty();
}
@Test
public void timeoutBeforePrimeEntryRefusesTheExpiredAttempt() throws Exception {
CountDownLatch timeoutArmed = new CountDownLatch(1);
AtomicInteger primeCalls = new AtomicInteger();
AtomicReference<Thread> primerThread = new AtomicReference<>();
GcpChannelPrimer primer =
channel -> {
primeCalls.incrementAndGet();
return Futures.immediateVoidFuture();
};
GcpChannelPrimeController controller = newController(primer, 1);
FakeChannel channel = newChannel();
// Arming the timeout is the primer thread's last step under the pool lock before it enters
// prime(). Park it there with the lock released, exactly as if it had been descheduled between
// dropping the lock and entering prime(), until the test lets it go.
scheduler.onSchedule =
() -> {
primerThread.set(Thread.currentThread());
timeoutArmed.countDown();
synchronized (lock) {
while (!scheduler.releaseParkedThread) {
awaitUninterruptibly(lock);
}
}
};
controller.startPrime(channel);
assertThat(timeoutArmed.await(5, TimeUnit.SECONDS)).isTrue();
assertThat(scheduler.tasks).hasSize(1);
// The timeout wins that race: it runs to completion, releasing the slot and closing the
// channel, before the primer thread gets to move on.
scheduler.execute(scheduler.tasks.get(0));
awaitCondition(() -> pool.failures.size() == 1);
assertThat(channel.isShutdown()).isTrue();
synchronized (lock) {
assertThat(controller.inFlightCount()).isEqualTo(0);
assertThat(controller.abandonedCount()).isEqualTo(0);
scheduler.releaseParkedThread = true;
lock.notifyAll();
}
// The primer thread then finds its attempt expired and returns to the pool without ever
// calling prime(): idle pool threads wait on a timed poll, unlike a parked or blocked primer.
awaitCondition(
() -> {
Thread.State state = primerThread.get().getState();
return state == Thread.State.TIMED_WAITING || state == Thread.State.TERMINATED;
});
assertThat(primeCalls.get()).isEqualTo(0);
assertThat(pool.failures).hasSize(1);
assertThat(pool.abandonReports.get()).isEqualTo(0);
assertThat(pool.published).isEmpty();
assertThat(scheduler.tasks).hasSize(1);
synchronized (lock) {
assertThat(controller.primingCount()).isEqualTo(0);
}
assertThat(pool.lockViolations).isEmpty();
}
@Test
public void timeoutWhileBlockedInPrimeAbandonsAndReportsOutsideTheLock() throws Exception {
CountDownLatch primerEntered = new CountDownLatch(1);
CountDownLatch releasePrimer = new CountDownLatch(1);
SettableFuture<Void> lateFuture = SettableFuture.create();
GcpChannelPrimer primer =
channel -> {
primerEntered.countDown();
awaitUninterruptibly(releasePrimer);
return lateFuture;
};
GcpChannelPrimeController controller = newController(primer, 3);
FakeChannel channel = newChannel();
controller.startPrime(channel);
assertThat(primerEntered.await(5, TimeUnit.SECONDS)).isTrue();
try {
scheduler.tasks.get(0).run();
assertThat(pool.abandonReports.get()).isEqualTo(1);
assertThat(pool.failures).hasSize(1);
assertThat(channel.isShutdown()).isTrue();
assertThat(scheduler.tasks).hasSize(1);
synchronized (lock) {
assertThat(controller.abandonedCount()).isEqualTo(1);
assertThat(controller.inFlightCount()).isEqualTo(0);
}
} finally {
releasePrimer.countDown();
}
awaitCondition(lateFuture::isCancelled);
synchronized (lock) {
assertThat(controller.abandonedCount()).isEqualTo(0);
}
assertThat(pool.lockViolations).isEmpty();
}
@Test
public void timeoutWhileBlockedInPrimeDuringShutdownDoesNotReportAbandoning() throws Exception {
CountDownLatch primerEntered = new CountDownLatch(1);
CountDownLatch releasePrimer = new CountDownLatch(1);
SettableFuture<Void> lateFuture = SettableFuture.create();
GcpChannelPrimer primer =
channel -> {
primerEntered.countDown();
awaitUninterruptibly(releasePrimer);
return lateFuture;
};
GcpChannelPrimeController controller = newController(primer, 3);
FakeChannel channel = newChannel();
controller.startPrime(channel);
assertThat(primerEntered.await(5, TimeUnit.SECONDS)).isTrue();
try {
pool.shuttingDown = true;
scheduler.tasks.get(0).run();
assertThat(pool.abandonReports.get()).isEqualTo(0);
assertThat(pool.failures).isEmpty();
assertThat(channel.isShutdown()).isTrue();
synchronized (lock) {
assertThat(controller.abandonedCount()).isEqualTo(0);
assertThat(controller.inFlightCount()).isEqualTo(0);
}
} finally {
releasePrimer.countDown();
}
awaitCondition(lateFuture::isCancelled);
assertThat(pool.lockViolations).isEmpty();
}
private GcpChannelPrimeController newController(GcpChannelPrimer primer, int maxAttempts) {
return new GcpChannelPrimeController(
lock, pool, primer, TimeUnit.SECONDS.toNanos(5), maxAttempts, scheduler);
}
private FakeChannel newChannel() {
return new FakeChannel(stateExecutor);
}
private static void awaitCondition(Callable<Boolean> condition) {
await().atMost(Duration.ofSeconds(5)).until(condition);
}
private static void awaitUninterruptibly(CountDownLatch latch) {
boolean interrupted = false;
while (true) {
try {
latch.await();
break;
} catch (InterruptedException failure) {
interrupted = true;
}
}
if (interrupted) {
Thread.currentThread().interrupt();
}
}
/**
* One {@link Object#wait()} on {@code monitor}, which the caller must hold; may wake spuriously.
*/
private static void awaitUninterruptibly(Object monitor) {
try {
monitor.wait();
} catch (InterruptedException failure) {
Thread.currentThread().interrupt();
}
}
private static final class FakeChannel extends GcpManagedChannelTest.FakeManagedChannel {
FakeChannel(ExecutorService exec) {
super(exec);
}
}
/** Records every pool callback together with whether it honored its lock contract. */
private static final class FakePool implements GcpChannelPrimeController.Pool {
private final Object lock;
volatile boolean shuttingDown;
volatile boolean full;
final List<ManagedChannel> published = new CopyOnWriteArrayList<>();
final List<Throwable> failures = new CopyOnWriteArrayList<>();
final AtomicInteger abandonReports = new AtomicInteger();
final List<String> lockViolations = new CopyOnWriteArrayList<>();
FakePool(Object lock) {
this.lock = lock;
}
@Override
public boolean isShuttingDown() {
expectLock("isShuttingDown", true);
return shuttingDown;
}
@Override
public boolean isFull() {
expectLock("isFull", true);
return full;
}
@Override
public void addPrimedChannel(ManagedChannel channel) {
expectLock("addPrimedChannel", true);
published.add(channel);
}
@Override
public void reportPrimeFailure(Throwable failure) {
expectLock("reportPrimeFailure", false);
failures.add(failure);
}
@Override
public void reportPrimeAbandoned() {
expectLock("reportPrimeAbandoned", false);
abandonReports.incrementAndGet();
}
private void expectLock(String method, boolean held) {
if (Thread.holdsLock(lock) != held) {
lockViolations.add(
method + (held ? " called without the pool lock" : " called under the pool lock"));
}
}
}
/**
* Runs immediate work on a real thread but only records delayed tasks, leaving it to the test to
* run a timeout or retry at the moment under test.
*/
private static final class RecordingScheduler implements ScheduledExecutorService {
private final ScheduledExecutorService delegate = Executors.newSingleThreadScheduledExecutor();
final List<Runnable> tasks = new CopyOnWriteArrayList<>();
// Runs on the scheduling thread once a delayed task is recorded, so a test can park that
// thread at the moment it arms a timeout.
@Nullable volatile Runnable onSchedule;
// Guarded by the test's pool lock: set under it and announced with notifyAll().
boolean releaseParkedThread;
@Override
public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) {
tasks.add(command);
Runnable hook = onSchedule;
if (hook != null) {
hook.run();
}
return delegate.schedule(() -> {}, 365, TimeUnit.DAYS);
}
@Override
public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit) {
throw new UnsupportedOperationException();
}
@Override
public ScheduledFuture<?> scheduleAtFixedRate(
Runnable command, long initialDelay, long period, TimeUnit unit) {
throw new UnsupportedOperationException();
}
@Override
public ScheduledFuture<?> scheduleWithFixedDelay(
Runnable command, long initialDelay, long delay, TimeUnit unit) {
throw new UnsupportedOperationException();
}
@Override
public void execute(Runnable command) {
delegate.execute(command);
}
@Override
public void shutdown() {
delegate.shutdown();
}
@Override
public List<Runnable> shutdownNow() {
return delegate.shutdownNow();
}
@Override
public boolean isShutdown() {
return delegate.isShutdown();
}
@Override
public boolean isTerminated() {
return delegate.isTerminated();
}
@Override
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
return delegate.awaitTermination(timeout, unit);
}
@Override
public <T> java.util.concurrent.Future<T> submit(Callable<T> task) {
return delegate.submit(task);
}
@Override
public <T> java.util.concurrent.Future<T> submit(Runnable task, T result) {
return delegate.submit(task, result);
}
@Override
public java.util.concurrent.Future<?> submit(Runnable task) {
return delegate.submit(task);
}
@Override
public <T> List<java.util.concurrent.Future<T>> invokeAll(
java.util.Collection<? extends Callable<T>> tasks) throws InterruptedException {
return delegate.invokeAll(tasks);
}
@Override
public <T> List<java.util.concurrent.Future<T>> invokeAll(
java.util.Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
throws InterruptedException {
return delegate.invokeAll(tasks, timeout, unit);
}
@Override
public <T> T invokeAny(java.util.Collection<? extends Callable<T>> tasks)
throws InterruptedException, java.util.concurrent.ExecutionException {
return delegate.invokeAny(tasks);
}
@Override
public <T> T invokeAny(
java.util.Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
throws InterruptedException,
java.util.concurrent.ExecutionException,
java.util.concurrent.TimeoutException {
return delegate.invokeAny(tasks, timeout, unit);
}
}
}