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

Commit b1f8c43

Browse files
authored
fix: stop overriding default grpc executor (#1355)
1 parent 25a23db commit b1f8c43

File tree

11 files changed

+227
-44
lines changed

11 files changed

+227
-44
lines changed

gax-grpc/src/main/java/com/google/api/gax/grpc/InstantiatingGrpcChannelProvider.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,11 @@ private InstantiatingGrpcChannelProvider(Builder builder) {
135135
: builder.directPathServiceConfig;
136136
}
137137

138+
/**
139+
* @deprecated If executor is not set, this channel provider will create channels with default
140+
* grpc executor.
141+
*/
142+
@Deprecated
138143
@Override
139144
public boolean needsExecutor() {
140145
return executor == null;
@@ -212,9 +217,7 @@ public TransportChannelProvider withCredentials(Credentials credentials) {
212217

213218
@Override
214219
public TransportChannel getTransportChannel() throws IOException {
215-
if (needsExecutor()) {
216-
throw new IllegalStateException("getTransportChannel() called when needsExecutor() is true");
217-
} else if (needsHeaders()) {
220+
if (needsHeaders()) {
218221
throw new IllegalStateException("getTransportChannel() called when needsHeaders() is true");
219222
} else if (needsEndpoint()) {
220223
throw new IllegalStateException("getTransportChannel() called when needsEndpoint() is true");

gax-grpc/src/test/java/com/google/api/gax/grpc/testing/LocalChannelProvider.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public boolean shouldAutoClose() {
7474
return true;
7575
}
7676

77+
@Deprecated
7778
@Override
7879
public boolean needsExecutor() {
7980
return false;

gax-httpjson/src/main/java/com/google/api/gax/httpjson/InstantiatingHttpJsonChannelProvider.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ private InstantiatingHttpJsonChannelProvider(
9393
this.mtlsProvider = mtlsProvider;
9494
}
9595

96+
/**
97+
* @deprecated If executor is not set, this channel provider will create channels with default
98+
* executor defined in {@link ManagedHttpJsonChannel}.
99+
*/
100+
@Deprecated
96101
@Override
97102
public boolean needsExecutor() {
98103
return executor == null;
@@ -149,9 +154,7 @@ public String getTransportName() {
149154

150155
@Override
151156
public TransportChannel getTransportChannel() throws IOException {
152-
if (needsExecutor()) {
153-
throw new IllegalStateException("getTransportChannel() called when needsExecutor() is true");
154-
} else if (needsHeaders()) {
157+
if (needsHeaders()) {
155158
throw new IllegalStateException("getTransportChannel() called when needsHeaders() is true");
156159
} else {
157160
try {

gax-httpjson/src/main/java/com/google/api/gax/httpjson/ManagedHttpJsonChannel.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,23 @@
3737
import com.google.api.core.BetaApi;
3838
import com.google.api.core.SettableApiFuture;
3939
import com.google.api.gax.core.BackgroundResource;
40+
import com.google.api.gax.core.InstantiatingExecutorProvider;
4041
import com.google.common.base.Preconditions;
4142
import com.google.common.collect.ImmutableList;
4243
import java.io.IOException;
4344
import java.util.LinkedList;
4445
import java.util.List;
4546
import java.util.concurrent.Executor;
47+
import java.util.concurrent.ExecutorService;
4648
import java.util.concurrent.TimeUnit;
4749
import javax.annotation.Nullable;
4850

4951
/** Implementation of HttpJsonChannel which can issue http-json calls. */
5052
@BetaApi
5153
public class ManagedHttpJsonChannel implements HttpJsonChannel, BackgroundResource {
5254
private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();
55+
private static final ExecutorService DEFAULT_EXECUTOR =
56+
InstantiatingExecutorProvider.newBuilder().build().getExecutor();
5357

5458
private final Executor executor;
5559
private final String endpoint;
@@ -134,7 +138,9 @@ public boolean awaitTermination(long duration, TimeUnit unit) throws Interrupted
134138
public void close() {}
135139

136140
public static Builder newBuilder() {
137-
return new Builder().setHeaderEnhancers(new LinkedList<HttpJsonHeaderEnhancer>());
141+
return new Builder()
142+
.setHeaderEnhancers(new LinkedList<HttpJsonHeaderEnhancer>())
143+
.setExecutor(DEFAULT_EXECUTOR);
138144
}
139145

140146
public static class Builder {
@@ -147,7 +153,7 @@ public static class Builder {
147153
private Builder() {}
148154

149155
public Builder setExecutor(Executor executor) {
150-
this.executor = executor;
156+
this.executor = Preconditions.checkNotNull(executor);
151157
return this;
152158
}
153159

@@ -167,7 +173,6 @@ public Builder setHttpTransport(HttpTransport httpTransport) {
167173
}
168174

169175
public ManagedHttpJsonChannel build() {
170-
Preconditions.checkNotNull(executor);
171176
Preconditions.checkNotNull(endpoint);
172177
return new ManagedHttpJsonChannel(
173178
executor, endpoint, jsonFactory, headerEnhancers, httpTransport);

gax/src/main/java/com/google/api/gax/rpc/ClientContext.java

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
import java.util.List;
5151
import java.util.Map;
5252
import java.util.Set;
53-
import java.util.concurrent.Executor;
5453
import java.util.concurrent.Executors;
5554
import java.util.concurrent.ScheduledExecutorService;
5655
import javax.annotation.Nonnull;
@@ -73,6 +72,10 @@ public abstract class ClientContext {
7372
*/
7473
public abstract List<BackgroundResource> getBackgroundResources();
7574

75+
/**
76+
* Gets the executor to use for running scheduled API call logic (such as retries and long-running
77+
* operations).
78+
*/
7679
public abstract ScheduledExecutorService getExecutor();
7780

7881
@Nullable
@@ -163,8 +166,8 @@ static String getEndpoint(
163166
public static ClientContext create(StubSettings settings) throws IOException {
164167
ApiClock clock = settings.getClock();
165168

166-
ExecutorProvider executorProvider = settings.getExecutorProvider();
167-
final ScheduledExecutorService executor = executorProvider.getExecutor();
169+
ExecutorProvider backgroundExecutorProvider = settings.getBackgroundExecutorProvider();
170+
final ScheduledExecutorService backgroundExecutor = backgroundExecutorProvider.getExecutor();
168171

169172
Credentials credentials = settings.getCredentialsProvider().getCredentials();
170173

@@ -177,8 +180,11 @@ public static ClientContext create(StubSettings settings) throws IOException {
177180
}
178181

179182
TransportChannelProvider transportChannelProvider = settings.getTransportChannelProvider();
180-
if (transportChannelProvider.needsExecutor()) {
181-
transportChannelProvider = transportChannelProvider.withExecutor((Executor) executor);
183+
// After needsExecutor and StubSettings#setExecutorProvider are deprecated, transport channel
184+
// executor can only be set from TransportChannelProvider#withExecutor directly, and a provider
185+
// will have a default executor if it needs one.
186+
if (transportChannelProvider.needsExecutor() && settings.getExecutorProvider() != null) {
187+
transportChannelProvider = transportChannelProvider.withExecutor(backgroundExecutor);
182188
}
183189
Map<String, String> headers = getHeadersFromSettings(settings);
184190
if (transportChannelProvider.needsHeaders()) {
@@ -216,7 +222,7 @@ public static ClientContext create(StubSettings settings) throws IOException {
216222
watchdogProvider = watchdogProvider.withClock(clock);
217223
}
218224
if (watchdogProvider.needsExecutor()) {
219-
watchdogProvider = watchdogProvider.withExecutor(executor);
225+
watchdogProvider = watchdogProvider.withExecutor(backgroundExecutor);
220226
}
221227
watchdog = watchdogProvider.getWatchdog();
222228
}
@@ -226,16 +232,16 @@ public static ClientContext create(StubSettings settings) throws IOException {
226232
if (transportChannelProvider.shouldAutoClose()) {
227233
backgroundResources.add(transportChannel);
228234
}
229-
if (executorProvider.shouldAutoClose()) {
230-
backgroundResources.add(new ExecutorAsBackgroundResource(executor));
235+
if (backgroundExecutorProvider.shouldAutoClose()) {
236+
backgroundResources.add(new ExecutorAsBackgroundResource(backgroundExecutor));
231237
}
232238
if (watchdogProvider != null && watchdogProvider.shouldAutoClose()) {
233239
backgroundResources.add(watchdog);
234240
}
235241

236242
return newBuilder()
237243
.setBackgroundResources(backgroundResources.build())
238-
.setExecutor(executor)
244+
.setExecutor(backgroundExecutor)
239245
.setCredentials(credentials)
240246
.setTransportChannel(transportChannel)
241247
.setHeaders(ImmutableMap.copyOf(settings.getHeaderProvider().getHeaders()))
@@ -289,6 +295,10 @@ public abstract static class Builder {
289295

290296
public abstract Builder setBackgroundResources(List<BackgroundResource> backgroundResources);
291297

298+
/**
299+
* Sets the executor to use for running scheduled API call logic (such as retries and
300+
* long-running operations).
301+
*/
292302
public abstract Builder setExecutor(ScheduledExecutorService value);
293303

294304
public abstract Builder setCredentials(Credentials value);

gax/src/main/java/com/google/api/gax/rpc/ClientSettings.java

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import com.google.api.gax.core.ExecutorProvider;
3737
import com.google.common.base.MoreObjects;
3838
import java.io.IOException;
39+
import java.util.concurrent.Executor;
3940
import javax.annotation.Nonnull;
4041
import javax.annotation.Nullable;
4142
import org.threeten.bp.Duration;
@@ -63,10 +64,16 @@ public final StubSettings getStubSettings() {
6364
return stubSettings;
6465
}
6566

67+
/** @deprecated Please use {@link #getBackgroundExecutorProvider()} */
68+
@Deprecated
6669
public final ExecutorProvider getExecutorProvider() {
6770
return stubSettings.getExecutorProvider();
6871
}
6972

73+
public final ExecutorProvider getBackgroundExecutorProvider() {
74+
return stubSettings.getBackgroundExecutorProvider();
75+
}
76+
7077
public final TransportChannelProvider getTransportChannelProvider() {
7178
return stubSettings.getTransportChannelProvider();
7279
}
@@ -112,6 +119,7 @@ public final Duration getWatchdogCheckInterval() {
112119
public String toString() {
113120
return MoreObjects.toStringHelper(this)
114121
.add("executorProvider", getExecutorProvider())
122+
.add("backgroundExecutorProvider", getBackgroundExecutorProvider())
115123
.add("transportChannelProvider", getTransportChannelProvider())
116124
.add("credentialsProvider", getCredentialsProvider())
117125
.add("headerProvider", getHeaderProvider())
@@ -159,9 +167,27 @@ protected StubSettings.Builder getStubSettings() {
159167
* call logic (such as retries and long-running operations), and also to pass to the transport
160168
* settings if an executor is needed for the transport and it doesn't have its own executor
161169
* provider.
170+
*
171+
* @deprecated Please use {@link #setBackgroundExecutorProvider(ExecutorProvider)} for setting
172+
* executor to use for running scheduled API call logic. To set executor for {@link
173+
* TransportChannelProvider}, please use {@link
174+
* TransportChannelProvider#withExecutor(Executor)} instead.
162175
*/
176+
@Deprecated
163177
public B setExecutorProvider(ExecutorProvider executorProvider) {
164178
stubSettings.setExecutorProvider(executorProvider);
179+
stubSettings.setBackgroundExecutorProvider(executorProvider);
180+
return self();
181+
}
182+
183+
/**
184+
* Sets the ExecutorProvider to use for getting the executor to use for running scheduled API
185+
* call logic (such as retries and long-running operations). This will not set the executor in
186+
* {@link TransportChannelProvider}. To set executor for {@link TransportChannelProvider},
187+
* please use {@link TransportChannelProvider#withExecutor(Executor)}.
188+
*/
189+
public B setBackgroundExecutorProvider(ExecutorProvider executorProvider) {
190+
stubSettings.setBackgroundExecutorProvider(executorProvider);
165191
return self();
166192
}
167193

@@ -238,11 +264,29 @@ public B setWatchdogCheckInterval(@Nullable Duration checkInterval) {
238264
return self();
239265
}
240266

241-
/** Gets the ExecutorProvider that was previously set on this Builder. */
267+
/**
268+
* Gets the ExecutorProvider that was previously set on this Builder. This ExecutorProvider is
269+
* to use for running asynchronous API call logic (such as retries and long-running operations),
270+
* and also to pass to the transport settings if an executor is needed for the transport and it
271+
* doesn't have its own executor provider.
272+
*
273+
* @deprecated Please use {@link #getBackgroundExecutorProvider()} for getting the executor
274+
* provider that's used for running scheduled API call logic.
275+
*/
276+
@Deprecated
242277
public ExecutorProvider getExecutorProvider() {
243278
return stubSettings.getExecutorProvider();
244279
}
245280

281+
/**
282+
* Gets the ExecutorProvider that was previously set on this Builder. This ExecutorProvider is
283+
* to use for running asynchronous API call logic (such as retries and long-running operations).
284+
* This ExecutorProvider is not used to set the executor in {@link TransportChannelProvider}.
285+
*/
286+
public ExecutorProvider getBackgroundExecutorProvider() {
287+
return stubSettings.getBackgroundExecutorProvider();
288+
}
289+
246290
/** Gets the TransportProvider that was previously set on this Builder. */
247291
public TransportChannelProvider getTransportChannelProvider() {
248292
return stubSettings.getTransportChannelProvider();
@@ -303,6 +347,7 @@ protected static void applyToAllUnaryMethods(
303347
public String toString() {
304348
return MoreObjects.toStringHelper(this)
305349
.add("executorProvider", getExecutorProvider())
350+
.add("backgroundExecutorProvider", getBackgroundExecutorProvider())
306351
.add("transportChannelProvider", getTransportChannelProvider())
307352
.add("credentialsProvider", getCredentialsProvider())
308353
.add("headerProvider", getHeaderProvider())

0 commit comments

Comments
 (0)