private void updateClusterConfig(String clusterName, ClusterConfig clusterConfig, boolean overwrite) { if (!ZKUtil.isClusterSetup(clusterName, zkClient)) { throw new HelixException("fail to update config. cluster: " + clusterName + " is NOT setup."); } HelixConfigScope scope = new HelixConfigScopeBuilder(ConfigScopeProperty.CLUSTER).forCluster(clusterName).build(); String zkPath = scope.getZkPath(); if (overwrite) { ZKUtil.createOrReplace(zkClient, zkPath, clusterConfig.getRecord(), true); } else { ZKUtil.createOrUpdate(zkClient, zkPath, clusterConfig.getRecord(), true, true); } }
@Test() public void testChildrenOperations() { List<ZNRecord> list = new ArrayList<ZNRecord>(); list.add(new ZNRecord("id1")); list.add(new ZNRecord("id2")); String path = PropertyPathBuilder.instanceConfig(clusterName); ZKUtil.createChildren(_gZkClient, path, list); list = ZKUtil.getChildren(_gZkClient, path); AssertJUnit.assertEquals(2, list.size()); ZKUtil.dropChildren(_gZkClient, path, list); ZKUtil.dropChildren(_gZkClient, path, new ZNRecord("id1")); list = ZKUtil.getChildren(_gZkClient, path); AssertJUnit.assertEquals(0, list.size()); ZKUtil.dropChildren(_gZkClient, path, (List<ZNRecord>) null); }
/** * Remove multiple configs * * @param scope scope specification of the entity set to query (e.g. cluster, resource, * participant, etc.) * @param recordToRemove the ZNRecord that holds the entries that needs to be removed */ public void remove(HelixConfigScope scope, ZNRecord recordToRemove) { if (scope == null || scope.getType() == null || !scope.isFullKey()) { LOG.error("fail to remove. invalid scope: " + scope); return; } String clusterName = scope.getClusterName(); if (!ZKUtil.isClusterSetup(clusterName, zkClient)) { throw new HelixException("fail to remove. cluster " + clusterName + " is not setup yet"); } String zkPath = scope.getZkPath(); ZKUtil.subtract(zkClient, zkPath, recordToRemove); }
/** * Partially updates the fields appearing in the given IdealState (input). * @param clusterName * @param resourceName * @param idealState */ @Override public void updateIdealState(String clusterName, String resourceName, IdealState idealState) { if (!ZKUtil.isClusterSetup(clusterName, _zkClient)) { throw new HelixException( "updateIdealState failed. Cluster: " + clusterName + " is NOT setup properly."); } String zkPath = PropertyPathBuilder.idealState(clusterName, resourceName); if (!_zkClient.exists(zkPath)) { throw new HelixException(String.format( "updateIdealState failed. The IdealState for the given resource does not already exist. Resource name: %s", resourceName)); } // Update by way of merge ZKUtil.createOrUpdate(_zkClient, zkPath, idealState.getRecord(), true, true); }
@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; }
if (!ZKUtil.isClusterSetup(clusterName, zkClient)) { throw new HelixException("cluster: " + clusterName + " is NOT setup."); String scopeStr = scope.getScopeStr(); String instanceName = scopeStr.substring(scopeStr.lastIndexOf('/') + 1); if (!ZKUtil.isInstanceSetup(zkClient, scope.getClusterName(), instanceName, InstanceType.PARTICIPANT)) { throw new HelixException("instance: " + instanceName + " is NOT setup in cluster: " ZKUtil.createOrMerge(zkClient, splits[0], update, true, true);
@Override public void addInstanceTag(String clusterName, String instanceName, String tag) { logger .info("Add instance tag {} for instance {} in cluster {}.", tag, instanceName, clusterName); if (!ZKUtil.isClusterSetup(clusterName, _zkClient)) { throw new HelixException("cluster " + clusterName + " is not setup yet"); } if (!ZKUtil.isInstanceSetup(_zkClient, clusterName, instanceName, InstanceType.PARTICIPANT)) { throw new HelixException("cluster " + clusterName + " instance " + instanceName + " is not setup yet"); } HelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_zkClient)); Builder keyBuilder = accessor.keyBuilder(); InstanceConfig config = accessor.getProperty(keyBuilder.instanceConfig(instanceName)); config.addTag(tag); accessor.setProperty(keyBuilder.instanceConfig(instanceName), config); }
@Override public void addInstance(String clusterName, InstanceConfig instanceConfig) { logger.info("Add instance {} to cluster {}.", instanceConfig.getInstanceName(), clusterName); if (!ZKUtil.isClusterSetup(clusterName, _zkClient)) { throw new HelixException("cluster " + clusterName + " is not setup yet"); } String instanceConfigsPath = PropertyPathBuilder.instanceConfig(clusterName); String nodeId = instanceConfig.getId(); String instanceConfigPath = instanceConfigsPath + "/" + nodeId; if (_zkClient.exists(instanceConfigPath)) { throw new HelixException("Node " + nodeId + " already exists in cluster " + clusterName); } ZKUtil.createChildren(_zkClient, instanceConfigsPath, instanceConfig.getRecord()); _zkClient.createPersistent(PropertyPathBuilder.instanceMessage(clusterName, nodeId), true); _zkClient.createPersistent(PropertyPathBuilder.instanceCurrentState(clusterName, nodeId), true); _zkClient.createPersistent(PropertyPathBuilder.instanceError(clusterName, nodeId), true); _zkClient.createPersistent(PropertyPathBuilder.instanceStatusUpdate(clusterName, nodeId), true); _zkClient.createPersistent(PropertyPathBuilder.instanceHistory(clusterName, nodeId), true); }
public static void createChildren(ZkClient client, String parentPath, List<ZNRecord> list) { client.createPersistent(parentPath, true); if (list != null) { for (ZNRecord record : list) { createChildren(client, parentPath, record); } } }
if (!ZKUtil.isInstanceSetup(_zkclient, _clusterName, _instanceName, _instanceType)) { if (!autoJoin) { throw new HelixException("Initial cluster structure is not set up for instance: "
String path = PropertyPathBuilder.instanceConfig(clusterName, "id7"); ZNRecord record = new ZNRecord("id7"); ZKUtil.createOrMerge(_gZkClient, path, record, true, true); record = _gZkClient.readData(path); AssertJUnit.assertEquals("id7", record.getId()); List<String> list = Arrays.asList("value1", "value2"); record.setListField("list", list); ZKUtil.createOrUpdate(_gZkClient, path, record, true, true); record = _gZkClient.readData(path); AssertJUnit.assertEquals(list, record.getListField("list")); List<String> list2 = Arrays.asList("value3", "value4"); record.setListField("list", list2); ZKUtil.createOrUpdate(_gZkClient, path, record, true, true); record = _gZkClient.readData(path); AssertJUnit.assertEquals(list2, record.getListField("list")); ZKUtil.createOrUpdate(_gZkClient, path, record, true, true); record = _gZkClient.readData(path); AssertJUnit.assertEquals(map, record.getMapField("map")); Map<String, String> map2 = new HashMap<String, String>() {{put("k2", "v2");}}; record.setMapField("map", map2); ZKUtil.createOrUpdate(_gZkClient, path, record, true, true); record = _gZkClient.readData(path); AssertJUnit.assertEquals(new HashMap<String, String>() {{
public static void dropChildren(ZkClient client, String parentPath, List<ZNRecord> list) { // TODO: check if parentPath exists if (list != null) { for (ZNRecord record : list) { dropChildren(client, parentPath, record); } } }
/** * Selectively removes fields appearing in the given IdealState (input) from the IdealState in ZK. * @param clusterName * @param resourceName * @param idealState */ @Override public void removeFromIdealState(String clusterName, String resourceName, IdealState idealState) { String zkPath = PropertyPathBuilder.idealState(clusterName, resourceName); ZKUtil.subtract(_zkClient, zkPath, idealState.getRecord()); }
@Test() public void testCreateOrReplace() { String path = PropertyPathBuilder.instanceConfig(clusterName, "id8"); ZNRecord record = new ZNRecord("id8"); ZKUtil.createOrReplace(_gZkClient, path, record, true); record = _gZkClient.readData(path); AssertJUnit.assertEquals("id8", record.getId()); record = new ZNRecord("id9"); ZKUtil.createOrReplace(_gZkClient, path, record, true); record = _gZkClient.readData(path); AssertJUnit.assertEquals("id9", record.getId()); }
@Test() public void testCreateOrMerge() { String path = PropertyPathBuilder.instanceConfig(clusterName, "id7"); ZNRecord record = new ZNRecord("id7"); List<String> list = Arrays.asList("value1"); record.setListField("list", list); ZKUtil.createOrMerge(_gZkClient, path, record, true, true); record = _gZkClient.readData(path); AssertJUnit.assertEquals(list, record.getListField("list")); record = new ZNRecord("id7"); List<String> list2 = Arrays.asList("value2"); record.setListField("list", list2); ZKUtil.createOrMerge(_gZkClient, path, record, true, true); record = _gZkClient.readData(path); AssertJUnit.assertEquals(Arrays.asList("value1", "value2"), record.getListField("list")); Map<String, String> map = new HashMap<String, String>() {{put("k1", "v1");}}; record.setMapField("map", map); ZKUtil.createOrMerge(_gZkClient, path, record, true, true); record = _gZkClient.readData(path); AssertJUnit.assertEquals(map, record.getMapField("map")); record = new ZNRecord("id7"); Map<String, String> map2 = new HashMap<String, String>() {{put("k2", "v2");}}; record.setMapField("map", map2); ZKUtil.createOrMerge(_gZkClient, path, record, true, true); record = _gZkClient.readData(path); AssertJUnit.assertEquals(new HashMap<String, String>() {{ put("k1", "v1"); put("k2", "v2"); }}, record.getMapField("map")); }
private boolean isClusterExist(String cluster) { HelixZkClient zkClient = getHelixZkClient(); if (ZKUtil.isClusterSetup(cluster, zkClient)) { return true; } return false; } }
if (!ZKUtil.isClusterSetup(clusterName, zkClient)) { throw new HelixException("cluster: " + clusterName + " is NOT setup."); String scopeStr = scope.getScopeStr(); String instanceName = scopeStr.substring(scopeStr.lastIndexOf('/') + 1); if (!ZKUtil.isInstanceSetup(zkClient, scope.getClusterName(), instanceName, InstanceType.PARTICIPANT)) { throw new HelixException("instance: " + instanceName + " is NOT setup in cluster: " ZKUtil.createOrMerge(zkClient, splits[0], update, true, true);
@Override public void removeInstanceTag(String clusterName, String instanceName, String tag) { logger.info("Remove instance tag {} for instance {} in cluster {}.", tag, instanceName, clusterName); if (!ZKUtil.isClusterSetup(clusterName, _zkClient)) { throw new HelixException("cluster " + clusterName + " is not setup yet"); } if (!ZKUtil.isInstanceSetup(_zkClient, clusterName, instanceName, InstanceType.PARTICIPANT)) { throw new HelixException( "cluster " + clusterName + " instance " + instanceName + " is not setup yet"); } ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_zkClient)); Builder keyBuilder = accessor.keyBuilder(); InstanceConfig config = accessor.getProperty(keyBuilder.instanceConfig(instanceName)); config.removeTag(tag); accessor.setProperty(keyBuilder.instanceConfig(instanceName), config); }
@Override public void addInstance(String clusterName, InstanceConfig instanceConfig) { logger.info("Add instance {} to cluster {}.", instanceConfig.getInstanceName(), clusterName); if (!ZKUtil.isClusterSetup(clusterName, _zkClient)) { throw new HelixException("cluster " + clusterName + " is not setup yet"); } String instanceConfigsPath = PropertyPathBuilder.instanceConfig(clusterName); String nodeId = instanceConfig.getId(); String instanceConfigPath = instanceConfigsPath + "/" + nodeId; if (_zkClient.exists(instanceConfigPath)) { throw new HelixException("Node " + nodeId + " already exists in cluster " + clusterName); } ZKUtil.createChildren(_zkClient, instanceConfigsPath, instanceConfig.getRecord()); _zkClient.createPersistent(PropertyPathBuilder.instanceMessage(clusterName, nodeId), true); _zkClient.createPersistent(PropertyPathBuilder.instanceCurrentState(clusterName, nodeId), true); _zkClient.createPersistent(PropertyPathBuilder.instanceError(clusterName, nodeId), true); _zkClient.createPersistent(PropertyPathBuilder.instanceStatusUpdate(clusterName, nodeId), true); _zkClient.createPersistent(PropertyPathBuilder.instanceHistory(clusterName, nodeId), true); }
public static void createChildren(HelixZkClient client, String parentPath, List<ZNRecord> list) { client.createPersistent(parentPath, true); if (list != null) { for (ZNRecord record : list) { createChildren(client, parentPath, record); } } }