xyuanlu commented on code in PR #2986: URL: https://github.com/apache/helix/pull/2986#discussion_r1924494000
########## 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) { + // Build instance-to-zone mapping for quick zone lookups. + Map<String, String> instanceToZoneMapping = new HashMap<>(); Review Comment: I think this part of the algo can be simplified. Please correct me if I am wrong. Input of this function: Current Zone->instance mapping. This is includes all instances and their zone info. Some(or all) instances are not included in previous assignment Current VG -> instance mapping. Return value: new VG -> instance mapping. Now we compute so many in between data structure, we build map and reverse map for same set of information. I think it can be improved to: 2. If current VG -> instance mapping is not null, we compute VG->Zone mapping of existing assignment. 3. Create new assignment result map, for each VG, dd all instances in those zones. 4. Assign remaining Zones (these are now zones that are not existed in previous mapping) a. We build map of zone -> instance count. b. Sort the VG in to min heap, Dont need to build a map, but sort based on instance size (we have the VG to zones map and zone to number of instance map) c. Sort remaining zones in decending order using zone -> instance count. Again, we dont need to build a map. d. Do the assignment, update map. In this way, we only build 1 data structure in between, and algo is simpler. - - 5 -- 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-unsubscr...@helix.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@helix.apache.org For additional commands, e-mail: reviews-h...@helix.apache.org