-
Notifications
You must be signed in to change notification settings - Fork 174
/
GrpcUtils.java
320 lines (284 loc) · 14.4 KB
/
GrpcUtils.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
/*
* Copyright © 2019 Apple Inc. and the ServiceTalk project 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.servicetalk.grpc.api;
import io.servicetalk.buffer.api.Buffer;
import io.servicetalk.buffer.api.BufferAllocator;
import io.servicetalk.concurrent.api.Publisher;
import io.servicetalk.http.api.HttpDeserializer;
import io.servicetalk.http.api.HttpHeaders;
import io.servicetalk.http.api.HttpMetaData;
import io.servicetalk.http.api.HttpRequestMetaData;
import io.servicetalk.http.api.HttpResponse;
import io.servicetalk.http.api.HttpResponseFactory;
import io.servicetalk.http.api.HttpResponseMetaData;
import io.servicetalk.http.api.HttpSerializer;
import io.servicetalk.http.api.StatelessTrailersTransformer;
import io.servicetalk.http.api.StreamingHttpResponse;
import io.servicetalk.http.api.StreamingHttpResponseFactory;
import io.servicetalk.serialization.api.SerializationException;
import com.google.protobuf.InvalidProtocolBufferException;
import com.google.rpc.Status;
import java.util.Base64;
import java.util.function.Supplier;
import javax.annotation.Nullable;
import static io.servicetalk.grpc.api.GrpcMessageEncoding.None;
import static io.servicetalk.http.api.CharSequences.contentEqualsIgnoreCase;
import static io.servicetalk.http.api.CharSequences.newAsciiString;
import static io.servicetalk.http.api.HttpHeaderNames.CONTENT_TYPE;
import static io.servicetalk.http.api.HttpHeaderNames.SERVER;
import static io.servicetalk.http.api.HttpHeaderNames.TE;
import static io.servicetalk.http.api.HttpHeaderNames.USER_AGENT;
import static io.servicetalk.http.api.HttpHeaderValues.TRAILERS;
import static io.servicetalk.http.api.HttpRequestMethod.POST;
import static java.lang.String.valueOf;
final class GrpcUtils {
private static final CharSequence GRPC_CONTENT_TYPE = newAsciiString("application/grpc");
private static final CharSequence GRPC_STATUS_CODE_TRAILER = newAsciiString("grpc-status");
private static final CharSequence GRPC_STATUS_DETAILS_TRAILER = newAsciiString("grpc-status-details-bin");
private static final CharSequence GRPC_STATUS_MESSAGE_TRAILER = newAsciiString("grpc-message");
// TODO (nkant): add project version
private static final CharSequence GRPC_USER_AGENT = newAsciiString("grpc-service-talk/");
private static final CharSequence IDENTITY = newAsciiString(None.encoding());
private static final CharSequence GRPC_MESSAGE_ENCODING_KEY = newAsciiString("grpc-encoding");
private static final GrpcStatus STATUS_OK = GrpcStatus.fromCodeValue(GrpcStatusCode.OK.value());
private GrpcUtils() {
// No instances.
}
static void initRequest(final HttpRequestMetaData request) {
assert request.method() == POST;
final HttpHeaders headers = request.headers();
headers.set(USER_AGENT, GRPC_USER_AGENT);
headers.set(TE, TRAILERS);
headers.set(CONTENT_TYPE, GRPC_CONTENT_TYPE);
}
static <T> StreamingHttpResponse newResponse(final StreamingHttpResponseFactory responseFactory,
@Nullable final Publisher<T> payload,
@Nullable final HttpSerializer<T> serializer,
final BufferAllocator allocator) {
final StreamingHttpResponse response = responseFactory.ok();
initResponse(response);
if (payload != null) {
assert serializer != null;
response.payloadBody(payload, serializer);
}
return response.transformRaw(new GrpcStatusUpdater(allocator, STATUS_OK));
}
static HttpResponse newResponse(final HttpResponseFactory responseFactory, final BufferAllocator allocator) {
final HttpResponse response = responseFactory.ok();
initResponse(response);
setStatus(response.trailers(), STATUS_OK, null, allocator);
return response;
}
static HttpResponse newErrorResponse(final HttpResponseFactory responseFactory, final Throwable cause,
final BufferAllocator allocator) {
HttpResponse response = newResponse(responseFactory, allocator);
setStatus(response.trailers(), cause, allocator);
return response;
}
static StreamingHttpResponse newErrorResponse(final StreamingHttpResponseFactory responseFactory,
final Throwable cause, final BufferAllocator allocator) {
StreamingHttpResponse response = newResponse(responseFactory, null, null, allocator);
response.transformRaw(new ErrorUpdater(cause, allocator));
return response;
}
static void setStatus(final HttpHeaders trailers, final GrpcStatus status, final @Nullable Status details,
final BufferAllocator allocator) {
trailers.set(GRPC_STATUS_CODE_TRAILER, valueOf(status.code().value()));
if (status.description() != null) {
trailers.set(GRPC_STATUS_MESSAGE_TRAILER, status.description());
}
if (details != null) {
trailers.set(GRPC_STATUS_DETAILS_TRAILER,
newAsciiString(allocator.wrap(Base64.getEncoder().encode(details.toByteArray()))));
}
}
static void setStatus(final HttpHeaders trailers, final Throwable cause, final BufferAllocator allocator) {
if (cause instanceof GrpcStatusException) {
GrpcStatusException grpcStatusException = (GrpcStatusException) cause;
GrpcUtils.setStatus(trailers, grpcStatusException.status(), grpcStatusException.applicationStatus(),
allocator);
} else {
GrpcUtils.setStatus(trailers, GrpcStatus.fromCodeValue(GrpcStatusCode.UNKNOWN.value()), null,
allocator);
}
}
static <Resp> Publisher<Resp> validateResponseAndGetPayload(final StreamingHttpResponse response,
final HttpDeserializer<Resp> deserializer) {
// In case of server error, gRPC may return only one HEADER frame with endStream=true. Our
// HTTP1-based implementation translates them into response headers so we need to look for
// the status in both headers and trailers. Since this is streaming response and we have the headers now, we
// check for error here first. If we see trailers later in payloadBodyAndTrailers(), we will check for error
// there.
final HttpHeaders respHeaders = response.headers();
GrpcStatusException grpcStatusException = extractGrpcExceptionFromHeaders(respHeaders);
if (grpcStatusException != null) {
return Publisher.failed(grpcStatusException);
}
return deserializer.deserialize(respHeaders, response.payloadBodyAndTrailers().map(o -> {
if (o instanceof HttpHeaders) {
// We have already checked for error in headers above, now we just check in trailers.
GrpcStatusException ex = extractGrpcExceptionFromHeaders((HttpHeaders) o);
if (ex != null) {
throw ex;
}
} else if (!(o instanceof Buffer)) {
throw new IllegalArgumentException("Unexpected payload type: " + o.getClass());
}
return o;
}).filter(o -> !(o instanceof HttpHeaders)).map(o -> (Buffer) o));
}
static <Resp> Resp validateResponseAndGetPayload(final HttpResponse response,
final HttpDeserializer<Resp> deserializer) {
final HttpHeaders trailers = response.trailers();
final HttpHeaders headers = response.headers();
// In case of server error, gRPC may return only one HEADER frame with endStream=true. Our
// HTTP1-based implementation translates them into response headers so we need to look for
// the status in both headers and trailers.
// We will try the trailers first as this is the most likely place to find the GRPC related headers.
GrpcStatusException grpcStatusException = extractGrpcExceptionFromHeaders(trailers);
if (grpcStatusException != null) {
throw grpcStatusException;
}
// There was no grpc-status in the trailers, so error may be in the headers.
grpcStatusException = extractGrpcExceptionFromHeaders(headers);
if (grpcStatusException != null) {
throw grpcStatusException;
}
return response.payloadBody(deserializer);
}
static GrpcMessageEncoding readGrpcMessageEncoding(final HttpMetaData httpMetaData) {
final CharSequence encoding = httpMetaData.headers().get(GRPC_MESSAGE_ENCODING_KEY);
// identity is a special header for no compression
if (encoding != null && !contentEqualsIgnoreCase(encoding, IDENTITY)) {
final String lowercaseEncoding = encoding.toString().toLowerCase();
throw new SerializationException("Compression " + lowercaseEncoding + " not supported");
} else {
return None;
}
}
private static void initResponse(final HttpResponseMetaData response) {
// The response status is 200 no matter what. Actual status is put in trailers.
final HttpHeaders headers = response.headers();
headers.set(SERVER, GRPC_USER_AGENT);
headers.set(CONTENT_TYPE, GRPC_CONTENT_TYPE);
}
@Nullable
private static GrpcStatusException extractGrpcExceptionFromHeaders(final HttpHeaders headers) {
final CharSequence statusCode = headers.get(GRPC_STATUS_CODE_TRAILER);
if (statusCode != null) {
final GrpcStatusCode grpcStatusCode = GrpcStatusCode.fromCodeValue(statusCode);
if (grpcStatusCode.value() != GrpcStatusCode.OK.value()) {
final GrpcStatus grpcStatus =
new GrpcStatus(grpcStatusCode, null, headers.get(GRPC_STATUS_MESSAGE_TRAILER));
return grpcStatus.asException(new StatusSupplier(headers, grpcStatus));
}
}
return null;
}
@Nullable
private static Status getStatusDetails(final HttpHeaders headers) {
final CharSequence details = headers.get(GRPC_STATUS_DETAILS_TRAILER);
if (details == null) {
return null;
}
try {
return Status.parser().parseFrom(Base64.getDecoder().decode(details.toString()));
} catch (InvalidProtocolBufferException e) {
throw new IllegalStateException("Could not decode grpc status details", e);
}
}
@SuppressWarnings("unchecked")
static <T> T uncheckedCast(Object o) {
return (T) o;
}
/**
* Supplies the {@link Status} by parsing (and caching) it in a lazy fashion once requested the
* first time.
*/
private static final class StatusSupplier implements Supplier<Status> {
private final HttpHeaders headers;
private final GrpcStatus fallbackStatus;
@Nullable
private volatile StatusHolder statusHolder;
StatusSupplier(HttpHeaders headers, final GrpcStatus fallbackStatus) {
this.headers = headers;
this.fallbackStatus = fallbackStatus;
}
@Nullable
@Override
public Status get() {
StatusHolder statusHolder = this.statusHolder;
if (statusHolder == null) {
// Cache the status (we don't bother caching any errors tho). Also its fine to only use a volatile here
// as at worse this will just update to the "same" status again.
final Status statusFromHeaders = getStatusDetails(headers);
if (statusFromHeaders == null) {
final Status.Builder builder = Status.newBuilder().setCode(fallbackStatus.code().value());
if (fallbackStatus.description() != null) {
builder.setMessage(fallbackStatus.description());
}
this.statusHolder = statusHolder = new StatusHolder(builder.build());
} else {
this.statusHolder = statusHolder = new StatusHolder(statusFromHeaders);
}
}
return statusHolder.status;
}
/**
* This class differentiates between the {@code null} application status if none is present to avoid
* continuously retrying to parse status when it isn't present.
*/
private static final class StatusHolder {
@Nullable
final Status status;
StatusHolder(@Nullable Status status) {
this.status = status;
}
}
}
static final class GrpcStatusUpdater extends StatelessTrailersTransformer<Object> {
private final BufferAllocator allocator;
private final GrpcStatus successStatus;
GrpcStatusUpdater(final BufferAllocator allocator, final GrpcStatus successStatus) {
this.allocator = allocator;
this.successStatus = successStatus;
}
@Override
protected HttpHeaders payloadComplete(final HttpHeaders trailers) {
GrpcUtils.setStatus(trailers, successStatus, null, allocator);
return trailers;
}
@Override
protected HttpHeaders payloadFailed(final Throwable cause, final HttpHeaders trailers) {
setStatus(trailers, cause, allocator);
// Swallow exception as we are converting it to the trailers.
return trailers;
}
}
private static final class ErrorUpdater extends StatelessTrailersTransformer<Object> {
private final Throwable cause;
private final BufferAllocator allocator;
ErrorUpdater(final Throwable cause, final BufferAllocator allocator) {
this.cause = cause;
this.allocator = allocator;
}
@Override
protected HttpHeaders payloadComplete(final HttpHeaders trailers) {
setStatus(trailers, cause, allocator);
return trailers;
}
}
}