/** * sync getChildNames * @return null if parentPath doesn't exist */ @Override public List<String> getChildNames(String parentPath, int options) { try { List<String> childNames = _zkClient.getChildren(parentPath); Collections.sort(childNames); return childNames; } catch (ZkNoNodeException e) { return null; } }
public static List<String> getInstancePropertyList(ZkClient zkClient, String clusterName, String instanceName, PropertyType property, String key) { String propertyPath = PropertyPathBuilder.instanceProperty(clusterName, instanceName, property, key); return zkClient.getChildren(propertyPath); } }
@Override public List<String> getClusters() { List<String> zkToplevelPathes = _zkClient.getChildren("/"); List<String> result = new ArrayList<String>(); for (String pathName : zkToplevelPathes) { if (ZKUtil.isClusterSetup(pathName, _zkClient)) { result.add(pathName); } } return result; }
@Override public List<String> getInstancesInCluster(String clusterName) { String memberInstancesPath = PropertyPathBuilder.instance(clusterName); return _zkClient.getChildren(memberInstancesPath); }
@Override public List<String> getResourcesInCluster(String clusterName) { return _zkClient.getChildren(PropertyPathBuilder.idealState(clusterName)); }
@Override public List<String> getStateModelDefs(String clusterName) { return _zkClient.getChildren(PropertyPathBuilder.stateModelDef(clusterName)); }
public void download(String zkPath, String fsPath) throws Exception { List<String> children = client.getChildren(zkPath); if (children != null && children.size() > 0) { new File(fsPath).mkdirs(); for (String child : children) { String childPath = zkPath.equals("/") ? "/" + child : zkPath + "/" + child; download(childPath, fsPath + "/" + child); } } else { System.out .println("Saving " + zkPath + " to " + new File(fsPath + suffix).getCanonicalPath()); OutputStream out = new FileOutputStream(fsPath + suffix); Object readData = client.readData(zkPath); if (readData != null) { out.write((byte[]) readData); } out.close(); } } }
public List<String> getChildren(final String path) { List<String> children = super.getChildren(path); for (String p : _dataMap.keySet()) { if (p.contains(path)) { String[] paths = p.split("/"); children.add(paths[paths.length-1]); } } return children; }
public static String getInstancePropertyNameListAsString(ZkClient zkClient, String clusterName, String instanceName, PropertyType instanceProperty, String key, MediaType mediaType) throws JsonGenerationException, JsonMappingException, IOException { String path = PropertyPathBuilder.instanceProperty(clusterName, instanceName, instanceProperty, key); if (zkClient.exists(path)) { List<String> recordNames = zkClient.getChildren(path); return ObjectToJson(recordNames); } return ObjectToJson(new ArrayList<String>()); }
String fromPath = concatenate(srcRootPath, path); List<String> children = srcClient.getChildren(fromPath); List<String> paths = new ArrayList<String>(); if (children != null && children.size() > 0) {
private ZNRecord readZkChild(String zkPath, ZkClient zkClient) { ZNRecord result = null; // read data and stat Stat stat = new Stat(); ZNRecord data = zkClient.readDataAndStat(zkPath, stat, true); if (data != null) { result = data; } else { result = new ZNRecord(""); } // read childrenList List<String> children = zkClient.getChildren(zkPath); if (children != null && children.size() > 0) { result.setSimpleField("numChildren", "" + children.size()); result.setListField("childrenList", children); } else { result.setSimpleField("numChildren", "" + 0); } return result; }
public static List<ZNRecord> getChildren(ZkClient client, String path) { // parent watch will be set by zkClient List<String> children = client.getChildren(path); if (children == null || children.size() == 0) { return Collections.emptyList(); } List<ZNRecord> childRecords = new ArrayList<ZNRecord>(); for (String child : children) { String childPath = path + "/" + child; Stat newStat = new Stat(); ZNRecord record = client.readDataAndStat(childPath, newStat, true); if (record != null) { record.setVersion(newStat.getVersion()); record.setCreationTime(newStat.getCtime()); record.setModifiedTime(newStat.getMtime()); childRecords.add(record); } } return childRecords; }
public boolean verifyByCallback(long timeout, List<ClusterVerifyTrigger> triggers) { _countdown = new CountDownLatch(1); for (ClusterVerifyTrigger trigger : triggers) { String path = trigger._triggerKey.getPath(); _zkclient.subscribeChildChanges(path, this); if (trigger._triggerOnChildDataChange) { List<String> childs = _zkclient.getChildren(path); for (String child : childs) { String childPath = String.format("%s/%s", path, child); _zkclient.subscribeDataChanges(childPath, this); } } } boolean success = false; try { success = verify(); if (!success) { success = _countdown.await(timeout, TimeUnit.MILLISECONDS); if (!success) { // make a final try if timeout success = verify(); } } } catch (Exception e) { LOG.error("Exception in verifier", e); } // clean up _zkclient.unsubscribeAll(); return success; }
@Override public List<String> getInstancesInClusterWithTag(String clusterName, String tag) { String memberInstancesPath = PropertyPathBuilder.instance(clusterName); List<String> instances = _zkClient.getChildren(memberInstancesPath); List<String> result = new ArrayList<String>(); HelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_zkClient)); Builder keyBuilder = accessor.keyBuilder(); for (String instanceName : instances) { InstanceConfig config = accessor.getProperty(keyBuilder.instanceConfig(instanceName)); if (config == null) { throw new IllegalStateException(String .format("Instance %s does not have a config, cluster might be in bad state", instanceName)); } if (config.containsTag(tag)) { result.add(instanceName); } } return result; }
retKeys = new ArrayList<String>(record.getMapFields().keySet()); } else { retKeys = zkClient.getChildren(zkPath);
List<String> children = _zkClient.getChildren(path); for (String child : children) { String newPath = path + "/" + child;
private void subscribeTrigger(ClusterVerifyTrigger trigger) { String path = trigger.getTriggerKey().getPath(); if (trigger.isTriggerOnDataChange()) { _zkClient.subscribeDataChanges(path, this); } if (trigger.isTriggerOnChildChange()) { _zkClient.subscribeChildChanges(path, this); } if (trigger.isTriggerOnChildDataChange()) { List<String> childs = _zkClient.getChildren(path); for (String child : childs) { String childPath = String.format("%s/%s", path, child); _zkClient.subscribeDataChanges(childPath, this); } } }
@Override public Representation delete() { String zkPath = getZKPath(); try { ZkClient zkClient = (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT); List<String> childNames = zkClient.getChildren(zkPath); if (childNames != null) { for (String childName : childNames) { String childPath = zkPath.equals("/") ? "/" + childName : zkPath + "/" + childName; zkClient.deleteRecursively(childPath); } } getResponse().setStatus(Status.SUCCESS_OK); } catch (Exception e) { getResponse().setEntity(ClusterRepresentationUtil.getErrorAsJsonStringFromException(e), MediaType.APPLICATION_JSON); getResponse().setStatus(Status.SUCCESS_OK); LOG.error("Error in delete zkChild: " + zkPath, e); } return null; } }
private ZNRecord readZkDataStatAndChild(String zkPath, ZkClient zkClient) { ZNRecord result = null; // read data and stat Stat stat = new Stat(); ZNRecord data = zkClient.readDataAndStat(zkPath, stat, true); if (data != null) { result = data; } else { result = new ZNRecord(""); } result.setSimpleField("zkPath", zkPath); result.setSimpleField("stat", stat.toString()); result.setSimpleField("numChildren", "" + stat.getNumChildren()); result.setSimpleField("ctime", "" + new Date(stat.getCtime())); result.setSimpleField("mtime", "" + new Date(stat.getMtime())); result.setSimpleField("dataLength", "" + stat.getDataLength()); // read childrenList List<String> children = zkClient.getChildren(zkPath); if (children != null && children.size() > 0) { result.setListField("children", children); } return result; }
@Override public Representation post(Representation entity) { String zkPath = getZKPath(); try { JsonParameters jsonParameters = new JsonParameters(entity); String command = jsonParameters.getCommand(); ZkClient zkClient = (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT); if (command.equalsIgnoreCase(JsonParameters.ZK_DELETE_CHILDREN)) { List<String> childNames = zkClient.getChildren(zkPath); if (childNames != null) { for (String childName : childNames) { String childPath = zkPath.equals("/") ? "/" + childName : zkPath + "/" + childName; zkClient.deleteRecursively(childPath); } } } else { throw new HelixException("Unsupported command: " + command + ". Should be one of [" + JsonParameters.ZK_DELETE_CHILDREN + "]"); } getResponse().setStatus(Status.SUCCESS_OK); } catch (Exception e) { getResponse().setEntity(ClusterRepresentationUtil.getErrorAsJsonStringFromException(e), MediaType.APPLICATION_JSON); getResponse().setStatus(Status.SUCCESS_OK); LOG.error("Error in post zkPath: " + zkPath, e); } return null; }