diff --git a/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java b/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java index f986f16b9..b41518667 100644 --- a/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java +++ b/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java @@ -138,13 +138,12 @@ public static ClientContext create(ClientSettings settings) throws IOException { public static ClientContext create(StubSettings settings) throws IOException { ApiClock clock = settings.getClock(); - ExecutorProvider workerExecutorProvider = settings.getWorkerExecutorProvider(); - final ScheduledExecutorService workerExecutor = workerExecutorProvider.getExecutor(); + ExecutorProvider backgroundExecutorProvider = settings.getBackgroundExecutorProvider(); + final ScheduledExecutorService backgroundExecutor = backgroundExecutorProvider.getExecutor(); + ExecutorProvider executorProvider = settings.getExecutorProvider(); final ScheduledExecutorService executor = - settings.getExecutorProvider() == null - ? null - : settings.getExecutorProvider().getExecutor(); + executorProvider == null ? null : executorProvider.getExecutor(); Credentials credentials = settings.getCredentialsProvider().getCredentials(); @@ -193,7 +192,7 @@ public static ClientContext create(StubSettings settings) throws IOException { watchdogProvider = watchdogProvider.withClock(clock); } if (watchdogProvider.needsExecutor()) { - watchdogProvider = watchdogProvider.withExecutor(workerExecutor); + watchdogProvider = watchdogProvider.withExecutor(backgroundExecutor); } watchdog = watchdogProvider.getWatchdog(); } @@ -203,16 +202,22 @@ public static ClientContext create(StubSettings settings) throws IOException { if (transportChannelProvider.shouldAutoClose()) { backgroundResources.add(transportChannel); } - if (workerExecutorProvider.shouldAutoClose()) { - backgroundResources.add(new ExecutorAsBackgroundResource(workerExecutor)); + if (backgroundExecutorProvider.shouldAutoClose()) { + backgroundResources.add(new ExecutorAsBackgroundResource(backgroundExecutor)); } if (watchdogProvider != null && watchdogProvider.shouldAutoClose()) { backgroundResources.add(watchdog); } + if (executorProvider != null + && executor != null + && executorProvider.shouldAutoClose() + && executor != backgroundExecutor) { + backgroundResources.add(new ExecutorAsBackgroundResource(executor)); + } return newBuilder() .setBackgroundResources(backgroundResources.build()) - .setExecutor(workerExecutor) + .setExecutor(backgroundExecutor) .setCredentials(credentials) .setTransportChannel(transportChannel) .setHeaders(ImmutableMap.copyOf(settings.getHeaderProvider().getHeaders())) diff --git a/gax/src/main/java/com/google/api/gax/rpc/ClientSettings.java b/gax/src/main/java/com/google/api/gax/rpc/ClientSettings.java index d532f75d6..e782694f3 100644 --- a/gax/src/main/java/com/google/api/gax/rpc/ClientSettings.java +++ b/gax/src/main/java/com/google/api/gax/rpc/ClientSettings.java @@ -36,6 +36,7 @@ import com.google.api.gax.core.ExecutorProvider; import com.google.common.base.MoreObjects; import java.io.IOException; +import java.util.concurrent.Executor; import javax.annotation.Nonnull; import javax.annotation.Nullable; import org.threeten.bp.Duration; @@ -63,8 +64,14 @@ public final StubSettings getStubSettings() { return stubSettings; } + @Deprecated + /** @deprecated Please use {@link #getBackgroundExecutorProvider()} */ public final ExecutorProvider getExecutorProvider() { - return stubSettings.getWorkerExecutorProvider(); + return stubSettings.getBackgroundExecutorProvider(); + } + + public final ExecutorProvider getBackgroundExecutorProvider() { + return stubSettings.getBackgroundExecutorProvider(); } public final TransportChannelProvider getTransportChannelProvider() { @@ -159,10 +166,25 @@ protected StubSettings.Builder getStubSettings() { * call logic (such as retries and long-running operations), and also to pass to the transport * settings if an executor is needed for the transport and it doesn't have its own executor * provider. + * + * @deprecated Please use {@link #setBackgroundExecutorProvider(ExecutorProvider)} for setting + * executor to use for running scheduled API call logic. To set executor for {@link + * TransportChannelProvider}, please use {@link + * TransportChannelProvider#withExecutor(Executor)} instead. */ + @Deprecated public B setExecutorProvider(ExecutorProvider executorProvider) { stubSettings.setExecutorProvider(executorProvider); - stubSettings.setWorkerExecutorProvider(executorProvider); + stubSettings.setBackgroundExecutorProvider(executorProvider); + return self(); + } + + /** + * Sets the ExecutorProvider to use for getting the executor to use for running scheduledf API + * call logic (such as retries and long-running operations). + */ + public B setBackgroundExecutorProvider(ExecutorProvider executorProvider) { + stubSettings.setBackgroundExecutorProvider(executorProvider); return self(); } @@ -239,9 +261,18 @@ public B setWatchdogCheckInterval(@Nullable Duration checkInterval) { return self(); } - /** Gets the ExecutorProvider that was previously set on this Builder. */ + @Deprecated + /** + * @deprecated Please use {@link #getBackgroundExecutorProvider()} Gets the ExecutorProvider + * that was previously set on this Builder. + */ public ExecutorProvider getExecutorProvider() { - return stubSettings.getWorkerExecutorProvider(); + return stubSettings.getBackgroundExecutorProvider(); + } + + /** Gets the ExecutorProvider that was previously set on this Builder. */ + public ExecutorProvider getBackgroundExecutorProvider() { + return stubSettings.getBackgroundExecutorProvider(); } /** Gets the TransportProvider that was previously set on this Builder. */ diff --git a/gax/src/main/java/com/google/api/gax/rpc/StubSettings.java b/gax/src/main/java/com/google/api/gax/rpc/StubSettings.java index c2076c4f9..7e54f874f 100644 --- a/gax/src/main/java/com/google/api/gax/rpc/StubSettings.java +++ b/gax/src/main/java/com/google/api/gax/rpc/StubSettings.java @@ -65,7 +65,7 @@ public abstract class StubSettings> { static final String QUOTA_PROJECT_ID_HEADER_KEY = "x-goog-user-project"; private final ExecutorProvider executorProvider; - private final ExecutorProvider workerExecutorProvider; + private final ExecutorProvider backgroundExecutorProvider; private final CredentialsProvider credentialsProvider; private final HeaderProvider headerProvider; private final HeaderProvider internalHeaderProvider; @@ -80,7 +80,7 @@ public abstract class StubSettings> { /** Constructs an instance of StubSettings. */ protected StubSettings(Builder builder) { this.executorProvider = builder.executorProvider; - this.workerExecutorProvider = builder.workerExecutorProvider; + this.backgroundExecutorProvider = builder.backgroundExecutorProvider; this.transportChannelProvider = builder.transportChannelProvider; this.credentialsProvider = builder.credentialsProvider; this.headerProvider = builder.headerProvider; @@ -93,14 +93,14 @@ protected StubSettings(Builder builder) { this.tracerFactory = builder.tracerFactory; } - /** @deprecated Please use {@link #getWorkerExecutorProvider()}. */ + /** @deprecated Please use {@link #getBackgroundExecutorProvider()}. */ @Deprecated public final ExecutorProvider getExecutorProvider() { return executorProvider; } - public final ExecutorProvider getWorkerExecutorProvider() { - return workerExecutorProvider; + public final ExecutorProvider getBackgroundExecutorProvider() { + return backgroundExecutorProvider; } public final TransportChannelProvider getTransportChannelProvider() { @@ -158,7 +158,7 @@ public ApiTracerFactory getTracerFactory() { public String toString() { return MoreObjects.toStringHelper(this) .add("executorProvider", executorProvider) - .add("workerExecutorProvider", workerExecutorProvider) + .add("workerExecutorProvider", backgroundExecutorProvider) .add("transportChannelProvider", transportChannelProvider) .add("credentialsProvider", credentialsProvider) .add("headerProvider", headerProvider) @@ -178,7 +178,7 @@ public abstract static class Builder< SettingsT extends StubSettings, B extends Builder> { private ExecutorProvider executorProvider; - private ExecutorProvider workerExecutorProvider; + private ExecutorProvider backgroundExecutorProvider; private CredentialsProvider credentialsProvider; private HeaderProvider headerProvider; private HeaderProvider internalHeaderProvider; @@ -193,7 +193,7 @@ public abstract static class Builder< /** Create a builder from a StubSettings object. */ protected Builder(StubSettings settings) { this.executorProvider = settings.executorProvider; - this.workerExecutorProvider = settings.workerExecutorProvider; + this.backgroundExecutorProvider = settings.backgroundExecutorProvider; this.transportChannelProvider = settings.transportChannelProvider; this.credentialsProvider = settings.credentialsProvider; this.headerProvider = settings.headerProvider; @@ -226,7 +226,7 @@ private static String getQuotaProjectIdFromClientContext(ClientContext clientCon protected Builder(ClientContext clientContext) { if (clientContext == null) { this.executorProvider = null; - this.workerExecutorProvider = InstantiatingExecutorProvider.newBuilder().build(); + this.backgroundExecutorProvider = InstantiatingExecutorProvider.newBuilder().build(); this.transportChannelProvider = null; this.credentialsProvider = NoCredentialsProvider.create(); this.headerProvider = new NoHeaderProvider(); @@ -239,7 +239,7 @@ protected Builder(ClientContext clientContext) { this.tracerFactory = NoopApiTracerFactory.getInstance(); } else { this.executorProvider = FixedExecutorProvider.create(clientContext.getExecutor()); - this.workerExecutorProvider = FixedExecutorProvider.create(clientContext.getExecutor()); + this.backgroundExecutorProvider = FixedExecutorProvider.create(clientContext.getExecutor()); this.transportChannelProvider = FixedTransportChannelProvider.create(clientContext.getTransportChannel()); this.credentialsProvider = FixedCredentialsProvider.create(clientContext.getCredentials()); @@ -266,8 +266,13 @@ protected B self() { } /** - * @deprecated Please use {@link #setWorkerExecutorProvider(ExecutorProvider)} for setting - * executor to use for running asynchronous API call logic. To set executor for {@link + * Sets the ExecutorProvider to use for getting the executor to use for running asynchronous API + * call logic (such as retries and long-running operations), and also to pass to the transport + * settings if an executor is needed for the transport and it doesn't have its own executor + * provider. + * + * @deprecated Please use {@link #setBackgroundExecutorProvider(ExecutorProvider)} for setting + * executor to use for running scheduled API call logic. To set executor for {@link * TransportChannelProvider}, please use {@link * TransportChannelProvider#withExecutor(Executor)} instead. */ @@ -279,16 +284,16 @@ public B setExecutorProvider(ExecutorProvider executorProvider) { // executor. After this method is deprecated, TransportChannelProvider's executor can only be // set with TransportChannelProvider#withExecutor. this.executorProvider = executorProvider; - this.workerExecutorProvider = executorProvider; + this.backgroundExecutorProvider = executorProvider; return self(); } /** - * Sets the executor to use for running asynchronous API call logic (such as retries and + * Sets the executor to use for running scheduled API call logic (such as retries and * long-running operations). */ - public B setWorkerExecutorProvider(ExecutorProvider workerExecutorProvider) { - this.workerExecutorProvider = workerExecutorProvider; + public B setBackgroundExecutorProvider(ExecutorProvider backgroundExecutorProvider) { + this.backgroundExecutorProvider = backgroundExecutorProvider; return self(); } @@ -395,15 +400,15 @@ public B setTracerFactory(@Nonnull ApiTracerFactory tracerFactory) { return self(); } - /** @deprecated Please use {@link #getWorkerExecutorProvider()}. */ + /** @deprecated Please use {@link #getBackgroundExecutorProvider()}. */ @Deprecated public ExecutorProvider getExecutorProvider() { return executorProvider; } /** Gets the ExecutorProvider that was previously set on this Builder. */ - public ExecutorProvider getWorkerExecutorProvider() { - return workerExecutorProvider; + public ExecutorProvider getBackgroundExecutorProvider() { + return backgroundExecutorProvider; } /** Gets the TransportProvider that was previously set on this Builder. */ @@ -475,7 +480,7 @@ protected static void applyToAllUnaryMethods( public String toString() { return MoreObjects.toStringHelper(this) .add("executorProvider", executorProvider) - .add("workerExecutorProvider", workerExecutorProvider) + .add("workerExecutorProvider", backgroundExecutorProvider) .add("transportChannelProvider", transportChannelProvider) .add("credentialsProvider", credentialsProvider) .add("headerProvider", headerProvider) diff --git a/gax/src/test/java/com/google/api/gax/rpc/ClientSettingsTest.java b/gax/src/test/java/com/google/api/gax/rpc/ClientSettingsTest.java index 87a73b131..7aba2eba0 100644 --- a/gax/src/test/java/com/google/api/gax/rpc/ClientSettingsTest.java +++ b/gax/src/test/java/com/google/api/gax/rpc/ClientSettingsTest.java @@ -111,7 +111,7 @@ public void testEmptyBuilder() throws Exception { FakeClientSettings.Builder builder = new FakeClientSettings.Builder(); Truth.assertThat(builder.getExecutorProvider()) .isInstanceOf(InstantiatingExecutorProvider.class); - Truth.assertThat(builder.getStubSettings().getWorkerExecutorProvider()) + Truth.assertThat(builder.getStubSettings().getBackgroundExecutorProvider()) .isSameInstanceAs(builder.getExecutorProvider()); Truth.assertThat(builder.getTransportChannelProvider()).isNull(); Truth.assertThat(builder.getCredentialsProvider()).isInstanceOf(NoCredentialsProvider.class); @@ -126,7 +126,7 @@ public void testEmptyBuilder() throws Exception { FakeClientSettings settings = builder.build(); Truth.assertThat(settings.getExecutorProvider()) .isSameInstanceAs(builder.getExecutorProvider()); - Truth.assertThat(settings.getStubSettings().getWorkerExecutorProvider()) + Truth.assertThat(settings.getStubSettings().getBackgroundExecutorProvider()) .isSameInstanceAs(settings.getExecutorProvider()); Truth.assertThat(settings.getTransportChannelProvider()) .isSameInstanceAs(builder.getTransportChannelProvider()); @@ -177,7 +177,7 @@ public void testBuilder() throws Exception { builder.setQuotaProjectId(quotaProjectId); Truth.assertThat(builder.getExecutorProvider()).isSameInstanceAs(executorProvider); - Truth.assertThat(builder.getStubSettings().getWorkerExecutorProvider()) + Truth.assertThat(builder.getStubSettings().getBackgroundExecutorProvider()) .isSameInstanceAs(executorProvider); Truth.assertThat(builder.getTransportChannelProvider()).isSameInstanceAs(transportProvider); Truth.assertThat(builder.getCredentialsProvider()).isSameInstanceAs(credentialsProvider); @@ -229,7 +229,7 @@ public void testBuilderFromClientContext() throws Exception { FakeClientSettings.Builder builder = new FakeClientSettings.Builder(clientContext); Truth.assertThat(builder.getExecutorProvider()).isInstanceOf(FixedExecutorProvider.class); - Truth.assertThat(builder.getStubSettings().getWorkerExecutorProvider()) + Truth.assertThat(builder.getStubSettings().getBackgroundExecutorProvider()) .isSameInstanceAs(builder.getExecutorProvider()); Truth.assertThat(builder.getTransportChannelProvider()) .isInstanceOf(FixedTransportChannelProvider.class); @@ -271,7 +271,7 @@ public void testBuilderFromSettings() throws Exception { FakeClientSettings.Builder newBuilder = new FakeClientSettings.Builder(settings); Truth.assertThat(newBuilder.getExecutorProvider()).isSameInstanceAs(executorProvider); - Truth.assertThat(newBuilder.getStubSettings().getWorkerExecutorProvider()) + Truth.assertThat(newBuilder.getStubSettings().getBackgroundExecutorProvider()) .isSameInstanceAs(executorProvider); Truth.assertThat(newBuilder.getTransportChannelProvider()).isSameInstanceAs(transportProvider); Truth.assertThat(newBuilder.getCredentialsProvider()).isSameInstanceAs(credentialsProvider);