From 27739350c08e3f0488c7d9823017cbe18f39817e Mon Sep 17 00:00:00 2001 From: Zhihong Yu Date: Sun, 6 Jan 2013 17:11:27 +0000 Subject: [PATCH] HBASE-7369 HConnectionManager should remove aborted connections (Bryan Baugher) git-svn-id: https://svn.apache.org/repos/asf/hbase/trunk@1429556 13f79535-47bb-0310-9956-ffa450edef68 --- .../hbase/client/HConnectionManager.java | 13 +++++++-- .../apache/hadoop/hbase/client/TestHCM.java | 27 +++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/client/HConnectionManager.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/client/HConnectionManager.java index 793bb6996f1..7d6f307b807 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/client/HConnectionManager.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/client/HConnectionManager.java @@ -211,6 +211,10 @@ public class HConnectionManager { if (connection == null) { connection = new HConnectionImplementation(conf, true); HBASE_INSTANCES.put(connectionKey, connection); + } else if (connection.isClosed()) { + HConnectionManager.deleteConnection(connectionKey, true, true); + connection = new HConnectionImplementation(conf, true); + HBASE_INSTANCES.put(connectionKey, connection); } connection.incCount(); return connection; @@ -2196,13 +2200,14 @@ public class HConnectionManager { closeZooKeeperWatcher(); } } - }else { + } else { if (t != null) { LOG.fatal(msg, t); } else { LOG.fatal(msg); } this.aborted = true; + close(); this.closed = true; } } @@ -2282,7 +2287,11 @@ public class HConnectionManager { @Override public void close() { if (managed) { - HConnectionManager.deleteConnection(this, stopProxy, false); + if (aborted) { + HConnectionManager.deleteStaleConnection(this); + } else { + HConnectionManager.deleteConnection(this, stopProxy, false); + } } else { close(true); } diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestHCM.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestHCM.java index 198e9e60ae4..02e86cbf3dd 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestHCM.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestHCM.java @@ -27,7 +27,10 @@ import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.util.ArrayList; import java.util.Arrays; +import java.util.HashMap; import java.util.List; +import java.util.Map; +import java.util.Map.Entry; import java.util.Random; import java.util.concurrent.SynchronousQueue; import java.util.concurrent.ThreadPoolExecutor; @@ -37,6 +40,8 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.*; +import org.apache.hadoop.hbase.client.HConnectionManager.HConnectionImplementation; +import org.apache.hadoop.hbase.client.HConnectionManager.HConnectionKey; import org.apache.hadoop.hbase.master.HMaster; import org.apache.hadoop.hbase.regionserver.HRegionServer; import org.apache.hadoop.hbase.util.Bytes; @@ -100,6 +105,28 @@ public class TestHCM { private static int getHConnectionManagerCacheSize(){ return HConnectionTestingUtility.getConnectionCount(); } + + @Test + public void abortingHConnectionRemovesItselfFromHCM() throws Exception { + // Save off current HConnections + Map oldHBaseInstances = + new HashMap(); + oldHBaseInstances.putAll(HConnectionManager.HBASE_INSTANCES); + + HConnectionManager.HBASE_INSTANCES.clear(); + + try { + HConnection connection = HConnectionManager.getConnection(TEST_UTIL.getConfiguration()); + connection.abort("test abortingHConnectionRemovesItselfFromHCM", new Exception( + "test abortingHConnectionRemovesItselfFromHCM")); + Assert.assertNotSame(connection, + HConnectionManager.getConnection(TEST_UTIL.getConfiguration())); + } finally { + // Put original HConnections back + HConnectionManager.HBASE_INSTANCES.clear(); + HConnectionManager.HBASE_INSTANCES.putAll(oldHBaseInstances); + } + } /** * Test that when we delete a location using the first row of a region