snleee commented on code in PR #9309:
URL: https://github.com/apache/pinot/pull/9309#discussion_r966612919


##########
pinot-controller/src/main/java/org/apache/pinot/controller/helix/core/assignment/segment/BaseSegmentAssignment.java:
##########
@@ -85,70 +89,41 @@ public void init(HelixManager helixManager, TableConfig 
tableConfig) {
       _logger.info("Initialized with replication: {} and partition column: {} 
for table: {}", _replication,
           _partitionColumn, _tableNameWithType);
     }
+    setSegmentAssignmentStrategyMap(instancePartitionsMap);
   }
 
   /**
-   * Returns the replication of the table.
+   * Sets Segment assignment strategy for different instance partitions and 
puts into a map of
+   * Map<InstancePartitionsType, SegmentAssignmentStrategy>
    */
-  protected abstract int getReplication(TableConfig tableConfig);
+  protected void setSegmentAssignmentStrategyMap(
+      Map<InstancePartitionsType, InstancePartitions> instancePartitionsMap) {
+      _assignmentStrategyMap = 
SegmentAssignmentStrategyFactory.getSegmentAssignmentStrategy(_helixManager,
+          _tableConfig, instancePartitionsMap);
+  }
 
   /**
-   * Helper method to check whether the number of replica-groups matches the 
table replication for replica-group based
-   * instance partitions. Log a warning if they do not match and use the one 
inside the instance partitions. The
-   * mismatch can happen when table is not configured correctly (table 
replication and numReplicaGroups does not match
-   * or replication changed without reassigning instances).
+   * Returns the replication of the table.
    */
