helix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From zzh...@apache.org
Subject git commit: HELIX-25: set participant Config should check existence of instance
Date Thu, 21 Mar 2013 22:44:21 GMT
Updated Branches:
  refs/heads/master 9eecbc319 -> 4d7703fcf


HELIX-25: set participant Config should check existence of instance


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

Branch: refs/heads/master
Commit: 4d7703fcfa06c17d4d0941e4a3ff51a553348354
Parents: 9eecbc3
Author: zzhang <zzhang5@uci.edu>
Authored: Thu Mar 21 15:44:14 2013 -0700
Committer: zzhang <zzhang5@uci.edu>
Committed: Thu Mar 21 15:44:14 2013 -0700

----------------------------------------------------------------------
 .../main/java/org/apache/helix/ConfigAccessor.java |   11 ++-
 .../apache/helix/manager/zk/ZKHelixManager.java    |   31 +----
 .../java/org/apache/helix/manager/zk/ZKUtil.java   |   40 +++++-
 .../java/org/apache/helix/model/ConfigScope.java   |    5 +
 .../java/org/apache/helix/TestConfigAccessor.java  |  124 ++++++++++-----
 5 files changed, 142 insertions(+), 69 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/4d7703fc/helix-core/src/main/java/org/apache/helix/ConfigAccessor.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/ConfigAccessor.java b/helix-core/src/main/java/org/apache/helix/ConfigAccessor.java
index 0734b72..aefb2be 100644
--- a/helix-core/src/main/java/org/apache/helix/ConfigAccessor.java
+++ b/helix-core/src/main/java/org/apache/helix/ConfigAccessor.java
@@ -134,9 +134,18 @@ public class ConfigAccessor
     String clusterName = scope.getClusterName();
     if (!ZKUtil.isClusterSetup(clusterName, zkClient))
     {
-      throw new HelixException("cluster " + clusterName + " is not setup yet");
+      throw new HelixException("cluster: " + clusterName + " is NOT setup.");
+    }
+
+    if (scope.getScope() == ConfigScopeProperty.PARTICIPANT) {
+       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:
" + clusterName);
+       }
     }
 
+    // use "|" to delimit resource and partition. e.g. /MyCluster/CONFIGS/PARTICIPANT/MyDB|MyDB_0
     String scopeStr = scope.getScopeStr();
     String[] splits = scopeStr.split("\\|");
 

http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/4d7703fc/helix-core/src/main/java/org/apache/helix/manager/zk/ZKHelixManager.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/manager/zk/ZKHelixManager.java b/helix-core/src/main/java/org/apache/helix/manager/zk/ZKHelixManager.java
index 86af1cf..c87f330 100644
--- a/helix-core/src/main/java/org/apache/helix/manager/zk/ZKHelixManager.java
+++ b/helix-core/src/main/java/org/apache/helix/manager/zk/ZKHelixManager.java
@@ -206,34 +206,6 @@ public class ZKHelixManager implements HelixManager
     }
   }
 
-  private boolean isInstanceSetup()
-  {
-    if (_instanceType == InstanceType.PARTICIPANT
-        || _instanceType == InstanceType.CONTROLLER_PARTICIPANT)
-    {
-      boolean isValid =
-          _zkClient.exists(PropertyPathConfig.getPath(PropertyType.CONFIGS,
-                                                      _clusterName,
-                                                      ConfigScopeProperty.PARTICIPANT.toString(),
-                                                      _instanceName))
-              && _zkClient.exists(PropertyPathConfig.getPath(PropertyType.MESSAGES,
-                                                             _clusterName,
-                                                             _instanceName))
-              && _zkClient.exists(PropertyPathConfig.getPath(PropertyType.CURRENTSTATES,
-                                                             _clusterName,
-                                                             _instanceName))
-              && _zkClient.exists(PropertyPathConfig.getPath(PropertyType.STATUSUPDATES,
-                                                             _clusterName,
-                                                             _instanceName))
-              && _zkClient.exists(PropertyPathConfig.getPath(PropertyType.ERRORS,
-                                                             _clusterName,
-                                                             _instanceName));
-
-      return isValid;
-    }
-    return true;
-  }
-  
   @Override
   public boolean removeListener(PropertyKey key, Object listener)
   {
@@ -694,7 +666,8 @@ public class ZKHelixManager implements HelixManager
       throw new HelixException("Initial cluster structure is not set up for cluster:"
           + _clusterName);
     }
