forked from grpc/grpc-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractServerStream.java
351 lines (316 loc) · 11.7 KB
/
AbstractServerStream.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
/*
* Copyright 2014 The gRPC Authors
*
* 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
*
* http://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 io.grpc.internal;
import com.google.common.base.Preconditions;
import io.grpc.Attributes;
import io.grpc.Decompressor;
import io.grpc.InternalStatus;
import io.grpc.Metadata;
import io.grpc.Status;
import javax.annotation.Nullable;
/**
* Abstract base class for {@link ServerStream} implementations. Extending classes only need to
* implement {@link #transportState()} and {@link #abstractServerStreamSink()}. Must only be called
* from the sending application thread.
*/
public abstract class AbstractServerStream extends AbstractStream
implements ServerStream, MessageFramer.Sink {
/**
* A sink for outbound operations, separated from the stream simply to avoid name
* collisions/confusion. Only called from application thread.
*/
protected interface Sink {
/**
* Sends response headers to the remote end point.
*
* @param headers the headers to be sent to client.
*/
void writeHeaders(Metadata headers);
/**
* Sends an outbound frame to the remote end point.
*
* @param frame a buffer containing the chunk of data to be sent.
* @param flush {@code true} if more data may not be arriving soon
* @param numMessages the number of messages this frame represents
*/
void writeFrame(WritableBuffer frame, boolean flush, int numMessages);
/**
* Sends trailers to the remote end point. This call implies end of stream.
*
* @param trailers metadata to be sent to the end point
* @param headersSent {@code true} if response headers have already been sent.
* @param status the status that the call ended with
*/
void writeTrailers(Metadata trailers, boolean headersSent, Status status);
/**
* Tears down the stream, typically in the event of a timeout. This method may be called
* multiple times and from any thread.
*
* <p>This is a clone of {@link ServerStream#cancel(Status)}.
*/
void cancel(Status status);
}
private final MessageFramer framer;
private final StatsTraceContext statsTraceCtx;
private boolean outboundClosed;
private boolean headersSent;
protected AbstractServerStream(
WritableBufferAllocator bufferAllocator, StatsTraceContext statsTraceCtx) {
this.statsTraceCtx = Preconditions.checkNotNull(statsTraceCtx, "statsTraceCtx");
framer = new MessageFramer(this, bufferAllocator, statsTraceCtx);
}
@Override
protected abstract TransportState transportState();
/**
* Sink for transport to be called to perform outbound operations. Each stream must have its own
* unique sink.
*/
protected abstract Sink abstractServerStreamSink();
@Override
protected final MessageFramer framer() {
return framer;
}
@Override
public final void writeHeaders(Metadata headers) {
Preconditions.checkNotNull(headers, "headers");
headersSent = true;
abstractServerStreamSink().writeHeaders(headers);
}
@Override
public final void deliverFrame(
WritableBuffer frame, boolean endOfStream, boolean flush, int numMessages) {
// Since endOfStream is triggered by the sending of trailers, avoid flush here and just flush
// after the trailers.
if (frame == null) {
assert endOfStream;
return;
}
if (endOfStream) {
flush = false;
}
abstractServerStreamSink().writeFrame(frame, flush, numMessages);
}
@Override
public final void close(Status status, Metadata trailers) {
Preconditions.checkNotNull(status, "status");
Preconditions.checkNotNull(trailers, "trailers");
if (!outboundClosed) {
outboundClosed = true;
endOfMessages();
addStatusToTrailers(trailers, status);
// Safe to set without synchronization because access is tightly controlled.
// closedStatus is only set from here, and is read from a place that has happen-after
// guarantees with respect to here.
transportState().setClosedStatus(status);
abstractServerStreamSink().writeTrailers(trailers, headersSent, status);
}
}
private void addStatusToTrailers(Metadata trailers, Status status) {
trailers.discardAll(InternalStatus.CODE_KEY);
trailers.discardAll(InternalStatus.MESSAGE_KEY);
trailers.put(InternalStatus.CODE_KEY, status);
if (status.getDescription() != null) {
trailers.put(InternalStatus.MESSAGE_KEY, status.getDescription());
}
}
@Override
public final void cancel(Status status) {
abstractServerStreamSink().cancel(status);
}
@Override
public final boolean isReady() {
return super.isReady();
}
@Override
public final void setDecompressor(Decompressor decompressor) {
transportState().setDecompressor(Preconditions.checkNotNull(decompressor, "decompressor"));
}
@Override public Attributes getAttributes() {
return Attributes.EMPTY;
}
@Override
public String getAuthority() {
return null;
}
@Override
public final void setListener(ServerStreamListener serverStreamListener) {
transportState().setListener(serverStreamListener);
}
@Override
public StatsTraceContext statsTraceContext() {
return statsTraceCtx;
}
/**
* This should only be called from the transport thread (except for private interactions with
* {@code AbstractServerStream}).
*/
protected abstract static class TransportState extends AbstractStream.TransportState {
/** Whether listener.closed() has been called. */
private boolean listenerClosed;
private ServerStreamListener listener;
private final StatsTraceContext statsTraceCtx;
private boolean endOfStream = false;
private boolean deframerClosed = false;
private boolean immediateCloseRequested = false;
private Runnable deframerClosedTask;
/** The status that the application used to close this stream. */
@Nullable
private Status closedStatus;
protected TransportState(
int maxMessageSize,
StatsTraceContext statsTraceCtx,
TransportTracer transportTracer) {
super(
maxMessageSize,
statsTraceCtx,
Preconditions.checkNotNull(transportTracer, "transportTracer"));
this.statsTraceCtx = Preconditions.checkNotNull(statsTraceCtx, "statsTraceCtx");
}
/**
* Sets the listener to receive notifications. Must be called in the context of the transport
* thread.
*/
public final void setListener(ServerStreamListener listener) {
Preconditions.checkState(this.listener == null, "setListener should be called only once");
this.listener = Preconditions.checkNotNull(listener, "listener");
}
@Override
public final void onStreamAllocated() {
super.onStreamAllocated();
getTransportTracer().reportRemoteStreamStarted();
}
@Override
public void deframerClosed(boolean hasPartialMessage) {
deframerClosed = true;
if (endOfStream && !immediateCloseRequested) {
if (hasPartialMessage) {
// We've received the entire stream and have data available but we don't have
// enough to read the next frame ... this is bad.
deframeFailed(
Status.INTERNAL
.withDescription("Encountered end-of-stream mid-frame")
.asRuntimeException());
deframerClosedTask = null;
return;
}
listener.halfClosed();
}
if (deframerClosedTask != null) {
deframerClosedTask.run();
deframerClosedTask = null;
}
}
@Override
protected ServerStreamListener listener() {
return listener;
}
/**
* Called in the transport thread to process the content of an inbound DATA frame from the
* client.
*
* @param frame the inbound HTTP/2 DATA frame. If this buffer is not used immediately, it must
* be retained.
* @param endOfStream {@code true} if no more data will be received on the stream.
*/
public void inboundDataReceived(ReadableBuffer frame, boolean endOfStream) {
Preconditions.checkState(!this.endOfStream, "Past end of stream");
// Deframe the message. If a failure occurs, deframeFailed will be called.
deframe(frame);
if (endOfStream) {
this.endOfStream = true;
closeDeframer(false);
}
}
/**
* Notifies failure to the listener of the stream. The transport is responsible for notifying
* the client of the failure independent of this method.
*
* <p>Unlike {@link #close(Status, Metadata)}, this method is only called from the
* transport. The transport should use this method instead of {@code close(Status)} for internal
* errors to prevent exposing unexpected states and exceptions to the application.
*
* @param status the error status. Must not be {@link Status#OK}.
*/
public final void transportReportStatus(final Status status) {
Preconditions.checkArgument(!status.isOk(), "status must not be OK");
if (deframerClosed) {
deframerClosedTask = null;
closeListener(status);
} else {
deframerClosedTask =
new Runnable() {
@Override
public void run() {
closeListener(status);
}
};
immediateCloseRequested = true;
closeDeframer(true);
}
}
/**
* Indicates the stream is considered completely closed and there is no further opportunity for
* error. It calls the listener's {@code closed()} if it was not already done by {@link
* #transportReportStatus}.
*/
public void complete() {
if (deframerClosed) {
deframerClosedTask = null;
closeListener(Status.OK);
} else {
deframerClosedTask =
new Runnable() {
@Override
public void run() {
closeListener(Status.OK);
}
};
immediateCloseRequested = true;
closeDeframer(true);
}
}
/**
* Closes the listener if not previously closed and frees resources. {@code newStatus} is a
* status generated by gRPC. It is <b>not</b> the status the stream closed with.
*/
private void closeListener(Status newStatus) {
// If newStatus is OK, the application must have already called AbstractServerStream.close()
// and the status passed in there was the actual status of the RPC.
// If newStatus non-OK, then the RPC ended some other way and the server application did
// not initiate the termination.
Preconditions.checkState(!newStatus.isOk() || closedStatus != null);
if (!listenerClosed) {
if (!newStatus.isOk()) {
statsTraceCtx.streamClosed(newStatus);
getTransportTracer().reportStreamClosed(false);
} else {
statsTraceCtx.streamClosed(closedStatus);
getTransportTracer().reportStreamClosed(closedStatus.isOk());
}
listenerClosed = true;
onStreamDeallocated();
listener().closed(newStatus);
}
}
/**
* Stores the {@code Status} that the application used to close this stream.
*/
private void setClosedStatus(Status closeStatus) {
Preconditions.checkState(closedStatus == null, "closedStatus can only be set once");
closedStatus = closeStatus;
}
}
}