Jackie-Jiang commented on a change in pull request #4269: Add interface and implementations for the new segment assignment URL: https://github.com/apache/incubator-pinot/pull/4269#discussion_r290127185
########## File path: pinot-controller/src/main/java/org/apache/pinot/controller/helix/core/assignment/segment/ReplicaGroupSegmentAssignmentStrategy.java ########## @@ -0,0 +1,270 @@ +/** + * 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. + */ +package org.apache.pinot.controller.helix.core.assignment.segment; + +import com.google.common.base.Preconditions; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.PriorityQueue; +import java.util.Set; +import java.util.TreeMap; +import org.apache.helix.ZNRecord; +import org.apache.helix.store.zk.ZkHelixPropertyStore; +import org.apache.pinot.common.config.ReplicaGroupStrategyConfig; +import org.apache.pinot.common.config.TableConfig; +import org.apache.pinot.common.metadata.ZKMetadataProvider; +import org.apache.pinot.common.metadata.segment.ColumnPartitionMetadata; +import org.apache.pinot.common.metadata.segment.OfflineSegmentZKMetadata; +import org.apache.pinot.common.utils.CommonConstants.Helix.StateModel.SegmentOnlineOfflineStateModel; +import org.apache.pinot.common.utils.Pairs; +import org.apache.pinot.controller.helix.core.assignment.InstancePartitions; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * Segment assignment strategy that assigns segment to the instance in the replica with the least number of segments. + * <p>Among multiple replicas, always mirror the assignment (pick the same index of the instance). + * <p> + * Inside each partition, assign the segment to the servers with the least segments already assigned. In case of a + * tie, assign to the server with the smallest index in the list. Do this for one replica and mirror the assignment to + * other replicas. + * <p> + * To rebalance a table, inside each partition, first calculate the number of segments on each server, loop over all + * the segments and keep the assignment if number of segments for the server has not been reached and track the not + * assigned segments, then assign the left-over segments to the servers with the least segments, or the smallest index + * if there is a tie. Repeat the process for all the partitions in one replica, and mirror the assignment to other + * replicas. With this greedy algorithm, the result is deterministic and with minimum segment moves. + */ +public class ReplicaGroupSegmentAssignmentStrategy implements SegmentAssignmentStrategy { + private static final Logger LOGGER = LoggerFactory.getLogger(ReplicaGroupSegmentAssignmentStrategy.class); + + private ZkHelixPropertyStore<ZNRecord> _propertyStore; + private String _tableNameWithType; + private String _partitionColumn; + + @Override + public void init(ZkHelixPropertyStore<ZNRecord> propertyStore, TableConfig tableConfig) { + _propertyStore = propertyStore; + _tableNameWithType = tableConfig.getTableName(); + ReplicaGroupStrategyConfig strategyConfig = tableConfig.getValidationConfig().getReplicaGroupStrategyConfig(); + _partitionColumn = strategyConfig != null ? strategyConfig.getPartitionColumn() : null; + + if (_partitionColumn == null) { + LOGGER.info("Initialized ReplicaGroupSegmentAssignmentStrategy for table: {} without partition column", + _tableNameWithType); + } else { + LOGGER.info("Initialized ReplicaGroupSegmentAssignmentStrategy for table: {} with partition column: {}", + _tableNameWithType, _partitionColumn); + } + } + + @Override + public List<String> assignSegment(String segmentName, Map<String, Map<String, String>> currentAssignment, + InstancePartitions instancePartitions) { + // Fetch partition id from segment ZK metadata if partition column is configured + int partitionId = 0; + if (_partitionColumn == null) { + Preconditions.checkState(instancePartitions.getNumPartitions() == 1, + "The instance partitions should contain only 1 partition"); + } else { + OfflineSegmentZKMetadata segmentZKMetadata = + ZKMetadataProvider.getOfflineSegmentZKMetadata(_propertyStore, _tableNameWithType, segmentName); + Preconditions + .checkState(segmentZKMetadata != null, "Failed to fetch segment ZK metadata for table: %s, segment: %s", + _tableNameWithType, segmentName); + partitionId = getPartitionId(segmentZKMetadata, instancePartitions); + } + + // First assign the segment to replica 0 + List<String> instances = instancePartitions.getInstances(partitionId, 0); + int[] numSegmentsAssigned = SegmentAssignmentUtils.getNumSegmentsAssigned(currentAssignment, instances); + int minNumSegmentsAssigned = numSegmentsAssigned[0]; + int instanceIdWithLeastSegmentsAssigned = 0; + int numInstances = numSegmentsAssigned.length; + for (int instanceId = 1; instanceId < numInstances; instanceId++) { + if (numSegmentsAssigned[instanceId] < minNumSegmentsAssigned) { + minNumSegmentsAssigned = numSegmentsAssigned[instanceId]; + instanceIdWithLeastSegmentsAssigned = instanceId; + } + } + + // Mirror the assignment to all replicas Review comment: Yes. We are going to always use mirror. I cannot think of any use case which needs different number of servers for each replica group, and we don't support that as well. If we actually need that, we can add the support later. ---------------------------------------------------------------- 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: us...@infra.apache.org With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org For additional commands, e-mail: commits-h...@pinot.apache.org