Skip to content
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 @@ -15,6 +15,8 @@
*/
package com.google.cloud.bigtable.admin.v2;

import static com.google.cloud.bigtable.admin.v2.BigtableTableAdminSettings.BIGTABLE_EMULATOR_HOST_ENV_VAR;

import com.google.api.gax.core.CredentialsProvider;
import com.google.cloud.bigtable.admin.v2.stub.BigtableInstanceAdminStubSettings;
import com.google.common.base.Preconditions;
Expand Down Expand Up @@ -79,6 +81,9 @@ 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");
return new Builder();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider;
import com.google.cloud.bigtable.admin.v2.stub.BigtableTableAdminStubSettings;
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.Nullable;

/**
Expand All @@ -48,6 +50,10 @@
* }</pre>
*/
public final class BigtableTableAdminSettings {

private static final Logger LOGGER = Logger.getLogger(BigtableTableAdminSettings.class.getName());
static final String BIGTABLE_EMULATOR_HOST_ENV_VAR = "BIGTABLE_EMULATOR_HOST";

private final String projectId;
private final String instanceId;
private final BigtableTableAdminStubSettings stubSettings;
Expand Down Expand Up @@ -84,19 +90,46 @@ public Builder toBuilder() {
return new Builder(this);
}

/** Returns a new builder for this class. */
/**
* Returns a new builder for this class.
*
* <p>If emulator configuration provided in BIGTABLE_EMULATOR_HOST environment variable then it
* creates a builder preconfigured to connect to Bigtable using emulator hostname and port number.
*/
public static Builder newBuilder() {
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. */
/** Create a new builder preconfigured to connect to the Bigtable emulator with port number. */
public static Builder newBuilderForEmulator(int port) {
Builder builder = newBuilder().setProjectId("fake-project").setInstanceId("fake-instance");
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("localhost:" + port)
.setEndpoint(hostname + ":" + port)
.setTransportChannelProvider(
InstantiatingGrpcChannelProvider.newBuilder()
.setPoolSize(1)
Expand All @@ -109,6 +142,7 @@ public ManagedChannelBuilder apply(ManagedChannelBuilder input) {
})
.build());

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import com.google.cloud.bigtable.data.v2.models.Query;
import com.google.cloud.bigtable.data.v2.models.Row;
import com.google.cloud.bigtable.data.v2.stub.EnhancedBigtableStubSettings;
import com.google.common.base.Strings;
import io.grpc.ManagedChannelBuilder;
import java.util.logging.Logger;
import javax.annotation.Nonnull;

/**
Expand Down Expand Up @@ -55,27 +57,56 @@
* EnhancedBigtableStubSettings}, which is exposed as {@link Builder#stubSettings()}.
*/
public final class BigtableDataSettings {

private static final Logger LOGGER = Logger.getLogger(BigtableDataSettings.class.getName());
private static final String BIGTABLE_EMULATOR_HOST_ENV_VAR = "BIGTABLE_EMULATOR_HOST";

private final EnhancedBigtableStubSettings stubSettings;

private BigtableDataSettings(Builder builder) {
stubSettings = builder.stubSettings().build();
}

/** Create a new builder. */
/**
* Create a new builder.
*
* <p>If emulator configuration provided in BIGTABLE_EMULATOR_HOST environment variable then it
* creates a builder preconfigured to connect to Bigtable using emulator hostname and port number.
*/
public static Builder newBuilder() {
String hostAndPort = System.getenv(BIGTABLE_EMULATOR_HOST_ENV_VAR);
if (!Strings.isNullOrEmpty(hostAndPort)) {
try {
int lastIndexOfCol = hostAndPort.lastIndexOf(":");
int port = Integer.parseInt(hostAndPort.substring(lastIndexOfCol + 1));
return newBuilderForEmulator(hostAndPort.substring(0, lastIndexOfCol), 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. */
/** Create a new builder preconfigured to connect to the Bigtable emulator with port number. */
public static Builder newBuilderForEmulator(int port) {
Builder builder = newBuilder();
return newBuilderForEmulator("localhost", port);
}

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

builder
.stubSettings()
.setProjectId("fake-project")
.setInstanceId("fake-instance")
.setCredentialsProvider(NoCredentialsProvider.create())
.setEndpoint("localhost:" + port)
.setEndpoint(hostname + ":" + port)
.setTransportChannelProvider(
InstantiatingGrpcChannelProvider.newBuilder()
.setMaxInboundMessageSize(256 * 1024 * 1024)
Expand All @@ -89,6 +120,7 @@ public ManagedChannelBuilder apply(ManagedChannelBuilder input) {
})
.build());

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,16 @@ public void start() throws Exception {

tableAdminClient =
BigtableTableAdminClient.create(
BigtableTableAdminSettings.newBuilderForEmulator(emulator.getPort()).build());
BigtableTableAdminSettings.newBuilderForEmulator(emulator.getPort())
.setProjectId("fake-project")
.setInstanceId("fake-instance")
.build());
dataClient =
BigtableDataClient.create(
BigtableDataSettings.newBuilderForEmulator(emulator.getPort()).build());
BigtableDataSettings.newBuilderForEmulator(emulator.getPort())
.setProjectId("fake-project")
.setInstanceId("fake-instance")
.build());

tableAdminClient.createTable(CreateTableRequest.of(TABLE_ID).addFamily(FAMILY_ID));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.google.cloud.bigtable.data.v2.it.env;

import static com.google.common.truth.TruthJUnit.assume;

import java.util.logging.Level;
import java.util.logging.Logger;
import org.junit.rules.ExternalResource;
Expand All @@ -37,14 +39,21 @@
* <p>By default, {@code emulator} will be used
*/
public class TestEnvRule extends ExternalResource {
private static final Logger LOGGER = Logger.getLogger(TestEnvRule.class.getName());

private static final Logger LOGGER = Logger.getLogger(TestEnvRule.class.getName());
private static final String BIGTABLE_EMULATOR_HOST_ENV_VAR = "BIGTABLE_EMULATOR_HOST";
private static final String ENV_PROPERTY = "bigtable.env";

private TestEnv testEnv;

@Override
protected void before() throws Throwable {
assume()
.withMessage(
"Integration tests can't run with the BIGTABLE_EMULATOR_HOST environment variable set"
+ ". Please use the emulator-it maven profile instead")
.that(System.getenv())
.doesNotContainKey(BIGTABLE_EMULATOR_HOST_ENV_VAR);
String env = System.getProperty(ENV_PROPERTY, "emulator");

switch (env) {
Expand Down