Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: defer instance admin api errors to RPC time rather then client construction time #1576

Merged
merged 1 commit into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,17 @@
import static com.google.cloud.bigtable.admin.v2.BigtableTableAdminSettings.BIGTABLE_EMULATOR_HOST_ENV_VAR;

import com.google.api.gax.core.CredentialsProvider;
import com.google.api.gax.core.NoCredentialsProvider;
import com.google.api.gax.grpc.ChannelPoolSettings;
import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider;
import com.google.cloud.bigtable.admin.v2.stub.BigtableInstanceAdminStubSettings;
import com.google.common.base.MoreObjects;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.base.Verify;
import io.grpc.ManagedChannelBuilder;
import java.io.IOException;
import java.util.logging.Logger;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

Expand All @@ -47,6 +53,9 @@
* }</pre>
*/
public final class BigtableInstanceAdminSettings {
private static final Logger LOGGER =
Logger.getLogger(BigtableInstanceAdminSettings.class.getName());

private final String projectId;
private final BigtableInstanceAdminStubSettings stubSettings;

Expand Down Expand Up @@ -115,12 +124,49 @@ public Builder toBuilder() {

/** Returns a new builder for this class. */
public static Builder newBuilder() {
Preconditions.checkState(
System.getenv(BIGTABLE_EMULATOR_HOST_ENV_VAR) == null,
"BigtableInstanceAdminSettings doesn't supported on Emulator");
String hostAndPort = System.getenv(BIGTABLE_EMULATOR_HOST_ENV_VAR);
if (!Strings.isNullOrEmpty(hostAndPort)) {
int port;
try {
port = Integer.parseInt(hostAndPort.substring(hostAndPort.lastIndexOf(":") + 1));
return newBuilderForEmulator(hostAndPort.substring(0, hostAndPort.lastIndexOf(":")), port);
} catch (NumberFormatException | IndexOutOfBoundsException ex) {
throw new RuntimeException(
"Invalid host/port in "
+ BIGTABLE_EMULATOR_HOST_ENV_VAR
+ " environment variable: "
+ hostAndPort);
}
}
return new Builder();
}

/** Create a new builder preconfigured to connect to the Bigtable emulator with port number. */
public static Builder newBuilderForEmulator(int port) {
return newBuilderForEmulator("localhost", port);
}

/**
* Creates a new builder preconfigured to connect to the Bigtable emulator with host name and port
* number.
*/
public static Builder newBuilderForEmulator(String hostname, int port) {
Builder builder = new Builder();

builder
.stubSettings()
.setCredentialsProvider(NoCredentialsProvider.create())
.setEndpoint(hostname + ":" + port)
.setTransportChannelProvider(
InstantiatingGrpcChannelProvider.newBuilder()
.setChannelPoolSettings(ChannelPoolSettings.staticallySized(1))
.setChannelConfigurator(ManagedChannelBuilder::usePlaintext)
.build());

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

/** Builder for BigtableInstanceAdminSettings. */
public static final class Builder {
@Nullable private String projectId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
*/
package com.google.cloud.bigtable.admin.v2;

import com.google.api.core.ApiFunction;
import com.google.api.gax.core.CredentialsProvider;
import com.google.api.gax.core.NoCredentialsProvider;
import com.google.api.gax.grpc.ChannelPoolSettings;
import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider;
import com.google.cloud.bigtable.admin.v2.stub.BigtableTableAdminStubSettings;
import com.google.common.base.MoreObjects;
Expand Down Expand Up @@ -174,14 +174,8 @@ public static Builder newBuilderForEmulator(String hostname, int port) {
.setEndpoint(hostname + ":" + port)
.setTransportChannelProvider(
InstantiatingGrpcChannelProvider.newBuilder()
.setPoolSize(1)
.setChannelConfigurator(
new ApiFunction<ManagedChannelBuilder, ManagedChannelBuilder>() {
@Override
public ManagedChannelBuilder apply(ManagedChannelBuilder input) {
return input.usePlaintext();
}
})
.setChannelPoolSettings(ChannelPoolSettings.staticallySized(1))
.setChannelConfigurator(ManagedChannelBuilder::usePlaintext)
.build());

LOGGER.info("Connecting to the Bigtable emulator at " + hostname + ":" + port);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
*/
package com.google.cloud.bigtable.data.v2;

import com.google.api.core.ApiFunction;
import com.google.api.core.BetaApi;
import com.google.api.gax.batching.Batcher;
import com.google.api.gax.batching.FlowController;
import com.google.api.gax.core.CredentialsProvider;
import com.google.api.gax.core.NoCredentialsProvider;
import com.google.api.gax.grpc.ChannelPoolSettings;
import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider;
import com.google.api.gax.rpc.UnaryCallSettings;
import com.google.auth.Credentials;
Expand Down Expand Up @@ -129,14 +129,8 @@ public static Builder newBuilderForEmulator(String hostname, int port) {
.setTransportChannelProvider(
InstantiatingGrpcChannelProvider.newBuilder()
.setMaxInboundMessageSize(256 * 1024 * 1024)
.setPoolSize(1)
.setChannelConfigurator(
new ApiFunction<ManagedChannelBuilder, ManagedChannelBuilder>() {
@Override
public ManagedChannelBuilder apply(ManagedChannelBuilder input) {
return input.usePlaintext();
}
})
.setChannelPoolSettings(ChannelPoolSettings.staticallySized(1))
.setChannelConfigurator(ManagedChannelBuilder::usePlaintext)
.setKeepAliveTime(Duration.ofSeconds(61)) // sends ping in this interval
.setKeepAliveTimeout(
Duration.ofSeconds(10)) // wait this long before considering the connection dead
Expand Down