igorbernstein2 commented on code in PR #24015:
URL: https://github.com/apache/beam/pull/24015#discussion_r1123328414


##########
sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/bigtable/BigtableConfigTranslator.java:
##########
@@ -0,0 +1,378 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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
+ *
+ *     http://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 org.apache.beam.sdk.io.gcp.bigtable;
+
+import com.google.api.gax.batching.BatchingSettings;
+import com.google.api.gax.batching.FlowControlSettings;
+import com.google.api.gax.core.FixedCredentialsProvider;
+import com.google.api.gax.grpc.ChannelPoolSettings;
+import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider;
+import com.google.api.gax.retrying.RetrySettings;
+import com.google.api.gax.rpc.FixedHeaderProvider;
+import com.google.api.gax.rpc.StubSettings;
+import com.google.auth.Credentials;
+import com.google.auth.oauth2.GoogleCredentials;
+import com.google.auth.oauth2.ServiceAccountJwtAccessCredentials;
+import com.google.cloud.bigtable.config.BigtableOptions;
+import com.google.cloud.bigtable.config.CredentialOptions;
+import com.google.cloud.bigtable.data.v2.BigtableDataSettings;
+import com.google.cloud.bigtable.data.v2.stub.BigtableBatchingCallSettings;
+import io.grpc.internal.GrpcUtil;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.security.GeneralSecurityException;
+import java.security.KeyStore;
+import java.security.PrivateKey;
+import java.util.Objects;
+import org.apache.beam.sdk.extensions.gcp.auth.CredentialFactory;
+import org.apache.beam.sdk.extensions.gcp.auth.NoopCredentialFactory;
+import org.apache.beam.sdk.extensions.gcp.options.GcpOptions;
+import org.apache.beam.sdk.options.PipelineOptions;
+import org.apache.beam.sdk.options.PipelineOptionsFactory;
+import org.apache.beam.sdk.options.ValueProvider;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Strings;
+import 
org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap;
+import org.checkerframework.checker.nullness.qual.NonNull;
+import org.threeten.bp.Duration;
+
+/**
+ * Helper class to translate {@link BigtableConfig}, {@link 
BigtableReadOptions}, {@link
+ * BigtableWriteOptions} and {@link PipelineOptions} to Bigtable Veneer 
settings.
+ *
+ * <p>Also translate {@link BigtableOptions} to {@link BigtableConfig} for 
backward compatibility.
+ * If the values are set on {@link BigtableConfig} directly, ignore the 
settings in {@link
+ * BigtableOptions}.
+ */
+@SuppressWarnings({
+  "nullness" // TODO(https://github.com/apache/beam/issues/20497)
+})
+class BigtableConfigTranslator {
+
+  /** Translate BigtableConfig and BigtableReadOptions to Veneer settings. */
+  static BigtableDataSettings translateReadToVeneerSettings(
+      @NonNull BigtableConfig config,
+      @NonNull BigtableReadOptions options,
+      @NonNull PipelineOptions pipelineOptions)
+      throws IOException {
+    BigtableDataSettings.Builder settings = buildBigtableDataSettings(config, 
pipelineOptions);
+    return configureReadSettings(settings, options);
+  }
+
+  /** Translate BigtableConfig and BigtableWriteOptions to Veneer settings. */
+  static BigtableDataSettings translateWriteToVeneerSettings(
+      @NonNull BigtableConfig config,
+      @NonNull BigtableWriteOptions options,
+      @NonNull PipelineOptions pipelineOptions)
+      throws IOException {
+
+    BigtableDataSettings.Builder settings = buildBigtableDataSettings(config, 
pipelineOptions);
+    return configureWriteSettings(settings, options);
+  }
+
+  /** Translate BigtableConfig and BigtableWriteOptions to Veneer settings. */
+  static BigtableDataSettings translateToVeneerSettings(
+      @NonNull BigtableConfig config, @NonNull PipelineOptions 
pipelineOptions) throws IOException {
+
+    return buildBigtableDataSettings(config, pipelineOptions).build();
+  }
+
+  private static BigtableDataSettings.Builder buildBigtableDataSettings(
+      BigtableConfig config, PipelineOptions pipelineOptions) throws 
IOException {
+    BigtableDataSettings.Builder dataBuilder;
+    boolean emulator = false;
+    if (!Strings.isNullOrEmpty(config.getEmulatorHost())) {
+      emulator = true;
+      String hostAndPort = config.getEmulatorHost();
+      try {
+        int lastIndexOfCol = hostAndPort.lastIndexOf(":");
+        int port = Integer.parseInt(hostAndPort.substring(lastIndexOfCol + 1));
+        dataBuilder =
+            BigtableDataSettings.newBuilderForEmulator(
+                hostAndPort.substring(0, lastIndexOfCol), port);
+      } catch (NumberFormatException | IndexOutOfBoundsException ex) {
+        throw new RuntimeException("Invalid host/port in BigtableConfig " + 
hostAndPort);
+      }
+    } else {
+      dataBuilder = BigtableDataSettings.newBuilder();
+    }
+
+    // Configure target
+    dataBuilder
+        .setProjectId(Objects.requireNonNull(config.getProjectId().get()))
+        .setInstanceId(Objects.requireNonNull(config.getInstanceId().get()));
+    if (config.getAppProfileId() != null
+        && !Strings.isNullOrEmpty(config.getAppProfileId().get())) {
+      
dataBuilder.setAppProfileId(Objects.requireNonNull(config.getAppProfileId().get()));
+    }
+
+    // Skip resetting the credentials if it's connected to an emulator
+    if (!emulator) {
+      if (pipelineOptions.as(GcpOptions.class).getGcpCredential() != null) {
+        dataBuilder
+            .stubSettings()
+            .setCredentialsProvider(
+                FixedCredentialsProvider.create(
+                    
(pipelineOptions.as(GcpOptions.class)).getGcpCredential()));
+      }
+
+      if (config.getCredentialFactory() != null) {
+        CredentialFactory credentialFactory = config.getCredentialFactory();
+        try {
+          dataBuilder
+              .stubSettings()
+              .setCredentialsProvider(
+                  
FixedCredentialsProvider.create(credentialFactory.getCredential()));
+        } catch (GeneralSecurityException e) {
+          throw new RuntimeException("Exception getting credentials ", e);
+        }
+      }
+    }
+
+    configureChannelPool(dataBuilder.stubSettings(), config);
+    configureHeaderProvider(dataBuilder.stubSettings(), pipelineOptions);
+
+    return dataBuilder;
+  }
+
+  private static void configureHeaderProvider(
+      StubSettings.Builder<?, ?> stubSettings, PipelineOptions 
pipelineOptions) {
+
+    ImmutableMap.Builder<String, String> headersBuilder =
+        ImmutableMap.<String, String>builder()
+            .put(
+                GrpcUtil.USER_AGENT_KEY.name(),
+                Objects.requireNonNull(pipelineOptions.getUserAgent()));
+
+    
stubSettings.setHeaderProvider(FixedHeaderProvider.create(headersBuilder.build()));
+  }
+
+  private static void configureChannelPool(
+      StubSettings.Builder<?, ?> stubSettings, BigtableConfig config) {
+    if (config.getChannelCount() != null
+        && stubSettings.getTransportChannelProvider() instanceof 
InstantiatingGrpcChannelProvider) {

Review Comment:
   Lets avoid adding unnecessary defensive code...it makes maintaining this 
harder in the future



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to