public boolean existsNode(CuratorFramework zk, String path, boolean watch) throws Exception { Stat stat = null; if (watch) { stat = zk.checkExists().watched().forPath(PathUtils.normalize_path(path)); } else { stat = zk.checkExists().forPath(PathUtils.normalize_path(path)); } return stat != null; }
public Integer getVersion(CuratorFramework zk, String path, boolean watch) throws Exception { String npath = PathUtils.normalize_path(path); Stat stat = null; if (existsNode(zk, npath, watch)) { if (watch) { stat = zk.checkExists().watched().forPath(PathUtils.normalize_path(path)); } else { stat = zk.checkExists().forPath(PathUtils.normalize_path(path)); } return Integer.valueOf(stat.getVersion()); } return null; }
public String createNode( CuratorFramework zk, String path, byte[] data, org.apache.zookeeper.CreateMode mode) throws Exception { String npath = PathUtils.normalize_path(path); return zk.create().withMode(mode).withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE).forPath(npath, data); }
public void deletereRcursive(CuratorFramework zk, String path) throws Exception { String npath = PathUtils.normalize_path(path); if (existsNode(zk, npath, false)) { zk.delete().guaranteed().deletingChildrenIfNeeded().forPath(npath); } }
public List<String> getChildren(CuratorFramework zk, String path, boolean watch) throws Exception { String npath = PathUtils.normalize_path(path); if (watch) { return zk.getChildren().watched().forPath(npath); } else { return zk.getChildren().forPath(npath); } }
public byte[] getData(CuratorFramework zk, String path, boolean watch) throws Exception { String npath = PathUtils.normalize_path(path); try { if (existsNode(zk, npath, watch)) { if (watch) { return zk.getData().watched().forPath(npath); } else { return zk.getData().forPath(npath); } } } catch (KeeperException e) { LOG.error("zookeeper getdata for path" + path, e); } return null; }
public static String getZKNodeData(String clusterName, String path) { String out = null; try { ClusterState clusterState = getAndCreateClusterState(clusterName); if (clusterState == null) { throw new IllegalStateException("Cluster state is null"); } byte[] data = clusterState.get_data(PathUtils.normalize_path(path), false); if (data != null && data.length > 0) { Object obj = Utils.maybe_deserialize(data); out = gson.toJson(obj); } } catch (Exception e) { LOG.error("Get zookeeper data error!", e); } return out; }
public static List<ZookeeperNode> listZKNodes(String clusterName, String parent) { List<ZookeeperNode> nodes = new ArrayList<>(); try { ClusterState clusterState = getAndCreateClusterState(clusterName); if (clusterState == null) { throw new IllegalStateException("Cluster state is null"); } List<String> elements = clusterState.get_children(parent, false); for (String element : elements) { String path = PathUtils.normalize_path(parent + Cluster.ZK_SEPERATOR + element); nodes.add(new ZookeeperNode(parent, element, hasChildren(clusterState, path))); } } catch (Exception e) { LOG.error("Get zookeeper info error!", e); } return nodes; }
public void mkdirs(CuratorFramework zk, String path) throws Exception { String npath = PathUtils.normalize_path(path); // the node is "/" if (npath.equals("/")) { return; } // the node exist if (existsNode(zk, npath, false)) { return; } mkdirs(zk, PathUtils.parent_path(npath)); try { createNode(zk, npath, JStormUtils.barr((byte) 7), org.apache.zookeeper.CreateMode.PERSISTENT); } catch (KeeperException e) {; // this can happen when multiple clients doing mkdir at same // time LOG.warn("zookeeper mkdirs for path" + path, e); } }
public Stat setData(CuratorFramework zk, String path, byte[] data) throws Exception { String npath = PathUtils.normalize_path(path); return zk.setData().forPath(npath, data); }
public void deleteNode(CuratorFramework zk, String path) throws Exception { zk.delete().forPath(PathUtils.normalize_path(path)); }