helix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From zzh...@apache.org
Subject [1/2] [HELIX-100] improve helix config api, namespaced user configs
Date Thu, 12 Sep 2013 21:13:54 GMT
Updated Branches:
  refs/heads/helix-logical-model 7de6a7fe8 -> d8ef5a2d7


http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/d8ef5a2d/helix-core/src/test/java/org/apache/helix/controller/strategy/TestAutoRebalanceStrategy.java
----------------------------------------------------------------------
diff --git a/helix-core/src/test/java/org/apache/helix/controller/strategy/TestAutoRebalanceStrategy.java
b/helix-core/src/test/java/org/apache/helix/controller/strategy/TestAutoRebalanceStrategy.java
index 9fb7ba9..bd6cd13 100644
--- a/helix-core/src/test/java/org/apache/helix/controller/strategy/TestAutoRebalanceStrategy.java
+++ b/helix-core/src/test/java/org/apache/helix/controller/strategy/TestAutoRebalanceStrategy.java
@@ -37,6 +37,8 @@ import org.apache.helix.HelixDefinedState;
 import org.apache.helix.Mocks.MockAccessor;
 import org.apache.helix.PropertyKey.Builder;
 import org.apache.helix.ZNRecord;
+import org.apache.helix.api.Id;
+import org.apache.helix.api.State;
 import org.apache.helix.controller.rebalancer.util.ConstraintBasedAssignment;
 import org.apache.helix.controller.stages.ClusterDataCache;
 import org.apache.helix.controller.strategy.AutoRebalanceStrategy.ReplicaPlacementScheme;
