2323import com .google .api .core .InternalApi ;
2424import com .google .api .core .NanoClock ;
2525import com .google .api .gax .core .CredentialsProvider ;
26- import com .google .api .gax .core .ExecutorProvider ;
2726import com .google .api .gax .core .GaxProperties ;
2827import com .google .api .gax .grpc .GaxGrpcProperties ;
2928import com .google .api .gax .grpc .GrpcCallContext ;
185184import java .nio .charset .StandardCharsets ;
186185import java .util .Comparator ;
187186import java .util .HashMap ;
188- import java .util .LinkedList ;
189187import java .util .List ;
190188import java .util .Map ;
191189import java .util .Map .Entry ;
200198import java .util .concurrent .Executors ;
201199import java .util .concurrent .Future ;
202200import java .util .concurrent .ScheduledExecutorService ;
203- import java .util .concurrent .ScheduledThreadPoolExecutor ;
204- import java .util .concurrent .ThreadFactory ;
205201import java .util .concurrent .TimeUnit ;
206202import java .util .stream .Collectors ;
207203import java .util .stream .Stream ;
211207/** Implementation of Cloud Spanner remote calls using Gapic libraries. */
212208@ InternalApi
213209public 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-
261210 private static final PathTemplate PROJECT_NAME_TEMPLATE =
262211 PathTemplate .create ("projects/{project}" );
263212 private static final PathTemplate OPERATION_NAME_TEMPLATE =
@@ -277,7 +226,6 @@ private void awaitTermination() throws InterruptedException {
277226 CLIENT_LIBRARY_LANGUAGE + "/" + GaxProperties .getLibraryVersion (GapicSpannerRpc .class );
278227 private static final String API_FILE = "grpc-gcp-apiconfig.json" ;
279228
280- private final ManagedInstantiatingExecutorProvider executorProvider ;
281229 private boolean rpcIsClosed ;
282230 private final SpannerStub spannerStub ;
283231 private final SpannerStub partitionedDmlStub ;
@@ -356,13 +304,6 @@ public GapicSpannerRpc(final SpannerOptions options) {
356304 this .compressorName = options .getCompressorName ();
357305
358306 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 ());
366307 // First check if SpannerOptions provides a TransportChannelProvider. Create one
367308 // with information gathered from SpannerOptions if none is provided
368309 InstantiatingGrpcChannelProvider .Builder defaultChannelProviderBuilder =
@@ -373,11 +314,6 @@ public GapicSpannerRpc(final SpannerOptions options) {
373314 .setMaxInboundMetadataSize (MAX_METADATA_SIZE )
374315 .setPoolSize (options .getNumChannels ())
375316
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-
381317 // Set a keepalive time of 120 seconds to help long running
382318 // commit GRPC calls succeed
383319 .setKeepAliveTime (Duration .ofSeconds (GRPC_KEEPALIVE_SECONDS ))
@@ -536,7 +472,6 @@ public <RequestT, ResponseT> UnaryCallable<RequestT, ResponseT> createUnaryCalla
536472 this .databaseAdminStubSettings = null ;
537473 this .spannerWatchdog = null ;
538474 this .partitionedDmlRetrySettings = null ;
539- this .executorProvider = null ;
540475 }
541476 }
542477
@@ -1932,15 +1867,13 @@ public void shutdown() {
19321867 this .instanceAdminStub .close ();
19331868 this .databaseAdminStub .close ();
19341869 this .spannerWatchdog .shutdown ();
1935- this .executorProvider .shutdown ();
19361870
19371871 try {
19381872 this .spannerStub .awaitTermination (10L , TimeUnit .SECONDS );
19391873 this .partitionedDmlStub .awaitTermination (10L , TimeUnit .SECONDS );
19401874 this .instanceAdminStub .awaitTermination (10L , TimeUnit .SECONDS );
19411875 this .databaseAdminStub .awaitTermination (10L , TimeUnit .SECONDS );
19421876 this .spannerWatchdog .awaitTermination (10L , TimeUnit .SECONDS );
1943- this .executorProvider .awaitTermination ();
19441877 } catch (InterruptedException e ) {
19451878 throw SpannerExceptionFactory .propagateInterrupt (e );
19461879 }
@@ -1954,7 +1887,6 @@ public void shutdownNow() {
19541887 this .instanceAdminStub .close ();
19551888 this .databaseAdminStub .close ();
19561889 this .spannerWatchdog .shutdown ();
1957- this .executorProvider .shutdown ();
19581890
19591891 this .spannerStub .shutdownNow ();
19601892 this .partitionedDmlStub .shutdownNow ();
0 commit comments