helix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From zzh...@apache.org
Subject git commit: HELIX-50: Ensure num replicas and preference list size in idealstate matches
Date Wed, 20 Mar 2013 01:09:37 GMT
Updated Branches:
  refs/heads/master f453ad3e5 -> 62239a519


HELIX-50: Ensure num replicas and preference list size in idealstate matches


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

Branch: refs/heads/master
Commit: 62239a519ed0990c65e3724d9748dcaf0cc945b5
Parents: f453ad3
Author: zzhang <zzhang5@uci.edu>
Authored: Tue Mar 19 18:09:31 2013 -0700
Committer: zzhang <zzhang5@uci.edu>
Committed: Tue Mar 19 18:09:31 2013 -0700

----------------------------------------------------------------------
 .../java/org/apache/helix/model/IdealState.java    |   22 +++++++++--
 .../org/apache/helix/model/TestIdealState.java     |   28 +++++++++++---
 2 files changed, 40 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/62239a51/helix-core/src/main/java/org/apache/helix/model/IdealState.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/model/IdealState.java b/helix-core/src/main/java/org/apache/helix/model/IdealState.java
index 871545f..9163cb9 100644
--- a/helix-core/src/main/java/org/apache/helix/model/IdealState.java
+++ b/helix-core/src/main/java/org/apache/helix/model/IdealState.java
@@ -331,12 +331,26 @@ public class IdealState extends HelixProperty
       return false;
     }
 
-    if (getIdealStateMode() == IdealStateModeProperty.AUTO
-        && getReplicas() == null)
+    if (getIdealStateMode() == IdealStateModeProperty.AUTO)
     {
-      logger.error("idealStates:" + _record + " does not have replica.");
-      return false;
+        String replicaStr = getReplicas();
+        if (replicaStr == null) {
+            logger.error("invalid ideal-state. missing replicas in auto mode. record was:
" + _record);
+            return false;
+        }
+
+        int replica = Integer.parseInt(replicaStr);
+        Set<String> partitionSet = getPartitionSet();
+        for (String partition : partitionSet) {
+            List<String> preferenceList = getPreferenceList(partition);
+            if (preferenceList == null || preferenceList.size() != replica) {
+                logger.error("invalid ideal-state. preference-list size not equals to replicas
in auto mode. record was: "
+                        + _record);
+                return false;
+            }
+        }
     }
+
     return true;
   }
 

http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/62239a51/helix-core/src/test/java/org/apache/helix/model/TestIdealState.java
----------------------------------------------------------------------
diff --git a/helix-core/src/test/java/org/apache/helix/model/TestIdealState.java b/helix-core/src/test/java/org/apache/helix/model/TestIdealState.java
index dca0aec..7b9688d 100644
--- a/helix-core/src/test/java/org/apache/helix/model/TestIdealState.java
+++ b/helix-core/src/test/java/org/apache/helix/model/TestIdealState.java
@@ -19,12 +19,7 @@ package org.apache.helix.model;
  * under the License.
  */
 
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Set;
+import java.util.*;
 
 import org.apache.helix.TestHelper;
 import org.apache.helix.model.IdealState;
@@ -77,4 +72,25 @@ public class TestIdealState
     System.out.println("END " + testName + " at "
         + new Date(System.currentTimeMillis()));
   }
+
+  @Test
+  public void testReplicas() {
+      IdealState idealState = new IdealState("test-db");
+      idealState.setIdealStateMode(IdealStateModeProperty.AUTO.toString());
+      idealState.setNumPartitions(4);
+      idealState.setStateModelDefRef("MasterSlave");
+
+      idealState.setReplicas("" + 2);
+
+      List<String> preferenceList = new ArrayList<String>();
+      preferenceList.add("node_0");
+      idealState.getRecord().setListField("test-db_0", preferenceList);
+      Assert.assertFalse(idealState.isValid(), "should fail since replicas not equals to
preference-list size");
+
+      preferenceList.add("node_1");
+      idealState.getRecord().setListField("test-db_0", preferenceList);
+      Assert.assertTrue(idealState.isValid(), "should pass since replicas equals to preference-list
size");
+
+
+  }
 }


Mime
View raw message