/
DelayedClientTransport.java
537 lines (498 loc) · 19.9 KB
/
DelayedClientTransport.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
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
/*
* Copyright 2015, Google Inc. All rights reserved.
*
* 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 Inc. 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 io.grpc.internal;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import io.grpc.CallOptions;
import io.grpc.Context;
import io.grpc.LoadBalancer2;
import io.grpc.LoadBalancer2.PickResult;
import io.grpc.LoadBalancer2.Subchannel;
import io.grpc.Metadata;
import io.grpc.MethodDescriptor;
import io.grpc.Status;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.concurrent.Executor;
import javax.annotation.Nullable;
import javax.annotation.concurrent.GuardedBy;
/**
* A client transport that queues requests before a real transport is available. When a backing
* transport supplier is later provided, this class delegates to the transports from it.
*
* <p>This transport owns the streams that it has created before {@link #setTransport} is
* called. When {@link #setTransport} is called, the ownership of pending streams and subsequent new
* streams are transferred to the given transport, thus this transport won't own any stream.
*/
class DelayedClientTransport implements ManagedClientTransport {
private final LogId lodId = LogId.allocate(getClass().getName());
private final Object lock = new Object();
private final Executor streamCreationExecutor;
private Listener listener;
/** 'lock' must be held when assigning to transportSupplier. */
private volatile Supplier<ClientTransport> transportSupplier;
@GuardedBy("lock")
private Collection<PendingStream> pendingStreams = new LinkedHashSet<PendingStream>();
// TODO(zhangkun83): remove it once LBv2 refactor is done. In practice ping() is only called on
// real transports.
@GuardedBy("lock")
private Collection<PendingPing> pendingPings = new ArrayList<PendingPing>();
/**
* When shutdown == true and pendingStreams == null, then the transport is considered terminated.
*/
@GuardedBy("lock")
private boolean shutdown;
/**
* The delayed client transport will come into a back-off interval if it fails to establish a real
* transport for all addresses, namely the channel is in TRANSIENT_FAILURE. When in a back-off
* interval, {@code backoffStatus != null}.
*
* <p>If the transport is in a back-off interval, then all fail fast streams (including the
* pending as well as new ones) will fail immediately. New non-fail fast streams can be created as
* {@link PendingStream} and will keep pending during this back-off period.
*/
@GuardedBy("lock")
@Nullable
private Status backoffStatus;
DelayedClientTransport(Executor streamCreationExecutor) {
this.streamCreationExecutor = streamCreationExecutor;
}
@Override
public final Runnable start(Listener listener) {
this.listener = Preconditions.checkNotNull(listener, "listener");
return null;
}
/**
* If the transport has acquired a transport {@link Supplier}, then returned stream is delegated
* from its supplier.
*
* <p>If the new stream to be created is with fail fast call option and the delayed transport is
* in a back-off interval, then a {@link FailingClientStream} is returned.
*
* <p>If it is not the above cases and the delayed transport is not shutdown, then a
* {@link PendingStream} is returned; if the transport is shutdown, then a
* {@link FailingClientStream} is returned.
*/
@Override
public final ClientStream newStream(MethodDescriptor<?, ?> method, Metadata headers,
CallOptions callOptions, StatsTraceContext statsTraceCtx) {
Supplier<ClientTransport> supplier = transportSupplier;
if (supplier == null) {
synchronized (lock) {
// Check again, since it may have changed while waiting for lock
supplier = transportSupplier;
if (supplier == null && !shutdown) {
if (backoffStatus != null && !callOptions.isWaitForReady()) {
return new FailingClientStream(backoffStatus);
}
PendingStream pendingStream = new PendingStream(method, headers, callOptions,
statsTraceCtx);
pendingStreams.add(pendingStream);
if (pendingStreams.size() == 1) {
listener.transportInUse(true);
}
return pendingStream;
}
}
}
if (supplier != null) {
return supplier.get().newStream(method, headers, callOptions, statsTraceCtx);
}
return new FailingClientStream(Status.UNAVAILABLE.withDescription("transport shutdown"));
}
@Override
public final ClientStream newStream(MethodDescriptor<?, ?> method, Metadata headers) {
return newStream(method, headers, CallOptions.DEFAULT, StatsTraceContext.NOOP);
}
@Override
public final void ping(final PingCallback callback, Executor executor) {
Supplier<ClientTransport> supplier = transportSupplier;
if (supplier == null) {
synchronized (lock) {
// Check again, since it may have changed while waiting for lock
supplier = transportSupplier;
if (supplier == null && !shutdown) {
PendingPing pendingPing = new PendingPing(callback, executor);
pendingPings.add(pendingPing);
return;
}
}
}
if (supplier != null) {
supplier.get().ping(callback, executor);
return;
}
executor.execute(new Runnable() {
@Override public void run() {
callback.onFailure(
Status.UNAVAILABLE.withDescription("transport shutdown").asException());
}
});
}
/**
* Prevents creating any new streams until {@link #setTransport} is called. Buffered streams are
* not failed, so if {@link #shutdown} is called when {@link #setTransport} has not been called,
* you still need to call {@link #setTransport} to make this transport terminated.
*/
@Override
public final void shutdown() {
synchronized (lock) {
if (shutdown) {
return;
}
shutdown = true;
listener.transportShutdown(
Status.UNAVAILABLE.withDescription("Channel requested transport to shut down"));
if (pendingStreams == null || pendingStreams.isEmpty()) {
pendingStreams = null;
listener.transportTerminated();
}
}
}
/**
* Shuts down this transport and cancels all streams that it owns, hence immediately terminates
* this transport.
*/
@Override
public final void shutdownNow(Status status) {
shutdown();
Collection<PendingStream> savedPendingStreams = null;
synchronized (lock) {
if (pendingStreams != null) {
savedPendingStreams = pendingStreams;
pendingStreams = null;
}
}
if (savedPendingStreams != null) {
for (PendingStream stream : savedPendingStreams) {
stream.cancel(status);
}
listener.transportTerminated();
}
// If savedPendingStreams == null, transportTerminated() has already been called in shutdown().
}
/**
* Transfers all the pending and future streams and pings to the given transport.
*
* <p>May only be called after {@link #start(Listener)}.
*
* <p>{@code transport} will be used for all future calls to {@link #newStream}, even if this
* transport is {@link #shutdown}.
*/
public final void setTransport(ClientTransport transport) {
Preconditions.checkArgument(this != transport,
"delayed transport calling setTransport on itself");
setTransportSupplier(Suppliers.ofInstance(transport));
}
/**
* Transfers all the pending and future streams and pings to the transports from the given {@link
* Supplier}.
*
* <p>May only be called after {@link #start}. No effect if already called.
*
* <p>Each stream or ping will result in an invocation to {@link Supplier#get} once. The supplier
* will be used for all future calls to {@link #newStream}, even if this transport is {@link
* #shutdown}.
*/
public final void setTransportSupplier(final Supplier<ClientTransport> supplier) {
synchronized (lock) {
if (transportSupplier != null) {
return;
}
Preconditions.checkState(listener != null, "start() not called");
transportSupplier = Preconditions.checkNotNull(supplier, "supplier");
for (PendingPing ping : pendingPings) {
ping.createRealPing(supplier.get());
}
pendingPings = null;
if (shutdown && pendingStreams != null) {
listener.transportTerminated();
}
if (pendingStreams != null && !pendingStreams.isEmpty()) {
final Collection<PendingStream> savedPendingStreams = pendingStreams;
// createRealStream may be expensive. It will start real streams on the transport. If there
// are pending requests, they will be serialized too, which may be expensive. Since we are
// now on transport thread, we need to offload the work to an executor.
streamCreationExecutor.execute(new Runnable() {
@Override public void run() {
for (final PendingStream stream : savedPendingStreams) {
stream.createRealStream(supplier.get());
}
// TODO(zhangkun83): some transports (e.g., netty) may have a short delay between
// stream.start() and transportInUse(true). If netty's transportInUse(true) is called
// after the delayed transport's transportInUse(false), the channel may have a brief
// period where all transports are not in-use, and may go to IDLE mode unexpectedly if
// the IDLE timeout is shorter (e.g., 0) than that brief period. Maybe we should
// have a minimum IDLE timeout?
synchronized (lock) {
listener.transportInUse(false);
}
}
});
}
pendingStreams = null;
if (!shutdown) {
listener.transportReady();
}
}
}
public final boolean hasPendingStreams() {
synchronized (lock) {
return pendingStreams != null && !pendingStreams.isEmpty();
}
}
@VisibleForTesting
final int getPendingStreamsCount() {
synchronized (lock) {
return pendingStreams == null ? 0 : pendingStreams.size();
}
}
/**
* True return value indicates that the delayed transport is in a back-off interval (in
* TRANSIENT_FAILURE), that all fail fast streams (including pending as well as new ones) should
* fail immediately, and that non-fail fast streams can be created as {@link PendingStream} and
* should keep pending during this back-off period.
*/
// TODO(zhangkun83): remove it once the LBv2 refactor is done.
@VisibleForTesting
final boolean isInBackoffPeriod() {
synchronized (lock) {
return backoffStatus != null;
}
}
/**
* Is only called at the beginning of {@link TransportSet#scheduleBackoff}.
*
* <p>Does jobs at the beginning of the back-off:
*
* <p>sets {@link #backoffStatus};
*
* <p>sets all pending streams with a fail fast call option of the delayed transport as
* {@link FailingClientStream}s, and removes them from the list of pending streams of the
* transport.
*
* @param status the causal status for triggering back-off.
*/
// TODO(zhangkun83): remove it once the LBv2 refactor is done.
final void startBackoff(final Status status) {
synchronized (lock) {
if (shutdown) {
return;
}
Preconditions.checkState(backoffStatus == null,
"Error when calling startBackoff: transport is already in backoff period");
backoffStatus = Status.UNAVAILABLE.withDescription("Channel in TRANSIENT_FAILURE state")
.withCause(status.asRuntimeException());
final ArrayList<PendingStream> failFastPendingStreams = new ArrayList<PendingStream>();
if (pendingStreams != null && !pendingStreams.isEmpty()) {
final Iterator<PendingStream> it = pendingStreams.iterator();
while (it.hasNext()) {
PendingStream stream = it.next();
if (!stream.callOptions.isWaitForReady()) {
failFastPendingStreams.add(stream);
it.remove();
}
}
class FailTheFailFastPendingStreams implements Runnable {
@Override
public void run() {
for (PendingStream stream : failFastPendingStreams) {
stream.setStream(new FailingClientStream(status));
}
}
}
streamCreationExecutor.execute(new FailTheFailFastPendingStreams());
}
}
}
/**
* Is only called at the beginning of the callback function of {@code endOfCurrentBackoff} in the
* {@link TransportSet#scheduleBackoff} method.
*/
// TODO(zhangkun83): remove it once the LBv2 refactor is done.
final void endBackoff() {
synchronized (lock) {
Preconditions.checkState(backoffStatus != null,
"Error when calling endBackoff: transport is not in backoff period");
backoffStatus = null;
}
}
/**
* Use the picker to try picking a transport for every pending stream and pending ping, proceed
* the stream or ping if the pick is successful, otherwise keep it pending.
*
* <p>If new pending streams are created while reprocess() is in progress, there is no guarantee
* that the pending streams will or will not be processed by this picker.
*
* <p>This method <strong>must not</strong> be called concurrently, either with itself or with
* {@link #setTransportSupplier}/{@link #setTransport}.
*/
final void reprocess(LoadBalancer2.SubchannelPicker picker) {
ArrayList<PendingStream> toProcess;
ArrayList<PendingStream> toRemove = new ArrayList<PendingStream>();
synchronized (lock) {
if (pendingStreams == null || pendingStreams.isEmpty()) {
return;
}
toProcess = new ArrayList<PendingStream>(pendingStreams);
}
for (final PendingStream stream : toProcess) {
PickResult pickResult = picker.pickSubchannel(
stream.callOptions.getAffinity(), stream.headers);
final ClientTransport realTransport;
Subchannel subchannel = pickResult.getSubchannel();
if (subchannel != null) {
realTransport = ((SubchannelImpl) subchannel).obtainActiveTransport();
} else {
realTransport = null;
}
if (realTransport != null) {
Executor executor = streamCreationExecutor;
// createRealStream may be expensive. It will start real streams on the transport. If
// there are pending requests, they will be serialized too, which may be expensive. Since
// we are now on transport thread, we need to offload the work to an executor.
if (stream.callOptions.getExecutor() != null) {
executor = stream.callOptions.getExecutor();
}
executor.execute(new Runnable() {
@Override
public void run() {
stream.createRealStream(realTransport);
}
});
toRemove.add(stream);
} else if (!pickResult.getStatus().isOk() && !stream.callOptions.isWaitForReady()) {
stream.setStream(new FailingClientStream(pickResult.getStatus()));
toRemove.add(stream);
} // other cases: stay pending
}
synchronized (lock) {
// Between this synchronized and the previous one:
// - Streams may have been cancelled, which may turn pendingStreams into emptiness.
// - shutdown() may be called, which may turn pendingStreams into null.
if (pendingStreams == null || pendingStreams.isEmpty()) {
return;
}
pendingStreams.removeAll(toRemove);
if (pendingStreams.isEmpty()) {
// There may be a brief gap between delayed transport clearing in-use state, and first real
// transport starting streams and setting in-use state. During the gap the whole channel's
// in-use state may be false. However, it shouldn't cause spurious switching to idleness
// (which would shutdown the transports and LoadBalancer) because the gap should be shorter
// than IDLE_MODE_DEFAULT_TIMEOUT_MILLIS (1 second).
listener.transportInUse(false);
if (shutdown) {
pendingStreams = null;
listener.transportTerminated();
} else {
// Because delayed transport is long-lived, we take this opportunity to down-size the
// hashmap.
pendingStreams = new LinkedHashSet<PendingStream>();
}
}
}
}
// TODO(carl-mastrangelo): remove this once the Subchannel change is in.
@Override
public LogId getLogId() {
return lodId;
}
@VisibleForTesting
@Nullable
final Supplier<ClientTransport> getTransportSupplier() {
return transportSupplier;
}
private class PendingStream extends DelayedStream {
private final MethodDescriptor<?, ?> method;
private final Metadata headers;
private final CallOptions callOptions;
private final Context context;
private final StatsTraceContext statsTraceCtx;
private PendingStream(MethodDescriptor<?, ?> method, Metadata headers,
CallOptions callOptions, StatsTraceContext statsTraceCtx) {
this.method = method;
this.headers = headers;
this.callOptions = callOptions;
this.context = Context.current();
this.statsTraceCtx = statsTraceCtx;
}
private void createRealStream(ClientTransport transport) {
ClientStream realStream;
Context origContext = context.attach();
try {
realStream = transport.newStream(method, headers, callOptions, statsTraceCtx);
} finally {
context.detach(origContext);
}
setStream(realStream);
}
@Override
public void cancel(Status reason) {
super.cancel(reason);
synchronized (lock) {
if (pendingStreams != null) {
boolean justRemovedAnElement = pendingStreams.remove(this);
if (pendingStreams.isEmpty() && justRemovedAnElement) {
listener.transportInUse(false);
if (shutdown) {
pendingStreams = null;
listener.transportTerminated();
}
}
}
}
}
}
private static class PendingPing {
private final PingCallback callback;
private final Executor executor;
public PendingPing(PingCallback callback, Executor executor) {
this.callback = callback;
this.executor = executor;
}
public void createRealPing(ClientTransport transport) {
try {
transport.ping(callback, executor);
} catch (final UnsupportedOperationException ex) {
executor.execute(new Runnable() {
@Override
public void run() {
callback.onFailure(ex);
}
});
}
}
}
}