Skip to content

Commit 06a0ced

Browse files
fix: defer instance admin api errors to RPC time rather then client construction time (#1576)
Will fix googleapis/java-bigtable-hbase#3518 Change-Id: If5201705a8fd4b8757fac0206b6e563c8cd0ae65
1 parent a68fb80 commit 06a0ced

File tree

3 files changed

+55
-21
lines changed

3 files changed

+55
-21
lines changed

google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/BigtableInstanceAdminSettings.java

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,17 @@
1818
import static com.google.cloud.bigtable.admin.v2.BigtableTableAdminSettings.BIGTABLE_EMULATOR_HOST_ENV_VAR;
1919

2020
import com.google.api.gax.core.CredentialsProvider;
21+
import com.google.api.gax.core.NoCredentialsProvider;
22+
import com.google.api.gax.grpc.ChannelPoolSettings;
23+
import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider;
2124
import com.google.cloud.bigtable.admin.v2.stub.BigtableInstanceAdminStubSettings;
2225
import com.google.common.base.MoreObjects;
2326
import com.google.common.base.Preconditions;
27+
import com.google.common.base.Strings;
2428
import com.google.common.base.Verify;
29+
import io.grpc.ManagedChannelBuilder;
2530
import java.io.IOException;
31+
import java.util.logging.Logger;
2632
import javax.annotation.Nonnull;
2733
import javax.annotation.Nullable;
2834

@@ -47,6 +53,9 @@
4753
* }</pre>
4854
*/
4955
public final class BigtableInstanceAdminSettings {
56+
private static final Logger LOGGER =
57+
Logger.getLogger(BigtableInstanceAdminSettings.class.getName());
58+
5059
private final String projectId;
5160
private final BigtableInstanceAdminStubSettings stubSettings;
5261

@@ -115,12 +124,49 @@ public Builder toBuilder() {
115124

116125
/** Returns a new builder for this class. */
117126
public static Builder newBuilder() {
118-
Preconditions.checkState(
119-
System.getenv(BIGTABLE_EMULATOR_HOST_ENV_VAR) == null,
120-
"BigtableInstanceAdminSettings doesn't supported on Emulator");
127+
String hostAndPort = System.getenv(BIGTABLE_EMULATOR_HOST_ENV_VAR);
128+
if (!Strings.isNullOrEmpty(hostAndPort)) {
129+
int port;
130+
try {
131+
port = Integer.parseInt(hostAndPort.substring(hostAndPort.lastIndexOf(":") + 1));
132+
return newBuilderForEmulator(hostAndPort.substring(0, hostAndPort.lastIndexOf(":")), port);
133+
} catch (NumberFormatException | IndexOutOfBoundsException ex) {
134+
throw new RuntimeException(
135+
"Invalid host/port in "
136+
+ BIGTABLE_EMULATOR_HOST_ENV_VAR
137+
+ " environment variable: "
138+
+ hostAndPort);
139+
}
140+
}
121141
return new Builder();
122142
}
123143

144+
/** Create a new builder preconfigured to connect to the Bigtable emulator with port number. */
145+
public static Builder newBuilderForEmulator(int port) {
146+
return newBuilderForEmulator("localhost", port);
147+
}
148+
149+
/**
150+
* Creates a new builder preconfigured to connect to the Bigtable emulator with host name and port
151+
* number.
152+
*/
153+
public static Builder newBuilderForEmulator(String hostname, int port) {
154+
Builder builder = new Builder();
155+
156+
builder
157+
.stubSettings()
158+
.setCredentialsProvider(NoCredentialsProvider.create())
159+
.setEndpoint(hostname + ":" + port)
160+
.setTransportChannelProvider(
161+
InstantiatingGrpcChannelProvider.newBuilder()
162+
.setChannelPoolSettings(ChannelPoolSettings.staticallySized(1))
163+
.setChannelConfigurator(ManagedChannelBuilder::usePlaintext)
164+
.build());
165+
166+
LOGGER.info("Connecting to the Bigtable emulator at " + hostname + ":" + port);
167+
return builder;
168+
}
169+
124170
/** Builder for BigtableInstanceAdminSettings. */
125171
public static final class Builder {
126172
@Nullable private String projectId;

google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/admin/v2/BigtableTableAdminSettings.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
*/
1616
package com.google.cloud.bigtable.admin.v2;
1717

18-
import com.google.api.core.ApiFunction;
1918
import com.google.api.gax.core.CredentialsProvider;
2019
import com.google.api.gax.core.NoCredentialsProvider;
20+
import com.google.api.gax.grpc.ChannelPoolSettings;
2121
import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider;
2222
import com.google.cloud.bigtable.admin.v2.stub.BigtableTableAdminStubSettings;
2323
import com.google.common.base.MoreObjects;
@@ -174,14 +174,8 @@ public static Builder newBuilderForEmulator(String hostname, int port) {
174174
.setEndpoint(hostname + ":" + port)
175175
.setTransportChannelProvider(
176176
InstantiatingGrpcChannelProvider.newBuilder()
177-
.setPoolSize(1)
178-
.setChannelConfigurator(
179-
new ApiFunction<ManagedChannelBuilder, ManagedChannelBuilder>() {
180-
@Override
181-
public ManagedChannelBuilder apply(ManagedChannelBuilder input) {
182-
return input.usePlaintext();
183-
}
184-
})
177+
.setChannelPoolSettings(ChannelPoolSettings.staticallySized(1))
178+
.setChannelConfigurator(ManagedChannelBuilder::usePlaintext)
185179
.build());
186180

187181
LOGGER.info("Connecting to the Bigtable emulator at " + hostname + ":" + port);

google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/BigtableDataSettings.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
*/
1616
package com.google.cloud.bigtable.data.v2;
1717

18-
import com.google.api.core.ApiFunction;
1918
import com.google.api.core.BetaApi;
2019
import com.google.api.gax.batching.Batcher;
2120
import com.google.api.gax.batching.FlowController;
2221
import com.google.api.gax.core.CredentialsProvider;
2322
import com.google.api.gax.core.NoCredentialsProvider;
23+
import com.google.api.gax.grpc.ChannelPoolSettings;
2424
import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider;
2525
import com.google.api.gax.rpc.UnaryCallSettings;
2626
import com.google.auth.Credentials;
@@ -129,14 +129,8 @@ public static Builder newBuilderForEmulator(String hostname, int port) {
129129
.setTransportChannelProvider(
130130
InstantiatingGrpcChannelProvider.newBuilder()
131131
.setMaxInboundMessageSize(256 * 1024 * 1024)
132-
.setPoolSize(1)
133-
.setChannelConfigurator(
134-
new ApiFunction<ManagedChannelBuilder, ManagedChannelBuilder>() {
135-
@Override
136-
public ManagedChannelBuilder apply(ManagedChannelBuilder input) {
137-
return input.usePlaintext();
138-
}
139-
})
132+
.setChannelPoolSettings(ChannelPoolSettings.staticallySized(1))
133+
.setChannelConfigurator(ManagedChannelBuilder::usePlaintext)
140134
.setKeepAliveTime(Duration.ofSeconds(61)) // sends ping in this interval
141135
.setKeepAliveTimeout(
142136
Duration.ofSeconds(10)) // wait this long before considering the connection dead

0 commit comments

Comments
 (0)