23
23
import com .google .api .core .InternalApi ;
24
24
import com .google .api .core .NanoClock ;
25
25
import com .google .api .gax .core .CredentialsProvider ;
26
- import com .google .api .gax .core .ExecutorProvider ;
27
26
import com .google .api .gax .core .GaxProperties ;
28
27
import com .google .api .gax .grpc .GaxGrpcProperties ;
29
28
import com .google .api .gax .grpc .GrpcCallContext ;
185
184
import java .nio .charset .StandardCharsets ;
186
185
import java .util .Comparator ;
187
186
import java .util .HashMap ;
188
- import java .util .LinkedList ;
189
187
import java .util .List ;
190
188
import java .util .Map ;
191
189
import java .util .Map .Entry ;
200
198
import java .util .concurrent .Executors ;
201
199
import java .util .concurrent .Future ;
202
200
import java .util .concurrent .ScheduledExecutorService ;
203
- import java .util .concurrent .ScheduledThreadPoolExecutor ;
204
- import java .util .concurrent .ThreadFactory ;
205
201
import java .util .concurrent .TimeUnit ;
206
202
import java .util .stream .Collectors ;
207
203
import java .util .stream .Stream ;
211
207
/** Implementation of Cloud Spanner remote calls using Gapic libraries. */
212
208
@ InternalApi
213
209
public class GapicSpannerRpc implements SpannerRpc {
214
-
215
- /**
216
- * {@link ExecutorProvider} that keeps track of the executors that are created and shuts these
217
- * down when the {@link SpannerRpc} is closed.
218
- */
219
- private static final class ManagedInstantiatingExecutorProvider implements ExecutorProvider {
220
-
221
- // 4 Gapic clients * 4 channels per client.
222
- private static final int DEFAULT_MIN_THREAD_COUNT = 16 ;
223
- private final List <ScheduledExecutorService > executors = new LinkedList <>();
224
- private final ThreadFactory threadFactory ;
225
-
226
- private ManagedInstantiatingExecutorProvider (ThreadFactory threadFactory ) {
227
- this .threadFactory = threadFactory ;
228
- }
229
-
230
- @ Override
231
- public boolean shouldAutoClose () {
232
- return false ;
233
- }
234
-
235
- @ Override
236
- public ScheduledExecutorService getExecutor () {
237
- int numCpus = Runtime .getRuntime ().availableProcessors ();
238
- int numThreads = Math .max (DEFAULT_MIN_THREAD_COUNT , numCpus );
239
- ScheduledExecutorService executor =
240
- new ScheduledThreadPoolExecutor (numThreads , threadFactory );
241
- synchronized (this ) {
242
- executors .add (executor );
243
- }
244
- return executor ;
245
- }
246
-
247
- /** Shuts down all executors that have been created by this {@link ExecutorProvider}. */
248
- private synchronized void shutdown () {
249
- for (ScheduledExecutorService executor : executors ) {
250
- executor .shutdown ();
251
- }
252
- }
253
-
254
- private void awaitTermination () throws InterruptedException {
255
- for (ScheduledExecutorService executor : executors ) {
256
- executor .awaitTermination (10L , TimeUnit .SECONDS );
257
- }
258
- }
259
- }
260
-
261
210
private static final PathTemplate PROJECT_NAME_TEMPLATE =
262
211
PathTemplate .create ("projects/{project}" );
263
212
private static final PathTemplate OPERATION_NAME_TEMPLATE =
@@ -277,7 +226,6 @@ private void awaitTermination() throws InterruptedException {
277
226
CLIENT_LIBRARY_LANGUAGE + "/" + GaxProperties .getLibraryVersion (GapicSpannerRpc .class );
278
227
private static final String API_FILE = "grpc-gcp-apiconfig.json" ;
279
228
280
- private final ManagedInstantiatingExecutorProvider executorProvider ;
281
229
private boolean rpcIsClosed ;
282
230
private final SpannerStub spannerStub ;
283
231
private final SpannerStub partitionedDmlStub ;
@@ -356,13 +304,6 @@ public GapicSpannerRpc(final SpannerOptions options) {
356
304
this .compressorName = options .getCompressorName ();
357
305
358
306
if (initializeStubs ) {
359
- // Create a managed executor provider.
360
- this .executorProvider =
361
- new ManagedInstantiatingExecutorProvider (
362
- new ThreadFactoryBuilder ()
363
- .setDaemon (true )
364
- .setNameFormat (options .getTransportChannelExecutorThreadNameFormat ())
365
- .build ());
366
307
// First check if SpannerOptions provides a TransportChannelProvider. Create one
367
308
// with information gathered from SpannerOptions if none is provided
368
309
InstantiatingGrpcChannelProvider .Builder defaultChannelProviderBuilder =
@@ -373,11 +314,6 @@ public GapicSpannerRpc(final SpannerOptions options) {
373
314
.setMaxInboundMetadataSize (MAX_METADATA_SIZE )
374
315
.setPoolSize (options .getNumChannels ())
375
316
376
- // Before updating this method to setExecutor, please verify with a code owner on
377
- // the lowest version of gax-grpc that needs to be supported. Currently v1.47.17,
378
- // which doesn't support the setExecutor variant.
379
- .setExecutorProvider (executorProvider )
380
-
381
317
// Set a keepalive time of 120 seconds to help long running
382
318
// commit GRPC calls succeed
383
319
.setKeepAliveTime (Duration .ofSeconds (GRPC_KEEPALIVE_SECONDS ))
@@ -536,7 +472,6 @@ public <RequestT, ResponseT> UnaryCallable<RequestT, ResponseT> createUnaryCalla
536
472
this .databaseAdminStubSettings = null ;
537
473
this .spannerWatchdog = null ;
538
474
this .partitionedDmlRetrySettings = null ;
539
- this .executorProvider = null ;
540
475
}
541
476
}
542
477
@@ -1932,15 +1867,13 @@ public void shutdown() {
1932
1867
this .instanceAdminStub .close ();
1933
1868
this .databaseAdminStub .close ();
1934
1869
this .spannerWatchdog .shutdown ();
1935
- this .executorProvider .shutdown ();
1936
1870
1937
1871
try {
1938
1872
this .spannerStub .awaitTermination (10L , TimeUnit .SECONDS );
1939
1873
this .partitionedDmlStub .awaitTermination (10L , TimeUnit .SECONDS );
1940
1874
this .instanceAdminStub .awaitTermination (10L , TimeUnit .SECONDS );
1941
1875
this .databaseAdminStub .awaitTermination (10L , TimeUnit .SECONDS );
1942
1876
this .spannerWatchdog .awaitTermination (10L , TimeUnit .SECONDS );
1943
- this .executorProvider .awaitTermination ();
1944
1877
} catch (InterruptedException e ) {
1945
1878
throw SpannerExceptionFactory .propagateInterrupt (e );
1946
1879
}
@@ -1954,7 +1887,6 @@ public void shutdownNow() {
1954
1887
this .instanceAdminStub .close ();
1955
1888
this .databaseAdminStub .close ();
1956
1889
this .spannerWatchdog .shutdown ();
1957
- this .executorProvider .shutdown ();
1958
1890
1959
1891
this .spannerStub .shutdownNow ();
1960
1892
this .partitionedDmlStub .shutdownNow ();
0 commit comments