public boolean disconnect() { try { if (this.zooKeeper != null) { this.zooKeeper.close(); this.zooKeeper = null; connected = false; removeWatchers(this.watchers.keySet()); return true; } } catch (Exception e) { LoggerFactory.getLogger().error( "Error occurred while disconnecting from ZooKeeper server", e); } return false; }
public String getData(String nodePath) { if (connected) { try { if (nodePath.length() == 0) { nodePath = "/"; } Stat s = zooKeeper.exists(nodePath, false); if (s != null) { return this.encryptionManager.decryptData(zooKeeper .getData(nodePath, false, s)); } } catch (Exception e) { LoggerFactory.getLogger().error( "Error occurred getting data for node: " + nodePath, e); } } return null; }
public String getNodeChild(String nodePath, int childIndex) { if (connected) { try { Stat s = zooKeeper.exists(nodePath, false); if (s != null) { return this.zooKeeper.getChildren(nodePath, false).get( childIndex); } } catch (Exception e) { LoggerFactory.getLogger().error( "Error occurred retrieving child " + childIndex + " of node: " + nodePath, e); } } return null; }
public Map<String, String> getSessionMeta() { Map<String, String> sessionMeta = new LinkedHashMap<String, String>(); try { if (zooKeeper != null) { sessionMeta.put(SESSION_ID, String.valueOf(zooKeeper .getSessionId())); sessionMeta.put(SESSION_STATE, String.valueOf(zooKeeper .getState().toString())); sessionMeta.put(CONNECT_STRING, this.connectString); sessionMeta.put(SESSION_TIMEOUT, String .valueOf(this.sessionTimeout)); } } catch (Exception e) { LoggerFactory.getLogger().error( "Error occurred retrieving session meta data.", e); } return sessionMeta; }
public boolean createNode(String parent, String nodeName) { if (connected) { try { String[] nodeElements = nodeName.split("/"); for (String nodeElement : nodeElements) { String node = parent + "/" + nodeElement; Stat s = zooKeeper.exists(node, false); if (s == null) { zooKeeper.create(node, this.encryptionManager .encryptData(null), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); parent = node; } } return true; } catch (Exception e) { LoggerFactory.getLogger().error( "Error occurred creating node: " + parent + "/" + nodeName, e); } } return false; }
public boolean deleteNode(String nodePath) { if (connected) { try { Stat s = zooKeeper.exists(nodePath, false); if (s != null) { List<String> children = zooKeeper.getChildren(nodePath, false); for (String child : children) { String node = nodePath + "/" + child; deleteNode(node); } zooKeeper.delete(nodePath, -1); } return true; } catch (Exception e) { LoggerFactory.getLogger().error( "Error occurred deleting node: " + nodePath, e); } } return false; }
public void addWatchers(Collection<String> selectedNodes, NodeListener nodeListener) { // add watcher for each node and add node to collection of // watched nodes if (connected) { for (String node : selectedNodes) { if (!watchers.containsKey(node)) { try { watchers.put(node, new NodeWatcher(node, nodeListener, zooKeeper)); } catch (Exception e) { LoggerFactory.getLogger().error( "Error occured adding node watcher for node: " + node, e); } } } } }
public void process(WatchedEvent event) { if (!closed) { try { if (event.getType() != EventType.NodeDeleted) { Stat s = zooKeeper.exists(nodePath, this); if (s != null) { zookeeper.getChildren(nodePath, this); } } } catch (Exception e) { LoggerFactory.getLogger().error( "Error occured re-adding node watcherfor node " + nodePath, e); } nodeListener.processEvent(event.getPath(), event.getType() .name(), null); } }
@Override public Stat setACL(String path, List<ACL> acl, int version) throws KeeperException, InterruptedException { int count = 0; do { try { return super.setACL(path, acl, version); } catch (KeeperException.ConnectionLossException e) { LoggerFactory.getLogger().warn( "ZooKeeper connection lost. Trying to reconnect."); Stat s = exists(path, false); if (s != null) { if (getACL(path, s).equals(acl)) { return s; } } else { return null; } } } while (!closed && (limit == -1 || count++ < limit)); return null; }
@Override public Stat setData(String path, byte[] data, int version) throws KeeperException, InterruptedException { int count = 0; do { try { return super.setData(path, data, version); } catch (KeeperException.ConnectionLossException e) { LoggerFactory.getLogger().warn( "ZooKeeper connection lost. Trying to reconnect."); Stat s = exists(path, false); if (s != null) { if (getData(path, false, s) == data) { return s; } } else { return null; } } } while (!closed && (limit == -1 || count++ < limit)); return null; }
public void addWatchers(Collection<String> selectedNodes, NodeListener nodeListener) { // add watcher for each node and add node to collection of // watched nodes if (connected) { for (String node : selectedNodes) { if (!watchers.containsKey(node)) { try { watchers.put(node, new NodeWatcher(node, nodeListener, zooKeeper)); } catch (Exception e) { LoggerFactory.getLogger().error( "Error occurred adding node watcher for node: " + node, e); } } } } }
public void process(WatchedEvent event) { if (!closed) { try { if (event.getType() != EventType.NodeDeleted) { Stat s = zooKeeper.exists(nodePath, this); if (s != null) { zookeeper.getChildren(nodePath, this); } } } catch (Exception e) { LoggerFactory.getLogger().error( "Error occurred re-adding node watcherfor node " + nodePath, e); } nodeListener.processEvent(event.getPath(), event.getType() .name(), null); } }
@Override public Stat setACL(String path, List<ACL> acl, int aclVersion) throws KeeperException, InterruptedException { int count = 0; do { try { return super.setACL(path, acl, aclVersion); } catch (KeeperException.ConnectionLossException e) { LoggerFactory.getLogger().warn( "ZooKeeper connection lost. Trying to reconnect."); Stat s = exists(path, false); if (s != null) { if (getACL(path, s).equals(acl)) { return s; } } else { return null; } } } while (!closed && (limit == -1 || count++ < limit)); return null; }