forked from googleapis/gax-java
/
GrpcCallableFactory.java
341 lines (305 loc) · 15.7 KB
/
GrpcCallableFactory.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
/*
* Copyright 2017 Google LLC
*
* 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 LLC 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 com.google.api.gax.grpc;
import com.google.api.core.BetaApi;
import com.google.api.core.InternalApi;
import com.google.api.gax.longrunning.OperationSnapshot;
import com.google.api.gax.rpc.BatchingCallSettings;
import com.google.api.gax.rpc.BidiStreamingCallable;
import com.google.api.gax.rpc.Callables;
import com.google.api.gax.rpc.ClientContext;
import com.google.api.gax.rpc.ClientStreamingCallable;
import com.google.api.gax.rpc.LongRunningClient;
import com.google.api.gax.rpc.OperationCallSettings;
import com.google.api.gax.rpc.OperationCallable;
import com.google.api.gax.rpc.PagedCallSettings;
import com.google.api.gax.rpc.ServerStreamingCallSettings;
import com.google.api.gax.rpc.ServerStreamingCallable;
import com.google.api.gax.rpc.StatusCode;
import com.google.api.gax.rpc.StreamingCallSettings;
import com.google.api.gax.rpc.UnaryCallSettings;
import com.google.api.gax.rpc.UnaryCallable;
import com.google.api.gax.tracing.SpanName;
import com.google.api.gax.tracing.TracedBatchingCallable;
import com.google.api.gax.tracing.TracedBidiCallable;
import com.google.api.gax.tracing.TracedClientStreamingCallable;
import com.google.api.gax.tracing.TracedOperationCallable;
import com.google.api.gax.tracing.TracedOperationInitialCallable;
import com.google.api.gax.tracing.TracedServerStreamingCallable;
import com.google.api.gax.tracing.TracedUnaryCallable;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableSet;
import com.google.longrunning.Operation;
import com.google.longrunning.stub.OperationsStub;
import io.grpc.MethodDescriptor;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.Nonnull;
/** Class with utility methods to create grpc-based direct callables. */
@BetaApi("The surface for use by generated code is not stable yet and may change in the future.")
public class GrpcCallableFactory {
// Used to extract service and method name from a grpc MethodDescriptor.
private static final Pattern FULL_METHOD_NAME_REGEX = Pattern.compile("^.*?([^./]+)/([^./]+)$");
private GrpcCallableFactory() {}
/**
* Create a Unary callable object with minimal grpc-specific functionality.
*
* @param grpcCallSettings the gRPC call settings
* @param callSettings the Unary call settings
* @param clientContext {@link ClientContext} to use to connect to the service.
*/
public static <RequestT, ResponseT> UnaryCallable<RequestT, ResponseT> createBaseUnaryCallable(
GrpcCallSettings<RequestT, ResponseT> grpcCallSettings,
UnaryCallSettings<?, ?> callSettings,
ClientContext clientContext) {
UnaryCallable<RequestT, ResponseT> callable =
GrpcRawCallableFactory.createUnaryCallable(
grpcCallSettings, callSettings.getRetryableCodes());
callable = Callables.retrying(callable, callSettings, clientContext);
return callable;
}
/**
* Create a callable object with grpc-specific functionality. Designed for use by generated code.
*
* @param grpcCallSettings the gRPC call settings
*/
public static <RequestT, ResponseT> UnaryCallable<RequestT, ResponseT> createUnaryCallable(
GrpcCallSettings<RequestT, ResponseT> grpcCallSettings,
UnaryCallSettings<RequestT, ResponseT> callSettings,
ClientContext clientContext) {
UnaryCallable<RequestT, ResponseT> callable =
createBaseUnaryCallable(grpcCallSettings, callSettings, clientContext);
callable =
new TracedUnaryCallable<>(
callable,
clientContext.getTracerFactory(),
getSpanName(grpcCallSettings.getMethodDescriptor()));
return callable.withDefaultCallContext(clientContext.getDefaultCallContext());
}
/**
* Create a paged callable object that represents a paged API method. Designed for use by
* generated code.
*
* @param grpcCallSettings the gRPC call settings
* @param pagedCallSettings {@link PagedCallSettings} to configure the paged settings with.
* @param clientContext {@link ClientContext} to use to connect to the service.
* @return {@link UnaryCallable} callable object.
*/
public static <RequestT, ResponseT, PagedListResponseT>
UnaryCallable<RequestT, PagedListResponseT> createPagedCallable(
GrpcCallSettings<RequestT, ResponseT> grpcCallSettings,
PagedCallSettings<RequestT, ResponseT, PagedListResponseT> pagedCallSettings,
ClientContext clientContext) {
UnaryCallable<RequestT, ResponseT> innerCallable =
createBaseUnaryCallable(grpcCallSettings, pagedCallSettings, clientContext);
UnaryCallable<RequestT, PagedListResponseT> pagedCallable =
Callables.paged(innerCallable, pagedCallSettings);
return pagedCallable.withDefaultCallContext(clientContext.getDefaultCallContext());
}
/**
* Create a callable object that represents a batching API method. Designed for use by generated
* code.
*
* @param grpcCallSettings the gRPC call settings
* @param batchingCallSettings {@link BatchingCallSettings} to configure the batching related
* settings with.
* @param clientContext {@link ClientContext} to use to connect to the service.
* @return {@link UnaryCallable} callable object.
*/
@BetaApi("The surface for batching is not stable yet and may change in the future.")
public static <RequestT, ResponseT> UnaryCallable<RequestT, ResponseT> createBatchingCallable(
GrpcCallSettings<RequestT, ResponseT> grpcCallSettings,
BatchingCallSettings<RequestT, ResponseT> batchingCallSettings,
ClientContext clientContext) {
UnaryCallable<RequestT, ResponseT> baseCallable =
createBaseUnaryCallable(grpcCallSettings, batchingCallSettings, clientContext);
// NOTE: Since batching happens asynchronously and the outermost callable simply buffers the
// request. Tracing will only start on the inner callable that accepts the batch.
UnaryCallable<RequestT, ResponseT> tracedCallable =
new TracedBatchingCallable<>(
baseCallable,
clientContext.getTracerFactory(),
getSpanName(grpcCallSettings.getMethodDescriptor()),
batchingCallSettings.getBatchingDescriptor());
UnaryCallable<RequestT, ResponseT> batchingCallable =
Callables.batching(tracedCallable, batchingCallSettings, clientContext);
return batchingCallable.withDefaultCallContext(clientContext.getDefaultCallContext());
}
/**
* Creates a callable object that represents a long-running operation. Designed for use by
* generated code.
*
* @param grpcCallSettings the gRPC call settings
* @param operationCallSettings {@link OperationCallSettings} to configure the method-level
* settings with.
* @param clientContext {@link ClientContext} to use to connect to the service.
* @param operationsStub {@link OperationsStub} to use to poll for updates on the Operation.
* @return {@link com.google.api.gax.rpc.OperationCallable} callable object.
*/
@BetaApi(
"The surface for long-running operations is not stable yet and may change in the future.")
public static <RequestT, ResponseT, MetadataT>
OperationCallable<RequestT, ResponseT, MetadataT> createOperationCallable(
GrpcCallSettings<RequestT, Operation> grpcCallSettings,
OperationCallSettings<RequestT, ResponseT, MetadataT> operationCallSettings,
ClientContext clientContext,
OperationsStub operationsStub) {
SpanName initialSpanName = getSpanName(grpcCallSettings.getMethodDescriptor());
SpanName operationSpanName =
SpanName.of(initialSpanName.getClientName(), initialSpanName.getMethodName() + "Operation");
UnaryCallable<RequestT, Operation> initialGrpcCallable =
createBaseUnaryCallable(
grpcCallSettings, operationCallSettings.getInitialCallSettings(), clientContext);
UnaryCallable<RequestT, OperationSnapshot> initialCallable =
new GrpcOperationSnapshotCallable<>(initialGrpcCallable);
// Create a sub-trace for the initial RPC that starts the operation.
UnaryCallable<RequestT, OperationSnapshot> tracedInitialCallable =
new TracedOperationInitialCallable<>(
initialCallable, clientContext.getTracerFactory(), initialSpanName);
LongRunningClient longRunningClient = new GrpcLongRunningClient(operationsStub);
OperationCallable<RequestT, ResponseT, MetadataT> operationCallable =
Callables.longRunningOperation(
tracedInitialCallable, operationCallSettings, clientContext, longRunningClient);
OperationCallable<RequestT, ResponseT, MetadataT> tracedOperationCallable =
new TracedOperationCallable<>(
operationCallable, clientContext.getTracerFactory(), operationSpanName);
return tracedOperationCallable.withDefaultCallContext(clientContext.getDefaultCallContext());
}
/**
* Create a bidirectional streaming callable object with grpc-specific functionality. Designed for
* use by generated code.
*
* @param grpcCallSettings the gRPC call settings
* @param streamingCallSettings {@link StreamingCallSettings} to configure the method-level
* settings with.
* @param clientContext {@link ClientContext} to use to connect to the service.
* @return {@link BidiStreamingCallable} callable object.
*/
@BetaApi("The surface for streaming is not stable yet and may change in the future.")
public static <RequestT, ResponseT>
BidiStreamingCallable<RequestT, ResponseT> createBidiStreamingCallable(
GrpcCallSettings<RequestT, ResponseT> grpcCallSettings,
StreamingCallSettings<RequestT, ResponseT> streamingCallSettings,
ClientContext clientContext) {
BidiStreamingCallable<RequestT, ResponseT> callable =
GrpcRawCallableFactory.createBidiStreamingCallable(
grpcCallSettings, ImmutableSet.<StatusCode.Code>of());
callable =
new TracedBidiCallable<>(
callable,
clientContext.getTracerFactory(),
getSpanName(grpcCallSettings.getMethodDescriptor()));
return callable.withDefaultCallContext(clientContext.getDefaultCallContext());
}
/**
* Create a server-streaming callable with grpc-specific functionality. Designed for use by
* generated code.
*
* @param grpcCallSettings the gRPC call settings
* @param streamingCallSettings {@link StreamingCallSettings} to configure the method-level
* settings with.
* @param clientContext {@link ClientContext} to use to connect to the service.
* @deprecated Please use ServerStreamingCallSettings
*/
@Deprecated
@BetaApi("The surface for streaming is not stable yet and may change in the future.")
public static <RequestT, ResponseT>
ServerStreamingCallable<RequestT, ResponseT> createServerStreamingCallable(
GrpcCallSettings<RequestT, ResponseT> grpcCallSettings,
StreamingCallSettings<RequestT, ResponseT> streamingCallSettings,
ClientContext clientContext) {
// up convert to new settings
ServerStreamingCallSettings<RequestT, ResponseT> serverStreamingCallSettings =
ServerStreamingCallSettings.<RequestT, ResponseT>newBuilder().build();
return createServerStreamingCallable(
grpcCallSettings, serverStreamingCallSettings, clientContext);
}
/**
* Create a server-streaming callable with grpc-specific functionality. Designed for use by
* generated code.
*
* @param grpcCallSettings the gRPC call settings
* @param streamingCallSettings {@link StreamingCallSettings} to configure the method-level
* settings with.
* @param clientContext {@link ClientContext} to use to connect to the service.
*/
@BetaApi("The surface for streaming is not stable yet and may change in the future.")
public static <RequestT, ResponseT>
ServerStreamingCallable<RequestT, ResponseT> createServerStreamingCallable(
GrpcCallSettings<RequestT, ResponseT> grpcCallSettings,
ServerStreamingCallSettings<RequestT, ResponseT> streamingCallSettings,
ClientContext clientContext) {
ServerStreamingCallable<RequestT, ResponseT> callable =
GrpcRawCallableFactory.createServerStreamingCallable(
grpcCallSettings, streamingCallSettings.getRetryableCodes());
if (clientContext.getStreamWatchdog() != null) {
callable = Callables.watched(callable, streamingCallSettings, clientContext);
}
callable = Callables.retrying(callable, streamingCallSettings, clientContext);
callable =
new TracedServerStreamingCallable<>(
callable,
clientContext.getTracerFactory(),
getSpanName(grpcCallSettings.getMethodDescriptor()));
return callable.withDefaultCallContext(clientContext.getDefaultCallContext());
}
/**
* Create a client-streaming callable object with grpc-specific functionality. Designed for use by
* generated code.
*
* @param grpcCallSettings the gRPC call settings
* @param streamingCallSettings {@link StreamingCallSettings} to configure the method-level
* settings with.
* @param clientContext {@link ClientContext} to use to connect to the service.
* @return {@link ClientStreamingCallable} callable object.
*/
@BetaApi("The surface for streaming is not stable yet and may change in the future.")
public static <RequestT, ResponseT>
ClientStreamingCallable<RequestT, ResponseT> createClientStreamingCallable(
GrpcCallSettings<RequestT, ResponseT> grpcCallSettings,
StreamingCallSettings<RequestT, ResponseT> streamingCallSettings,
ClientContext clientContext) {
ClientStreamingCallable<RequestT, ResponseT> callable =
GrpcRawCallableFactory.createClientStreamingCallable(
grpcCallSettings, ImmutableSet.<StatusCode.Code>of());
callable =
new TracedClientStreamingCallable<>(
callable,
clientContext.getTracerFactory(),
getSpanName(grpcCallSettings.getMethodDescriptor()));
return callable.withDefaultCallContext(clientContext.getDefaultCallContext());
}
@InternalApi("Visible for testing")
static SpanName getSpanName(@Nonnull MethodDescriptor<?, ?> methodDescriptor) {
Matcher matcher = FULL_METHOD_NAME_REGEX.matcher(methodDescriptor.getFullMethodName());
Preconditions.checkArgument(matcher.matches(), "Invalid fullMethodName");
return SpanName.of(matcher.group(1), matcher.group(2));
}
}