shounakmk219 commented on code in PR #11128:
URL: https://github.com/apache/pinot/pull/11128#discussion_r1274574539


##########
pinot-controller/src/main/java/org/apache/pinot/controller/helix/core/rebalance/tenant/DefaultTenantRebalancer.java:
##########
@@ -0,0 +1,193 @@
+/**
+ * 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.rebalance.tenant;
+
+import com.google.common.collect.Sets;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.NoSuchElementException;
+import java.util.Set;
+import java.util.UUID;
+import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.concurrent.ExecutorService;
+import org.apache.commons.configuration.BaseConfiguration;
+import org.apache.commons.configuration.Configuration;
+import org.apache.pinot.common.exception.TableNotFoundException;
+import org.apache.pinot.controller.helix.core.PinotHelixResourceManager;
+import org.apache.pinot.controller.helix.core.rebalance.RebalanceResult;
+import org.apache.pinot.spi.config.table.TableConfig;
+import org.apache.pinot.spi.utils.RebalanceConfigConstants;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class DefaultTenantRebalancer implements TenantRebalancer {
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(DefaultTenantRebalancer.class);
+  PinotHelixResourceManager _pinotHelixResourceManager;
+  ExecutorService _executorService;
+
+  public DefaultTenantRebalancer(PinotHelixResourceManager 
pinotHelixResourceManager, ExecutorService executorService) {
+    _pinotHelixResourceManager = pinotHelixResourceManager;
+    _executorService = executorService;
+  }
+
+  @Override
+  public TenantRebalanceResult rebalance(TenantRebalanceContext context) {
+    Map<String, RebalanceResult> rebalanceResult = new HashMap<>();
+    Set<String> tables = getTenantTables(context.getTenantName());
+    tables.forEach(table -> {
+      try {
+        Configuration config = extractRebalanceConfig(context);
+        config.setProperty(RebalanceConfigConstants.DRY_RUN, true);
+        rebalanceResult.put(table, 
_pinotHelixResourceManager.rebalanceTable(table, config, false));
+      } catch (TableNotFoundException exception) {
+        rebalanceResult.put(table, new RebalanceResult(null, 
RebalanceResult.Status.FAILED, exception.getMessage(),
+            null, null, null));
+      }
+    });
+    if (context.isDryRun() || context.isDowntime()) {
+      return new TenantRebalanceResult(null, rebalanceResult, 
context.isVerboseResult());
+    } else {
+      for (String table : rebalanceResult.keySet()) {
+        RebalanceResult result = rebalanceResult.get(table);
+        if (result.getStatus() == RebalanceResult.Status.DONE) {
+          rebalanceResult.put(table, new RebalanceResult(result.getJobId(), 
RebalanceResult.Status.IN_PROGRESS,
+              "In progress, check controller task status for the", 
result.getInstanceAssignment(),
+              result.getTierInstanceAssignment(), 
result.getSegmentAssignment()));
+        }
+      }
+    }
+
+    String tenantRebalanceJobId = createUniqueRebalanceJobIdentifier();
+    TenantRebalanceObserver observer = new 
ZkBasedTenantRebalanceObserver(tenantRebalanceJobId, context.getTenantName(),
+        tables, _pinotHelixResourceManager);
+    observer.onTrigger(TenantRebalanceObserver.Trigger.START_TRIGGER, null, 
null);
+    Set<String> parallelTables;
+    Set<String> sequentialTables;
+    final ConcurrentLinkedQueue<String> parallelQueue;
+    try {
+      if (context.getDegreeOfParallelism() > 1) {
+        if (!context.getParallelWhitelist().isEmpty()) {
+          parallelTables = new HashSet<>(context.getParallelWhitelist());
+        } else {
+          parallelTables = new HashSet<>(tables);
+        }
+        if (!context.getParallelBlacklist().isEmpty()) {
+          parallelTables = Sets.difference(parallelTables, 
context.getParallelBlacklist());
+        }
+        parallelQueue = new ConcurrentLinkedQueue<>(parallelTables);
+        for (int i = 0; i < context.getDegreeOfParallelism(); i++) {
+          _executorService.submit(() -> {
+            try {
+              while (true) {
+                String table = parallelQueue.remove();
+                Configuration config = extractRebalanceConfig(context);
+                config.setProperty(RebalanceConfigConstants.DRY_RUN, false);
+                config.setProperty(RebalanceConfigConstants.JOB_ID, 
rebalanceResult.get(table).getJobId());
+                rebalanceTable(table, config, observer);
+              }
+            } catch (NoSuchElementException ignore) {
+            }
+          });
+        }
+        sequentialTables = Sets.difference(tables, parallelTables);
+      } else {
+        sequentialTables = new HashSet<>(tables);
+        parallelQueue = new ConcurrentLinkedQueue<>();
+      }
+      _executorService.submit(() -> {
+        while (!parallelQueue.isEmpty()) {
+          try {
+            Thread.sleep(5000);

Review Comment:
   Hey @saurabhd336 thanks for pointing this out, I have made changes to avoid 
the thread hogging. Now the last active thread from the parallel threads will 
pick up the job to rebalance the sequential tables.



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