org.apache.helix.manager.zk.ZKUtil类的使用及代码示例

x33g5p2x  于2022-02-05 转载在 其他  
字(13.2k)|赞(0)|评价(0)|浏览(235)

本文整理了Java中org.apache.helix.manager.zk.ZKUtil类的一些代码示例,展示了ZKUtil类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ZKUtil类的具体详情如下:
包路径:org.apache.helix.manager.zk.ZKUtil
类名称:ZKUtil

ZKUtil介绍

暂无

代码示例

代码示例来源:origin: apache/helix

  1. private void updateClusterConfig(String clusterName, ClusterConfig clusterConfig, boolean overwrite) {
  2. if (!ZKUtil.isClusterSetup(clusterName, zkClient)) {
  3. throw new HelixException("fail to update config. cluster: " + clusterName + " is NOT setup.");
  4. }
  5. HelixConfigScope scope =
  6. new HelixConfigScopeBuilder(ConfigScopeProperty.CLUSTER).forCluster(clusterName).build();
  7. String zkPath = scope.getZkPath();
  8. if (overwrite) {
  9. ZKUtil.createOrReplace(zkClient, zkPath, clusterConfig.getRecord(), true);
  10. } else {
  11. ZKUtil.createOrUpdate(zkClient, zkPath, clusterConfig.getRecord(), true, true);
  12. }
  13. }

代码示例来源:origin: apache/helix

  1. @Test()
  2. public void testChildrenOperations() {
  3. List<ZNRecord> list = new ArrayList<ZNRecord>();
  4. list.add(new ZNRecord("id1"));
  5. list.add(new ZNRecord("id2"));
  6. String path = PropertyPathBuilder.instanceConfig(clusterName);
  7. ZKUtil.createChildren(_gZkClient, path, list);
  8. list = ZKUtil.getChildren(_gZkClient, path);
  9. AssertJUnit.assertEquals(2, list.size());
  10. ZKUtil.dropChildren(_gZkClient, path, list);
  11. ZKUtil.dropChildren(_gZkClient, path, new ZNRecord("id1"));
  12. list = ZKUtil.getChildren(_gZkClient, path);
  13. AssertJUnit.assertEquals(0, list.size());
  14. ZKUtil.dropChildren(_gZkClient, path, (List<ZNRecord>) null);
  15. }

代码示例来源:origin: apache/helix

  1. /**
  2. * Remove multiple configs
  3. *
  4. * @param scope scope specification of the entity set to query (e.g. cluster, resource,
  5. * participant, etc.)
  6. * @param recordToRemove the ZNRecord that holds the entries that needs to be removed
  7. */
  8. public void remove(HelixConfigScope scope, ZNRecord recordToRemove) {
  9. if (scope == null || scope.getType() == null || !scope.isFullKey()) {
  10. LOG.error("fail to remove. invalid scope: " + scope);
  11. return;
  12. }
  13. String clusterName = scope.getClusterName();
  14. if (!ZKUtil.isClusterSetup(clusterName, zkClient)) {
  15. throw new HelixException("fail to remove. cluster " + clusterName + " is not setup yet");
  16. }
  17. String zkPath = scope.getZkPath();
  18. ZKUtil.subtract(zkClient, zkPath, recordToRemove);
  19. }

代码示例来源:origin: apache/helix

  1. /**
  2. * Partially updates the fields appearing in the given IdealState (input).
  3. * @param clusterName
  4. * @param resourceName
  5. * @param idealState
  6. */
  7. @Override
  8. public void updateIdealState(String clusterName, String resourceName, IdealState idealState) {
  9. if (!ZKUtil.isClusterSetup(clusterName, _zkClient)) {
  10. throw new HelixException(
  11. "updateIdealState failed. Cluster: " + clusterName + " is NOT setup properly.");
  12. }
  13. String zkPath = PropertyPathBuilder.idealState(clusterName, resourceName);
  14. if (!_zkClient.exists(zkPath)) {
  15. throw new HelixException(String.format(
  16. "updateIdealState failed. The IdealState for the given resource does not already exist. Resource name: %s",
  17. resourceName));
  18. }
  19. // Update by way of merge
  20. ZKUtil.createOrUpdate(_zkClient, zkPath, idealState.getRecord(), true, true);
  21. }

