public boolean delete() throws IOException { try { zk.delete(path, -1); return true; } catch (Exception e) { throw new IOException(e); } } }
public void deleteNodeRecursively(String path) throws InterruptedException, KeeperException { try { delete(path, -1); } catch (KeeperException.NotEmptyException e) { List<String> children = getChildren(path, null); for (String child : children) { deleteNodeRecursively(ZkPath.append(path, child)); } delete(path, -1); } catch (KeeperException.NoNodeException e) { // Silently return if the node has already been deleted. return; } }
public void deleteIfExists(String path) throws KeeperException, InterruptedException { if (exists(path, false) != null) { delete(path, -1); } }
@Override public void releaseRingGroupConductor() throws IOException { try { if (zk.exists(ringGroupConductorOnlinePath, false) != null) { zk.delete(ringGroupConductorOnlinePath, -1); return; } throw new IllegalStateException( "Can't release the ring group conductor lock when it's not currently set!"); } catch (Exception e) { throw new IOException(e); } }
@Override public void clearCommandQueue() throws IOException { try { List<String> children = zk.getChildren(ZkPath.append(path, COMMAND_QUEUE_PATH), false); for (String child : children) { zk.delete(ZkPath.append(path, COMMAND_QUEUE_PATH, child), 0); } } catch (Exception e) { throw new IOException(e); } }
@Override public HostCommand nextCommand() throws IOException { try { // get the queue and sort so we have correct ordering List<String> children = zk.getChildren(ZkPath.append(path, COMMAND_QUEUE_PATH), false); Collections.sort(children); // if there are no children, the queue is empty. if (children.size() == 0) { currentCommand.set(null); return null; } // parse out the actual command String headOfQueuePath = ZkPath.append(path, COMMAND_QUEUE_PATH, children.get(0)); HostCommand nextCommand = HostCommand.valueOf(zk.getString(headOfQueuePath)); // set the current command first (modifying the queue will call the queue listeners) currentCommand.set(nextCommand); // delete the head of the queue zk.delete(headOfQueuePath, -1); return nextCommand; } catch (Exception e) { throw new IOException(e); } }
@Test public void testDeletion() throws Exception { getZk().create(ZkPath.append(getRoot(), "map"), null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); getZk().create(ZkPath.append(getRoot(), "map/1"), "2".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); final WatchedMap<String> m = new WatchedMap<>(getZk(), ZkPath.append(getRoot(), "map"), new StringElementLoader()); assertEquals(new HashMap<String, String>() {{ put("1", "2"); }}, m); getZk().delete(ZkPath.append(getRoot(), "map/1"), 0); WaitUntil.orDie(() -> Collections.EMPTY_MAP.equals(m)); assertEquals(Collections.EMPTY_MAP, m); } }
@Test public void testClaimRingGroupConductor() throws Exception { ZkDomainGroup dg = ZkDomainGroup.create(getZk(), null, dg_root, "blah"); dg.setDomainVersions(Collections.emptyMap()); final RingGroup rg = ZkRingGroup.create(getZk(), ring_group, dg, coordinator); create(ZkPath.append(ring_group, ZkRingGroup.RING_GROUP_CONDUCTOR_ONLINE_PATH)); assertFalse(rg.claimRingGroupConductor(RingGroupConductorMode.ACTIVE)); getZk().delete(ZkPath.append(ring_group, ZkRingGroup.RING_GROUP_CONDUCTOR_ONLINE_PATH), -1); assertTrue(rg.claimRingGroupConductor(RingGroupConductorMode.ACTIVE)); assertFalse(rg.claimRingGroupConductor(RingGroupConductorMode.ACTIVE)); rg.releaseRingGroupConductor(); assertTrue(rg.claimRingGroupConductor(RingGroupConductorMode.ACTIVE)); WaitUntil.orDie(() -> { try { return RingGroupConductorMode.ACTIVE.equals(rg.getRingGroupConductorMode()); } catch (IOException e) { throw new RuntimeException(e); } }); assertEquals(RingGroupConductorMode.ACTIVE, rg.getRingGroupConductorMode()); }