/** {@inheritDoc} */ @Override public void close() throws IOException { synchronized (mMonitor) { if (mCurrentNode != null) { try { if (ZooKeeperMonitor.this.mZKClient.exists(mCurrentNode) != null) { ZooKeeperMonitor.this.mZKClient.delete(mCurrentNode, -1); } } catch (KeeperException e) { throw new IOException(e); } } } } }
/** * Creates a ZooKeeper node and all its parents, if necessary. * * @param path of the node to create. * @throws KeeperException on I/O error. */ public void createNodeRecursively(File path) throws KeeperException { if (exists(path) != null) { return; } if (path.getPath().equals("/")) { // No need to create the root node "/" : return; } final File parent = path.getParentFile(); if (parent != null) { createNodeRecursively(parent); } try { LOG.debug("Creating ZooKeeper node: {}", path); final File createdPath = this.create(path, EMPTY_BYTES, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); Preconditions.checkState(createdPath.equals(path)); } catch (NodeExistsException exn) { LOG.debug("ZooKeeper node already exists: {}", path); } }
final File preceding = new File(mLockDir, children[index - 1]); LOG.debug("{}: waiting for preceding node {} to disappear", this, preceding); if (mZKClient.exists(preceding, mLockWatcher) != null) { if (absoluteDeadline > 0.0) { final long timeLeftMS = (long) ((absoluteDeadline - Time.now()) * 1000);
/** * Removes a ZooKeeper node and all of its children, if necessary. * * @param path of the node to remove. * @throws KeeperException on I/O error. */ public void deleteNodeRecursively(File path) throws KeeperException { Stat stat = exists(path); // Race condition if someone else updates the znode in the meantime if (stat == null) { return; } List<String> children = getChildren(path, null, null); for (String child : children) { deleteNodeRecursively(new File(path, child)); } delete(path, stat.getVersion()); }