snleee commented on a change in pull request #4504: [Instance Assignment] Add 
implementations for instance assignment
URL: https://github.com/apache/incubator-pinot/pull/4504#discussion_r311762224
 
 

 ##########
 File path: 
pinot-controller/src/main/java/org/apache/pinot/controller/helix/core/assignment/instance/InstanceReplicaPartitionSelector.java
 ##########
 @@ -0,0 +1,187 @@
+/**
+ * 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.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.TreeMap;
+import org.apache.helix.model.InstanceConfig;
+import org.apache.pinot.common.config.instance.InstanceReplicaPartitionConfig;
+import org.apache.pinot.controller.helix.core.assignment.InstancePartitions;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * The instance replica/partition selector is responsible for selecting the 
instances for each replica and partition.
+ */
+public class InstanceReplicaPartitionSelector {
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(InstanceReplicaPartitionSelector.class);
+
+  private final InstanceReplicaPartitionConfig _replicaPartitionConfig;
+  private final String _tableNameWithType;
+  private final int _tableNameHash;
+
+  public InstanceReplicaPartitionSelector(InstanceReplicaPartitionConfig 
replicaPartitionConfig,
+      String tableNameWithType) {
+    _replicaPartitionConfig = replicaPartitionConfig;
+    _tableNameWithType = tableNameWithType;
+    _tableNameHash = Math.abs(tableNameWithType.hashCode());
+  }
+
+  /**
+   * Selects instances based on the replica and partition config, and stores 
the result into the given instance
+   * partitions.
+   */
+  public InstancePartitions selectInstances(Map<Integer, List<InstanceConfig>> 
poolToInstanceConfigsMap) {
+    int numPools = poolToInstanceConfigsMap.size();
+    Preconditions.checkState(numPools != 0, "No pool qualified for selection");
+    List<Integer> pools = new ArrayList<>(poolToInstanceConfigsMap.keySet());
+    pools.sort(null);
+    LOGGER.info("Starting instance replica/partition selection for table: {} 
with hash: {} from pools: {}",
+        _tableNameWithType, _tableNameHash, pools);
+
+    InstancePartitions instancePartitions = new 
InstancePartitions(_tableNameWithType);
+
+    if (_replicaPartitionConfig.isReplicaGroupBased()) {
+      // Replica-group based selection
+
+      int numReplicas = _replicaPartitionConfig.getNumReplicas();
+      Preconditions.checkState(numReplicas > 0, "Number of replicas must be 
positive");
+      Map<Integer, List<Integer>> poolToReplicaIdsMap = new TreeMap<>();
+      for (int replicaId = 0; replicaId < numReplicas; replicaId++) {
+        // Pick one pool for each replica based on the table name hash
+        int pool = pools.get((_tableNameHash + replicaId) % numPools);
+        poolToReplicaIdsMap.computeIfAbsent(pool, k -> new 
ArrayList<>()).add(replicaId);
+      }
+      LOGGER.info("Selecting {} replicas from pool: {} for table: {}", 
numReplicas, poolToReplicaIdsMap,
+          _tableNameWithType);
+
+      int numInstancesPerReplica = 
_replicaPartitionConfig.getNumInstancesPerReplica();
+      if (numInstancesPerReplica > 0) {
+        // Check if we have enough instances if number of instances per 
replica is configured
+        for (Map.Entry<Integer, List<Integer>> entry : 
poolToReplicaIdsMap.entrySet()) {
+          int pool = entry.getKey();
+          int numInstancesInPool = poolToInstanceConfigsMap.get(pool).size();
+          int numInstancesToSelect = numInstancesPerReplica * 
entry.getValue().size();
+          Preconditions.checkState(numInstancesToSelect <= numInstancesInPool,
+              "Not enough qualified instances from pool: %s (%s in the pool, 
asked for %s)", pool, numInstancesInPool,
+              numInstancesToSelect);
+        }
+      } else {
+        // Use as many instances as possible if number of instances per 
replica is not configured
+        numInstancesPerReplica = Integer.MAX_VALUE;
+        for (Map.Entry<Integer, List<Integer>> entry : 
poolToReplicaIdsMap.entrySet()) {
+          int pool = entry.getKey();
+          int numReplicasInPool = entry.getValue().size();
+          int numInstancesInPool = poolToInstanceConfigsMap.get(pool).size();
+          Preconditions.checkState(numReplicasInPool <= numInstancesInPool,
+              "Not enough qualified instances from pool: %s, cannot select %s 
replicas from %s instances", pool,
+              numReplicasInPool, numInstancesInPool);
+          numInstancesPerReplica = Math.min(numInstancesPerReplica, 
numInstancesInPool / numReplicasInPool);
+        }
+      }
+      LOGGER.info("Selecting {} instances per replica for table: {}", 
numInstancesPerReplica, _tableNameWithType);
+
+      String[][] replicaIdToInstancesMap = new 
String[numReplicas][numInstancesPerReplica];
+      for (Map.Entry<Integer, List<Integer>> entry : 
poolToReplicaIdsMap.entrySet()) {
+        List<InstanceConfig> instanceConfigsInPool = 
poolToInstanceConfigsMap.get(entry.getKey());
+        List<Integer> replicaIdsInPool = entry.getValue();
+
+        // Use round-robin to assign instances to each replica so that they 
get instances with similar picking priority
+        int instanceIdInPool = 0;
+        for (int instanceIdInReplica = 0; instanceIdInReplica < 
numInstancesPerReplica; instanceIdInReplica++) {
+          for (int replicaId : replicaIdsInPool) {
+            replicaIdToInstancesMap[replicaId][instanceIdInReplica] =
+                
instanceConfigsInPool.get(instanceIdInPool++).getInstanceName();
+          }
+        }
+      }
+
+      // Assign instances within a replica to one partition if not configured
 
 Review comment:
   I am a bit confused on the term that we are using.
   
   `replica group - a set of instances that contains all segments for a 
partition or table`
   
   `replica - we used to use this term for indicating replication factor for a 
segment (e.g. 3 replicas)`
   
   Is this still correct in your comments? What do you mean by `instances 
within a replica`?

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to