From 8d748e2721b28e2d9f9a0466dae52bb86101cb8b Mon Sep 17 00:00:00 2001 From: Michael Darakananda Date: Mon, 8 May 2017 15:38:11 +1000 Subject: [PATCH 1/7] Move CredentialProvider up to ClientSettings Updates GoogleCloudPlatform/google-cloud-java#2034. This commit should resolve the first 3 bullets in the linked issue. The generated code needs to be updated to take advantage of this added functionality. --- .../com/google/api/gax/grpc/AuthCallable.java | 52 +++++++++++++++ .../google/api/gax/grpc/ClientSettings.java | 31 ++++++++- .../grpc/InstantiatingChannelProvider.java | 4 ++ .../google/api/gax/grpc/UnaryCallable.java | 5 ++ .../google/api/gax/grpc/AuthCallableTest.java | 65 +++++++++++++++++++ 5 files changed, 156 insertions(+), 1 deletion(-) create mode 100644 gax-grpc/src/main/java/com/google/api/gax/grpc/AuthCallable.java create mode 100644 gax-grpc/src/test/java/com/google/api/gax/grpc/AuthCallableTest.java diff --git a/gax-grpc/src/main/java/com/google/api/gax/grpc/AuthCallable.java b/gax-grpc/src/main/java/com/google/api/gax/grpc/AuthCallable.java new file mode 100644 index 000000000..4edeed3f1 --- /dev/null +++ b/gax-grpc/src/main/java/com/google/api/gax/grpc/AuthCallable.java @@ -0,0 +1,52 @@ +/* + * Copyright 2016, 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 com.google.api.gax.grpc; + +import com.google.api.core.ApiFuture; +import com.google.common.base.Preconditions; +import io.grpc.CallCredentials; + +class AuthCallable implements FutureCallable { + private final FutureCallable callable; + private final CallCredentials credentials; + + AuthCallable(FutureCallable callable, CallCredentials credentials) { + this.callable = Preconditions.checkNotNull(callable); + this.credentials = Preconditions.checkNotNull(credentials); + } + + @Override + public ApiFuture futureCall(RequestT request, CallContext context) { + if (context.getCallOptions().getCredentials() == null) { + context = context.withCallOptions(context.getCallOptions().withCallCredentials(credentials)); + } + return callable.futureCall(request, context); + } +} diff --git a/gax-grpc/src/main/java/com/google/api/gax/grpc/ClientSettings.java b/gax-grpc/src/main/java/com/google/api/gax/grpc/ClientSettings.java index fc1d3a5f6..0d716b3de 100644 --- a/gax-grpc/src/main/java/com/google/api/gax/grpc/ClientSettings.java +++ b/gax-grpc/src/main/java/com/google/api/gax/grpc/ClientSettings.java @@ -30,10 +30,12 @@ package com.google.api.gax.grpc; import com.google.api.core.BetaApi; +import com.google.api.gax.core.CredentialsProvider; import com.google.api.gax.retrying.RetrySettings; import io.grpc.Status; import java.io.IOException; import java.util.Set; +import javax.annotation.Nullable; /** * A base settings class to configure a service API class. @@ -65,11 +67,21 @@ public abstract class ClientSettings { private final ExecutorProvider executorProvider; private final ChannelProvider channelProvider; + @Nullable private final CredentialsProvider credentialsProvider; - /** Constructs an instance of ClientSettings. */ + @Deprecated protected ClientSettings(ExecutorProvider executorProvider, ChannelProvider channelProvider) { + this(executorProvider, channelProvider, null); + } + + /** Constructs an instance of ClientSettings. */ + protected ClientSettings( + ExecutorProvider executorProvider, + ChannelProvider channelProvider, + CredentialsProvider credentialsProvider) { this.executorProvider = executorProvider; this.channelProvider = channelProvider; + this.credentialsProvider = credentialsProvider; } /** Gets a channel and an executor for making calls. */ @@ -85,10 +97,16 @@ public final ChannelProvider getChannelProvider() { return channelProvider; } + @Nullable + public final CredentialsProvider getCredentialsProvider() { + return credentialsProvider; + } + public abstract static class Builder { private ExecutorProvider executorProvider; private ChannelProvider channelProvider; + private CredentialsProvider credentialsProvider; /** Create a builder from a ClientSettings object. */ protected Builder(ClientSettings settings) { @@ -118,6 +136,12 @@ public Builder setChannelProvider(ChannelProvider channelProvider) { return this; } + /** Sets the CredentialsProvider to use for getting the channel to make calls with. */ + public Builder setCredentialsProvider(CredentialsProvider credentialsProvider) { + this.credentialsProvider = credentialsProvider; + return this; + } + /** Gets the ExecutorProvider that was previously set on this Builder. */ public ExecutorProvider getExecutorProvider() { return executorProvider; @@ -128,6 +152,11 @@ public ChannelProvider getChannelProvider() { return channelProvider; } + /** Gets the CredentialsProvider that was previously set on this Builder. */ + public CredentialsProvider getCredentialsProvider() { + return credentialsProvider; + } + /** Performs a merge, using only non-null fields */ protected Builder applyToAllUnaryMethods( Iterable methodSettingsBuilders, diff --git a/gax-grpc/src/main/java/com/google/api/gax/grpc/InstantiatingChannelProvider.java b/gax-grpc/src/main/java/com/google/api/gax/grpc/InstantiatingChannelProvider.java index 45e8d210f..09fedace9 100644 --- a/gax-grpc/src/main/java/com/google/api/gax/grpc/InstantiatingChannelProvider.java +++ b/gax-grpc/src/main/java/com/google/api/gax/grpc/InstantiatingChannelProvider.java @@ -139,6 +139,7 @@ private ManagedChannel createChannel(Executor executor) throws IOException { * Gets the credentials which will be used to call the service. If the credentials have not been * acquired yet, then they will be acquired when this function is called. */ + @Deprecated public Credentials getCredentials() throws IOException { return getCredentialsProvider().getCredentials(); } @@ -147,6 +148,7 @@ public Credentials getCredentials() throws IOException { * The credentials to use in order to call the service. Credentials will not be acquired until * they are required. */ + @Deprecated public CredentialsProvider getCredentialsProvider() { return credentialsProvider; } @@ -261,12 +263,14 @@ public Builder setExecutorProvider(ExecutorProvider executorProvider) { * Sets the CredentialsProvider which will acquire the credentials for making calls to the * service. Credentials will not be acquired until they are required. */ + @Deprecated public Builder setCredentialsProvider(CredentialsProvider credentialsProvider) { this.credentialsProvider = credentialsProvider; return this; } /** The previously set CredentialsProvider. */ + @Deprecated public CredentialsProvider getCredentialsProvider() { return credentialsProvider; } diff --git a/gax-grpc/src/main/java/com/google/api/gax/grpc/UnaryCallable.java b/gax-grpc/src/main/java/com/google/api/gax/grpc/UnaryCallable.java index df3e79c59..bff19ab93 100644 --- a/gax-grpc/src/main/java/com/google/api/gax/grpc/UnaryCallable.java +++ b/gax-grpc/src/main/java/com/google/api/gax/grpc/UnaryCallable.java @@ -38,6 +38,7 @@ import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableSet; +import io.grpc.CallCredentials; import io.grpc.Channel; import io.grpc.Status; import java.util.concurrent.ScheduledExecutorService; @@ -328,4 +329,8 @@ UnaryCallable batching( return new UnaryCallable<>( new BatchingCallable<>(callable, batchingDescriptor, batcherFactory), channel, settings); } + + UnaryCallable auth(CallCredentials credentials) { + return new UnaryCallable<>(new AuthCallable<>(callable, credentials)); + } } diff --git a/gax-grpc/src/test/java/com/google/api/gax/grpc/AuthCallableTest.java b/gax-grpc/src/test/java/com/google/api/gax/grpc/AuthCallableTest.java new file mode 100644 index 000000000..1e17af876 --- /dev/null +++ b/gax-grpc/src/test/java/com/google/api/gax/grpc/AuthCallableTest.java @@ -0,0 +1,65 @@ +/* + * Copyright 2016, 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 com.google.api.gax.grpc; + +import com.google.api.core.ApiFuture; +import com.google.api.core.ApiFutures; +import com.google.common.truth.Truth; +import io.grpc.CallCredentials; +import java.util.concurrent.CancellationException; +import java.util.concurrent.ExecutionException; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; +import org.mockito.Mockito; + +@RunWith(JUnit4.class) +public class AuthCallableTest { + private static class StashCallable implements FutureCallable { + CallCredentials lastCredentials; + + @Override + public ApiFuture futureCall(Integer request, CallContext context) { + lastCredentials = context.getCallOptions().getCredentials(); + return ApiFutures.immediateFuture(42); + } + } + + @Test + public void testAuth() throws InterruptedException, ExecutionException, CancellationException { + StashCallable stash = new StashCallable(); + Truth.assertThat(UnaryCallable.create(stash).futureCall(0).get()).isEqualTo(42); + Truth.assertThat(stash.lastCredentials).isNull(); + + CallCredentials cred = Mockito.mock(CallCredentials.class); + Truth.assertThat(UnaryCallable.create(stash).auth(cred).futureCall(0).get()).isEqualTo(42); + Truth.assertThat(stash.lastCredentials).isEqualTo(cred); + } +} From 591056d978bb93d12b47ef3be5964207aeaed3ae Mon Sep 17 00:00:00 2001 From: Michael Darakananda Date: Tue, 9 May 2017 09:44:55 +1000 Subject: [PATCH 2/7] pr comment --- .../com/google/api/gax/grpc/AuthCallable.java | 8 +++- .../google/api/gax/grpc/UnaryCallable.java | 10 +++-- .../google/api/gax/grpc/AuthCallableTest.java | 16 +++++-- .../api/gax/core/NoCredentialsProvider.java | 43 +++++++++++++++++++ 4 files changed, 68 insertions(+), 9 deletions(-) create mode 100644 gax/src/main/java/com/google/api/gax/core/NoCredentialsProvider.java diff --git a/gax-grpc/src/main/java/com/google/api/gax/grpc/AuthCallable.java b/gax-grpc/src/main/java/com/google/api/gax/grpc/AuthCallable.java index 4edeed3f1..15d859916 100644 --- a/gax-grpc/src/main/java/com/google/api/gax/grpc/AuthCallable.java +++ b/gax-grpc/src/main/java/com/google/api/gax/grpc/AuthCallable.java @@ -1,5 +1,5 @@ /* - * Copyright 2016, Google Inc. All rights reserved. + * Copyright 2017, 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 @@ -32,7 +32,11 @@ import com.google.api.core.ApiFuture; import com.google.common.base.Preconditions; import io.grpc.CallCredentials; - +/** + * Implements the credential insertion for {@link UnaryCallable}. + * + *

