You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@helix.apache.org by GitBox <gi...@apache.org> on 2020/09/08 20:33:33 UTC

[GitHub] [helix] zhangmeng916 commented on a change in pull request #1307: Add TrieClusterTopology for retrieving hierarchical topology

zhangmeng916 commented on a change in pull request #1307:
URL: https://github.com/apache/helix/pull/1307#discussion_r485180072



##########
File path: helix-core/src/main/java/org/apache/helix/model/TrieClusterTopology.java
##########
@@ -0,0 +1,244 @@
+package org.apache.helix.model;
+
+/*
+ * 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.Arrays;
+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 java.util.stream.Collectors;
+
+import org.apache.helix.HelixException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * This is a class that uses a trie data structure to represent cluster topology. Each node
+ * except the terminal node represents a certain domain in the topology, and an terminal node
+ * represents an instance in the cluster.
+ */
+public class TrieClusterTopology {
+  private static Logger logger = LoggerFactory.getLogger(TrieClusterTopology.class);
+  private static final String DELIMITER = "/";
+  private static final String CONNECTOR = "_";
+
+  private final TrieNode _rootNode;
+  private final String[] _topologyKeys;
+
+  public TrieClusterTopology(final List<String> liveNodes,
+      final Map<String, InstanceConfig> instanceConfigMap, ClusterConfig clusterConfig) {
+    if (instanceConfigMap == null || !instanceConfigMap.keySet().containsAll(liveNodes)) {
+      throw new HelixException(String.format("Config for instances %s is not found!",
+          instanceConfigMap == null ? liveNodes : liveNodes.removeAll(instanceConfigMap.keySet())));
+    }
+    // A list of all keys in cluster topology, e.g., a cluster topology defined as
+    // /group/zone/rack/instance will return ["group", "zone", "rack", "instance"].
+    _topologyKeys = Arrays.asList(clusterConfig.getTopology().trim().split(DELIMITER)).stream()
+        .filter(str -> !str.isEmpty()).collect(Collectors.toList()).toArray(new String[0]);
+    _rootNode = new TrieNode(new HashMap<>(), DELIMITER);
+    constructTrie(instanceConfigMap);
+  }
+
+  /**
+   * Return the topology of a cluster as a map. The key of the map is the first level of
+   * domain, and the value is a set of string that represents the path to each end node in that
+   * domain. E.g., assume the topology is defined as /group/zone/rack/instance, the result may be {
+   * ["group_0": {"zone_0/rack_0/instance_0", "zone_1/rack_1/instance_1"}], ["group_1": {"zone_1
+   * /rack_1/instance_1", "zone_1/rack_1/instance_2"}]}
+   */
+  public Map<String, Set<String>> getClusterTopology() {

Review comment:
       This is the API by default gets the full topology of the cluster. The key of the map will be the first level of domain defined in topology, e.g. zone, faultdomain, etc. 




----------------------------------------------------------------
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:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@helix.apache.org
For additional commands, e-mail: reviews-help@helix.apache.org