/** * Recursively delete the node with the given path. * <p> * Important: All versions, of all nodes, under the given node are deleted. * <p> * If there is an error with deleting one of the sub-nodes in the tree, * this operation would abort and would be the responsibility of the app to handle the same. * * See {@link #delete(String, int)} for more details. * * @throws IllegalArgumentException if an invalid path is specified */ public static void deleteRecursive(ZooKeeper zk, final String pathRoot) throws InterruptedException, KeeperException { PathUtils.validatePath(pathRoot); List<String> tree = listSubTreeBFS(zk, pathRoot); LOG.debug("Deleting " + tree); LOG.debug("Deleting " + tree.size() + " subnodes "); for (int i = tree.size() - 1; i >= 0 ; --i) { //Delete the leaves first and eventually get rid of the root zk.delete(tree.get(i), -1); //Delete all versions of the node with -1. } }
/** * Recursively delete the node with the given path. (async version). * * <p> * Important: All versions, of all nodes, under the given node are deleted. * <p> * If there is an error with deleting one of the sub-nodes in the tree, * this operation would abort and would be the responsibility of the app to handle the same. * <p> * @param zk the zookeeper handle * @param pathRoot the path to be deleted * @param cb call back method * @param ctx the context the callback method is called with * @throws IllegalArgumentException if an invalid path is specified */ public static void deleteRecursive(ZooKeeper zk, final String pathRoot, VoidCallback cb, Object ctx) throws InterruptedException, KeeperException { PathUtils.validatePath(pathRoot); List<String> tree = listSubTreeBFS(zk, pathRoot); LOG.debug("Deleting " + tree); LOG.debug("Deleting " + tree.size() + " subnodes "); for (int i = tree.size() - 1; i >= 0 ; --i) { //Delete the leaves first and eventually get rid of the root zk.delete(tree.get(i), -1, cb, ctx); //Delete all versions of the node with -1. } }
/** * Recursively delete the node with the given path. * <p> * Important: All versions, of all nodes, under the given node are deleted. * <p> * If there is an error with deleting one of the sub-nodes in the tree, * this operation would abort and would be the responsibility of the app to handle the same. * * See {@link #delete(String, int)} for more details. * * @throws IllegalArgumentException if an invalid path is specified */ public static void deleteRecursive(ZooKeeper zk, final String pathRoot) throws InterruptedException, KeeperException { PathUtils.validatePath(pathRoot); List<String> tree = listSubTreeBFS(zk, pathRoot); LOG.debug("Deleting " + tree); LOG.debug("Deleting " + tree.size() + " subnodes "); for (int i = tree.size() - 1; i >= 0 ; --i) { //Delete the leaves first and eventually get rid of the root zk.delete(tree.get(i), -1); //Delete all versions of the node with -1. } }
/** * Recursively delete the node with the given path. (async version). * * <p> * Important: All versions, of all nodes, under the given node are deleted. * <p> * If there is an error with deleting one of the sub-nodes in the tree, * this operation would abort and would be the responsibility of the app to handle the same. * <p> * @param zk the zookeeper handle * @param pathRoot the path to be deleted * @param cb call back method * @param ctx the context the callback method is called with * @throws IllegalArgumentException if an invalid path is specified */ public static void deleteRecursive(ZooKeeper zk, final String pathRoot, VoidCallback cb, Object ctx) throws InterruptedException, KeeperException { PathUtils.validatePath(pathRoot); List<String> tree = listSubTreeBFS(zk, pathRoot); LOG.debug("Deleting " + tree); LOG.debug("Deleting " + tree.size() + " subnodes "); for (int i = tree.size() - 1; i >= 0 ; --i) { //Delete the leaves first and eventually get rid of the root zk.delete(tree.get(i), -1, cb, ctx); //Delete all versions of the node with -1. } }
/** * @param zk ZooKeeper client. * @param root Root path. * @return All children znodes for given path. * @throws Exception If failed/ */ private List<String> listSubTree(ZooKeeper zk, String root) throws Exception { for (int i = 0; i < 30; i++) { try { return ZKUtil.listSubTreeBFS(zk, root); } catch (KeeperException.NoNodeException e) { info("NoNodeException when get znodes, will retry: " + e); } } throw new Exception("Failed to get znodes: " + root); }
final List<String> paths = ZKUtil.listSubTreeBFS( client.getZookeeperClient().getZooKeeper(), namespacedPath);