-  protected void checkReplication(InstancePartitions instancePartitions) {
-    int numReplicaGroups = instancePartitions.getNumReplicaGroups();
-    if (numReplicaGroups != _replication) {
-      _logger.warn(
-          "Number of replica-groups in instance partitions {}: {} does not 
match replication in table config: {} for "
-              + "table: {}, using: {}", 
instancePartitions.getInstancePartitionsName(), numReplicaGroups, _replication,
-          _tableNameWithType, numReplicaGroups);
-    }
-  }
+  protected abstract int getReplication(TableConfig tableConfig);
 
   /**
    * Helper method to assign instances based on the current assignment and 
instance partitions.
    */
   protected List<String> assignSegment(String segmentName, Map<String, 
Map<String, String>> currentAssignment,

Review Comment:
   I think that we can remove this helper method and directly call the 
following line in `OfflineSegmentAssignment/RealtimeSegmentAssignment`
   ```
   segmentAssignmentStrategy.assignSegment(segmentName,
           currentAssignment, instancePartitions, instancePartitionsType);
   ```



##########
pinot-controller/src/main/java/org/apache/pinot/controller/helix/core/assignment/segment/SegmentAssignment.java:
##########
@@ -31,20 +31,17 @@
 
 /**
  * Interface for segment assignment and table rebalance.
- * <p>
- * TODO: Add SegmentAssignmentStrategy interface and support custom segment 
assignment strategy (e.g. cost based segment
- *       assignment). SegmentAssignmentStrategy should not be coupled with 
SegmentAssignment, and SegmentAssignment
- *       should be able to choose the segment assignment strategy based on the 
configuration.
  */
 public interface SegmentAssignment {
 
   /**
    * Initializes the segment assignment.
-   *
    * @param helixManager Helix manager
    * @param tableConfig Table config
+   * @param instancePartitionsMap Map from type (OFFLINE|CONSUMING|COMPLETED) 
to instance partitions
    */
-  void init(HelixManager helixManager, TableConfig tableConfig);
+  void init(HelixManager helixManager, TableConfig tableConfig,
+      Map<InstancePartitionsType, InstancePartitions> instancePartitionsMap);

Review Comment:
   I don't think that we need to add `instancePartitionsMap` here if we 
initialize the segment assignment once in the beginning of
   
   1. assignSegment()
   
   2. rebalanceTable()
   
   and pass the object around.



##########
pinot-controller/src/main/java/org/apache/pinot/controller/helix/core/assignment/segment/strategy/SegmentAssignmentStrategyFactory.java:
##########
@@ -0,0 +1,114 @@
+/**
+ * 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.strategy;
+
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.helix.HelixManager;
+import org.apache.pinot.common.assignment.InstancePartitions;
+import org.apache.pinot.spi.config.table.TableConfig;
+import org.apache.pinot.spi.config.table.TableType;
+import org.apache.pinot.spi.config.table.assignment.InstancePartitionsType;
+import org.apache.pinot.spi.config.table.assignment.SegmentAssignmentConfig;
+import org.apache.pinot.spi.utils.CommonConstants.Segment.AssignmentStrategy;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * Factory for SegmentAssignmentStrategy
+ */
+public class SegmentAssignmentStrategyFactory {
+
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(SegmentAssignmentStrategyFactory.class);
+  private SegmentAssignmentStrategyFactory() {
+  }
+
+  /**
+   * Determine Segment Assignment strategy
+   */
+  public static Map<InstancePartitionsType, SegmentAssignmentStrategy>
+  getSegmentAssignmentStrategy(HelixManager helixManager,

Review Comment:
   I feel that this logic becomes too complex if we also need to handle all of 
`OFFLINE/CONSUMING/COMPLETED`. I think that we should consider keeping this 
factory to be very thin (given an `AssignmentStrategy` enum, return the correct 
object).
   
   Instead, we should think about putting the logic for picking the correct 
assignment in each `OfflineSegmentAssignment / RealtimeSegmentAssignment`.
   
   This will make this logic less complex because we just need to handle 
`OFFLINE` from `OfflineSegmentAssignment` and `CONSUMING/COMPLETED` from 
`RealtimeSegmentAssignment`.
   
   
   
   



##########
pinot-controller/src/main/java/org/apache/pinot/controller/helix/core/assignment/segment/RealtimeSegmentAssignment.java:
##########
@@ -183,8 +176,10 @@ public Map<String, Map<String, String>> 
rebalanceTable(Map<String, Map<String, S
         config.getBoolean(RebalanceConfigConstants.BOOTSTRAP, 
RebalanceConfigConstants.DEFAULT_BOOTSTRAP);
 
     // Rebalance tiers first
+    SegmentAssignmentStrategy segmentAssignmentStrategy = 
_assignmentStrategyMap.get(InstancePartitionsType.COMPLETED);

Review Comment:
   Why don't we initialize the segmentAssignmentStrategy directly from 
`SegmentAssignmentStrategyFactory` here?
   
   In that case, we won't need to change the interface for 
`SegmentAssignment:init()` to take the `instancePartitionsMap`.



##########
pinot-controller/src/main/java/org/apache/pinot/controller/helix/core/assignment/segment/BaseSegmentAssignment.java:
##########
@@ -194,60 +171,16 @@ protected Map<String, Map<String, String>> 
reassignSegments(String instanceParti
       // When bootstrap is enabled, start with an empty assignment and 
reassign all segments
       newAssignment = new TreeMap<>();
       for (String segment : currentAssignment.keySet()) {
-        List<String> assignedInstances = assignSegment(segment, newAssignment, 
instancePartitions);
+        List<String> assignedInstances = assignSegment(segment, newAssignment, 
instancePartitions,

Review Comment:
   This can be simply
   ```
   List<String> assignedInstances = assignmentStrategy.assignSegment(...)
   ```



##########
pinot-controller/src/main/java/org/apache/pinot/controller/helix/core/assignment/segment/BaseSegmentAssignment.java:
##########
@@ -68,11 +68,15 @@ public abstract class BaseSegmentAssignment implements 
SegmentAssignment {
   protected String _tableNameWithType;
   protected int _replication;
   protected String _partitionColumn;
+  protected TableConfig _tableConfig;
+  protected Map<InstancePartitionsType, SegmentAssignmentStrategy> 
_assignmentStrategyMap;
 
   @Override
-  public void init(HelixManager helixManager, TableConfig tableConfig) {
+  public void init(HelixManager helixManager, TableConfig tableConfig,
+      Map<InstancePartitionsType, InstancePartitions> instancePartitionsMap) {
     _helixManager = helixManager;
     _tableNameWithType = tableConfig.getTableName();
+    _tableConfig = tableConfig;

Review Comment:
   Do we access this from anywhere? For `setSegmentAssignmentStrategyMap()`, we 
can push the local `tableConfig`?



-- 
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