You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@helix.apache.org by zz...@apache.org on 2013/09/18 22:11:07 UTC
[1/2] [HELIX-98] clean up setting constraint api,
[HELIX-246] Refactor scheduler task config to comply with new
rebalancer config and fix related scheduler task tests
Updated Branches:
refs/heads/helix-logical-model 6a0abbc72 -> 05c20e96b
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/05c20e96/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 a0400f2..88992c1 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
@@ -128,6 +128,7 @@ public class IdealState extends HelixProperty {
* Get the associated resource
* @return the name of the resource
*/
+ @Deprecated
public String getResourceName() {
return _record.getId();
}
@@ -174,6 +175,7 @@ public class IdealState extends HelixProperty {
* Define a custom rebalancer that implements {@link Rebalancer}
* @param rebalancerClassName the name of the custom rebalancing class
*/
+ @Deprecated
public void setRebalancerClassName(String rebalancerClassName) {
_record
.setSimpleField(IdealStateProperty.REBALANCER_CLASS_NAME.toString(), rebalancerClassName);
@@ -183,6 +185,7 @@ public class IdealState extends HelixProperty {
* Get the name of the user-defined rebalancer associated with this resource
* @return the rebalancer class name, or null if none is being used
*/
+ @Deprecated
public String getRebalancerClassName() {
return _record.getSimpleField(IdealStateProperty.REBALANCER_CLASS_NAME.toString());
}
@@ -276,6 +279,7 @@ public class IdealState extends HelixProperty {
* Get all of the partitions
* @return a set of partition names
*/
+ @Deprecated
public Set<String> getPartitionStringSet() {
if (getRebalanceMode() == RebalanceMode.SEMI_AUTO
|| getRebalanceMode() == RebalanceMode.FULL_AUTO) {
@@ -302,19 +306,11 @@ public class IdealState extends HelixProperty {
}
/**
- * Set the current mapping of a partition
- * @param partition the partition to set
- * @param instanceStateMap (instance name, state) pairs
- */
- public void setInstanceStateMap(String partition, Map<String, String> instanceStateMap) {
- _record.setMapField(partition, instanceStateMap);
- }
-
- /**
* Get the current mapping of a partition
* @param partitionName the name of the partition
* @return the instances where the replicas live and the state of each
*/
+ @Deprecated
public Map<String, String> getInstanceStateMap(String partitionName) {
return _record.getMapField(partitionName);
}
@@ -357,6 +353,7 @@ public class IdealState extends HelixProperty {
* @param partitionName the partition to look up
* @return set of instance names
*/
+ @Deprecated
public Set<String> getInstanceSet(String partitionName) {
if (getRebalanceMode() == RebalanceMode.SEMI_AUTO
|| getRebalanceMode() == RebalanceMode.FULL_AUTO) {
@@ -397,15 +394,6 @@ public class IdealState extends HelixProperty {
/**
* Set the preference list of a partition
- * @param partitionName the name of the partition to set
- * @param preferenceList a list of instances that can serve replicas of the partition
- */
- public void setPreferenceList(String partitionName, List<String> preferenceList) {
- _record.setListField(partitionName, preferenceList);
- }
-
- /**
- * Set the preference list of a partition
* @param partitionId the id of the partition to set
* @param preferenceList a list of participants that can serve replicas of the partition
*/
@@ -422,6 +410,7 @@ public class IdealState extends HelixProperty {
* @param partitionName the name of the partition
* @return a list of instances that can serve replicas of the partition
*/
+ @Deprecated
public List<String> getPreferenceList(String partitionName) {
List<String> instanceStateList = _record.getListField(partitionName);
@@ -453,6 +442,7 @@ public class IdealState extends HelixProperty {
* Get the state model associated with this resource
* @return an identifier of the state model
*/
+ @Deprecated
public String getStateModelDefRef() {
return _record.getSimpleField(IdealStateProperty.STATE_MODEL_DEF_REF.toString());
}
@@ -469,6 +459,7 @@ public class IdealState extends HelixProperty {
* Set the state model associated with this resource
* @param stateModel state model identifier
*/
+ @Deprecated
public void setStateModelDefRef(String stateModel) {
_record.setSimpleField(IdealStateProperty.STATE_MODEL_DEF_REF.toString(), stateModel);
}
@@ -558,6 +549,7 @@ public class IdealState extends HelixProperty {
* Set the state model factory associated with this resource
* @param name state model factory name
*/
+ @Deprecated
public void setStateModelFactoryName(String name) {
_record.setSimpleField(IdealStateProperty.STATE_MODEL_FACTORY_NAME.toString(), name);
}
@@ -574,6 +566,7 @@ public class IdealState extends HelixProperty {
* Get the state model factory associated with this resource
* @return state model factory name
*/
+ @Deprecated
public String getStateModelFactoryName() {
return _record.getStringField(IdealStateProperty.STATE_MODEL_FACTORY_NAME.toString(),
HelixConstants.DEFAULT_STATE_MODEL_FACTORY);
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/05c20e96/helix-core/src/main/java/org/apache/helix/model/builder/ClusterConstraintsBuilder.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/model/builder/ClusterConstraintsBuilder.java b/helix-core/src/main/java/org/apache/helix/model/builder/ClusterConstraintsBuilder.java
index 59cd825..7a0e7a0 100644
--- a/helix-core/src/main/java/org/apache/helix/model/builder/ClusterConstraintsBuilder.java
+++ b/helix-core/src/main/java/org/apache/helix/model/builder/ClusterConstraintsBuilder.java
@@ -22,6 +22,7 @@ package org.apache.helix.model.builder;
import java.util.HashMap;
import java.util.Map;
+import org.apache.helix.api.ConstraintId;
import org.apache.helix.model.ClusterConstraints;
import org.apache.helix.model.ClusterConstraints.ConstraintType;
import org.apache.log4j.Logger;
@@ -40,8 +41,8 @@ public class ClusterConstraintsBuilder {
* TRANSITION : OFFLINE->SLAVE
* CONSTRAINT_VALUE : 1
*/
- private final Map<String, ConstraintItemBuilder> _constraintBuilderMap =
- new HashMap<String, ConstraintItemBuilder>();
+ private final Map<ConstraintId, ConstraintItemBuilder> _constraintBuilderMap =
+ new HashMap<ConstraintId, ConstraintItemBuilder>();
public ClusterConstraintsBuilder(ConstraintType type) {
if (type == null) {
@@ -50,8 +51,8 @@ public class ClusterConstraintsBuilder {
_constraintType = type;
}
- public ClusterConstraintsBuilder addConstraintAttribute(String constraintId, String attribute,
- String value) {
+ public ClusterConstraintsBuilder addConstraintAttribute(ConstraintId constraintId,
+ String attribute, String value) {
if (!_constraintBuilderMap.containsKey(constraintId)) {
_constraintBuilderMap.put(constraintId, new ConstraintItemBuilder());
}
@@ -63,7 +64,7 @@ public class ClusterConstraintsBuilder {
public ClusterConstraints build() {
ClusterConstraints constraints = new ClusterConstraints(_constraintType);
- for (String constraintId : _constraintBuilderMap.keySet()) {
+ for (ConstraintId constraintId : _constraintBuilderMap.keySet()) {
ConstraintItemBuilder builder = _constraintBuilderMap.get(constraintId);
constraints.addConstraintItem(constraintId, builder.build());
}
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/05c20e96/helix-core/src/main/java/org/apache/helix/model/builder/IdealStateBuilder.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/model/builder/IdealStateBuilder.java b/helix-core/src/main/java/org/apache/helix/model/builder/IdealStateBuilder.java
index 7ae9dfe..3eb1afb 100644
--- a/helix-core/src/main/java/org/apache/helix/model/builder/IdealStateBuilder.java
+++ b/helix-core/src/main/java/org/apache/helix/model/builder/IdealStateBuilder.java
@@ -24,6 +24,7 @@ import org.apache.helix.HelixException;
import org.apache.helix.ZNRecord;
import org.apache.helix.api.ResourceId;
import org.apache.helix.api.StateModelDefId;
+import org.apache.helix.api.StateModelFactoryId;
import org.apache.helix.model.IdealState;
public abstract class IdealStateBuilder {
@@ -156,8 +157,8 @@ public abstract class IdealStateBuilder {
IdealState idealstate = new IdealState(_record);
idealstate.setNumPartitions(numPartitions);
idealstate.setMaxPartitionsPerInstance(maxPartitionsPerNode);
- idealstate.setStateModelDefRef(stateModel);
- idealstate.setStateModelFactoryName(stateModelFactoryName);
+ idealstate.setStateModelDefId(StateModelDefId.from(stateModel));
+ idealstate.setStateModelFactoryId(StateModelFactoryId.from(stateModelFactoryName));
idealstate.setRebalanceMode(rebalancerMode);
idealstate.setReplicas("" + numReplica);
@@ -166,5 +167,4 @@ public abstract class IdealStateBuilder {
}
return idealstate;
}
-
}
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/05c20e96/helix-core/src/main/java/org/apache/helix/model/builder/MessageConstraintItemBuilder.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/model/builder/MessageConstraintItemBuilder.java b/helix-core/src/main/java/org/apache/helix/model/builder/MessageConstraintItemBuilder.java
new file mode 100644
index 0000000..a44315b
--- /dev/null
+++ b/helix-core/src/main/java/org/apache/helix/model/builder/MessageConstraintItemBuilder.java
@@ -0,0 +1,107 @@
+package org.apache.helix.model.builder;
+
+import org.apache.helix.api.ParticipantId;
+import org.apache.helix.api.ResourceId;
+import org.apache.helix.model.ClusterConstraints.ConstraintAttribute;
+import org.apache.helix.model.ConstraintItem;
+import org.apache.helix.model.Message;
+import org.apache.helix.model.Message.MessageType;
+import org.apache.helix.model.Transition;
+
+/*
+ * 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.
+ */
+
+/**
+ * Specify a ConstraintItem based on a message (commonly used for transition constraints)
+ */
+public class MessageConstraintItemBuilder {
+ private ConstraintItemBuilder _builder;
+
+ /**
+ * Instantiate the builder
+ */
+ public MessageConstraintItemBuilder() {
+ _builder = new ConstraintItemBuilder();
+ }
+
+ /**
+ * Set the message type of the constraint
+ * @param messageType message type
+ * @return MessageConstraintItemBuilder
+ */
+ public MessageConstraintItemBuilder messageType(MessageType messageType) {
+ _builder.addConstraintAttribute(ConstraintAttribute.MESSAGE_TYPE.toString(),
+ messageType.toString());
+ return this;
+ }
+
+ /**
+ * Set a participant as part of the constraint scope
+ * @param participantId the participant to constrain
+ * @return MessageConstraintItemBuilder
+ */
+ public MessageConstraintItemBuilder participant(ParticipantId participantId) {
+ _builder.addConstraintAttribute(ConstraintAttribute.INSTANCE.toString(),
+ participantId.stringify());
+ return this;
+ }
+
+ /**
+ * Set a resource as part of the constraint scope
+ * @param resourceId the resource to constrain
+ * @return MessageConstraintItemBuilder
+ */
+ public MessageConstraintItemBuilder resource(ResourceId resourceId) {
+ _builder
+ .addConstraintAttribute(ConstraintAttribute.RESOURCE.toString(), resourceId.stringify());
+ return this;
+ }
+
+ /**
+ * Set the transition to constrain for transition message types
+ * @param transition the transition to constrain
+ * @return MessageConstraintItemBuilder
+ */
+ public MessageConstraintItemBuilder transition(Transition transition) {
+ // if this is a transition constraint, the message type must be STATE_TRANSITION
+ _builder.addConstraintAttribute(ConstraintAttribute.MESSAGE_TYPE.toString(),
+ Message.MessageType.STATE_TRANSITION.toString());
+ _builder.addConstraintAttribute(ConstraintAttribute.TRANSITION.toString(),
+ transition.toString());
+ return this;
+ }
+
+ /**
+ * Set the value of the constraint
+ * @param value constraint value
+ * @return MessageConstraintItemBuilder
+ */
+ public MessageConstraintItemBuilder constraintValue(String value) {
+ _builder.addConstraintAttribute(ConstraintAttribute.CONSTRAINT_VALUE.toString(), value);
+ return this;
+ }
+
+ /**
+ * Get the ConstraintItem instance that is built
+ * @return ConstraintItem
+ */
+ public ConstraintItem build() {
+ return _builder.build();
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/05c20e96/helix-core/src/main/java/org/apache/helix/model/builder/StateConstraintItemBuilder.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/model/builder/StateConstraintItemBuilder.java b/helix-core/src/main/java/org/apache/helix/model/builder/StateConstraintItemBuilder.java
new file mode 100644
index 0000000..d9af9f5
--- /dev/null
+++ b/helix-core/src/main/java/org/apache/helix/model/builder/StateConstraintItemBuilder.java
@@ -0,0 +1,92 @@
+package org.apache.helix.model.builder;
+
+import org.apache.helix.api.State;
+import org.apache.helix.api.StateModelDefId;
+import org.apache.helix.model.ClusterConstraints.ConstraintAttribute;
+import org.apache.helix.model.ConstraintItem;
+
+/*
+ * 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.
+ */
+
+/**
+ * Build a ConstraintItem corresponding to a state bound
+ */
+public class StateConstraintItemBuilder {
+ private ConstraintItemBuilder _builder;
+
+ /**
+ * Instantiate the builder
+ */
+ public StateConstraintItemBuilder() {
+ _builder = new ConstraintItemBuilder();
+ }
+
+ /**
+ * Get the state model definition to constrain
+ * @param stateModelDefId state model definition identifier
+ * @return StateConstraintItemBuilder
+ */
+ public StateConstraintItemBuilder stateModel(StateModelDefId stateModelDefId) {
+ _builder.addConstraintAttribute(ConstraintAttribute.STATE_MODEL.toString(),
+ stateModelDefId.stringify());
+ return this;
+ }
+
+ /**
+ * Get the state to constrain
+ * @param state state object
+ * @return StateConstraintItemBuilder
+ */
+ public StateConstraintItemBuilder state(State state) {
+ _builder.addConstraintAttribute(ConstraintAttribute.STATE.toString(), state.toString());
+ return this;
+ }
+
+ /**
+ * Set a numerical upper bound for the replicas that can be in a state
+ * @param upperBound maximum replica count for a state, per partition
+ * @return StateConstraintItemBuilder
+ */
+ public StateConstraintItemBuilder upperBound(int upperBound) {
+ _builder.addConstraintAttribute(ConstraintAttribute.CONSTRAINT_VALUE.toString(),
+ Integer.toString(upperBound));
+ return this;
+ }
+
+ /**
+ * Set an upper bound for the replicas that can be in a state. This can be numerical, or "N" for
+ * number of nodes, or "R" for total number of replicas per partition
+ * @param dynamicUpperBound maximum replica count for a state, per partition, can also be "N" or
+ * "R"
+ * @return StateConstraintItemBuilder
+ */
+ public StateConstraintItemBuilder dynamicUpperBound(String dynamicUpperBound) {
+ _builder.addConstraintAttribute(ConstraintAttribute.CONSTRAINT_VALUE.toString(),
+ dynamicUpperBound);
+ return this;
+ }
+
+ /**
+ * Get the ConstraintItem instance that is built
+ * @return ConstraintItem
+ */
+ public ConstraintItem build() {
+ return _builder.build();
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/05c20e96/helix-core/src/main/java/org/apache/helix/monitoring/mbeans/ResourceMonitor.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/monitoring/mbeans/ResourceMonitor.java b/helix-core/src/main/java/org/apache/helix/monitoring/mbeans/ResourceMonitor.java
index 2041999..7dcbfda 100644
--- a/helix-core/src/main/java/org/apache/helix/monitoring/mbeans/ResourceMonitor.java
+++ b/helix-core/src/main/java/org/apache/helix/monitoring/mbeans/ResourceMonitor.java
@@ -22,6 +22,9 @@ package org.apache.helix.monitoring.mbeans;
import java.util.Map;
import org.apache.helix.HelixDefinedState;
+import org.apache.helix.api.ParticipantId;
+import org.apache.helix.api.PartitionId;
+import org.apache.helix.api.State;
import org.apache.helix.model.ExternalView;
import org.apache.helix.model.IdealState;
import org.apache.log4j.Logger;
@@ -86,30 +89,31 @@ public class ResourceMonitor implements ResourceMonitorMBean {
// TODO fix this; IdealState shall have either map fields (CUSTOM mode)
// or list fields (AUDO mode)
- for (String partitionName : idealState.getRecord().getMapFields().keySet()) {
- Map<String, String> idealRecord = idealState.getInstanceStateMap(partitionName);
- Map<String, String> externalViewRecord = externalView.getStateMap(partitionName);
+ for (PartitionId partitionId : idealState.getPartitionSet()) {
+ Map<ParticipantId, State> idealRecord = idealState.getParticipantStateMap(partitionId);
+ Map<ParticipantId, State> externalViewRecord = externalView.getStateMap(partitionId);
if (externalViewRecord == null) {
numOfDiff += idealRecord.size();
continue;
}
- for (String host : idealRecord.keySet()) {
+ for (ParticipantId host : idealRecord.keySet()) {
if (!externalViewRecord.containsKey(host)
|| !externalViewRecord.get(host).equals(idealRecord.get(host))) {
numOfDiff++;
}
}
- for (String host : externalViewRecord.keySet()) {
- if (externalViewRecord.get(host).equalsIgnoreCase(HelixDefinedState.ERROR.toString())) {
+ for (ParticipantId host : externalViewRecord.keySet()) {
+ if (externalViewRecord.get(host).toString()
+ .equalsIgnoreCase(HelixDefinedState.ERROR.toString())) {
numOfErrorPartitions++;
}
}
}
_numOfErrorPartitions = numOfErrorPartitions;
_externalViewIdealStateDiff = numOfDiff;
- _numOfPartitionsInExternalView = externalView.getPartitionStringSet().size();
+ _numOfPartitionsInExternalView = externalView.getPartitionSet().size();
}
@Override
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/05c20e96/helix-core/src/main/java/org/apache/helix/participant/HelixCustomCodeRunner.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/participant/HelixCustomCodeRunner.java b/helix-core/src/main/java/org/apache/helix/participant/HelixCustomCodeRunner.java
index 4aa8e6e..cd238e1 100644
--- a/helix-core/src/main/java/org/apache/helix/participant/HelixCustomCodeRunner.java
+++ b/helix-core/src/main/java/org/apache/helix/participant/HelixCustomCodeRunner.java
@@ -28,6 +28,9 @@ import org.apache.helix.HelixConstants.StateModelToken;
import org.apache.helix.HelixDataAccessor;
import org.apache.helix.HelixManager;
import org.apache.helix.PropertyKey.Builder;
+import org.apache.helix.ZNRecord;
+import org.apache.helix.api.StateModelDefId;
+import org.apache.helix.api.StateModelFactoryId;
import org.apache.helix.manager.zk.ZKHelixDataAccessor;
import org.apache.helix.manager.zk.ZNRecordSerializer;
import org.apache.helix.manager.zk.ZkBaseDataAccessor;
@@ -127,15 +130,16 @@ public class HelixCustomCodeRunner {
zkClient = new ZkClient(_zkAddr, ZkClient.DEFAULT_CONNECTION_TIMEOUT);
zkClient.setZkSerializer(new ZNRecordSerializer());
HelixDataAccessor accessor =
- new ZKHelixDataAccessor(_manager.getClusterName(), new ZkBaseDataAccessor(zkClient));
+ new ZKHelixDataAccessor(_manager.getClusterName(), new ZkBaseDataAccessor<ZNRecord>(
+ zkClient));
Builder keyBuilder = accessor.keyBuilder();
IdealState idealState = new IdealState(_resourceName);
idealState.setRebalanceMode(RebalanceMode.SEMI_AUTO);
idealState.setReplicas(StateModelToken.ANY_LIVEINSTANCE.toString());
idealState.setNumPartitions(1);
- idealState.setStateModelDefRef(LEADER_STANDBY);
- idealState.setStateModelFactoryName(_resourceName);
+ idealState.setStateModelDefId(StateModelDefId.from(LEADER_STANDBY));
+ idealState.setStateModelFactoryId(StateModelFactoryId.from(_resourceName));
List<String> prefList =
new ArrayList<String>(Arrays.asList(StateModelToken.ANY_LIVEINSTANCE.toString()));
idealState.getRecord().setListField(_resourceName + "_0", prefList);
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/05c20e96/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 ba417be..fa60c34 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
@@ -310,8 +310,8 @@ public class ClusterSetup {
accessor.getChildValues(accessor.keyBuilder().idealStates());
for (IdealState idealState : existingIdealStates) {
swapInstanceInIdealState(idealState, oldInstanceName, newInstanceName);
- accessor.setProperty(accessor.keyBuilder().idealState(idealState.getResourceName()),
- idealState);
+ accessor.setProperty(
+ accessor.keyBuilder().idealState(idealState.getResourceId().stringify()), idealState);
}
}
@@ -383,15 +383,15 @@ public class ClusterSetup {
IdealState idealState = _admin.getResourceIdealState(clusterName, resourceName);
if (idealState.getRebalanceMode() == RebalanceMode.FULL_AUTO
|| idealState.getRebalanceMode() == RebalanceMode.CUSTOMIZED) {
- _logger.info("Skipping idealState " + idealState.getResourceName() + " "
+ _logger.info("Skipping idealState " + idealState.getResourceId() + " "
+ idealState.getRebalanceMode());
return;
}
boolean anyLiveInstance = false;
for (List<String> list : idealState.getRecord().getListFields().values()) {
if (list.contains(StateModelToken.ANY_LIVEINSTANCE.toString())) {
- _logger.info("Skipping idealState " + idealState.getResourceName()
- + " with ANY_LIVEINSTANCE");
+ _logger
+ .info("Skipping idealState " + idealState.getResourceId() + " with ANY_LIVEINSTANCE");
anyLiveInstance = true;
continue;
}
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/05c20e96/helix-core/src/main/java/org/apache/helix/util/RebalanceUtil.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/util/RebalanceUtil.java b/helix-core/src/main/java/org/apache/helix/util/RebalanceUtil.java
index 0911cc4..4b014de 100644
--- a/helix-core/src/main/java/org/apache/helix/util/RebalanceUtil.java
+++ b/helix-core/src/main/java/org/apache/helix/util/RebalanceUtil.java
@@ -27,9 +27,13 @@ import java.util.Map;
import java.util.TreeMap;
import org.apache.helix.HelixException;
+import org.apache.helix.api.PartitionId;
import org.apache.helix.model.IdealState;
import org.apache.helix.model.StateModelDefinition;
+import com.google.common.base.Functions;
+import com.google.common.collect.Lists;
+
public class RebalanceUtil {
public static Map<String, Object> buildInternalIdealState(IdealState state) {
// Try parse the partition number from name DB_n. If not, sort the partitions and
@@ -37,17 +41,19 @@ public class RebalanceUtil {
Map<String, Integer> partitionIndex = new HashMap<String, Integer>();
Map<String, String> reversePartitionIndex = new HashMap<String, String>();
boolean indexInPartitionName = true;
- for (String partitionId : state.getPartitionStringSet()) {
- int lastPos = partitionId.lastIndexOf("_");
+ for (PartitionId partitionId : state.getPartitionSet()) {
+ String partitionName = partitionId.stringify();
+ int lastPos = partitionName.lastIndexOf("_");
if (lastPos < 0) {
indexInPartitionName = false;
break;
}
try {
- String idStr = partitionId.substring(lastPos + 1);
+ String idStr = partitionName.substring(lastPos + 1);
int partition = Integer.parseInt(idStr);
- partitionIndex.put(partitionId, partition);
- reversePartitionIndex.put(state.getResourceName() + "_" + partition, partitionId);
+ partitionIndex.put(partitionName, partition);
+ reversePartitionIndex.put(state.getResourceId().stringify() + "_" + partition,
+ partitionName);
} catch (Exception e) {
indexInPartitionName = false;
partitionIndex.clear();
@@ -58,19 +64,20 @@ public class RebalanceUtil {
if (indexInPartitionName == false) {
List<String> partitions = new ArrayList<String>();
- partitions.addAll(state.getPartitionStringSet());
+ partitions.addAll(Lists.transform(Lists.newArrayList(state.getPartitionSet()),
+ Functions.toStringFunction()));
Collections.sort(partitions);
for (int i = 0; i < partitions.size(); i++) {
partitionIndex.put(partitions.get(i), i);
- reversePartitionIndex.put(state.getResourceName() + "_" + i, partitions.get(i));
+ reversePartitionIndex.put(state.getResourceId().stringify() + "_" + i, partitions.get(i));
}
}
Map<String, List<Integer>> nodeMasterAssignmentMap = new TreeMap<String, List<Integer>>();
Map<String, Map<String, List<Integer>>> combinedNodeSlaveAssignmentMap =
new TreeMap<String, Map<String, List<Integer>>>();
- for (String partition : state.getPartitionStringSet()) {
- List<String> instances = state.getRecord().getListField(partition);
+ for (PartitionId partition : state.getPartitionSet()) {
+ List<String> instances = state.getRecord().getListField(partition.stringify());
String master = instances.get(0);
if (!nodeMasterAssignmentMap.containsKey(master)) {
nodeMasterAssignmentMap.put(master, new ArrayList<Integer>());
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/05c20e96/helix-core/src/test/java/org/apache/helix/TestZKCallback.java
----------------------------------------------------------------------
diff --git a/helix-core/src/test/java/org/apache/helix/TestZKCallback.java b/helix-core/src/test/java/org/apache/helix/TestZKCallback.java
index 7abfd4c..40a189d 100644
--- a/helix-core/src/test/java/org/apache/helix/TestZKCallback.java
+++ b/helix-core/src/test/java/org/apache/helix/TestZKCallback.java
@@ -160,7 +160,7 @@ public class TestZKCallback extends ZkUnitTestBase {
IdealState idealState = new IdealState("db-1234");
idealState.setNumPartitions(400);
idealState.setReplicas(Integer.toString(2));
- idealState.setStateModelDefRef("StateModeldef");
+ idealState.setStateModelDefId(StateModelDefId.from("StateModeldef"));
accessor.setProperty(keyBuilder.idealState("db-1234"), idealState);
Thread.sleep(100);
AssertJUnit.assertTrue(testListener.idealStateChangeReceived);
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/05c20e96/helix-core/src/test/java/org/apache/helix/ZkUnitTestBase.java
----------------------------------------------------------------------
diff --git a/helix-core/src/test/java/org/apache/helix/ZkUnitTestBase.java b/helix-core/src/test/java/org/apache/helix/ZkUnitTestBase.java
index 9a484d6..7eb53fe 100644
--- a/helix-core/src/test/java/org/apache/helix/ZkUnitTestBase.java
+++ b/helix-core/src/test/java/org/apache/helix/ZkUnitTestBase.java
@@ -29,7 +29,9 @@ import org.I0Itec.zkclient.ZkConnection;
import org.I0Itec.zkclient.ZkServer;
import org.apache.helix.PropertyKey.Builder;
import org.apache.helix.api.MessageId;
+import org.apache.helix.api.PartitionId;
import org.apache.helix.api.State;
+import org.apache.helix.api.StateModelDefId;
import org.apache.helix.controller.pipeline.Pipeline;
import org.apache.helix.controller.pipeline.Stage;
import org.apache.helix.controller.pipeline.StageContext;
@@ -166,7 +168,7 @@ public class ZkUnitTestBase {
public void verifyEnabled(ZkClient zkClient, String clusterName, String instance,
boolean wantEnabled) {
ZKHelixDataAccessor accessor =
- new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor(zkClient));
+ new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(zkClient));
Builder keyBuilder = accessor.keyBuilder();
InstanceConfig config = accessor.getProperty(keyBuilder.instanceConfig(instance));
@@ -175,15 +177,15 @@ public class ZkUnitTestBase {
public void verifyReplication(ZkClient zkClient, String clusterName, String resource, int repl) {
ZKHelixDataAccessor accessor =
- new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor(zkClient));
+ new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(zkClient));
Builder keyBuilder = accessor.keyBuilder();
IdealState idealState = accessor.getProperty(keyBuilder.idealState(resource));
- for (String partitionName : idealState.getPartitionStringSet()) {
+ for (PartitionId partitionId : idealState.getPartitionSet()) {
if (idealState.getRebalanceMode() == RebalanceMode.SEMI_AUTO) {
- AssertJUnit.assertEquals(repl, idealState.getPreferenceList(partitionName).size());
+ AssertJUnit.assertEquals(repl, idealState.getPreferenceList(partitionId).size());
} else if (idealState.getRebalanceMode() == RebalanceMode.CUSTOMIZED) {
- AssertJUnit.assertEquals(repl, idealState.getInstanceStateMap(partitionName).size());
+ AssertJUnit.assertEquals(repl, idealState.getParticipantStateMap(partitionId).size());
}
}
}
@@ -249,20 +251,19 @@ public class ZkUnitTestBase {
protected void setupStateModel(String clusterName) {
ZKHelixDataAccessor accessor =
- new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor(_gZkClient));
+ new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_gZkClient));
Builder keyBuilder = accessor.keyBuilder();
- StateModelConfigGenerator generator = new StateModelConfigGenerator();
StateModelDefinition masterSlave =
- new StateModelDefinition(generator.generateConfigForMasterSlave());
+ new StateModelDefinition(StateModelConfigGenerator.generateConfigForMasterSlave());
accessor.setProperty(keyBuilder.stateModelDef(masterSlave.getId()), masterSlave);
StateModelDefinition leaderStandby =
- new StateModelDefinition(generator.generateConfigForLeaderStandby());
+ new StateModelDefinition(StateModelConfigGenerator.generateConfigForLeaderStandby());
accessor.setProperty(keyBuilder.stateModelDef(leaderStandby.getId()), leaderStandby);
StateModelDefinition onlineOffline =
- new StateModelDefinition(generator.generateConfigForOnlineOffline());
+ new StateModelDefinition(StateModelConfigGenerator.generateConfigForOnlineOffline());
accessor.setProperty(keyBuilder.stateModelDef(onlineOffline.getId()), onlineOffline);
}
@@ -270,7 +271,7 @@ public class ZkUnitTestBase {
protected List<IdealState> setupIdealState(String clusterName, int[] nodes, String[] resources,
int partitions, int replicas) {
ZKHelixDataAccessor accessor =
- new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor(_gZkClient));
+ new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(_gZkClient));
Builder keyBuilder = accessor.keyBuilder();
List<IdealState> idealStates = new ArrayList<IdealState>();
@@ -291,7 +292,7 @@ public class ZkUnitTestBase {
}
idealState.setReplicas(Integer.toString(replicas));
- idealState.setStateModelDefRef("MasterSlave");
+ idealState.setStateModelDefId(StateModelDefId.from("MasterSlave"));
idealState.setRebalanceMode(RebalanceMode.SEMI_AUTO);
idealState.setNumPartitions(partitions);
idealStates.add(idealState);
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/05c20e96/helix-core/src/test/java/org/apache/helix/controller/stages/BaseStageTest.java
----------------------------------------------------------------------
diff --git a/helix-core/src/test/java/org/apache/helix/controller/stages/BaseStageTest.java b/helix-core/src/test/java/org/apache/helix/controller/stages/BaseStageTest.java
index 4d96431..f7cdcba 100644
--- a/helix-core/src/test/java/org/apache/helix/controller/stages/BaseStageTest.java
+++ b/helix-core/src/test/java/org/apache/helix/controller/stages/BaseStageTest.java
@@ -99,7 +99,7 @@ public class BaseStageTest {
record.setListField(resourceName + "_" + p, value);
}
IdealState idealState = new IdealState(record);
- idealState.setStateModelDefRef("MasterSlave");
+ idealState.setStateModelDefId(StateModelDefId.from("MasterSlave"));
idealState.setRebalanceMode(rebalanceMode);
idealState.setNumPartitions(partitions);
idealStates.add(idealState);
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/05c20e96/helix-core/src/test/java/org/apache/helix/controller/stages/TestBestPossibleCalcStageCompatibility.java
----------------------------------------------------------------------
diff --git a/helix-core/src/test/java/org/apache/helix/controller/stages/TestBestPossibleCalcStageCompatibility.java b/helix-core/src/test/java/org/apache/helix/controller/stages/TestBestPossibleCalcStageCompatibility.java
index 8027d11..a8783c3 100644
--- a/helix-core/src/test/java/org/apache/helix/controller/stages/TestBestPossibleCalcStageCompatibility.java
+++ b/helix-core/src/test/java/org/apache/helix/controller/stages/TestBestPossibleCalcStageCompatibility.java
@@ -31,6 +31,7 @@ import org.apache.helix.api.PartitionId;
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.model.IdealState;
import org.apache.helix.model.IdealState.IdealStateModeProperty;
import org.testng.AssertJUnit;
@@ -130,17 +131,17 @@ public class TestBestPossibleCalcStageCompatibility extends BaseStageTest {
String resourceName = resources[i];
IdealState idealState = new IdealState(resourceName);
for (int p = 0; p < partitions; p++) {
- List<String> value = new ArrayList<String>();
+ List<ParticipantId> value = new ArrayList<ParticipantId>();
for (int r = 0; r < replicas; r++) {
- value.add("localhost_" + (p + r + 1) % nodes);
+ value.add(ParticipantId.from("localhost_" + (p + r + 1) % nodes));
}
- idealState.setPreferenceList(resourceName + "_" + p, value);
+ idealState.setPreferenceList(PartitionId.from(resourceName + "_" + p), value);
Map<ParticipantId, State> preferenceMap = new HashMap<ParticipantId, State>();
preferenceMap.put(ParticipantId.from("localhost_" + (p + 1) % 5), State.from("MASTER"));
idealState.setParticipantStateMap(
PartitionId.from(ResourceId.from(resourceName), Integer.toString(p)), preferenceMap);
}
- idealState.setStateModelDefRef("MasterSlave");
+ idealState.setStateModelDefId(StateModelDefId.from("MasterSlave"));
idealState.setIdealStateMode(mode.toString());
idealState.setNumPartitions(partitions);
idealStates.add(idealState);
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/05c20e96/helix-core/src/test/java/org/apache/helix/controller/stages/TestCompatibilityCheckStage.java
----------------------------------------------------------------------
diff --git a/helix-core/src/test/java/org/apache/helix/controller/stages/TestCompatibilityCheckStage.java b/helix-core/src/test/java/org/apache/helix/controller/stages/TestCompatibilityCheckStage.java
index 47875fc..28b8a19 100644
--- a/helix-core/src/test/java/org/apache/helix/controller/stages/TestCompatibilityCheckStage.java
+++ b/helix-core/src/test/java/org/apache/helix/controller/stages/TestCompatibilityCheckStage.java
@@ -25,6 +25,7 @@ import java.util.List;
import org.apache.helix.Mocks;
import org.apache.helix.PropertyKey.Builder;
import org.apache.helix.ZNRecord;
+import org.apache.helix.api.StateModelDefId;
import org.apache.helix.controller.pipeline.StageContext;
import org.apache.helix.controller.strategy.DefaultTwoStateStrategy;
import org.apache.helix.model.IdealState;
@@ -52,7 +53,7 @@ public class TestCompatibilityCheckStage extends BaseStageTest {
DefaultTwoStateStrategy.calculateIdealState(instances, partitions, replicas, resourceName,
"MASTER", "SLAVE");
IdealState idealState = new IdealState(record);
- idealState.setStateModelDefRef("MasterSlave");
+ idealState.setStateModelDefId(StateModelDefId.from("MasterSlave"));
Builder keyBuilder = accessor.keyBuilder();
accessor.setProperty(keyBuilder.idealState(resourceName), idealState);
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/05c20e96/helix-core/src/test/java/org/apache/helix/controller/stages/TestResourceComputationStage.java
----------------------------------------------------------------------
diff --git a/helix-core/src/test/java/org/apache/helix/controller/stages/TestResourceComputationStage.java b/helix-core/src/test/java/org/apache/helix/controller/stages/TestResourceComputationStage.java
index b9cba1f..45507a1 100644
--- a/helix-core/src/test/java/org/apache/helix/controller/stages/TestResourceComputationStage.java
+++ b/helix-core/src/test/java/org/apache/helix/controller/stages/TestResourceComputationStage.java
@@ -31,6 +31,7 @@ import org.apache.helix.api.PartitionId;
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.controller.pipeline.StageContext;
import org.apache.helix.controller.strategy.DefaultTwoStateStrategy;
import org.apache.helix.model.CurrentState;
@@ -60,7 +61,7 @@ public class TestResourceComputationStage extends BaseStageTest {
DefaultTwoStateStrategy.calculateIdealState(instances, partitions, replicas, resourceName,
"MASTER", "SLAVE");
IdealState idealState = new IdealState(record);
- idealState.setStateModelDefRef("MasterSlave");
+ idealState.setStateModelDefId(StateModelDefId.from("MasterSlave"));
HelixDataAccessor accessor = manager.getHelixDataAccessor();
Builder keyBuilder = accessor.keyBuilder();
@@ -104,7 +105,7 @@ public class TestResourceComputationStage extends BaseStageTest {
AssertJUnit.assertTrue(resourceMap.containsKey(resourceId));
AssertJUnit.assertEquals(resourceMap.get(resourceId).getId(), resourceId);
AssertJUnit.assertEquals(resourceMap.get(resourceId).getRebalancerConfig()
- .getStateModelDefId(), idealState.getStateModelDefRef());
+ .getStateModelDefId(), idealState.getStateModelDefId());
AssertJUnit.assertEquals(resourceMap.get(resourceId).getPartitionSet().size(),
idealState.getNumPartitions());
}
@@ -129,7 +130,7 @@ public class TestResourceComputationStage extends BaseStageTest {
DefaultTwoStateStrategy.calculateIdealState(instances, partitions, replicas,
resourceName, "MASTER", "SLAVE");
IdealState idealState = new IdealState(record);
- idealState.setStateModelDefRef("MasterSlave");
+ idealState.setStateModelDefId(StateModelDefId.from("MasterSlave"));
HelixDataAccessor accessor = manager.getHelixDataAccessor();
Builder keyBuilder = accessor.keyBuilder();
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/05c20e96/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 02e7e29..7eb811b 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
@@ -35,6 +35,8 @@ import java.util.TreeSet;
import org.apache.helix.HelixDefinedState;
import org.apache.helix.ZNRecord;
+import org.apache.helix.api.ClusterConfig;
+import org.apache.helix.api.ClusterId;
import org.apache.helix.api.MessageId;
import org.apache.helix.api.Participant;
import org.apache.helix.api.ParticipantId;
@@ -220,6 +222,15 @@ public class TestNewAutoRebalanceStrategy {
private Map<String, Map<String, String>> getMapping(final Map<String, List<String>> listResult) {
final Map<PartitionId, Map<ParticipantId, State>> mapResult =
new HashMap<PartitionId, Map<ParticipantId, State>>();
+ ClusterId clusterId = ClusterId.from("clusterId");
+ ClusterConfig.Builder clusterConfigBuilder =
+ new ClusterConfig.Builder(clusterId).addStateModelDefinition(_stateModelDef);
+ for (State state : _stateModelDef.getStatesPriorityList()) {
+ clusterConfigBuilder.addStateUpperBoundConstraint(Scope.cluster(clusterId),
+ _stateModelDef.getStateModelDefId(), state,
+ _stateModelDef.getNumParticipantsPerState(state));
+ }
+ ClusterConfig clusterConfig = clusterConfigBuilder.build();
for (String partition : _partitions) {
PartitionId partitionId = PartitionId.from(partition);
Set<ParticipantId> disabledParticipantsForPartition = Collections.emptySet();
@@ -249,9 +260,9 @@ public class TestNewAutoRebalanceStrategy {
Map<ParticipantId, State> replicaMap =
ResourceAssignment.replicaMapFromStringMap(_currentMapping.get(partition));
Map<ParticipantId, State> assignment =
- NewConstraintBasedAssignment.computeAutoBestStateForPartition(liveParticipantMap,
- _stateModelDef, participantPreferenceList, replicaMap,
- disabledParticipantsForPartition);
+ NewConstraintBasedAssignment.computeAutoBestStateForPartition(clusterConfig,
+ ResourceId.from(RESOURCE_NAME), liveParticipantMap, _stateModelDef,
+ participantPreferenceList, replicaMap, disabledParticipantsForPartition);
mapResult.put(partitionId, assignment);
}
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/05c20e96/helix-core/src/test/java/org/apache/helix/integration/TestAddStateModelFactoryAfterConnect.java
----------------------------------------------------------------------
diff --git a/helix-core/src/test/java/org/apache/helix/integration/TestAddStateModelFactoryAfterConnect.java b/helix-core/src/test/java/org/apache/helix/integration/TestAddStateModelFactoryAfterConnect.java
index f38f3b4..5325e30 100644
--- a/helix-core/src/test/java/org/apache/helix/integration/TestAddStateModelFactoryAfterConnect.java
+++ b/helix-core/src/test/java/org/apache/helix/integration/TestAddStateModelFactoryAfterConnect.java
@@ -25,6 +25,7 @@ import java.util.List;
import org.apache.helix.PropertyKey.Builder;
import org.apache.helix.TestHelper;
import org.apache.helix.ZNRecord;
+import org.apache.helix.api.StateModelFactoryId;
import org.apache.helix.manager.zk.ZKHelixDataAccessor;
import org.apache.helix.manager.zk.ZkBaseDataAccessor;
import org.apache.helix.mock.controller.ClusterController;
@@ -84,7 +85,7 @@ public class TestAddStateModelFactoryAfterConnect extends ZkIntegrationTestBase
ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, baseAccessor);
Builder keyBuilder = accessor.keyBuilder();
IdealState idealState = accessor.getProperty(keyBuilder.idealState("TestDB1"));
- idealState.setStateModelFactoryName("TestDB1_Factory");
+ idealState.setStateModelFactoryId(StateModelFactoryId.from("TestDB1_Factory"));
accessor.setProperty(keyBuilder.idealState("TestDB1"), idealState);
setupTool.rebalanceStorageCluster(clusterName, "TestDB1", 3);
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/05c20e96/helix-core/src/test/java/org/apache/helix/integration/TestAutoRebalance.java
----------------------------------------------------------------------
diff --git a/helix-core/src/test/java/org/apache/helix/integration/TestAutoRebalance.java b/helix-core/src/test/java/org/apache/helix/integration/TestAutoRebalance.java
index ba44fcf..e837626 100644
--- a/helix-core/src/test/java/org/apache/helix/integration/TestAutoRebalance.java
+++ b/helix-core/src/test/java/org/apache/helix/integration/TestAutoRebalance.java
@@ -30,6 +30,7 @@ import org.apache.helix.PropertyKey.Builder;
import org.apache.helix.TestHelper;
import org.apache.helix.TestHelper.StartCMResult;
import org.apache.helix.ZNRecord;
+import org.apache.helix.api.State;
import org.apache.helix.controller.HelixControllerMain;
import org.apache.helix.controller.stages.ClusterDataCache;
import org.apache.helix.manager.zk.ZKHelixDataAccessor;
@@ -262,13 +263,13 @@ public class TestAutoRebalance extends ZkStandAloneCMTestBaseWithPropertyServerC
return false;
}
- int numberOfPartitions = idealState.getRecord().getListFields()
- .size();
+ int numberOfPartitions = idealState.getRecord().getListFields().size();
ClusterDataCache cache = new ClusterDataCache();
cache.refresh(accessor);
- String masterValue =
- cache.getStateModelDef(cache.getIdealState(_resourceName).getStateModelDefRef())
- .getStatesPriorityStringList().get(0);
+ State masterValue =
+ cache
+ .getStateModelDef(cache.getIdealState(_resourceName).getStateModelDefId().stringify())
+ .getStatesPriorityList().get(0);
int replicas = Integer.parseInt(cache.getIdealState(_resourceName).getReplicas());
String instanceGroupTag = cache.getIdealState(_resourceName).getInstanceGroupTag();
int instances = 0;
@@ -284,8 +285,8 @@ public class TestAutoRebalance extends ZkStandAloneCMTestBaseWithPropertyServerC
if (ev == null) {
return false;
}
- return verifyBalanceExternalView(ev.getRecord(), numberOfPartitions, masterValue, replicas,
- instances);
+ return verifyBalanceExternalView(ev.getRecord(), numberOfPartitions, masterValue.toString(),
+ replicas, instances);
}
@Override
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/05c20e96/helix-core/src/test/java/org/apache/helix/integration/TestAutoRebalancePartitionLimit.java
----------------------------------------------------------------------
diff --git a/helix-core/src/test/java/org/apache/helix/integration/TestAutoRebalancePartitionLimit.java b/helix-core/src/test/java/org/apache/helix/integration/TestAutoRebalancePartitionLimit.java
index 2a0c645..2446b67 100644
--- a/helix-core/src/test/java/org/apache/helix/integration/TestAutoRebalancePartitionLimit.java
+++ b/helix-core/src/test/java/org/apache/helix/integration/TestAutoRebalancePartitionLimit.java
@@ -29,6 +29,7 @@ import org.apache.helix.PropertyKey.Builder;
import org.apache.helix.TestHelper;
import org.apache.helix.TestHelper.StartCMResult;
import org.apache.helix.ZNRecord;
+import org.apache.helix.api.State;
import org.apache.helix.controller.HelixControllerMain;
import org.apache.helix.controller.stages.ClusterDataCache;
import org.apache.helix.manager.zk.ZKHelixDataAccessor;
@@ -229,13 +230,15 @@ public class TestAutoRebalancePartitionLimit extends ZkStandAloneCMTestBaseWithP
.size();
ClusterDataCache cache = new ClusterDataCache();
cache.refresh(accessor);
- String masterValue =
- cache.getStateModelDef(cache.getIdealState(_resourceName).getStateModelDefRef())
- .getStatesPriorityStringList().get(0);
+ State masterValue =
+ cache
+ .getStateModelDef(cache.getIdealState(_resourceName).getStateModelDefId().stringify())
+ .getStatesPriorityList().get(0);
int replicas = Integer.parseInt(cache.getIdealState(_resourceName).getReplicas());
return verifyBalanceExternalView(accessor.getProperty(keyBuilder.externalView(_resourceName))
- .getRecord(), numberOfPartitions, masterValue, replicas, cache.getLiveInstances().size(),
- cache.getIdealState(_resourceName).getMaxPartitionsPerInstance());
+ .getRecord(), numberOfPartitions, masterValue.toString(), replicas, cache
+ .getLiveInstances().size(), cache.getIdealState(_resourceName)
+ .getMaxPartitionsPerInstance());
}
@Override
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/05c20e96/helix-core/src/test/java/org/apache/helix/integration/TestCustomizedIdealStateRebalancer.java
----------------------------------------------------------------------
diff --git a/helix-core/src/test/java/org/apache/helix/integration/TestCustomizedIdealStateRebalancer.java b/helix-core/src/test/java/org/apache/helix/integration/TestCustomizedIdealStateRebalancer.java
index 1b0a0b4..4f7b814 100644
--- a/helix-core/src/test/java/org/apache/helix/integration/TestCustomizedIdealStateRebalancer.java
+++ b/helix-core/src/test/java/org/apache/helix/integration/TestCustomizedIdealStateRebalancer.java
@@ -107,9 +107,9 @@ public class TestCustomizedIdealStateRebalancer extends
Assert.assertEquals(ev.getStateMap(partition).size(), 1);
}
IdealState is = accessor.getProperty(keyBuilder.idealState(db2));
- for (String partition : is.getPartitionStringSet()) {
+ for (PartitionId partition : is.getPartitionSet()) {
Assert.assertEquals(is.getPreferenceList(partition).size(), 0);
- Assert.assertEquals(is.getInstanceStateMap(partition).size(), 0);
+ Assert.assertEquals(is.getParticipantStateMap(partition).size(), 0);
}
Assert.assertTrue(testRebalancerInvoked);
}
@@ -136,9 +136,11 @@ public class TestCustomizedIdealStateRebalancer extends
.size();
ClusterDataCache cache = new ClusterDataCache();
cache.refresh(accessor);
- String masterValue =
- cache.getStateModelDef(cache.getIdealState(_resourceName).getStateModelDefRef())
- .getStatesPriorityStringList().get(0);
+ State masterValue =
+ cache
+ .getStateModelDef(
+ cache.getIdealState(_resourceName).getStateModelDefId().stringify())
+ .getStatesPriorityList().get(0);
int replicas = Integer.parseInt(cache.getIdealState(_resourceName).getReplicas());
String instanceGroupTag = cache.getIdealState(_resourceName).getInstanceGroupTag();
int instances = 0;
@@ -151,8 +153,8 @@ public class TestCustomizedIdealStateRebalancer extends
instances = cache.getLiveInstances().size();
}
ExternalView externalView = accessor.getProperty(keyBuilder.externalView(_resourceName));
- return verifyBalanceExternalView(externalView.getRecord(), numberOfPartitions, masterValue,
- replicas, instances);
+ return verifyBalanceExternalView(externalView.getRecord(), numberOfPartitions,
+ masterValue.toString(), replicas, instances);
} catch (Exception e) {
return false;
}
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/05c20e96/helix-core/src/test/java/org/apache/helix/integration/TestInvalidAutoIdealState.java
----------------------------------------------------------------------
diff --git a/helix-core/src/test/java/org/apache/helix/integration/TestInvalidAutoIdealState.java b/helix-core/src/test/java/org/apache/helix/integration/TestInvalidAutoIdealState.java
index fa4d5a8..7f4335f 100644
--- a/helix-core/src/test/java/org/apache/helix/integration/TestInvalidAutoIdealState.java
+++ b/helix-core/src/test/java/org/apache/helix/integration/TestInvalidAutoIdealState.java
@@ -28,6 +28,7 @@ import org.apache.helix.HelixDataAccessor;
import org.apache.helix.PropertyKey.Builder;
import org.apache.helix.TestHelper;
import org.apache.helix.ZkUnitTestBase;
+import org.apache.helix.api.StateModelDefId;
import org.apache.helix.manager.zk.ZKHelixAdmin;
import org.apache.helix.mock.controller.ClusterController;
import org.apache.helix.mock.participant.MockParticipant;
@@ -81,7 +82,7 @@ public class TestInvalidAutoIdealState extends ZkUnitTestBase {
idealState.setRebalanceMode(RebalanceMode.SEMI_AUTO);
idealState.setNumPartitions(2);
idealState.setReplicas("" + 2); // should be 3
- idealState.setStateModelDefRef("MasterSlave");
+ idealState.setStateModelDefId(StateModelDefId.from("MasterSlave"));
idealState.getRecord().setListField("TestDB_0",
Arrays.asList("localhost_12918", "localhost_12919", "localhost_12920"));
idealState.getRecord().setListField("TestDB_1",
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/05c20e96/helix-core/src/test/java/org/apache/helix/integration/TestRenamePartition.java
----------------------------------------------------------------------
diff --git a/helix-core/src/test/java/org/apache/helix/integration/TestRenamePartition.java b/helix-core/src/test/java/org/apache/helix/integration/TestRenamePartition.java
index 37a62ef..a0330d4 100644
--- a/helix-core/src/test/java/org/apache/helix/integration/TestRenamePartition.java
+++ b/helix-core/src/test/java/org/apache/helix/integration/TestRenamePartition.java
@@ -27,6 +27,7 @@ import java.util.Map;
import org.apache.helix.PropertyKey.Builder;
import org.apache.helix.TestHelper;
import org.apache.helix.ZNRecord;
+import org.apache.helix.api.StateModelDefId;
import org.apache.helix.controller.HelixControllerMain;
import org.apache.helix.controller.strategy.DefaultTwoStateStrategy;
import org.apache.helix.manager.zk.ZKHelixDataAccessor;
@@ -100,7 +101,7 @@ public class TestRenamePartition extends ZkIntegrationTestBase {
IdealState idealState = new IdealState(destIS);
idealState.setRebalanceMode(RebalanceMode.CUSTOMIZED);
idealState.setReplicas("3");
- idealState.setStateModelDefRef("MasterSlave");
+ idealState.setStateModelDefId(StateModelDefId.from("MasterSlave"));
ZKHelixDataAccessor accessor =
new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor(_gZkClient));
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/05c20e96/helix-core/src/test/java/org/apache/helix/integration/TestSchedulerMessage.java
----------------------------------------------------------------------
diff --git a/helix-core/src/test/java/org/apache/helix/integration/TestSchedulerMessage.java b/helix-core/src/test/java/org/apache/helix/integration/TestSchedulerMessage.java
index 0a1e76a..90d317c 100644
--- a/helix-core/src/test/java/org/apache/helix/integration/TestSchedulerMessage.java
+++ b/helix-core/src/test/java/org/apache/helix/integration/TestSchedulerMessage.java
@@ -535,14 +535,14 @@ public class TestSchedulerMessage extends ZkStandAloneCMTestBaseWithPropertyServ
keyBuilder.controllerTaskStatus(MessageType.SCHEDULER_MSG.toString(), msgId);
ZNRecord statusUpdate = helixDataAccessor.getProperty(controllerTaskStatus).getRecord();
Assert.assertTrue(statusUpdate.getMapField("SentMessageCount").get("MessageCount")
- .equals("" + (_PARTITIONS * 3 / 5)));
+ .equals("" + avgReplicas));
int messageResultCount = 0;
for (String key : statusUpdate.getMapFields().keySet()) {
if (key.startsWith("MessageResult")) {
messageResultCount++;
}
}
- Assert.assertEquals(messageResultCount, _PARTITIONS * 3 / 5);
+ Assert.assertEquals(messageResultCount, avgReplicas);
int count = 0;
// System.out.println(i);
@@ -552,10 +552,7 @@ public class TestSchedulerMessage extends ZkStandAloneCMTestBaseWithPropertyServ
}
// System.out.println(count);
- if (count != avgReplicas * (i + 1)) {
- System.out.println("count: " + count + ", expect: " + avgReplicas * (i + 1));
- }
- // Assert.assertEquals(count, _PARTITIONS * 3 / 5 * (i + 1));
+ Assert.assertEquals(count, avgReplicas * (i + 1));
}
}
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/05c20e96/helix-core/src/test/java/org/apache/helix/integration/TestStateTransitionTimeout.java
----------------------------------------------------------------------
diff --git a/helix-core/src/test/java/org/apache/helix/integration/TestStateTransitionTimeout.java b/helix-core/src/test/java/org/apache/helix/integration/TestStateTransitionTimeout.java
index 7500003..4fff890 100644
--- a/helix-core/src/test/java/org/apache/helix/integration/TestStateTransitionTimeout.java
+++ b/helix-core/src/test/java/org/apache/helix/integration/TestStateTransitionTimeout.java
@@ -31,6 +31,9 @@ import org.apache.helix.NotificationContext;
import org.apache.helix.PropertyKey.Builder;
import org.apache.helix.TestHelper;
import org.apache.helix.TestHelper.StartCMResult;
+import org.apache.helix.api.ParticipantId;
+import org.apache.helix.api.PartitionId;
+import org.apache.helix.api.State;
import org.apache.helix.controller.HelixControllerMain;
import org.apache.helix.manager.zk.ZNRecordSerializer;
import org.apache.helix.manager.zk.ZkClient;
@@ -176,9 +179,9 @@ public class TestStateTransitionTimeout extends ZkStandAloneCMTestBase {
String instanceName = PARTICIPANT_PREFIX + "_" + (START_PORT + i);
SleepStateModelFactory factory = new SleepStateModelFactory(1000);
factories.put(instanceName, factory);
- for (String p : idealState.getPartitionStringSet()) {
- if (idealState.getPreferenceList(p).get(0).equals(instanceName)) {
- factory.addPartition(p);
+ for (PartitionId p : idealState.getPartitionSet()) {
+ if (idealState.getPreferenceList(p).get(0).equals(ParticipantId.from(instanceName))) {
+ factory.addPartition(p.stringify());
}
}
@@ -198,11 +201,11 @@ public class TestStateTransitionTimeout extends ZkStandAloneCMTestBase {
Builder kb = accessor.keyBuilder();
ExternalView ev = accessor.getProperty(kb.externalView(TEST_DB));
- for (String p : idealState.getPartitionStringSet()) {
- String idealMaster = idealState.getPreferenceList(p).get(0);
- Assert.assertTrue(ev.getStateMap(p).get(idealMaster).equals("ERROR"));
+ for (PartitionId p : idealState.getPartitionSet()) {
+ ParticipantId idealMaster = idealState.getPreferenceList(p).get(0);
+ Assert.assertTrue(ev.getStateMap(p).get(idealMaster).equals(State.from("ERROR")));
- TimeOutStateModel model = factories.get(idealMaster).getStateModel(p);
+ TimeOutStateModel model = factories.get(idealMaster.stringify()).getStateModel(p.stringify());
Assert.assertEquals(model._errorCallcount, 1);
Assert.assertEquals(model._error.getCode(), ErrorCode.TIMEOUT);
}
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/05c20e96/helix-core/src/test/java/org/apache/helix/integration/TestZkReconnect.java
----------------------------------------------------------------------
diff --git a/helix-core/src/test/java/org/apache/helix/integration/TestZkReconnect.java b/helix-core/src/test/java/org/apache/helix/integration/TestZkReconnect.java
index e2c04d6..2be136d 100644
--- a/helix-core/src/test/java/org/apache/helix/integration/TestZkReconnect.java
+++ b/helix-core/src/test/java/org/apache/helix/integration/TestZkReconnect.java
@@ -35,6 +35,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.StateModelFactoryId;
import org.apache.helix.model.IdealState;
import org.apache.helix.model.Message;
import org.apache.helix.participant.statemachine.StateModel;
@@ -100,7 +101,7 @@ public class TestZkReconnect {
IdealState idealState = helixAdmin.getResourceIdealState(clusterName, resourceName);
idealState.setReplicas("1");
- idealState.setStateModelFactoryName("test");
+ idealState.setStateModelFactoryId(StateModelFactoryId.from("test"));
idealState.setPartitionState(PartitionId.from(ResourceId.from(resourceName), "0"),
ParticipantId.from(instanceId), State.from("ONLINE"));
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/05c20e96/helix-core/src/test/java/org/apache/helix/manager/zk/TestZNRecordSizeLimit.java
----------------------------------------------------------------------
diff --git a/helix-core/src/test/java/org/apache/helix/manager/zk/TestZNRecordSizeLimit.java b/helix-core/src/test/java/org/apache/helix/manager/zk/TestZNRecordSizeLimit.java
index 5d09a92..cd65a42 100644
--- a/helix-core/src/test/java/org/apache/helix/manager/zk/TestZNRecordSizeLimit.java
+++ b/helix-core/src/test/java/org/apache/helix/manager/zk/TestZNRecordSizeLimit.java
@@ -27,6 +27,7 @@ import org.apache.helix.HelixProperty;
import org.apache.helix.PropertyKey.Builder;
import org.apache.helix.ZNRecord;
import org.apache.helix.ZkUnitTestBase;
+import org.apache.helix.api.StateModelDefId;
import org.apache.helix.model.IdealState;
import org.apache.helix.model.IdealState.RebalanceMode;
import org.apache.helix.model.InstanceConfig;
@@ -108,11 +109,11 @@ public class TestZNRecordSizeLimit extends ZkUnitTestBase {
// oversized data should not create any new data on zk
ZKHelixDataAccessor accessor =
- new ZKHelixDataAccessor(className, new ZkBaseDataAccessor(zkClient));
+ new ZKHelixDataAccessor(className, new ZkBaseDataAccessor<ZNRecord>(zkClient));
Builder keyBuilder = accessor.keyBuilder();
IdealState idealState = new IdealState("currentState");
- idealState.setStateModelDefRef("MasterSlave");
+ idealState.setStateModelDefId(StateModelDefId.from("MasterSlave"));
idealState.setRebalanceMode(RebalanceMode.SEMI_AUTO);
idealState.setNumPartitions(10);
@@ -128,7 +129,7 @@ public class TestZNRecordSizeLimit extends ZkUnitTestBase {
// legal sized data gets written to zk
idealState.getRecord().getSimpleFields().clear();
- idealState.setStateModelDefRef("MasterSlave");
+ idealState.setStateModelDefId(StateModelDefId.from("MasterSlave"));
idealState.setRebalanceMode(RebalanceMode.SEMI_AUTO);
idealState.setNumPartitions(10);
@@ -142,7 +143,7 @@ public class TestZNRecordSizeLimit extends ZkUnitTestBase {
// oversized data should not update existing data on zk
idealState.getRecord().getSimpleFields().clear();
- idealState.setStateModelDefRef("MasterSlave");
+ idealState.setStateModelDefId(StateModelDefId.from("MasterSlave"));
idealState.setRebalanceMode(RebalanceMode.SEMI_AUTO);
idealState.setNumPartitions(10);
for (int i = 900; i < 1024; i++) {
@@ -231,12 +232,12 @@ public class TestZNRecordSizeLimit extends ZkUnitTestBase {
// oversized data should not create any new data on zk
ZKHelixDataAccessor accessor =
- new ZKHelixDataAccessor(className, new ZkBaseDataAccessor(zkClient));
+ new ZKHelixDataAccessor(className, new ZkBaseDataAccessor<ZNRecord>(zkClient));
Builder keyBuilder = accessor.keyBuilder();
// ZNRecord statusUpdates = new ZNRecord("statusUpdates");
IdealState idealState = new IdealState("currentState");
- idealState.setStateModelDefRef("MasterSlave");
+ idealState.setStateModelDefId(StateModelDefId.from("MasterSlave"));
idealState.setRebalanceMode(RebalanceMode.SEMI_AUTO);
idealState.setNumPartitions(10);
@@ -250,7 +251,7 @@ public class TestZNRecordSizeLimit extends ZkUnitTestBase {
// legal sized data gets written to zk
idealState.getRecord().getSimpleFields().clear();
- idealState.setStateModelDefRef("MasterSlave");
+ idealState.setStateModelDefId(StateModelDefId.from("MasterSlave"));
idealState.setRebalanceMode(RebalanceMode.SEMI_AUTO);
idealState.setNumPartitions(10);
@@ -264,7 +265,7 @@ public class TestZNRecordSizeLimit extends ZkUnitTestBase {
// oversized data should not update existing data on zk
idealState.getRecord().getSimpleFields().clear();
- idealState.setStateModelDefRef("MasterSlave");
+ idealState.setStateModelDefId(StateModelDefId.from("MasterSlave"));
idealState.setRebalanceMode(RebalanceMode.SEMI_AUTO);
idealState.setNumPartitions(10);
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/05c20e96/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 908a248..187a077 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
@@ -33,6 +33,7 @@ import org.apache.helix.PropertyType;
import org.apache.helix.TestHelper;
import org.apache.helix.ZNRecord;
import org.apache.helix.ZkUnitTestBase;
+import org.apache.helix.api.ConstraintId;
import org.apache.helix.model.ClusterConstraints;
import org.apache.helix.model.ClusterConstraints.ConstraintAttribute;
import org.apache.helix.model.ClusterConstraints.ConstraintType;
@@ -270,7 +271,7 @@ public class TestZkHelixAdmin extends ZkUnitTestBase {
constraints =
accessor.getProperty(keyBuilder.constraint(ConstraintType.MESSAGE_CONSTRAINT.toString()));
Assert.assertNotNull(constraints, "message-constraint should exist");
- ConstraintItem item = constraints.getConstraintItem("constraint1");
+ ConstraintItem item = constraints.getConstraintItem(ConstraintId.from("constraint1"));
Assert.assertNotNull(item, "message-constraint for constraint1 should exist");
Assert.assertEquals(item.getConstraintValue(), "1");
Assert.assertEquals(item.getAttributeValue(ConstraintAttribute.RESOURCE), "MyDB");
@@ -278,7 +279,7 @@ public class TestZkHelixAdmin extends ZkUnitTestBase {
// test admin.getMessageConstraints()
constraints = tool.getConstraints(clusterName, ConstraintType.MESSAGE_CONSTRAINT);
Assert.assertNotNull(constraints, "message-constraint should exist");
- item = constraints.getConstraintItem("constraint1");
+ item = constraints.getConstraintItem(ConstraintId.from("constraint1"));
Assert.assertNotNull(item, "message-constraint for constraint1 should exist");
Assert.assertEquals(item.getConstraintValue(), "1");
Assert.assertEquals(item.getAttributeValue(ConstraintAttribute.RESOURCE), "MyDB");
@@ -288,7 +289,7 @@ public class TestZkHelixAdmin extends ZkUnitTestBase {
constraints =
accessor.getProperty(keyBuilder.constraint(ConstraintType.MESSAGE_CONSTRAINT.toString()));
Assert.assertNotNull(constraints, "message-constraint should exist");
- item = constraints.getConstraintItem("constraint1");
+ item = constraints.getConstraintItem(ConstraintId.from("constraint1"));
Assert.assertNull(item, "message-constraint for constraint1 should NOT exist");
System.out.println("END " + clusterName + " at " + new Date(System.currentTimeMillis()));
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/05c20e96/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 2045526..170578a 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
@@ -29,6 +29,9 @@ import java.util.Map;
import java.util.Set;
import org.apache.helix.TestHelper;
+import org.apache.helix.api.ParticipantId;
+import org.apache.helix.api.PartitionId;
+import org.apache.helix.api.StateModelDefId;
import org.apache.helix.model.IdealState.IdealStateModeProperty;
import org.apache.helix.model.IdealState.RebalanceMode;
import org.testng.Assert;
@@ -52,24 +55,28 @@ public class TestIdealState {
// test SEMI_AUTO mode
idealState.setRebalanceMode(RebalanceMode.SEMI_AUTO);
- Set<String> instances = idealState.getInstanceSet("TestDB_0");
+ Set<ParticipantId> instances = idealState.getParticipantSet(PartitionId.from("TestDB_0"));
// System.out.println("instances: " + instances);
Assert.assertEquals(instances.size(), 2, "Should contain node_1 and node_2");
- Assert.assertTrue(instances.contains("node_1"), "Should contain node_1 and node_2");
- Assert.assertTrue(instances.contains("node_2"), "Should contain node_1 and node_2");
+ Assert.assertTrue(instances.contains(ParticipantId.from("node_1")),
+ "Should contain node_1 and node_2");
+ Assert.assertTrue(instances.contains(ParticipantId.from("node_2")),
+ "Should contain node_1 and node_2");
- instances = idealState.getInstanceSet("TestDB_nonExist_auto");
+ instances = idealState.getParticipantSet(PartitionId.from("TestDB_nonExist_auto"));
Assert.assertEquals(instances, Collections.emptySet(), "Should get empty set");
// test CUSTOMIZED mode
idealState.setRebalanceMode(RebalanceMode.CUSTOMIZED);
- instances = idealState.getInstanceSet("TestDB_1");
+ instances = idealState.getParticipantSet(PartitionId.from("TestDB_1"));
// System.out.println("instances: " + instances);
Assert.assertEquals(instances.size(), 2, "Should contain node_3 and node_4");
- Assert.assertTrue(instances.contains("node_3"), "Should contain node_3 and node_4");
- Assert.assertTrue(instances.contains("node_4"), "Should contain node_3 and node_4");
+ Assert.assertTrue(instances.contains(ParticipantId.from("node_3")),
+ "Should contain node_3 and node_4");
+ Assert.assertTrue(instances.contains(ParticipantId.from("node_4")),
+ "Should contain node_3 and node_4");
- instances = idealState.getInstanceSet("TestDB_nonExist_custom");
+ instances = idealState.getParticipantSet(PartitionId.from("TestDB_nonExist_custom"));
Assert.assertEquals(instances, Collections.emptySet(), "Should get empty set");
System.out.println("END " + testName + " at " + new Date(System.currentTimeMillis()));
@@ -80,7 +87,7 @@ public class TestIdealState {
IdealState idealState = new IdealState("test-db");
idealState.setRebalanceMode(RebalanceMode.SEMI_AUTO);
idealState.setNumPartitions(4);
- idealState.setStateModelDefRef("MasterSlave");
+ idealState.setStateModelDefId(StateModelDefId.from("MasterSlave"));
idealState.setReplicas("" + 2);
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/05c20e96/helix-core/src/test/java/org/apache/helix/tools/TestHelixAdminCli.java
----------------------------------------------------------------------
diff --git a/helix-core/src/test/java/org/apache/helix/tools/TestHelixAdminCli.java b/helix-core/src/test/java/org/apache/helix/tools/TestHelixAdminCli.java
index 6935de4..a9679de 100644
--- a/helix-core/src/test/java/org/apache/helix/tools/TestHelixAdminCli.java
+++ b/helix-core/src/test/java/org/apache/helix/tools/TestHelixAdminCli.java
@@ -29,6 +29,8 @@ import org.apache.helix.BaseDataAccessor;
import org.apache.helix.HelixDataAccessor;
import org.apache.helix.TestHelper;
import org.apache.helix.ZNRecord;
+import org.apache.helix.api.ParticipantId;
+import org.apache.helix.api.PartitionId;
import org.apache.helix.controller.HelixControllerMain;
import org.apache.helix.integration.ZkIntegrationTestBase;
import org.apache.helix.manager.zk.ZKHelixDataAccessor;
@@ -651,13 +653,13 @@ public class TestHelixAdminCli extends ZkIntegrationTestBase {
ClusterSetup.processCommandLineArgs(command.split("\\s+"));
IdealState dbIs = accessor.getProperty(accessor.keyBuilder().idealState("db_11"));
- Set<String> hosts = new HashSet<String>();
- for (String p : dbIs.getPartitionStringSet()) {
- for (String hostName : dbIs.getInstanceStateMap(p).keySet()) {
+ Set<ParticipantId> hosts = new HashSet<ParticipantId>();
+ for (PartitionId p : dbIs.getPartitionSet()) {
+ for (ParticipantId participantId : dbIs.getParticipantStateMap(p).keySet()) {
InstanceConfig config =
- accessor.getProperty(accessor.keyBuilder().instanceConfig(hostName));
+ accessor.getProperty(accessor.keyBuilder().instanceConfig(participantId.stringify()));
Assert.assertTrue(config.containsTag("tag1"));
- hosts.add(hostName);
+ hosts.add(participantId);
}
}
Assert.assertEquals(hosts.size(), 2);
@@ -673,13 +675,13 @@ public class TestHelixAdminCli extends ZkIntegrationTestBase {
ClusterSetup.processCommandLineArgs(command.split("\\s+"));
dbIs = accessor.getProperty(accessor.keyBuilder().idealState("db_11"));
- hosts = new HashSet<String>();
- for (String p : dbIs.getPartitionStringSet()) {
- for (String hostName : dbIs.getInstanceStateMap(p).keySet()) {
+ hosts = new HashSet<ParticipantId>();
+ for (PartitionId p : dbIs.getPartitionSet()) {
+ for (ParticipantId participantId : dbIs.getParticipantStateMap(p).keySet()) {
InstanceConfig config =
- accessor.getProperty(accessor.keyBuilder().instanceConfig(hostName));
+ accessor.getProperty(accessor.keyBuilder().instanceConfig(participantId.stringify()));
Assert.assertTrue(config.containsTag("tag2"));
- hosts.add(hostName);
+ hosts.add(participantId);
}
}
Assert.assertEquals(hosts.size(), 4);
@@ -701,13 +703,13 @@ public class TestHelixAdminCli extends ZkIntegrationTestBase {
command = "-zkSvr localhost:2183 -rebalance " + clusterName + " db_11 3 -instanceGroupTag tag2";
ClusterSetup.processCommandLineArgs(command.split("\\s+"));
dbIs = accessor.getProperty(accessor.keyBuilder().idealState("db_11"));
- hosts = new HashSet<String>();
- for (String p : dbIs.getPartitionStringSet()) {
- for (String hostName : dbIs.getInstanceStateMap(p).keySet()) {
+ hosts = new HashSet<ParticipantId>();
+ for (PartitionId p : dbIs.getPartitionSet()) {
+ for (ParticipantId participantId : dbIs.getParticipantStateMap(p).keySet()) {
InstanceConfig config =
- accessor.getProperty(accessor.keyBuilder().instanceConfig(hostName));
+ accessor.getProperty(accessor.keyBuilder().instanceConfig(participantId.stringify()));
Assert.assertTrue(config.containsTag("tag2"));
- hosts.add(hostName);
+ hosts.add(participantId);
}
}
Assert.assertEquals(hosts.size(), 3);
[2/2] git commit: [HELIX-98] clean up setting constraint api,
[HELIX-246] Refactor scheduler task config to comply with new
rebalancer config and fix related scheduler task tests
Posted by zz...@apache.org.
[HELIX-98] clean up setting constraint api, [HELIX-246] Refactor scheduler task config to comply with new rebalancer config and fix related scheduler task tests
Project: http://git-wip-us.apache.org/repos/asf/incubator-helix/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-helix/commit/05c20e96
Tree: http://git-wip-us.apache.org/repos/asf/incubator-helix/tree/05c20e96
Diff: http://git-wip-us.apache.org/repos/asf/incubator-helix/diff/05c20e96
Branch: refs/heads/helix-logical-model
Commit: 05c20e96bac75f237ad5f1388244397e3207c3ad
Parents: 6a0abbc
Author: zzhang <zz...@apache.org>
Authored: Wed Sep 18 13:10:51 2013 -0700
Committer: zzhang <zz...@apache.org>
Committed: Wed Sep 18 13:10:51 2013 -0700
----------------------------------------------------------------------
.../main/java/org/apache/helix/api/Cluster.java | 40 ++-
.../org/apache/helix/api/ClusterAccessor.java | 2 +-
.../org/apache/helix/api/ClusterConfig.java | 245 ++++++++++++++++++-
.../java/org/apache/helix/api/ConstraintId.java | 74 ++++++
.../helix/api/CustomRebalancerConfig.java | 10 +
.../helix/api/FullAutoRebalancerConfig.java | 8 +
.../apache/helix/api/ParticipantAccessor.java | 5 +-
.../java/org/apache/helix/api/PartitionId.java | 8 +
.../org/apache/helix/api/RebalancerConfig.java | 24 ++
.../main/java/org/apache/helix/api/Scope.java | 31 +++
.../helix/api/SemiAutoRebalancerConfig.java | 10 +
.../helix/api/UserDefinedRebalancerConfig.java | 9 +
.../controller/rebalancer/AutoRebalancer.java | 1 +
.../controller/rebalancer/CustomRebalancer.java | 4 +-
.../rebalancer/NewAutoRebalancer.java | 25 +-
.../rebalancer/NewSemiAutoRebalancer.java | 6 +-
.../helix/controller/rebalancer/Rebalancer.java | 1 +
.../rebalancer/SemiAutoRebalancer.java | 1 +
.../util/ConstraintBasedAssignment.java | 1 +
.../util/NewConstraintBasedAssignment.java | 29 ++-
.../stages/BestPossibleStateCalcStage.java | 4 +-
.../stages/NewBestPossibleStateCalcStage.java | 3 +-
.../stages/NewResourceComputationStage.java | 160 ++++++++----
.../stages/RebalanceIdealStateStage.java | 4 +-
.../stages/StatsAggregationStage.java | 2 +-
.../strategy/EspressoRelayStrategy.java | 3 +-
.../DefaultSchedulerMessageHandlerFactory.java | 6 +-
.../apache/helix/manager/zk/ZKHelixAdmin.java | 39 +--
.../apache/helix/model/ClusterConstraints.java | 38 ++-
.../org/apache/helix/model/ExternalView.java | 3 +
.../java/org/apache/helix/model/IdealState.java | 29 +--
.../builder/ClusterConstraintsBuilder.java | 11 +-
.../helix/model/builder/IdealStateBuilder.java | 6 +-
.../builder/MessageConstraintItemBuilder.java | 107 ++++++++
.../builder/StateConstraintItemBuilder.java | 92 +++++++
.../monitoring/mbeans/ResourceMonitor.java | 18 +-
.../participant/HelixCustomCodeRunner.java | 10 +-
.../org/apache/helix/tools/ClusterSetup.java | 10 +-
.../org/apache/helix/util/RebalanceUtil.java | 25 +-
.../java/org/apache/helix/TestZKCallback.java | 2 +-
.../java/org/apache/helix/ZkUnitTestBase.java | 25 +-
.../helix/controller/stages/BaseStageTest.java | 2 +-
.../TestBestPossibleCalcStageCompatibility.java | 9 +-
.../stages/TestCompatibilityCheckStage.java | 3 +-
.../stages/TestResourceComputationStage.java | 7 +-
.../strategy/TestNewAutoRebalanceStrategy.java | 17 +-
.../TestAddStateModelFactoryAfterConnect.java | 3 +-
.../helix/integration/TestAutoRebalance.java | 15 +-
.../TestAutoRebalancePartitionLimit.java | 13 +-
.../TestCustomizedIdealStateRebalancer.java | 16 +-
.../integration/TestInvalidAutoIdealState.java | 3 +-
.../helix/integration/TestRenamePartition.java | 3 +-
.../helix/integration/TestSchedulerMessage.java | 9 +-
.../integration/TestStateTransitionTimeout.java | 17 +-
.../helix/integration/TestZkReconnect.java | 3 +-
.../helix/manager/zk/TestZNRecordSizeLimit.java | 17 +-
.../helix/manager/zk/TestZkHelixAdmin.java | 7 +-
.../org/apache/helix/model/TestIdealState.java | 25 +-
.../apache/helix/tools/TestHelixAdminCli.java | 32 +--
59 files changed, 1068 insertions(+), 264 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/05c20e96/helix-core/src/main/java/org/apache/helix/api/Cluster.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/api/Cluster.java b/helix-core/src/main/java/org/apache/helix/api/Cluster.java
index 005ae0d..56a84e9 100644
--- a/helix-core/src/main/java/org/apache/helix/api/Cluster.java
+++ b/helix-core/src/main/java/org/apache/helix/api/Cluster.java
@@ -25,6 +25,7 @@ import java.util.Map;
import org.apache.helix.model.ClusterConstraints;
import org.apache.helix.model.ClusterConstraints.ConstraintType;
import org.apache.helix.model.StateModelDefinition;
+import org.apache.helix.model.Transition;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableMap;
@@ -99,8 +100,10 @@ public class Cluster {
}
});
_config =
- new ClusterConfig(id, resourceConfigMap, participantConfigMap, constraintMap,
- stateModelMap, userConfig, isPaused);
+ new ClusterConfig.Builder(id).addResources(resourceConfigMap.values())
+ .addParticipants(participantConfigMap.values()).addConstraints(constraintMap.values())
+ .addStateModelDefinitions(stateModelMap.values()).setPausedStatus(isPaused)
+ .userConfig(userConfig).build();
_resourceMap = ImmutableMap.copyOf(resourceMap);
@@ -222,10 +225,43 @@ public class Cluster {
}
/**
+ * Get the maximum number of participants that can be in a state
+ * @param scope the scope for the bound
+ * @param stateModelDefId the state model of the state
+ * @param state the constrained state
+ * @return The upper bound, which can be "-1" if unspecified, a numerical upper bound, "R" for
+ * number of replicas, or "N" for number of participants
+ */
+ public String getStateUpperBoundConstraint(Scope<?> scope, StateModelDefId stateModelDefId,
+ State state) {
+ return _config.getStateUpperBoundConstraint(scope, stateModelDefId, state);
+ }
+
+ /**
+ * Get the limit of simultaneous execution of a transition
+ * @param scope the scope under which the transition is constrained
+ * @param stateModelDefId the state model of which the transition is a part
+ * @param transition the constrained transition
+ * @return the limit, or Integer.MAX_VALUE if there is no limit
+ */
+ public int getTransitionConstraint(Scope<?> scope, StateModelDefId stateModelDefId,
+ Transition transition) {
+ return _config.getTransitionConstraint(scope, stateModelDefId, transition);
+ }
+
+ /**
* Check the pasued status of the cluster
* @return true if paused, false otherwise
*/
public boolean isPaused() {
return _config.isPaused();
}
+
+ /**
+ * Get the ClusterConfig specifying this cluster
+ * @return ClusterConfig
+ */
+ public ClusterConfig getConfig() {
+ return _config;
+ }
}
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/05c20e96/helix-core/src/main/java/org/apache/helix/api/ClusterAccessor.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/api/ClusterAccessor.java b/helix-core/src/main/java/org/apache/helix/api/ClusterAccessor.java
index a46edad..e30825c 100644
--- a/helix-core/src/main/java/org/apache/helix/api/ClusterAccessor.java
+++ b/helix-core/src/main/java/org/apache/helix/api/ClusterAccessor.java
@@ -66,7 +66,7 @@ public class ClusterAccessor {
public boolean createCluster(ClusterConfig cluster) {
boolean created = _accessor.createProperty(_keyBuilder.cluster(), null);
if (!created) {
- LOG.warn("Cluster already created. Aborting.");
+ // LOG.warn("Cluster already created. Aborting.");
// return false;
}
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/05c20e96/helix-core/src/main/java/org/apache/helix/api/ClusterConfig.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/api/ClusterConfig.java b/helix-core/src/main/java/org/apache/helix/api/ClusterConfig.java
index 27da37d..ba1e78e 100644
--- a/helix-core/src/main/java/org/apache/helix/api/ClusterConfig.java
+++ b/helix-core/src/main/java/org/apache/helix/api/ClusterConfig.java
@@ -3,12 +3,21 @@ package org.apache.helix.api;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
+import java.util.Set;
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.ClusterConstraints.ConstraintValue;
+import org.apache.helix.model.ConstraintItem;
+import org.apache.helix.model.Message.MessageType;
import org.apache.helix.model.StateModelDefinition;
+import org.apache.helix.model.Transition;
+import org.apache.helix.model.builder.ConstraintItemBuilder;
+import org.apache.log4j.Logger;
import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Maps;
/*
* Licensed to the Apache Software Foundation (ASF) under one
@@ -33,6 +42,8 @@ import com.google.common.collect.ImmutableMap;
* Configuration properties of a cluster
*/
public class ClusterConfig {
+ private static final Logger LOG = Logger.getLogger(ClusterConfig.class);
+
private final ClusterId _id;
private final Map<ResourceId, ResourceConfig> _resourceMap;
private final Map<ParticipantId, ParticipantConfig> _participantMap;
@@ -51,7 +62,7 @@ public class ClusterConfig {
* @param userConfig user-defined cluster properties
* @param isPaused true if paused, false if active
*/
- public ClusterConfig(ClusterId id, Map<ResourceId, ResourceConfig> resourceMap,
+ private ClusterConfig(ClusterId id, Map<ResourceId, ResourceConfig> resourceMap,
Map<ParticipantId, ParticipantConfig> participantMap,
Map<ConstraintType, ClusterConstraints> constraintMap,
Map<StateModelDefId, StateModelDefinition> stateModelMap, UserConfig userConfig,
@@ -90,6 +101,105 @@ public class ClusterConfig {
}
/**
+ * Get the maximum number of participants that can be in a state
+ * @param scope the scope for the bound
+ * @param stateModelDefId the state model of the state
+ * @param state the constrained state
+ * @return The upper bound, which can be "-1" if unspecified, a numerical upper bound, "R" for
+ * number of replicas, or "N" for number of participants
+ */
+ public String getStateUpperBoundConstraint(Scope<?> scope, StateModelDefId stateModelDefId,
+ State state) {
+ // set up attributes to match based on the scope
+ ClusterConstraints stateConstraints = getConstraintMap().get(ConstraintType.STATE_CONSTRAINT);
+ Map<ConstraintAttribute, String> matchAttributes = Maps.newHashMap();
+ matchAttributes.put(ConstraintAttribute.STATE, state.toString());
+ matchAttributes.put(ConstraintAttribute.STATE_MODEL, stateModelDefId.toString());
+ switch (scope.getType()) {
+ case CLUSTER:
+ // cluster is implicit
+ break;
+ case RESOURCE:
+ matchAttributes.put(ConstraintAttribute.RESOURCE, scope.getScopedId().stringify());
+ break;
+ default:
+ LOG.error("Unsupported scope for state constraint: " + scope);
+ return "-1";
+ }
+ Set<ConstraintItem> matches = stateConstraints.match(matchAttributes);
+ int value = -1;
+ for (ConstraintItem item : matches) {
+ // match: if an R or N is found, always choose that one
+ // otherwise, take the minimum of the counts specified in the constraints
+ String constraintValue = item.getConstraintValue();
+ if (constraintValue != null) {
+ if (constraintValue.equals(ConstraintValue.N.toString())
+ || constraintValue.equals(ConstraintValue.R.toString())) {
+ return constraintValue;
+ } else {
+ try {
+ int current = Integer.parseInt(constraintValue);
+ if (value == -1 || current < value) {
+ value = current;
+ }
+ } catch (NumberFormatException e) {
+ LOG.error("Invalid state upper bound: " + constraintValue);
+ }
+ }
+ }
+ }
+ return Integer.toString(value);
+ }
+
+ /**
+ * Get the limit of simultaneous execution of a transition
+ * @param scope the scope under which the transition is constrained
+ * @param stateModelDefId the state model of which the transition is a part
+ * @param transition the constrained transition
+ * @return the limit, or Integer.MAX_VALUE if there is no limit
+ */
+ public int getTransitionConstraint(Scope<?> scope, StateModelDefId stateModelDefId,
+ Transition transition) {
+ // set up attributes to match based on the scope
+ ClusterConstraints transitionConstraints =
+ getConstraintMap().get(ConstraintType.MESSAGE_CONSTRAINT);
+ Map<ConstraintAttribute, String> matchAttributes = Maps.newHashMap();
+ matchAttributes.put(ConstraintAttribute.STATE_MODEL, stateModelDefId.toString());
+ matchAttributes.put(ConstraintAttribute.MESSAGE_TYPE, MessageType.STATE_TRANSITION.toString());
+ matchAttributes.put(ConstraintAttribute.TRANSITION, transition.toString());
+ switch (scope.getType()) {
+ case CLUSTER:
+ // cluster is implicit
+ break;
+ case RESOURCE:
+ matchAttributes.put(ConstraintAttribute.RESOURCE, scope.getScopedId().stringify());
+ break;
+ case PARTICIPANT:
+ matchAttributes.put(ConstraintAttribute.INSTANCE, scope.getScopedId().stringify());
+ break;
+ default:
+ LOG.error("Unsupported scope for transition constraints: " + scope);
+ return Integer.MAX_VALUE;
+ }
+ Set<ConstraintItem> matches = transitionConstraints.match(matchAttributes);
+ int value = Integer.MAX_VALUE;
+ for (ConstraintItem item : matches) {
+ String constraintValue = item.getConstraintValue();
+ if (constraintValue != null) {
+ try {
+ int current = Integer.parseInt(constraintValue);
+ if (current < value) {
+ value = current;
+ }
+ } catch (NumberFormatException e) {
+ LOG.error("Invalid in-flight transition cap: " + constraintValue);
+ }
+ }
+ }
+ return value;
+ }
+
+ /**
* Get participants of the cluster
* @return a map of participant id to participant, or empty map if none
*/
@@ -197,7 +307,11 @@ public class ClusterConfig {
* @return Builder
*/
public Builder addConstraint(ClusterConstraints constraint) {
- _constraintMap.put(constraint.getType(), constraint);
+ ClusterConstraints existConstraints = getConstraintsInstance(constraint.getType());
+ for (ConstraintId constraintId : constraint.getConstraintItems().keySet()) {
+ existConstraints
+ .addConstraintItem(constraintId, constraint.getConstraintItem(constraintId));
+ }
return this;
}
@@ -214,12 +328,121 @@ public class ClusterConfig {
}
/**
- * Add a constraint to the cluster
- * @param constraint cluster constraint of a specific type
+ * Add a constraint on the maximum number of in-flight transitions of a certain type
+ * @param scope scope of the constraint
+ * @param constraintId unique constraint identifier
+ * @param stateModelDefId identifies the state model containing the transition
+ * @param transition the transition to constrain
+ * @param maxInFlightTransitions number of allowed in-flight transitions in the scope
+ * @return Builder
+ */
+ public Builder addTransitionConstraint(Scope<?> scope, StateModelDefId stateModelDefId,
+ Transition transition, int maxInFlightTransitions) {
+ Map<String, String> attributes = Maps.newHashMap();
+ attributes.put(ConstraintAttribute.MESSAGE_TYPE.toString(),
+ MessageType.STATE_TRANSITION.toString());
+ attributes.put(ConstraintAttribute.CONSTRAINT_VALUE.toString(),
+ Integer.toString(maxInFlightTransitions));
+ attributes.put(ConstraintAttribute.TRANSITION.toString(), transition.toString());
+ attributes.put(ConstraintAttribute.STATE_MODEL.toString(), stateModelDefId.stringify());
+ switch (scope.getType()) {
+ case CLUSTER:
+ // cluster is implicit
+ break;
+ case RESOURCE:
+ attributes.put(ConstraintAttribute.RESOURCE.toString(), scope.getScopedId().stringify());
+ break;
+ case PARTICIPANT:
+ attributes.put(ConstraintAttribute.INSTANCE.toString(), scope.getScopedId().stringify());
+ break;
+ default:
+ LOG.error("Unsupported scope for adding a transition constraint: " + scope);
+ return this;
+ }
+ ConstraintItem item = new ConstraintItemBuilder().addConstraintAttributes(attributes).build();
+ ClusterConstraints constraints = getConstraintsInstance(ConstraintType.MESSAGE_CONSTRAINT);
+ constraints.addConstraintItem(ConstraintId.from(scope, stateModelDefId, transition), item);
+ return this;
+ }
+
+ /**
+ * Add a state upper bound constraint
+ * @param scope scope under which the constraint is valid
+ * @param stateModelDefId identifier of the state model that owns the state
+ * @param state the state to constrain
+ * @param upperBound maximum number of replicas per partition in the state
+ * @return
+ */
+ public Builder addStateUpperBoundConstraint(Scope<?> scope, StateModelDefId stateModelDefId,
+ State state, int upperBound) {
+ return addStateUpperBoundConstraint(scope, stateModelDefId, state,
+ Integer.toString(upperBound));
+ }
+
+ /**
+ * Add a state upper bound constraint
+ * @param scope scope under which the constraint is valid
+ * @param stateModelDefId identifier of the state model that owns the state
+ * @param state the state to constrain
+ * @param dynamicUpperBound the upper bound of replicas per partition in the state, can be a
+ * number, or the currently supported special bound values:<br />
+ * "R" - Refers to the number of replicas specified during resource
+ * creation. This allows having different replication factor for each
+ * resource without having to create a different state machine. <br />
+ * "N" - Refers to all nodes in the cluster. Useful for resources that need
+ * to exist on all nodes. This way one can add/remove nodes without having
+ * the change the bounds.
+ * @return Builder
+ */
+ public Builder addStateUpperBoundConstraint(Scope<?> scope, StateModelDefId stateModelDefId,
+ State state, String dynamicUpperBound) {
+ Map<String, String> attributes = Maps.newHashMap();
+ attributes.put(ConstraintAttribute.STATE.toString(), state.toString());
+ attributes.put(ConstraintAttribute.STATE_MODEL.toString(), stateModelDefId.stringify());
+ attributes.put(ConstraintAttribute.CONSTRAINT_VALUE.toString(), dynamicUpperBound);
+ switch (scope.getType()) {
+ case CLUSTER:
+ // cluster is implicit
+ break;
+ case RESOURCE:
+ attributes.put(ConstraintAttribute.RESOURCE.toString(), scope.getScopedId().stringify());
+ break;
+ default:
+ LOG.error("Unsupported scope for adding a state constraint: " + scope);
+ return this;
+ }
+ ConstraintItem item = new ConstraintItemBuilder().addConstraintAttributes(attributes).build();
+ ClusterConstraints constraints = getConstraintsInstance(ConstraintType.STATE_CONSTRAINT);
+ constraints.addConstraintItem(ConstraintId.from(scope, stateModelDefId, state), item);
+ return this;
+ }
+
+ /**
+ * Add a state model definition to the cluster
+ * @param stateModelDef state model definition of the cluster
* @return Builder
*/
public Builder addStateModelDefinition(StateModelDefinition stateModelDef) {
_stateModelMap.put(stateModelDef.getStateModelDefId(), stateModelDef);
+ // add state constraints from the state model definition
+ for (State state : stateModelDef.getStatesPriorityList()) {
+ if (!stateModelDef.getNumParticipantsPerState(state).equals("-1")) {
+ addStateUpperBoundConstraint(Scope.cluster(_id), stateModelDef.getStateModelDefId(),
+ state, stateModelDef.getNumParticipantsPerState(state));
+ }
+ }
+ return this;
+ }
+
+ /**
+ * Add multiple state model definitions
+ * @param stateModelDefs collection of state model definitions for the cluster
+ * @return Builder
+ */
+ public Builder addStateModelDefinitions(Collection<StateModelDefinition> stateModelDefs) {
+ for (StateModelDefinition stateModelDef : stateModelDefs) {
+ addStateModelDefinition(stateModelDef);
+ }
return this;
}
@@ -251,5 +474,19 @@ public class ClusterConfig {
return new ClusterConfig(_id, _resourceMap, _participantMap, _constraintMap, _stateModelMap,
_userConfig, _isPaused);
}
+
+ /**
+ * Get a valid instance of ClusterConstraints for a type
+ * @param type the type
+ * @return ClusterConstraints
+ */
+ private ClusterConstraints getConstraintsInstance(ConstraintType type) {
+ ClusterConstraints constraints = _constraintMap.get(type);
+ if (constraints == null) {
+ constraints = new ClusterConstraints(type);
+ _constraintMap.put(type, constraints);
+ }
+ return constraints;
+ }
}
}
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/05c20e96/helix-core/src/main/java/org/apache/helix/api/ConstraintId.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/api/ConstraintId.java b/helix-core/src/main/java/org/apache/helix/api/ConstraintId.java
new file mode 100644
index 0000000..85c9430
--- /dev/null
+++ b/helix-core/src/main/java/org/apache/helix/api/ConstraintId.java
@@ -0,0 +1,74 @@
+package org.apache.helix.api;
+
+import org.apache.helix.model.Transition;
+
+/*
+ * 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.
+ */
+
+/**
+ * Identifies a constraint item on the cluster
+ */
+public class ConstraintId extends Id {
+ private final String _constraintId;
+
+ /**
+ * Create a constraint id
+ * @param constraintId string representing the constraint id
+ */
+ private ConstraintId(String constraintId) {
+ _constraintId = constraintId;
+ }
+
+ @Override
+ public String stringify() {
+ return _constraintId;
+ }
+
+ /**
+ * Get a constraint id from a string
+ * @param constraintId string representing the constraint id
+ * @return ConstraintId
+ */
+ public static ConstraintId from(String constraintId) {
+ return new ConstraintId(constraintId);
+ }
+
+ /**
+ * Get a state constraint id based on the state model definition and state
+ * @param scope the scope of the constraint
+ * @param stateModelDefId the state model
+ * @param state the constrained state
+ * @return ConstraintId
+ */
+ public static ConstraintId from(Scope<?> scope, StateModelDefId stateModelDefId, State state) {
+ return new ConstraintId(scope + "|" + stateModelDefId + "|" + state);
+ }
+
+ /**
+ * Get a state constraint id based on the state model definition and transition
+ * @param scope the scope of the constraint
+ * @param stateModelDefId the state model
+ * @param transition the constrained transition
+ * @return ConstraintId
+ */
+ public static ConstraintId from(Scope<?> scope, StateModelDefId stateModelDefId,
+ Transition transition) {
+ return new ConstraintId(scope + "|" + stateModelDefId + "|" + transition);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/05c20e96/helix-core/src/main/java/org/apache/helix/api/CustomRebalancerConfig.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/api/CustomRebalancerConfig.java b/helix-core/src/main/java/org/apache/helix/api/CustomRebalancerConfig.java
index 97d4a45..a5f2bbe 100644
--- a/helix-core/src/main/java/org/apache/helix/api/CustomRebalancerConfig.java
+++ b/helix-core/src/main/java/org/apache/helix/api/CustomRebalancerConfig.java
@@ -112,6 +112,16 @@ public final class CustomRebalancerConfig extends RebalancerConfig {
_preferenceMaps = new HashMap<PartitionId, Map<ParticipantId, State>>();
}
+ /**
+ * Construct builder using an existing custom rebalancer config
+ * @param config
+ */
+ public Builder(CustomRebalancerConfig config) {
+ super(config);
+ _preferenceMaps = new HashMap<PartitionId, Map<ParticipantId, State>>();
+ _preferenceMaps.putAll(config.getPreferenceMaps());
+ }
+
/**
* Add a preference map of a partition
* @param partitionId the partition to set
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/05c20e96/helix-core/src/main/java/org/apache/helix/api/FullAutoRebalancerConfig.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/api/FullAutoRebalancerConfig.java b/helix-core/src/main/java/org/apache/helix/api/FullAutoRebalancerConfig.java
index 3482d16..599d20d 100644
--- a/helix-core/src/main/java/org/apache/helix/api/FullAutoRebalancerConfig.java
+++ b/helix-core/src/main/java/org/apache/helix/api/FullAutoRebalancerConfig.java
@@ -67,6 +67,14 @@ public final class FullAutoRebalancerConfig extends RebalancerConfig {
super(resourceId);
}
+ /**
+ * Construct a builder using an existing full-auto rebalancer config
+ * @param config
+ */
+ public Builder(FullAutoRebalancerConfig config) {
+ super(config);
+ }
+
@Override
public FullAutoRebalancerConfig build() {
if (_partitionMap.isEmpty()) {
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/05c20e96/helix-core/src/main/java/org/apache/helix/api/ParticipantAccessor.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/api/ParticipantAccessor.java b/helix-core/src/main/java/org/apache/helix/api/ParticipantAccessor.java
index 6c1be9e..46895d3 100644
--- a/helix-core/src/main/java/org/apache/helix/api/ParticipantAccessor.java
+++ b/helix-core/src/main/java/org/apache/helix/api/ParticipantAccessor.java
@@ -170,11 +170,10 @@ public class ParticipantAccessor {
} else {
// check partitions exist. warn if not
for (PartitionId partitionId : partitionIdSet) {
- String partitionName = partitionId.stringify();
if ((idealState.getRebalanceMode() == RebalanceMode.SEMI_AUTO && idealState
- .getPreferenceList(partitionName) == null)
+ .getPreferenceList(partitionId) == null)
|| (idealState.getRebalanceMode() == RebalanceMode.CUSTOMIZED && idealState
- .getInstanceStateMap(partitionName) == null)) {
+ .getParticipantStateMap(partitionId) == null)) {
LOG.warn("Cluster: " + _clusterId + ", resource: " + resourceId + ", partition: "
+ partitionId + ", partition does NOT exist in ideal state");
}
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/05c20e96/helix-core/src/main/java/org/apache/helix/api/PartitionId.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/api/PartitionId.java b/helix-core/src/main/java/org/apache/helix/api/PartitionId.java
index e72f1b3..757406d 100644
--- a/helix-core/src/main/java/org/apache/helix/api/PartitionId.java
+++ b/helix-core/src/main/java/org/apache/helix/api/PartitionId.java
@@ -28,6 +28,14 @@ public class PartitionId extends Id {
_partitionName = partitionName;
}
+ /**
+ * Get the id of the resource containing this partition
+ * @return ResourceId
+ */
+ public ResourceId getResourceId() {
+ return _resourceId;
+ }
+
@Override
public String stringify() {
// check in case the partition name is instantiated incorrectly
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/05c20e96/helix-core/src/main/java/org/apache/helix/api/RebalancerConfig.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/api/RebalancerConfig.java b/helix-core/src/main/java/org/apache/helix/api/RebalancerConfig.java
index 2f531e0..d377af8 100644
--- a/helix-core/src/main/java/org/apache/helix/api/RebalancerConfig.java
+++ b/helix-core/src/main/java/org/apache/helix/api/RebalancerConfig.java
@@ -376,6 +376,22 @@ public class RebalancerConfig extends NamespacedConfig {
}
/**
+ * Construct a builder from an existing rebalancer config
+ * @param config
+ */
+ public Builder(RebalancerConfig config) {
+ _resourceId = config.getResourceId();
+ _partitionMap = new TreeMap<PartitionId, Partition>();
+ _partitionMap.putAll(config.getPartitionMap());
+ _stateModelDefId = config.getStateModelDefId();
+ _stateModelFactoryId = config.getStateModelFactoryId();
+ _anyLiveParticipant = config.canAssignAnyLiveParticipant();
+ _replicaCount = config.getReplicaCount();
+ _maxPartitionsPerParticipant = config.getMaxPartitionsPerParticipant();
+ _participantGroupTag = config.getParticipantGroupTag();
+ }
+
+ /**
* Set the state model definition
* @param stateModelDefId state model identifier
* @return Builder
@@ -513,6 +529,14 @@ public class RebalancerConfig extends NamespacedConfig {
}
/**
+ * Construct with an existing rebalancer config
+ * @param config
+ */
+ public SimpleBuilder(RebalancerConfig config) {
+ super(config);
+ }
+
+ /**
* Build a rebalancer config
* @return
*/
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/05c20e96/helix-core/src/main/java/org/apache/helix/api/Scope.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/api/Scope.java b/helix-core/src/main/java/org/apache/helix/api/Scope.java
index e887f98..4bff194 100644
--- a/helix-core/src/main/java/org/apache/helix/api/Scope.java
+++ b/helix-core/src/main/java/org/apache/helix/api/Scope.java
@@ -24,6 +24,13 @@ package org.apache.helix.api;
* of cluster, participant, partition, or resource, but not more than one of these at any time.
*/
public class Scope<T extends Id> {
+ public enum ScopeType {
+ CLUSTER,
+ PARTICIPANT,
+ PARTITION,
+ RESOURCE
+ }
+
private final T _id;
/**
@@ -42,6 +49,30 @@ public class Scope<T extends Id> {
return _id;
}
+ @Override
+ public String toString() {
+ return getType() + "{" + getScopedId() + "}";
+ }
+
+ /**
+ * Get the Helix entity type that this scope covers
+ * @return scope type
+ */
+ public ScopeType getType() {
+ Class<?> idClass = _id.getClass();
+ if (idClass == ClusterId.class) {
+ return ScopeType.CLUSTER;
+ } else if (idClass == ParticipantId.class) {
+ return ScopeType.PARTICIPANT;
+ } else if (idClass == PartitionId.class) {
+ return ScopeType.PARTITION;
+ } else if (idClass == ResourceId.class) {
+ return ScopeType.RESOURCE;
+ } else {
+ return null;
+ }
+ }
+
/**
* Get a cluster scope
* @param clusterId the id of the cluster that is scoped
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/05c20e96/helix-core/src/main/java/org/apache/helix/api/SemiAutoRebalancerConfig.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/api/SemiAutoRebalancerConfig.java b/helix-core/src/main/java/org/apache/helix/api/SemiAutoRebalancerConfig.java
index a5284a8..409769e 100644
--- a/helix-core/src/main/java/org/apache/helix/api/SemiAutoRebalancerConfig.java
+++ b/helix-core/src/main/java/org/apache/helix/api/SemiAutoRebalancerConfig.java
@@ -116,6 +116,16 @@ public final class SemiAutoRebalancerConfig extends RebalancerConfig {
}
/**
+ * Construct a builder from an existing semi-auto rebalancer config
+ * @param config
+ */
+ public Builder(SemiAutoRebalancerConfig config) {
+ super(config);
+ _preferenceLists = new HashMap<PartitionId, List<ParticipantId>>();
+ _preferenceLists.putAll(config.getPreferenceLists());
+ }
+
+ /**
* Add a preference list of a partition
* @param partitionId the partition to set
* @param preferenceList list of participant ids, most preferred first
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/05c20e96/helix-core/src/main/java/org/apache/helix/api/UserDefinedRebalancerConfig.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/api/UserDefinedRebalancerConfig.java b/helix-core/src/main/java/org/apache/helix/api/UserDefinedRebalancerConfig.java
index 345c80b..d9517a9 100644
--- a/helix-core/src/main/java/org/apache/helix/api/UserDefinedRebalancerConfig.java
+++ b/helix-core/src/main/java/org/apache/helix/api/UserDefinedRebalancerConfig.java
@@ -88,6 +88,15 @@ public final class UserDefinedRebalancerConfig extends RebalancerConfig {
super(resourceId);
}
+ /**
+ * Construct a builder using an existing user-defined rebalancer config
+ * @param config
+ */
+ public Builder(UserDefinedRebalancerConfig config) {
+ super(config);
+ _rebalancerRef = config.getRebalancerRef();
+ }
+
public Builder rebalancerRef(RebalancerRef rebalancerRef) {
_rebalancerRef = rebalancerRef;
return this;
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/05c20e96/helix-core/src/main/java/org/apache/helix/controller/rebalancer/AutoRebalancer.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/controller/rebalancer/AutoRebalancer.java b/helix-core/src/main/java/org/apache/helix/controller/rebalancer/AutoRebalancer.java
index 32b4e92..1c69dfe 100644
--- a/helix-core/src/main/java/org/apache/helix/controller/rebalancer/AutoRebalancer.java
+++ b/helix-core/src/main/java/org/apache/helix/controller/rebalancer/AutoRebalancer.java
@@ -56,6 +56,7 @@ import org.apache.log4j.Logger;
* The output is a preference list and a mapping based on that preference list, i.e. partition p
* has a replica on node k with state s.
*/
+@Deprecated
public class AutoRebalancer implements Rebalancer {
// These should be final, but are initialized in init rather than a constructor
private HelixManager _manager;
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/05c20e96/helix-core/src/main/java/org/apache/helix/controller/rebalancer/CustomRebalancer.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/controller/rebalancer/CustomRebalancer.java b/helix-core/src/main/java/org/apache/helix/controller/rebalancer/CustomRebalancer.java
index 689561b..709b61e 100644
--- a/helix-core/src/main/java/org/apache/helix/controller/rebalancer/CustomRebalancer.java
+++ b/helix-core/src/main/java/org/apache/helix/controller/rebalancer/CustomRebalancer.java
@@ -46,6 +46,7 @@ import org.apache.log4j.Logger;
* The output is a verified mapping based on that preference list, i.e. partition p has a replica
* on node k with state s, where s may be a dropped or error state if necessary.
*/
+@Deprecated
public class CustomRebalancer implements Rebalancer {
private static final Logger LOG = Logger.getLogger(CustomRebalancer.class);
@@ -70,7 +71,8 @@ public class CustomRebalancer implements Rebalancer {
Set<String> disabledInstancesForPartition =
clusterData.getDisabledInstancesForPartition(partition.toString());
Map<String, String> idealStateMap =
- currentIdealState.getInstanceStateMap(partition.getPartitionName());
+ IdealState.stringMapFromParticipantStateMap(currentIdealState
+ .getParticipantStateMap(PartitionId.from(partition.getPartitionName())));
Map<String, String> bestStateForPartition =
computeCustomizedBestStateForPartition(clusterData, stateModelDef, idealStateMap,
currentStateMap, disabledInstancesForPartition);
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/05c20e96/helix-core/src/main/java/org/apache/helix/controller/rebalancer/NewAutoRebalancer.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/controller/rebalancer/NewAutoRebalancer.java b/helix-core/src/main/java/org/apache/helix/controller/rebalancer/NewAutoRebalancer.java
index 8212b7f..1972f03 100644
--- a/helix-core/src/main/java/org/apache/helix/controller/rebalancer/NewAutoRebalancer.java
+++ b/helix-core/src/main/java/org/apache/helix/controller/rebalancer/NewAutoRebalancer.java
@@ -35,7 +35,6 @@ import org.apache.helix.api.Participant;
import org.apache.helix.api.ParticipantId;
import org.apache.helix.api.PartitionId;
import org.apache.helix.api.State;
-import org.apache.helix.controller.rebalancer.util.ConstraintBasedAssignment;
import org.apache.helix.controller.rebalancer.util.NewConstraintBasedAssignment;
import org.apache.helix.controller.stages.ResourceCurrentState;
import org.apache.helix.controller.strategy.AutoRebalanceStrategy;
@@ -82,13 +81,23 @@ public class NewAutoRebalancer implements NewRebalancer<FullAutoRebalancerConfig
replicas = config.getReplicaCount();
}
- LinkedHashMap<String, Integer> stateCountMap =
- ConstraintBasedAssignment.stateCount(stateModelDef, liveParticipants.size(), replicas);
+ // count how many replicas should be in each state
+ LinkedHashMap<State, Integer> stateCountMap =
+ NewConstraintBasedAssignment.stateCount(cluster.getConfig(), config.getResourceId(),
+ stateModelDef, liveParticipants.size(), replicas);
+ LinkedHashMap<String, Integer> rawStateCountMap = new LinkedHashMap<String, Integer>();
+ for (State state : stateCountMap.keySet()) {
+ rawStateCountMap.put(state.toString(), stateCountMap.get(state));
+ }
+
+ // get the participant lists
List<ParticipantId> liveParticipantList =
new ArrayList<ParticipantId>(liveParticipants.keySet());
List<ParticipantId> allParticipantList =
new ArrayList<ParticipantId>(cluster.getParticipantMap().keySet());
List<String> liveNodes = Lists.transform(liveParticipantList, Functions.toStringFunction());
+
+ // compute the current mapping from the current state
Map<PartitionId, Map<ParticipantId, State>> currentMapping =
currentMapping(config, currentState, stateCountMap);
@@ -109,9 +118,9 @@ public class NewAutoRebalancer implements NewRebalancer<FullAutoRebalancerConfig
liveNodes = new ArrayList<String>(taggedNodes);
}
+ // determine which nodes the replicas should live on
List<String> allNodes = Lists.transform(allParticipantList, Functions.toStringFunction());
int maxPartition = config.getMaxPartitionsPerParticipant();
-
if (LOG.isInfoEnabled()) {
LOG.info("currentMapping: " + currentMapping);
LOG.info("stateCountMap: " + stateCountMap);
@@ -122,7 +131,7 @@ public class NewAutoRebalancer implements NewRebalancer<FullAutoRebalancerConfig
ReplicaPlacementScheme placementScheme = new DefaultPlacementScheme();
_algorithm =
new AutoRebalanceStrategy(config.getResourceId().stringify(), partitionNames,
- stateCountMap, maxPartition, placementScheme);
+ rawStateCountMap, maxPartition, placementScheme);
ZNRecord newMapping =
_algorithm.computePartitionAssignment(liveNodes,
ResourceAssignment.stringMapsFromReplicaMaps(currentMapping), allNodes);
@@ -153,8 +162,8 @@ public class NewAutoRebalancer implements NewRebalancer<FullAutoRebalancerConfig
preferenceList =
NewConstraintBasedAssignment.getPreferenceList(cluster, partition, preferenceList);
Map<ParticipantId, State> bestStateForPartition =
- NewConstraintBasedAssignment.computeAutoBestStateForPartition(liveParticipants,
- stateModelDef, preferenceList,
+ NewConstraintBasedAssignment.computeAutoBestStateForPartition(cluster.getConfig(),
+ config.getResourceId(), liveParticipants, stateModelDef, preferenceList,
currentState.getCurrentStateMap(config.getResourceId(), partition),
disabledParticipantsForPartition);
partitionMapping.addReplicaMap(partition, bestStateForPartition);
@@ -164,7 +173,7 @@ public class NewAutoRebalancer implements NewRebalancer<FullAutoRebalancerConfig
private Map<PartitionId, Map<ParticipantId, State>> currentMapping(
FullAutoRebalancerConfig config, ResourceCurrentState currentStateOutput,
- Map<String, Integer> stateCountMap) {
+ Map<State, Integer> stateCountMap) {
Map<PartitionId, Map<ParticipantId, State>> map =
new HashMap<PartitionId, Map<ParticipantId, State>>();
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/05c20e96/helix-core/src/main/java/org/apache/helix/controller/rebalancer/NewSemiAutoRebalancer.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/controller/rebalancer/NewSemiAutoRebalancer.java b/helix-core/src/main/java/org/apache/helix/controller/rebalancer/NewSemiAutoRebalancer.java
index cb061ab..3153623 100644
--- a/helix-core/src/main/java/org/apache/helix/controller/rebalancer/NewSemiAutoRebalancer.java
+++ b/helix-core/src/main/java/org/apache/helix/controller/rebalancer/NewSemiAutoRebalancer.java
@@ -66,9 +66,9 @@ public class NewSemiAutoRebalancer implements NewRebalancer<SemiAutoRebalancerCo
NewConstraintBasedAssignment.getPreferenceList(cluster, partition,
config.getPreferenceList(partition));
Map<ParticipantId, State> bestStateForPartition =
- NewConstraintBasedAssignment.computeAutoBestStateForPartition(
- cluster.getLiveParticipantMap(), stateModelDef, preferenceList, currentStateMap,
- disabledInstancesForPartition);
+ NewConstraintBasedAssignment.computeAutoBestStateForPartition(cluster.getConfig(),
+ config.getResourceId(), cluster.getLiveParticipantMap(), stateModelDef,
+ preferenceList, currentStateMap, disabledInstancesForPartition);
partitionMapping.addReplicaMap(partition, bestStateForPartition);
}
return partitionMapping;
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/05c20e96/helix-core/src/main/java/org/apache/helix/controller/rebalancer/Rebalancer.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/controller/rebalancer/Rebalancer.java b/helix-core/src/main/java/org/apache/helix/controller/rebalancer/Rebalancer.java
index e4f165b..5dd399f 100644
--- a/helix-core/src/main/java/org/apache/helix/controller/rebalancer/Rebalancer.java
+++ b/helix-core/src/main/java/org/apache/helix/controller/rebalancer/Rebalancer.java
@@ -31,6 +31,7 @@ import org.apache.helix.model.ResourceAssignment;
* This will be invoked on all changes that happen in the cluster.<br/>
* Simply return the newIdealState for a resource in this method.<br/>
*/
+@Deprecated
public interface Rebalancer {
/**
* Initialize the rebalancer with a HelixManager if necessary
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/05c20e96/helix-core/src/main/java/org/apache/helix/controller/rebalancer/SemiAutoRebalancer.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/controller/rebalancer/SemiAutoRebalancer.java b/helix-core/src/main/java/org/apache/helix/controller/rebalancer/SemiAutoRebalancer.java
index d525ea3..433b4a7 100644
--- a/helix-core/src/main/java/org/apache/helix/controller/rebalancer/SemiAutoRebalancer.java
+++ b/helix-core/src/main/java/org/apache/helix/controller/rebalancer/SemiAutoRebalancer.java
@@ -45,6 +45,7 @@ import org.apache.log4j.Logger;
* The output is a mapping based on that preference list, i.e. partition p has a replica on node k
* with state s.
*/
+@Deprecated
public class SemiAutoRebalancer implements Rebalancer {
private static final Logger LOG = Logger.getLogger(SemiAutoRebalancer.class);
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/05c20e96/helix-core/src/main/java/org/apache/helix/controller/rebalancer/util/ConstraintBasedAssignment.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/controller/rebalancer/util/ConstraintBasedAssignment.java b/helix-core/src/main/java/org/apache/helix/controller/rebalancer/util/ConstraintBasedAssignment.java
index ee7524f..dce0a07 100644
--- a/helix-core/src/main/java/org/apache/helix/controller/rebalancer/util/ConstraintBasedAssignment.java
+++ b/helix-core/src/main/java/org/apache/helix/controller/rebalancer/util/ConstraintBasedAssignment.java
@@ -41,6 +41,7 @@ import org.apache.log4j.Logger;
* Collection of functions that will compute the best possible states given the live instances and
* an ideal state.
*/
+@Deprecated
public class ConstraintBasedAssignment {
private static Logger logger = Logger.getLogger(ConstraintBasedAssignment.class);
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/05c20e96/helix-core/src/main/java/org/apache/helix/controller/rebalancer/util/NewConstraintBasedAssignment.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/controller/rebalancer/util/NewConstraintBasedAssignment.java b/helix-core/src/main/java/org/apache/helix/controller/rebalancer/util/NewConstraintBasedAssignment.java
index 69d9fcf..603edd0 100644
--- a/helix-core/src/main/java/org/apache/helix/controller/rebalancer/util/NewConstraintBasedAssignment.java
+++ b/helix-core/src/main/java/org/apache/helix/controller/rebalancer/util/NewConstraintBasedAssignment.java
@@ -31,9 +31,12 @@ import java.util.Set;
import org.apache.helix.HelixConstants.StateModelToken;
import org.apache.helix.HelixDefinedState;
import org.apache.helix.api.Cluster;
+import org.apache.helix.api.ClusterConfig;
import org.apache.helix.api.Participant;
import org.apache.helix.api.ParticipantId;
import org.apache.helix.api.PartitionId;
+import org.apache.helix.api.ResourceId;
+import org.apache.helix.api.Scope;
import org.apache.helix.api.State;
import org.apache.helix.model.StateModelDefinition;
import org.apache.log4j.Logger;
@@ -96,10 +99,10 @@ public class NewConstraintBasedAssignment {
* @param disabledParticipantsForPartition
* @return
*/
- public static Map<ParticipantId, State> computeAutoBestStateForPartition(
- Map<ParticipantId, Participant> liveParticipantMap, StateModelDefinition stateModelDef,
- List<ParticipantId> participantPreferenceList, Map<ParticipantId, State> currentStateMap,
- Set<ParticipantId> disabledParticipantsForPartition) {
+ public static Map<ParticipantId, State> computeAutoBestStateForPartition(ClusterConfig cluster,
+ ResourceId resourceId, Map<ParticipantId, Participant> liveParticipantMap,
+ StateModelDefinition stateModelDef, List<ParticipantId> participantPreferenceList,
+ Map<ParticipantId, State> currentStateMap, Set<ParticipantId> disabledParticipantsForPartition) {
Map<ParticipantId, State> participantStateMap = new HashMap<ParticipantId, State>();
// if the resource is deleted, instancePreferenceList will be empty and
@@ -128,7 +131,9 @@ public class NewConstraintBasedAssignment {
boolean assigned[] = new boolean[participantPreferenceList.size()];
for (State state : statesPriorityList) {
- String num = stateModelDef.getNumParticipantsPerState(state);
+ String num =
+ cluster.getStateUpperBoundConstraint(Scope.resource(resourceId),
+ stateModelDef.getStateModelDefId(), state);
int stateCount = -1;
if ("N".equals(num)) {
Set<ParticipantId> liveAndEnabled = new HashSet<ParticipantId>(liveParticipantMap.keySet());
@@ -171,19 +176,23 @@ public class NewConstraintBasedAssignment {
/**
* Get the number of replicas that should be in each state for a partition
+ * @param cluster cluster configuration
+ * @param resourceId the resource for which to get the state count
* @param stateModelDef StateModelDefinition object
* @param liveNodesNb number of live nodes
* @param total number of replicas
* @return state count map: state->count
*/
- public static LinkedHashMap<State, Integer> stateCount(StateModelDefinition stateModelDef,
- int liveNodesNb, int totalReplicas) {
+ public static LinkedHashMap<State, Integer> stateCount(ClusterConfig cluster,
+ ResourceId resourceId, StateModelDefinition stateModelDef, int liveNodesNb, int totalReplicas) {
LinkedHashMap<State, Integer> stateCountMap = new LinkedHashMap<State, Integer>();
List<State> statesPriorityList = stateModelDef.getStatesPriorityList();
int replicas = totalReplicas;
for (State state : statesPriorityList) {
- String num = stateModelDef.getNumParticipantsPerState(state);
+ String num =
+ cluster.getStateUpperBoundConstraint(Scope.resource(resourceId),
+ stateModelDef.getStateModelDefId(), state);
if ("N".equals(num)) {
stateCountMap.put(state, liveNodesNb);
} else if ("R".equals(num)) {
@@ -207,7 +216,9 @@ public class NewConstraintBasedAssignment {
// get state count for R
for (State state : statesPriorityList) {
- String num = stateModelDef.getNumParticipantsPerState(state);
+ String num =
+ cluster.getStateUpperBoundConstraint(Scope.resource(resourceId),
+ stateModelDef.getStateModelDefId(), state);
if ("R".equals(num)) {
stateCountMap.put(state, replicas);
// should have at most one state using R
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/05c20e96/helix-core/src/main/java/org/apache/helix/controller/stages/BestPossibleStateCalcStage.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/controller/stages/BestPossibleStateCalcStage.java b/helix-core/src/main/java/org/apache/helix/controller/stages/BestPossibleStateCalcStage.java
index c7473ff..dbbcb1e 100644
--- a/helix-core/src/main/java/org/apache/helix/controller/stages/BestPossibleStateCalcStage.java
+++ b/helix-core/src/main/java/org/apache/helix/controller/stages/BestPossibleStateCalcStage.java
@@ -97,8 +97,8 @@ public class BestPossibleStateCalcStage extends AbstractBaseStage {
Rebalancer rebalancer = null;
if (idealState.getRebalanceMode() == RebalanceMode.USER_DEFINED
- && idealState.getRebalancerClassName() != null) {
- String rebalancerClassName = idealState.getRebalancerClassName();
+ && idealState.getRebalancerRef() != null) {
+ String rebalancerClassName = idealState.getRebalancerRef().toString();
logger
.info("resource " + resourceName + " use idealStateRebalancer " + rebalancerClassName);
try {
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/05c20e96/helix-core/src/main/java/org/apache/helix/controller/stages/NewBestPossibleStateCalcStage.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/controller/stages/NewBestPossibleStateCalcStage.java b/helix-core/src/main/java/org/apache/helix/controller/stages/NewBestPossibleStateCalcStage.java
index a89707b..021c9b8 100644
--- a/helix-core/src/main/java/org/apache/helix/controller/stages/NewBestPossibleStateCalcStage.java
+++ b/helix-core/src/main/java/org/apache/helix/controller/stages/NewBestPossibleStateCalcStage.java
@@ -101,7 +101,8 @@ public class NewBestPossibleStateCalcStage extends AbstractBaseStage {
NewConstraintBasedAssignment.getDisabledParticipants(cluster.getParticipantMap(),
partitionId);
partitionMapping.addReplicaMap(partitionId, NewConstraintBasedAssignment
- .computeAutoBestStateForPartition(cluster.getLiveParticipantMap(), stateModelDef, null,
+ .computeAutoBestStateForPartition(cluster.getConfig(), resourceId,
+ cluster.getLiveParticipantMap(), stateModelDef, null,
currentStateOutput.getCurrentStateMap(resourceId, partitionId),
disabledParticipantsForPartition));
}
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/05c20e96/helix-core/src/main/java/org/apache/helix/controller/stages/NewResourceComputationStage.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/controller/stages/NewResourceComputationStage.java b/helix-core/src/main/java/org/apache/helix/controller/stages/NewResourceComputationStage.java
index 0cc1ea1..457b470 100644
--- a/helix-core/src/main/java/org/apache/helix/controller/stages/NewResourceComputationStage.java
+++ b/helix-core/src/main/java/org/apache/helix/controller/stages/NewResourceComputationStage.java
@@ -20,10 +20,11 @@ package org.apache.helix.controller.stages;
*/
import java.util.HashMap;
-import java.util.LinkedHashMap;
import java.util.Map;
import org.apache.helix.api.Cluster;
+import org.apache.helix.api.CustomRebalancerConfig;
+import org.apache.helix.api.FullAutoRebalancerConfig;
import org.apache.helix.api.Participant;
import org.apache.helix.api.Partition;
import org.apache.helix.api.PartitionId;
@@ -31,7 +32,9 @@ import org.apache.helix.api.RebalancerConfig;
import org.apache.helix.api.Resource;
import org.apache.helix.api.ResourceConfig;
import org.apache.helix.api.ResourceId;
+import org.apache.helix.api.SemiAutoRebalancerConfig;
import org.apache.helix.api.StateModelFactoryId;
+import org.apache.helix.api.UserDefinedRebalancerConfig;
import org.apache.helix.controller.pipeline.AbstractBaseStage;
import org.apache.helix.controller.pipeline.StageException;
import org.apache.helix.model.CurrentState;
@@ -47,29 +50,99 @@ public class NewResourceComputationStage extends AbstractBaseStage {
private static Logger LOG = Logger.getLogger(NewResourceComputationStage.class);
@Override
- public void process(ClusterEvent event) throws Exception {
+ public void process(ClusterEvent event) throws StageException {
Cluster cluster = event.getAttribute("ClusterDataCache");
if (cluster == null) {
- throw new StageException("Missing attributes in event:" + event + ". Requires Cluster");
+ throw new StageException("Missing attributes in event: " + event + ". Requires Cluster");
+ }
+
+ Map<ResourceId, ResourceConfig> resCfgMap = new HashMap<ResourceId, ResourceConfig>();
+ Map<ResourceId, ResourceConfig> csResCfgMap = getCurStateResourceCfgMap(cluster);
+
+ // ideal-state may be removed, add all resource config in current-state but not in ideal-state
+ for (ResourceId resourceId : csResCfgMap.keySet()) {
+ if (!cluster.getResourceMap().keySet().contains(resourceId)) {
+ resCfgMap.put(resourceId, csResCfgMap.get(resourceId));
+ }
}
- Map<ResourceId, ResourceConfig.Builder> resourceBuilderMap =
- new LinkedHashMap<ResourceId, ResourceConfig.Builder>();
- // include all resources in ideal-state
for (ResourceId resourceId : cluster.getResourceMap().keySet()) {
Resource resource = cluster.getResource(resourceId);
- RebalancerConfig rebalancerConfig = resource.getRebalancerConfig();
-
- ResourceConfig.Builder resourceBuilder = new ResourceConfig.Builder(resourceId);
- resourceBuilder.rebalancerConfig(rebalancerConfig);
- resourceBuilder.bucketSize(resource.getBucketSize());
- resourceBuilder.batchMessageMode(resource.getBatchMessageMode());
- resourceBuilder.schedulerTaskConfig(resource.getSchedulerTaskConfig());
- resourceBuilderMap.put(resourceId, resourceBuilder);
+ RebalancerConfig rebalancerCfg = resource.getRebalancerConfig();
+
+ ResourceConfig.Builder resCfgBuilder = new ResourceConfig.Builder(resourceId);
+ resCfgBuilder.bucketSize(resource.getBucketSize());
+ resCfgBuilder.batchMessageMode(resource.getBatchMessageMode());
+ resCfgBuilder.schedulerTaskConfig(resource.getSchedulerTaskConfig());
+
+ switch (rebalancerCfg.getRebalancerMode()) {
+ case USER_DEFINED: {
+ UserDefinedRebalancerConfig.Builder builder =
+ new UserDefinedRebalancerConfig.Builder(UserDefinedRebalancerConfig.from(rebalancerCfg));
+ if (csResCfgMap.containsKey(resourceId)) {
+ builder.addPartitions(csResCfgMap.get(resourceId).getPartitionMap().values());
+ }
+ resCfgBuilder.rebalancerConfig(builder.build());
+ resCfgMap.put(resourceId, resCfgBuilder.build());
+ break;
+ }
+ case FULL_AUTO: {
+ FullAutoRebalancerConfig.Builder builder =
+ new FullAutoRebalancerConfig.Builder(FullAutoRebalancerConfig.from(rebalancerCfg));
+ if (csResCfgMap.containsKey(resourceId)) {
+ builder.addPartitions(csResCfgMap.get(resourceId).getPartitionMap().values());
+ }
+ resCfgBuilder.rebalancerConfig(builder.build());
+ resCfgMap.put(resourceId, resCfgBuilder.build());
+ break;
+ }
+ case SEMI_AUTO: {
+ SemiAutoRebalancerConfig.Builder builder =
+ new SemiAutoRebalancerConfig.Builder(SemiAutoRebalancerConfig.from(rebalancerCfg));
+ if (csResCfgMap.containsKey(resourceId)) {
+ builder.addPartitions(csResCfgMap.get(resourceId).getPartitionMap().values());
+ }
+ resCfgBuilder.rebalancerConfig(builder.build());
+ resCfgMap.put(resourceId, resCfgBuilder.build());
+ break;
+ }
+ case CUSTOMIZED: {
+ CustomRebalancerConfig.Builder builder =
+ new CustomRebalancerConfig.Builder(CustomRebalancerConfig.from(rebalancerCfg));
+ if (csResCfgMap.containsKey(resourceId)) {
+ builder.addPartitions(csResCfgMap.get(resourceId).getPartitionMap().values());
+ }
+ resCfgBuilder.rebalancerConfig(builder.build());
+ resCfgMap.put(resourceId, resCfgBuilder.build());
+ break;
+ }
+ default:
+ RebalancerConfig.SimpleBuilder builder = new RebalancerConfig.SimpleBuilder(rebalancerCfg);
+ if (csResCfgMap.containsKey(resourceId)) {
+ builder.addPartitions(csResCfgMap.get(resourceId).getPartitionMap().values());
+ }
+ resCfgBuilder.rebalancerConfig(builder.build());
+ resCfgMap.put(resourceId, resCfgBuilder.build());
+ break;
+ }
+
}
- // include all partitions from CurrentState as well since idealState might be removed
- Map<ResourceId, RebalancerConfig.SimpleBuilder> rebalancerConfigBuilderMap =
+ event.addAttribute(AttributeName.RESOURCES.toString(), resCfgMap);
+ }
+
+ /**
+ * Get resource config's from current-state
+ * @param cluster
+ * @return resource config map or empty map if not available
+ * @throws StageException
+ */
+ Map<ResourceId, ResourceConfig> getCurStateResourceCfgMap(Cluster cluster)
+ throws StageException {
+ Map<ResourceId, ResourceConfig.Builder> resCfgBuilderMap =
+ new HashMap<ResourceId, ResourceConfig.Builder>();
+
+ Map<ResourceId, RebalancerConfig.SimpleBuilder> rebCfgBuilderMap =
new HashMap<ResourceId, RebalancerConfig.SimpleBuilder>();
for (Participant liveParticipant : cluster.getLiveParticipantMap().values()) {
@@ -84,46 +157,35 @@ public class NewResourceComputationStage extends AbstractBaseStage {
+ currentState.getResourceId());
}
- // don't overwrite ideal state configs
- if (!resourceBuilderMap.containsKey(resourceId)) {
- if (!rebalancerConfigBuilderMap.containsKey(resourceId)) {
- RebalancerConfig.SimpleBuilder rebalancerConfigBuilder =
- new RebalancerConfig.SimpleBuilder(resourceId);
- rebalancerConfigBuilder.stateModelDef(currentState.getStateModelDefId());
- rebalancerConfigBuilder.stateModelFactoryId(StateModelFactoryId.from(currentState
- .getStateModelFactoryName()));
- rebalancerConfigBuilderMap.put(resourceId, rebalancerConfigBuilder);
- }
- ResourceConfig.Builder resourceBuilder = new ResourceConfig.Builder(resourceId);
- resourceBuilder.bucketSize(currentState.getBucketSize());
- resourceBuilder.batchMessageMode(currentState.getBatchMessageMode());
- resourceBuilderMap.put(resourceId, resourceBuilder);
+ if (!resCfgBuilderMap.containsKey(resourceId)) {
+ RebalancerConfig.SimpleBuilder rebCfgBuilder =
+ new RebalancerConfig.SimpleBuilder(resourceId);
+ rebCfgBuilder.stateModelDef(currentState.getStateModelDefId());
+ rebCfgBuilder.stateModelFactoryId(StateModelFactoryId.from(currentState
+ .getStateModelFactoryName()));
+ rebCfgBuilderMap.put(resourceId, rebCfgBuilder);
+
+ ResourceConfig.Builder resCfgBuilder = new ResourceConfig.Builder(resourceId);
+ resCfgBuilder.bucketSize(currentState.getBucketSize());
+ resCfgBuilder.batchMessageMode(currentState.getBatchMessageMode());
+ resCfgBuilderMap.put(resourceId, resCfgBuilder);
}
- // add all partitions in current-state
- if (rebalancerConfigBuilderMap.containsKey(resourceId)) {
- RebalancerConfig.SimpleBuilder rebalancerConfigBuilder =
- rebalancerConfigBuilderMap.get(resourceId);
- for (PartitionId partitionId : currentState.getPartitionStateMap().keySet()) {
- rebalancerConfigBuilder.addPartition(new Partition(partitionId));
- }
+ RebalancerConfig.SimpleBuilder rebCfgBuilder = rebCfgBuilderMap.get(resourceId);
+ for (PartitionId partitionId : currentState.getPartitionStateMap().keySet()) {
+ rebCfgBuilder.addPartition(new Partition(partitionId));
}
}
-
}
- // convert builder-map to resource-map
- Map<ResourceId, ResourceConfig> resourceMap = new LinkedHashMap<ResourceId, ResourceConfig>();
- for (ResourceId resourceId : resourceBuilderMap.keySet()) {
- ResourceConfig.Builder resourceConfigBuilder = resourceBuilderMap.get(resourceId);
- if (rebalancerConfigBuilderMap.containsKey(resourceId)) {
- RebalancerConfig.SimpleBuilder rebalancerConfigBuilder =
- rebalancerConfigBuilderMap.get(resourceId);
- resourceConfigBuilder.rebalancerConfig(rebalancerConfigBuilder.build());
- }
- resourceMap.put(resourceId, resourceConfigBuilder.build());
+ Map<ResourceId, ResourceConfig> resCfgMap = new HashMap<ResourceId, ResourceConfig>();
+ for (ResourceId resourceId : resCfgBuilderMap.keySet()) {
+ ResourceConfig.Builder resCfgBuilder = resCfgBuilderMap.get(resourceId);
+ RebalancerConfig.SimpleBuilder rebCfgBuilder = rebCfgBuilderMap.get(resourceId);
+ resCfgBuilder.rebalancerConfig(rebCfgBuilder.build());
+ resCfgMap.put(resourceId, resCfgBuilder.build());
}
- event.addAttribute(AttributeName.RESOURCES.toString(), resourceMap);
+ return resCfgMap;
}
}
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/05c20e96/helix-core/src/main/java/org/apache/helix/controller/stages/RebalanceIdealStateStage.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/controller/stages/RebalanceIdealStateStage.java b/helix-core/src/main/java/org/apache/helix/controller/stages/RebalanceIdealStateStage.java
index e6b7398..ffc14d6 100644
--- a/helix-core/src/main/java/org/apache/helix/controller/stages/RebalanceIdealStateStage.java
+++ b/helix-core/src/main/java/org/apache/helix/controller/stages/RebalanceIdealStateStage.java
@@ -54,8 +54,8 @@ public class RebalanceIdealStateStage extends AbstractBaseStage {
for (String resourceName : idealStateMap.keySet()) {
IdealState currentIdealState = idealStateMap.get(resourceName);
if (currentIdealState.getRebalanceMode() == RebalanceMode.USER_DEFINED
- && currentIdealState.getRebalancerClassName() != null) {
- String rebalancerClassName = currentIdealState.getRebalancerClassName();
+ && currentIdealState.getRebalancerRef() != null) {
+ String rebalancerClassName = currentIdealState.getRebalancerRef().toString();
LOG.info("resource " + resourceName + " use idealStateRebalancer " + rebalancerClassName);
try {
Rebalancer balancer =
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/05c20e96/helix-core/src/main/java/org/apache/helix/controller/stages/StatsAggregationStage.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/controller/stages/StatsAggregationStage.java b/helix-core/src/main/java/org/apache/helix/controller/stages/StatsAggregationStage.java
index 00612dd..c48f156 100644
--- a/helix-core/src/main/java/org/apache/helix/controller/stages/StatsAggregationStage.java
+++ b/helix-core/src/main/java/org/apache/helix/controller/stages/StatsAggregationStage.java
@@ -320,7 +320,7 @@ public class StatsAggregationStage extends AbstractBaseStage {
Builder kb = accessor.keyBuilder();
List<IdealState> idealStates = accessor.getChildValues(kb.idealStates());
for (IdealState idealState : idealStates) {
- String resourceName = idealState.getResourceName();
+ String resourceName = idealState.getResourceId().stringify();
if (actualStatName.contains("=" + resourceName + ".")
|| actualStatName.contains("=" + resourceName + ";")) {
return resourceName;
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/05c20e96/helix-core/src/main/java/org/apache/helix/controller/strategy/EspressoRelayStrategy.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/controller/strategy/EspressoRelayStrategy.java b/helix-core/src/main/java/org/apache/helix/controller/strategy/EspressoRelayStrategy.java
index 4e88499..6a7bb59 100644
--- a/helix-core/src/main/java/org/apache/helix/controller/strategy/EspressoRelayStrategy.java
+++ b/helix-core/src/main/java/org/apache/helix/controller/strategy/EspressoRelayStrategy.java
@@ -27,6 +27,7 @@ import java.util.Map;
import java.util.TreeMap;
import org.apache.helix.HelixException;
+import org.apache.helix.api.StateModelDefId;
import org.apache.helix.model.IdealState;
public class EspressoRelayStrategy {
@@ -42,7 +43,7 @@ public class EspressoRelayStrategy {
IdealState result = new IdealState(resultRecordName);
result.setNumPartitions(partitions.size());
result.setReplicas("" + replica);
- result.setStateModelDefRef(stateModelName);
+ result.setStateModelDefId(StateModelDefId.from(stateModelName));
int groups = instances.size() / replica;
int remainder = instances.size() % replica;
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/05c20e96/helix-core/src/main/java/org/apache/helix/manager/zk/DefaultSchedulerMessageHandlerFactory.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/manager/zk/DefaultSchedulerMessageHandlerFactory.java b/helix-core/src/main/java/org/apache/helix/manager/zk/DefaultSchedulerMessageHandlerFactory.java
index 0a223fb..9d61914 100644
--- a/helix-core/src/main/java/org/apache/helix/manager/zk/DefaultSchedulerMessageHandlerFactory.java
+++ b/helix-core/src/main/java/org/apache/helix/manager/zk/DefaultSchedulerMessageHandlerFactory.java
@@ -40,6 +40,7 @@ import org.apache.helix.api.MessageId;
import org.apache.helix.api.ParticipantId;
import org.apache.helix.api.PartitionId;
import org.apache.helix.api.State;
+import org.apache.helix.api.StateModelDefId;
import org.apache.helix.messaging.AsyncCallback;
import org.apache.helix.messaging.handling.HelixTaskResult;
import org.apache.helix.messaging.handling.MessageHandler;
@@ -180,7 +181,7 @@ public class DefaultSchedulerMessageHandlerFactory implements MessageHandlerFact
}
IdealState newAddedScheduledTasks = new IdealState(taskQueueName);
newAddedScheduledTasks.setBucketSize(TASKQUEUE_BUCKET_NUM);
- newAddedScheduledTasks.setStateModelDefRef(SCHEDULER_TASK_QUEUE);
+ newAddedScheduledTasks.setStateModelDefId(StateModelDefId.from(SCHEDULER_TASK_QUEUE));
synchronized (_manager) {
int existingTopPartitionId = 0;
@@ -233,8 +234,9 @@ public class DefaultSchedulerMessageHandlerFactory implements MessageHandlerFact
private int findTopPartitionId(IdealState currentTaskQueue) {
int topId = 0;
- for (String partitionName : currentTaskQueue.getPartitionStringSet()) {
+ for (PartitionId partitionId : currentTaskQueue.getPartitionSet()) {
try {
+ String partitionName = partitionId.stringify();
String partitionNumStr = partitionName.substring(partitionName.lastIndexOf('_') + 1);
int num = Integer.parseInt(partitionNumStr);
if (topId < num) {
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/05c20e96/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 61b1cd1..14c63f4 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
@@ -53,12 +53,14 @@ import org.apache.helix.PropertyType;
import org.apache.helix.ZNRecord;
import org.apache.helix.alerts.AlertsHolder;
import org.apache.helix.alerts.StatsHolder;
+import org.apache.helix.api.ConstraintId;
import org.apache.helix.api.MessageId;
import org.apache.helix.api.PartitionId;
import org.apache.helix.api.ResourceId;
import org.apache.helix.api.SessionId;
import org.apache.helix.api.State;
import org.apache.helix.api.StateModelDefId;
+import org.apache.helix.api.StateModelFactoryId;
import org.apache.helix.controller.strategy.DefaultTwoStateStrategy;
import org.apache.helix.model.Alerts;
import org.apache.helix.model.ClusterConstraints;
@@ -240,9 +242,9 @@ public class ZKHelixAdmin implements HelixAdmin {
IdealState idealState = new IdealState(idealStateRecord);
for (String partitionName : partitionNames) {
if ((idealState.getRebalanceMode() == RebalanceMode.SEMI_AUTO && idealState
- .getPreferenceList(partitionName) == null)
+ .getPreferenceList(PartitionId.from(partitionName)) == null)
|| (idealState.getRebalanceMode() == RebalanceMode.CUSTOMIZED && idealState
- .getInstanceStateMap(partitionName) == null)) {
+ .getParticipantStateMap(PartitionId.from(partitionName)) == null)) {
logger.warn("Cluster: " + clusterName + ", resource: " + resourceName + ", partition: "
+ partitionName + ", partition does not exist in ideal state");
}
@@ -390,7 +392,7 @@ public class ZKHelixAdmin implements HelixAdmin {
message.setStateModelDef(stateModelDef);
message.setFromState(State.from(HelixDefinedState.ERROR.toString()));
message.setToState(stateModel.getInitialState());
- message.setStateModelFactoryName(idealState.getStateModelFactoryName());
+ message.setStateModelFactoryId(idealState.getStateModelFactoryId());
resetMessages.add(message);
messageKeys.add(keyBuilder.message(instanceName, message.getId()));
@@ -590,7 +592,7 @@ public class ZKHelixAdmin implements HelixAdmin {
@Override
public void addResource(String clusterName, String resourceName, IdealState idealstate) {
- String stateModelRef = idealstate.getStateModelDefRef();
+ String stateModelRef = idealstate.getStateModelDefId().stringify();
String stateModelDefPath =
PropertyPathConfig.getPath(PropertyType.STATEMODELDEFS, clusterName, stateModelRef);
if (!_zkClient.exists(stateModelDefPath)) {
@@ -631,10 +633,11 @@ public class ZKHelixAdmin implements HelixAdmin {
}
IdealState idealState = new IdealState(resourceName);
idealState.setNumPartitions(partitions);
- idealState.setStateModelDefRef(stateModelRef);
+ idealState.setStateModelDefId(StateModelDefId.from(stateModelRef));
idealState.setRebalanceMode(mode);
idealState.setReplicas("" + 0);
- idealState.setStateModelFactoryName(HelixConstants.DEFAULT_STATE_MODEL_FACTORY);
+ idealState.setStateModelFactoryId(StateModelFactoryId
+ .from(HelixConstants.DEFAULT_STATE_MODEL_FACTORY));
if (maxPartitionsPerInstance > 0 && maxPartitionsPerInstance < Integer.MAX_VALUE) {
idealState.setMaxPartitionsPerInstance(maxPartitionsPerInstance);
}
@@ -900,7 +903,7 @@ public class ZKHelixAdmin implements HelixAdmin {
IdealState idealState = new IdealState(clusterName);
idealState.setNumPartitions(1);
- idealState.setStateModelDefRef("LeaderStandby");
+ idealState.setStateModelDefId(StateModelDefId.from("LeaderStandby"));
List<String> controllers = getInstancesInCluster(grandCluster);
if (controllers.size() == 0) {
@@ -918,7 +921,7 @@ public class ZKHelixAdmin implements HelixAdmin {
new ZKHelixDataAccessor(grandCluster, new ZkBaseDataAccessor<ZNRecord>(_zkClient));
Builder keyBuilder = accessor.keyBuilder();
- accessor.setProperty(keyBuilder.idealState(idealState.getResourceName()), idealState);
+ accessor.setProperty(keyBuilder.idealState(idealState.getResourceId().stringify()), idealState);
}
@Override
@@ -986,7 +989,7 @@ public class ZKHelixAdmin implements HelixAdmin {
}
idealState.setReplicas(Integer.toString(replica));
int partitions = idealState.getNumPartitions();
- String stateModelName = idealState.getStateModelDefRef();
+ String stateModelName = idealState.getStateModelDefId().stringify();
StateModelDefinition stateModDef = getStateModelDef(clusterName, stateModelName);
if (stateModDef == null) {
@@ -1103,7 +1106,7 @@ public class ZKHelixAdmin implements HelixAdmin {
currentData == null ? new ClusterConstraints(constraintType) : new ClusterConstraints(
currentData);
- constraints.addConstraintItem(constraintId, constraintItem);
+ constraints.addConstraintItem(ConstraintId.from(constraintId), constraintItem);
return constraints.getRecord();
}
}, AccessOption.PERSISTENT);
@@ -1123,7 +1126,7 @@ public class ZKHelixAdmin implements HelixAdmin {
if (currentData != null) {
ClusterConstraints constraints = new ClusterConstraints(currentData);
- constraints.removeConstraintItem(constraintId);
+ constraints.removeConstraintItem(ConstraintId.from(constraintId));
return constraints.getRecord();
}
return null;
@@ -1152,8 +1155,9 @@ public class ZKHelixAdmin implements HelixAdmin {
@Override
public void rebalance(String clusterName, IdealState currentIdealState, List<String> instanceNames) {
Set<String> activeInstances = new HashSet<String>();
- for (String partition : currentIdealState.getPartitionStringSet()) {
- activeInstances.addAll(currentIdealState.getRecord().getListField(partition));
+ for (PartitionId partition : currentIdealState.getPartitionSet()) {
+ activeInstances.addAll(IdealState.stringListFromPreferenceList(currentIdealState
+ .getPreferenceList(partition)));
}
instanceNames.removeAll(activeInstances);
Map<String, Object> previousIdealState =
@@ -1162,17 +1166,16 @@ public class ZKHelixAdmin implements HelixAdmin {
Map<String, Object> balancedRecord =
DefaultTwoStateStrategy.calculateNextIdealState(instanceNames, previousIdealState);
StateModelDefinition stateModDef =
- this.getStateModelDef(clusterName, currentIdealState.getStateModelDefRef());
+ this.getStateModelDef(clusterName, currentIdealState.getStateModelDefId().stringify());
if (stateModDef == null) {
- throw new HelixException("cannot find state model: "
- + currentIdealState.getStateModelDefRef());
+ throw new HelixException("cannot find state model: " + currentIdealState.getStateModelDefId());
}
String[] states = RebalanceUtil.parseStates(clusterName, stateModDef);
ZNRecord newIdealStateRecord =
- DefaultTwoStateStrategy.convertToZNRecord(balancedRecord,
- currentIdealState.getResourceName(), states[0], states[1]);
+ DefaultTwoStateStrategy.convertToZNRecord(balancedRecord, currentIdealState.getResourceId()
+ .stringify(), states[0], states[1]);
Set<String> partitionSet = new HashSet<String>();
partitionSet.addAll(newIdealStateRecord.getMapFields().keySet());
partitionSet.addAll(newIdealStateRecord.getListFields().keySet());
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/05c20e96/helix-core/src/main/java/org/apache/helix/model/ClusterConstraints.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/model/ClusterConstraints.java b/helix-core/src/main/java/org/apache/helix/model/ClusterConstraints.java
index ef47a12..2b014ae 100644
--- a/helix-core/src/main/java/org/apache/helix/model/ClusterConstraints.java
+++ b/helix-core/src/main/java/org/apache/helix/model/ClusterConstraints.java
@@ -27,6 +27,7 @@ import java.util.TreeMap;
import org.apache.helix.HelixProperty;
import org.apache.helix.ZNRecord;
+import org.apache.helix.api.ConstraintId;
import org.apache.helix.model.Message.MessageType;
import org.apache.helix.model.builder.ConstraintItemBuilder;
import org.apache.log4j.Logger;
@@ -42,9 +43,11 @@ public class ClusterConstraints extends HelixProperty {
*/
public enum ConstraintAttribute {
STATE,
+ STATE_MODEL,
MESSAGE_TYPE,
TRANSITION,
RESOURCE,
+ PARTITION,
INSTANCE,
CONSTRAINT_VALUE
}
@@ -53,7 +56,9 @@ public class ClusterConstraints extends HelixProperty {
* Possible special values that constraint attributes can take
*/
public enum ConstraintValue {
- ANY
+ ANY,
+ N,
+ R
}
/**
@@ -65,7 +70,8 @@ public class ClusterConstraints extends HelixProperty {
}
// constraint-id -> constraint-item
- private final Map<String, ConstraintItem> _constraints = new HashMap<String, ConstraintItem>();
+ private final Map<ConstraintId, ConstraintItem> _constraints =
+ new HashMap<ConstraintId, ConstraintItem>();
/**
* Instantiate constraints as a given type
@@ -96,7 +102,7 @@ public class ClusterConstraints extends HelixProperty {
builder.addConstraintAttributes(_record.getMapField(constraintId)).build();
// ignore item with empty attributes or no constraint-value
if (item.getAttributes().size() > 0 && item.getConstraintValue() != null) {
- addConstraintItem(constraintId, item);
+ addConstraintItem(ConstraintId.from(constraintId), item);
} else {
LOG.error("Skip invalid constraint. key: " + constraintId + ", value: "
+ _record.getMapField(constraintId));
@@ -109,13 +115,13 @@ public class ClusterConstraints extends HelixProperty {
* @param constraintId unique constraint identifier
* @param item the constraint as a {@link ConstraintItem}
*/
- public void addConstraintItem(String constraintId, ConstraintItem item) {
+ public void addConstraintItem(ConstraintId constraintId, ConstraintItem item) {
Map<String, String> map = new TreeMap<String, String>();
for (ConstraintAttribute attr : item.getAttributes().keySet()) {
map.put(attr.toString(), item.getAttributeValue(attr));
}
map.put(ConstraintAttribute.CONSTRAINT_VALUE.toString(), item.getConstraintValue());
- _record.setMapField(constraintId, map);
+ _record.setMapField(constraintId.stringify(), map);
_constraints.put(constraintId, item);
}
@@ -123,8 +129,8 @@ public class ClusterConstraints extends HelixProperty {
* Add multiple constraint items.
* @param items (constraint identifier, {@link ConstrantItem}) pairs
*/
- public void addConstraintItems(Map<String, ConstraintItem> items) {
- for (String constraintId : items.keySet()) {
+ public void addConstraintItems(Map<ConstraintId, ConstraintItem> items) {
+ for (ConstraintId constraintId : items.keySet()) {
addConstraintItem(constraintId, items.get(constraintId));
}
}
@@ -133,9 +139,9 @@ public class ClusterConstraints extends HelixProperty {
* remove a constraint-item
* @param constraintId unique constraint identifier
*/
- public void removeConstraintItem(String constraintId) {
+ public void removeConstraintItem(ConstraintId constraintId) {
_constraints.remove(constraintId);
- _record.getMapFields().remove(constraintId);
+ _record.getMapFields().remove(constraintId.stringify());
}
/**
@@ -143,7 +149,7 @@ public class ClusterConstraints extends HelixProperty {
* @param constraintId unique constraint identifier
* @return {@link ConstraintItem} or null if not present
*/
- public ConstraintItem getConstraintItem(String constraintId) {
+ public ConstraintItem getConstraintItem(ConstraintId constraintId) {
return _constraints.get(constraintId);
}
@@ -163,6 +169,14 @@ public class ClusterConstraints extends HelixProperty {
}
/**
+ * Get all constraint items in this collection of constraints
+ * @return map of constraint id to constraint item
+ */
+ public Map<ConstraintId, ConstraintItem> getConstraintItems() {
+ return _constraints;
+ }
+
+ /**
* convert a message to constraint attribute pairs
* @param msg a {@link Message} containing constraint attributes
* @return constraint attribute scope-value pairs
@@ -182,13 +196,15 @@ public class ClusterConstraints extends HelixProperty {
if (msg.getTgtName() != null) {
attributes.put(ConstraintAttribute.INSTANCE, msg.getTgtName());
}
+ if (msg.getStateModelDefId() != null) {
+ attributes.put(ConstraintAttribute.STATE_MODEL, msg.getStateModelDefId().stringify());
+ }
}
return attributes;
}
@Override
public boolean isValid() {
- // TODO Auto-generated method stub
return true;
}
http://git-wip-us.apache.org/repos/asf/incubator-helix/blob/05c20e96/helix-core/src/main/java/org/apache/helix/model/ExternalView.java
----------------------------------------------------------------------
diff --git a/helix-core/src/main/java/org/apache/helix/model/ExternalView.java b/helix-core/src/main/java/org/apache/helix/model/ExternalView.java
index b8f4ee5..f4304f4 100644
--- a/helix-core/src/main/java/org/apache/helix/model/ExternalView.java
+++ b/helix-core/src/main/java/org/apache/helix/model/ExternalView.java
@@ -146,6 +146,9 @@ public class ExternalView extends HelixProperty {
*/
public Map<ParticipantId, State> getStateMap(PartitionId partitionId) {
Map<String, String> rawStateMap = getStateMap(partitionId.stringify());
+ if (rawStateMap == null) {
+ return null;
+ }
ImmutableMap.Builder<ParticipantId, State> builder =
new ImmutableMap.Builder<ParticipantId, State>();
for (String participantName : rawStateMap.keySet()) {