Add API in StateModelDefinition to get second top states Currently, we are facing an issue that state trasition jumpped two steps ahead for one instance, which will cause issue. To fix this, we need an API to get the second top states.
Project: http://git-wip-us.apache.org/repos/asf/helix/repo Commit: http://git-wip-us.apache.org/repos/asf/helix/commit/5798bfdc Tree: http://git-wip-us.apache.org/repos/asf/helix/tree/5798bfdc Diff: http://git-wip-us.apache.org/repos/asf/helix/diff/5798bfdc Branch: refs/heads/master Commit: 5798bfdcac1a30c1a81596ca24bffd659b6f79a5 Parents: c8ec624 Author: Junkai Xue <[email protected]> Authored: Mon Sep 25 14:46:58 2017 -0700 Committer: Junkai Xue <[email protected]> Committed: Mon Sep 25 14:46:58 2017 -0700 ---------------------------------------------------------------------- .../helix/model/StateModelDefinition.java | 21 ++++++++++++++++++++ 1 file changed, 21 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/helix/blob/5798bfdc/helix-core/src/main/java/org/apache/helix/model/StateModelDefinition.java ---------------------------------------------------------------------- diff --git a/helix-core/src/main/java/org/apache/helix/model/StateModelDefinition.java b/helix-core/src/main/java/org/apache/helix/model/StateModelDefinition.java index 420f0e5..d86e286 100644 --- a/helix-core/src/main/java/org/apache/helix/model/StateModelDefinition.java +++ b/helix-core/src/main/java/org/apache/helix/model/StateModelDefinition.java @@ -23,9 +23,11 @@ import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; +import java.util.HashSet; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.TreeMap; import org.apache.helix.HelixDefinedState; @@ -184,6 +186,25 @@ public class StateModelDefinition extends HelixProperty { return _statesCountMap.get(state); } + /** + * Get the second top states, which need one step transition to top state + * @return a set of second top states + */ + public Set<String> getSecondTopStates() { + Set<String> secondTopStates = new HashSet<String>(); + if (_statesPriorityList == null || _statesPriorityList.isEmpty()) { + return secondTopStates; + } + String topState = _statesPriorityList.get(0); + for (String state : _stateTransitionTable.keySet()) { + Map<String, String> transitionMap = _stateTransitionTable.get(state); + if (transitionMap.containsKey(topState) && transitionMap.get(topState).equals(topState)) { + secondTopStates.add(state); + } + } + return secondTopStates; + } + @Override public boolean isValid() { return StateModelDefinitionValidator.isStateModelDefinitionValid(this);
