private void delete(String zkPath) { client.deleteRecursively(zkPath); }
public static void dropChildren(ZkClient client, String parentPath, ZNRecord nodeRecord) { // TODO: check if parentPath exists String id = nodeRecord.getId(); String temp = parentPath + "/" + id; client.deleteRecursively(temp); }
if (recreateIfExists) { logger.warn("Root directory exists.Cleaning the root directory:" + root); _zkClient.deleteRecursively(root); } else { logger.info("Cluster " + clusterName + " already exists");
@Override public void addStateModelDef(String clusterName, String stateModelDef, StateModelDefinition stateModel, boolean recreateIfExists) { logger .info("Add StateModelDef {} in cluster {} with StateModel {}.", stateModelDef, clusterName, stateModel == null ? "NULL" : stateModel.toString()); if (!ZKUtil.isClusterSetup(clusterName, _zkClient)) { throw new HelixException("cluster " + clusterName + " is not setup yet"); } String stateModelDefPath = PropertyPathBuilder.stateModelDef(clusterName); String stateModelPath = stateModelDefPath + "/" + stateModelDef; if (_zkClient.exists(stateModelPath)) { if (recreateIfExists) { logger.info( "Operation.State Model directory exists:" + stateModelPath + ", remove and recreate."); _zkClient.deleteRecursively(stateModelPath); } else { logger.info("Skip the operation. State Model directory exists:" + stateModelPath); return; } } HelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_zkClient)); Builder keyBuilder = accessor.keyBuilder(); accessor.setProperty(keyBuilder.stateModelDef(stateModelDef), stateModel); }
@Override public void dropCluster(String clusterName) { logger.info("Deleting cluster {}.", clusterName); HelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_zkClient)); Builder keyBuilder = accessor.keyBuilder(); String root = "/" + clusterName; if (accessor.getChildNames(keyBuilder.liveInstances()).size() > 0) { throw new HelixException( "There are still live instances in the cluster, shut them down first."); } if (accessor.getProperty(keyBuilder.controllerLeader()) != null) { throw new HelixException("There are still LEADER in the cluster, shut them down first."); } _zkClient.deleteRecursively(root); }
@Override public Representation delete() { String zkPath = getZKPath(); try { ZkClient zkClient = (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT); zkClient.deleteRecursively(zkPath); 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 zkPath: " + zkPath, e); } return null; }
@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; } }
@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; }
_zkclient.deleteRecursively(path);
@Override public void dropInstance(String clusterName, InstanceConfig instanceConfig) { logger.info("Drop instance {} from cluster {}.", instanceConfig.getInstanceName(), clusterName); String instanceName = instanceConfig.getInstanceName(); String instanceConfigPath = PropertyPathBuilder.instanceConfig(clusterName, instanceName); if (!_zkClient.exists(instanceConfigPath)) { throw new HelixException( "Node " + instanceName + " does not exist in config for cluster " + clusterName); } String instancePath = PropertyPathBuilder.instance(clusterName, instanceName); if (!_zkClient.exists(instancePath)) { throw new HelixException( "Node " + instanceName + " does not exist in instances for cluster " + clusterName); } String liveInstancePath = PropertyPathBuilder.liveInstance(clusterName, instanceName); if (_zkClient.exists(liveInstancePath)) { throw new HelixException( "Node " + instanceName + " is still alive for cluster " + clusterName + ", can't drop."); } // delete config path String instanceConfigsPath = PropertyPathBuilder.instanceConfig(clusterName); ZKUtil.dropChildren(_zkClient, instanceConfigsPath, instanceConfig.getRecord()); // delete instance path _zkClient.deleteRecursively(instancePath); }
@Test() void testGetStat() { String path = "/tmp/getStatTest"; _zkClient.deleteRecursively(path); Stat stat, newStat; stat = _zkClient.getStat(path); AssertJUnit.assertNull(stat); _zkClient.createPersistent(path, true); stat = _zkClient.getStat(path); AssertJUnit.assertNotNull(stat); newStat = _zkClient.getStat(path); AssertJUnit.assertEquals(stat, newStat); _zkClient.writeData(path, new ZNRecord("Test")); newStat = _zkClient.getStat(path); AssertJUnit.assertNotSame(stat, newStat); }