List of usage examples for org.apache.zookeeper KeeperException code
Code code
To view the source code for org.apache.zookeeper KeeperException code.
Click Source Link
From source file:com.andyadc.menagerie.ZkUtils.java
License:Apache License
/** * Deletes an element from ZooKeeper safely. * <p>/*w ww . j a va2 s. co m*/ * If the element has already been deleted from ZooKeeper, then a NoNode Exception is normally thrown by ZooKeeper. * This method suppresses those NoNode exceptions . * * @param zk the ZooKeeper client to use * @param nodeToDelete the node to delete * @param version the version of that node to remove * @return true if the node was deleted * @throws KeeperException if some issue with the ZooKeeper server occurs * @throws InterruptedException if some communication error happens * between the ZooKeeper client and the ZooKeeper service */ public static boolean safeDelete(ZooKeeper zk, String nodeToDelete, int version) throws KeeperException, InterruptedException { try { zk.delete(nodeToDelete, version); return true; } catch (KeeperException ke) { //if the node has already been deleted, don't worry about it if (ke.code() != KeeperException.Code.NONODE) throw ke; else return false; } }
From source file:com.andyadc.menagerie.ZkUtils.java
License:Apache License
/** * Deletes all the listed elements from ZooKeeper safely. * <p>// w ww .j a va 2s . co m * If any element has already been deleted from ZooKeeper, then a NoNode Exception is normally thrown by ZooKeeper. * This method suppresses those exceptions, while allowing through all other kinds. * <p> * If ZooKeeper Exception which is <i>not</i> of the type NoNode, then it will be thrown immediately, and any * elements (such as additional children) which had not yet been deleted will not be deleted by this method. * * @param zk the ZooKeeper client to use * @param nodeToDelete the node to recursively delete * @param version the version of the nodes to remove * @throws KeeperException if some issue with the ZooKeeper server which is <i>not</i> of type NoNode occurs. * @throws InterruptedException if some communication error happens between the ZooKeeper client and server */ public static void recursiveSafeDelete(ZooKeeper zk, String nodeToDelete, int version) throws KeeperException, InterruptedException { try { List<String> children = zk.getChildren(nodeToDelete, false); for (String child : children) { recursiveSafeDelete(zk, nodeToDelete + "/" + child, version); } //delete this node safeDelete(zk, nodeToDelete, version); } catch (KeeperException ke) { if (ke.code() != KeeperException.Code.NONODE) throw ke; } }
From source file:com.andyadc.menagerie.ZkUtils.java
License:Apache License
/** * Creates a new node safely.//from w w w . ja va 2 s .c o m * <p> * If a node already exists, ZooKeeper may throw a KeeperException of type {@code KeeperException.Code.NODEEXISTS}. * This method suppresses that exception, allow creates of potentially contentious nodes to remain atomic. * <p> * If the node already exists, the data will <i>not</i> be set to that node. This reflects the fact that a * create-and-set operation is not atomic, and may result in a data race. Instead, this method will quietly return. * <p> * If any other type of KeeperException is thrown, that exception will be propogated up the stack. * <p> * Note: If the CreateMode of the node of interest is sequential, then this method is unnecessary, as ZooKeeper * will never throw a NODEEXISTS exception for modes of those types. * * @param zk the ZooKeeper client to use * @param nodeToCreate the node to create * @param data the data to set on the newly created node. * @param privileges the privileges to associate with that node * @param createMode the Mode that node will possess * @return the name of the newly qualified node * @throws KeeperException if an Error occurs on the ZooKeeper server which is <i>not</i> of type NODEEXISTS * @throws InterruptedException if a communication error between the client and server occurs. */ public static String safeCreate(ZooKeeper zk, String nodeToCreate, byte[] data, List<ACL> privileges, CreateMode createMode) throws KeeperException, InterruptedException { try { return zk.create(nodeToCreate, data, privileges, createMode); } catch (KeeperException ke) { //if the node has already been created, don't worry about it if (ke.code() != KeeperException.Code.NODEEXISTS) throw ke; else { /* This only happens if {@code createMode} is EPHEMERAL or PERSISTENT--sequential modes do not throw this error code. In the case that we are looking for EPHEMERAL or PERSISTENT, then we already know the full path of the node we were trying to create, so just return that, since it already exists. */ return nodeToCreate; } } }
From source file:com.andyadc.menagerie.ZkUtils.java
License:Apache License
/** * Attempts to get data from ZooKeeper in a single operation. * <p>/*w ww .j a v a 2s .c o m*/ * Because it is possible that the node under request does not exist, it is possible that * a KeeperException.NONODE exception will be thrown. This method swallows * NONODE exceptions and returns an empty byte[] when that node does not exist. * * @param zk the ZooKeeper client * @param node the node to get data for * @param watcher any watcher to attach to this node, if it exists * @param stat a stat object for use in the getData call * @return the byte[] information contained in that node, if that node exists. If {@code node} does not exist, * then this returns an empty byte[] * @throws InterruptedException if communication between the ZooKeeper client and servers are broken * @throws KeeperException if there is a problem getting Data from the ZooKeeper server that <i>doesn't</i> have * the error code of NONODE */ public static byte[] safeGetData(ZooKeeper zk, String node, Watcher watcher, Stat stat) throws InterruptedException, KeeperException { try { return zk.getData(node, watcher, stat); } catch (KeeperException e) { if (e.code() != KeeperException.Code.NONODE) throw e; else return new byte[] {}; } }
From source file:com.andyadc.menagerie.ZkUtils.java
License:Apache License
/** * Attempts to get data from ZooKeeper in a single operation. Because it is possible that the node under request * does not exist, it is possible that a KeeperException.NONODE exception will be thrown. This method swallows * NONODE exceptions and returns an empty byte[] when that node does not exist. * * @param zk the ZooKeeper client//from w ww .j a v a2 s .c o m * @param node the node to get data for * @param watch true if the default watcher is to be attached to this node's data * @param stat a stat object for use in the getData call * @return the byte[] information contained in that node, if that node exists. If {@code node} does not exist, * then this returns an empty byte[] * @throws InterruptedException if communication between the ZooKeeper client and servers are broken * @throws KeeperException if there is a problem getting Data from the ZooKeeper server that <i>doesn't</i> have * the error code of NONODE */ public static byte[] safeGetData(ZooKeeper zk, String node, boolean watch, Stat stat) throws InterruptedException, KeeperException { try { return zk.getData(node, watch, stat); } catch (KeeperException e) { if (e.code() != KeeperException.Code.NONODE) throw e; else return new byte[] {}; } }
From source file:com.anteam.demo.zookeeper.DataMonitor.java
License:Apache License
@SuppressWarnings("deprecation") public void process(WatchedEvent event) { System.out.println("----------------------------------------"); System.out.println("event:" + event); String path = event.getPath(); System.out.println("DataMonitor.process.path:" + path); if (event.getType() == Event.EventType.None) { // We are are being told that the state of the // connection has changed switch (event.getState()) { case SyncConnected: // In this particular example we don't need to do anything // here - watches are automatically re-registered with // server and any watches triggered while the client was // disconnected will be delivered (in order of course) break; case Expired: // It's all over dead = true;//from ww w . ja v a2 s . c o m listener.closing(KeeperException.Code.SessionExpired); break; default: break; } } else { if (path != null && path.equals(znode)) { // Something has changed on the node, let's find out zk.exists(znode, true, this, null); } } if (chainedWatcher != null) { chainedWatcher.process(event); } }
From source file:com.api6.zkclient.exception.ZKException.java
License:Apache License
public static ZKException create(KeeperException e) { switch (e.code()) { case NONODE://from ww w . ja va2 s . c o m return new ZKNoNodeException(e); case NODEEXISTS: return new ZKNodeExistsException(e); default: return new ZKException(e); } }
From source file:com.att.nsa.zkUtils.ZkPath.java
License:Open Source License
public static void ensureZkPathExists(ZooKeeper zk, String path) throws InterruptedException, KeeperException { try {//from w ww . j a v a2 s .co m zk.create(path, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } catch (KeeperException e) { if (e.code().equals(Code.NODEEXISTS)) { // already there (checking exists() up front doesn't remove // the need to catch this, as independent processes could // race thru this code. } else { throw e; } } }
From source file:com.cc.zk.common.ZkStateReader.java
License:Apache License
/** * TODO ??/*w w w. ja va 2 s.com*/ * @throws KeeperException * @throws InterruptedException */ public void getRemoteLiveNodes() throws KeeperException, InterruptedException { List<String> nodes = zkClient.getChildren(ZkStateReader.LIVE_NODES_ZKNODE, new Watcher() { @Override public void process(WatchedEvent event) { // session events are not change events, // and do not remove the watcher if (EventType.None.equals(event.getType())) { return; } try { System.out.println("ZkStateReader.getLiveNodes().new Watcher() {...}.process()----------"); synchronized (ZkStateReader.this.getUpdateLock()) { List<String> nodes = zkClient.getChildren(LIVE_NODES_ZKNODE, this, true); ZkStateReader.this.liveNodes.addAll(nodes); } } catch (KeeperException e) { if (e.code() == KeeperException.Code.SESSIONEXPIRED || e.code() == KeeperException.Code.CONNECTIONLOSS) { log.warn("ZooKeeper watch triggered, but Solr cannot talk to ZK"); return; } log.error("", e); throw new ZooKeeperException(CeException.ErrorCode.SERVER_ERROR, "", e); } catch (InterruptedException e) { // Restore the interrupted status Thread.currentThread().interrupt(); log.warn("", e); return; } } }, true); ZkStateReader.this.liveNodes.addAll(nodes); }
From source file:com.cloudera.flume.master.ZKClient.java
License:Apache License
/** * Will make sure that a node exists, with a given path and data. If the node * already exists, will not try to set the data. *///from w w w . jav a2 s . c om public boolean ensureExists(final String path, final byte[] data) throws KeeperException, IOException, InterruptedException { Preconditions.checkArgument(zk != null); final FixedRetryPolicy policy = new FixedRetryPolicy(3); if (zk.exists(path, false) != null) { return true; } ZKRetryable<Boolean> retry = new ZKRetryable<Boolean>() { public boolean doTry() throws Exception { result = true; try { zk.create(path, data, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); return true; } catch (KeeperException k) { if (k.code() == Code.NODEEXISTS) { // Note that we could have this situation: try to create, succeed // but get CONNECTIONLOSS, reconnect, try to create, return false. // Until ZK-22 goes in there really isn't a much better way of doing // this result = false; return true; // true to denote success of operation } defaultHandleException(k); } return false; } }; ZKRetryHarness harness = new ZKRetryHarness(retry, policy); harness.attempt(); return retry.getResult(); }