@Override public boolean isRingGroupConductorOnline() throws IOException { try { return zk.exists(ringGroupConductorOnlinePath, false) != null; } catch (Exception e) { throw new IOException(e); } }
public synchronized void block(int timeoutMS) throws InterruptedException, KeeperException { // Wait only if it doesn't exist if (waiting && zk.exists(nodePath, this) == null) { if (LOG.isDebugEnabled()) { LOG.debug("Wait for creation of node " + nodePath); } if (timeoutMS != 0) { this.wait(timeoutMS); } else { this.wait(); } } }
public Long getLongOrNull(String path) throws KeeperException, InterruptedException { if (exists(path, false) == null) { return null; } else { return Long.parseLong(new String(getData(path, false, new Stat()))); } }
public void deleteIfExists(String path) throws KeeperException, InterruptedException { if (exists(path, false) != null) { delete(path, -1); } }
private void watchForCreation() throws InterruptedException, KeeperException { synchronized (this) { value = null; stat = new Stat(); } if (zk.exists(nodePath, watcher) != null) { watchForData(); } }
@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); } }
protected void createNodeRecursively(String path) throws Exception { String[] toks = path.split("/"); String newPath = "/"; for (int i = 0; i < toks.length; i++) { newPath += toks[i]; if (zk.exists(newPath, false) == null) { zk.create(newPath, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } if (i != 0) { newPath += "/"; } } }
public void setOrCreate(String path, String value, CreateMode createMode) throws KeeperException, InterruptedException { if (exists(path, false) == null) { create(path, value.getBytes(), DEFAULT_ACL, createMode); } else { setData(path, value.getBytes(), -1); } }
public void ensureCreated(String path, byte[] value, CreateMode createMode) throws InterruptedException, KeeperException { if (!path.isEmpty() && exists(path, false) == null) { ensureCreated(new File(path).getParent(), null, createMode); create(path, value, DEFAULT_ACL, createMode); NodeCreationBarrier.block(ZooKeeperPlus.this, path); } }
@Override public boolean claimRingGroupConductor(RingGroupConductorMode mode) throws IOException { try { if (zk.exists(ringGroupConductorOnlinePath, false) == null) { zk.create(ringGroupConductorOnlinePath, BytesUtils.stringToBytes(mode.toString()), CreateMode.EPHEMERAL); return true; } return false; } catch (Exception e) { throw new IOException(e); } }
private int getNextDomainId() throws KeeperException, InterruptedException { final String domainIdCounterPath = ZkPath.append(domainsRoot, KEY_DOMAIN_ID_COUNTER); if (zk.exists(domainIdCounterPath, false) == null) { zk.create(domainIdCounterPath, Integer.toString(1).getBytes()); return 1; } while (true) { final Stat stat = new Stat(); final byte[] data = zk.getData(domainIdCounterPath, false, stat); int lastVersionNumber = Integer.parseInt(new String(data)); try { lastVersionNumber++; zk.setData(domainIdCounterPath, Integer.toString(lastVersionNumber).getBytes(), stat.getVersion()); return lastVersionNumber; } catch (KeeperException.BadVersionException e) { if (LOG.isDebugEnabled()) { LOG.debug("Tried to set the domain id counter to " + lastVersionNumber + " but was preempted by another writer. Retrying."); } } } }
@Override public Long getUpSince() throws IOException { if (getState() == HostState.OFFLINE) { return null; } try { Stat stat = zk.exists(state.getPath(), false); if (stat == null) { return null; } else { return stat.getCtime(); } } catch (Exception e) { throw new IOException(e); } }
@Override public void detectCompletion(ZooKeeperPlus zk, String basePath, String relPath, CompletionAwaiter awaiter) throws KeeperException, InterruptedException { if (zk.exists(ZkPath.append(basePath, relPath, NODE_NAME), new CreationWatcher(relPath, awaiter)) != null) { awaiter.completed(relPath); } } }
private void assertExists(String path, ZooKeeperPlus zkp) throws KeeperException, InterruptedException { assertTrue(zkp.exists(path, false) != null); }
@Test public void testDelete() throws Exception { ZkDomain dc = ZkDomain.create(getZk(), getRoot(), "domain0", 1, ConstantStorageEngine.Factory.class.getName(), "---", Murmur64Partitioner.class.getName(), 0, Collections.<String>emptyList()); assertNotNull(getZk().exists(ZkPath.append(getRoot(), "domain0"), false)); assertTrue(dc.delete()); WaitUntil.orDie(() -> { try { return getZk().exists(ZkPath.append(getRoot(), "domain0"), false) == null; } catch (KeeperException e) { throw new RuntimeException(e); } catch (InterruptedException e) { throw new RuntimeException(e); } }); assertNull(getZk().exists(ZkPath.append(getRoot(), "domain0"), false)); } }
public static ZkPartitionProperties create(ZooKeeperPlus zk, String partsRoot, int partNum, long numBytes, long numRecords) throws KeeperException, InterruptedException { String partPath = ZkPath.append(partsRoot, nodeName(partNum)); // if the node already exists, then don't try to create a new one if (zk.exists(partPath, false) == null) { zk.create(partPath, null); zk.createLong(ZkPath.append(partPath, "num_bytes"), numBytes); zk.createLong(ZkPath.append(partPath, "num_records"), numRecords); zk.create(ZkPath.append(partPath, DotComplete.NODE_NAME), null); } return new ZkPartitionProperties(zk, partPath); }
@Test public void testDelete() throws Exception { ZkRing ring = ZkRing.create(getZk(), coordinator, ZkPath.append(getRoot(), "ring-group-one"), 1, null, null); ring.delete(); assertTrue(getZk().exists(ZkPath.append(getRoot(), "ring-group-one/ring-1"), null) == null); } }
@Test public void testCancelVersion() throws Exception { DomainVersion dv = ZkDomainVersion.create(getZk(), getRoot(), 1, null, null); assertEquals(1, dv.getVersionNumber()); assertNull(dv.getClosedAt()); assertFalse(DomainVersions.isClosed(dv)); dv.cancel(); assertNull(getZk().exists(ZkPath.append(getRoot(), "v/1"), false)); }