Index: src/main/java/org/apache/hadoop/hbase/zookeeper/ZKUtil.java =================================================================== --- src/main/java/org/apache/hadoop/hbase/zookeeper/ZKUtil.java (revision 1308444) +++ src/main/java/org/apache/hadoop/hbase/zookeeper/ZKUtil.java (working copy) @@ -258,9 +258,26 @@ */ public static int checkExists(ZooKeeperWatcher zkw, String znode) throws KeeperException { + return checkExists(zkw, znode, 0); + } + + /** + * Check if the specified node exists. Sets no watches. + * + * @param zkw zk reference + * @param znode path of node to watch + * @param timeout maximum time in ms after znode is considered not available. + * @return version of the node if it exists, -1 if does not exist + * @throws KeeperException if unexpected zookeeper exception + */ + public static int checkExists(ZooKeeperWatcher zkw, String znode, int timeout) + throws KeeperException { try { - Stat s = zkw.getRecoverableZooKeeper().exists(znode, null); - return s != null ? s.getVersion() : -1; + if (timeout == 0) { + Stat s = zkw.getRecoverableZooKeeper().exists(znode, null); + return s != null ? s.getVersion() : -1; + } + return checkExists(zkw.getRecoverableZooKeeper().getZooKeeper(), znode, timeout); } catch (KeeperException e) { LOG.warn(zkw.prefix("Unable to set watcher on znode (" + znode + ")"), e); zkw.keeperException(e); @@ -272,6 +289,36 @@ } } + /** + * Check if the specified node exists. Sets no watches. + * + * @param zk zk reference + * @param znode path of node to watch + * @param timeout maximum time in ms after znode is considered not available. + * @return version of the node if it exists, -1 if does not exist + * @throws KeeperException if unexpected zookeeper exception + */ + public static int checkExists(ZooKeeper zk, String znode, int timeout) + throws KeeperException, InterruptedException { + final int maxNumAttempts = timeout / HConstants.SOCKET_RETRY_WAIT_MS; + + KeeperException keeperEx = null; + int attempt = 0; + do { + try { + Stat s = zk.exists(znode, false); + if (s != null) return s.getVersion(); + keeperEx = null; + } catch (KeeperException e) { + keeperEx = e; + } + Threads.sleepWithoutInterrupt(HConstants.SOCKET_RETRY_WAIT_MS); + } while (++attempt < maxNumAttempts); + + if (keeperEx != null) throw keeperEx; + return(-1); + } + // // Znode listings // @@ -1142,23 +1189,14 @@ HConstants.DEFAULT_ZK_SESSION_TIMEOUT), EmptyWatcher.instance); final int maxTimeMs = 10000; - final int maxNumAttempts = maxTimeMs / HConstants.SOCKET_RETRY_WAIT_MS; - KeeperException keeperEx = null; try { try { - for (int attempt = 0; attempt < maxNumAttempts; ++attempt) { - try { - if (zk.exists(parentZNode, false) != null) { - LOG.info("Parent znode exists: " + parentZNode); - keeperEx = null; - break; - } - } catch (KeeperException e) { - keeperEx = e; - } - Threads.sleepWithoutInterrupt(HConstants.SOCKET_RETRY_WAIT_MS); + if (checkExists(zk, parentZNode, maxTimeMs) != -1) { + LOG.info("Parent znode exists: " + parentZNode); } + } catch (KeeperException ex) { + throw new IOException(ex); } finally { zk.close(); } @@ -1166,9 +1204,7 @@ Thread.currentThread().interrupt(); } - if (keeperEx != null) { - throw new IOException(keeperEx); - } + throw new IOException("Parent znode not found after " + maxTimeMs + "ms timeout"); } Index: src/main/java/org/apache/hadoop/hbase/zookeeper/ZooKeeperNodeTracker.java =================================================================== --- src/main/java/org/apache/hadoop/hbase/zookeeper/ZooKeeperNodeTracker.java (revision 1308444) +++ src/main/java/org/apache/hadoop/hbase/zookeeper/ZooKeeperNodeTracker.java (working copy) @@ -234,8 +234,19 @@ * false if doesnot exists. */ public boolean checkIfBaseNodeAvailable() { + return checkIfBaseNodeAvailable(0); + } + + /** + * Checks if the baseznode set as per the property 'zookeeper.znode.parent' + * exists. + * @param timeout maximum time in ms after znode is considered not available. + * @return true if baseznode exists. + * false if doesnot exists. + */ + public boolean checkIfBaseNodeAvailable(int timeout) { try { - if (ZKUtil.checkExists(watcher, watcher.baseZNode) == -1) { + if (ZKUtil.checkExists(watcher, watcher.baseZNode, timeout) == -1) { return false; } } catch (KeeperException e) { Index: src/main/java/org/apache/hadoop/hbase/regionserver/HRegionServer.java =================================================================== --- src/main/java/org/apache/hadoop/hbase/regionserver/HRegionServer.java (revision 1308444) +++ src/main/java/org/apache/hadoop/hbase/regionserver/HRegionServer.java (working copy) @@ -615,7 +615,8 @@ */ private void blockAndCheckIfStopped(ZooKeeperNodeTracker tracker) throws IOException, InterruptedException { - if (false == tracker.checkIfBaseNodeAvailable()) { + int timeout = conf.getInt("hbase.basenode.avail.timeout", 1000); + if (false == tracker.checkIfBaseNodeAvailable(timeout)) { String errorMsg = "Check the value configured in 'zookeeper.znode.parent'. " + "There could be a mismatch with the one configured in the master."; LOG.error(errorMsg);