/** * Gets the data of the node, blocking until the node is available. * * @return data of the node * @throws InterruptedException if the waiting thread is interrupted */ public synchronized byte[] blockUntilAvailable() throws InterruptedException { return blockUntilAvailable(0, false); }
/** * Check if there is a master available. * * @return true if there is a master set, false if not. */ public boolean hasMaster() { return super.getData(false) != null; }
@Override public synchronized void nodeDataChanged(String path) { if (path.equals(node)) { nodeCreated(path); } }
@Override public synchronized void nodeCreated(String path) { if (!path.equals(node)) return; try { byte[] data = ZKUtil.getDataAndWatch(watcher, node); if (data != null) { this.data = data; notifyAll(); } else { nodeDeleted(path); } } catch (KeeperException e) { abortable.abort("Unexpected exception handling nodeCreated event", e); } }
/** * Set the balancer on/off * * @param balancerOn * @throws org.apache.zookeeper.KeeperException */ public void setBalancerOn(boolean balancerOn) throws KeeperException { byte[] upData = toByteArray(balancerOn); try { ZKUtil.createAndWatch(watcher, watcher.balancerZNode, upData); } catch (KeeperException.NodeExistsException nee) { ZKUtil.setData(watcher, watcher.balancerZNode, upData); } super.nodeDataChanged(watcher.balancerZNode); }
/** * Checks if cluster is up. * * @return true if the cluster up ('shutdown' is its name up in zk) znode * exists with data, false if not */ public boolean isClusterUp() { return super.getData(false) != null; }
/** * Utilty method to wait indefinitely on a znode availability while checking * if the fserver is shut down * * @param tracker * znode tracker to use * @throws java.io.IOException * any IO exception, plus if the RS is stopped * @throws InterruptedException */ private void blockAndCheckIfStopped(ZooKeeperNodeTracker tracker) throws IOException, InterruptedException { while (tracker.blockUntilAvailable(this.msgInterval, false) == null) { if (this.stopped) { throw new IOException("Received the shutdown message while waiting."); } } }
@Override public synchronized void nodeDeleted(String path) { if (path.equals(node)) { try { if (ZKUtil.watchAndCheckExists(watcher, node)) { nodeCreated(path); } else { this.data = null; } } catch (KeeperException e) { abortable.abort("Unexpected exception handling nodeDeleted event", e); } } }
/** * Get the address of the current master if one is available. Returns null if * no current master. If refresh is set, try to load the data from ZK again, * otherwise, cached data will be used. * * @param refresh * whether to refresh the data by calling ZK directly. * @return Server name or null if timed out. */ public ServerName getMasterAddress(final boolean refresh) { try { return ServerName.parseFrom(super.getData(refresh)); } catch (DeserializationException e) { LOG.warn("Failed parse", e); return null; } }
/** * Return true if the balance switch is on, false otherwise */ public boolean isBalancerOn() { byte[] upData = super.getData(false); try { // if data in ZK is null, use default of on. return upData == null || parseFrom(upData).getBalancerOn(); } catch (DeserializationException dex) { LOG.error("ZK state for LoadBalancer could not be parsed " + Bytes.toStringBinary(upData)); // return false to be safe. return false; } }