xyuanlu commented on code in PR #2986: URL: https://github.com/apache/helix/pull/2986#discussion_r1924494364
########## helix-core/src/main/java/org/apache/helix/cloud/topology/FaultZoneBasedVirtualGroupAssignmentAlgorithm.java: ########## @@ -0,0 +1,195 @@ +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 java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.PriorityQueue; +import java.util.Queue; +import java.util.Set; +import java.util.TreeSet; +import java.util.stream.Collectors; + +import org.apache.commons.math3.util.Pair; + +import static org.apache.helix.util.VirtualTopologyUtil.computeVirtualGroupId; + +/** + * A virtual group assignment algorithm that assigns zones and their instances to virtual groups + * a way that preserves existing zone-to-group assignments whenever possible, and balances any + * remaining unassigned zones across the least-loaded groups. If the requested number of groups + * differs from the existing assignment, a new distribution is computed. Otherwise, if a zone + * already exists in the provided assignment, all its instances (including newly discovered ones) + * are placed in the same group, ensuring no zone is split across multiple virtual groups. + */ +public class FaultZoneBasedVirtualGroupAssignmentAlgorithm implements VirtualGroupAssignmentAlgorithm { + + private static final FaultZoneBasedVirtualGroupAssignmentAlgorithm _instance = + new FaultZoneBasedVirtualGroupAssignmentAlgorithm(); + + private FaultZoneBasedVirtualGroupAssignmentAlgorithm() { + } + + public static FaultZoneBasedVirtualGroupAssignmentAlgorithm getInstance() { + return _instance; + } + + @Override + public Map<String, Set<String>> computeAssignment(int numGroups, String virtualGroupName, + Map<String, Set<String>> zoneMapping, Map<String, Set<String>> virtualZoneMapping) { Review Comment: nit: also please align naming, we have Physical zone and Virtual group I think. Virtual zone is confusing. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
