|
57 | 57 | import com.google.api.gax.rpc.UnavailableException; |
58 | 58 | import com.google.api.gax.rpc.WatchdogProvider; |
59 | 59 | import com.google.api.pathtemplate.PathTemplate; |
| 60 | +import com.google.auth.Credentials; |
60 | 61 | import com.google.cloud.RetryHelper; |
61 | 62 | import com.google.cloud.RetryHelper.RetryHelperException; |
62 | 63 | import com.google.cloud.grpc.GcpManagedChannel; |
|
209 | 210 | import java.util.concurrent.ConcurrentLinkedDeque; |
210 | 211 | import java.util.concurrent.ConcurrentMap; |
211 | 212 | import java.util.concurrent.ExecutionException; |
| 213 | +import java.util.concurrent.Executor; |
212 | 214 | import java.util.concurrent.ExecutorService; |
213 | 215 | import java.util.concurrent.Executors; |
214 | 216 | import java.util.concurrent.Future; |
|
223 | 225 | public class GapicSpannerRpc implements SpannerRpc { |
224 | 226 | private static final PathTemplate PROJECT_NAME_TEMPLATE = |
225 | 227 | PathTemplate.create("projects/{project}"); |
| 228 | + private static final String EXPERIMENTAL_LOCATION_API_ENV_VAR = |
| 229 | + "GOOGLE_SPANNER_EXPERIMENTAL_LOCATION_API"; |
226 | 230 | private static final PathTemplate OPERATION_NAME_TEMPLATE = |
227 | 231 | PathTemplate.create("{database=projects/*/instances/*/databases/*}/operations/{operation}"); |
228 | 232 | private static final int MAX_MESSAGE_SIZE = 256 * 1024 * 1024; |
@@ -285,6 +289,96 @@ public class GapicSpannerRpc implements SpannerRpc { |
285 | 289 |
|
286 | 290 | private final GrpcCallContext baseGrpcCallContext; |
287 | 291 |
|
| 292 | + private static final class KeyAwareTransportChannelProvider implements TransportChannelProvider { |
| 293 | + private final InstantiatingGrpcChannelProvider baseProvider; |
| 294 | + |
| 295 | + private KeyAwareTransportChannelProvider(InstantiatingGrpcChannelProvider.Builder builder) { |
| 296 | + this.baseProvider = builder.build(); |
| 297 | + } |
| 298 | + |
| 299 | + private KeyAwareTransportChannelProvider(InstantiatingGrpcChannelProvider baseProvider) { |
| 300 | + this.baseProvider = baseProvider; |
| 301 | + } |
| 302 | + |
| 303 | + @Override |
| 304 | + public GrpcTransportChannel getTransportChannel() throws IOException { |
| 305 | + return GrpcTransportChannel.newBuilder() |
| 306 | + .setManagedChannel(KeyAwareChannel.create(baseProvider)) |
| 307 | + .build(); |
| 308 | + } |
| 309 | + |
| 310 | + @Override |
| 311 | + public String getTransportName() { |
| 312 | + return baseProvider.getTransportName(); |
| 313 | + } |
| 314 | + |
| 315 | + @Override |
| 316 | + public boolean needsEndpoint() { |
| 317 | + return baseProvider.needsEndpoint(); |
| 318 | + } |
| 319 | + |
| 320 | + @Override |
| 321 | + public boolean needsCredentials() { |
| 322 | + return baseProvider.needsCredentials(); |
| 323 | + } |
| 324 | + |
| 325 | + @Override |
| 326 | + public boolean needsExecutor() { |
| 327 | + return baseProvider.needsExecutor(); |
| 328 | + } |
| 329 | + |
| 330 | + @Override |
| 331 | + public boolean needsHeaders() { |
| 332 | + return baseProvider.needsHeaders(); |
| 333 | + } |
| 334 | + |
| 335 | + @Override |
| 336 | + public boolean shouldAutoClose() { |
| 337 | + return baseProvider.shouldAutoClose(); |
| 338 | + } |
| 339 | + |
| 340 | + @Override |
| 341 | + public TransportChannelProvider withEndpoint(String endpoint) { |
| 342 | + return new KeyAwareTransportChannelProvider( |
| 343 | + (InstantiatingGrpcChannelProvider) baseProvider.withEndpoint(endpoint)); |
| 344 | + } |
| 345 | + |
| 346 | + @Override |
| 347 | + public TransportChannelProvider withCredentials(Credentials credentials) { |
| 348 | + return new KeyAwareTransportChannelProvider( |
| 349 | + (InstantiatingGrpcChannelProvider) baseProvider.withCredentials(credentials)); |
| 350 | + } |
| 351 | + |
| 352 | + @Override |
| 353 | + public TransportChannelProvider withHeaders(Map<String, String> headers) { |
| 354 | + return new KeyAwareTransportChannelProvider( |
| 355 | + (InstantiatingGrpcChannelProvider) baseProvider.withHeaders(headers)); |
| 356 | + } |
| 357 | + |
| 358 | + @Override |
| 359 | + public TransportChannelProvider withPoolSize(int poolSize) { |
| 360 | + return new KeyAwareTransportChannelProvider( |
| 361 | + (InstantiatingGrpcChannelProvider) baseProvider.withPoolSize(poolSize)); |
| 362 | + } |
| 363 | + |
| 364 | + @Override |
| 365 | + public TransportChannelProvider withExecutor(ScheduledExecutorService executor) { |
| 366 | + return new KeyAwareTransportChannelProvider( |
| 367 | + (InstantiatingGrpcChannelProvider) baseProvider.withExecutor(executor)); |
| 368 | + } |
| 369 | + |
| 370 | + @Override |
| 371 | + public TransportChannelProvider withExecutor(Executor executor) { |
| 372 | + return new KeyAwareTransportChannelProvider( |
| 373 | + (InstantiatingGrpcChannelProvider) baseProvider.withExecutor(executor)); |
| 374 | + } |
| 375 | + |
| 376 | + @Override |
| 377 | + public boolean acceptsPoolSize() { |
| 378 | + return baseProvider.acceptsPoolSize(); |
| 379 | + } |
| 380 | + } |
| 381 | + |
288 | 382 | public static GapicSpannerRpc create(SpannerOptions options) { |
289 | 383 | return new GapicSpannerRpc(options); |
290 | 384 | } |
@@ -393,9 +487,13 @@ public GapicSpannerRpc(final SpannerOptions options) { |
393 | 487 | // If it is enabled in options uses the channel pool provided by the gRPC-GCP extension. |
394 | 488 | maybeEnableGrpcGcpExtension(defaultChannelProviderBuilder, options); |
395 | 489 |
|
| 490 | + boolean enableLocationApi = |
| 491 | + Boolean.parseBoolean(System.getenv(EXPERIMENTAL_LOCATION_API_ENV_VAR)); |
396 | 492 | TransportChannelProvider channelProvider = |
397 | | - MoreObjects.firstNonNull( |
398 | | - options.getChannelProvider(), defaultChannelProviderBuilder.build()); |
| 493 | + enableLocationApi |
| 494 | + ? new KeyAwareTransportChannelProvider(defaultChannelProviderBuilder) |
| 495 | + : MoreObjects.firstNonNull( |
| 496 | + options.getChannelProvider(), defaultChannelProviderBuilder.build()); |
399 | 497 |
|
400 | 498 | CredentialsProvider credentialsProvider = |
401 | 499 | GrpcTransportOptions.setUpCredentialsProvider(options); |
|
0 commit comments