Index: src/main/java/org/apache/hadoop/hbase/client/HConnectionManager.java =================================================================== --- src/main/java/org/apache/hadoop/hbase/client/HConnectionManager.java (revision 1145766) +++ src/main/java/org/apache/hadoop/hbase/client/HConnectionManager.java (working copy) @@ -199,10 +199,24 @@ * . */ public static void deleteConnection(Configuration conf, boolean stopProxy) { - deleteConnection(new HConnectionKey(conf), stopProxy); + deleteConnection(new HConnectionKey(conf), stopProxy, false); } /** + * Delete stale connection information for the instance specified by configuration. + * This will then close connection to + * the zookeeper ensemble and let go of all resources. + * + * @param conf + * configuration whose identity is used to find {@link HConnection} + * instance. + * . + */ + public static void deleteStaleConnection(Configuration conf) { + deleteConnection(new HConnectionKey(conf), true, true); + } + + /** * Delete information for all connections. * @param stopProxy stop the proxy as well * @throws IOException @@ -212,7 +226,7 @@ Set connectionKeys = new HashSet(); connectionKeys.addAll(HBASE_INSTANCES.keySet()); for (HConnectionKey connectionKey : connectionKeys) { - deleteConnection(connectionKey, stopProxy); + deleteConnection(connectionKey, stopProxy, false); } HBASE_INSTANCES.clear(); } @@ -223,20 +237,21 @@ for (Entry connectionEntry : HBASE_INSTANCES .entrySet()) { if (connectionEntry.getValue() == connection) { - deleteConnection(connectionEntry.getKey(), stopProxy); + deleteConnection(connectionEntry.getKey(), stopProxy, false); break; } } } } - private static void deleteConnection(HConnectionKey connectionKey, boolean stopProxy) { + private static void deleteConnection(HConnectionKey connectionKey, + boolean stopProxy, boolean staleConnection) { synchronized (HBASE_INSTANCES) { HConnectionImplementation connection = HBASE_INSTANCES .get(connectionKey); if (connection != null) { connection.decCount(); - if (connection.isZeroReference()) { + if (connection.isZeroReference() || staleConnection) { HBASE_INSTANCES.remove(connectionKey); connection.close(stopProxy); } else if (stopProxy) { Index: src/main/java/org/apache/hadoop/hbase/client/HBaseAdmin.java =================================================================== --- src/main/java/org/apache/hadoop/hbase/client/HBaseAdmin.java (revision 1145766) +++ src/main/java/org/apache/hadoop/hbase/client/HBaseAdmin.java (working copy) @@ -21,12 +21,11 @@ import java.io.Closeable; import java.io.IOException; +import java.lang.reflect.UndeclaredThrowableException; import java.net.SocketTimeoutException; import java.util.Arrays; import java.util.LinkedList; import java.util.List; -import java.util.Map; -import java.util.TreeMap; import java.util.concurrent.atomic.AtomicInteger; import java.util.regex.Pattern; @@ -75,7 +74,7 @@ public class HBaseAdmin implements Abortable, Closeable { private final Log LOG = LogFactory.getLog(this.getClass().getName()); // private final HConnection connection; - private final HConnection connection; + private HConnection connection; private volatile Configuration conf; private final long pause; private final int numRetries; @@ -94,11 +93,17 @@ public HBaseAdmin(Configuration c) throws MasterNotRunningException, ZooKeeperConnectionException { this.conf = HBaseConfiguration.create(c); - this.connection = HConnectionManager.getConnection(this.conf); + this.connection = HConnectionManager.getConnection(this.conf); this.pause = this.conf.getLong("hbase.client.pause", 1000); this.numRetries = this.conf.getInt("hbase.client.retries.number", 10); this.retryLongerMultiplier = this.conf.getInt("hbase.client.retries.longer.multiplier", 10); - this.connection.getMaster(); + try { + this.connection.getMaster(); + } catch (UndeclaredThrowableException ute) { + HConnectionManager.deleteStaleConnection(this.conf); + this.connection = HConnectionManager.getConnection(this.conf); + this.connection.getMaster(); + } } /** @@ -146,7 +151,15 @@ */ public HMasterInterface getMaster() throws MasterNotRunningException, ZooKeeperConnectionException { - return this.connection.getMaster(); + HMasterInterface master = null; + try { + master = this.connection.getMaster(); + } catch (UndeclaredThrowableException ute) { + HConnectionManager.deleteStaleConnection(this.conf); + this.connection = HConnectionManager.getConnection(this.conf); + master = this.connection.getMaster(); + } + return master; } /** @return - true if the master server is running @@ -154,7 +167,15 @@ * @throws MasterNotRunningException */ public boolean isMasterRunning() throws MasterNotRunningException, ZooKeeperConnectionException { - return this.connection.isMasterRunning(); + boolean isMasterRunning = false; + try { + isMasterRunning = this.connection.isMasterRunning(); + } catch (UndeclaredThrowableException ute) { + HConnectionManager.deleteStaleConnection(this.conf); + this.connection = HConnectionManager.getConnection(this.conf); + isMasterRunning = this.connection.isMasterRunning(); + } + return isMasterRunning; } /**