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

 ##########
 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:
   Need to set _stopPeriodicTask to be false? Otherwise, a controller that 
gains leadership, and then loses it, and then gains again, will end up 
processing only the first table, right?
   
   Instead of two booleans, does it make sense to use a task state enum? 
(INITED, RUNNING, STOPPING, STOPPED).  Maybe even add the task state in the 
base class
   
   Maybe useful to add a comment that we don't need to synchronize here since 
the synchronization between run and stop is in the base class?

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