forked from grpc/grpc-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChannelImpl.java
More file actions
374 lines (337 loc) · 11.9 KB
/
ChannelImpl.java
File metadata and controls
374 lines (337 loc) · 11.9 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
/*
* Copyright 2014, 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;
import com.google.common.base.Preconditions;
import com.google.common.base.Throwables;
import io.grpc.transport.ClientStream;
import io.grpc.transport.ClientStreamListener;
import io.grpc.transport.ClientTransport;
import io.grpc.transport.ClientTransportFactory;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
import javax.annotation.concurrent.GuardedBy;
import javax.annotation.concurrent.ThreadSafe;
/** A communication channel for making outgoing RPCs. */
@ThreadSafe
public final class ChannelImpl implements Channel {
private static final Logger log = Logger.getLogger(ChannelImpl.class.getName());
private static class NoopClientStream implements ClientStream {
@Override public void writeMessage(InputStream message, int length, Runnable accepted) {}
@Override public void flush() {}
@Override public void cancel() {}
@Override public void halfClose() {}
@Override public void request(int numMessages) {}
}
private final ClientTransportFactory transportFactory;
private final ExecutorService executor;
/**
* All transports that are not stopped. At the very least {@link #activeTransport} will be
* present, but previously used transports that still have streams or are stopping may also be
* present.
*/
@GuardedBy("this")
private Collection<ClientTransport> transports = new ArrayList<ClientTransport>();
/** The transport for new outgoing requests. */
@GuardedBy("this")
private ClientTransport activeTransport;
@GuardedBy("this")
private boolean shutdown;
@GuardedBy("this")
private boolean terminated;
private Runnable terminationRunnable;
public ChannelImpl(ClientTransportFactory transportFactory, ExecutorService executor) {
this.transportFactory = transportFactory;
this.executor = executor;
}
/** Hack to allow executors to auto-shutdown. Not for general use. */
// TODO(ejona86): Replace with a real API.
void setTerminationRunnable(Runnable runnable) {
this.terminationRunnable = runnable;
}
/**
* Initiates an orderly shutdown in which preexisting calls continue but new calls are immediately
* cancelled.
*/
public synchronized ChannelImpl shutdown() {
if (shutdown) {
return this;
}
shutdown = true;
if (activeTransport != null) {
activeTransport.shutdown();
activeTransport = null;
} else if (transports.isEmpty()) {
terminated = true;
notifyAll();
if (terminationRunnable != null) {
terminationRunnable.run();
}
}
return this;
}
/**
* Initiates a forceful shutdown in which preexisting and new calls are cancelled. Although
* forceful, the shutdown process is still not instantaneous; {@link #isTerminated()} will likely
* return {@code false} immediately after this method returns.
*
* <p>NOT YET IMPLEMENTED. This method currently behaves identically to shutdown().
*/
// TODO(ejona86): cancel preexisting calls.
public synchronized ChannelImpl shutdownNow() {
shutdown();
return this;
}
/**
* Returns whether the channel is shutdown. Shutdown channels immediately cancel any new calls,
* but may still have some calls being processed.
*
* @see #shutdown()
* @see #isTerminated()
*/
public synchronized boolean isShutdown() {
return shutdown;
}
/**
* Waits for the channel to become terminated, giving up if the timeout is reached.
*
* @return whether the channel is terminated, as would be done by {@link #isTerminated()}.
*/
public synchronized boolean awaitTerminated(long timeout, TimeUnit unit)
throws InterruptedException {
long timeoutNanos = unit.toNanos(timeout);
long endTimeNanos = System.nanoTime() + timeoutNanos;
while (!terminated && (timeoutNanos = endTimeNanos - System.nanoTime()) > 0) {
TimeUnit.NANOSECONDS.timedWait(this, timeoutNanos);
}
return terminated;
}
/**
* Returns whether the channel is terminated. Terminated channels have no running calls and
* relevant resources released (like TCP connections).
*
* @see #isShutdown()
*/
public synchronized boolean isTerminated() {
return terminated;
}
/**
* Creates a new outgoing call on the channel.
*/
@Override
public <ReqT, RespT> Call<ReqT, RespT> newCall(MethodDescriptor<ReqT, RespT> method) {
return new CallImpl<ReqT, RespT>(method, new SerializingExecutor(executor));
}
private synchronized ClientTransport obtainActiveTransport() {
if (shutdown) {
return null;
}
if (activeTransport != null) {
return activeTransport;
}
activeTransport = transportFactory.newClientTransport();
transports.add(activeTransport);
activeTransport.start(new TransportListener(activeTransport));
return activeTransport;
}
private class TransportListener implements ClientTransport.Listener {
private final ClientTransport transport;
public TransportListener(ClientTransport transport) {
this.transport = transport;
}
@Override
public void transportShutdown() {
synchronized (ChannelImpl.this) {
if (activeTransport == transport) {
activeTransport = null;
}
}
}
@Override
public void transportTerminated() {
synchronized (ChannelImpl.this) {
if (activeTransport == transport) {
log.warning("transportTerminated called without previous transportShutdown");
activeTransport = null;
}
transportShutdown();
transports.remove(transport);
if (shutdown && transports.isEmpty()) {
if (terminated) {
log.warning("transportTerminated called after already terminated");
}
terminated = true;
ChannelImpl.this.notifyAll();
if (terminationRunnable != null) {
terminationRunnable.run();
}
}
}
}
}
private class CallImpl<ReqT, RespT> extends Call<ReqT, RespT> {
private final MethodDescriptor<ReqT, RespT> method;
private final SerializingExecutor callExecutor;
private final boolean unaryRequest;
private ClientStream stream;
public CallImpl(MethodDescriptor<ReqT, RespT> method, SerializingExecutor executor) {
this.method = method;
this.callExecutor = executor;
this.unaryRequest = method.getType() == MethodType.UNARY
|| method.getType() == MethodType.SERVER_STREAMING;
}
@Override
public void start(Listener<RespT> observer, Metadata.Headers headers) {
Preconditions.checkState(stream == null, "Already started");
ClientStreamListener listener = new ClientStreamListenerImpl(observer);
ClientTransport transport = obtainActiveTransport();
if (transport == null) {
stream = new NoopClientStream();
listener.closed(Status.CANCELLED.withDescription("Channel is shutdown"),
new Metadata.Trailers());
return;
}
try {
stream = transport.newStream(method, headers, listener);
} catch (IllegalStateException ex) {
// We can race with the transport and end up trying to use a terminated transport.
// TODO(ejona86): Improve the API to remove the possibility of the race.
stream = new NoopClientStream();
listener.closed(Status.fromThrowable(ex), new Metadata.Trailers());
return;
}
}
@Override
public void request(int numMessages) {
stream.request(numMessages);
}
@Override
public void cancel() {
// Cancel is called in exception handling cases, so it may be the case that the
// stream was never successfully created.
if (stream != null) {
stream.cancel();
}
}
@Override
public void halfClose() {
Preconditions.checkState(stream != null, "Not started");
stream.halfClose();
}
private int available(InputStream is) {
try {
return is.available();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
@Override
public void sendPayload(ReqT payload) {
Preconditions.checkState(stream != null, "Not started");
boolean failed = true;
try {
InputStream payloadIs = method.streamRequest(payload);
stream.writeMessage(payloadIs, available(payloadIs), null);
failed = false;
} finally {
if (failed) {
cancel();
}
}
// For unary requests, we don't flush since we know that halfClose should be coming soon. This
// allows us to piggy-back the END_STREAM=true on the last payload frame without opening the
// possibility of broken applications forgetting to call halfClose without noticing.
if (!unaryRequest) {
stream.flush();
}
}
private class ClientStreamListenerImpl implements ClientStreamListener {
private final Listener<RespT> observer;
private boolean closed;
public ClientStreamListenerImpl(Listener<RespT> observer) {
Preconditions.checkNotNull(observer);
this.observer = observer;
}
@Override
public void headersRead(final Metadata.Headers headers) {
callExecutor.execute(new Runnable() {
@Override
public void run() {
try {
if (closed) {
return;
}
observer.onHeaders(headers);
} catch (Throwable t) {
cancel();
throw Throwables.propagate(t);
}
}
});
}
@Override
public void messageRead(final InputStream message) {
callExecutor.execute(new Runnable() {
@Override
public void run() {
try {
if (closed) {
return;
}
try {
observer.onPayload(method.parseResponse(message));
} finally {
message.close();
}
} catch (Throwable t) {
cancel();
throw Throwables.propagate(t);
}
}
});
}
@Override
public void closed(final Status status, final Metadata.Trailers trailers) {
callExecutor.execute(new Runnable() {
@Override
public void run() {
closed = true;
observer.onClose(status, trailers);
}
});
}
}
}
}