代码示例来源:origin: org.apache.helix/helix-core

  1. @Override
  2. public List<String> getClusters() {
  3. List<String> zkToplevelPathes = _zkClient.getChildren("/");
  4. List<String> result = new ArrayList<String>();
  5. for (String pathName : zkToplevelPathes) {
  6. if (ZKUtil.isClusterSetup(pathName, _zkClient)) {
  7. result.add(pathName);
  8. }
  9. }
  10. return result;
  11. }

代码示例来源:origin: org.apache.helix/helix-core

  1. if (!ZKUtil.isClusterSetup(clusterName, zkClient)) {
  2. throw new HelixException("cluster: " + clusterName + " is NOT setup.");
  3. String scopeStr = scope.getScopeStr();
  4. String instanceName = scopeStr.substring(scopeStr.lastIndexOf('/') + 1);
  5. if (!ZKUtil.isInstanceSetup(zkClient, scope.getClusterName(), instanceName,
  6. InstanceType.PARTICIPANT)) {
  7. throw new HelixException("instance: " + instanceName + " is NOT setup in cluster: "
  8. ZKUtil.createOrMerge(zkClient, splits[0], update, true, true);

代码示例来源:origin: org.apache.helix/helix-core

  1. @Override
  2. public void addInstanceTag(String clusterName, String instanceName, String tag) {
  3. logger
  4. .info("Add instance tag {} for instance {} in cluster {}.", tag, instanceName, clusterName);
  5. if (!ZKUtil.isClusterSetup(clusterName, _zkClient)) {
  6. throw new HelixException("cluster " + clusterName + " is not setup yet");
  7. }
  8. if (!ZKUtil.isInstanceSetup(_zkClient, clusterName, instanceName, InstanceType.PARTICIPANT)) {
  9. throw new HelixException("cluster " + clusterName + " instance " + instanceName + " is not setup yet");
  10. }
  11. HelixDataAccessor accessor =
  12. new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_zkClient));
  13. Builder keyBuilder = accessor.keyBuilder();
  14. InstanceConfig config = accessor.getProperty(keyBuilder.instanceConfig(instanceName));
  15. config.addTag(tag);
  16. accessor.setProperty(keyBuilder.instanceConfig(instanceName), config);
  17. }

代码示例来源:origin: org.apache.helix/helix-core

  1. @Override
  2. public void addInstance(String clusterName, InstanceConfig instanceConfig) {
  3. logger.info("Add instance {} to cluster {}.", instanceConfig.getInstanceName(), clusterName);
  4. if (!ZKUtil.isClusterSetup(clusterName, _zkClient)) {
  5. throw new HelixException("cluster " + clusterName + " is not setup yet");
  6. }
  7. String instanceConfigsPath = PropertyPathBuilder.instanceConfig(clusterName);
  8. String nodeId = instanceConfig.getId();
  9. String instanceConfigPath = instanceConfigsPath + "/" + nodeId;
  10. if (_zkClient.exists(instanceConfigPath)) {
  11. throw new HelixException("Node " + nodeId + " already exists in cluster " + clusterName);
  12. }
  13. ZKUtil.createChildren(_zkClient, instanceConfigsPath, instanceConfig.getRecord());
  14. _zkClient.createPersistent(PropertyPathBuilder.instanceMessage(clusterName, nodeId), true);
  15. _zkClient.createPersistent(PropertyPathBuilder.instanceCurrentState(clusterName, nodeId), true);
  16. _zkClient.createPersistent(PropertyPathBuilder.instanceError(clusterName, nodeId), true);
  17. _zkClient.createPersistent(PropertyPathBuilder.instanceStatusUpdate(clusterName, nodeId), true);
  18. _zkClient.createPersistent(PropertyPathBuilder.instanceHistory(clusterName, nodeId), true);
  19. }

代码示例来源:origin: org.apache.helix/helix-core

  1. public static void createChildren(ZkClient client, String parentPath, List<ZNRecord> list) {
  2. client.createPersistent(parentPath, true);
  3. if (list != null) {
  4. for (ZNRecord record : list) {
  5. createChildren(client, parentPath, record);
  6. }
  7. }
  8. }

代码示例来源:origin: apache/helix

  1. if (!ZKUtil.isInstanceSetup(_zkclient, _clusterName, _instanceName, _instanceType)) {
  2. if (!autoJoin) {
  3. throw new HelixException("Initial cluster structure is not set up for instance: "

代码示例来源:origin: apache/helix

  1. String path = PropertyPathBuilder.instanceConfig(clusterName, "id7");
  2. ZNRecord record = new ZNRecord("id7");
  3. ZKUtil.createOrMerge(_gZkClient, path, record, true, true);
  4. record = _gZkClient.readData(path);
  5. AssertJUnit.assertEquals("id7", record.getId());
  6. List<String> list = Arrays.asList("value1", "value2");
  7. record.setListField("list", list);
  8. ZKUtil.createOrUpdate(_gZkClient, path, record, true, true);
  9. record = _gZkClient.readData(path);
  10. AssertJUnit.assertEquals(list, record.getListField("list"));
  11. List<String> list2 = Arrays.asList("value3", "value4");
  12. record.setListField("list", list2);
  13. ZKUtil.createOrUpdate(_gZkClient, path, record, true, true);
  14. record = _gZkClient.readData(path);
  15. AssertJUnit.assertEquals(list2, record.getListField("list"));
  16. ZKUtil.createOrUpdate(_gZkClient, path, record, true, true);
  17. record = _gZkClient.readData(path);
  18. AssertJUnit.assertEquals(map, record.getMapField("map"));
  19. Map<String, String> map2 = new HashMap<String, String>() {{put("k2", "v2");}};
  20. record.setMapField("map", map2);
  21. ZKUtil.createOrUpdate(_gZkClient, path, record, true, true);
  22. record = _gZkClient.readData(path);
  23. AssertJUnit.assertEquals(new HashMap<String, String>() {{

代码示例来源:origin: org.apache.helix/helix-core

  1. public static void dropChildren(ZkClient client, String parentPath, List<ZNRecord> list) {
  2. // TODO: check if parentPath exists
  3. if (list != null) {
  4. for (ZNRecord record : list) {
  5. dropChildren(client, parentPath, record);
  6. }
  7. }
  8. }

代码示例来源:origin: apache/helix

  1. /**
  2. * Selectively removes fields appearing in the given IdealState (input) from the IdealState in ZK.
  3. * @param clusterName
  4. * @param resourceName
  5. * @param idealState
  6. */
  7. @Override
  8. public void removeFromIdealState(String clusterName, String resourceName, IdealState idealState) {
  9. String zkPath = PropertyPathBuilder.idealState(clusterName, resourceName);
  10. ZKUtil.subtract(_zkClient, zkPath, idealState.getRecord());
  11. }

代码示例来源:origin: apache/helix

  1. @Test()
  2. public void testCreateOrReplace() {
  3. String path = PropertyPathBuilder.instanceConfig(clusterName, "id8");
  4. ZNRecord record = new ZNRecord("id8");
  5. ZKUtil.createOrReplace(_gZkClient, path, record, true);
  6. record = _gZkClient.readData(path);
  7. AssertJUnit.assertEquals("id8", record.getId());
  8. record = new ZNRecord("id9");
  9. ZKUtil.createOrReplace(_gZkClient, path, record, true);
  10. record = _gZkClient.readData(path);
  11. AssertJUnit.assertEquals("id9", record.getId());
  12. }

代码示例来源:origin: apache/helix

  1. @Test()
  2. public void testCreateOrMerge() {
  3. String path = PropertyPathBuilder.instanceConfig(clusterName, "id7");
  4. ZNRecord record = new ZNRecord("id7");
  5. List<String> list = Arrays.asList("value1");
  6. record.setListField("list", list);
  7. ZKUtil.createOrMerge(_gZkClient, path, record, true, true);
  8. record = _gZkClient.readData(path);
  9. AssertJUnit.assertEquals(list, record.getListField("list"));
  10. record = new ZNRecord("id7");
  11. List<String> list2 = Arrays.asList("value2");
  12. record.setListField("list", list2);
  13. ZKUtil.createOrMerge(_gZkClient, path, record, true, true);
  14. record = _gZkClient.readData(path);
  15. AssertJUnit.assertEquals(Arrays.asList("value1", "value2"), record.getListField("list"));
  16. Map<String, String> map = new HashMap<String, String>() {{put("k1", "v1");}};
  17. record.setMapField("map", map);
  18. ZKUtil.createOrMerge(_gZkClient, path, record, true, true);
  19. record = _gZkClient.readData(path);
  20. AssertJUnit.assertEquals(map, record.getMapField("map"));
  21. record = new ZNRecord("id7");
  22. Map<String, String> map2 = new HashMap<String, String>() {{put("k2", "v2");}};
  23. record.setMapField("map", map2);
  24. ZKUtil.createOrMerge(_gZkClient, path, record, true, true);
  25. record = _gZkClient.readData(path);
  26. AssertJUnit.assertEquals(new HashMap<String, String>() {{
  27. put("k1", "v1");
  28. put("k2", "v2");
  29. }}, record.getMapField("map"));
  30. }

代码示例来源:origin: apache/helix

  1. private boolean isClusterExist(String cluster) {
  2. HelixZkClient zkClient = getHelixZkClient();
  3. if (ZKUtil.isClusterSetup(cluster, zkClient)) {
  4. return true;
  5. }
  6. return false;
  7. }
  8. }

代码示例来源:origin: apache/helix

  1. if (!ZKUtil.isClusterSetup(clusterName, zkClient)) {
  2. throw new HelixException("cluster: " + clusterName + " is NOT setup.");
  3. String scopeStr = scope.getScopeStr();
  4. String instanceName = scopeStr.substring(scopeStr.lastIndexOf('/') + 1);
  5. if (!ZKUtil.isInstanceSetup(zkClient, scope.getClusterName(), instanceName,
  6. InstanceType.PARTICIPANT)) {
  7. throw new HelixException("instance: " + instanceName + " is NOT setup in cluster: "
  8. ZKUtil.createOrMerge(zkClient, splits[0], update, true, true);

代码示例来源:origin: org.apache.helix/helix-core

  1. @Override
  2. public void removeInstanceTag(String clusterName, String instanceName, String tag) {
  3. logger.info("Remove instance tag {} for instance {} in cluster {}.", tag, instanceName,
  4. clusterName);
  5. if (!ZKUtil.isClusterSetup(clusterName, _zkClient)) {
  6. throw new HelixException("cluster " + clusterName + " is not setup yet");
  7. }
  8. if (!ZKUtil.isInstanceSetup(_zkClient, clusterName, instanceName, InstanceType.PARTICIPANT)) {
  9. throw new HelixException(
  10. "cluster " + clusterName + " instance " + instanceName + " is not setup yet");
  11. }
  12. ZKHelixDataAccessor accessor =
  13. new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_zkClient));
  14. Builder keyBuilder = accessor.keyBuilder();
  15. InstanceConfig config = accessor.getProperty(keyBuilder.instanceConfig(instanceName));
  16. config.removeTag(tag);
  17. accessor.setProperty(keyBuilder.instanceConfig(instanceName), config);
  18. }

代码示例来源:origin: apache/helix

  1. @Override
  2. public void addInstance(String clusterName, InstanceConfig instanceConfig) {
  3. logger.info("Add instance {} to cluster {}.", instanceConfig.getInstanceName(), clusterName);
  4. if (!ZKUtil.isClusterSetup(clusterName, _zkClient)) {
  5. throw new HelixException("cluster " + clusterName + " is not setup yet");
  6. }
  7. String instanceConfigsPath = PropertyPathBuilder.instanceConfig(clusterName);
  8. String nodeId = instanceConfig.getId();
  9. String instanceConfigPath = instanceConfigsPath + "/" + nodeId;
  10. if (_zkClient.exists(instanceConfigPath)) {
  11. throw new HelixException("Node " + nodeId + " already exists in cluster " + clusterName);
  12. }
  13. ZKUtil.createChildren(_zkClient, instanceConfigsPath, instanceConfig.getRecord());
  14. _zkClient.createPersistent(PropertyPathBuilder.instanceMessage(clusterName, nodeId), true);
  15. _zkClient.createPersistent(PropertyPathBuilder.instanceCurrentState(clusterName, nodeId), true);
  16. _zkClient.createPersistent(PropertyPathBuilder.instanceError(clusterName, nodeId), true);
  17. _zkClient.createPersistent(PropertyPathBuilder.instanceStatusUpdate(clusterName, nodeId), true);
  18. _zkClient.createPersistent(PropertyPathBuilder.instanceHistory(clusterName, nodeId), true);
  19. }

代码示例来源:origin: apache/helix

  1. public static void createChildren(HelixZkClient client, String parentPath, List<ZNRecord> list) {
  2. client.createPersistent(parentPath, true);
  3. if (list != null) {
  4. for (ZNRecord record : list) {
  5. createChildren(client, parentPath, record);
  6. }
  7. }
  8. }

相关文章