/** * Handles KeeperExceptions in client calls. * <p> * This may be temporary but for now this gives one place to deal with these. * <p> * TODO: Currently this method rethrows the exception to let the caller handle * <p> * @param ke the exception to rethrow * @throws KeeperException if a ZooKeeper operation fails */ public void keeperException(KeeperException ke) throws KeeperException { LOG.error(prefix("Received unexpected KeeperException, re-throwing exception"), ke); throw ke; }
/** * Log the InterruptedException and interrupt current thread * @param ie The IterruptedException to log * @param throwLater Whether we will throw the exception latter */ public void interruptedExceptionNoThrow(InterruptedException ie, boolean throwLater) { LOG.debug(prefix("Received InterruptedException, will interrupt current thread" + (throwLater ? " and rethrow a SystemErrorException" : "")), ie); // At least preserve interrupt. Thread.currentThread().interrupt(); }
boolean exists = s != null ? true : false; if (exists) { LOG.debug(zkw.prefix("Set watcher on existing znode=" + znode)); } else { LOG.debug(zkw.prefix("Set watcher on znode that does not yet exist, " + znode)); LOG.warn(zkw.prefix("Unable to set watcher on znode " + znode), e); zkw.keeperException(e); return false; } catch (InterruptedException e) { LOG.warn(zkw.prefix("Unable to set watcher on znode " + znode), e); zkw.interruptedException(e); return false;
private static byte[] getDataInternal(ZKWatcher zkw, String znode, Stat stat, boolean watcherSet) throws KeeperException { try { byte [] data = zkw.getRecoverableZooKeeper().getData(znode, zkw, stat); logRetrievedMsg(zkw, znode, data, watcherSet); return data; } catch (KeeperException.NoNodeException e) { // This log can get pretty annoying when we cycle on 100ms waits. // Enable trace if you really want to see it. LOG.trace(zkw.prefix("Unable to get data of znode " + znode + " " + "because node does not exist (not an error)")); return null; } catch (KeeperException e) { LOG.warn(zkw.prefix("Unable to get data of znode " + znode), e); zkw.keeperException(e); return null; } catch (InterruptedException e) { LOG.warn(zkw.prefix("Unable to get data of znode " + znode), e); zkw.interruptedException(e); return null; } }
return !zkw.getRecoverableZooKeeper().getChildren(znode, null).isEmpty(); } catch(KeeperException.NoNodeException ke) { LOG.debug(zkw.prefix("Unable to list children of znode " + znode + " because node does not exist (not an error)")); return false; } catch (KeeperException e) { LOG.warn(zkw.prefix("Unable to list children of znode " + znode), e); zkw.keeperException(e); return false; } catch (InterruptedException e) { LOG.warn(zkw.prefix("Unable to list children of znode " + znode), e); zkw.interruptedException(e); return false;
return children; } catch(KeeperException.NoNodeException ke) { LOG.debug(zkw.prefix("Unable to list children of znode " + znode + " " + "because node does not exist (not an error)")); return null; } catch (KeeperException e) { LOG.warn(zkw.prefix("Unable to list children of znode " + znode + " "), e); zkw.keeperException(e); return null; } catch (InterruptedException e) { LOG.warn(zkw.prefix("Unable to list children of znode " + znode + " "), e); zkw.interruptedException(e); return null;
/** * Get znode data. Does not set a watcher. * * @return ZNode data, null if the node does not exist or if there is an error. */ public static byte [] getData(ZKWatcher zkw, String znode) throws KeeperException, InterruptedException { try { byte [] data = zkw.getRecoverableZooKeeper().getData(znode, null, null); logRetrievedMsg(zkw, znode, data, false); return data; } catch (KeeperException.NoNodeException e) { LOG.debug(zkw.prefix("Unable to get data of znode " + znode + " " + "because node does not exist (not an error)")); return null; } catch (KeeperException e) { LOG.warn(zkw.prefix("Unable to get data of znode " + znode), e); zkw.keeperException(e); return null; } }
return data; } catch (KeeperException.NoNodeException e) { LOG.debug(zkw.prefix("Unable to get data of znode " + znode + " " + "because node does not exist (not necessarily an error)")); return null; } catch (KeeperException e) { LOG.warn(zkw.prefix("Unable to get data of znode " + znode), e); zkw.keeperException(e); return null; } catch (InterruptedException e) { LOG.warn(zkw.prefix("Unable to get data of znode " + znode), e); zkw.interruptedException(e); return null;
/** * Check if the specified node exists. Sets no watches. * * @param zkw zk reference * @param znode path of node to watch * @return version of the node if it exists, -1 if does not exist * @throws KeeperException if unexpected zookeeper exception */ public static int checkExists(ZKWatcher zkw, String znode) throws KeeperException { try { Stat s = zkw.getRecoverableZooKeeper().exists(znode, null); return s != null ? s.getVersion() : -1; } catch (KeeperException e) { LOG.warn(zkw.prefix("Unable to set watcher on znode (" + znode + ")"), e); zkw.keeperException(e); return -1; } catch (InterruptedException e) { LOG.warn(zkw.prefix("Unable to set watcher on znode (" + znode + ")"), e); zkw.interruptedException(e); return -1; } }
private static void logRetrievedMsg(final ZKWatcher zkw, final String znode, final byte [] data, final boolean watcherSet) { if (!LOG.isTraceEnabled()) { return; } LOG.trace(zkw.prefix("Retrieved " + ((data == null)? 0: data.length) + " byte(s) of data from znode " + znode + (watcherSet? " and set watcher; ": "; data=") + (data == null? "null": data.length == 0? "empty": ( znode.startsWith(zkw.getZNodePaths().metaZNodePrefix)? getServerNameOrEmptyString(data): znode.startsWith(zkw.getZNodePaths().backupMasterAddressesZNode)? getServerNameOrEmptyString(data): StringUtils.abbreviate(Bytes.toStringBinary(data), 32))))); }
/** * Watch the specified znode, but only if exists. Useful when watching * for deletions. Uses .getData() (and handles NoNodeException) instead * of .exists() to accomplish this, as .getData() will only set a watch if * the znode exists. * @param zkw zk reference * @param znode path of node to watch * @return true if the watch is set, false if node does not exists * @throws KeeperException if unexpected zookeeper exception */ public static boolean setWatchIfNodeExists(ZKWatcher zkw, String znode) throws KeeperException { try { zkw.getRecoverableZooKeeper().getData(znode, true, null); return true; } catch (NoNodeException e) { return false; } catch (InterruptedException e) { LOG.warn(zkw.prefix("Unable to set watcher on znode " + znode), e); zkw.interruptedException(e); return false; } }
public void stop() { try { synchronized (clusterHasActiveMaster) { // Master is already stopped, wake up the manager // thread so that it can shutdown soon. clusterHasActiveMaster.notifyAll(); } // If our address is in ZK, delete it on our way out ServerName activeMaster = null; try { activeMaster = MasterAddressTracker.getMasterAddress(this.watcher); } catch (IOException e) { LOG.warn("Failed get of master address: " + e.toString()); } if (activeMaster != null && activeMaster.equals(this.sn)) { ZKUtil.deleteNode(watcher, watcher.getZNodePaths().masterAddressZNode); // We may have failed to delete the znode at the previous step, but // we delete the file anyway: a second attempt to delete the znode is likely to fail again. ZNodeClearer.deleteMyEphemeralNodeOnDisk(); } } catch (KeeperException e) { LOG.debug(this.watcher.prefix("Failed delete of our master address node; " + e.getMessage())); } } }
/** * Get the number of children of the specified node. * * If the node does not exist or has no children, returns 0. * * Sets no watches at all. * * @param zkw zk reference * @param znode path of node to count children of * @return number of children of specified node, 0 if none or parent does not * exist * @throws KeeperException if unexpected zookeeper exception */ public static int getNumberOfChildren(ZKWatcher zkw, String znode) throws KeeperException { try { Stat stat = zkw.getRecoverableZooKeeper().exists(znode, null); return stat == null ? 0 : stat.getNumChildren(); } catch(KeeperException e) { LOG.warn(zkw.prefix("Unable to get children of node " + znode)); zkw.keeperException(e); } catch(InterruptedException e) { zkw.interruptedException(e); } return 0; }
LOG.debug(prefix("Received ZooKeeper Event, " + "type=" + event.getType() + ", " + "state=" + event.getState() + ", " +
private void createBaseZNodes() throws ZooKeeperConnectionException { try { // Create all the necessary "directories" of znodes ZKUtil.createWithParents(this, znodePaths.baseZNode); ZKUtil.createAndFailSilent(this, znodePaths.rsZNode); ZKUtil.createAndFailSilent(this, znodePaths.drainingZNode); ZKUtil.createAndFailSilent(this, znodePaths.tableZNode); ZKUtil.createAndFailSilent(this, znodePaths.splitLogZNode); ZKUtil.createAndFailSilent(this, znodePaths.backupMasterAddressesZNode); ZKUtil.createAndFailSilent(this, znodePaths.tableLockZNode); ZKUtil.createAndFailSilent(this, znodePaths.masterMaintZNode); } catch (KeeperException e) { throw new ZooKeeperConnectionException( prefix("Unexpected KeeperException creating base node"), e); } }
} catch (KeeperException ke) { throw new HBaseIOException( this.zooKeeper.prefix("Unable to decommission '" + server.getServerName() + "'."), ke);
} catch (KeeperException ke) { throw new HBaseIOException( this.zooKeeper.prefix("Unable to recommission '" + server.getServerName() + "'."), ke);
/** * Log the InterruptedException and interrupt current thread * @param ie The IterruptedException to log * @param throwLater Whether we will throw the exception latter */ public void interruptedExceptionNoThrow(InterruptedException ie, boolean throwLater) { LOG.debug(prefix("Received InterruptedException, will interrupt current thread" + (throwLater ? " and rethrow a SystemErrorException" : "")), ie); // At least preserve interrupt. Thread.currentThread().interrupt(); }