From 4cad8583cfb6a008b40e215c88faeb01e4ccd592 Mon Sep 17 00:00:00 2001
From: Chris Larsen <clarsen@yahoo-inc.com>
Date: Tue, 8 Mar 2016 16:46:34 -0800
Subject: [PATCH] [hbase10] Fix the init() method to properly construct a
 single Connection object. Previously N objects were instantiated because the
 method didn't check to see if the connection was null.

---
 .../java/com/yahoo/ycsb/db/HBaseClient10.java  | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)

diff --git a/hbase10/src/main/java/com/yahoo/ycsb/db/HBaseClient10.java b/hbase10/src/main/java/com/yahoo/ycsb/db/HBaseClient10.java
index 00fb615b..a41c1987 100644
--- a/hbase10/src/main/java/com/yahoo/ycsb/db/HBaseClient10.java
+++ b/hbase10/src/main/java/com/yahoo/ycsb/db/HBaseClient10.java
@@ -51,7 +51,6 @@ import java.util.HashMap;
 import java.util.Map;
 import java.util.Set;
 import java.util.Vector;
-import java.util.concurrent.atomic.AtomicInteger;
 
 /**
  * HBase 1.0 client for YCSB framework.
@@ -64,7 +63,9 @@ import java.util.concurrent.atomic.AtomicInteger;
  */
 public class HBaseClient10 extends com.yahoo.ycsb.DB {
   private Configuration config = HBaseConfiguration.create();
-  private static final AtomicInteger THREAD_COUNT = new AtomicInteger(0);
+  
+  // Must be an object for synchronization and tracking running thread counts. 
+  private static Integer threadCount = 0;
 
   private boolean debug = false;
 
@@ -132,9 +133,11 @@ public class HBaseClient10 extends com.yahoo.ycsb.DB {
     }
 
     try {
-      THREAD_COUNT.getAndIncrement();
-      synchronized(THREAD_COUNT) {
-        connection = ConnectionFactory.createConnection(config);
+      synchronized(threadCount) {
+        ++threadCount;
+        if (connection == null) {
+          connection = ConnectionFactory.createConnection(config);
+        }
       }
     } catch (java.io.IOException e) {
       throw new DBException(e);
@@ -190,10 +193,11 @@ public class HBaseClient10 extends com.yahoo.ycsb.DB {
       long en = System.nanoTime();
       final String type = clientSideBuffering ? "UPDATE" : "CLEANUP";
       measurements.measure(type, (int) ((en - st) / 1000));
-      synchronized(THREAD_COUNT) {
-        int threadCount = THREAD_COUNT.decrementAndGet();
+      synchronized(threadCount) {
+        --threadCount;
         if (threadCount <= 0 && connection != null) {
           connection.close();
+          connection = null;
         }
       }
     } catch (IOException e) {
-- 
GitLab