-    if (!isInstanceSetup())
+
+    if (!ZKUtil.isInstanceSetup(_zkClient, _clusterName, _instanceName, _instanceType))
     {
       throw new HelixException("Initial cluster structure is not set up for instance:"
           + _instanceName + " instanceType:" + _instanceType);

http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/4d7703fc/helix-core/src/main/java/org/apache/helix/manager/zk/ZKUtil.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/manager/zk/ZKUtil.java b/helix-core/src/main/java/org/apache/helix/manager/zk/ZKUtil.java
index 85458f0..a8066b0 100644
--- a/helix-core/src/main/java/org/apache/helix/manager/zk/ZKUtil.java
+++ b/helix-core/src/main/java/org/apache/helix/manager/zk/ZKUtil.java
@@ -24,6 +24,7 @@ import java.util.Collections;
 import java.util.List;
 
 import org.I0Itec.zkclient.DataUpdater;
+import org.apache.helix.InstanceType;
 import org.apache.helix.PropertyPathConfig;
 import org.apache.helix.PropertyType;
 import org.apache.helix.ZNRecord;
@@ -74,13 +75,48 @@ public final class ZKUtil
     
     for(String path:requiredPaths){
       if(!zkClient.exists(path)){
-        isValid =false;
-        logger.error("Invalid cluster setup, missing znode path: "+path);
+        isValid = false;
+        logger.error("Invalid cluster setup, missing znode path: " + path);
       }
     }
     return isValid;
   }
 
+    public static boolean isInstanceSetup(ZkClient zkclient, String clusterName, String instanceName,
InstanceType type)
+    {
+        if (type == InstanceType.PARTICIPANT || type == InstanceType.CONTROLLER_PARTICIPANT)
+        {
+            ArrayList<String> requiredPaths = new ArrayList<String>();
+            requiredPaths.add(PropertyPathConfig.getPath(PropertyType.CONFIGS,
+                                                         clusterName,
+                                                         ConfigScopeProperty.PARTICIPANT.toString(),
+                                                         instanceName));
+            requiredPaths.add(PropertyPathConfig.getPath(PropertyType.MESSAGES,
+                                                         clusterName,
+                                                         instanceName));
+            requiredPaths.add(PropertyPathConfig.getPath(PropertyType.CURRENTSTATES,
+                                                         clusterName,
+                                                         instanceName));
+            requiredPaths.add(PropertyPathConfig.getPath(PropertyType.STATUSUPDATES,
+                                                         clusterName,
+                                                         instanceName));
+            requiredPaths.add(PropertyPathConfig.getPath(PropertyType.ERRORS,
+                                                         clusterName,
+                                                         instanceName));
+            boolean isValid =true;
+
+            for(String path:requiredPaths){
+                if(!zkclient.exists(path)){
+                    isValid =false;
+                    logger.error("Invalid instance setup, missing znode path: " + path);
+                }
+            }
+            return isValid;
+        }
+
+        return true;
+    }
+
   public static void createChildren(ZkClient client,
                                     String parentPath,
                                     List<ZNRecord> list)

http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/4d7703fc/helix-core/src/main/java/org/apache/helix/model/ConfigScope.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/model/ConfigScope.java b/helix-core/src/main/java/org/apache/helix/model/ConfigScope.java
index 87c7c34..50de79d 100644
--- a/helix-core/src/main/java/org/apache/helix/model/ConfigScope.java
+++ b/helix-core/src/main/java/org/apache/helix/model/ConfigScope.java
@@ -82,6 +82,11 @@ public class ConfigScope
 
   private final String _clusterName;
   private final ConfigScopeProperty _scope;
