From 2dd5105559f6f61f9780b11e44bccbd2c08d68ae Mon Sep 17 00:00:00 2001 From: Igor Bernstein Date: Thu, 24 Oct 2019 10:16:52 -0400 Subject: [PATCH] feat: Add Experimental DirectPath support (#8) * Add Experimental DirectPath support * finish removing DirectPathEnv * format * remove sys prop check, this will be enabled in the next version of grpc * english --- google-cloud-bigtable/pom.xml | 2 +- .../v2/stub/EnhancedBigtableStubSettings.java | 40 +++- .../data/v2/it/DirectPathFallbackIT.java | 5 +- .../test_helpers/env/AbstractTestEnv.java | 4 + .../test_helpers/env/DirectPathEnv.java | 194 ------------------ .../test_helpers/env/TestEnvRule.java | 3 - 6 files changed, 46 insertions(+), 202 deletions(-) delete mode 100644 google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/test_helpers/env/DirectPathEnv.java diff --git a/google-cloud-bigtable/pom.xml b/google-cloud-bigtable/pom.xml index f46de3ed0..90306c3b7 100644 --- a/google-cloud-bigtable/pom.xml +++ b/google-cloud-bigtable/pom.xml @@ -257,7 +257,7 @@ - direct_path + prod true diff --git a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/EnhancedBigtableStubSettings.java b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/EnhancedBigtableStubSettings.java index 001ac4e8e..a08bb6bff 100644 --- a/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/EnhancedBigtableStubSettings.java +++ b/google-cloud-bigtable/src/main/java/com/google/cloud/bigtable/data/v2/stub/EnhancedBigtableStubSettings.java @@ -42,6 +42,7 @@ import com.google.common.collect.ImmutableSet; import java.util.List; import java.util.Set; +import java.util.logging.Logger; import javax.annotation.Nonnull; import org.threeten.bp.Duration; @@ -74,10 +75,18 @@ * } */ public class EnhancedBigtableStubSettings extends StubSettings { + private static final Logger logger = + Logger.getLogger(EnhancedBigtableStubSettings.class.getName()); + // The largest message that can be received is a 256 MB ReadRowsResponse. private static final int MAX_MESSAGE_SIZE = 256 * 1024 * 1024; private static final String SERVER_DEFAULT_APP_PROFILE_ID = ""; + // TODO(igorbernstein2): Remove this once DirectPath goes to public beta + // Temporary endpoint for the DirectPath private alpha + private static final String DIRECT_PATH_ENV_VAR = "GOOGLE_CLOUD_ENABLE_DIRECT_PATH"; + private static final String DIRECT_PATH_ENDPOINT = "directpath-bigtable.googleapis.com:443"; + private static final Set IDEMPOTENT_RETRY_CODES = ImmutableSet.of(Code.DEADLINE_EXCEEDED, Code.UNAVAILABLE); @@ -133,6 +142,12 @@ public class EnhancedBigtableStubSettings extends StubSettings // diff --git a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/it/DirectPathFallbackIT.java b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/it/DirectPathFallbackIT.java index bd26d9cac..781369b18 100644 --- a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/it/DirectPathFallbackIT.java +++ b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/data/v2/it/DirectPathFallbackIT.java @@ -23,7 +23,6 @@ import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider; import com.google.cloud.bigtable.data.v2.BigtableDataClient; import com.google.cloud.bigtable.data.v2.BigtableDataSettings; -import com.google.cloud.bigtable.test_helpers.env.DirectPathEnv; import com.google.cloud.bigtable.test_helpers.env.TestEnvRule; import io.grpc.ManagedChannelBuilder; import io.grpc.alts.ComputeEngineChannelBuilder; @@ -82,8 +81,8 @@ public DirectPathFallbackIT() { public void setup() throws IOException { assume() .withMessage("DirectPath integration tests can only run against DirectPathEnv") - .that(testEnvRule.env()) - .isInstanceOf(DirectPathEnv.class); + .that(testEnvRule.env().isDirectPathEnabled()) + .isTrue(); BigtableDataSettings defaultSettings = testEnvRule.env().getDataClientSettings(); InstantiatingGrpcChannelProvider defaultTransportProvider = diff --git a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/test_helpers/env/AbstractTestEnv.java b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/test_helpers/env/AbstractTestEnv.java index db0cb6bf8..6fc9e2681 100644 --- a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/test_helpers/env/AbstractTestEnv.java +++ b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/test_helpers/env/AbstractTestEnv.java @@ -74,6 +74,10 @@ public boolean isInstanceAdminSupported() { return true; } + public boolean isDirectPathEnabled() { + return "bigtable".equals(System.getenv("GOOGLE_CLOUD_ENABLE_DIRECT_PATH")); + } + void cleanUpStale() { cleanupStaleTables(); if (isInstanceAdminSupported()) { diff --git a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/test_helpers/env/DirectPathEnv.java b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/test_helpers/env/DirectPathEnv.java deleted file mode 100644 index 2ef4b05c7..000000000 --- a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/test_helpers/env/DirectPathEnv.java +++ /dev/null @@ -1,194 +0,0 @@ -/* - * Copyright 2019 Google LLC - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.google.cloud.bigtable.test_helpers.env; - -import com.google.api.core.ApiFunction; -import com.google.cloud.bigtable.admin.v2.BigtableInstanceAdminClient; -import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient; -import com.google.cloud.bigtable.data.v2.BigtableDataClient; -import com.google.cloud.bigtable.data.v2.BigtableDataSettings; -import com.google.cloud.bigtable.data.v2.stub.EnhancedBigtableStubSettings; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; -import io.grpc.ManagedChannelBuilder; -import java.io.IOException; - -/** - * Test environment that uses an existing bigtable table in the directpath environment. - * - *

The test environment can be accessed via both, CFE and direct path. The transport is - * controlled via the environment variable {@code GOOGLE_CLOUD_ENABLE_DIRECT_PATH}. Which contains - * comma delimited list of service names that should be accessed via direct path. - * - *

The target table must have a pre-existing family {@code cf}. The target table is configured - * via the system properties: - * - *

    - *
  • {@code bigtable.project} - *
  • {@code bigtable.instance} - *
  • {@code bigtable.table} - *
- */ -public class DirectPathEnv extends AbstractTestEnv { - // TODO(igorbernstein): move direct path conditional logic to gax - private static final String DIRECT_PATH_ENV_VAR = "GOOGLE_CLOUD_ENABLE_DIRECT_PATH"; - private static final String DIRECT_PATH_END_POINT = - "testdirectpath-bigtable.sandbox.googleapis.com:443"; - - private static final String PROJECT_PROPERTY_NAME = "bigtable.project"; - private static final String INSTANCE_PROPERTY_NAME = "bigtable.instance"; - private static final String TABLE_PROPERTY_NAME = "bigtable.table"; - - private final boolean directPathEnabled; - private final String projectId; - private final String instanceId; - private final String tableId; - - private final BigtableDataSettings dataSettings; - - private BigtableDataClient dataClient; - private BigtableTableAdminClient tableAdminClient; - private BigtableInstanceAdminClient instanceAdminClient; - - static DirectPathEnv create() { - return new DirectPathEnv( - isDirectPathEnabled(), - getRequiredProperty(PROJECT_PROPERTY_NAME), - getRequiredProperty(INSTANCE_PROPERTY_NAME), - getRequiredProperty(TABLE_PROPERTY_NAME)); - } - - private DirectPathEnv( - boolean directPathEnabled, String projectId, String instanceId, String tableId) { - this.directPathEnabled = directPathEnabled; - this.projectId = projectId; - this.instanceId = instanceId; - this.tableId = tableId; - - // Build the data client settings - BigtableDataSettings.Builder settingsBuilder = - BigtableDataSettings.newBuilder().setProjectId(projectId).setInstanceId(instanceId); - - // Direct path test environment uses a different endpoint. - settingsBuilder.stubSettings().setEndpoint(DIRECT_PATH_END_POINT); - - // TODO: Remove this after https://github.com/googleapis/gax-java/pull/707 is merged - if (directPathEnabled) { - ImmutableMap pickFirstStrategy = - ImmutableMap.of("pick_first", ImmutableMap.of()); - - ImmutableMap childPolicy = - ImmutableMap.of("childPolicy", ImmutableList.of(pickFirstStrategy)); - - ImmutableMap grpcLbPolicy = - ImmutableMap.of("grpclb", childPolicy); - - final ImmutableMap loadBalancingConfig = - ImmutableMap.of("loadBalancingConfig", ImmutableList.of(grpcLbPolicy)); - - settingsBuilder - .stubSettings() - .setTransportChannelProvider( - EnhancedBigtableStubSettings.defaultGrpcTransportProviderBuilder() - .setChannelConfigurator( - new ApiFunction() { - @Override - public ManagedChannelBuilder apply(ManagedChannelBuilder builder) { - // Configure pick_first strategy to force a single subchannel per - // ManagedChannel - builder.defaultServiceConfig(loadBalancingConfig); - - return builder; - } - }) - .build()); - } - - this.dataSettings = settingsBuilder.build(); - } - - @Override - void start() throws IOException { - dataClient = BigtableDataClient.create(dataSettings); - tableAdminClient = BigtableTableAdminClient.create(projectId, instanceId); - instanceAdminClient = BigtableInstanceAdminClient.create(projectId); - } - - @Override - void stop() throws Exception { - dataClient.close(); - tableAdminClient.close(); - instanceAdminClient.close(); - } - - @Override - public BigtableDataClient getDataClient() { - return dataClient; - } - - @Override - public BigtableTableAdminClient getTableAdminClient() { - return tableAdminClient; - } - - @Override - public BigtableInstanceAdminClient getInstanceAdminClient() { - return instanceAdminClient; - } - - @Override - public BigtableDataSettings getDataClientSettings() { - return dataSettings; - } - - @Override - public String getProjectId() { - return projectId; - } - - @Override - public String getInstanceId() { - return instanceId; - } - - @Override - public String getTableId() { - return tableId; - } - - private static String getRequiredProperty(String prop) { - String value = System.getProperty(prop); - if (value == null || value.isEmpty()) { - throw new RuntimeException("Missing system property: " + prop); - } - return value; - } - - // TODO(igorbernstein): move direct path conditional logic to gax - private static boolean isDirectPathEnabled() { - String whiteList = System.getenv(DIRECT_PATH_ENV_VAR); - if (whiteList == null) { - return false; - } - - for (String service : whiteList.split(",")) { - if (!service.isEmpty() && DIRECT_PATH_END_POINT.contains(service)) { - return true; - } - } - return false; - } -} diff --git a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/test_helpers/env/TestEnvRule.java b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/test_helpers/env/TestEnvRule.java index 3393ab358..293f8b47e 100644 --- a/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/test_helpers/env/TestEnvRule.java +++ b/google-cloud-bigtable/src/test/java/com/google/cloud/bigtable/test_helpers/env/TestEnvRule.java @@ -63,9 +63,6 @@ protected void before() throws Throwable { case "prod": testEnv = ProdEnv.fromSystemProperties(); break; - case "direct_path": - testEnv = DirectPathEnv.create(); - break; default: throw new IllegalArgumentException( String.format(