tonytanger commented on code in PR #25523:
URL: https://github.com/apache/beam/pull/25523#discussion_r1110407872


##########
sdks/java/io/google-cloud-platform/src/main/java/org/apache/beam/sdk/io/gcp/bigtable/changestreams/dao/BigtableChangeStreamAccessor.java:
##########
@@ -0,0 +1,220 @@
+/*
+ * 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.changestreams.dao;
+
+import static org.apache.beam.sdk.util.Preconditions.checkArgumentNotNull;
+import static org.apache.beam.sdk.util.Preconditions.checkStateNotNull;
+
+import com.google.api.gax.retrying.RetrySettings;
+import com.google.cloud.bigtable.admin.v2.BigtableInstanceAdminClient;
+import com.google.cloud.bigtable.admin.v2.BigtableInstanceAdminSettings;
+import com.google.cloud.bigtable.admin.v2.BigtableTableAdminClient;
+import com.google.cloud.bigtable.admin.v2.BigtableTableAdminSettings;
+import com.google.cloud.bigtable.data.v2.BigtableDataClient;
+import com.google.cloud.bigtable.data.v2.BigtableDataSettings;
+import java.io.IOException;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicInteger;
+import org.apache.beam.sdk.io.gcp.bigtable.BigtableConfig;
+import org.checkerframework.checker.nullness.qual.NonNull;
+import org.threeten.bp.Duration;
+
+/**
+ * This is probably a temporary solution to what is a bigger migration from
+ * cloud-bigtable-client-core to java-bigtable.
+ *
+ * <p>This class creates and maintains the lifecycle java-bigtable clients to 
interact with Cloud
+ * Bigtable. This class creates singletons of data and admin clients for each
+ * project/instance/table/app profile. Per workers, there should only be 1 
instance of the client
+ * for each table/app profile. This ensures we're not creating many/excessive 
connections with the
+ * backend and the jobs on the same machine shares the same sets of 
connections.
+ */
+public class BigtableChangeStreamAccessor implements AutoCloseable {
+  // Create one bigtable data/admin client per bigtable config 
(project/instance/table/app profile)
+  private static final ConcurrentHashMap<BigtableConfig, 
BigtableChangeStreamAccessor>
+      bigtableAccessors = new ConcurrentHashMap<>();
+
+  private static final ConcurrentHashMap<BigtableConfig, AtomicInteger> 
refcounts =
+      new ConcurrentHashMap<>();
+
+  private final BigtableDataClient dataClient;
+  private final BigtableTableAdminClient tableAdminClient;
+  private final BigtableInstanceAdminClient instanceAdminClient;
+  private final BigtableConfig bigtableConfig;
+
+  private BigtableChangeStreamAccessor(
+      BigtableDataClient dataClient,
+      BigtableTableAdminClient tableAdminClient,
+      BigtableInstanceAdminClient instanceAdminClient,
+      BigtableConfig bigtableConfig) {
+    this.dataClient = dataClient;
+    this.tableAdminClient = tableAdminClient;
+    this.instanceAdminClient = instanceAdminClient;
+    this.bigtableConfig = bigtableConfig;
+  }
+
+  /**
+   * Create a BigtableAccess if it doesn't exist and store it in the cache for 
faster access. If it
+   * does exist, just return it.
+   *
+   * @param bigtableConfig config that contains all the parameters to connect 
to a Cloud Bigtable
+   *     instance
+   * @return data and admin clients connected to the Cloud Bigtable instance
+   * @throws IOException if the connection fails
+   */
+  public static synchronized BigtableChangeStreamAccessor getOrCreate(
+      @NonNull BigtableConfig bigtableConfig) throws IOException {
+    if (bigtableAccessors.get(bigtableConfig) == null) {
+      BigtableChangeStreamAccessor bigtableAccessor =
+          BigtableChangeStreamAccessor.createAccessor(bigtableConfig);
+      bigtableAccessors.put(bigtableConfig, bigtableAccessor);
+      refcounts.putIfAbsent(bigtableConfig, new AtomicInteger(0));
+    }
+    checkStateNotNull(refcounts.get(bigtableConfig)).incrementAndGet();
+    return checkStateNotNull(bigtableAccessors.get(bigtableConfig));
+  }
+
+  private static BigtableChangeStreamAccessor createAccessor(@NonNull 
BigtableConfig bigtableConfig)
+      throws IOException {
+    String projectId = 
checkArgumentNotNull(bigtableConfig.getProjectId()).get();
+    String instanceId = 
checkArgumentNotNull(bigtableConfig.getInstanceId()).get();
+    String appProfileId = 
checkArgumentNotNull(bigtableConfig.getAppProfileId()).get();
+    BigtableDataSettings.Builder dataSettingsBuilder = 
BigtableDataSettings.newBuilder();
+    BigtableTableAdminSettings.Builder tableAdminSettingsBuilder =
+        BigtableTableAdminSettings.newBuilder();
+    BigtableInstanceAdminSettings.Builder instanceAdminSettingsBuilder =
+        BigtableInstanceAdminSettings.newBuilder();
+
+    dataSettingsBuilder.setProjectId(projectId);
+    tableAdminSettingsBuilder.setProjectId(projectId);
+    instanceAdminSettingsBuilder.setProjectId(projectId);
+
+    dataSettingsBuilder.setInstanceId(instanceId);
+    tableAdminSettingsBuilder.setInstanceId(instanceId);
+
+    if (appProfileId != null) {
+      dataSettingsBuilder.setAppProfileId(appProfileId);
+    }
+
+    RetrySettings.Builder readRowRetrySettings =

Review Comment:
   Because the default don't work well for us if we are checkpointing 
frequently. A connection might hang or fall in the outliers and is very slow to 
respond. We want to recover quickly from that.



-- 
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