+
+    /**
+     * _scopeStr is like:
+     *   "/ClusterName/CONFIGS/{CLUSTER|PARTICIPANT|RESOURCE|PARTITION}/{clusterName|instanceName|resourceName}{|partitionName}"
+     */
   private final String _scopeStr;
 
   public ConfigScope(ConfigScopeBuilder configScopeBuilder)

http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/4d7703fc/helix-core/src/test/java/org/apache/helix/TestConfigAccessor.java
----------------------------------------------------------------------
diff --git a/helix-core/src/test/java/org/apache/helix/TestConfigAccessor.java b/helix-core/src/test/java/org/apache/helix/TestConfigAccessor.java
index 76c331f..6a313e0 100644
--- a/helix-core/src/test/java/org/apache/helix/TestConfigAccessor.java
+++ b/helix-core/src/test/java/org/apache/helix/TestConfigAccessor.java
@@ -19,10 +19,13 @@ package org.apache.helix;
  * under the License.
  */
 
+import java.util.Date;
 import java.util.List;
 
+import org.apache.helix.manager.zk.ZKHelixAdmin;
 import org.apache.helix.model.ConfigScope;
 import org.apache.helix.model.ConfigScope.ConfigScopeProperty;
+import org.apache.helix.model.InstanceConfig;
 import org.apache.helix.model.builder.ConfigScopeBuilder;
 import org.testng.Assert;
 import org.testng.annotations.Test;
