Skip to content
This repository has been archived by the owner on Sep 26, 2023. It is now read-only.

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
mutianf committed Jun 3, 2021
1 parent 5e426cf commit fca5464
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 38 deletions.
23 changes: 14 additions & 9 deletions gax/src/main/java/com/google/api/gax/rpc/ClientContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,12 @@ static String getEndpoint(
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();

Expand Down Expand Up @@ -223,7 +222,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();
}
Expand All @@ -233,16 +232,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()))
Expand Down
41 changes: 37 additions & 4 deletions gax/src/main/java/com/google/api/gax/rpc/ClientSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -239,9 +261,20 @@ public B setWatchdogCheckInterval(@Nullable Duration checkInterval) {
return self();
}

/** Gets the ExecutorProvider that was previously set on this Builder. */
@Deprecated
/**
* Gets the ExecutorProvider that was previously set on this Builder.
*
* @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. */
Expand Down
45 changes: 25 additions & 20 deletions gax/src/main/java/com/google/api/gax/rpc/StubSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public abstract class StubSettings<SettingsT extends StubSettings<SettingsT>> {
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;
Expand All @@ -89,7 +89,7 @@ public abstract class StubSettings<SettingsT extends StubSettings<SettingsT>> {
/** 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;
Expand All @@ -104,14 +104,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() {
Expand Down Expand Up @@ -178,7 +178,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)
Expand All @@ -200,7 +200,7 @@ public abstract static class Builder<
SettingsT extends StubSettings<SettingsT>, B extends Builder<SettingsT, B>> {

private ExecutorProvider executorProvider;
private ExecutorProvider workerExecutorProvider;
private ExecutorProvider backgroundExecutorProvider;
private CredentialsProvider credentialsProvider;
private HeaderProvider headerProvider;
private HeaderProvider internalHeaderProvider;
Expand All @@ -224,7 +224,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;
Expand Down Expand Up @@ -259,7 +259,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();
Expand All @@ -273,7 +273,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());
Expand Down Expand Up @@ -303,8 +303,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.
*/
Expand All @@ -316,16 +321,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();
}

Expand Down Expand Up @@ -446,15 +451,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. */
Expand Down Expand Up @@ -530,7 +535,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)
Expand Down
10 changes: 5 additions & 5 deletions gax/src/test/java/com/google/api/gax/rpc/ClientSettingsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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());
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit fca5464

Please sign in to comment.