Repository: helix
Updated Branches:
  refs/heads/helix-0.6.x e94a9f5f9 -> 8a279a366


[HELIX-591] Provide getResourcesWithTag in HelixAdmin to retrieve all resources 
with a group tag.


Project: http://git-wip-us.apache.org/repos/asf/helix/repo
Commit: http://git-wip-us.apache.org/repos/asf/helix/commit/8a279a36
Tree: http://git-wip-us.apache.org/repos/asf/helix/tree/8a279a36
Diff: http://git-wip-us.apache.org/repos/asf/helix/diff/8a279a36

Branch: refs/heads/helix-0.6.x
Commit: 8a279a366d4e6c43366a1c5867a02f26768e5627
Parents: e94a9f5
Author: Lei Xia <[email protected]>
Authored: Mon Apr 27 13:36:37 2015 -0700
Committer: Lei Xia <[email protected]>
Committed: Mon Apr 27 13:36:37 2015 -0700

----------------------------------------------------------------------
 .../main/java/org/apache/helix/HelixAdmin.java  |  7 +++
 .../apache/helix/manager/zk/ZKHelixAdmin.java   | 18 +++++++
 .../org/apache/helix/tools/ClusterSetup.java    | 25 +++++++--
 .../helix/manager/zk/TestZkHelixAdmin.java      | 54 +++++++++++++++++++-
 4 files changed, 98 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/helix/blob/8a279a36/helix-core/src/main/java/org/apache/helix/HelixAdmin.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/HelixAdmin.java 
