|
| 1 | +/* |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.google.cloud.bigtable.gaxx.grpc; |
| 17 | + |
| 18 | +import com.google.api.core.InternalApi; |
| 19 | +import com.google.api.gax.grpc.ChannelFactory; |
| 20 | +import com.google.api.gax.grpc.ChannelPoolSettings; |
| 21 | +import com.google.api.gax.grpc.GrpcTransportChannel; |
| 22 | +import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider; |
| 23 | +import com.google.api.gax.rpc.TransportChannel; |
| 24 | +import com.google.api.gax.rpc.TransportChannelProvider; |
| 25 | +import com.google.auth.Credentials; |
| 26 | +import com.google.common.base.Preconditions; |
| 27 | +import io.grpc.ManagedChannel; |
| 28 | +import java.io.IOException; |
| 29 | +import java.util.Map; |
| 30 | +import java.util.concurrent.Executor; |
| 31 | +import java.util.concurrent.ScheduledExecutorService; |
| 32 | + |
| 33 | +/** |
| 34 | + * An instance of TransportChannelProvider that provides a TransportChannel through a supplied |
| 35 | + * InstantiatingGrpcChannelProvider. |
| 36 | + */ |
| 37 | +@InternalApi |
| 38 | +public final class BigtableTransportChannelProvider implements TransportChannelProvider { |
| 39 | + |
| 40 | + private final InstantiatingGrpcChannelProvider delegate; |
| 41 | + |
| 42 | + private BigtableTransportChannelProvider( |
| 43 | + InstantiatingGrpcChannelProvider instantiatingGrpcChannelProvider) { |
| 44 | + delegate = Preconditions.checkNotNull(instantiatingGrpcChannelProvider); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public boolean shouldAutoClose() { |
| 49 | + return delegate.shouldAutoClose(); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public boolean needsExecutor() { |
| 54 | + return delegate.needsExecutor(); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public BigtableTransportChannelProvider withExecutor(ScheduledExecutorService executor) { |
| 59 | + return withExecutor((Executor) executor); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public BigtableTransportChannelProvider withExecutor(Executor executor) { |
| 64 | + InstantiatingGrpcChannelProvider newChannelProvider = |
| 65 | + (InstantiatingGrpcChannelProvider) delegate.withExecutor(executor); |
| 66 | + return new BigtableTransportChannelProvider(newChannelProvider); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public boolean needsHeaders() { |
| 71 | + return delegate.needsHeaders(); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public BigtableTransportChannelProvider withHeaders(Map<String, String> headers) { |
| 76 | + InstantiatingGrpcChannelProvider newChannelProvider = |
| 77 | + (InstantiatingGrpcChannelProvider) delegate.withHeaders(headers); |
| 78 | + return new BigtableTransportChannelProvider(newChannelProvider); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public boolean needsEndpoint() { |
| 83 | + return delegate.needsEndpoint(); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public TransportChannelProvider withEndpoint(String endpoint) { |
| 88 | + InstantiatingGrpcChannelProvider newChannelProvider = |
| 89 | + (InstantiatingGrpcChannelProvider) delegate.withEndpoint(endpoint); |
| 90 | + return new BigtableTransportChannelProvider(newChannelProvider); |
| 91 | + } |
| 92 | + |
| 93 | + @Deprecated |
| 94 | + @Override |
| 95 | + public boolean acceptsPoolSize() { |
| 96 | + return delegate.acceptsPoolSize(); |
| 97 | + } |
| 98 | + |
| 99 | + @Deprecated |
| 100 | + @Override |
| 101 | + public TransportChannelProvider withPoolSize(int size) { |
| 102 | + InstantiatingGrpcChannelProvider newChannelProvider = |
| 103 | + (InstantiatingGrpcChannelProvider) delegate.withPoolSize(size); |
| 104 | + return new BigtableTransportChannelProvider(newChannelProvider); |
| 105 | + } |
| 106 | + |
| 107 | + /** Expected to only be called once when BigtableClientContext is created */ |
| 108 | + @Override |
| 109 | + public TransportChannel getTransportChannel() throws IOException { |
| 110 | + // This provider's main purpose is to replace the default GAX ChannelPool |
| 111 | + // with a custom BigtableChannelPool, reusing the delegate's configuration. |
| 112 | + |
| 113 | + // To create our pool, we need a factory for raw gRPC channels. |
| 114 | + // We achieve this by configuring our delegate to not use its own pooling |
| 115 | + // (by setting pool size to 1) and then calling getTransportChannel() on it. |
| 116 | + InstantiatingGrpcChannelProvider singleChannelProvider = |
| 117 | + delegate.toBuilder().setChannelPoolSettings(ChannelPoolSettings.staticallySized(1)).build(); |
| 118 | + |
| 119 | + ChannelFactory channelFactory = |
| 120 | + () -> { |
| 121 | + try { |
| 122 | + GrpcTransportChannel channel = |
| 123 | + (GrpcTransportChannel) singleChannelProvider.getTransportChannel(); |
| 124 | + return (ManagedChannel) channel.getChannel(); |
| 125 | + } catch (IOException e) { |
| 126 | + throw new java.io.UncheckedIOException(e); |
| 127 | + } |
| 128 | + }; |
| 129 | + |
| 130 | + BigtableChannelPoolSettings btPoolSettings = |
| 131 | + BigtableChannelPoolSettings.copyFrom(delegate.getChannelPoolSettings()); |
| 132 | + |
| 133 | + BigtableChannelPool btChannelPool = BigtableChannelPool.create(btPoolSettings, channelFactory); |
| 134 | + |
| 135 | + return GrpcTransportChannel.create(btChannelPool); |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public String getTransportName() { |
| 140 | + return "bigtable"; |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public boolean needsCredentials() { |
| 145 | + return delegate.needsCredentials(); |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public TransportChannelProvider withCredentials(Credentials credentials) { |
| 150 | + InstantiatingGrpcChannelProvider newChannelProvider = |
| 151 | + (InstantiatingGrpcChannelProvider) delegate.withCredentials(credentials); |
| 152 | + return new BigtableTransportChannelProvider(newChannelProvider); |
| 153 | + } |
| 154 | + |
| 155 | + /** Creates a BigtableTransportChannelProvider. */ |
| 156 | + public static BigtableTransportChannelProvider create( |
| 157 | + InstantiatingGrpcChannelProvider instantiatingGrpcChannelProvider) { |
| 158 | + return new BigtableTransportChannelProvider(instantiatingGrpcChannelProvider); |
| 159 | + } |
| 160 | +} |
0 commit comments