jasperjiaguo commented on code in PR #11578:
URL: https://github.com/apache/pinot/pull/11578#discussion_r1372507127


##########
pinot-controller/src/main/java/org/apache/pinot/controller/helix/core/assignment/instance/MirrorServerSetInstancePartitionSelector.java:
##########
@@ -0,0 +1,353 @@
+/**
+ * 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.instance;
+
+import com.google.common.base.Preconditions;
+import java.util.AbstractMap;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Random;
+import java.util.Set;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+import javax.annotation.Nullable;
+import org.apache.helix.model.InstanceConfig;
+import org.apache.pinot.common.assignment.InstancePartitions;
+import 
org.apache.pinot.spi.config.table.assignment.InstanceReplicaGroupPartitionConfig;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * Detailed design see 
https://docs.google.com/document/d/1xxPkGPxyY21gAkFi9gtFDeSzEXjPjp-IQW70kHynsL8
+ * During each creation/update/scale, the algorithm will refer to the 
corresponding tenant level instance partitions and
+ * generate an instance partition by taking numInstancePerReplicaGroup mirror 
server sets from the tenant level
+ * instance partitions.
+ *
+ * If an existingInstancePartition is provided, the algorithm will generate a 
best effort assignment that resembles
+ * the existingInstancePartition.
+ *
+ * Assumptions for this algorithm:
+ *  1. The number of replica groups in the tenant level instance partitions is 
the same as the number of replica groups
+ *     in the table config.
+ *  2. The number of partitions at replica group level is 1
+ *  3. This algorithm only works for replica group based table assignment
+ */
+public class MirrorServerSetInstancePartitionSelector extends 
InstancePartitionSelector {
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(MirrorServerSetInstancePartitionSelector.class);
+  private final InstancePartitions _preConfiguredInstancePartitions;
+
+  // dimensions of target instance partition
+  private final int _numTargetInstancesPerReplicaGroup;
+  private final int _numTargetReplicaGroups;
+  private final int _numTargetTotalInstances;
+
+  // dimensions of pre-configured instance partition
+  private int _numPreConfiguredReplicaGroups;
+  private int _numPreConfiguredInstancesPerReplicaGroup;
+
+  // dimensions of existing instance partition
+  private int _numExistingReplicaGroups;
+  private int _numExistingInstancesPerReplicaGroup;
+
+  // look up tables for pre-configured instance partition
+  private final List<List<String>> _preConfiguredMirroredServerLists = new 
ArrayList<>();
+  private final Map<String, Integer> _preConfiguredInstanceNameToOffsetMap = 
new HashMap<>();
+
+  private final List<List<String>> _existingMirroredServerLists = new 
ArrayList<>();
+
+  public 
MirrorServerSetInstancePartitionSelector(InstanceReplicaGroupPartitionConfig 
replicaGroupPartitionConfig,
+      String tableNameWithType, @Nullable InstancePartitions 
existingInstancePartitions,
+      InstancePartitions preConfiguredInstancePartitions) {
+    super(replicaGroupPartitionConfig, tableNameWithType, 
existingInstancePartitions);
+    _preConfiguredInstancePartitions = preConfiguredInstancePartitions;
+    _numTargetInstancesPerReplicaGroup = 
_replicaGroupPartitionConfig.getNumInstancesPerReplicaGroup();
+    _numTargetReplicaGroups = 
_replicaGroupPartitionConfig.getNumReplicaGroups();
+    _numTargetTotalInstances = _numTargetInstancesPerReplicaGroup * 
_numTargetReplicaGroups;
+  }
+
+  /**
+   * validate if the poolToInstanceConfigsMap is a valid input for 
pre-configuration based replica-group selection
+   */
+  private void validatePoolDiversePreconditions(Map<Integer, 
List<InstanceConfig>> poolToInstanceConfigsMap) {
+
+    LOGGER.info("Validating pre-configured instance partitions for 
pre-configuration based replica-group selection");
+
+    // numTargetInstancesPerReplica should be positive
+    Preconditions.checkState(_numTargetInstancesPerReplicaGroup > 0,
+        "Number of instances per replica must be positive");
+    LOGGER.info("Number of instances per replica: {}", 
_numTargetInstancesPerReplicaGroup);
+    // _numTargetReplicaGroups should be positive
+    Preconditions.checkState(_numTargetReplicaGroups > 0, "Number of 
replica-groups must be positive");
+    LOGGER.info("Number of replica-groups: {}", _numTargetReplicaGroups);
+    // validate target partition count is 1
+    Preconditions.checkState(_replicaGroupPartitionConfig.getNumPartitions() 
<= 1,
+        "This algorithm does not support table level partitioning for target 
assignment");
+    LOGGER.info("Number of partitions: {}", 
_replicaGroupPartitionConfig.getNumPartitions());
+
+    // Validate the existing instance partitions is null or has only one 
partition
+    Preconditions.checkState(
+        (_existingInstancePartitions == null || 
_existingInstancePartitions.getNumPartitions() == 1),
+        "This algorithm does not support table level partitioning for existing 
assignment");
+    LOGGER.info("Number of partitions in existing instance partitions: {}", 
_existingInstancePartitions == null ? 0
+        : _existingInstancePartitions.getNumPartitions());
+
+    _numExistingReplicaGroups =
+        _existingInstancePartitions == null ? 0 : 
_existingInstancePartitions.getNumReplicaGroups();
+    _numExistingInstancesPerReplicaGroup =
+        _existingInstancePartitions == null ? 0 : 
_existingInstancePartitions.getInstances(0, 0).size();
+
+    // Validate the pre-configured instance partitions is not null and has 
only one partition
+    Preconditions.checkState(_preConfiguredInstancePartitions != null,
+        "Pre-configured instance partitions must be provided for 
pre-configuration based selection");
+    
Preconditions.checkState(_preConfiguredInstancePartitions.getNumPartitions() == 
1,
+        "This algorithm does not support table level partitioning for 
pre-configured assignment");
+    LOGGER.info("Number of partitions in pre-configured instance partitions: 
{}", _preConfiguredInstancePartitions
+        .getNumPartitions());
+
+    // Validate the number of replica-groups in the pre-configured instance 
partitions is equal to the target
+    // number of replica-groups
+    _numPreConfiguredReplicaGroups = 
_preConfiguredInstancePartitions.getNumReplicaGroups();
+    Preconditions.checkState(_numPreConfiguredReplicaGroups == 
_numTargetReplicaGroups,
+        "The number of replica-groups %s in the pre-configured instance 
partitions "
+            + "is not equal to the target number of replica-groups %s", 
_numPreConfiguredReplicaGroups,
+        _numTargetReplicaGroups);
+    LOGGER.info("Number of replica-groups in pre-configured instance 
partitions: {}", _numPreConfiguredReplicaGroups);
+
+    // Validate the number of instances per replica-group in the 
pre-configured instance partitions is greater than or
+    // equal to the target number of instances per replica-group
+    _numPreConfiguredInstancesPerReplicaGroup = 
_preConfiguredInstancePartitions.getInstances(0, 0).size();
+    Preconditions.checkState(_numPreConfiguredInstancesPerReplicaGroup >= 
_numTargetInstancesPerReplicaGroup,
+        "The number of instances per replica-group in the pre-configured "

Review Comment:
   reformated the file with formatter



-- 
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: commits-unsubscr...@pinot.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org
For additional commands, e-mail: commits-h...@pinot.apache.org

Reply via email to