1717package com .google .cloud .spanner .spi .v1 ;
1818
1919import com .google .api .core .InternalApi ;
20- import com .google .api .gax .grpc .GrpcTransportChannel ;
2120import com .google .api .gax .grpc .InstantiatingGrpcChannelProvider ;
2221import com .google .api .gax .grpc .InstantiatingGrpcChannelProvider .Builder ;
23- import com .google .api .gax .rpc .TransportChannelProvider ;
2422import com .google .cloud .spanner .ErrorCode ;
2523import com .google .cloud .spanner .SpannerExceptionFactory ;
2624import com .google .common .annotations .VisibleForTesting ;
@@ -92,13 +90,14 @@ public ChannelEndpoint get(String address) {
9290 try {
9391 // Create a new provider with the same config but different endpoint.
9492 // This is thread-safe as withEndpoint() returns a new provider instance.
95- TransportChannelProvider newProvider = createProviderWithAuthorityOverride (addr );
93+ InstantiatingGrpcChannelProvider newProvider =
94+ createProviderWithAuthorityOverride (addr );
9695 GrpcChannelEndpoint endpoint = new GrpcChannelEndpoint (addr , newProvider );
97- logger .log (Level .INFO , "Location-aware endpoint created for address: {0}" , addr );
96+ logger .log (Level .FINE , "Location-aware endpoint created for address: {0}" , addr );
9897 return endpoint ;
9998 } catch (IOException e ) {
10099 logger .log (
101- Level .WARNING , "Failed to create location-aware endpoint for address: " + addr , e );
100+ Level .FINE , "Failed to create location-aware endpoint for address: " + addr , e );
102101 throw SpannerExceptionFactory .newSpannerException (
103102 ErrorCode .INTERNAL , "Failed to create channel for address: " + addr , e );
104103 }
@@ -111,7 +110,7 @@ public ChannelEndpoint getIfPresent(String address) {
111110 return servers .get (address );
112111 }
113112
114- private TransportChannelProvider createProviderWithAuthorityOverride (String address ) {
113+ private InstantiatingGrpcChannelProvider createProviderWithAuthorityOverride (String address ) {
115114 InstantiatingGrpcChannelProvider endpointProvider =
116115 (InstantiatingGrpcChannelProvider ) baseProvider .withEndpoint (address );
117116 if (Objects .equals (defaultAuthority , address )) {
@@ -191,15 +190,19 @@ static class GrpcChannelEndpoint implements ChannelEndpoint {
191190 * @param provider the channel provider (must be a gRPC provider)
192191 * @throws IOException if the channel cannot be created
193192 */
194- GrpcChannelEndpoint (String address , TransportChannelProvider provider ) throws IOException {
193+ GrpcChannelEndpoint (String address , InstantiatingGrpcChannelProvider provider )
194+ throws IOException {
195195 this .address = address ;
196- TransportChannelProvider readyProvider = provider ;
196+ // Build a raw ManagedChannel directly instead of going through getTransportChannel(),
197+ // which wraps the channel in a ChannelPool that does not support getState().
198+ // Location-aware routing needs getState() to check channel connectivity.
199+ InstantiatingGrpcChannelProvider readyProvider = provider ;
197200 if (provider .needsHeaders ()) {
198- readyProvider = provider .withHeaders (java .util .Collections .emptyMap ());
201+ readyProvider =
202+ (InstantiatingGrpcChannelProvider )
203+ provider .withHeaders (java .util .Collections .emptyMap ());
199204 }
200- GrpcTransportChannel transportChannel =
201- (GrpcTransportChannel ) readyProvider .getTransportChannel ();
202- this .channel = (ManagedChannel ) transportChannel .getChannel ();
205+ this .channel = readyProvider .createDecoratedChannelBuilder ().build ();
203206 }
204207
205208 /**
@@ -240,7 +243,7 @@ public boolean isHealthy() {
240243 return ready ;
241244 } catch (UnsupportedOperationException e ) {
242245 logger .log (
243- Level .WARNING ,
246+ Level .FINE ,
244247 "getState(false) unsupported for location-aware endpoint {0}, treating as not ready" ,
245248 address );
246249 return false ;
0 commit comments