zhangmeng916 commented on a change in pull request #1307: URL: https://github.com/apache/helix/pull/1307#discussion_r491130388
########## File path: helix-core/src/main/java/org/apache/helix/api/topology/ClusterTopology.java ########## @@ -0,0 +1,248 @@ +package org.apache.helix.api.topology; + +/* + * 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. + */ + +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.Deque; +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 org.apache.helix.model.ClusterConfig; +import org.apache.helix.model.ClusterTrie; +import org.apache.helix.model.InstanceConfig; +import org.apache.helix.model.TrieNode; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import static org.apache.helix.model.ClusterTrie.CONNECTOR; +import static org.apache.helix.model.ClusterTrie.DELIMITER; + + +public class ClusterTopology { + private static Logger logger = LoggerFactory.getLogger(ClusterTopology.class); + + private final ClusterTrie _trieClusterTopology; + + public ClusterTopology(final List<String> liveNodes, + final Map<String, InstanceConfig> instanceConfigMap, ClusterConfig clusterConfig) { + _trieClusterTopology = new ClusterTrie(liveNodes, instanceConfigMap, clusterConfig); + } + + /** + * Return the whole topology of a cluster as a map. The key of the map is the first level of + * domain, and the value is a list of string that represents the path to each end node in that + * domain. E.g., assume the topology is defined as /group/zone/rack/host, the result may be { + * ["/group:0": {"/zone:0/rack:0/host:0", "/zone:1/rack:1/host:1"}], ["/group:1": {"/zone:1 + * /rack:1/host:1", "/zone:1/rack:1/host:2"}]} + */ + public Map<String, List<String>> getTopologyMap() { + return getTopologyUnderDomain(new HashMap<>()); + } + + /** + * Return all the instances under fault zone type. The key of the returned map is each fault + * zone name, and the value is a list of string that represents the path to each end node in + * that fault zone. + * @return , e.g. if the fault zone is "zone", it may return {["/group:0/zone:0": {"rack:0/host + * :0", "rack:1/host:1"}, ["/group:0/zone:1": {"/rack:0:host:2", "/rack:1/host:3"}]} + */ + public Map<String, List<String>> getFaultZoneMap() { + String faultZone = _trieClusterTopology.getFaultZoneType(); + if (faultZone == null) { + throw new IllegalArgumentException("The fault zone in cluster config is not defined"); + } + return getTopologyUnderDomainType(faultZone); + } + + /** + * Return the instances whose domain field is not valid + */ + public List<String> getInvalidInstances() { + return _trieClusterTopology.getInvalidInstances(); + } + + /** + * Return the topology under a certain domain as a map. The key of the returned map is the next + * level domain, and the value is a list of string that represents the path to each end node in + * that domain. + * @param domain A map defining the domain name and its value, e.g. {["group": "1"], ["zone", + * "2"]} + * @return the topology under the given domain, e.g. {["/group:1/zone:2/rack:0": {"/host:0", + * "/host:1"}, ["/group:1/zone:2/rack:1": {"/host:2", "/host:3"}]} + */ + private Map<String, List<String>> getTopologyUnderDomain(Map<String, String> domain) { Review comment: I tried with a null value for domainType, but feel the logic is still very difficult to merge. If you look at the two functions: /// private TrieNode getStartNode(LinkedHashMap<String, String> domainMap) { TrieNode curNode = _trieClusterTopology.getRootNode(); TrieNode nextNode; for (Map.Entry<String, String> entry : domainMap.entrySet()) { nextNode = curNode.getChildren().get(entry.getKey() + CONNECTOR + entry.getValue()); if (nextNode == null) { throw new IllegalArgumentException(String .format("The input domain %s does not have the value %s", entry.getKey(), entry.getValue())); } curNode = nextNode; } return curNode; } private List<TrieNode> getStartNodes(String domainType) { List<TrieNode> results = new ArrayList<>(); TrieNode curNode = _trieClusterTopology.getRootNode(); Deque<TrieNode> nodeStack = new ArrayDeque<>(); nodeStack.push(curNode); while (!nodeStack.isEmpty()) { curNode = nodeStack.pop(); if (curNode.getNodeKey().equals(domainType)) { results.add(curNode); } else { for (TrieNode child : curNode.getChildren().values()) { nodeStack.push(child); } } } return results; } /// They're not easy to combine, since one is to find a specific node in the trie, while the other is to find all nodes for a certain level across the trie. Thoughts? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
