Zhangyx39 commented on a change in pull request #986: SAMZA-2156: Couchbase 
support for Samza Table API
URL: https://github.com/apache/samza/pull/986#discussion_r276424654
 
 

 ##########
 File path: 
samza-kv-remote/src/main/java/org/apache/samza/table/remote/couchbase/CouchbaseBucketRegistry.java
 ##########
 @@ -0,0 +1,198 @@
+/*
+ * 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.samza.table.remote.couchbase;
+
+import com.couchbase.client.java.Bucket;
+import com.couchbase.client.java.Cluster;
+import com.couchbase.client.java.CouchbaseCluster;
+import com.couchbase.client.java.auth.CertAuthenticator;
+import com.couchbase.client.java.env.CouchbaseEnvironment;
+import com.couchbase.client.java.env.DefaultCouchbaseEnvironment;
+import java.io.Serializable;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * The CouchbaseBucketRegistry is intended to reuse the same {@link Cluster} 
instance given same clusterNodes and reuse
+ * the same {@link Bucket} given same bucketName. Instantiating a Bucket is 
expensive. Different tasks within a
+ * container that is using the same bucket should use this registry to avoid 
creating multiple Buckets.
+ */
+public class CouchbaseBucketRegistry {
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(CouchbaseBucketRegistry.class);
+  private Map<String, Bucket> openedBuckets;
+  private Map<String, Cluster> openedClusters;
+  private Map<String, Integer> bucketUsageCounts;
+  private Map<String, Integer> clusterUsageCounts;
+
+  /**
+   * Constructor of the CouchbaseTableRegistry.
+   */
+  public CouchbaseBucketRegistry() {
+    openedBuckets = new HashMap<>();
+    openedClusters = new HashMap<>();
+    bucketUsageCounts = new HashMap<>();
+    clusterUsageCounts = new HashMap<>();
+  }
+
+  /**
+   * A synchronized method to open a bucket given the bucket name and cluster 
nodes. Once a bucket has been opened,
+   * the reference of the bucket will be stored in the registry, and the same 
reference will be returned given same
+   * bucket name. Each time this method is called, the counter of usage of the 
bucket is increased by one.
+   * @param bucketName name of the bucket to be opened
+   * @param clusterNodes list of cluster nodes
+   * @param configs couchbase environment configs
+   * @return A Bucket instance associated with the bucket name and cluster 
nodes
+   */
+  public synchronized Bucket getBucket(String bucketName, List<String> 
clusterNodes,
 
 Review comment:
   We do need synchronized here. The purpose is that we not only want to keep 
the map correct, but we also want to prevent creating multiple Bucket instances 
in different thread. Think about this:
   1. Thread 1 and thread 2 both check that the "bucket-id" key does not exist 
in the map.
   2. Then they both try to create a new bucket instance and put to the map
   3. Although the map is thread safe and only one Bucket instance can be put 
to the map with key "bucket-id", two instances of Bucket have been created.

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to