b/helix-core/src/main/java/org/apache/helix/HelixAdmin.java
index 7bb4c3a..47ff113 100644
--- a/helix-core/src/main/java/org/apache/helix/HelixAdmin.java
+++ b/helix-core/src/main/java/org/apache/helix/HelixAdmin.java
@@ -58,6 +58,13 @@ public interface HelixAdmin {
   List<String> getResourcesInCluster(String clusterName);
 
   /**
+  * Get a list of resources in a cluster with a tag
+  * @param clusterName
+  * @param tag
+  */
+  List<String> getResourcesInClusterWithTag(String clusterName, String tag);
+
+  /**
    * Add a cluster
    * @param clusterName
    * @return true if successfully created, or if cluster already exists

http://git-wip-us.apache.org/repos/asf/helix/blob/8a279a36/helix-core/src/main/java/org/apache/helix/manager/zk/ZKHelixAdmin.java
----------------------------------------------------------------------
diff --git 
a/helix-core/src/main/java/org/apache/helix/manager/zk/ZKHelixAdmin.java 
b/helix-core/src/main/java/org/apache/helix/manager/zk/ZKHelixAdmin.java
index 89fdab7..be0d17a 100644
--- a/helix-core/src/main/java/org/apache/helix/manager/zk/ZKHelixAdmin.java
+++ b/helix-core/src/main/java/org/apache/helix/manager/zk/ZKHelixAdmin.java
@@ -669,6 +669,24 @@ public class ZKHelixAdmin implements HelixAdmin {
   }
 
   @Override
+  public List<String> getResourcesInClusterWithTag(String clusterName, String 
tag) {
+    List<String> resourcesWithTag = new ArrayList<String>();
+
+    HelixDataAccessor accessor =
+        new ZKHelixDataAccessor(clusterName, new 
ZkBaseDataAccessor<ZNRecord>(_zkClient));
+    Builder keyBuilder = accessor.keyBuilder();
+
+    for (String resourceName : getResourcesInCluster(clusterName)) {
+      IdealState is = 
accessor.getProperty(keyBuilder.idealStates(resourceName));
+      if (is != null && is.getInstanceGroupTag() != null && 
is.getInstanceGroupTag().equals(tag)) {
+        resourcesWithTag.add(resourceName);
+      }
+    }
+
+    return resourcesWithTag;
+  }
+
+  @Override
   public IdealState getResourceIdealState(String clusterName, String 
resourceName) {
     HelixDataAccessor accessor =
         new ZKHelixDataAccessor(clusterName, new 
ZkBaseDataAccessor<ZNRecord>(_zkClient));

http://git-wip-us.apache.org/repos/asf/helix/blob/8a279a36/helix-core/src/main/java/org/apache/helix/tools/ClusterSetup.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/tools/ClusterSetup.java 
b/helix-core/src/main/java/org/apache/helix/tools/ClusterSetup.java
index 4234ce1..3a06a2c 100644
--- a/helix-core/src/main/java/org/apache/helix/tools/ClusterSetup.java
+++ b/helix-core/src/main/java/org/apache/helix/tools/ClusterSetup.java
@@ -88,6 +88,7 @@ public class ClusterSetup {
   public static final String expandCluster = "expandCluster";
   public static final String expandResource = "expandResource";
   public static final String mode = "mode";
+  public static final String tag = "tag";
   public static final String instanceGroupTag = "instanceGroupTag";
   public static final String bucketSize = "bucketSize";
   public static final String resourceKeyPrefix = "key";
@@ -602,7 +603,7 @@ public class ClusterSetup {
             .withDescription("List resources hosted in a cluster").create();
     listResourceOption.setArgs(1);
     listResourceOption.setRequired(false);
-    listResourceOption.setArgName("clusterName");
+    listResourceOption.setArgName("clusterName <-tag TagValue>");
 
     Option listInstancesOption =
         OptionBuilder.withLongOpt(listInstances).withDescription("List 
Instances in a cluster")
@@ -666,6 +667,13 @@ public class ClusterSetup {
     resourceModeOption.setRequired(false);
     resourceModeOption.setArgName("IdealState mode");
 
+    Option resourceTagOption =
+        OptionBuilder.withLongOpt(tag)
+            .withDescription("Specify resource tag, used with listResources 
command").create();
+    resourceTagOption.setArgs(1);
+    resourceTagOption.setRequired(false);
+    resourceTagOption.setArgName("tag");
+
     Option resourceBucketSizeOption =
         OptionBuilder.withLongOpt(bucketSize)
             .withDescription("Specify size of a bucket, used with 
addResourceGroup command")
@@ -907,6 +915,7 @@ public class ClusterSetup {
     group.addOption(rebalanceOption);
     group.addOption(addResourceOption);
     group.addOption(resourceModeOption);
+    group.addOption(resourceTagOption);
     group.addOption(resourceBucketSizeOption);
     group.addOption(maxPartitionsPerNodeOption);
     group.addOption(expandResourceOption);
@@ -1096,10 +1105,18 @@ public class ClusterSetup {
 
     if (cmd.hasOption(listResources)) {
       String clusterName = cmd.getOptionValue(listResources);
-      List<String> resourceNames =
-          
setupTool.getClusterManagementTool().getResourcesInCluster(clusterName);
+      List<String> resourceNames = null;
+      if (cmd.hasOption(tag)) {
+        String tagValue = cmd.getOptionValues(tag)[0];
+        resourceNames = setupTool.getClusterManagementTool()
+            .getResourcesInClusterWithTag(clusterName, tagValue);
+        System.out.println(
+            "Existing resources in cluster " + clusterName + " with tag " + 
tagValue + " :");
+      } else {
+        resourceNames = 
setupTool.getClusterManagementTool().getResourcesInCluster(clusterName);
+        System.out.println("Existing resources in cluster " + clusterName + 
":");
+      }
 
-      System.out.println("Existing resources in cluster " + clusterName + ":");
       for (String resourceName : resourceNames) {
         System.out.println(resourceName);
       }

http://git-wip-us.apache.org/repos/asf/helix/blob/8a279a36/helix-core/src/test/java/org/apache/helix/manager/zk/TestZkHelixAdmin.java
----------------------------------------------------------------------
diff --git 
a/helix-core/src/test/java/org/apache/helix/manager/zk/TestZkHelixAdmin.java 
b/helix-core/src/test/java/org/apache/helix/manager/zk/TestZkHelixAdmin.java
index cfdf1a3..bbaca31 100644
--- a/helix-core/src/test/java/org/apache/helix/manager/zk/TestZkHelixAdmin.java
+++ b/helix-core/src/test/java/org/apache/helix/manager/zk/TestZkHelixAdmin.java
@@ -30,14 +30,12 @@ import org.apache.helix.model.ClusterConstraints;
 import org.apache.helix.model.ClusterConstraints.ConstraintAttribute;
 import org.apache.helix.model.ClusterConstraints.ConstraintType;
 import org.apache.helix.model.HelixConfigScope.ConfigScopeProperty;
-import org.apache.helix.model.ConfigScope;
 import org.apache.helix.model.ConstraintItem;
 import org.apache.helix.model.ExternalView;
 import org.apache.helix.model.HelixConfigScope;
 import org.apache.helix.model.IdealState;
 import org.apache.helix.model.InstanceConfig;
 import org.apache.helix.model.StateModelDefinition;
-import org.apache.helix.model.builder.ConfigScopeBuilder;
 import org.apache.helix.model.builder.ConstraintItemBuilder;
 import org.apache.helix.model.builder.HelixConfigScopeBuilder;
 import org.apache.helix.tools.StateModelConfigGenerator;
@@ -314,4 +312,56 @@ public class TestZkHelixAdmin extends ZkUnitTestBase {
     Assert.assertTrue(idealState.isEnabled());
     System.out.println("END " + clusterName + " at " + new 
Date(System.currentTimeMillis()));
   }
+
+  @Test
+  public void testGetResourcesWithTag() {
+    String TEST_TAG = "TestTAG";
+
+    final String clusterName = getShortClassName();
+    String rootPath = "/" + clusterName;
+    if (_gZkClient.exists(rootPath)) {
+      _gZkClient.deleteRecursive(rootPath);
+    }
+
+    HelixAdmin tool = new ZKHelixAdmin(_gZkClient);
+    tool.addCluster(clusterName, true);
+    Assert.assertTrue(ZKUtil.isClusterSetup(clusterName, _gZkClient));
+
+    tool.addStateModelDef(clusterName, "OnlineOffline",
+        new 
StateModelDefinition(StateModelConfigGenerator.generateConfigForOnlineOffline()));
+
+    for (int i = 0; i < 4; i++) {
+      String instanceName = "host" + i + "_9999";
+      InstanceConfig config = new InstanceConfig(instanceName);
+      config.setHostName("host" + i);
+      config.setPort("9999");
+      // set tag to two instances
+      if (i < 2) {
+        config.addTag(TEST_TAG);
+      }
+      tool.addInstance(clusterName, config);
+      tool.enableInstance(clusterName, instanceName, true);
+      String path = PropertyPathConfig.getPath(PropertyType.INSTANCES, 
clusterName, instanceName);
+      AssertJUnit.assertTrue(_gZkClient.exists(path));
+    }
+
+    for (int i = 0; i < 4; i++) {
+      String resourceName = "database_" + i;
+      IdealState is = new IdealState(resourceName);
+      is.setStateModelDefRef("OnlineOffline");
+      is.setNumPartitions(2);
+      is.setRebalanceMode(IdealState.RebalanceMode.FULL_AUTO);
+      is.setReplicas("1");
+      is.enable(true);
+      if (i < 2) {
+        is.setInstanceGroupTag(TEST_TAG);
+      }
+      tool.addResource(clusterName, resourceName, is);
+    }
+
+    List<String> allResources = tool.getResourcesInCluster(clusterName);
+    List<String> resourcesWithTag = 
tool.getResourcesInClusterWithTag(clusterName, TEST_TAG);
+    AssertJUnit.assertEquals(allResources.size(), 4);
+    AssertJUnit.assertEquals(resourcesWithTag.size(), 2);
+  }
 }

Reply via email to