/** * 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 * @throws org.apache.zookeeper.KeeperException */ public void keeperException(KeeperException ke) throws KeeperException { LOG.error( prefix("Received unexpected KeeperException, re-throwing exception"), ke); throw ke; }
/** * Handles InterruptedExceptions in client calls. * <p> * This may be temporary but for now this gives one place to deal with these. * <p> * TODO: Currently, this method does nothing. Is this ever expected to happen? * Do we abort or can we let it run? Maybe this should be logged as WARN? It * shouldn't happen? * <p> * @param ie */ public void interruptedException(InterruptedException ie) { LOG.debug(prefix("Received InterruptedException, doing nothing here"), ie); // At least preserver interrupt. Thread.currentThread().interrupt(); // no-op }
/** * Deletes all unassigned nodes regardless of their state. * * <p> * No watchers are set. * * <p> * This method is used by the Master during cluster startup to clear out any * existing state from other cluster runs. * * @param zkw zk reference * @throws org.apache.zookeeper.KeeperException if unexpected zookeeper exception */ public static void deleteAllNodes(ZooKeeperWatcher zkw) throws KeeperException { LOG.debug(zkw.prefix("Deleting any existing unassigned nodes")); ZKUtil.deleteChildrenRecursively(zkw, zkw.assignmentZNode); }
boolean exists = s != null ? true : false; if (exists) { LOG.debug(zkw.prefix("Set watcher on existing znode " + znode)); } else { LOG.debug(zkw.prefix(znode + " does not exist. Watcher is set.")); 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(ZooKeeperWatcher 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; } }
/** * 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(ZooKeeperWatcher zkw, String znode) throws KeeperException { 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; } 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); } catch (InterruptedException e) { LOG .warn(zkw.prefix("Unable to list children of znode " + znode + " "), e); zkw.interruptedException(e);
private static void logRetrievedMsg(final ZooKeeperWatcher zkw, final String znode, final byte[] data, final boolean watcherSet) { if (!LOG.isDebugEnabled()) return; LOG .debug(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.assignmentZNode) ? ZKAssign .toString(data) : // We should not be doing this // reaching into another class znode.startsWith(zkw.backupMasterAddressesZNode) ? getServerNameOrEmptyString(data) : StringUtils.abbreviate(Bytes .toStringBinary(data), 32))))); }
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 org.apache.zookeeper.KeeperException * if unexpected zookeeper exception */ public static int checkExists(ZooKeeperWatcher 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; } }
public void stop() { try { // 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.getMasterAddressZNode()); // 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.error( this.watcher.prefix("Error deleting our own master address node"), e); } } }
public static void createNodeOffline(ZooKeeperWatcher zkw, EntityGroupInfo entityGroup, ServerName serverName, final EventType event) throws KeeperException, KeeperException.NodeExistsException { LOG.debug(zkw.prefix("Creating unassigned node for " + entityGroup.getEncodedName() + " in OFFLINE state")); EntityGroupTransaction rt = EntityGroupTransaction.createEntityGroupTransition(event, entityGroup.getEntityGroupName(), serverName); String node = getNodeName(zkw, entityGroup.getEncodedName()); ZKUtil.createAndWatch(zkw, node, rt.toByteArray()); }
/** * 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 org.apache.zookeeper.KeeperException * if unexpected zookeeper exception */ public static int getNumberOfChildren(ZooKeeperWatcher 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; }
private void createBaseZNodes() throws ZooKeeperConnectionException { try { // Create all the necessary "directories" of znodes ZKUtil.createAndFailSilent(this, baseZNode); ZKUtil.createAndFailSilent(this, assignmentZNode); ZKUtil.createAndFailSilent(this, fsZNode); ZKUtil.createAndFailSilent(this, drainingZNode); ZKUtil.createAndFailSilent(this, tableZNode); ZKUtil.createAndFailSilent(this, backupMasterAddressesZNode); } catch (KeeperException e) { throw new ZooKeeperConnectionException( prefix("Unexpected KeeperException creating base node"), e); } }
LOG.debug(prefix("Received ZooKeeper Event, " + "type=" + event.getType() + ", " + "state=" + event.getState() + ", " + "path=" + event.getPath()));
final EntityGroupInfo entityGroup, final ServerName serverName) throws KeeperException, IOException { LOG.debug(zkw.prefix("Creating ephemeral node for " + entityGroup.getEncodedName() + " in SPLITTING state")); EntityGroupTransaction data = EntityGroupTransaction
/** * Creates an unassigned node in the OFFLINE state for the specified * entityGroup. * <p> * Runs asynchronously. Depends on no pre-existing znode. * * <p> * Sets a watcher on the unassigned entityGroup node. * * @param zkw zk reference * @param entityGroup entityGroup to be created as offline * @param serverName server transition will happen on * @param cb * @param ctx * @throws org.apache.zookeeper.KeeperException if unexpected zookeeper exception * @throws org.apache.zookeeper.KeeperException.NodeExistsException if node already exists */ public static void asyncCreateNodeOffline(ZooKeeperWatcher zkw, EntityGroupInfo entityGroup, ServerName serverName, final AsyncCallback.StringCallback cb, final Object ctx) throws KeeperException { LOG.debug(zkw.prefix("Async create of unassigned node for " + entityGroup.getEncodedName() + " with OFFLINE state")); EntityGroupTransaction rt = EntityGroupTransaction.createEntityGroupTransition( EventType.M_ZK_ENTITYGROUP_OFFLINE, entityGroup.getEntityGroupName(), serverName); String node = getNodeName(zkw, entityGroup.getEncodedName()); ZKUtil.asyncCreate(zkw, node, rt.toByteArray(), cb, ctx); }
ServerName serverName) throws KeeperException, KeeperException.NodeExistsException { LOG.debug(zkw.prefix("Creating unassigned node for " + entityGroup.getEncodedName() + " in a CLOSING state")); EntityGroupTransaction rt = EntityGroupTransaction.createEntityGroupTransition(