List of usage examples for org.apache.zookeeper KeeperException create
public static KeeperException create(Code code)
From source file:org.apache.distributedlog.util.Utils.java
License:Apache License
/** * Delete the given <i>path</i> from zookeeper. * * @param zk/*from ww w. j a v a2 s . co m*/ * zookeeper client * @param path * path to delete * @param version * version used to set data * @return future representing the version after this operation. */ public static CompletableFuture<Void> zkDelete(ZooKeeper zk, String path, LongVersion version) { final CompletableFuture<Void> promise = new CompletableFuture<Void>(); zk.delete(path, (int) version.getLongVersion(), new AsyncCallback.VoidCallback() { @Override public void processResult(int rc, String path, Object ctx) { if (KeeperException.Code.OK.intValue() == rc) { promise.complete(null); return; } promise.completeExceptionally(KeeperException.create(KeeperException.Code.get(rc))); return; } }, null); return promise; }
From source file:org.apache.distributedlog.util.Utils.java
License:Apache License
/** * Delete the given <i>path</i> from zookeeper. * * @param zkc// ww w . j a v a 2 s. c om * zookeeper client * @param path * path to delete * @param version * version used to set data * @return future representing if the delete is successful. Return true if the node is deleted, * false if the node doesn't exist, otherwise future will throw exception * */ public static CompletableFuture<Boolean> zkDeleteIfNotExist(ZooKeeperClient zkc, String path, LongVersion version) { ZooKeeper zk; try { zk = zkc.get(); } catch (ZooKeeperClient.ZooKeeperConnectionException e) { return FutureUtils.exception(zkException(e, path)); } catch (InterruptedException e) { return FutureUtils.exception(zkException(e, path)); } final CompletableFuture<Boolean> promise = new CompletableFuture<Boolean>(); zk.delete(path, (int) version.getLongVersion(), new AsyncCallback.VoidCallback() { @Override public void processResult(int rc, String path, Object ctx) { if (KeeperException.Code.OK.intValue() == rc) { promise.complete(true); } else if (KeeperException.Code.NONODE.intValue() == rc) { promise.complete(false); } else { promise.completeExceptionally(KeeperException.create(KeeperException.Code.get(rc))); } } }, null); return promise; }
From source file:org.apache.distributedlog.zk.ZKTransaction.java
License:Apache License
@Override public void processResult(int rc, String path, Object ctx, List<OpResult> results) { if (KeeperException.Code.OK.intValue() == rc) { // transaction succeed for (int i = 0; i < ops.size(); i++) { ops.get(i).commitOpResult(results.get(i)); }//w ww .j a v a 2 s .c o m FutureUtils.complete(result, null); } else { KeeperException ke = KeeperException.create(KeeperException.Code.get(rc)); for (int i = 0; i < ops.size(); i++) { ops.get(i).abortOpResult(ke, null != results ? results.get(i) : null); } FutureUtils.completeExceptionally(result, ke); } }
From source file:org.apache.distributedlog.zk.ZKVersionedSetOp.java
License:Apache License
@Override protected void abortOpResult(Throwable t, @Nullable OpResult opResult) { Throwable cause;//from w ww.j a va2 s . co m if (null == opResult) { cause = t; } else { assert (opResult instanceof OpResult.ErrorResult); OpResult.ErrorResult errorResult = (OpResult.ErrorResult) opResult; if (KeeperException.Code.OK.intValue() == errorResult.getErr()) { cause = t; } else { cause = KeeperException.create(KeeperException.Code.get(errorResult.getErr())); } } if (null != listener) { listener.onAbort(cause); } }
From source file:org.apache.distributedlog.zk.ZKWatcherManager.java
License:Apache License
public void unregisterChildWatcher(String path, Watcher watcher, boolean removeFromServer) { Set<Watcher> watchers = childWatches.get(path); if (null == watchers) { logger.warn("No watchers found on path {} while unregistering child watcher {}.", path, watcher); return;//from w ww . ja va 2s . c o m } synchronized (watchers) { if (watchers.remove(watcher)) { allWatchesGauge.decrementAndGet(); } else { logger.warn("Remove a non-registered child watcher {} from path {}", watcher, path); } if (watchers.isEmpty()) { // best-efforts to remove watches try { if (null != zkc && removeFromServer) { zkc.get().removeWatches(path, this, WatcherType.Children, true, new AsyncCallback.VoidCallback() { @Override public void processResult(int rc, String path, Object ctx) { if (KeeperException.Code.OK.intValue() == rc) { logger.debug("Successfully removed children watches from {}", path); } else { logger.debug( "Encountered exception on removing children watches from {}", path, KeeperException.create(KeeperException.Code.get(rc))); } } }, null); } } catch (InterruptedException e) { logger.debug("Encountered exception on removing watches from {}", path, e); } catch (ZooKeeperClient.ZooKeeperConnectionException e) { logger.debug("Encountered exception on removing watches from {}", path, e); } childWatches.remove(path, watchers); } } }
From source file:org.apache.hadoop.hbase.zookeeper.ZKAssign.java
License:Apache License
/** * Deletes an existing unassigned node that is in the specified state for the * specified region./* w w w . jav a2s.co m*/ * * <p>If a node does not already exist for this region, a * {@link NoNodeException} will be thrown. * * <p>No watcher is set whether this succeeds or not. * * <p>Returns false if the node was not in the proper state but did exist. * * <p>This method is used when a region finishes opening/closing. * The Master acknowledges completion * of the specified regions transition to being closed/opened. * * @param zkw zk reference * @param regionName region to be deleted from zk * @param expectedState state region must be in for delete to complete * @param expectedVersion of the znode that is to be deleted. * If expectedVersion need not be compared while deleting the znode * pass -1 * @throws KeeperException if unexpected zookeeper exception * @throws KeeperException.NoNodeException if node does not exist */ public static boolean deleteNode(ZooKeeperWatcher zkw, String regionName, EventType expectedState, int expectedVersion) throws KeeperException, KeeperException.NoNodeException { LOG.debug(zkw.prefix("Deleting existing unassigned " + "node for " + regionName + " that is in expected state " + expectedState)); String node = getNodeName(zkw, regionName); zkw.sync(node); Stat stat = new Stat(); byte[] bytes = ZKUtil.getDataNoWatch(zkw, node, stat); if (bytes == null) { // If it came back null, node does not exist. throw KeeperException.create(Code.NONODE); } RegionTransitionData data = RegionTransitionData.fromBytes(bytes); if (!data.getEventType().equals(expectedState)) { LOG.warn(zkw.prefix("Attempting to delete unassigned " + "node " + regionName + " in " + expectedState + " state but node is in " + data.getEventType() + " state")); return false; } if (expectedVersion != -1 && stat.getVersion() != expectedVersion) { LOG.warn("The node " + regionName + " we are trying to delete is not" + " the expected one. Got a version mismatch"); return false; } if (!ZKUtil.deleteNode(zkw, node, stat.getVersion())) { LOG.warn(zkw.prefix("Attempting to delete " + "unassigned node " + regionName + " in " + expectedState + " state but after verifying state, we got a version mismatch")); return false; } LOG.debug(zkw.prefix("Successfully deleted unassigned node for region " + regionName + " in expected state " + expectedState)); return true; }
From source file:org.apache.niolex.address.ext.ZKOperatorTest.java
License:Apache License
@SuppressWarnings("unchecked") @Test/*from www .j a va 2s .c o m*/ public void testRemoveACLTree() throws Exception { ZooKeeper zkback = zkop.zooKeeper(); Field f = FieldUtil.getField(ZKConnector.class, "zk"); ZooKeeper zk = mock(ZooKeeper.class); KeeperException throwable1 = KeeperException.create(KeeperException.Code.BADVERSION); KeeperException throwable2 = KeeperException.create(KeeperException.Code.APIERROR); when(zk.setACL(anyString(), anyList(), anyInt())).thenThrow(throwable1, throwable2, throwable1, throwable2); // List<ACL> acl = OPMain.getCDR4Server(); FieldUtil.setFieldValue(zkop, f, zk); boolean flag = false; try { zkop.removeACL("/localhost:9001", acl); } catch (ZKException e) { flag = true; assertEquals(e.getCode(), ZKException.Code.OTHER); } assertTrue(flag); // ------ flag = false; try { zkop.addACL("/localhost:9001", acl); } catch (ZKException e) { flag = true; assertEquals(e.getCode(), ZKException.Code.OTHER); } assertTrue(flag); FieldUtil.setFieldValue(zkop, f, zkback); }
From source file:org.apache.niolex.address.ext.ZKOperatorTest.java
License:Apache License
@Test @Order(14)/*ww w . j av a 2 s. c o m*/ public void testUpdateClientTrigger() throws Exception { ZooKeeper zkback = zkop.zooKeeper(); Field f = FieldUtil.getField(ZKConnector.class, "zk"); ZooKeeper zk = mock(ZooKeeper.class); KeeperException throwable1 = KeeperException.create(KeeperException.Code.BADVERSION); KeeperException throwable2 = KeeperException.create(KeeperException.Code.APIERROR); when(zk.setData(anyString(), any(byte[].class), anyInt())).thenThrow(throwable1, throwable2, throwable1, throwable2); // FieldUtil.setFieldValue(zkop, f, zk); boolean flag = false; try { String path = "/find/services/org.new/clients/1"; zkop.updateClientTrigger(path, "lx-cli"); } catch (ZKException e) { flag = true; assertEquals(e.getCode(), ZKException.Code.OTHER); } assertTrue(flag); FieldUtil.setFieldValue(zkop, f, zkback); }
From source file:org.apache.niolex.zookeeper.core.ZKConnectorExceTest.java
License:Apache License
@Test @Order(7)/* w w w . j a va2 s . co m*/ public void testCreateNodeIfAbsentEx() throws Exception { String path = "/path/to/echeo"; ZKC.zk = mock(ZooKeeper.class); KeeperException ke = KeeperException.create(KeeperException.Code.NODEEXISTS); doThrow(ke).when(ZKC.zk).create(path, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); assertFalse(ZKC.createNodeIfAbsent(path, null)); }
From source file:org.apache.niolex.zookeeper.core.ZKConnectorExceTest.java
License:Apache License
@Test @Order(8)//from w ww .j a va 2 s .c o m public void testMakeSurePathExistsEx() throws Exception { ZKC.makeSurePathExists("/lex/zkc/a"); ZKC.makeSurePathExists("/lex/zkc/b"); ZKC.zk = mock(ZooKeeper.class); KeeperException ke = KeeperException.create(KeeperException.Code.NODEEXISTS); doThrow(ke).when(ZKC.zk).create("/lex/zkc", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); KeeperException ke2 = KeeperException.create(KeeperException.Code.AUTHFAILED); doThrow(ke2).when(ZKC.zk).create("/lex/zkc/c", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); try { ZKC.makeSurePathExists("/lex/zkc/c"); assertFalse(true); } catch (ZKException e) { assertEquals(e.getCode(), ZKException.Code.NO_AUTH); } }