@@ -30,95 +33,99 @@ import org.testng.annotations.Test;
 
 public class TestConfigAccessor extends ZkUnitTestBase
 {
-  final String _className = getShortClassName();
-  final String _clusterName = "CLUSTER_" + _className;
-
   @Test
-  public void testZkConfigAccessor() throws Exception
+  public void testBasic() throws Exception
   {
-    TestHelper.setupCluster(_clusterName, ZK_ADDR, 12918, "localhost", "TestDB", 1, 10, 5,
3,
+      String className = TestHelper.getTestClassName();
+      String methodName = TestHelper.getTestMethodName();
+      String clusterName = className + "_" + methodName;
+
+      System.out.println("START " + clusterName + " at "
+              + new Date(System.currentTimeMillis()));
+
+      TestHelper.setupCluster(clusterName, ZK_ADDR, 12918, "localhost", "TestDB", 1, 10,
5, 3,
         "MasterSlave", true);
 
-    ConfigAccessor appConfig = new ConfigAccessor(_gZkClient);
-    ConfigScope clusterScope = new ConfigScopeBuilder().forCluster(_clusterName).build();
+    ConfigAccessor configAccessor = new ConfigAccessor(_gZkClient);
+    ConfigScope clusterScope = new ConfigScopeBuilder().forCluster(clusterName).build();
 
     // cluster scope config
-    String clusterConfigValue = appConfig.get(clusterScope, "clusterConfigKey");
+    String clusterConfigValue = configAccessor.get(clusterScope, "clusterConfigKey");
     Assert.assertNull(clusterConfigValue);
 
-    appConfig.set(clusterScope, "clusterConfigKey", "clusterConfigValue");
-    clusterConfigValue = appConfig.get(clusterScope, "clusterConfigKey");
+    configAccessor.set(clusterScope, "clusterConfigKey", "clusterConfigValue");
+    clusterConfigValue = configAccessor.get(clusterScope, "clusterConfigKey");
     Assert.assertEquals(clusterConfigValue, "clusterConfigValue");
 
     // resource scope config
-    ConfigScope resourceScope = new ConfigScopeBuilder().forCluster(_clusterName)
+    ConfigScope resourceScope = new ConfigScopeBuilder().forCluster(clusterName)
         .forResource("testResource").build();
-    appConfig.set(resourceScope, "resourceConfigKey", "resourceConfigValue");
-    String resourceConfigValue = appConfig.get(resourceScope, "resourceConfigKey");
+    configAccessor.set(resourceScope, "resourceConfigKey", "resourceConfigValue");
+    String resourceConfigValue = configAccessor.get(resourceScope, "resourceConfigKey");
     Assert.assertEquals(resourceConfigValue, "resourceConfigValue");
 
     // partition scope config
-    ConfigScope partitionScope = new ConfigScopeBuilder().forCluster(_clusterName)
+    ConfigScope partitionScope = new ConfigScopeBuilder().forCluster(clusterName)
         .forResource("testResource").forPartition("testPartition").build();
-    appConfig.set(partitionScope, "partitionConfigKey", "partitionConfigValue");
-    String partitionConfigValue = appConfig.get(partitionScope, "partitionConfigKey");
+    configAccessor.set(partitionScope, "partitionConfigKey", "partitionConfigValue");
+    String partitionConfigValue = configAccessor.get(partitionScope, "partitionConfigKey");
     Assert.assertEquals(partitionConfigValue, "partitionConfigValue");
 
     // participant scope config
-    ConfigScope participantScope = new ConfigScopeBuilder().forCluster(_clusterName)
+    ConfigScope participantScope = new ConfigScopeBuilder().forCluster(clusterName)
         .forParticipant("localhost_12918").build();
-    appConfig.set(participantScope, "participantConfigKey", "participantConfigValue");
-    String participantConfigValue = appConfig.get(participantScope, "participantConfigKey");
+    configAccessor.set(participantScope, "participantConfigKey", "participantConfigValue");
+    String participantConfigValue = configAccessor.get(participantScope, "participantConfigKey");
     Assert.assertEquals(participantConfigValue, "participantConfigValue");
 
-    List<String> keys = appConfig.getKeys(ConfigScopeProperty.RESOURCE, _clusterName);
+    List<String> keys = configAccessor.getKeys(ConfigScopeProperty.RESOURCE, clusterName);
     Assert.assertEquals(keys.size(), 1, "should be [testResource]");
     Assert.assertEquals(keys.get(0), "testResource");
 
-    keys = appConfig.getKeys(ConfigScopeProperty.CLUSTER, _clusterName);
-    Assert.assertEquals(keys.size(), 1, "should be [" + _clusterName + "]");
-    Assert.assertEquals(keys.get(0), _clusterName);
+    keys = configAccessor.getKeys(ConfigScopeProperty.CLUSTER, clusterName);
+    Assert.assertEquals(keys.size(), 1, "should be [" + clusterName + "]");
+    Assert.assertEquals(keys.get(0), clusterName);
 
-    keys = appConfig.getKeys(ConfigScopeProperty.PARTICIPANT, _clusterName);
+    keys = configAccessor.getKeys(ConfigScopeProperty.PARTICIPANT, clusterName);
     Assert.assertEquals(keys.size(), 5, "should be [localhost_12918~22] sorted");
     Assert.assertEquals(keys.get(0), "localhost_12918");
     Assert.assertEquals(keys.get(4), "localhost_12922");
 
-    keys = appConfig.getKeys(ConfigScopeProperty.PARTITION, _clusterName, "testResource");
+    keys = configAccessor.getKeys(ConfigScopeProperty.PARTITION, clusterName, "testResource");
     Assert.assertEquals(keys.size(), 1, "should be [testPartition]");
     Assert.assertEquals(keys.get(0), "testPartition");
 
-    keys = appConfig.getKeys(ConfigScopeProperty.RESOURCE, _clusterName, "testResource");
+    keys = configAccessor.getKeys(ConfigScopeProperty.RESOURCE, clusterName, "testResource");
     Assert.assertEquals(keys.size(), 1, "should be [resourceConfigKey]");
     Assert.assertEquals(keys.get(0), "resourceConfigKey");
 
-    keys = appConfig.getKeys(ConfigScopeProperty.CLUSTER, _clusterName, _clusterName);
+    keys = configAccessor.getKeys(ConfigScopeProperty.CLUSTER, clusterName, clusterName);
     Assert.assertEquals(keys.size(), 1, "should be [clusterConfigKey]");
     Assert.assertEquals(keys.get(0), "clusterConfigKey");
 
-    keys = appConfig.getKeys(ConfigScopeProperty.PARTICIPANT, _clusterName, "localhost_12918");
+    keys = configAccessor.getKeys(ConfigScopeProperty.PARTICIPANT, clusterName, "localhost_12918");
     Assert.assertEquals(keys.size(), 4, "should be [HELIX_ENABLED, HELIX_HOST, HELIX_PORT,
participantConfigKey]");
     Assert.assertEquals(keys.get(3), "participantConfigKey");
 
-    keys = appConfig.getKeys(ConfigScopeProperty.PARTITION, _clusterName, "testResource",
"testPartition");
+    keys = configAccessor.getKeys(ConfigScopeProperty.PARTITION, clusterName, "testResource",
"testPartition");
     Assert.assertEquals(keys.size(), 1, "should be [partitionConfigKey]");
     Assert.assertEquals(keys.get(0), "partitionConfigKey");
 
     // test configAccessor.remove()
-    appConfig.remove(clusterScope, "clusterConfigKey");
-    clusterConfigValue = appConfig.get(clusterScope, "clusterConfigKey");
+    configAccessor.remove(clusterScope, "clusterConfigKey");
+    clusterConfigValue = configAccessor.get(clusterScope, "clusterConfigKey");
     Assert.assertNull(clusterConfigValue, "Should be null since it's removed");
 
-    appConfig.remove(resourceScope, "resourceConfigKey");
-    resourceConfigValue = appConfig.get(resourceScope, "resourceConfigKey");
+    configAccessor.remove(resourceScope, "resourceConfigKey");
+    resourceConfigValue = configAccessor.get(resourceScope, "resourceConfigKey");
     Assert.assertNull(resourceConfigValue, "Should be null since it's removed");
 
-    appConfig.remove(partitionScope, "partitionConfigKey");
-    partitionConfigValue = appConfig.get(partitionScope, "partitionConfigKey");
+    configAccessor.remove(partitionScope, "partitionConfigKey");
+    partitionConfigValue = configAccessor.get(partitionScope, "partitionConfigKey");
     Assert.assertNull(partitionConfigValue, "Should be null since it's removed");
     
-    appConfig.remove(participantScope, "participantConfigKey");
-    participantConfigValue = appConfig.get(partitionScope, "participantConfigKey");
+    configAccessor.remove(participantScope, "participantConfigKey");
+    participantConfigValue = configAccessor.get(partitionScope, "participantConfigKey");
     Assert.assertNull(participantConfigValue, "Should be null since it's removed");
     
     // negative tests
@@ -149,5 +156,48 @@ public class TestConfigAccessor extends ZkUnitTestBase
       // OK
     }
 
+      System.out.println("END " + clusterName + " at "
+              + new Date(System.currentTimeMillis()));
+
   }
+
+    // HELIX-25: set participant Config should check existence of instance
+    @Test
+    public void testSetNonexistentParticipantConfig() throws Exception
+    {
+        String className = TestHelper.getTestClassName();
+        String methodName = TestHelper.getTestMethodName();
+        String clusterName = className + "_" + methodName;
+
+        System.out.println("START " + clusterName + " at "
+                + new Date(System.currentTimeMillis()));
+
+        ZKHelixAdmin admin = new ZKHelixAdmin(_gZkClient);
+        admin.addCluster(clusterName, true);
+        ConfigAccessor configAccessor = new ConfigAccessor(_gZkClient);
+        ConfigScope participantScope = new ConfigScopeBuilder().forCluster(clusterName)
+                .forParticipant("localhost_12918").build();
+
+        try {
+            configAccessor.set(participantScope, "participantConfigKey", "participantConfigValue");
+            Assert.fail("Except fail to set participant-config because participant: localhost_12918
is not added to cluster yet");
+        } catch (HelixException e) {
+            // OK
+        }
+        admin.addInstance(clusterName, new InstanceConfig("localhost_12918"));
+
+        try {
+            configAccessor.set(participantScope, "participantConfigKey", "participantConfigValue");
+        } catch (Exception e) {
+            Assert.fail("Except succeed to set participant-config because participant: localhost_12918
has been added to cluster");
+        }
+
+        String participantConfigValue = configAccessor.get(participantScope, "participantConfigKey");
+        Assert.assertEquals(participantConfigValue, "participantConfigValue");
+
+        System.out.println("END " + clusterName + " at "
+                + new Date(System.currentTimeMillis()));
+
+
+    }
 }


Mime
View raw message