npawar commented on a change in pull request #3622: Start and stop 
ControllerPeriodicTasks based on leadership changes
URL: https://github.com/apache/incubator-pinot/pull/3622#discussion_r243466001
 
 

 ##########
 File path: 
pinot-controller/src/main/java/com/linkedin/pinot/controller/helix/core/periodictask/ControllerPeriodicTask.java
 ##########
 @@ -55,67 +57,90 @@ private static long getRandomInitialDelayInSeconds() {
     return MIN_INITIAL_DELAY_IN_SECONDS + 
RANDOM.nextInt(MAX_INITIAL_DELAY_IN_SECONDS - MIN_INITIAL_DELAY_IN_SECONDS);
   }
 
+  /**
+   * Reset flags, and call initTask which initializes each individual task
+   */
   @Override
-  public void init() {
+  public final void init() {
+    _stopPeriodicTask = false;
+    _periodicTaskInProgress = false;
+    initTask();
   }
 
+  /**
+   * Execute the ControllerPeriodicTask.
+   * The _periodicTaskInProgress is enabled at the beginning and disabled 
before exiting,
+   * to ensure that we can wait for a task in progress to finish when stop has 
been invoked
+   */
   @Override
-  public void run() {
-    if (!isLeader()) {
-      skipLeaderTask();
-    } else {
-      List<String> allTableNames = _pinotHelixResourceManager.getAllTables();
-      processLeaderTask(allTableNames);
-    }
-  }
-
-  private void skipLeaderTask() {
-    if (_isLeader) {
-      LOGGER.info("Current pinot controller lost leadership.");
-      _isLeader = false;
-      onBecomeNotLeader();
-    }
-    LOGGER.info("Skip running periodic task: {} on non-leader controller", 
_taskName);
-  }
+  public final void run() {
+    _periodicTaskInProgress = true;
 
-  private void processLeaderTask(List<String> tables) {
-    if (!_isLeader) {
-      LOGGER.info("Current pinot controller became leader. Starting {} with 
running frequency of {} seconds.",
-          _taskName, _intervalInSeconds);
-      _isLeader = true;
-      onBecomeLeader();
-    }
+    List<String> tableNamesWithType = 
_pinotHelixResourceManager.getAllTables();
 
 Review comment:
   Loss of leadership, and regaining it, will invoke 
PeriodicTaskScheduler.start() which creates new executor service and calls the 
PeriodicTask.init() before scheduling. The init() method resets the 
stopPeriodicTask flag. So we should not need to set it to false again.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on 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