@Override public Void run() throws KeeperException, InterruptedException { ZKUtil.deleteRecursive(zkClient, znodeWorkingDir); return null; } });
/** * 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. } }
String error = ZKUtil.validateFileInput(snapshotFile); if (null != error) { System.err.println(error);
@Override public boolean exec() throws CliException { if (args.length < 2) { throw new MalformedCommandException(getUsageStr()); } String path = args[1]; boolean watch = cl.hasOption("w"); boolean withStat = cl.hasOption("s"); boolean recursive = cl.hasOption("R"); try { if (recursive) { ZKUtil.visitSubTreeDFS(zk, path, watch, new StringCallback() { @Override public void processResult(int rc, String path, Object ctx, String name) { out.println(path); } }); } else { Stat stat = withStat ? new Stat() : null; List<String> children = zk.getChildren(path, watch, stat); printChildren(children, stat); } } catch (IllegalArgumentException ex) { throw new MalformedPathException(ex.getMessage()); } catch (KeeperException|InterruptedException ex) { throw new CliWrapperException(ex); } return watch; }
ZKUtil.visitSubTreeDFS(zk, path, false, new StringCallback() { @Override public void processResult(int rc, String p, Object ctx, String name) {
@Override public boolean exec() throws CliException { printDeprecatedWarning(); String path = args[1]; try { ZKUtil.deleteRecursive(zk, path); } catch (IllegalArgumentException ex) { throw new MalformedPathException(ex.getMessage()); } catch (KeeperException|InterruptedException ex) { throw new CliWrapperException(ex); } return false; }
/** * 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. } }
@Test public void testValidateFileInput() throws IOException { File file = File.createTempFile("test", ".junit", testData); file.deleteOnExit(); String absolutePath = file.getAbsolutePath(); String error = ZKUtil.validateFileInput(absolutePath); assertNull(error); }
try { LOG.info("Delete the path associated with the log {}, ZK Path {}", name, zkPath); ZKUtil.deleteRecursive(writerZKC.get(), zkPath); } catch (InterruptedException ie) { LOG.error("Interrupted while accessing ZK", ie);
/** * 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. } }
@Test public void testValidateFileInputNotExist() { String fileName = UUID.randomUUID().toString(); File file = new File(testData, fileName); String absolutePath = file.getAbsolutePath(); String error = ZKUtil.validateFileInput(absolutePath); assertNotNull(error); String expectedMessage = "File '" + absolutePath + "' does not exist."; assertEquals(expectedMessage, error); }
@Override public void onComponentRemoved(final String componentId) throws IOException { try { ZKUtil.deleteRecursive(getZooKeeper(), getComponentPath(componentId)); } catch (final KeeperException ke) { // Node doesn't exist so just ignore final Code exceptionCode = ke.code(); if (Code.NONODE == exceptionCode) { return; } if (Code.SESSIONEXPIRED == exceptionCode) { invalidateClient(); onComponentRemoved(componentId); return; } throw new IOException("Unable to remove state for component with ID '" + componentId + " with exception code " + exceptionCode, ke); } catch (final InterruptedException e) { Thread.currentThread().interrupt(); throw new IOException("Failed to remove state for component with ID '" + componentId + "' from ZooKeeper due to being interrupted", e); } }
/** * 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. } }
@Test public void testValidateFileInputDirectory() throws Exception { File file = File.createTempFile("test", ".junit", testData); file.deleteOnExit(); // delete file, as we need directory not file file.delete(); file.mkdir(); String absolutePath = file.getAbsolutePath(); String error = ZKUtil.validateFileInput(absolutePath); assertNotNull(error); String expectedMessage = "'" + absolutePath + "' is a direcory. it must be a file."; assertEquals(expectedMessage, error); }
public void deleteLog() throws IOException { lock.checkOwnershipAndReacquire(); FutureUtils.result(purgeLogSegmentsOlderThanTxnId(-1)); try { Utils.closeQuietly(lock); zooKeeperClient.get().exists(logMetadata.getLogSegmentsPath(), false); zooKeeperClient.get().exists(logMetadata.getMaxTxIdPath(), false); if (logMetadata.getLogRootPath().toLowerCase().contains("distributedlog")) { ZKUtil.deleteRecursive(zooKeeperClient.get(), logMetadata.getLogRootPath()); } else { LOG.warn("Skip deletion of unrecognized ZK Path {}", logMetadata.getLogRootPath()); } } catch (InterruptedException ie) { LOG.error("Interrupted while deleting log znodes", ie); throw new DLInterruptedException("Interrupted while deleting " + logMetadata.getLogRootPath(), ie); } catch (KeeperException ke) { LOG.error("Error deleting" + logMetadata.getLogRootPath() + " in zookeeper", ke); } }
/** * @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); }
@Test public void testUnreadableFileInput() throws Exception { //skip this test on Windows, coverage on Linux assumeTrue(!org.apache.zookeeper.Shell.WINDOWS); File file = File.createTempFile("test", ".junit", testData); file.setReadable(false, false); file.deleteOnExit(); String absolutePath = file.getAbsolutePath(); String error = ZKUtil.validateFileInput(absolutePath); assertNotNull(error); String expectedMessage = "Read permission is denied on the file '" + absolutePath + "'"; assertEquals(expectedMessage, error); }
ZKUtil.deleteRecursive(zk, "/a", cb, ctx); synchronized (ctx) { ctx.wait();
final List<String> paths = ZKUtil.listSubTreeBFS( client.getZookeeperClient().getZooKeeper(), namespacedPath);
String error = ZKUtil.validateFileInput(args[0]); if (null != error) { System.err.println(error);