private void setTableState(final String tableName, final ZooKeeperProtos.Table.State state) throws KeeperException { String znode = ZKUtil.joinZNode(this.watcher.tableZNode, tableName); if (ZKUtil.checkExists(this.watcher, znode) == -1) { ZKUtil.createAndFailSilent(this.watcher, znode); } synchronized (this.cache) { ZooKeeperProtos.Table.Builder builder = ZooKeeperProtos.Table .newBuilder(); builder.setState(state); byte[] data = builder.build().toByteArray(); ZKUtil.setData(this.watcher, znode, data); this.cache.put(tableName, state); } }
/** * Delete the specified node. Sets no watches. Throws all exceptions. */ public static void deleteNode(ZooKeeperWatcher zkw, String node) throws KeeperException { deleteNode(zkw, node, -1); }
/** * Deletes the table in zookeeper. Fails silently if the table is not * currently disabled in zookeeper. Sets no watches. * * @param tableName * @throws org.apache.zookeeper.KeeperException * unexpected zookeeper exception */ public void setDeletedTable(final String tableName) throws KeeperException { synchronized (this.cache) { if (this.cache.remove(tableName) == null) { LOG.warn("Moving table " + tableName + " state to deleted but was " + "already deleted"); } ZKUtil.deleteNodeFailSilent(this.watcher, ZKUtil.joinZNode(this.watcher.tableZNode, tableName)); } }
/** * Set data into node creating node if it doesn't yet exist. Does not set * watch. * * @param zkw * zk reference * @param znode * path of node * @param data * data to set for node * @throws org.apache.zookeeper.KeeperException */ public static void createSetData(final ZooKeeperWatcher zkw, final String znode, final byte[] data) throws KeeperException { if (checkExists(zkw, znode) == -1) { ZKUtil.createWithParents(zkw, znode); } ZKUtil.setData(zkw, znode, data); }
/** * List all the children of the specified znode, setting a watch for children * changes and also setting a watch on every individual child in order to get * the NodeCreated and NodeDeleted events. * * @param zkw * zookeeper reference * @param znode * node to get children of and watch * @return list of znode names, null if the node doesn't exist * @throws org.apache.zookeeper.KeeperException */ public static List<String> listChildrenAndWatchThem(ZooKeeperWatcher zkw, String znode) throws KeeperException { List<String> children = listChildrenAndWatchForNewChildren(zkw, znode); if (children == null) { return null; } for (String child : children) { watchAndCheckExists(zkw, joinZNode(znode, child)); } return children; }
public static ClusterId readClusterIdZNode(ZooKeeperWatcher watcher) throws KeeperException { if (ZKUtil.checkExists(watcher, watcher.clusterIdZNode) != -1) { byte[] data = ZKUtil.getData(watcher, watcher.clusterIdZNode); if (data != null) { try { return ClusterId.parseFrom(data); } catch (DeserializationException e) { throw ZKUtil.convert(e); } } } return null; }
String backupZNode = ZKUtil.joinZNode( this.watcher.backupMasterAddressesZNode, this.sn.toString()); if (MasterAddressTracker.setMasterAddress(this.watcher, if (ZKUtil.checkExists(this.watcher, backupZNode) != -1) { LOG.info("Deleting ZNode for " + backupZNode + " from backup master directory"); ZKUtil.deleteNodeFailSilent(this.watcher, backupZNode); byte[] bytes = ZKUtil.getDataAndWatch(this.watcher, this.watcher.getMasterAddressZNode()); if (bytes == null) { msg = ("Current master has this master's address, " + currentMaster + "; master was restarted? Deleting node."); ZKUtil.deleteNode(this.watcher, this.watcher.getMasterAddressZNode());
return; waitForZKConnectionIfAuthenticating(zkw); zkw.getRecoverableZooKeeper().create(znode, new byte[0], createACL(zkw, znode), CreateMode.PERSISTENT); } catch (KeeperException.NodeExistsException nee) { return; } catch (KeeperException.NoNodeException nne) { createWithParents(zkw, getParent(znode)); createWithParents(zkw, znode); } catch (InterruptedException ie) { zkw.interruptedException(ie);
String znode, byte[] data) throws KeeperException { try { waitForZKConnectionIfAuthenticating(zkw); zkw.getRecoverableZooKeeper().create(znode, data, createACL(zkw, znode), CreateMode.EPHEMERAL); } catch (KeeperException.NodeExistsException nee) { if (!watchAndCheckExists(zkw, znode)) { return createEphemeralNodeAndWatch(zkw, znode, data);
ZKUtil.waitForZKConnectionIfAuthenticating(watcher); } catch (InterruptedException e) { throw new IllegalStateException("ZookeeperNodeTracker on " + this.node if (ZKUtil.watchAndCheckExists(watcher, node)) { byte[] data = ZKUtil.getDataAndWatch(watcher, node); if (data != null) { this.data = data;
try { this.data = ZKUtil.getDataAndWatch(watcher, node); } catch (KeeperException e) { if (!nodeExistsChecked) { try { nodeExistsChecked = (ZKUtil.checkExists(watcher, node) != -1); } catch (KeeperException e) { LOG.warn( try { this.data = ZKUtil.getDataAndWatch(watcher, node); } catch (KeeperException e) { LOG.warn("Unexpected exception handling blockUntilAvailable", e);
/** * @return True if cluster has an active master. */ public boolean isActiveMaster() { try { if (ZKUtil.checkExists(watcher, watcher.getMasterAddressZNode()) >= 0) { return true; } } catch (KeeperException ke) { LOG.info("Received an unexpected KeeperException when checking " + "isActiveMaster : " + ke); } return false; }
/** * Gets the current data in the unassigned node for the specified entityGroup * name or fully-qualified path. * * <p> * Returns null if the entityGroup does not currently have a node. * * <p> * Sets a watch on the node if the node exists. * * @param zkw zk reference * @param pathOrEntityGroupName fully-specified path or entityGroup name * @return znode content * @throws org.apache.zookeeper.KeeperException if unexpected zookeeper exception */ public static byte[] getData(ZooKeeperWatcher zkw, String pathOrEntityGroupName) throws KeeperException { String node = getPath(zkw, pathOrEntityGroupName); return ZKUtil.getDataAndWatch(zkw, node); }
String node = getNodeName(zkw, entityGroup.getEncodedName()); zkw.sync(node); int version = ZKUtil.checkExists(zkw, node); if (version == -1) { return ZKUtil.createAndWatch(zkw, node, data); } else { boolean setData = false; try { setData = ZKUtil.setData(zkw, node, data, version);
/** * Blocks until there is at least one node in entityGroups in transition. * <p> * Used in testing only. * @param zkw zk reference * @throws org.apache.zookeeper.KeeperException * @throws InterruptedException */ public static void blockUntilRIT(ZooKeeperWatcher zkw) throws KeeperException, InterruptedException { while (!ZKUtil.nodeHasChildren(zkw, zkw.assignmentZNode)) { List<String> znodes = ZKUtil.listChildrenAndWatchForNewChildren(zkw, zkw.assignmentZNode); if (znodes == null || znodes.isEmpty()) { LOG.debug("No RIT in ZK"); } Thread.sleep(100); } }
/** * delete the master znode if its content is same as the parameter */ public static boolean deleteIfEquals(ZooKeeperWatcher zkw, final String content) { if (content == null) { throw new IllegalArgumentException("Content must not be null"); } try { Stat stat = new Stat(); byte[] data = ZKUtil.getDataNoWatch(zkw, zkw.getMasterAddressZNode(), stat); ServerName sn = ServerName.parseFrom(data); if (sn != null && content.equals(sn.toString())) { return (ZKUtil.deleteNode(zkw, zkw.getMasterAddressZNode(), stat.getVersion())); } } catch (KeeperException e) { LOG.warn("Can't get or delete the master znode", e); } catch (DeserializationException e) { LOG.warn("Can't get or delete the master znode", e); } return false; } }
String nodeName = ZKAssign.getNodeName(zkw, entityGroup .getEntityGroupInfo().getEncodedName()); ZKUtil.getDataAndWatch(zkw, nodeName, stat); List<String> znodes = ZKUtil.listChildrenAndWatchForNewChildren(zkw, zkw.assignmentZNode); String entityGroupName = znodes.get(0);
byte[] data, final AsyncCallback.StringCallback cb, final Object ctx) { try { waitForZKConnectionIfAuthenticating(zkw); zkw.getRecoverableZooKeeper().getZooKeeper().create(znode, data, createACL(zkw, znode), CreateMode.PERSISTENT, cb, ctx); } catch (InterruptedException e) { zkw.interruptedException(e);
/** * Delete the assignment node regardless of its current state. * <p> * Fail silent even if the node does not exist at all. * @param watcher * @param entityGroupInfo * @throws org.apache.zookeeper.KeeperException */ public static void deleteNodeFailSilent(ZooKeeperWatcher watcher, EntityGroupInfo entityGroupInfo) throws KeeperException { String node = getNodeName(watcher, entityGroupInfo.getEncodedName()); ZKUtil.deleteNodeFailSilent(watcher, node); }
nodes = ZKUtil.listChildrenAndWatchForNewChildren(this.watcher, this.watcher.assignmentZNode); if (!nodes.isEmpty()) {