@@ -122,13 +124,14 @@ public class TestAutoRebalanceStrategy {
    */
   private StateModelDefinition getIncompleteStateModelDef(String modelName, String initialState,
       LinkedHashMap<String, Integer> states) {
-    StateModelDefinition.Builder builder = new StateModelDefinition.Builder(modelName);
-    builder.initialState(initialState);
-    int i = states.size();
+    StateModelDefinition.Builder builder =
+        new StateModelDefinition.Builder(Id.stateModelDef(modelName));
+    builder.initialState(State.from(initialState));
+    int i = 0;
     for (String state : states.keySet()) {
-      builder.addState(state, i);
-      builder.upperBound(state, states.get(state));
-      i--;
+      builder.addState(State.from(state), i);
+      builder.upperBound(State.from(state), states.get(state));
+      i++;
     }
     return builder.build();
   }

http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/d8ef5a2d/helix-core/src/test/java/org/apache/helix/controller/strategy/TestNewAutoRebalanceStrategy.java
----------------------------------------------------------------------
diff --git a/helix-core/src/test/java/org/apache/helix/controller/strategy/TestNewAutoRebalanceStrategy.java
b/helix-core/src/test/java/org/apache/helix/controller/strategy/TestNewAutoRebalanceStrategy.java
index d220db8..6db3e6c 100644
--- a/helix-core/src/test/java/org/apache/helix/controller/strategy/TestNewAutoRebalanceStrategy.java
+++ b/helix-core/src/test/java/org/apache/helix/controller/strategy/TestNewAutoRebalanceStrategy.java
@@ -42,6 +42,7 @@ import org.apache.helix.api.ParticipantId;
 import org.apache.helix.api.PartitionId;
 import org.apache.helix.api.ResourceId;
 import org.apache.helix.api.State;
+import org.apache.helix.api.UserConfig;
 import org.apache.helix.controller.rebalancer.util.NewConstraintBasedAssignment;
 import org.apache.helix.controller.strategy.AutoRebalanceStrategy.ReplicaPlacementScheme;
 import org.apache.helix.model.CurrentState;
@@ -131,13 +132,14 @@ public class TestNewAutoRebalanceStrategy {
    */
   private StateModelDefinition getIncompleteStateModelDef(String modelName, String initialState,
       LinkedHashMap<String, Integer> states) {
-    StateModelDefinition.Builder builder = new StateModelDefinition.Builder(modelName);
-    builder.initialState(initialState);
-    int i = states.size();
+    StateModelDefinition.Builder builder =
+        new StateModelDefinition.Builder(Id.stateModelDef(modelName));
+    builder.initialState(State.from(initialState));
+    int i = 0;
     for (String state : states.keySet()) {
-      builder.addState(state, i);
-      builder.upperBound(state, states.get(state));
-      i--;
+      builder.addState(State.from(state), i);
+      builder.upperBound(State.from(state), states.get(state));
+      i++;
     }
     return builder.build();
   }
@@ -231,7 +233,7 @@ public class TestNewAutoRebalanceStrategy {
           ParticipantId participantId = Id.participant(nodeName);
           Participant participant =
               new Participant(participantId, "hostname", 0, true, disabledPartitionIdSet,
tags,
-                  null, currentStateMap, messageMap);
+                  null, currentStateMap, messageMap, new UserConfig(participantId));
           liveParticipantMap.put(participantId, participant);
         }
         List<ParticipantId> participantPreferenceList =

http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/d8ef5a2d/helix-examples/src/main/java/org/apache/helix/examples/NewModelExample.java
----------------------------------------------------------------------
diff --git a/helix-examples/src/main/java/org/apache/helix/examples/NewModelExample.java b/helix-examples/src/main/java/org/apache/helix/examples/NewModelExample.java
new file mode 100644
index 0000000..642a8a3
--- /dev/null
+++ b/helix-examples/src/main/java/org/apache/helix/examples/NewModelExample.java
@@ -0,0 +1,113 @@
+package org.apache.helix.examples;
+
+import java.util.concurrent.TimeUnit;
+
+import org.apache.helix.BaseDataAccessor;
+import org.apache.helix.HelixDataAccessor;
+import org.apache.helix.ZNRecord;
+import org.apache.helix.api.ClusterAccessor;
+import org.apache.helix.api.ClusterConfig;
+import org.apache.helix.api.ClusterId;
+import org.apache.helix.api.Id;
+import org.apache.helix.api.ParticipantConfig;
+import org.apache.helix.api.ParticipantId;
+import org.apache.helix.api.RebalancerConfig;
+import org.apache.helix.api.ResourceConfig;
+import org.apache.helix.api.ResourceId;
+import org.apache.helix.api.State;
+import org.apache.helix.api.StateModelDefId;
+import org.apache.helix.api.StateModelDefinitionAccessor;
+import org.apache.helix.manager.zk.ZKHelixAdmin;
+import org.apache.helix.manager.zk.ZKHelixDataAccessor;
+import org.apache.helix.manager.zk.ZNRecordSerializer;
+import org.apache.helix.manager.zk.ZkBaseDataAccessor;
+import org.apache.helix.manager.zk.ZkClient;
+import org.apache.helix.model.IdealState.RebalanceMode;
+import org.apache.helix.model.StateModelDefinition;
+import org.apache.log4j.Logger;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+public class NewModelExample {
+  private static final Logger LOG = Logger.getLogger(NewModelExample.class);
+
+  public static void main(String[] args) {
+    if (args.length < 1) {
+      LOG.error("USAGE: NewModelExample zkAddress");
+      System.exit(1);
+    }
+    StateModelDefinition lockUnlock = getLockUnlockModel();
+    ResourceConfig resource = getResource(lockUnlock);
+    ParticipantConfig participant = getParticipant();
+    ClusterId clusterId = Id.cluster("exampleCluster");
+    ClusterConfig cluster =
+        new ClusterConfig.Builder(clusterId).addResource(resource).addParticipant(participant)
+            .build();
+    int timeOutInSec = Integer.parseInt(System.getProperty(ZKHelixAdmin.CONNECTION_TIMEOUT,
"30"));
+    ZkClient zkClient = new ZkClient(args[0], timeOutInSec * 1000);
+    zkClient.setZkSerializer(new ZNRecordSerializer());
+    zkClient.waitUntilConnected(timeOutInSec, TimeUnit.SECONDS);
+    BaseDataAccessor<ZNRecord> baseDataAccessor = new ZkBaseDataAccessor<ZNRecord>(zkClient);
+    HelixDataAccessor accessor = new ZKHelixDataAccessor(clusterId.stringify(), baseDataAccessor);
+    persistStateModel(lockUnlock, accessor);
+    createCluster(cluster, accessor);
+  }
+
+  private static void persistStateModel(StateModelDefinition stateModelDef,
+      HelixDataAccessor helixAccessor) {
+    StateModelDefinitionAccessor accessor = new StateModelDefinitionAccessor(helixAccessor);
+    accessor.addStateModelDefinition(stateModelDef);
+  }
+
+  private static void createCluster(ClusterConfig cluster, HelixDataAccessor helixAccessor)
{
+    ClusterAccessor accessor = new ClusterAccessor(cluster.getId(), helixAccessor);
+    accessor.createCluster(cluster);
+  }
+
+  private static ParticipantConfig getParticipant() {
+    ParticipantId participantId = Id.participant("localhost_0");
+    ParticipantConfig.Builder participantBuilder =
+        new ParticipantConfig.Builder(participantId).hostName("localhost").port(0);
+    return participantBuilder.build();
+  }
+
+  private static ResourceConfig getResource(StateModelDefinition stateModelDef) {
+    ResourceId resourceId = Id.resource("exampleResource");
+    RebalancerConfig.Builder rebalanceConfigBuilder =
+        new RebalancerConfig.Builder(resourceId).rebalancerMode(RebalanceMode.FULL_AUTO)
+            .replicaCount(3).addPartitions(5).stateModelDef(stateModelDef.getStateModelDefId());
+    ResourceConfig.Builder resourceBuilder =
+        new ResourceConfig.Builder(resourceId).rebalancerConfig(rebalanceConfigBuilder.build());
+    return resourceBuilder.build();
+  }
+
+  private static StateModelDefinition getLockUnlockModel() {
+    final State LOCKED = State.from("LOCKED");
+    final State RELEASED = State.from("RELEASED");
+    final State DROPPED = State.from("DROPPED");
+    StateModelDefId stateModelId = Id.stateModelDef("LockUnlock");
+    StateModelDefinition.Builder stateModelBuilder =
+        new StateModelDefinition.Builder(stateModelId).addState(LOCKED, 0).addState(RELEASED,
1)
+            .addState(DROPPED, 2).addTransition(RELEASED, LOCKED, 0)
+            .addTransition(LOCKED, RELEASED, 1).upperBound(LOCKED, 1).upperBound(RELEASED,
-1)
+            .upperBound(DROPPED, -1).initialState(RELEASED);
+    return stateModelBuilder.build();
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/d8ef5a2d/helix-examples/src/main/java/org/apache/helix/examples/Quickstart.java
----------------------------------------------------------------------
diff --git a/helix-examples/src/main/java/org/apache/helix/examples/Quickstart.java b/helix-examples/src/main/java/org/apache/helix/examples/Quickstart.java
index 0e12fc8..2ff7c2c 100644
--- a/helix-examples/src/main/java/org/apache/helix/examples/Quickstart.java
+++ b/helix-examples/src/main/java/org/apache/helix/examples/Quickstart.java
@@ -32,9 +32,11 @@ import org.apache.helix.HelixAdmin;
 import org.apache.helix.HelixManager;
 import org.apache.helix.HelixManagerFactory;
 import org.apache.helix.InstanceType;
+import org.apache.helix.api.Id;
+import org.apache.helix.api.State;
+import org.apache.helix.api.StateModelDefId;
 import org.apache.helix.controller.HelixControllerMain;
 import org.apache.helix.manager.zk.ZKHelixAdmin;
-import org.apache.helix.manager.zk.ZNRecordSerializer;
 import org.apache.helix.model.ExternalView;
 import org.apache.helix.model.InstanceConfig;
 import org.apache.helix.model.StateModelDefinition;
@@ -49,13 +51,13 @@ public class Quickstart {
   private static final int NUM_PARTITIONS = 6;
   private static final int NUM_REPLICAS = 2;
 
-  private static final String STATE_MODEL_NAME = "MyStateModel";
+  private static final StateModelDefId STATE_MODEL_NAME = Id.stateModelDef("MyStateModel");
 
   // states
-  private static final String SLAVE = "SLAVE";
-  private static final String OFFLINE = "OFFLINE";
-  private static final String MASTER = "MASTER";
-  private static final String DROPPED = "DROPPED";
+  private static final State SLAVE = State.from("SLAVE");
+  private static final State OFFLINE = State.from("OFFLINE");
+  private static final State MASTER = State.from("MASTER");
+  private static final State DROPPED = State.from("DROPPED");
 
   private static List<InstanceConfig> INSTANCE_CONFIG_LIST;
   private static List<MyProcess> PROCESS_LIST;
@@ -90,11 +92,11 @@ public class Quickstart {
     // Add a state model
     StateModelDefinition myStateModel = defineStateModel();
     echo("Configuring StateModel: " + "MyStateModel  with 1 Master and 1 Slave");
-    admin.addStateModelDef(CLUSTER_NAME, STATE_MODEL_NAME, myStateModel);
+    admin.addStateModelDef(CLUSTER_NAME, STATE_MODEL_NAME.stringify(), myStateModel);
 
     // Add a resource with 6 partitions and 2 replicas
     echo("Adding a resource MyResource: " + "with 6 partitions and 2 replicas");
-    admin.addResource(CLUSTER_NAME, RESOURCE_NAME, NUM_PARTITIONS, STATE_MODEL_NAME, "AUTO");
+    admin.addResource(CLUSTER_NAME, RESOURCE_NAME, NUM_PARTITIONS, STATE_MODEL_NAME.stringify(),
"AUTO");
     // this will set up the ideal state, it calculates the preference list for
     // each partition similar to consistent hashing
     admin.rebalance(CLUSTER_NAME, RESOURCE_NAME, NUM_REPLICAS);
@@ -250,7 +252,7 @@ public class Quickstart {
           new MasterSlaveStateModelFactory(instanceName);
 
       StateMachineEngine stateMach = manager.getStateMachineEngine();
-      stateMach.registerStateModelFactory(STATE_MODEL_NAME, stateModelFactory);
+      stateMach.registerStateModelFactory(STATE_MODEL_NAME.stringify(), stateModelFactory);
       manager.connect();
     }
 


Mime
View raw message