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 2022/01/19 19:06:57 UTC

[GitHub] [helix] jiajunwang commented on a change in pull request #1935: Implement java API and utils for virtual topology group

jiajunwang commented on a change in pull request #1935:
URL: https://github.com/apache/helix/pull/1935#discussion_r788057091



##########
File path: helix-core/src/main/java/org/apache/helix/cloud/topology/VirtualTopologyGroupStrategy.java
##########
@@ -0,0 +1,80 @@
+package org.apache.helix.cloud.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 com.google.common.collect.ImmutableMap;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import org.apache.helix.util.HelixUtil;
+
+public enum VirtualTopologyGroupStrategy {
+
+  /**
+   * A default assignment strategy that is deterministic and stable where:
+   * 1. assignment is guaranteed consistent for same inputs.
+   * 2. number of instance shuffles is reduced.
+   */
+  DEFAULT {
+    @Override
+    public Map<String, Set<String>> computeAssignment(
+        int numGroups, String virtualGroupName, Map<String, Set<String>> zoneMapping) {
+      List<String> sortedInstances = HelixUtil.sortAndFlattenZoneMapping(zoneMapping);
+      Map<String, Set<String>> assignment = new HashMap<>();
+      int instancesPerGroup = sortedInstances.size() / numGroups;
+
+      // instances.size = instancePerGroup * numGroups + residual
+      // step 1, continuously assign following groupInd order until entering residual section
+      for (int instanceInd = 0; instanceInd < instancesPerGroup * numGroups; instanceInd++) {
+        int groupIndex = instanceInd / instancesPerGroup;
+        String groupId = computeVirtualGroupId(groupIndex, virtualGroupName);
+        assignment.putIfAbsent(groupId, new HashSet<>());
+        assignment.get(groupId).add(sortedInstances.get(instanceInd));
+      }
+      // step 2, assign the residuals, either stick to the last assigned group or round-robin to all groups.
+      for (int instanceInd = instancesPerGroup * numGroups; instanceInd < sortedInstances.size(); instanceInd++) {
+        int groupIndex = numGroups <= zoneMapping.keySet().size()
+            ? numGroups - 1
+            : instanceInd % numGroups;
+        String groupId = computeVirtualGroupId(groupIndex, virtualGroupName);
+        assignment.putIfAbsent(groupId, new HashSet<>());
+        assignment.get(groupId).add(sortedInstances.get(instanceInd));
+      }
+      return ImmutableMap.copyOf(assignment);
+    }
+  };
+
+  /**
+   * Compute the assignment for each virtual topology group.
+   *
+   * @param numGroups number of the virtual groups
+   * @param virtualGroupName virtual group name
+   * @param zoneMapping current zone mapping from zoneId to instanceIds
+   * @return the assignment as mapping from virtual group ID to instanceIds
+   */
+  public abstract Map<String, Set<String>> computeAssignment(
+      int numGroups, String virtualGroupName, Map<String, Set<String>> zoneMapping);
+
+  private static String computeVirtualGroupId(int groupIndex, String virtualGroupName) {
+    return virtualGroupName + "_" + groupIndex;

Review comment:
       nit, avoid hard code?




-- 
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.

To unsubscribe, e-mail: reviews-unsubscribe@helix.apache.org

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