List of usage examples for org.apache.zookeeper KeeperException create
public static KeeperException create(Code code)
From source file:com.alibaba.wasp.zookeeper.ZKAssign.java
License:Apache License
/** * Deletes an existing unassigned node that is in the specified state for the * specified entityGroup.//from w ww .j a v a2 s . com * * <p> * If a node does not already exist for this entityGroup, a * {@link org.apache.zookeeper.KeeperException.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 entityGroup finishes opening/closing. The Master * acknowledges completion of the specified entityGroups transition to being * closed/opened. * * @param zkw zk reference * @param entityGroupName entityGroup to be deleted from zk * @param expectedState state entityGroup 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 org.apache.zookeeper.KeeperException if unexpected zookeeper exception * @throws org.apache.zookeeper.KeeperException.NoNodeException if node does not exist */ public static boolean deleteNode(ZooKeeperWatcher zkw, String entityGroupName, EventType expectedState, int expectedVersion) throws KeeperException, KeeperException.NoNodeException { LOG.debug(zkw.prefix("Deleting existing unassigned " + "node for " + entityGroupName + " that is in expected state " + expectedState)); String node = getNodeName(zkw, entityGroupName); 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); } EntityGroupTransaction rt = getEntityGroupTransition(bytes); EventType et = rt.getEventType(); if (!et.equals(expectedState)) { LOG.warn(zkw.prefix("Attempting to delete unassigned node " + entityGroupName + " in " + expectedState + " state but node is in " + et + " state")); return false; } if (expectedVersion != -1 && stat.getVersion() != expectedVersion) { LOG.warn("The node " + entityGroupName + " 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 " + entityGroupName + " in " + expectedState + " state but after verifying state, we got a version mismatch")); return false; } LOG.debug(zkw.prefix("Successfully deleted unassigned node for entityGroup " + entityGroupName + " in expected state " + expectedState)); return true; }
From source file:com.edmunds.zookeeper.election.ZooKeeperElection.java
License:Apache License
private void error(String message, Code code, String path, ZooKeeperElectionContext context) { // Don't bother generating a stack trace if the connection is lost. final KeeperException exception = code == Code.CONNECTIONLOSS ? null : KeeperException.create(code); logger.error(String.format("%s [path = %s]", message, path), exception); withdrawInternal(context, String.format("Error: %s [path = %s]", message, path)); }
From source file:com.linkedin.d2.balancer.zkfs.ZKFSDirectory.java
License:Apache License
@Override public void getServiceNames(final Callback<List<String>> callback) { final ZooKeeper zk = _connection.getZooKeeper(); final String path = ZKFSUtil.servicePath(_basePath); zk.getChildren(path, false, new AsyncCallback.Children2Callback() { @Override//from w w w .j a va2 s . co m public void processResult(int rc, String path, Object ctx, List<String> children, Stat stat) { KeeperException.Code code = KeeperException.Code.get(rc); switch (code) { case OK: callback.onSuccess(children); break; case NONODE: callback.onSuccess(Collections.<String>emptyList()); break; default: callback.onError(KeeperException.create(code)); break; } } }, null); }
From source file:com.linkedin.d2.balancer.zkfs.ZKFSDirectory.java
License:Apache License
@Override public void getClusterNames(final Callback<List<String>> callback) { final ZooKeeper zk = _connection.getZooKeeper(); final String path = ZKFSUtil.clusterPath(_basePath); zk.getChildren(path, false, new AsyncCallback.Children2Callback() { @Override/* w w w .ja v a 2 s .c om*/ public void processResult(int rc, String path, Object ctx, List<String> children, Stat stat) { KeeperException.Code code = KeeperException.Code.get(rc); switch (code) { case OK: callback.onSuccess(children); break; case NONODE: callback.onSuccess(Collections.<String>emptyList()); break; default: callback.onError(KeeperException.create(code)); break; } } }, null); }
From source file:com.linkedin.d2.discovery.stores.zk.DeltaWriteZooKeeperPermanentStore.java
License:Apache License
/** * Returns callback of ensurePersistentNodeExists used in ZookeeperPermanentStore's put. * The callback would first try getting existing node's data. If it does not exist or is * different from the new value, it is overwritten. Otherwise nothing happens. * * @param listenTo// www. java2s .c o m * @param discoveryProperties * @param callback */ @Override protected Callback<None> getExistsCallBack(final String listenTo, final T discoveryProperties, final Callback<None> callback) { final String path = getPath(listenTo); final AsyncCallback.StatCallback dataCallback = new AsyncCallback.StatCallback() { @Override public void processResult(int rc, String path, Object ctx, Stat stat) { KeeperException.Code code = KeeperException.Code.get(rc); switch (code) { case OK: callback.onSuccess(None.none()); break; default: callback.onError(KeeperException.create(code)); break; } } }; final AsyncCallback.DataCallback getDataCallback = new AsyncCallback.DataCallback() { @Override public void processResult(int rc, String path, Object context, byte[] bytes, Stat stat) { _log.debug("Data callback got rc {} for path {}", rc, path); KeeperException.Code result = KeeperException.Code.get(rc); switch (result) { case OK: // Get property currently in store. T propertiesInStore = null; if (bytes != null) { try { propertiesInStore = _serializer.fromBytes(bytes); } catch (PropertySerializationException e) { _log.warn("Unable to de-serialize properties for {}, overwriting", path, e); } } // Compare with new property and only call setData if it is different. if (propertiesInStore == null || !propertiesInStore.equals(discoveryProperties)) { _log.debug("Updating value for {}", path); _zk.setData(path, _serializer.toBytes(discoveryProperties), -1, dataCallback, null); } else { _log.debug("Node is up to date, skipping write for {}", path); callback.onSuccess(None.none()); } break; default: callback.onError(KeeperException.create(result)); break; } } }; return new Callback<None>() { @Override public void onSuccess(None none) { _zk.getData(path, false, getDataCallback, null); } @Override public void onError(Throwable e) { _log.debug("Exist : failed for path {}", path); callback.onError(e); } }; }
From source file:com.linkedin.d2.discovery.stores.zk.ZKConnection.java
License:Apache License
/** * checks if the path in zk exist or not. If it doesn't exist, will create the node. * * @param path//from w ww . j av a 2s . c o m * @param callback */ public void ensurePersistentNodeExists(String path, final Callback<None> callback) { final ZooKeeper zk = zk(); // Remove any trailing slash except for when we just want the root while (path.endsWith("/") && path.length() > 1) { path = path.substring(0, path.length() - 1); } final String normalizedPath = path; AsyncCallback.StringCallback createCallback = new AsyncCallback.StringCallback() { @Override public void processResult(int rc, String unused, Object ctx, String name) { KeeperException.Code code = KeeperException.Code.get(rc); switch (code) { case OK: case NODEEXISTS: callback.onSuccess(None.none()); break; case NONODE: // create parent and retry String parent = normalizedPath.substring(0, normalizedPath.lastIndexOf('/')); ensurePersistentNodeExists(parent, new Callback<None>() { @Override public void onSuccess(None none) { ensurePersistentNodeExists(normalizedPath, callback); } @Override public void onError(Throwable e) { callback.onError(e); } }); break; default: callback.onError(KeeperException.create(code)); break; } } }; try { zk.create(normalizedPath, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, createCallback, null); } catch (Exception e) { callback.onError(e); } }
From source file:com.linkedin.d2.discovery.stores.zk.ZKConnection.java
License:Apache License
private void setDataUnsafe(final String path, final byte[] data, final Callback<None> callback, final int count) { final ZooKeeper zk = zk(); final AsyncCallback.StatCallback dataCallback = new AsyncCallback.StatCallback() { @Override/*from w w w . j a v a 2s. c om*/ public void processResult(int rc, String path, Object ctx, Stat stat) { KeeperException.Code code = KeeperException.Code.get(rc); switch (code) { case OK: callback.onSuccess(None.none()); break; case BADVERSION: if (count < MAX_RETRIES) { LOG.info("setDataUnsafe: ignored BADVERSION for {}", path); setDataUnsafe(path, data, callback, count + 1); } else { callback.onError(KeeperException.create(code)); } break; default: callback.onError(KeeperException.create(code)); break; } } }; final AsyncCallback.StatCallback statCallback = new AsyncCallback.StatCallback() { @Override public void processResult(int rc, String path, Object ctx, Stat stat) { KeeperException.Code code = KeeperException.Code.get(rc); switch (code) { case OK: zk.setData(path, data, stat.getVersion(), dataCallback, null); break; default: callback.onError(KeeperException.create(code)); break; } } }; try { zk.exists(path, false, statCallback, null); } catch (Exception e) { callback.onError(e); } }
From source file:com.linkedin.d2.discovery.stores.zk.ZKConnection.java
License:Apache License
private void removeNodeUnsafe(final String path, final Callback<None> callback, final int count) { final ZooKeeper zk = zk(); final AsyncCallback.VoidCallback deleteCallback = new AsyncCallback.VoidCallback() { @Override//w ww. j a v a2 s.com public void processResult(int rc, String path, Object ctx) { KeeperException.Code code = KeeperException.Code.get(rc); switch (code) { case OK: callback.onSuccess(None.none()); break; case BADVERSION: // Need to retry if (count < MAX_RETRIES) { LOG.info("removeNodeUnsafe: retrying after ignoring BADVERSION for {}", path); removeNodeUnsafe(path, callback, count + 1); } else { callback.onError(KeeperException.create(code)); } break; default: callback.onError(KeeperException.create(code)); break; } } }; final AsyncCallback.StatCallback existsCallback = new AsyncCallback.StatCallback() { @Override public void processResult(int rc, String path, Object ctx, Stat stat) { KeeperException.Code code = KeeperException.Code.get(rc); switch (code) { case OK: zk.delete(path, stat.getVersion(), deleteCallback, null); break; case NONODE: callback.onSuccess(None.none()); break; default: callback.onError(KeeperException.create(code)); break; } } }; try { zk.exists(path, false, existsCallback, null); } catch (Exception e) { callback.onError(e); } }
From source file:com.linkedin.d2.discovery.stores.zk.ZKConnection.java
License:Apache License
/** * see {@link #removeNodeUnsafe} but remove recursively * * @param path/* w w w. ja v a 2 s. c om*/ * @param callback */ public void removeNodeUnsafeRecursive(final String path, final Callback<None> callback) { final ZooKeeper zk = zk(); final Callback<None> deleteThisNodeCallback = new Callback<None>() { @Override public void onSuccess(None none) { removeNodeUnsafe(path, callback); } @Override public void onError(Throwable e) { callback.onError(e); } }; // Note ChildrenCallback is compatible with a ZK 3.2 server; Children2Callback is // compatible only with ZK 3.3+ server. final AsyncCallback.ChildrenCallback childCallback = new AsyncCallback.ChildrenCallback() { @Override public void processResult(int rc, String path, Object ctx, List<String> children) { KeeperException.Code code = KeeperException.Code.get(rc); switch (code) { case OK: Callback<None> multiCallback = Callbacks.countDown(deleteThisNodeCallback, children.size()); for (String child : children) { removeNodeUnsafeRecursive(path + "/" + child, multiCallback); } break; default: callback.onError(KeeperException.create(code)); break; } } }; try { zk.getChildren(path, false, childCallback, null); } catch (Exception e) { callback.onError(e); } }
From source file:com.linkedin.d2.discovery.stores.zk.ZkUtil.java
License:Apache License
public static <T> AsyncCallback.DataCallback zkDataCallback(final Callback<T> callback, final PropertySerializer<T> serializer) { return new AsyncCallback.DataCallback() { @Override/*from w w w.j ava 2 s .c om*/ public void processResult(int rc, String path, Object context, byte[] bytes, Stat stat) { LOG.trace("Data callback got rc {} for path {}", rc, path); KeeperException.Code result = KeeperException.Code.get(rc); switch (result) { case OK: try { callback.onSuccess(serializer.fromBytes(bytes)); } catch (PropertySerializationException e) { callback.onError(e); } break; case NONODE: callback.onSuccess(null); break; default: callback.onError(KeeperException.create(result)); break; } } }; }