Refine search
@Override public StateMap getState(final String componentId) throws IOException { verifyEnabled(); try { final Stat stat = new Stat(); final String path = getComponentPath(componentId); final byte[] data = getZooKeeper().getData(path, false, stat); final StateMap stateMap = deserialize(data, stat.getVersion(), componentId); return stateMap; } catch (final InterruptedException e) { Thread.currentThread().interrupt(); throw new IOException("Failed to obtain value from ZooKeeper for component with ID " + componentId + ", due to interruption", e); } catch (final KeeperException ke) { final Code exceptionCode = ke.code(); if (Code.NONODE == exceptionCode) { return new StandardStateMap(null, -1L); } if (Code.SESSIONEXPIRED == exceptionCode) { invalidateClient(); return getState(componentId); } throw new IOException("Failed to obtain value from ZooKeeper for component with ID " + componentId + " with exception code " + exceptionCode, ke); } catch (final IOException ioe) { // provide more context in the error message throw new IOException("Failed to obtain value from ZooKeeper for component with ID " + componentId, ioe); } }
private void expireZooKeeperSession(ZooKeeper zk, int timeout) throws IOException, InterruptedException, KeeperException { final CountDownLatch latch = new CountDownLatch(1); ZooKeeper newZk = new ZooKeeper(zkServers, timeout, new Watcher() { @Override public void process(WatchedEvent event) { if (event.getType() == EventType.None && event.getState() == KeeperState.SyncConnected) { latch.countDown(); } }}, zk.getSessionId(), zk.getSessionPasswd()); if (!latch.await(timeout, TimeUnit.MILLISECONDS)) { throw KeeperException.create(KeeperException.Code.CONNECTIONLOSS); } newZk.close(); }
private void checkRoot() throws IOException, InterruptedException { ZooKeeper zk = new ZooKeeper(hostPort, 10000, this); try { zk.create(dirOnZK, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } catch (KeeperException.NodeExistsException ke) { // expected, sort of } catch (KeeperException ke) { Assert.fail("Unexpected exception code for create " + dirOnZK + ": " + ke.getMessage()); } try { zk.create(testDirOnZK, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } catch (KeeperException.NodeExistsException ke) { // expected, sort of } catch (KeeperException ke) { Assert.fail("Unexpected exception code for create " + testDirOnZK + ": " + ke.getMessage()); } zk.close(); }
private void deleteNodeIfExists(ZooKeeper zk, String nodeName) throws InterruptedException { try { zk.delete(nodeName, -1); } catch (KeeperException ke) { Code code = ke.code(); boolean valid = code == KeeperException.Code.NONODE || code == KeeperException.Code.NOTEMPTY; if (!valid) { Assert.fail("Unexpected exception code for delete: " + ke.getMessage()); } } }
private List<OpResult> validatePath(Iterable<Op> ops) { List<OpResult> results = new ArrayList<OpResult>(); boolean error = false; for (Op op : ops) { try { op.validate(); } catch (IllegalArgumentException iae) { LOG.error("IllegalArgumentException: " + iae.getMessage()); ErrorResult err = new ErrorResult( KeeperException.Code.BADARGUMENTS.intValue()); results.add(err); error = true; continue; } catch (KeeperException ke) { LOG.error("KeeperException: " + ke.getMessage()); ErrorResult err = new ErrorResult(ke.code().intValue()); results.add(err); error = true; continue; } ErrorResult err = new ErrorResult( KeeperException.Code.RUNTIMEINCONSISTENCY.intValue()); results.add(err); } if (false == error) { results.clear(); } return results; }
zkOps.add(toZooKeeperOp(zkw, op)); switch (ke.code()) { case NODEEXISTS: case NONODE: LOG.info("On call to ZK.multi, received exception: " + ke.toString() + "." + " Attempting to run operations sequentially because" + " runSequentialOnMultiFailure is: " + runSequentialOnMultiFailure + "."); processSequentially(zkw, ops); break;
lsnr.getChildren("/foo", true); Assert.fail(); } catch (KeeperException e) { Assert.assertEquals(KeeperException.Code.NONODE, e.code()); Assert.assertEquals("/foo", e.getPath()); lsnr.getChildren("/foo/bar", true); Assert.fail(); } catch (KeeperException e) { Assert.assertEquals(KeeperException.Code.NONODE, e.code()); Assert.assertEquals("/foo/bar", e.getPath()); client.create("/foo", "parent".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); Assert.assertNotNull(lsnr.getChildren("/foo", true));
Assert.fail("Creating node " + parentName + ke.getMessage()); Code code = ke.code(); boolean valid = code == KeeperException.Code.NONODE || code == KeeperException.Code.NOTEMPTY; if (!valid) { Assert.fail("Unexpected exception code for delete: " + ke.getMessage()); Code code = ke.code(); boolean valid = code == KeeperException.Code.NODEEXISTS; if (!valid) { Assert.fail("Unexpected exception code for createin: " + ke.getMessage()); Assert.fail("Should be impossible to delete a non-empty node " + parentName); } catch (KeeperException ke) { Code code = ke.code(); boolean valid = code == KeeperException.Code.NOTEMPTY; if (!valid) { Assert.fail("Should be impossible to create child off Ephemeral node " + nodeName); } catch (KeeperException ke) { Code code = ke.code(); boolean valid = code == KeeperException.Code.NOCHILDRENFOREPHEMERALS; if (!valid) { Code code = ke.code(); boolean valid = code == KeeperException.Code.NONODE; if (!valid) {
private void delete_create_get_set_test_1() throws IOException, InterruptedException, KeeperException { checkRoot(); ZooKeeper zk = new ZooKeeper(hostPort, 10000, this); String parentName = testDirOnZK; String nodeName = parentName + "/benwashere"; try { zk.delete(nodeName, -1); } catch (KeeperException ke) { Code code = ke.code(); boolean valid = code == KeeperException.Code.NONODE || code == KeeperException.Code.NOTEMPTY; if (!valid) { Assert.fail("Unexpected exception code for delete: " + ke.getMessage()); zk.create(nodeName, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } catch (KeeperException ke) { Code code = ke.code(); boolean valid = code == KeeperException.Code.NODEEXISTS; if (!valid) { Assert.fail("Unexpected exception code for create: " + ke.getMessage()); Assert.fail("Should have gotten BadVersion exception"); } catch (KeeperException ke) { if (ke.code() != Code.BADVERSION) { Assert.fail("Should have gotten BadVersion exception"); Assert.fail("Should have gotten BadVersion exception"); } catch (KeeperException ke) { Code code = ke.code();
public void run() { try { Stat stat = new Stat(); String path = zk.create("/hammers/hammer-", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL); byte tag[] = (path + " was here!").getBytes(); String startPath = "/hammers/start"; System.out.println("Waiting for " + startPath); while (zk.exists(startPath, true) == null) { wait(); System.out.print(i + "\r"); List<String> childs = zk.getChildren("/hammers", false); Collections.shuffle(childs); for (String s : childs) { e.printStackTrace(); e.printStackTrace(); } catch (KeeperException e) { e.printStackTrace();
try { zk = createClient(); zk.addAuthInfo("digest", "ben:passwd".getBytes()); ArrayList<ACL> testACL = new ArrayList<ACL>(); testACL.add(new ACL(Perms.ALL, new Id("auth", null))); zk.create("/acltest", new byte[0], testACL, CreateMode.PERSISTENT); zk.close(); zk = createClient(); zk.addAuthInfo("digest", "ben:passwd2".getBytes()); Assert.fail("Should have received a permission error"); } catch (KeeperException e) { Assert.assertEquals(Code.NOAUTH, e.code()); zk = createClient(); zk.getData("/acltest", false, null); List<ACL> acls = zk.getACL("/acltest", new Stat()); Assert.assertEquals(1, acls.size()); Assert.assertEquals(Ids.OPEN_ACL_UNSAFE, acls);
zk = createClient(); try { zk.addAuthInfo("digest", "ben:passwd".getBytes()); zk.create("/ben", new byte[0], Ids.READ_ACL_UNSAFE, CreateMode.PERSISTENT, this, results); zk.create("/ben/2", new byte[0], Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT, this, results); zk.delete("/ben", -1, this, results); zk.create("/ben2", new byte[0], Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT, this, results); Assert.assertEquals(Code.NOAUTH, Code.get(results.get(1))); Assert.assertEquals(0, (int) results.get(2)); Assert.assertEquals(0, (int) results.get(3)); zk.addAuthInfo("digest", "ben:passwd2".getBytes()); try { zk.getData("/ben2", false, new Stat()); Assert.fail("Should have received a permission error"); } catch (KeeperException e) { Assert.assertEquals(Code.NOAUTH, e.code()); try { zk.addAuthInfo("digest", "ben:passwd".getBytes()); zk.getData("/ben2", false, new Stat()); } finally { zk.close();
lsnr.getData("/foo", true, null); Assert.fail(); } catch (KeeperException e) { Assert.assertEquals(KeeperException.Code.NONODE, e.code()); Assert.assertEquals("/foo", e.getPath()); lsnr.getData("/foo/bar", true, null); Assert.fail(); } catch (KeeperException e) { Assert.assertEquals(KeeperException.Code.NONODE, e.code()); Assert.assertEquals("/foo/bar", e.getPath()); client.create("/foo", "parent".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); Assert.assertNotNull(lsnr.getData("/foo", true, null)); client.create("/foo/bar", "child".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
while (true) { try { s = new Stat(); oldACL = zk.getACL("/", s); break; } catch (KeeperException e) { switch (e.code()) { case CONNECTIONLOSS: case SESSIONEXPIRED: break; } catch (KeeperException e) { switch (e.code()) { case CONNECTIONLOSS: case SESSIONEXPIRED: break; } catch (KeeperException e) { switch (e.code()) { case CONNECTIONLOSS: case SESSIONEXPIRED: zk.close(); ZKUtil.createAndFailSilent(ZKW, aclZnode);
String parentName = testDirOnZK; String nodeName = parentName + "/enode_abc"; ZooKeeper zk = new ZooKeeper(hostPort, 10000, this); Stat stat = zk.exists(parentName, false); if (stat == null) { try { zk.create(parentName, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } catch (KeeperException ke) { Assert.fail("Creating node " + parentName + ke.getMessage()); zk.create(nodeName, null, Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL); } catch (KeeperException ke) { Code code = ke.code(); boolean valid = code == KeeperException.Code.NODEEXISTS; if (!valid) { Assert.fail("Unexpected exception code for createin: " + ke.getMessage());
@Test public void testDeleteWithChildren() throws Exception { ZooKeeper zk = createClient(); zk.create("/parent", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); zk.create("/parent/child", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); try { zk.delete("/parent", -1); Assert.fail("Should have received a not equals message"); } catch (KeeperException e) { Assert.assertEquals(KeeperException.Code.NOTEMPTY, e.code()); } zk.delete("/parent/child", -1); zk.delete("/parent", -1); zk.close(); }
try { startTime = EnvironmentEdgeManager.currentTime(); Stat nodeStat = checkZk().setData(path, newData, version); return nodeStat; } catch (KeeperException e) { switch (e.code()) { case CONNECTIONLOSS: retryOrThrow(retryCounter, e, "setData"); Stat stat = new Stat(); byte[] revData = checkZk().getData(path, false, stat); if(Bytes.compareTo(revData, newData) == 0) {
public void createNode(String nodePath) { try { if (zk.exists(ZK_REGISTRY_PATH, true) == null) { zk.create(ZK_REGISTRY_PATH, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } zk.create(ZK_REGISTRY_PATH + "/" + nodePath, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); LOGGER.info("Node " + nodePath + " have been created successfully."); zk.close(); } catch (InterruptedException e) { LOGGER.error("Creating node " + nodePath + " failed."); e.printStackTrace(); } catch (KeeperException e) { LOGGER.error("Creating node " + nodePath + " failed."); e.printStackTrace(); } }
public void deleteNode(String nodePath) { checkGroupPath(nodePath); try { List<String> children = zk.getChildren(nodePath, true); for (String c : children) { zk.delete(nodePath + "/" + c, -1); } zk.delete(nodePath, -1); zk.close(); } catch (KeeperException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } }
private void createIfDoesntExist(String path, byte[] data, boolean force, ZooKeeper zooKeeper) throws InterruptedException, KeeperException { try { zooKeeper.create(path, data, defaultAcl(), CreateMode.PERSISTENT); } catch (KeeperException ke) { //Explicit Overwrite if (KeeperException.Code.NODEEXISTS.equals(ke.code())) { if (force) { zooKeeper.delete(path, -1); zooKeeper.create(path, data, defaultAcl(), CreateMode.PERSISTENT); } } else { throw ke; } } }