Package-private for internal use. + */ class AuthCallable implements FutureCallable { private final FutureCallable callable; private final CallCredentials credentials; diff --git a/gax-grpc/src/main/java/com/google/api/gax/grpc/UnaryCallable.java b/gax-grpc/src/main/java/com/google/api/gax/grpc/UnaryCallable.java index bff19ab93..141b60d03 100644 --- a/gax-grpc/src/main/java/com/google/api/gax/grpc/UnaryCallable.java +++ b/gax-grpc/src/main/java/com/google/api/gax/grpc/UnaryCallable.java @@ -35,12 +35,13 @@ import com.google.api.core.NanoClock; import com.google.api.gax.batching.BatchingSettings; import com.google.api.gax.retrying.RetrySettings; +import com.google.auth.Credentials; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableSet; -import io.grpc.CallCredentials; import io.grpc.Channel; import io.grpc.Status; +import io.grpc.auth.MoreCallCredentials; import java.util.concurrent.ScheduledExecutorService; import javax.annotation.Nullable; @@ -330,7 +331,10 @@ UnaryCallable batching( new BatchingCallable<>(callable, batchingDescriptor, batcherFactory), channel, settings); } - UnaryCallable auth(CallCredentials credentials) { - return new UnaryCallable<>(new AuthCallable<>(callable, credentials)); + UnaryCallable withAuth(Credentials credentials) { + if (credentials == null) { + return this; + } + return new UnaryCallable<>(new AuthCallable<>(callable, MoreCallCredentials.from(credentials))); } } diff --git a/gax-grpc/src/test/java/com/google/api/gax/grpc/AuthCallableTest.java b/gax-grpc/src/test/java/com/google/api/gax/grpc/AuthCallableTest.java index 1e17af876..c8f9d684e 100644 --- a/gax-grpc/src/test/java/com/google/api/gax/grpc/AuthCallableTest.java +++ b/gax-grpc/src/test/java/com/google/api/gax/grpc/AuthCallableTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2016, Google Inc. All rights reserved. + * Copyright 2017, 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 @@ -31,6 +31,8 @@ import com.google.api.core.ApiFuture; import com.google.api.core.ApiFutures; +import com.google.api.gax.core.NoCredentialsProvider; +import com.google.auth.Credentials; import com.google.common.truth.Truth; import io.grpc.CallCredentials; import java.util.concurrent.CancellationException; @@ -58,8 +60,14 @@ public void testAuth() throws InterruptedException, ExecutionException, Cancella Truth.assertThat(UnaryCallable.create(stash).futureCall(0).get()).isEqualTo(42); Truth.assertThat(stash.lastCredentials).isNull(); - CallCredentials cred = Mockito.mock(CallCredentials.class); - Truth.assertThat(UnaryCallable.create(stash).auth(cred).futureCall(0).get()).isEqualTo(42); - Truth.assertThat(stash.lastCredentials).isEqualTo(cred); + Credentials cred = Mockito.mock(Credentials.class); + Truth.assertThat(UnaryCallable.create(stash).withAuth(cred).futureCall(0).get()).isEqualTo(42); + Truth.assertThat(stash.lastCredentials).isNotNull(); + + NoCredentialsProvider provider = new NoCredentialsProvider(); + Truth.assertThat( + UnaryCallable.create(stash).withAuth(provider.getCredentials()).futureCall(0).get()) + .isEqualTo(42); + Truth.assertThat(stash.lastCredentials).isNull(); } } diff --git a/gax/src/main/java/com/google/api/gax/core/NoCredentialsProvider.java b/gax/src/main/java/com/google/api/gax/core/NoCredentialsProvider.java new file mode 100644 index 000000000..149490a11 --- /dev/null +++ b/gax/src/main/java/com/google/api/gax/core/NoCredentialsProvider.java @@ -0,0 +1,43 @@ +/* + * Copyright 2017, 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 com.google.api.gax.core; + +import com.google.api.core.BetaApi; +import com.google.auth.Credentials; + +/** NoCredentialsProvider is a CredentialsProvider which always returns null. */ +@BetaApi +public final class NoCredentialsProvider implements CredentialsProvider { + + @Override + public Credentials getCredentials() { + return null; + } +} From 6341a2c3471c20fe62fdac52ada5edc2daea008a Mon Sep 17 00:00:00 2001 From: Michael Darakananda Date: Tue, 9 May 2017 16:41:59 +1000 Subject: [PATCH 3/7] thread through config Major changes are: - ClientContext is added to pull together channel, executor, and credentials, which are often needed together. This change will also simplify future work if more ClientSettings are added. - The above change requires a corresponding change in toolkit. OperationsClient is updated using the new toolkit. --- .../api/gax/grpc/BatchingCallSettings.java | 9 +- .../api/gax/grpc/ClientInitContext.java | 73 +++++++++++++++++ .../google/api/gax/grpc/ClientSettings.java | 20 +++++ .../api/gax/grpc/OperationCallSettings.java | 10 +-- .../api/gax/grpc/OperationCallable.java | 43 ++++++---- .../api/gax/grpc/PagedCallSettings.java | 11 +-- .../api/gax/grpc/SimpleCallSettings.java | 6 +- .../api/gax/grpc/UnaryCallSettingsTyped.java | 11 ++- .../google/api/gax/grpc/UnaryCallable.java | 82 +++++++++++++++++-- .../google/longrunning/OperationsClient.java | 37 ++++++--- .../longrunning/OperationsSettings.java | 19 +++-- .../longrunning/PagedResponseWrappers.java | 4 +- .../com/google/longrunning/package-info.java | 2 +- .../google/api/gax/grpc/AuthCallableTest.java | 12 +-- .../api/gax/grpc/OperationCallableTest.java | 28 ++++++- .../com/google/api/gax/grpc/SettingsTest.java | 16 +++- 16 files changed, 296 insertions(+), 87 deletions(-) create mode 100644 gax-grpc/src/main/java/com/google/api/gax/grpc/ClientInitContext.java diff --git a/gax-grpc/src/main/java/com/google/api/gax/grpc/BatchingCallSettings.java b/gax-grpc/src/main/java/com/google/api/gax/grpc/BatchingCallSettings.java index b413f1bf8..ff25d8410 100644 --- a/gax-grpc/src/main/java/com/google/api/gax/grpc/BatchingCallSettings.java +++ b/gax-grpc/src/main/java/com/google/api/gax/grpc/BatchingCallSettings.java @@ -34,11 +34,9 @@ import com.google.api.gax.retrying.RetrySettings; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableSet; -import io.grpc.Channel; import io.grpc.MethodDescriptor; import io.grpc.Status; import java.util.Set; -import java.util.concurrent.ScheduledExecutorService; /** * A settings class to configure a UnaryCallable for calls to an API method that supports batching. @@ -52,9 +50,10 @@ public final class BatchingCallSettings private BatcherFactory batcherFactory; /** Package-private, for use by UnaryCallable. */ - UnaryCallable create(Channel channel, ScheduledExecutorService executor) { - UnaryCallable baseCallable = createBaseCallable(channel, executor); - batcherFactory = new BatcherFactory<>(batchingDescriptor, batchingSettings, executor); + UnaryCallable create(ClientInitContext context) { + UnaryCallable baseCallable = createBaseCallable(context); + batcherFactory = + new BatcherFactory<>(batchingDescriptor, batchingSettings, context.getExecutor()); return baseCallable.batching(batchingDescriptor, batcherFactory); } diff --git a/gax-grpc/src/main/java/com/google/api/gax/grpc/ClientInitContext.java b/gax-grpc/src/main/java/com/google/api/gax/grpc/ClientInitContext.java new file mode 100644 index 000000000..b333c0196 --- /dev/null +++ b/gax-grpc/src/main/java/com/google/api/gax/grpc/ClientInitContext.java @@ -0,0 +1,73 @@ +/* + * Copyright 2016, 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 com.google.api.gax.grpc; + +import com.google.auto.value.AutoValue; +import io.grpc.CallCredentials; +import io.grpc.Channel; +import java.util.concurrent.ScheduledExecutorService; +import javax.annotation.Nullable; + +/** + * Encapsulates data used to initialize a client. + * + *

Unlike {@link ClientSettings} which allows users to configure the client, {@code + * ClientInitContext} gathers members to simplify initialization later on. + * + *

It it intended to be used in generated code. Most users will not need to use it. + */ +@AutoValue +public abstract class ClientInitContext { + public abstract Channel getChannel(); + + public abstract ScheduledExecutorService getExecutor(); + + @Nullable + public abstract CallCredentials getCallCredentials(); + + public Builder toBuilder() { + return new AutoValue_ClientInitContext.Builder(this); + } + + public static Builder newBuilder() { + return new AutoValue_ClientInitContext.Builder(); + } + + @AutoValue.Builder + public abstract static class Builder { + public abstract Builder setChannel(Channel value); + + public abstract Builder setExecutor(ScheduledExecutorService value); + + public abstract Builder setCallCredentials(CallCredentials value); + + public abstract ClientInitContext build(); + } +} diff --git a/gax-grpc/src/main/java/com/google/api/gax/grpc/ClientSettings.java b/gax-grpc/src/main/java/com/google/api/gax/grpc/ClientSettings.java index 0d716b3de..2e9f4e666 100644 --- a/gax-grpc/src/main/java/com/google/api/gax/grpc/ClientSettings.java +++ b/gax-grpc/src/main/java/com/google/api/gax/grpc/ClientSettings.java @@ -31,7 +31,9 @@ import com.google.api.core.BetaApi; import com.google.api.gax.core.CredentialsProvider; +import com.google.api.gax.core.NoCredentialsProvider; import com.google.api.gax.retrying.RetrySettings; +import com.google.common.base.MoreObjects; import io.grpc.Status; import java.io.IOException; import java.util.Set; @@ -102,6 +104,14 @@ public final CredentialsProvider getCredentialsProvider() { return credentialsProvider; } + public String toString() { + return MoreObjects.toStringHelper(this) + .add("executorProvider", executorProvider) + .add("channelProvider", channelProvider) + .add("credentialsProvider", credentialsProvider) + .toString(); + } + public abstract static class Builder { private ExecutorProvider executorProvider; @@ -112,11 +122,13 @@ public abstract static class Builder { protected Builder(ClientSettings settings) { this.executorProvider = settings.executorProvider; this.channelProvider = settings.channelProvider; + this.credentialsProvider = settings.credentialsProvider; } protected Builder(InstantiatingChannelProvider channelProvider) { this.executorProvider = InstantiatingExecutorProvider.newBuilder().build(); this.channelProvider = channelProvider; + this.credentialsProvider = new NoCredentialsProvider(); } /** @@ -177,5 +189,13 @@ protected Builder applyToAllUnaryMethods( } public abstract ClientSettings build() throws IOException; + + public String toString() { + return MoreObjects.toStringHelper(this) + .add("executorProvider", executorProvider) + .add("channelProvider", channelProvider) + .add("credentialsProvider", credentialsProvider) + .toString(); + } } } diff --git a/gax-grpc/src/main/java/com/google/api/gax/grpc/OperationCallSettings.java b/gax-grpc/src/main/java/com/google/api/gax/grpc/OperationCallSettings.java index 4ea687d82..03fa13eb8 100644 --- a/gax-grpc/src/main/java/com/google/api/gax/grpc/OperationCallSettings.java +++ b/gax-grpc/src/main/java/com/google/api/gax/grpc/OperationCallSettings.java @@ -33,9 +33,7 @@ import com.google.longrunning.Operation; import com.google.longrunning.OperationsClient; import com.google.protobuf.Message; -import io.grpc.Channel; import io.grpc.MethodDescriptor; -import java.util.concurrent.ScheduledExecutorService; import org.threeten.bp.Duration; /** @@ -59,12 +57,10 @@ public final Duration getPollingInterval() { // package-private for internal use. OperationCallable createOperationCallable( - Channel channel, ScheduledExecutorService executor, OperationsClient operationsClient) { - UnaryCallable initialCallable = - initialCallSettings.create(channel, executor); + ClientInitContext context, OperationsClient operationsClient) { + UnaryCallable initialCallable = initialCallSettings.create(context); OperationCallable operationCallable = - new OperationCallable<>( - initialCallable, channel, executor, operationsClient, responseClass, this); + new OperationCallable<>(initialCallable, context, operationsClient, responseClass, this); return operationCallable; } diff --git a/gax-grpc/src/main/java/com/google/api/gax/grpc/OperationCallable.java b/gax-grpc/src/main/java/com/google/api/gax/grpc/OperationCallable.java index 5c0d5a26e..b89a0313f 100644 --- a/gax-grpc/src/main/java/com/google/api/gax/grpc/OperationCallable.java +++ b/gax-grpc/src/main/java/com/google/api/gax/grpc/OperationCallable.java @@ -48,8 +48,7 @@ @BetaApi public final class OperationCallable { private final UnaryCallable initialCallable; - private final Channel channel; - private final ScheduledExecutorService executor; + private final ClientInitContext clientContext; private final OperationsClient operationsClient; private final Class responseClass; private final OperationCallSettings settings; @@ -57,14 +56,12 @@ public final class OperationCallable { /** Package-private for internal use. */ OperationCallable( UnaryCallable initialCallable, - Channel channel, - ScheduledExecutorService executor, + ClientInitContext clientContext, OperationsClient operationsClient, Class responseClass, OperationCallSettings settings) { this.initialCallable = Preconditions.checkNotNull(initialCallable); - this.channel = channel; - this.executor = executor; + this.clientContext = clientContext; this.operationsClient = operationsClient; this.responseClass = responseClass; this.settings = settings; @@ -78,7 +75,11 @@ public final class OperationCallable { */ OperationCallable bind(Channel boundChannel) { return new OperationCallable<>( - initialCallable, boundChannel, executor, operationsClient, responseClass, settings); + initialCallable, + clientContext.toBuilder().setChannel(boundChannel).build(), + operationsClient, + responseClass, + settings); } /** @@ -87,17 +88,26 @@ OperationCallable bind(Channel boundChannel) { * * @param operationCallSettings {@link com.google.api.gax.grpc.OperationCallSettings} to configure * the method-level settings with. - * @param channel {@link Channel} to use to connect to the service. - * @param executor {@link ScheduledExecutorService} to use to schedule polling work. + * @param clientContext {@link ClientInitContext} to use to connect to the service. * @param operationsClient {@link OperationsClient} to use to poll for updates on the Operation. * @return {@link com.google.api.gax.grpc.OperationCallable} callable object. */ + public static OperationCallable create( + OperationCallSettings operationCallSettings, + ClientInitContext clientContext, + OperationsClient operationsClient) { + return operationCallSettings.createOperationCallable(clientContext, operationsClient); + } + + @Deprecated public static OperationCallable create( OperationCallSettings operationCallSettings, Channel channel, ScheduledExecutorService executor, OperationsClient operationsClient) { - return operationCallSettings.createOperationCallable(channel, executor, operationsClient); + return operationCallSettings.createOperationCallable( + ClientInitContext.newBuilder().setChannel(channel).setExecutor(executor).build(), + operationsClient); } /** @@ -111,14 +121,18 @@ public static OperationCallable futureCall(RequestT request, CallContext context) { if (context.getChannel() == null) { - context = context.withChannel(channel); + context = context.withChannel(clientContext.getChannel()); } ApiFuture initialCallFuture = initialCallable.futureCall(request, context); Duration pollingInterval = settings != null ? settings.getPollingInterval() : OperationFuture.DEFAULT_POLLING_INTERVAL; OperationFuture operationFuture = OperationFuture.create( - operationsClient, initialCallFuture, executor, responseClass, pollingInterval); + operationsClient, + initialCallFuture, + clientContext.getExecutor(), + responseClass, + pollingInterval); return operationFuture; } @@ -130,7 +144,7 @@ public OperationFuture futureCall(RequestT request, CallContext conte * @return {@link OperationFuture} for the call result */ public OperationFuture futureCall(RequestT request) { - return futureCall(request, CallContext.createDefault().withChannel(channel)); + return futureCall(request, CallContext.createDefault().withChannel(clientContext.getChannel())); } /** @@ -175,7 +189,8 @@ public OperationFuture resumeFutureCall(String operationName) { .getOperationCallable() .futureCall(GetOperationRequest.newBuilder().setName(operationName).build()); OperationFuture operationFuture = - OperationFuture.create(operationsClient, getOperationFuture, executor, responseClass); + OperationFuture.create( + operationsClient, getOperationFuture, clientContext.getExecutor(), responseClass); return operationFuture; } } diff --git a/gax-grpc/src/main/java/com/google/api/gax/grpc/PagedCallSettings.java b/gax-grpc/src/main/java/com/google/api/gax/grpc/PagedCallSettings.java index 0fbdc701f..da26afe47 100644 --- a/gax-grpc/src/main/java/com/google/api/gax/grpc/PagedCallSettings.java +++ b/gax-grpc/src/main/java/com/google/api/gax/grpc/PagedCallSettings.java @@ -32,11 +32,9 @@ import com.google.api.core.BetaApi; import com.google.api.gax.retrying.RetrySettings; import com.google.common.collect.ImmutableSet; -import io.grpc.Channel; import io.grpc.MethodDescriptor; import io.grpc.Status; import java.util.Set; -import java.util.concurrent.ScheduledExecutorService; /** * A settings class to configure a UnaryCallable for calls to an API method that supports page @@ -49,14 +47,13 @@ public final class PagedCallSettings pagedListResponseFactory; /** Package-private, for use by UnaryCallable. */ - UnaryCallable create(Channel channel, ScheduledExecutorService executor) { - return createBaseCallable(channel, executor); + UnaryCallable create(ClientInitContext context) { + return createBaseCallable(context); } /** Package-private, for use by UnaryCallable. */ - UnaryCallable createPagedVariant( - Channel channel, ScheduledExecutorService executor) { - UnaryCallable baseCallable = createBaseCallable(channel, executor); + UnaryCallable createPagedVariant(ClientInitContext context) { + UnaryCallable baseCallable = createBaseCallable(context); return baseCallable.paged(pagedListResponseFactory); } diff --git a/gax-grpc/src/main/java/com/google/api/gax/grpc/SimpleCallSettings.java b/gax-grpc/src/main/java/com/google/api/gax/grpc/SimpleCallSettings.java index 02e50bfeb..0a96890af 100644 --- a/gax-grpc/src/main/java/com/google/api/gax/grpc/SimpleCallSettings.java +++ b/gax-grpc/src/main/java/com/google/api/gax/grpc/SimpleCallSettings.java @@ -32,11 +32,9 @@ import com.google.api.core.BetaApi; import com.google.api.gax.retrying.RetrySettings; import com.google.common.collect.ImmutableSet; -import io.grpc.Channel; import io.grpc.MethodDescriptor; import io.grpc.Status; import java.util.Set; -import java.util.concurrent.ScheduledExecutorService; /** * A settings class to configure a UnaryCallable for calls to a simple API method (i.e. that doesn't @@ -47,8 +45,8 @@ public final class SimpleCallSettings extends UnaryCallSettingsTyped { /** Package-private, for use by UnaryCallable. */ - UnaryCallable create(Channel channel, ScheduledExecutorService executor) { - return createBaseCallable(channel, executor); + UnaryCallable create(ClientInitContext context) { + return createBaseCallable(context); } private SimpleCallSettings( diff --git a/gax-grpc/src/main/java/com/google/api/gax/grpc/UnaryCallSettingsTyped.java b/gax-grpc/src/main/java/com/google/api/gax/grpc/UnaryCallSettingsTyped.java index 1a4799567..2388d22ed 100644 --- a/gax-grpc/src/main/java/com/google/api/gax/grpc/UnaryCallSettingsTyped.java +++ b/gax-grpc/src/main/java/com/google/api/gax/grpc/UnaryCallSettingsTyped.java @@ -32,10 +32,8 @@ import com.google.api.core.BetaApi; import com.google.api.gax.retrying.RetrySettings; import com.google.common.collect.ImmutableSet; -import io.grpc.Channel; import io.grpc.MethodDescriptor; import io.grpc.Status; -import java.util.concurrent.ScheduledExecutorService; /** * A settings class with generic typing configure a UnaryCallable. @@ -67,18 +65,19 @@ protected UnaryCallSettingsTyped( this.methodDescriptor = methodDescriptor; } - protected UnaryCallable createBaseCallable( - Channel channel, ScheduledExecutorService executor) { + protected UnaryCallable createBaseCallable(ClientInitContext context) { ClientCallFactory clientCallFactory = new DescriptorClientCallFactory<>(methodDescriptor); UnaryCallable callable = - new UnaryCallable<>(new DirectCallable<>(clientCallFactory), channel, this); + new UnaryCallable<>(new DirectCallable<>(clientCallFactory), context.getChannel(), this); if (getRetryableCodes() != null) { callable = callable.retryableOn(ImmutableSet.copyOf(getRetryableCodes())); } if (getRetrySettings() != null) { - callable = callable.retrying(getRetrySettings(), executor); + callable = callable.retrying(getRetrySettings(), context.getExecutor()); } + // withAuth works properly even if getCallCredentials is null. + callable = callable.withAuth(context.getCallCredentials()); return callable; } diff --git a/gax-grpc/src/main/java/com/google/api/gax/grpc/UnaryCallable.java b/gax-grpc/src/main/java/com/google/api/gax/grpc/UnaryCallable.java index 141b60d03..89649fe84 100644 --- a/gax-grpc/src/main/java/com/google/api/gax/grpc/UnaryCallable.java +++ b/gax-grpc/src/main/java/com/google/api/gax/grpc/UnaryCallable.java @@ -35,13 +35,12 @@ import com.google.api.core.NanoClock; import com.google.api.gax.batching.BatchingSettings; import com.google.api.gax.retrying.RetrySettings; -import com.google.auth.Credentials; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableSet; +import io.grpc.CallCredentials; import io.grpc.Channel; import io.grpc.Status; -import io.grpc.auth.MoreCallCredentials; import java.util.concurrent.ScheduledExecutorService; import javax.annotation.Nullable; @@ -97,6 +96,20 @@ public final class UnaryCallable { private final Channel channel; @Nullable private final UnaryCallSettings settings; + /** + * Create a callable object that represents a simple API method. Public only for technical reasons + * - for advanced usage + * + * @param simpleCallSettings {@link com.google.api.gax.grpc.SimpleCallSettings} to configure the + * method-level settings with. + * @param context {@link ClientInitContext} to use to connect to the service. + * @return {@link com.google.api.gax.grpc.UnaryCallable} callable object. + */ + public static UnaryCallable create( + SimpleCallSettings simpleCallSettings, ClientInitContext context) { + return simpleCallSettings.create(context); + } + /** * Create a callable object that represents a simple API method. Public only for technical reasons * - for advanced usage @@ -107,11 +120,29 @@ public final class UnaryCallable { * @param executor {@link ScheduledExecutorService} to use when connecting to the service. * @return {@link com.google.api.gax.grpc.UnaryCallable} callable object. */ + @Deprecated public static UnaryCallable create( SimpleCallSettings simpleCallSettings, Channel channel, ScheduledExecutorService executor) { - return simpleCallSettings.create(channel, executor); + return simpleCallSettings.create( + ClientInitContext.newBuilder().setChannel(channel).setExecutor(executor).build()); + } + + /** + * Create a paged callable object that represents a paged API method. Public only for technical + * reasons - for advanced usage + * + * @param PagedCallSettings {@link com.google.api.gax.grpc.PagedCallSettings} to configure the + * paged settings with. + * @param context {@link ClientInitContext} to use to connect to the service. + * @return {@link com.google.api.gax.grpc.UnaryCallable} callable object. + */ + public static + UnaryCallable createPagedVariant( + PagedCallSettings PagedCallSettings, + ClientInitContext context) { + return PagedCallSettings.createPagedVariant(context); } /** @@ -124,12 +155,29 @@ public static UnaryCallable create( * @param executor {@link ScheduledExecutorService} to use to when connecting to the service. * @return {@link com.google.api.gax.grpc.UnaryCallable} callable object. */ + @Deprecated public static UnaryCallable createPagedVariant( PagedCallSettings PagedCallSettings, Channel channel, ScheduledExecutorService executor) { - return PagedCallSettings.createPagedVariant(channel, executor); + return PagedCallSettings.createPagedVariant( + ClientInitContext.newBuilder().setChannel(channel).setExecutor(executor).build()); + } + + /** + * Create a base callable object that represents a paged API method. Public only for technical + * reasons - for advanced usage + * + * @param PagedCallSettings {@link com.google.api.gax.grpc.PagedCallSettings} to configure the + * paged settings with. + * @param context {@link ClientInitContext} to use to connect to the service. + * @return {@link com.google.api.gax.grpc.UnaryCallable} callable object. + */ + public static UnaryCallable create( + PagedCallSettings PagedCallSettings, + ClientInitContext context) { + return PagedCallSettings.create(context); } /** @@ -142,11 +190,27 @@ UnaryCallable createPagedVariant( * @param executor {@link ScheduledExecutorService} to use to when connecting to the service. * @return {@link com.google.api.gax.grpc.UnaryCallable} callable object. */ + @Deprecated public static UnaryCallable create( PagedCallSettings PagedCallSettings, Channel channel, ScheduledExecutorService executor) { - return PagedCallSettings.create(channel, executor); + return PagedCallSettings.create( + ClientInitContext.newBuilder().setChannel(channel).setExecutor(executor).build()); + } + + /** + * Create a callable object that represents a batching API method. Public only for technical + * reasons - for advanced usage + * + * @param batchingCallSettings {@link BatchingSettings} to configure the batching related settings + * with. + * @param context {@link ClientInitContext} to use to connect to the service. + * @return {@link com.google.api.gax.grpc.UnaryCallable} callable object. + */ + public static UnaryCallable create( + BatchingCallSettings batchingCallSettings, ClientInitContext context) { + return batchingCallSettings.create(context); } /** @@ -159,11 +223,13 @@ public static UnaryCallable UnaryCallable create( BatchingCallSettings batchingCallSettings, Channel channel, ScheduledExecutorService executor) { - return batchingCallSettings.create(channel, executor); + return batchingCallSettings.create( + ClientInitContext.newBuilder().setChannel(channel).setExecutor(executor).build()); } /** @@ -331,10 +397,10 @@ UnaryCallable batching( new BatchingCallable<>(callable, batchingDescriptor, batcherFactory), channel, settings); } - UnaryCallable withAuth(Credentials credentials) { + UnaryCallable withAuth(CallCredentials credentials) { if (credentials == null) { return this; } - return new UnaryCallable<>(new AuthCallable<>(callable, MoreCallCredentials.from(credentials))); + return new UnaryCallable<>(new AuthCallable<>(callable, credentials), channel, settings); } } diff --git a/gax-grpc/src/main/java/com/google/longrunning/OperationsClient.java b/gax-grpc/src/main/java/com/google/longrunning/OperationsClient.java index a5bd46890..95de0bdcd 100644 --- a/gax-grpc/src/main/java/com/google/longrunning/OperationsClient.java +++ b/gax-grpc/src/main/java/com/google/longrunning/OperationsClient.java @@ -31,11 +31,15 @@ import static com.google.longrunning.PagedResponseWrappers.ListOperationsPagedResponse; +import com.google.api.core.BetaApi; +import com.google.api.gax.core.CredentialsProvider; import com.google.api.gax.grpc.ChannelAndExecutor; +import com.google.api.gax.grpc.ClientInitContext; import com.google.api.gax.grpc.UnaryCallable; import com.google.protobuf.Empty; -import com.google.protobuf.ExperimentalApi; +import io.grpc.CallCredentials; import io.grpc.ManagedChannel; +import io.grpc.auth.MoreCallCredentials; import java.io.Closeable; import java.io.IOException; import java.util.ArrayList; @@ -73,13 +77,13 @@ * methods: * *

    - *
  1. A "flattened" method. With this type of method, the fields of the request type have been + *
  2. A "flattened" method. With this type of method, the fields of the request type have been * converted into function parameters. It may be the case that not all fields are available as * parameters, and not every API method will have a flattened method entry point. - *
  3. A "request object" method. This type of method only takes one parameter, a request object, + *
  4. A "request object" method. This type of method only takes one parameter, a request object, * which must be constructed before the call. Not every API method will have a request object * method. - *
  5. A "callable" method. This type of method takes no parameters and returns an immutable API + *
  6. A "callable" method. This type of method takes no parameters and returns an immutable API * callable object, which can be used to initiate calls to the service. *
* @@ -106,7 +110,7 @@ * */ @Generated("by GAPIC") -@ExperimentalApi +@BetaApi public class OperationsClient implements AutoCloseable { private final OperationsSettings settings; private final ScheduledExecutorService executor; @@ -138,17 +142,28 @@ protected OperationsClient(OperationsSettings settings) throws IOException { this.executor = channelAndExecutor.getExecutor(); this.channel = channelAndExecutor.getChannel(); + CredentialsProvider credentialsProvider = settings.getCredentialsProvider(); + CallCredentials callCredentials = null; + if (credentialsProvider.getCredentials() != null) { + callCredentials = MoreCallCredentials.from(credentialsProvider.getCredentials()); + } + ClientInitContext clientContext = + ClientInitContext.newBuilder() + .setExecutor(this.executor) + .setChannel(this.channel) + .setCallCredentials(callCredentials) + .build(); + this.getOperationCallable = - UnaryCallable.create(settings.getOperationSettings(), this.channel, this.executor); + UnaryCallable.create(settings.getOperationSettings(), clientContext); this.listOperationsCallable = - UnaryCallable.create(settings.listOperationsSettings(), this.channel, this.executor); + UnaryCallable.create(settings.listOperationsSettings(), clientContext); this.listOperationsPagedCallable = - UnaryCallable.createPagedVariant( - settings.listOperationsSettings(), this.channel, this.executor); + UnaryCallable.createPagedVariant(settings.listOperationsSettings(), clientContext); this.cancelOperationCallable = - UnaryCallable.create(settings.cancelOperationSettings(), this.channel, this.executor); + UnaryCallable.create(settings.cancelOperationSettings(), clientContext); this.deleteOperationCallable = - UnaryCallable.create(settings.deleteOperationSettings(), this.channel, this.executor); + UnaryCallable.create(settings.deleteOperationSettings(), clientContext); if (settings.getChannelProvider().shouldAutoClose()) { closeables.add( diff --git a/gax-grpc/src/main/java/com/google/longrunning/OperationsSettings.java b/gax-grpc/src/main/java/com/google/longrunning/OperationsSettings.java index 1993cf41d..a2233f4df 100644 --- a/gax-grpc/src/main/java/com/google/longrunning/OperationsSettings.java +++ b/gax-grpc/src/main/java/com/google/longrunning/OperationsSettings.java @@ -32,6 +32,8 @@ import static com.google.longrunning.PagedResponseWrappers.ListOperationsPagedResponse; import com.google.api.core.ApiFuture; +import com.google.api.core.BetaApi; +import com.google.api.gax.core.CredentialsProvider; import com.google.api.gax.core.GoogleCredentialsProvider; import com.google.api.gax.core.PropertiesProvider; import com.google.api.gax.grpc.CallContext; @@ -54,7 +56,6 @@ import com.google.common.collect.Lists; import com.google.common.collect.Sets; import com.google.protobuf.Empty; -import com.google.protobuf.ExperimentalApi; import io.grpc.Status; import java.io.IOException; import javax.annotation.Generated; @@ -63,7 +64,7 @@ // AUTO-GENERATED DOCUMENTATION AND CLASS /** Settings class to configure an instance of {@link OperationsClient}. */ @Generated("by GAPIC v0.0.5") -@ExperimentalApi +@BetaApi public class OperationsSettings extends ClientSettings { private static final String DEFAULT_GAPIC_NAME = "gapic"; @@ -145,8 +146,7 @@ public static GoogleCredentialsProvider.Builder defaultCredentialsProviderBuilde /** Returns a builder for the default ChannelProvider for this service. */ public static InstantiatingChannelProvider.Builder defaultChannelProviderBuilder() { return InstantiatingChannelProvider.newBuilder() - .setGeneratorHeader(DEFAULT_GAPIC_NAME, getGapicVersion()) - .setCredentialsProvider(defaultCredentialsProviderBuilder().build()); + .setGeneratorHeader(DEFAULT_GAPIC_NAME, getGapicVersion()); } private static String getGapicVersion() { @@ -175,7 +175,10 @@ public Builder toBuilder() { } private OperationsSettings(Builder settingsBuilder) throws IOException { - super(settingsBuilder.getExecutorProvider(), settingsBuilder.getChannelProvider()); + super( + settingsBuilder.getExecutorProvider(), + settingsBuilder.getChannelProvider(), + settingsBuilder.getCredentialsProvider()); getOperationSettings = settingsBuilder.getOperationSettings().build(); listOperationsSettings = settingsBuilder.listOperationsSettings().build(); @@ -353,6 +356,12 @@ public Builder setChannelProvider(ChannelProvider channelProvider) { return this; } + @Override + public Builder setCredentialsProvider(CredentialsProvider credentialsProvider) { + super.setCredentialsProvider(credentialsProvider); + return this; + } + /** * Applies the given settings to all of the unary API methods in this service. Only values that * are non-null will be applied, so this method is not capable of un-setting any values. diff --git a/gax-grpc/src/main/java/com/google/longrunning/PagedResponseWrappers.java b/gax-grpc/src/main/java/com/google/longrunning/PagedResponseWrappers.java index a887c4e03..2aee28385 100644 --- a/gax-grpc/src/main/java/com/google/longrunning/PagedResponseWrappers.java +++ b/gax-grpc/src/main/java/com/google/longrunning/PagedResponseWrappers.java @@ -32,11 +32,11 @@ import com.google.api.core.ApiFunction; import com.google.api.core.ApiFuture; import com.google.api.core.ApiFutures; +import com.google.api.core.BetaApi; import com.google.api.gax.grpc.AbstractFixedSizeCollection; import com.google.api.gax.grpc.AbstractPage; import com.google.api.gax.grpc.AbstractPagedListResponse; import com.google.api.gax.grpc.PageContext; -import com.google.protobuf.ExperimentalApi; import java.util.List; import javax.annotation.Generated; @@ -47,7 +47,7 @@ * page streaming pattern. */ @Generated("by GAPIC") -@ExperimentalApi +@BetaApi public class PagedResponseWrappers { public static class ListOperationsPagedResponse diff --git a/gax-grpc/src/main/java/com/google/longrunning/package-info.java b/gax-grpc/src/main/java/com/google/longrunning/package-info.java index ce09bf29b..76593c6ce 100644 --- a/gax-grpc/src/main/java/com/google/longrunning/package-info.java +++ b/gax-grpc/src/main/java/com/google/longrunning/package-info.java @@ -33,7 +33,7 @@ * *

The interfaces provided are listed below, along with usage samples. * - *

= =============== OperationsClient ================ + *

================ OperationsClient ================ * *

Service Description: Manages long-running operations with an API service. * diff --git a/gax-grpc/src/test/java/com/google/api/gax/grpc/AuthCallableTest.java b/gax-grpc/src/test/java/com/google/api/gax/grpc/AuthCallableTest.java index c8f9d684e..e7b4b6cfc 100644 --- a/gax-grpc/src/test/java/com/google/api/gax/grpc/AuthCallableTest.java +++ b/gax-grpc/src/test/java/com/google/api/gax/grpc/AuthCallableTest.java @@ -31,8 +31,6 @@ import com.google.api.core.ApiFuture; import com.google.api.core.ApiFutures; -import com.google.api.gax.core.NoCredentialsProvider; -import com.google.auth.Credentials; import com.google.common.truth.Truth; import io.grpc.CallCredentials; import java.util.concurrent.CancellationException; @@ -60,14 +58,8 @@ public void testAuth() throws InterruptedException, ExecutionException, Cancella Truth.assertThat(UnaryCallable.create(stash).futureCall(0).get()).isEqualTo(42); Truth.assertThat(stash.lastCredentials).isNull(); - Credentials cred = Mockito.mock(Credentials.class); + CallCredentials cred = Mockito.mock(CallCredentials.class); Truth.assertThat(UnaryCallable.create(stash).withAuth(cred).futureCall(0).get()).isEqualTo(42); - Truth.assertThat(stash.lastCredentials).isNotNull(); - - NoCredentialsProvider provider = new NoCredentialsProvider(); - Truth.assertThat( - UnaryCallable.create(stash).withAuth(provider.getCredentials()).futureCall(0).get()) - .isEqualTo(42); - Truth.assertThat(stash.lastCredentials).isNull(); + Truth.assertThat(stash.lastCredentials).isEqualTo(cred); } } diff --git a/gax-grpc/src/test/java/com/google/api/gax/grpc/OperationCallableTest.java b/gax-grpc/src/test/java/com/google/api/gax/grpc/OperationCallableTest.java index e88dfd929..b1e7a6d74 100644 --- a/gax-grpc/src/test/java/com/google/api/gax/grpc/OperationCallableTest.java +++ b/gax-grpc/src/test/java/com/google/api/gax/grpc/OperationCallableTest.java @@ -94,10 +94,16 @@ public void testCall() { OperationCallable callable = new OperationCallable( - stashUnaryCallable, null, executor, operationsClient, Color.class, null); + stashUnaryCallable, + ClientInitContext.newBuilder() + .setChannel(Mockito.mock(Channel.class)) + .setExecutor(executor) + .build(), + operationsClient, + Color.class, + null); Color response = callable.call(2, CallContext.createDefault()); Truth.assertThat(response).isEqualTo(injectedResponse); - Truth.assertThat(stash.context.getChannel()).isNull(); Truth.assertThat(stash.context.getCallOptions()).isEqualTo(CallOptions.DEFAULT); } @@ -117,7 +123,14 @@ public void testBind() { Channel channel = Mockito.mock(Channel.class); OperationCallable callable = new OperationCallable( - stashUnaryCallable, null, executor, operationsClient, Color.class, null); + stashUnaryCallable, + ClientInitContext.newBuilder() + .setChannel(Mockito.mock(Channel.class)) + .setExecutor(executor) + .build(), + operationsClient, + Color.class, + null); callable = callable.bind(channel); Color response = callable.call(2); Truth.assertThat(response).isEqualTo(injectedResponse); @@ -141,7 +154,14 @@ public void testResumeFutureCall() throws Exception { OperationCallable callable = new OperationCallable( - stashUnaryCallable, null, executor, operationsClient, Color.class, null); + stashUnaryCallable, + ClientInitContext.newBuilder() + .setChannel(Mockito.mock(Channel.class)) + .setExecutor(executor) + .build(), + operationsClient, + Color.class, + null); OperationFuture operationFuture = callable.futureCall(2); Color response = callable.resumeFutureCall(operationFuture.getOperationName()).get(); diff --git a/gax-grpc/src/test/java/com/google/api/gax/grpc/SettingsTest.java b/gax-grpc/src/test/java/com/google/api/gax/grpc/SettingsTest.java index 330d6fd9c..b2c87f8ad 100644 --- a/gax-grpc/src/test/java/com/google/api/gax/grpc/SettingsTest.java +++ b/gax-grpc/src/test/java/com/google/api/gax/grpc/SettingsTest.java @@ -157,7 +157,10 @@ public Builder toBuilder() { } private FakeSettings(Builder settingsBuilder) throws IOException { - super(settingsBuilder.getExecutorProvider(), settingsBuilder.getChannelProvider()); + super( + settingsBuilder.getExecutorProvider(), + settingsBuilder.getChannelProvider(), + settingsBuilder.getCredentialsProvider()); this.fakeMethodSimple = settingsBuilder.fakeMethodSimple().build(); this.fakePagedMethod = settingsBuilder.fakePagedMethod().build(); @@ -472,6 +475,9 @@ public void callSettingsTimeoutNoRetries() throws IOException { .setMaxRpcTimeout(timeout)); FakeSettings settingsB = builderB.build(); + System.err.println(builderA); + System.err.println(builderB); + assertIsReflectionEqual(builderA, builderB); assertIsReflectionEqual(settingsA, settingsB); } @@ -544,13 +550,15 @@ private static void assertIsReflectionEqual(FakeSettings settingsA, FakeSettings "fakePagedMethod", "fakeMethodBatching", "channelProvider", - "executorProvider" + "executorProvider", + "credentialsProvider" }); assertIsReflectionEqual(settingsA.fakeMethodSimple, settingsB.fakeMethodSimple); assertIsReflectionEqual(settingsA.fakePagedMethod, settingsB.fakePagedMethod); assertIsReflectionEqual(settingsA.fakeMethodBatching, settingsB.fakeMethodBatching); assertIsReflectionEqual(settingsA.getChannelProvider(), settingsB.getChannelProvider()); assertIsReflectionEqual(settingsA.getExecutorProvider(), settingsB.getExecutorProvider()); + assertIsReflectionEqual(settingsA.getCredentialsProvider(), settingsB.getCredentialsProvider()); } private static void assertIsReflectionEqual( @@ -563,13 +571,15 @@ private static void assertIsReflectionEqual( "fakePagedMethod", "fakeMethodBatching", "channelProvider", - "executorProvider" + "executorProvider", + "credentialsProvider" }); assertIsReflectionEqual(builderA.fakeMethodSimple, builderB.fakeMethodSimple); assertIsReflectionEqual(builderA.fakePagedMethod, builderB.fakePagedMethod); assertIsReflectionEqual(builderA.fakeMethodBatching, builderB.fakeMethodBatching); assertIsReflectionEqual(builderA.getChannelProvider(), builderB.getChannelProvider()); assertIsReflectionEqual(builderA.getExecutorProvider(), builderB.getExecutorProvider()); + assertIsReflectionEqual(builderA.getCredentialsProvider(), builderB.getCredentialsProvider()); } private static void assertIsReflectionEqual( From a1dcbd41086375b7ec5576425b57d1e07f167070 Mon Sep 17 00:00:00 2001 From: Michael Darakananda Date: Fri, 12 May 2017 15:11:11 +1000 Subject: [PATCH 4/7] pr comment --- .../api/gax/grpc/ChannelAndExecutor.java | 1 + .../api/gax/grpc/ClientInitContext.java | 67 +++++++++++++++++-- .../google/api/gax/grpc/ClientSettings.java | 1 + .../api/gax/grpc/OperationCallable.java | 21 +----- .../google/longrunning/OperationsClient.java | 43 +----------- .../api/gax/grpc/OperationCallableTest.java | 33 +-------- .../com/google/api/gax/grpc/SettingsTest.java | 3 - 7 files changed, 70 insertions(+), 99 deletions(-) diff --git a/gax-grpc/src/main/java/com/google/api/gax/grpc/ChannelAndExecutor.java b/gax-grpc/src/main/java/com/google/api/gax/grpc/ChannelAndExecutor.java index 5eb58b542..60b38f3d9 100644 --- a/gax-grpc/src/main/java/com/google/api/gax/grpc/ChannelAndExecutor.java +++ b/gax-grpc/src/main/java/com/google/api/gax/grpc/ChannelAndExecutor.java @@ -41,6 +41,7 @@ */ @BetaApi @AutoValue +@Deprecated public abstract class ChannelAndExecutor { public abstract ScheduledExecutorService getExecutor(); diff --git a/gax-grpc/src/main/java/com/google/api/gax/grpc/ClientInitContext.java b/gax-grpc/src/main/java/com/google/api/gax/grpc/ClientInitContext.java index b333c0196..bfb8d43f0 100644 --- a/gax-grpc/src/main/java/com/google/api/gax/grpc/ClientInitContext.java +++ b/gax-grpc/src/main/java/com/google/api/gax/grpc/ClientInitContext.java @@ -1,5 +1,5 @@ /* - * Copyright 2016, Google Inc. All rights reserved. + * Copyright 2017, 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 @@ -29,9 +29,16 @@ */ package com.google.api.gax.grpc; +import com.google.api.core.BetaApi; +import com.google.auth.Credentials; import com.google.auto.value.AutoValue; +import com.google.common.collect.ImmutableList; import io.grpc.CallCredentials; import io.grpc.Channel; +import io.grpc.ManagedChannel; +import io.grpc.auth.MoreCallCredentials; +import java.io.IOException; +import java.util.Collection; import java.util.concurrent.ScheduledExecutorService; import javax.annotation.Nullable; @@ -41,10 +48,13 @@ *

Unlike {@link ClientSettings} which allows users to configure the client, {@code * ClientInitContext} gathers members to simplify initialization later on. * - *

It it intended to be used in generated code. Most users will not need to use it. + *

It is intended to be used in generated code. Most users will not need to use it. */ +@BetaApi @AutoValue public abstract class ClientInitContext { + public abstract Collection getCloseables(); + public abstract Channel getChannel(); public abstract ScheduledExecutorService getExecutor(); @@ -52,16 +62,59 @@ public abstract class ClientInitContext { @Nullable public abstract CallCredentials getCallCredentials(); - public Builder toBuilder() { - return new AutoValue_ClientInitContext.Builder(this); + static Builder newBuilder() { + return new AutoValue_ClientInitContext.Builder(); } - public static Builder newBuilder() { - return new AutoValue_ClientInitContext.Builder(); + public static ClientInitContext initialize(ClientSettings settings) throws IOException { + ImmutableList.Builder closeables = ImmutableList.builder(); + + ExecutorProvider executorProvider = settings.getExecutorProvider(); + final ScheduledExecutorService executor = executorProvider.getExecutor(); + if (executorProvider.shouldAutoClose()) { + closeables.add( + new AutoCloseable() { + @Override + public void close() { + executor.shutdown(); + } + }); + } + + final ManagedChannel channel; + ChannelProvider channelProvider = settings.getChannelProvider(); + if (channelProvider.needsExecutor()) { + channel = channelProvider.getChannel(executor); + } else { + channel = channelProvider.getChannel(); + } + if (channelProvider.shouldAutoClose()) { + closeables.add( + new AutoCloseable() { + @Override + public void close() { + channel.shutdown(); + } + }); + } + + CallCredentials callCredentials = null; + Credentials credentials = settings.getCredentialsProvider().getCredentials(); + if (credentials != null) { + callCredentials = MoreCallCredentials.from(credentials); + } + return newBuilder() + .setCloseables(closeables.build()) + .setChannel(channel) + .setExecutor(executor) + .setCallCredentials(callCredentials) + .build(); } @AutoValue.Builder - public abstract static class Builder { + abstract static class Builder { + public abstract Builder setCloseables(Collection value); + public abstract Builder setChannel(Channel value); public abstract Builder setExecutor(ScheduledExecutorService value); diff --git a/gax-grpc/src/main/java/com/google/api/gax/grpc/ClientSettings.java b/gax-grpc/src/main/java/com/google/api/gax/grpc/ClientSettings.java index 2e9f4e666..86ba65bc8 100644 --- a/gax-grpc/src/main/java/com/google/api/gax/grpc/ClientSettings.java +++ b/gax-grpc/src/main/java/com/google/api/gax/grpc/ClientSettings.java @@ -87,6 +87,7 @@ protected ClientSettings( } /** Gets a channel and an executor for making calls. */ + @Deprecated public final ChannelAndExecutor getChannelAndExecutor() throws IOException { return ChannelAndExecutor.create(executorProvider, channelProvider); } diff --git a/gax-grpc/src/main/java/com/google/api/gax/grpc/OperationCallable.java b/gax-grpc/src/main/java/com/google/api/gax/grpc/OperationCallable.java index b89a0313f..07962caf3 100644 --- a/gax-grpc/src/main/java/com/google/api/gax/grpc/OperationCallable.java +++ b/gax-grpc/src/main/java/com/google/api/gax/grpc/OperationCallable.java @@ -67,21 +67,6 @@ public final class OperationCallable { this.settings = settings; } - /** - * Create an OperationCallable with a bound channel. If a call is made without specifying a - * channel, the {@code boundChannel} is used instead. - * - *

Package-private for internal use. - */ - OperationCallable bind(Channel boundChannel) { - return new OperationCallable<>( - initialCallable, - clientContext.toBuilder().setChannel(boundChannel).build(), - operationsClient, - responseClass, - settings); - } - /** * Creates a callable object that represents a long-running operation. Public only for technical * reasons - for advanced usage @@ -112,8 +97,8 @@ public static OperationCallable futureCall(RequestT request) { /** * Initiates an operation and polls for the final result. If the {@link io.grpc.Channel} * encapsulated in the given {@link com.google.api.gax.grpc.CallContext} is null, a channel must - * have already been bound, using {@link #bind(Channel)}. + * have already been bound at construction time. * * @param request The request to initiate the operation. * @param context {@link com.google.api.gax.grpc.CallContext} to make the call with diff --git a/gax-grpc/src/main/java/com/google/longrunning/OperationsClient.java b/gax-grpc/src/main/java/com/google/longrunning/OperationsClient.java index 95de0bdcd..7549f01dc 100644 --- a/gax-grpc/src/main/java/com/google/longrunning/OperationsClient.java +++ b/gax-grpc/src/main/java/com/google/longrunning/OperationsClient.java @@ -32,19 +32,12 @@ import static com.google.longrunning.PagedResponseWrappers.ListOperationsPagedResponse; import com.google.api.core.BetaApi; -import com.google.api.gax.core.CredentialsProvider; -import com.google.api.gax.grpc.ChannelAndExecutor; import com.google.api.gax.grpc.ClientInitContext; import com.google.api.gax.grpc.UnaryCallable; import com.google.protobuf.Empty; -import io.grpc.CallCredentials; -import io.grpc.ManagedChannel; -import io.grpc.auth.MoreCallCredentials; -import java.io.Closeable; import java.io.IOException; import java.util.ArrayList; import java.util.List; -import java.util.concurrent.ScheduledExecutorService; import javax.annotation.Generated; // AUTO-GENERATED DOCUMENTATION AND SERVICE @@ -113,8 +106,6 @@ @BetaApi public class OperationsClient implements AutoCloseable { private final OperationsSettings settings; - private final ScheduledExecutorService executor; - private final ManagedChannel channel; private final List closeables = new ArrayList<>(); private final UnaryCallable getOperationCallable; @@ -138,21 +129,8 @@ public static final OperationsClient create(OperationsSettings settings) throws */ protected OperationsClient(OperationsSettings settings) throws IOException { this.settings = settings; - ChannelAndExecutor channelAndExecutor = settings.getChannelAndExecutor(); - this.executor = channelAndExecutor.getExecutor(); - this.channel = channelAndExecutor.getChannel(); - CredentialsProvider credentialsProvider = settings.getCredentialsProvider(); - CallCredentials callCredentials = null; - if (credentialsProvider.getCredentials() != null) { - callCredentials = MoreCallCredentials.from(credentialsProvider.getCredentials()); - } - ClientInitContext clientContext = - ClientInitContext.newBuilder() - .setExecutor(this.executor) - .setChannel(this.channel) - .setCallCredentials(callCredentials) - .build(); + ClientInitContext clientContext = ClientInitContext.initialize(settings); this.getOperationCallable = UnaryCallable.create(settings.getOperationSettings(), clientContext); @@ -165,24 +143,7 @@ protected OperationsClient(OperationsSettings settings) throws IOException { this.deleteOperationCallable = UnaryCallable.create(settings.deleteOperationSettings(), clientContext); - if (settings.getChannelProvider().shouldAutoClose()) { - closeables.add( - new Closeable() { - @Override - public void close() throws IOException { - channel.shutdown(); - } - }); - } - if (settings.getExecutorProvider().shouldAutoClose()) { - closeables.add( - new Closeable() { - @Override - public void close() throws IOException { - executor.shutdown(); - } - }); - } + closeables.addAll(clientContext.getCloseables()); } public final OperationsSettings getSettings() { diff --git a/gax-grpc/src/test/java/com/google/api/gax/grpc/OperationCallableTest.java b/gax-grpc/src/test/java/com/google/api/gax/grpc/OperationCallableTest.java index b1e7a6d74..5dd110678 100644 --- a/gax-grpc/src/test/java/com/google/api/gax/grpc/OperationCallableTest.java +++ b/gax-grpc/src/test/java/com/google/api/gax/grpc/OperationCallableTest.java @@ -42,6 +42,7 @@ import io.grpc.Channel; import java.io.IOException; import java.util.Arrays; +import java.util.Collections; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledThreadPoolExecutor; import org.junit.After; @@ -98,6 +99,7 @@ public void testCall() { ClientInitContext.newBuilder() .setChannel(Mockito.mock(Channel.class)) .setExecutor(executor) + .setCloseables(Collections.emptyList()) .build(), operationsClient, Color.class, @@ -107,36 +109,6 @@ public void testCall() { Truth.assertThat(stash.context.getCallOptions()).isEqualTo(CallOptions.DEFAULT); } - @Test - public void testBind() { - String opName = "testBind"; - Color injectedResponse = Color.newBuilder().setBlue(1.0f).build(); - Operation resultOperation = - Operation.newBuilder() - .setName(opName) - .setDone(true) - .setResponse(Any.pack(injectedResponse)) - .build(); - StashCallable stash = new StashCallable<>(resultOperation); - UnaryCallable stashUnaryCallable = new UnaryCallable<>(stash); - - Channel channel = Mockito.mock(Channel.class); - OperationCallable callable = - new OperationCallable( - stashUnaryCallable, - ClientInitContext.newBuilder() - .setChannel(Mockito.mock(Channel.class)) - .setExecutor(executor) - .build(), - operationsClient, - Color.class, - null); - callable = callable.bind(channel); - Color response = callable.call(2); - Truth.assertThat(response).isEqualTo(injectedResponse); - Truth.assertThat(stash.context.getChannel()).isSameAs(channel); - } - @Test public void testResumeFutureCall() throws Exception { String opName = "testCall"; @@ -158,6 +130,7 @@ public void testResumeFutureCall() throws Exception { ClientInitContext.newBuilder() .setChannel(Mockito.mock(Channel.class)) .setExecutor(executor) + .setCloseables(Collections.emptyList()) .build(), operationsClient, Color.class, diff --git a/gax-grpc/src/test/java/com/google/api/gax/grpc/SettingsTest.java b/gax-grpc/src/test/java/com/google/api/gax/grpc/SettingsTest.java index b2c87f8ad..6dea8de41 100644 --- a/gax-grpc/src/test/java/com/google/api/gax/grpc/SettingsTest.java +++ b/gax-grpc/src/test/java/com/google/api/gax/grpc/SettingsTest.java @@ -475,9 +475,6 @@ public void callSettingsTimeoutNoRetries() throws IOException { .setMaxRpcTimeout(timeout)); FakeSettings settingsB = builderB.build(); - System.err.println(builderA); - System.err.println(builderB); - assertIsReflectionEqual(builderA, builderB); assertIsReflectionEqual(settingsA, settingsB); } From 0f985bad0ca26ba1187c6da34b50c6525f8050e2 Mon Sep 17 00:00:00 2001 From: Michael Darakananda Date: Mon, 15 May 2017 16:45:26 +1000 Subject: [PATCH 5/7] pr comments --- .../api/gax/grpc/BatchingCallSettings.java | 2 +- ...entInitContext.java => ClientContext.java} | 24 +++++--------- .../api/gax/grpc/OperationCallSettings.java | 2 +- .../api/gax/grpc/OperationCallable.java | 10 +++--- .../api/gax/grpc/PagedCallSettings.java | 4 +-- .../api/gax/grpc/SimpleCallSettings.java | 2 +- .../api/gax/grpc/UnaryCallSettingsTyped.java | 6 ++-- .../google/api/gax/grpc/UnaryCallable.java | 32 ++++++++++--------- .../google/longrunning/OperationsClient.java | 4 +-- .../google/api/gax/grpc/AuthCallableTest.java | 11 +++++-- .../api/gax/grpc/OperationCallableTest.java | 4 +-- 11 files changed, 50 insertions(+), 51 deletions(-) rename gax-grpc/src/main/java/com/google/api/gax/grpc/{ClientInitContext.java => ClientContext.java} (82%) diff --git a/gax-grpc/src/main/java/com/google/api/gax/grpc/BatchingCallSettings.java b/gax-grpc/src/main/java/com/google/api/gax/grpc/BatchingCallSettings.java index ff25d8410..7a2749e2d 100644 --- a/gax-grpc/src/main/java/com/google/api/gax/grpc/BatchingCallSettings.java +++ b/gax-grpc/src/main/java/com/google/api/gax/grpc/BatchingCallSettings.java @@ -50,7 +50,7 @@ public final class BatchingCallSettings private BatcherFactory batcherFactory; /** Package-private, for use by UnaryCallable. */ - UnaryCallable create(ClientInitContext context) { + UnaryCallable create(ClientContext context) { UnaryCallable baseCallable = createBaseCallable(context); batcherFactory = new BatcherFactory<>(batchingDescriptor, batchingSettings, context.getExecutor()); diff --git a/gax-grpc/src/main/java/com/google/api/gax/grpc/ClientInitContext.java b/gax-grpc/src/main/java/com/google/api/gax/grpc/ClientContext.java similarity index 82% rename from gax-grpc/src/main/java/com/google/api/gax/grpc/ClientInitContext.java rename to gax-grpc/src/main/java/com/google/api/gax/grpc/ClientContext.java index bfb8d43f0..a2bedb580 100644 --- a/gax-grpc/src/main/java/com/google/api/gax/grpc/ClientInitContext.java +++ b/gax-grpc/src/main/java/com/google/api/gax/grpc/ClientContext.java @@ -33,10 +33,8 @@ import com.google.auth.Credentials; import com.google.auto.value.AutoValue; import com.google.common.collect.ImmutableList; -import io.grpc.CallCredentials; import io.grpc.Channel; import io.grpc.ManagedChannel; -import io.grpc.auth.MoreCallCredentials; import java.io.IOException; import java.util.Collection; import java.util.concurrent.ScheduledExecutorService; @@ -46,13 +44,13 @@ * Encapsulates data used to initialize a client. * *

Unlike {@link ClientSettings} which allows users to configure the client, {@code - * ClientInitContext} gathers members to simplify initialization later on. + * ClientContext} gathers members to simplify initialization later on. * *

It is intended to be used in generated code. Most users will not need to use it. */ @BetaApi @AutoValue -public abstract class ClientInitContext { +public abstract class ClientContext { public abstract Collection getCloseables(); public abstract Channel getChannel(); @@ -60,13 +58,13 @@ public abstract class ClientInitContext { public abstract ScheduledExecutorService getExecutor(); @Nullable - public abstract CallCredentials getCallCredentials(); + public abstract Credentials getCredentials(); static Builder newBuilder() { - return new AutoValue_ClientInitContext.Builder(); + return new AutoValue_ClientContext.Builder(); } - public static ClientInitContext initialize(ClientSettings settings) throws IOException { + public static ClientContext create(ClientSettings settings) throws IOException { ImmutableList.Builder closeables = ImmutableList.builder(); ExecutorProvider executorProvider = settings.getExecutorProvider(); @@ -97,17 +95,11 @@ public void close() { } }); } - - CallCredentials callCredentials = null; - Credentials credentials = settings.getCredentialsProvider().getCredentials(); - if (credentials != null) { - callCredentials = MoreCallCredentials.from(credentials); - } return newBuilder() .setCloseables(closeables.build()) .setChannel(channel) .setExecutor(executor) - .setCallCredentials(callCredentials) + .setCredentials(settings.getCredentialsProvider().getCredentials()) .build(); } @@ -119,8 +111,8 @@ abstract static class Builder { public abstract Builder setExecutor(ScheduledExecutorService value); - public abstract Builder setCallCredentials(CallCredentials value); + public abstract Builder setCredentials(Credentials value); - public abstract ClientInitContext build(); + public abstract ClientContext build(); } } diff --git a/gax-grpc/src/main/java/com/google/api/gax/grpc/OperationCallSettings.java b/gax-grpc/src/main/java/com/google/api/gax/grpc/OperationCallSettings.java index 03fa13eb8..4096bd94c 100644 --- a/gax-grpc/src/main/java/com/google/api/gax/grpc/OperationCallSettings.java +++ b/gax-grpc/src/main/java/com/google/api/gax/grpc/OperationCallSettings.java @@ -57,7 +57,7 @@ public final Duration getPollingInterval() { // package-private for internal use. OperationCallable createOperationCallable( - ClientInitContext context, OperationsClient operationsClient) { + ClientContext context, OperationsClient operationsClient) { UnaryCallable initialCallable = initialCallSettings.create(context); OperationCallable operationCallable = new OperationCallable<>(initialCallable, context, operationsClient, responseClass, this); diff --git a/gax-grpc/src/main/java/com/google/api/gax/grpc/OperationCallable.java b/gax-grpc/src/main/java/com/google/api/gax/grpc/OperationCallable.java index 07962caf3..c0765baca 100644 --- a/gax-grpc/src/main/java/com/google/api/gax/grpc/OperationCallable.java +++ b/gax-grpc/src/main/java/com/google/api/gax/grpc/OperationCallable.java @@ -48,7 +48,7 @@ @BetaApi public final class OperationCallable { private final UnaryCallable initialCallable; - private final ClientInitContext clientContext; + private final ClientContext clientContext; private final OperationsClient operationsClient; private final Class responseClass; private final OperationCallSettings settings; @@ -56,7 +56,7 @@ public final class OperationCallable { /** Package-private for internal use. */ OperationCallable( UnaryCallable initialCallable, - ClientInitContext clientContext, + ClientContext clientContext, OperationsClient operationsClient, Class responseClass, OperationCallSettings settings) { @@ -73,13 +73,13 @@ public final class OperationCallable { * * @param operationCallSettings {@link com.google.api.gax.grpc.OperationCallSettings} to configure * the method-level settings with. - * @param clientContext {@link ClientInitContext} to use to connect to the service. + * @param clientContext {@link ClientContext} to use to connect to the service. * @param operationsClient {@link OperationsClient} to use to poll for updates on the Operation. * @return {@link com.google.api.gax.grpc.OperationCallable} callable object. */ public static OperationCallable create( OperationCallSettings operationCallSettings, - ClientInitContext clientContext, + ClientContext clientContext, OperationsClient operationsClient) { return operationCallSettings.createOperationCallable(clientContext, operationsClient); } @@ -91,7 +91,7 @@ public static OperationCallable pagedListResponseFactory; /** Package-private, for use by UnaryCallable. */ - UnaryCallable create(ClientInitContext context) { + UnaryCallable create(ClientContext context) { return createBaseCallable(context); } /** Package-private, for use by UnaryCallable. */ - UnaryCallable createPagedVariant(ClientInitContext context) { + UnaryCallable createPagedVariant(ClientContext context) { UnaryCallable baseCallable = createBaseCallable(context); return baseCallable.paged(pagedListResponseFactory); } diff --git a/gax-grpc/src/main/java/com/google/api/gax/grpc/SimpleCallSettings.java b/gax-grpc/src/main/java/com/google/api/gax/grpc/SimpleCallSettings.java index 0a96890af..1255127f4 100644 --- a/gax-grpc/src/main/java/com/google/api/gax/grpc/SimpleCallSettings.java +++ b/gax-grpc/src/main/java/com/google/api/gax/grpc/SimpleCallSettings.java @@ -45,7 +45,7 @@ public final class SimpleCallSettings extends UnaryCallSettingsTyped { /** Package-private, for use by UnaryCallable. */ - UnaryCallable create(ClientInitContext context) { + UnaryCallable create(ClientContext context) { return createBaseCallable(context); } diff --git a/gax-grpc/src/main/java/com/google/api/gax/grpc/UnaryCallSettingsTyped.java b/gax-grpc/src/main/java/com/google/api/gax/grpc/UnaryCallSettingsTyped.java index 2388d22ed..60c259c8e 100644 --- a/gax-grpc/src/main/java/com/google/api/gax/grpc/UnaryCallSettingsTyped.java +++ b/gax-grpc/src/main/java/com/google/api/gax/grpc/UnaryCallSettingsTyped.java @@ -65,7 +65,7 @@ protected UnaryCallSettingsTyped( this.methodDescriptor = methodDescriptor; } - protected UnaryCallable createBaseCallable(ClientInitContext context) { + protected UnaryCallable createBaseCallable(ClientContext context) { ClientCallFactory clientCallFactory = new DescriptorClientCallFactory<>(methodDescriptor); UnaryCallable callable = @@ -76,8 +76,8 @@ protected UnaryCallable createBaseCallable(ClientInitContex if (getRetrySettings() != null) { callable = callable.retrying(getRetrySettings(), context.getExecutor()); } - // withAuth works properly even if getCallCredentials is null. - callable = callable.withAuth(context.getCallCredentials()); + // withAuth works properly even if getCredentials returns null. + callable = callable.withAuth(context.getCredentials()); return callable; } diff --git a/gax-grpc/src/main/java/com/google/api/gax/grpc/UnaryCallable.java b/gax-grpc/src/main/java/com/google/api/gax/grpc/UnaryCallable.java index 89649fe84..1c5ecd6ed 100644 --- a/gax-grpc/src/main/java/com/google/api/gax/grpc/UnaryCallable.java +++ b/gax-grpc/src/main/java/com/google/api/gax/grpc/UnaryCallable.java @@ -35,12 +35,13 @@ import com.google.api.core.NanoClock; import com.google.api.gax.batching.BatchingSettings; import com.google.api.gax.retrying.RetrySettings; +import com.google.auth.Credentials; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableSet; -import io.grpc.CallCredentials; import io.grpc.Channel; import io.grpc.Status; +import io.grpc.auth.MoreCallCredentials; import java.util.concurrent.ScheduledExecutorService; import javax.annotation.Nullable; @@ -102,11 +103,11 @@ public final class UnaryCallable { * * @param simpleCallSettings {@link com.google.api.gax.grpc.SimpleCallSettings} to configure the * method-level settings with. - * @param context {@link ClientInitContext} to use to connect to the service. + * @param context {@link ClientContext} to use to connect to the service. * @return {@link com.google.api.gax.grpc.UnaryCallable} callable object. */ public static UnaryCallable create( - SimpleCallSettings simpleCallSettings, ClientInitContext context) { + SimpleCallSettings simpleCallSettings, ClientContext context) { return simpleCallSettings.create(context); } @@ -126,7 +127,7 @@ public static UnaryCallable create( Channel channel, ScheduledExecutorService executor) { return simpleCallSettings.create( - ClientInitContext.newBuilder().setChannel(channel).setExecutor(executor).build()); + ClientContext.newBuilder().setChannel(channel).setExecutor(executor).build()); } /** @@ -135,13 +136,13 @@ public static UnaryCallable create( * * @param PagedCallSettings {@link com.google.api.gax.grpc.PagedCallSettings} to configure the * paged settings with. - * @param context {@link ClientInitContext} to use to connect to the service. + * @param context {@link ClientContext} to use to connect to the service. * @return {@link com.google.api.gax.grpc.UnaryCallable} callable object. */ public static UnaryCallable createPagedVariant( PagedCallSettings PagedCallSettings, - ClientInitContext context) { + ClientContext context) { return PagedCallSettings.createPagedVariant(context); } @@ -162,7 +163,7 @@ UnaryCallable createPagedVariant( Channel channel, ScheduledExecutorService executor) { return PagedCallSettings.createPagedVariant( - ClientInitContext.newBuilder().setChannel(channel).setExecutor(executor).build()); + ClientContext.newBuilder().setChannel(channel).setExecutor(executor).build()); } /** @@ -171,12 +172,12 @@ UnaryCallable createPagedVariant( * * @param PagedCallSettings {@link com.google.api.gax.grpc.PagedCallSettings} to configure the * paged settings with. - * @param context {@link ClientInitContext} to use to connect to the service. + * @param context {@link ClientContext} to use to connect to the service. * @return {@link com.google.api.gax.grpc.UnaryCallable} callable object. */ public static UnaryCallable create( PagedCallSettings PagedCallSettings, - ClientInitContext context) { + ClientContext context) { return PagedCallSettings.create(context); } @@ -196,7 +197,7 @@ public static UnaryCallable UnaryCallable UnaryCallable create( - BatchingCallSettings batchingCallSettings, ClientInitContext context) { + BatchingCallSettings batchingCallSettings, ClientContext context) { return batchingCallSettings.create(context); } @@ -229,7 +230,7 @@ public static UnaryCallable create( Channel channel, ScheduledExecutorService executor) { return batchingCallSettings.create( - ClientInitContext.newBuilder().setChannel(channel).setExecutor(executor).build()); + ClientContext.newBuilder().setChannel(channel).setExecutor(executor).build()); } /** @@ -397,10 +398,11 @@ UnaryCallable batching( new BatchingCallable<>(callable, batchingDescriptor, batcherFactory), channel, settings); } - UnaryCallable withAuth(CallCredentials credentials) { + UnaryCallable withAuth(Credentials credentials) { if (credentials == null) { return this; } - return new UnaryCallable<>(new AuthCallable<>(callable, credentials), channel, settings); + return new UnaryCallable<>( + new AuthCallable<>(callable, MoreCallCredentials.from(credentials)), channel, settings); } } diff --git a/gax-grpc/src/main/java/com/google/longrunning/OperationsClient.java b/gax-grpc/src/main/java/com/google/longrunning/OperationsClient.java index 7549f01dc..909fdcc38 100644 --- a/gax-grpc/src/main/java/com/google/longrunning/OperationsClient.java +++ b/gax-grpc/src/main/java/com/google/longrunning/OperationsClient.java @@ -32,7 +32,7 @@ import static com.google.longrunning.PagedResponseWrappers.ListOperationsPagedResponse; import com.google.api.core.BetaApi; -import com.google.api.gax.grpc.ClientInitContext; +import com.google.api.gax.grpc.ClientContext; import com.google.api.gax.grpc.UnaryCallable; import com.google.protobuf.Empty; import java.io.IOException; @@ -130,7 +130,7 @@ public static final OperationsClient create(OperationsSettings settings) throws protected OperationsClient(OperationsSettings settings) throws IOException { this.settings = settings; - ClientInitContext clientContext = ClientInitContext.initialize(settings); + ClientContext clientContext = ClientContext.create(settings); this.getOperationCallable = UnaryCallable.create(settings.getOperationSettings(), clientContext); diff --git a/gax-grpc/src/test/java/com/google/api/gax/grpc/AuthCallableTest.java b/gax-grpc/src/test/java/com/google/api/gax/grpc/AuthCallableTest.java index e7b4b6cfc..014c59d56 100644 --- a/gax-grpc/src/test/java/com/google/api/gax/grpc/AuthCallableTest.java +++ b/gax-grpc/src/test/java/com/google/api/gax/grpc/AuthCallableTest.java @@ -31,6 +31,7 @@ import com.google.api.core.ApiFuture; import com.google.api.core.ApiFutures; +import com.google.auth.Credentials; import com.google.common.truth.Truth; import io.grpc.CallCredentials; import java.util.concurrent.CancellationException; @@ -58,8 +59,12 @@ public void testAuth() throws InterruptedException, ExecutionException, Cancella Truth.assertThat(UnaryCallable.create(stash).futureCall(0).get()).isEqualTo(42); Truth.assertThat(stash.lastCredentials).isNull(); - CallCredentials cred = Mockito.mock(CallCredentials.class); - Truth.assertThat(UnaryCallable.create(stash).withAuth(cred).futureCall(0).get()).isEqualTo(42); - Truth.assertThat(stash.lastCredentials).isEqualTo(cred); + Truth.assertThat( + UnaryCallable.create(stash) + .withAuth(Mockito.mock(Credentials.class)) + .futureCall(0) + .get()) + .isEqualTo(42); + Truth.assertThat(stash.lastCredentials).isNotNull(); } } diff --git a/gax-grpc/src/test/java/com/google/api/gax/grpc/OperationCallableTest.java b/gax-grpc/src/test/java/com/google/api/gax/grpc/OperationCallableTest.java index 5dd110678..87fad1bd1 100644 --- a/gax-grpc/src/test/java/com/google/api/gax/grpc/OperationCallableTest.java +++ b/gax-grpc/src/test/java/com/google/api/gax/grpc/OperationCallableTest.java @@ -96,7 +96,7 @@ public void testCall() { OperationCallable callable = new OperationCallable( stashUnaryCallable, - ClientInitContext.newBuilder() + ClientContext.newBuilder() .setChannel(Mockito.mock(Channel.class)) .setExecutor(executor) .setCloseables(Collections.emptyList()) @@ -127,7 +127,7 @@ public void testResumeFutureCall() throws Exception { OperationCallable callable = new OperationCallable( stashUnaryCallable, - ClientInitContext.newBuilder() + ClientContext.newBuilder() .setChannel(Mockito.mock(Channel.class)) .setExecutor(executor) .setCloseables(Collections.emptyList()) From 79efe90718d01805b7270b521a75503f02127b17 Mon Sep 17 00:00:00 2001 From: Michael Darakananda Date: Thu, 18 May 2017 17:11:37 +1000 Subject: [PATCH 6/7] pr comment --- .../src/main/java/com/google/api/gax/grpc/AuthCallable.java | 6 ++++-- .../main/java/com/google/api/gax/grpc/ClientContext.java | 6 ++---- .../main/java/com/google/api/gax/grpc/UnaryCallable.java | 4 +--- .../main/java/com/google/longrunning/OperationsClient.java | 6 ++---- 4 files changed, 9 insertions(+), 13 deletions(-) diff --git a/gax-grpc/src/main/java/com/google/api/gax/grpc/AuthCallable.java b/gax-grpc/src/main/java/com/google/api/gax/grpc/AuthCallable.java index 15d859916..e3f16e60b 100644 --- a/gax-grpc/src/main/java/com/google/api/gax/grpc/AuthCallable.java +++ b/gax-grpc/src/main/java/com/google/api/gax/grpc/AuthCallable.java @@ -30,8 +30,10 @@ package com.google.api.gax.grpc; import com.google.api.core.ApiFuture; +import com.google.auth.Credentials; import com.google.common.base.Preconditions; import io.grpc.CallCredentials; +import io.grpc.auth.MoreCallCredentials; /** * Implements the credential insertion for {@link UnaryCallable}. * @@ -41,9 +43,9 @@ class AuthCallable implements FutureCallable callable; private final CallCredentials credentials; - AuthCallable(FutureCallable callable, CallCredentials credentials) { + AuthCallable(FutureCallable callable, Credentials credentials) { this.callable = Preconditions.checkNotNull(callable); - this.credentials = Preconditions.checkNotNull(credentials); + this.credentials = MoreCallCredentials.from(Preconditions.checkNotNull(credentials)); } @Override diff --git a/gax-grpc/src/main/java/com/google/api/gax/grpc/ClientContext.java b/gax-grpc/src/main/java/com/google/api/gax/grpc/ClientContext.java index a2bedb580..c95cac8dc 100644 --- a/gax-grpc/src/main/java/com/google/api/gax/grpc/ClientContext.java +++ b/gax-grpc/src/main/java/com/google/api/gax/grpc/ClientContext.java @@ -41,12 +41,10 @@ import javax.annotation.Nullable; /** - * Encapsulates data used to initialize a client. + * Encapsulates client state, including channel, executor, and credentials. * *

Unlike {@link ClientSettings} which allows users to configure the client, {@code - * ClientContext} gathers members to simplify initialization later on. - * - *

It is intended to be used in generated code. Most users will not need to use it. + * ClientContext} is intended to be used in generated code. Most users will not need to use it. */ @BetaApi @AutoValue diff --git a/gax-grpc/src/main/java/com/google/api/gax/grpc/UnaryCallable.java b/gax-grpc/src/main/java/com/google/api/gax/grpc/UnaryCallable.java index 1c5ecd6ed..9649176ff 100644 --- a/gax-grpc/src/main/java/com/google/api/gax/grpc/UnaryCallable.java +++ b/gax-grpc/src/main/java/com/google/api/gax/grpc/UnaryCallable.java @@ -41,7 +41,6 @@ import com.google.common.collect.ImmutableSet; import io.grpc.Channel; import io.grpc.Status; -import io.grpc.auth.MoreCallCredentials; import java.util.concurrent.ScheduledExecutorService; import javax.annotation.Nullable; @@ -402,7 +401,6 @@ UnaryCallable withAuth(Credentials credentials) { if (credentials == null) { return this; } - return new UnaryCallable<>( - new AuthCallable<>(callable, MoreCallCredentials.from(credentials)), channel, settings); + return new UnaryCallable<>(new AuthCallable<>(callable, credentials), channel, settings); } } diff --git a/gax-grpc/src/main/java/com/google/longrunning/OperationsClient.java b/gax-grpc/src/main/java/com/google/longrunning/OperationsClient.java index 909fdcc38..f85b26292 100644 --- a/gax-grpc/src/main/java/com/google/longrunning/OperationsClient.java +++ b/gax-grpc/src/main/java/com/google/longrunning/OperationsClient.java @@ -91,12 +91,10 @@ * *

  * 
- * InstantiatingChannelProvider channelProvider =
- *     OperationsSettings.defaultChannelProviderBuilder()
+ * OperationsSettings operationsSettings =
+ *     OperationsSettings.defaultBuilder()
  *         .setCredentialsProvider(FixedCredentialsProvider.create(myCredentials))
  *         .build();
- * OperationsSettings operationsSettings =
- *     OperationsSettings.defaultBuilder().setChannelProvider(channelProvider).build();
  * OperationsClient operationsClient =
  *     OperationsClient.create(operationsSettings);
  * 

From 994737d7e56c90d740f863a8a2ac3613d3606fcf Mon Sep 17 00:00:00 2001
From: Michael Darakananda 
Date: Fri, 19 May 2017 09:43:56 +1000
Subject: [PATCH 7/7] whitespace

---
 gax-grpc/src/main/java/com/google/api/gax/grpc/AuthCallable.java | 1 +
 1 file changed, 1 insertion(+)

diff --git a/gax-grpc/src/main/java/com/google/api/gax/grpc/AuthCallable.java b/gax-grpc/src/main/java/com/google/api/gax/grpc/AuthCallable.java
index e3f16e60b..b6417ffba 100644
--- a/gax-grpc/src/main/java/com/google/api/gax/grpc/AuthCallable.java
+++ b/gax-grpc/src/main/java/com/google/api/gax/grpc/AuthCallable.java
@@ -34,6 +34,7 @@
 import com.google.common.base.Preconditions;
 import io.grpc.CallCredentials;
 import io.grpc.auth.MoreCallCredentials;
+
 /**
  * Implements the credential insertion for {@link UnaryCallable}.
  *