@Override public void removeData(String path, int version) throws NoSuchElementException, BadVersionException, NotEmptyException, IOException, KeeperException, InterruptedException { try { zkClient.delete(path, version, true); } catch (KeeperException.NoNodeException e) { throw new NoSuchElementException(path); } catch (KeeperException.NotEmptyException e) { throw new NotEmptyException(path); } catch (KeeperException.BadVersionException e) { throw new BadVersionException(version, path); } catch (InterruptedException e) { throw e; } }
public static void clean(SolrZkClient zkClient, String path) throws InterruptedException, KeeperException { traverseZkTree(zkClient, path, VISIT_ORDER.VISIT_POST, znode -> { try { if (!znode.equals("/")) { try { zkClient.delete(znode, -1, true); } catch (KeeperException.NotEmptyException e) { clean(zkClient, znode); } } } catch (KeeperException.NoNodeException r) { return; } }); }
/** * Delete a path and all of its sub nodes * @param filter for node to be deleted */ public static void clean(SolrZkClient zkClient, String path, Predicate<String> filter) throws InterruptedException, KeeperException { if (filter == null) { clean(zkClient, path); return; } TreeSet<String> paths = new TreeSet<>(Comparator.comparingInt(String::length).reversed()); traverseZkTree(zkClient, path, VISIT_ORDER.VISIT_POST, znode -> { if (!znode.equals("/") && filter.test(znode)) paths.add(znode); }); for (String subpath : paths) { if (!subpath.equals("/")) { try { zkClient.delete(subpath, -1, true); } catch (KeeperException.NotEmptyException | KeeperException.NoNodeException e) { // expected } } } }
public void clean(String path) throws InterruptedException, KeeperException { List<String> children; try { children = getChildren(path, null, true); } catch (NoNodeException r) { return; } for (String string : children) { // we can't clean the built-in zookeeper node if (path.equals("/") && string.equals("zookeeper")) continue; if (path.equals("/")) { clean(path + string); } else { clean(path + "/" + string); } } try { if (!path.equals("/")) { try { delete(path, -1, true); } catch (NotEmptyException e) { clean(path); } } } catch (NoNodeException r) { return; } }