Jackie-Jiang commented on code in PR #11740:
URL: https://github.com/apache/pinot/pull/11740#discussion_r1351001671


##########
pinot-controller/src/main/java/org/apache/pinot/controller/helix/core/rebalance/TableRebalanceRetryConfig.java:
##########
@@ -0,0 +1,74 @@
+/**
+ * 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;
+
+/**
+ * Track the job configs and retry numbers as part of the job ZK metadata for 
future retry.
+ */
+public class TableRebalanceRetryConfig {

Review Comment:
   This is more like a context for rebalance, so suggest renaming it to 
`TableRebalanceContext`. Also, suggest moving `jobId` from `RebalanceConfig` 
into this class because that is also context



##########
pinot-controller/src/main/java/org/apache/pinot/controller/helix/core/rebalance/RebalanceConfig.java:
##########
@@ -84,6 +84,24 @@ public class RebalanceConfig {
   @ApiModelProperty(example = "false")
   private boolean _updateTargetTier = false;
 
+  // Update job status every this interval as heartbeat, to indicate the job 
is still actively running.
+  @JsonProperty("heartbeatIntervalInMs")
+  @ApiModelProperty(example = "300000")
+  private long _heartbeatIntervalInMs = 300000L;
+
+  // The job is considered as failed if not updating its status by this 
timeout, even though it's IN_PROGRESS status.
+  @JsonProperty("heartbeatTimeoutInMs")
+  @ApiModelProperty(example = "3600000")
+  private long _heartbeatTimeoutInMs = 3600000L;
+
+  @JsonProperty("maxRetry")
+  @ApiModelProperty(example = "3")
+  private int _maxRetry = 3;
+
+  @JsonProperty("retryInitialDelayInMs")
+  @ApiModelProperty(example = "300000")
+  private long _retryInitialDelayInMs = 300000L;

Review Comment:
   Not sure if we need this delay, or the exponential backoff. Rebalance task 
itself won't change ideal state before external view converges, thus no more 
messages will be sent before all messages for this table is processed.



##########
pinot-controller/src/main/java/org/apache/pinot/controller/helix/core/rebalance/TableRebalanceRetryConfig.java:
##########
@@ -0,0 +1,74 @@
+/**
+ * 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;
+
+/**
+ * Track the job configs and retry numbers as part of the job ZK metadata for 
future retry.
+ */
+public class TableRebalanceRetryConfig {
+  private String _originalJobId;
+  private int _retryNum;

Review Comment:
   For retry related config, we usually use `maxAttempts` and `attemptId`



##########
pinot-controller/src/main/java/org/apache/pinot/controller/helix/core/rebalance/RebalanceChecker.java:
##########
@@ -0,0 +1,279 @@
+/**
+ * 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;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Preconditions;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ExecutorService;
+import org.apache.commons.lang3.RandomUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.lang3.tuple.Pair;
+import org.apache.pinot.common.metadata.ZKMetadataProvider;
+import org.apache.pinot.common.metadata.controllerjob.ControllerJobType;
+import org.apache.pinot.common.metrics.ControllerMeter;
+import org.apache.pinot.common.metrics.ControllerMetrics;
+import org.apache.pinot.controller.ControllerConf;
+import org.apache.pinot.controller.LeadControllerManager;
+import org.apache.pinot.controller.helix.core.PinotHelixResourceManager;
+import 
org.apache.pinot.controller.helix.core.periodictask.ControllerPeriodicTask;
+import org.apache.pinot.spi.config.table.TableConfig;
+import org.apache.pinot.spi.utils.CommonConstants;
+import org.apache.pinot.spi.utils.JsonUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * Periodic task to check whether a user triggered rebalance job is completed 
or not, and retry if failed. The retry
+ * job is started with the same rebalance configs provided by the user and 
does best effort to stop the other jobs
+ * for the same table. This task can be configured to just check failures and 
report metrics, and not to do retry.
+ */
+public class RebalanceChecker extends ControllerPeriodicTask<Void> {
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(RebalanceChecker.class);
+  private static final double RETRY_DELAY_SCALE_FACTOR = 2.0;
+  private final boolean _checkOnly;
+  private final ExecutorService _executorService;
+
+  public RebalanceChecker(PinotHelixResourceManager pinotHelixResourceManager,
+      LeadControllerManager leadControllerManager, ControllerConf config, 
ControllerMetrics controllerMetrics,
+      ExecutorService executorService) {
+    super(RebalanceChecker.class.getSimpleName(), 
config.getRebalanceCheckerFrequencyInSeconds(),
+        config.getRebalanceCheckerInitialDelayInSeconds(), 
pinotHelixResourceManager, leadControllerManager,
+        controllerMetrics);
+    _checkOnly = config.isRebalanceCheckerCheckOnly();
+    _executorService = executorService;
+  }
+
+  @Override
+  protected void processTable(String tableNameWithType) {
+    _executorService.submit(() -> {

Review Comment:
   This will create one thread per table, and cause lots of ZK accesses. I 
wouldn't parallel them, but only schedule the needed retries into separate 
threads.



##########
pinot-controller/src/main/java/org/apache/pinot/controller/helix/core/rebalance/RebalanceConfig.java:
##########
@@ -84,6 +84,24 @@ public class RebalanceConfig {
   @ApiModelProperty(example = "false")
   private boolean _updateTargetTier = false;
 
+  // Update job status every this interval as heartbeat, to indicate the job 
is still actively running.
+  @JsonProperty("heartbeatIntervalInMs")
+  @ApiModelProperty(example = "300000")
+  private long _heartbeatIntervalInMs = 300000L;
+
+  // The job is considered as failed if not updating its status by this 
timeout, even though it's IN_PROGRESS status.
+  @JsonProperty("heartbeatTimeoutInMs")
+  @ApiModelProperty(example = "3600000")
+  private long _heartbeatTimeoutInMs = 3600000L;
+
+  @JsonProperty("maxRetry")
+  @ApiModelProperty(example = "3")
+  private int _maxRetry = 3;

Review Comment:
   Suggest changing it to `_maxAttempts`



##########
pinot-controller/src/main/java/org/apache/pinot/controller/helix/core/rebalance/RebalanceChecker.java:
##########
@@ -0,0 +1,279 @@
+/**
+ * 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;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Preconditions;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ExecutorService;
+import org.apache.commons.lang3.RandomUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.lang3.tuple.Pair;
+import org.apache.pinot.common.metadata.ZKMetadataProvider;
+import org.apache.pinot.common.metadata.controllerjob.ControllerJobType;
+import org.apache.pinot.common.metrics.ControllerMeter;
+import org.apache.pinot.common.metrics.ControllerMetrics;
+import org.apache.pinot.controller.ControllerConf;
+import org.apache.pinot.controller.LeadControllerManager;
+import org.apache.pinot.controller.helix.core.PinotHelixResourceManager;
+import 
org.apache.pinot.controller.helix.core.periodictask.ControllerPeriodicTask;
+import org.apache.pinot.spi.config.table.TableConfig;
+import org.apache.pinot.spi.utils.CommonConstants;
+import org.apache.pinot.spi.utils.JsonUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * Periodic task to check whether a user triggered rebalance job is completed 
or not, and retry if failed. The retry
+ * job is started with the same rebalance configs provided by the user and 
does best effort to stop the other jobs
+ * for the same table. This task can be configured to just check failures and 
report metrics, and not to do retry.
+ */
+public class RebalanceChecker extends ControllerPeriodicTask<Void> {
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(RebalanceChecker.class);
+  private static final double RETRY_DELAY_SCALE_FACTOR = 2.0;
+  private final boolean _checkOnly;
+  private final ExecutorService _executorService;
+
+  public RebalanceChecker(PinotHelixResourceManager pinotHelixResourceManager,
+      LeadControllerManager leadControllerManager, ControllerConf config, 
ControllerMetrics controllerMetrics,
+      ExecutorService executorService) {
+    super(RebalanceChecker.class.getSimpleName(), 
config.getRebalanceCheckerFrequencyInSeconds(),
+        config.getRebalanceCheckerInitialDelayInSeconds(), 
pinotHelixResourceManager, leadControllerManager,
+        controllerMetrics);
+    _checkOnly = config.isRebalanceCheckerCheckOnly();
+    _executorService = executorService;
+  }
+
+  @Override
+  protected void processTable(String tableNameWithType) {
+    _executorService.submit(() -> {
+      try {
+        LOGGER.info("Start to retry rebalance for table: {}", 
tableNameWithType);
+        TableConfig tableConfig = 
_pinotHelixResourceManager.getTableConfig(tableNameWithType);
+        Preconditions.checkState(tableConfig != null, "Failed to find table 
config for table: {}", tableNameWithType);
+        retryRebalanceTable(tableNameWithType, tableConfig);
+      } catch (Throwable t) {
+        LOGGER.error("Failed to retry rebalance for table: {}", 
tableNameWithType, t);
+      }
+    });
+  }
+
+  @VisibleForTesting
+  void retryRebalanceTable(String tableNameWithType, TableConfig tableConfig)
+      throws Exception {
+    // Get all rebalance jobs as tracked in ZK for this table to check if 
there is any failure to retry.
+    Map<String, Map<String, String>> allJobMetadata = 
_pinotHelixResourceManager.getAllJobsForTable(tableNameWithType,

Review Comment:
   We don't want to do this per table. Instead, we may read all rebalance job 
metadata once, and then process all of them together. You may directly override 
`processTables()` which process all tables together



##########
pinot-controller/src/main/java/org/apache/pinot/controller/ControllerConf.java:
##########
@@ -199,6 +199,9 @@ public static class ControllerPeriodicTasksConf {
     // RealtimeSegmentRelocator has been rebranded as SegmentRelocator
     public static final String 
DEPRECATED_REALTIME_SEGMENT_RELOCATION_INITIAL_DELAY_IN_SECONDS =
         "controller.realtimeSegmentRelocation.initialDelayInSeconds";
+    public static final String REBALANCE_CHECKER_INITIAL_DELAY_IN_SECONDS =
+        "controller.rebalanceChecker.initialDelayInSeconds";
+    public static final String REBALANCE_CHECKER_CHECK_ONLY = 
"controller.rebalanceChecker.checkOnly";

Review Comment:
   This config will void the retry from the rebalance call, so I definitely 
won't make it the default. IMO we can just remove it, and if by any change 
rebalance checker has a bug, we can turn it completely off



##########
pinot-controller/src/main/java/org/apache/pinot/controller/helix/core/rebalance/TableRebalancer.java:
##########
@@ -137,6 +139,23 @@ public static String createUniqueRebalanceJobIdentifier() {
   }
 
   public RebalanceResult rebalance(TableConfig tableConfig, RebalanceConfig 
rebalanceConfig) {
+    long startTime = System.currentTimeMillis();
+    String tableNameWithType = tableConfig.getTableName();
+    String status = "ABORTED";

Review Comment:
   What does `ABORTED` stand for? When would it happen?
   Currently seems it can occur only when exception is somehow not caught. 
Should we call it `ERRORED` and make it a valid `Status` enum?



##########
pinot-controller/src/main/java/org/apache/pinot/controller/helix/core/rebalance/ZkBasedTableRebalanceObserver.java:
##########
@@ -126,30 +143,73 @@ public int getNumUpdatesToZk() {
     return _numUpdatesToZk;
   }
 
+  @VisibleForTesting
+  TableRebalanceRetryConfig getTableRebalanceJobRetryConfig() {
+    return _tableRebalanceJobRetryConfig;
+  }
+
   private void trackStatsInZk() {
+    Map<String, String> jobMetadata =
+        createJobMetadata(_tableNameWithType, _rebalanceJobId, 
_tableRebalanceProgressStats,
+            _tableRebalanceJobRetryConfig);
+    _pinotHelixResourceManager.addControllerJobToZK(_rebalanceJobId, 
jobMetadata,
+        
ZKMetadataProvider.constructPropertyStorePathForControllerJob(ControllerJobType.TABLE_REBALANCE),
+        prevJobMetadata -> {
+          // Abort the job when we're sure it has failed, otherwise continue 
to update the status.
+          if (prevJobMetadata == null) {
+            return true;
+          }
+          String prevStatsInStr = 
prevJobMetadata.get(RebalanceJobConstants.JOB_METADATA_KEY_REBALANCE_PROGRESS_STATS);
+          TableRebalanceProgressStats prevStats;
+          try {
+            prevStats = JsonUtils.stringToObject(prevStatsInStr, 
TableRebalanceProgressStats.class);
+          } catch (JsonProcessingException ignore) {
+            return true;
+          }
+          boolean jobFailed =
+              prevStats != null && 
RebalanceResult.Status.FAILED.toString().equals(prevStats.getStatus());

Review Comment:
   Now I see how this abort is wired. I feel the abstraction is not correct 
though because basically we allow an observer to abort the execution by 
throwing an runtime exception. This is not safe because we cannot differentiate 
this abort vs other exceptions.
   One option to make it cleaner is to add an API `isAborted()` into the 
observer, and call it in the `TableRebalancer`



##########
pinot-controller/src/main/java/org/apache/pinot/controller/helix/core/rebalance/ZkBasedTableRebalanceObserver.java:
##########
@@ -98,10 +117,9 @@ private void updateOnStart(Map<String, Map<String, String>> 
currentState,
 
   @Override
   public void onSuccess(String msg) {
-    Preconditions.checkState(_tableRebalanceProgressStats.getStatus() != 
RebalanceResult.Status.DONE.toString(),
+    
Preconditions.checkState(!RebalanceResult.Status.DONE.toString().equals(_tableRebalanceProgressStats.getStatus()),

Review Comment:
   Good catch! IMO a better fix is to directly put `Status` instead of `String` 
in the `_tableRebalanceProgressStats`



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