190190import com .google .spanner .v1 .SpannerGrpc ;
191191import com .google .spanner .v1 .Transaction ;
192192import io .grpc .CallCredentials ;
193- import io .grpc .CallOptions ;
194- import io .grpc .Channel ;
195- import io .grpc .ClientCall ;
196- import io .grpc .ClientInterceptor ;
197193import io .grpc .Context ;
198194import io .grpc .ForwardingChannelBuilder2 ;
199195import io .grpc .ManagedChannel ;
200196import io .grpc .ManagedChannelBuilder ;
201197import io .grpc .MethodDescriptor ;
202- import io .grpc .NameResolver ;
203- import io .grpc .StatusRuntimeException ;
204198import io .grpc .auth .MoreCallCredentials ;
205- import io .grpc .netty .shaded .io .grpc .netty .NettyChannelBuilder ;
206- import io .grpc .netty .shaded .io .netty .channel .nio .NioEventLoopGroup ;
207- import io .grpc .netty .shaded .io .netty .channel .socket .nio .NioSocketChannel ;
208199import java .io .IOException ;
209200import java .io .UnsupportedEncodingException ;
210201import java .net .URLDecoder ;
@@ -363,20 +354,29 @@ public GapicSpannerRpc(final SpannerOptions options) {
363354
364355 if (initializeStubs ) {
365356 CredentialsProvider credentialsProvider =
366- GrpcTransportOptions .setUpCredentialsProvider (options );
367-
368- InstantiatingGrpcChannelProvider .Builder defaultChannelProviderBuilder = getDefaultChannelProviderBuilder (options , headerProviderWithUserAgent , isEnableDirectAccess );
369-
370- if (options .getChannelProvider () == null && isEnableDirectAccess && isEnableGcpFallbackEnv ()) {
371- InstantiatingGrpcChannelProvider .Builder cloudPathProviderBuilder = getDefaultChannelProviderBuilder (options , headerProviderWithUserAgent , /*isEnableDirectAccess=*/ false );
357+ GrpcTransportOptions .setUpCredentialsProvider (options );
358+
359+ InstantiatingGrpcChannelProvider .Builder defaultChannelProviderBuilder =
360+ getDefaultChannelProviderBuilder (
361+ options , headerProviderWithUserAgent , isEnableDirectAccess );
362+
363+ if (options .getChannelProvider () == null
364+ && isEnableDirectAccess
365+ && isEnableGcpFallbackEnv ()) {
366+ InstantiatingGrpcChannelProvider .Builder cloudPathProviderBuilder =
367+ getDefaultChannelProviderBuilder (
368+ options , headerProviderWithUserAgent , /* isEnableDirectAccess= */ false );
372369 cloudPathProviderBuilder .setAttemptDirectPath (false );
373370
374371 final AtomicReference <ManagedChannelBuilder > cloudPathBuilderRef = new AtomicReference <>();
375- cloudPathProviderBuilder .setChannelConfigurator (builder -> {
376- cloudPathBuilderRef .set (builder );
377- return builder ;
378- });
372+ cloudPathProviderBuilder .setChannelConfigurator (
373+ builder -> {
374+ cloudPathBuilderRef .set (builder );
375+ return builder ;
376+ });
379377
378+ // Build the cloudPathProvider to extract the builder which will be provided to
379+ // FallbackChannelBuilder.
380380 try (TransportChannel ignored = cloudPathProviderBuilder .build ().getTransportChannel ()) {
381381 } catch (Exception e ) {
382382 throw asSpannerException (e );
@@ -390,50 +390,52 @@ public GapicSpannerRpc(final SpannerOptions options) {
390390 try {
391391 Credentials credentials = credentialsProvider .getCredentials ();
392392 if (credentials != null ) {
393- cloudPathBuilder .intercept (new ClientInterceptor () {
394- @ Override
395- public <ReqT , RespT > ClientCall <ReqT , RespT > interceptCall (
396- MethodDescriptor <ReqT , RespT > method , CallOptions callOptions , Channel next ) {
397- return next .newCall (method , callOptions .withCallCredentials (MoreCallCredentials .from (credentials )));
398- }
399- });
393+ cloudPathBuilder .intercept (
394+ new ClientInterceptor () {
395+ @ Override
396+ public <ReqT , RespT > ClientCall <ReqT , RespT > interceptCall (
397+ MethodDescriptor <ReqT , RespT > method , CallOptions callOptions , Channel next ) {
398+ return next .newCall (
399+ method ,
400+ callOptions .withCallCredentials (MoreCallCredentials .from (credentials )));
401+ }
402+ });
400403 }
401404 } catch (Exception e ) {
402405 throw asSpannerException (e );
403406 }
404407
405- defaultChannelProviderBuilder .setChannelConfigurator (directPathBuilder -> {
406- String jsonApiConfig = parseGrpcGcpApiConfig ();
407- GcpManagedChannelOptions gcpOptions = options .getGrpcGcpOptions ();
408- if (gcpOptions == null ) {
409- gcpOptions = GcpManagedChannelOptions .newBuilder ().build ();
410- }
408+ defaultChannelProviderBuilder .setChannelConfigurator (
409+ directPathBuilder -> {
410+ String jsonApiConfig = parseGrpcGcpApiConfig ();
411+ GcpManagedChannelOptions gcpOptions = options .getGrpcGcpOptions ();
412+ if (gcpOptions == null ) {
413+ gcpOptions = GcpManagedChannelOptions .newBuilder ().build ();
414+ }
411415
412- GcpManagedChannelBuilder primaryGcpBuilder =
413- GcpManagedChannelBuilder .forDelegateBuilder (directPathBuilder )
414- .withApiConfigJsonString (jsonApiConfig )
415- .withOptions (gcpOptions );
416-
417- GcpManagedChannelBuilder fallbackGcpBuilder =
418- GcpManagedChannelBuilder .forDelegateBuilder (cloudPathBuilder )
419- .withApiConfigJsonString (jsonApiConfig )
420- .withOptions (gcpOptions );
421-
422- GcpFallbackOpenTelemetry fallbackTelemetry = GcpFallbackOpenTelemetry .newBuilder ()
423- .withSdk (options .getOpenTelemetry ())
424- .build ();
425-
426- return new FallbackChannelBuilder (
427- primaryGcpBuilder ,
428- fallbackGcpBuilder ,
429- GcpFallbackChannelOptions .newBuilder ()
430- .setPrimaryChannelName ("directpath" )
431- .setFallbackChannelName ("cloudpath" )
432- .setMinFailedCalls (1 )
433- .setGcpFallbackOpenTelemetry (fallbackTelemetry )
434- .build ()
435- );
436- });
416+ GcpManagedChannelBuilder primaryGcpBuilder =
417+ GcpManagedChannelBuilder .forDelegateBuilder (directPathBuilder )
418+ .withApiConfigJsonString (jsonApiConfig )
419+ .withOptions (gcpOptions );
420+
421+ GcpManagedChannelBuilder fallbackGcpBuilder =
422+ GcpManagedChannelBuilder .forDelegateBuilder (cloudPathBuilder )
423+ .withApiConfigJsonString (jsonApiConfig )
424+ .withOptions (gcpOptions );
425+
426+ GcpFallbackOpenTelemetry fallbackTelemetry =
427+ GcpFallbackOpenTelemetry .newBuilder ().withSdk (options .getOpenTelemetry ()).build ();
428+
429+ return new FallbackChannelBuilder (
430+ primaryGcpBuilder ,
431+ fallbackGcpBuilder ,
432+ GcpFallbackChannelOptions .newBuilder ()
433+ .setPrimaryChannelName ("directpath" )
434+ .setFallbackChannelName ("cloudpath" )
435+ .setMinFailedCalls (1 )
436+ .setGcpFallbackOpenTelemetry (fallbackTelemetry )
437+ .build ());
438+ });
437439 }
438440
439441 // First check if SpannerOptions provides a TransportChannelProvider. Create one
@@ -614,7 +616,10 @@ private static String parseGrpcGcpApiConfig() {
614616 }
615617 }
616618
617- private InstantiatingGrpcChannelProvider .Builder getDefaultChannelProviderBuilder (final SpannerOptions options , final HeaderProvider headerProviderWithUserAgent , Boolean isEnableDirectAccess ) {
619+ private InstantiatingGrpcChannelProvider .Builder getDefaultChannelProviderBuilder (
620+ final SpannerOptions options ,
621+ final HeaderProvider headerProviderWithUserAgent ,
622+ boolean isEnableDirectAccess ) {
618623 InstantiatingGrpcChannelProvider .Builder defaultChannelProviderBuilder =
619624 InstantiatingGrpcChannelProvider .newBuilder ()
620625 .setChannelConfigurator (options .getChannelConfigurator ())
@@ -2412,7 +2417,8 @@ private static Duration systemProperty(String name, int defaultValue) {
24122417 }
24132418
24142419 // Wrapper class to build the GcpFallbackChannel using GAX's configuration
2415- private static class FallbackChannelBuilder extends ForwardingChannelBuilder2 <FallbackChannelBuilder > {
2420+ private static class FallbackChannelBuilder
2421+ extends ForwardingChannelBuilder2 <FallbackChannelBuilder > {
24162422 private final GcpFallbackChannelOptions options ;
24172423
24182424 private final GcpManagedChannelBuilder primaryGcpBuilder ;
@@ -2437,8 +2443,8 @@ protected ManagedChannelBuilder<?> delegate() {
24372443 }
24382444
24392445 /**
2440- * Overrides the build method to return our custom GcpFallbackChannel
2441- * instead of a standard gRPC channel.
2446+ * Overrides the build method to return our custom GcpFallbackChannel instead of a standard gRPC
2447+ * channel.
24422448 */
24432449 @ Override
24442450 public ManagedChannel build () {
0 commit comments