public void create(String path, byte[] data, CreateMode createMode) throws KeeperException, InterruptedException { create(path, data, DEFAULT_ACL, createMode); }
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); }
public void create(String path, byte[] data) throws KeeperException, InterruptedException { create(path, data, DEFAULT_ACL, DEFAULT_CREATE_MODE); }
public void createLong(String path, long value) throws KeeperException, InterruptedException { create(path, (Long.toString(value)).getBytes(), DEFAULT_ACL, DEFAULT_CREATE_MODE); }
public void createInt(String path, int value) throws KeeperException, InterruptedException { create(path, (Integer.toString(value)).getBytes(), DEFAULT_ACL, DEFAULT_CREATE_MODE); }
public static ZkRing create(ZooKeeperPlus zk, Coordinator coordinator, String ringGroup, int ringNum, RingGroup group, DataLocationChangeListener dataLocationChangeListener) throws KeeperException, InterruptedException { String ringPath = ZkPath.append(ringGroup, "ring-" + ringNum); zk.create(ringPath, null); zk.create(ZkPath.append(ringPath, HOSTS_PATH_SEGMENT), null); return new ZkRing(zk, ringPath, group, coordinator, dataLocationChangeListener); }
protected void create(String path, byte[] data) throws Exception { getZk().create(path, data, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } }
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 static ZkRingGroup create(ZooKeeperPlus zk, String path, ZkDomainGroup domainGroup, Coordinator coordinator) throws KeeperException, InterruptedException, IOException { zk.create(path, domainGroup.getName().getBytes()); zk.create(ZkPath.append(path, CLIENTS_PATH), null); zk.create(ZkPath.append(path, SERVERS_PATH), null); zk.create(ZkPath.append(path, DotComplete.NODE_NAME), null); return new ZkRingGroup(zk, path, domainGroup, coordinator); }
@Override public void enqueueCommand(HostCommand command) throws IOException { try { zk.create(ZkPath.append(path, COMMAND_QUEUE_PATH, "command_"), command.toString().getBytes(), CreateMode.PERSISTENT_SEQUENTIAL); } catch (Exception e) { throw new IOException(e); } }
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."); } } } }
@Test public void testWaitForCreation() throws InterruptedException, KeeperException { final ZooKeeperPlus zk = getZk(); final String nodePath = ZkPath.append(getRoot(), "watchedNode"); WatchedInt wi = new WatchedInt(zk, nodePath, false); assertNull(wi.get()); Thread t = new Thread(() -> { try { // Wait before creating it Thread.sleep(2000); } catch (InterruptedException e) { throw new RuntimeException(e); } try { zk.create(nodePath, "42".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } catch (KeeperException | InterruptedException e) { throw new RuntimeException(e); } }); // Try waiting t.start(); wi = new WatchedInt(zk, nodePath, true); assertEquals(Integer.valueOf(42), wi.get()); }
@Before public void setUp() throws Exception { getZk().create(ZkPath.append(getRoot(), ZkDomain.VERSIONS_PATH), null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } }
@Test public void testUpdate() throws InterruptedException, KeeperException { final ZooKeeperPlus zk = getZk(); final String nodePath = ZkPath.append(getRoot(), "watchedNode"); zk.create(nodePath, "0".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); final WatchedInt wi = new WatchedInt(zk, nodePath, true); assertEquals(Integer.valueOf(0), wi.get()); WatchedNodeUpdater<Integer> incrementer = current -> { if (current == null) { return 0; } else { return current + 1; } }; final int finalValue = 64; for (int i = 0; i < finalValue; ++i) { wi.update(incrementer); } WaitUntil.orDie(() -> Integer.valueOf(finalValue).equals(wi.get())); assertEquals(Integer.valueOf(finalValue), wi.get()); }
@Test public void testIt() throws Exception { final ZooKeeperPlus zk = getZk(); final String colRoot = ZkPath.append(getRoot(), "collection"); zk.create(colRoot, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); final ElementLoader<String> elementLoader = (zk1, basePath, relPath) -> { try { return new String(zk1.getData(ZkPath.append(basePath, relPath), false, new Stat())); } catch (Exception e) { throw new RuntimeException(e); } }; final WatchedMap<String> c1 = new WatchedMap<>(zk, colRoot, elementLoader); dumpZk(); WaitUntil.orDie(() -> 0 == c1.size()); assertEquals(0, c1.size()); zk.create(ZkPath.append(colRoot, "first"), "data".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); WaitUntil.orDie(() -> 1 == c1.size()); assertEquals(1, c1.size()); }
@Test public void testIt() throws Exception { final ZooKeeperPlus zk = getZk(); final String nodePath = ZkPath.append(getRoot(), "watchedNode"); zk.create(nodePath, "1".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); final WatchedLong wl = new WatchedLong(zk, nodePath); assertEquals(Long.valueOf(1), wl.get()); zk.setData(nodePath, "55".getBytes(), -1); WaitUntil.orDie(() -> Long.valueOf(55).equals(wl.get())); assertEquals(Long.valueOf(55), wl.get()); zk.setData(nodePath, null, -1); WaitUntil.orDie(() -> wl.get() == null); assertNull(wl.get()); final WatchedLong wl2 = new WatchedLong(zk, nodePath); WaitUntil.orDie(() -> null == wl2.get()); assertNull(wl2.get()); wl2.set(22L); WaitUntil.orDie(() -> Long.valueOf(22).equals(wl2.get()) && Long.valueOf(22).equals(wl.get())); assertEquals(Long.valueOf(22), wl2.get()); assertEquals(Long.valueOf(22), wl.get()); } }
@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); } }