Index: src/main/java/org/apache/hadoop/hbase/client/HTablePool.java =================================================================== --- src/main/java/org/apache/hadoop/hbase/client/HTablePool.java (revision 87552) +++ src/main/java/org/apache/hadoop/hbase/client/HTablePool.java (working copy) @@ -24,6 +24,8 @@ import java.util.Collection; import java.util.List; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicInteger; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; @@ -62,6 +64,7 @@ */ public class HTablePool implements Closeable { private final PoolMap tables; + private final ConcurrentHashMap concurrentUsingTableCounts; private final int maxSize; private final PoolType poolType; private final Configuration config; @@ -159,8 +162,20 @@ } this.tables = new PoolMap(this.poolType, this.maxSize); + this.concurrentUsingTableCounts = new ConcurrentHashMap(); } + private void increaseConcurrentUsingTableCount(String tableName) { + AtomicInteger initCount = new AtomicInteger(0); + AtomicInteger previousCount = null; + if ((previousCount = concurrentUsingTableCounts.putIfAbsent(tableName, initCount)) == null) { + // no count associate with tableName + initCount.incrementAndGet(); + } else { + previousCount.incrementAndGet(); + } + } + /** * Get a reference to the specified table from the pool. *

@@ -175,6 +190,8 @@ public HTableInterface getTable(String tableName) { // call the old getTable implementation renamed to findOrCreateTable HTableInterface table = findOrCreateTable(tableName); + // increase concurrent using table count + increaseConcurrentUsingTableCount(tableName); // return a proxy table so when user closes the proxy, the actual table // will be returned to the pool return new PooledHTable(table); @@ -255,6 +272,9 @@ private void returnTable(HTableInterface table) throws IOException { // this is the old putTable method renamed and made private String tableName = Bytes.toString(table.getTableName()); + // decrease concurrent using table count because table will be closed or pooled + concurrentUsingTableCounts.get(table).decrementAndGet(); + if (tables.size(tableName) >= maxSize) { // release table instance since we're not reusing it this.tables.remove(tableName, table); @@ -314,6 +334,11 @@ public int getCurrentPoolSize(String tableName) { return tables.size(tableName); } + + public int getConcurrentUsingTableCount(String tableName) { + AtomicInteger value = concurrentUsingTableCounts.get(tableName); + return value == null ? 0 : value.get(); + } /** * A proxy class that implements HTableInterface.close method to return the