public static boolean hasChildren(ClusterState clusterState, String path) throws Exception { if (clusterState == null || path == null) { return false; } List<String> children = clusterState.get_children(path, false); return children != null && !children.isEmpty(); }
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; }
private static ClusterState getAndCreateClusterState(String clusterName) { ClusterState state = null; try { state = clusterSates.get(clusterName); if (state == null) { Map zkConf = UIUtils.resetZKConfig(Utils.readStormConfig(), clusterName); state = new DistributedClusterState(zkConf); ClusterState old = clusterSates.putIfAbsent(clusterName, state); if (old != null) { try { state.close(); } catch (Exception e) { LOG.warn("Close state error!", e); } state = old; } } } catch (Exception e) { LOG.error("Create cluster state error!"); } return state; }