From 46f46a3395b76f6f2a668c7aaa0f19a7ee060a4f Mon Sep 17 00:00:00 2001 From: Igor Bernstein Date: Thu, 27 Feb 2020 18:09:58 -0500 Subject: [PATCH 1/2] feat: allow client authors to create their own callable chains --- .../api/gax/grpc/GrpcCallableFactory.java | 31 ++--- .../api/gax/grpc/GrpcRawCallableFactory.java | 120 ++++++++++++++++++ 2 files changed, 128 insertions(+), 23 deletions(-) create mode 100644 gax-grpc/src/main/java/com/google/api/gax/grpc/GrpcRawCallableFactory.java diff --git a/gax-grpc/src/main/java/com/google/api/gax/grpc/GrpcCallableFactory.java b/gax-grpc/src/main/java/com/google/api/gax/grpc/GrpcCallableFactory.java index 5b45c9bff..e94842b27 100644 --- a/gax-grpc/src/main/java/com/google/api/gax/grpc/GrpcCallableFactory.java +++ b/gax-grpc/src/main/java/com/google/api/gax/grpc/GrpcCallableFactory.java @@ -84,12 +84,8 @@ public static UnaryCallable createBas UnaryCallSettings callSettings, ClientContext clientContext) { UnaryCallable callable = - new GrpcDirectCallable<>(grpcCallSettings.getMethodDescriptor()); - if (grpcCallSettings.getParamsExtractor() != null) { - callable = - new GrpcUnaryRequestParamCallable<>(callable, grpcCallSettings.getParamsExtractor()); - } - callable = new GrpcExceptionCallable<>(callable, callSettings.getRetryableCodes()); + GrpcRawCallableFactory.createUnaryCallable( + grpcCallSettings, callSettings.getRetryableCodes()); callable = Callables.retrying(callable, callSettings, clientContext); @@ -234,10 +230,8 @@ BidiStreamingCallable createBidiStreamingCallable( StreamingCallSettings streamingCallSettings, ClientContext clientContext) { BidiStreamingCallable callable = - new GrpcDirectBidiStreamingCallable<>(grpcCallSettings.getMethodDescriptor()); - - callable = - new GrpcExceptionBidiStreamingCallable<>(callable, ImmutableSet.of()); + GrpcRawCallableFactory.createBidiStreamingCallable( + grpcCallSettings, ImmutableSet.of()); callable = new TracedBidiCallable<>( @@ -290,15 +284,8 @@ ServerStreamingCallable createServerStreamingCallable( ServerStreamingCallSettings streamingCallSettings, ClientContext clientContext) { ServerStreamingCallable callable = - new GrpcDirectServerStreamingCallable<>(grpcCallSettings.getMethodDescriptor()); - if (grpcCallSettings.getParamsExtractor() != null) { - callable = - new GrpcServerStreamingRequestParamCallable<>( - callable, grpcCallSettings.getParamsExtractor()); - } - callable = - new GrpcExceptionServerStreamingCallable<>( - callable, streamingCallSettings.getRetryableCodes()); + GrpcRawCallableFactory.createServerStreamingCallable( + grpcCallSettings, streamingCallSettings.getRetryableCodes()); if (clientContext.getStreamWatchdog() != null) { callable = Callables.watched(callable, streamingCallSettings, clientContext); @@ -332,10 +319,8 @@ ClientStreamingCallable createClientStreamingCallable( StreamingCallSettings streamingCallSettings, ClientContext clientContext) { ClientStreamingCallable callable = - new GrpcDirectClientStreamingCallable<>(grpcCallSettings.getMethodDescriptor()); - - callable = - new GrpcExceptionClientStreamingCallable<>(callable, ImmutableSet.of()); + GrpcRawCallableFactory.createClientStreamingCallable( + grpcCallSettings, ImmutableSet.of()); callable = new TracedClientStreamingCallable<>( diff --git a/gax-grpc/src/main/java/com/google/api/gax/grpc/GrpcRawCallableFactory.java b/gax-grpc/src/main/java/com/google/api/gax/grpc/GrpcRawCallableFactory.java new file mode 100644 index 000000000..a3dbbbe3f --- /dev/null +++ b/gax-grpc/src/main/java/com/google/api/gax/grpc/GrpcRawCallableFactory.java @@ -0,0 +1,120 @@ +/* + * 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.rpc.BidiStreamingCallable; +import com.google.api.gax.rpc.ClientStreamingCallable; +import com.google.api.gax.rpc.ServerStreamingCallable; +import com.google.api.gax.rpc.StatusCode; +import com.google.api.gax.rpc.UnaryCallable; +import java.util.Set; + +/** Class with utility methods to create low level grpc-based direct callables. */ +@InternalApi("For internal use by google-cloud-java clients only") +public class GrpcRawCallableFactory { + private GrpcRawCallableFactory() {} + + /** + * Create a Unary callable object with minimal grpc-specific functionality. + * + * @param grpcCallSettings the gRPC call settings + * @param retryableCodes the {@link StatusCode.Code} that should be marked as retryable + */ + public static UnaryCallable createUnaryCallable( + GrpcCallSettings grpcCallSettings, Set retryableCodes) { + UnaryCallable callable = + new GrpcDirectCallable<>(grpcCallSettings.getMethodDescriptor()); + if (grpcCallSettings.getParamsExtractor() != null) { + callable = + new GrpcUnaryRequestParamCallable<>(callable, grpcCallSettings.getParamsExtractor()); + } + return new GrpcExceptionCallable<>(callable, retryableCodes); + } + + /** + * Create a bidirectional streaming callable object with grpc-specific functionality. Designed for + * use by generated code. + * + * @param grpcCallSettings the gRPC call settings + * @param retryableCodes the {@link StatusCode.Code} that should be marked as retryable + * @return {@link BidiStreamingCallable} callable object. + */ + @BetaApi("The surface for streaming is not stable yet and may change in the future.") + public static + BidiStreamingCallable createBidiStreamingCallable( + GrpcCallSettings grpcCallSettings, + Set retryableCodes) { + BidiStreamingCallable callable = + new GrpcDirectBidiStreamingCallable<>(grpcCallSettings.getMethodDescriptor()); + + return new GrpcExceptionBidiStreamingCallable<>(callable, retryableCodes); + } + + /** + * Create a server-streaming callable with grpc-specific functionality. Designed for use by + * generated code. + * + * @param grpcCallSettings the gRPC call settings + * @param retryableCodes the {@link StatusCode.Code} that should be marked as retryable + */ + @BetaApi("The surface for streaming is not stable yet and may change in the future.") + public static + ServerStreamingCallable createServerStreamingCallable( + GrpcCallSettings grpcCallSettings, + Set retryableCodes) { + ServerStreamingCallable callable = + new GrpcDirectServerStreamingCallable<>(grpcCallSettings.getMethodDescriptor()); + if (grpcCallSettings.getParamsExtractor() != null) { + callable = + new GrpcServerStreamingRequestParamCallable<>( + callable, grpcCallSettings.getParamsExtractor()); + } + return new GrpcExceptionServerStreamingCallable<>(callable, retryableCodes); + } + /** + * Create a client-streaming callable object with grpc-specific functionality. Designed for use by + * generated code. + * + * @param grpcCallSettings the gRPC call settings + * @param retryableCodes the {@link StatusCode.Code} that should be marked as retryable + */ + @BetaApi("The surface for streaming is not stable yet and may change in the future.") + public static + ClientStreamingCallable createClientStreamingCallable( + GrpcCallSettings grpcCallSettings, + Set retryableCodes) { + ClientStreamingCallable callable = + new GrpcDirectClientStreamingCallable<>(grpcCallSettings.getMethodDescriptor()); + + return new GrpcExceptionClientStreamingCallable<>(callable, retryableCodes); + } +} From d44f50fa28b6a40cd806523158d2f3183fb19097 Mon Sep 17 00:00:00 2001 From: Igor Bernstein Date: Thu, 5 Mar 2020 16:30:42 -0500 Subject: [PATCH 2/2] fix year --- .../java/com/google/api/gax/grpc/GrpcRawCallableFactory.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gax-grpc/src/main/java/com/google/api/gax/grpc/GrpcRawCallableFactory.java b/gax-grpc/src/main/java/com/google/api/gax/grpc/GrpcRawCallableFactory.java index a3dbbbe3f..248760cde 100644 --- a/gax-grpc/src/main/java/com/google/api/gax/grpc/GrpcRawCallableFactory.java +++ b/gax-grpc/src/main/java/com/google/api/gax/grpc/GrpcRawCallableFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2017 Google LLC + * Copyright 2020 Google LLC * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are