GrantPSpencer commented on code in PR #2932:
URL: https://github.com/apache/helix/pull/2932#discussion_r1851153555


##########
helix-core/src/main/java/org/apache/helix/controller/stages/ParticipantDeregistrationStage.java:
##########
@@ -0,0 +1,116 @@
+package org.apache.helix.controller.stages;
+
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import org.apache.helix.HelixException;
+import org.apache.helix.HelixManager;
+import 
org.apache.helix.controller.dataproviders.ResourceControllerDataProvider;
+import org.apache.helix.controller.pipeline.AbstractAsyncBaseStage;
+import org.apache.helix.controller.pipeline.AsyncWorkerType;
+import org.apache.helix.model.ClusterConfig;
+import org.apache.helix.model.InstanceConfig;
+import org.apache.helix.model.LiveInstance;
+import org.apache.helix.model.ParticipantHistory;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import static org.apache.helix.util.RebalanceUtil.scheduleOnDemandPipeline;
+
+
+public class ParticipantDeregistrationStage extends AbstractAsyncBaseStage {
+  private static final Logger LOG = 
LoggerFactory.getLogger(ParticipantDeregistrationStage.class);
+
+  @Override
+  public AsyncWorkerType getAsyncWorkerType() {
+    return AsyncWorkerType.ParticipantDeregistrationWorker;
+  }
+
+  @Override
+  public void execute(ClusterEvent event) throws Exception {
+    HelixManager manager = 
event.getAttribute(AttributeName.helixmanager.name());
+    ClusterConfig clusterConfig = 
manager.getConfigAccessor().getClusterConfig(manager.getClusterName());
+    if (clusterConfig == null || 
!clusterConfig.isParticipantDeregistrationEnabled()) {
+      LOG.info("Cluster config is null or participant deregistration is not 
enabled. "
+          + "Skipping participant deregistration.");
+      return;
+    }
+
+    ResourceControllerDataProvider cache = 
event.getAttribute(AttributeName.ControllerDataProvider.name());
+    Map<String, Long> offlineTimeMap = cache.getInstanceOfflineTimeMap();
+    long deregisterDelay = clusterConfig.getParticipantDeregistrationTimeout();
+    long stageStartTime = System.currentTimeMillis();
+    Set<String> participantsToDeregister = new HashSet<>();
+    long earliestDeregisterTime = Long.MAX_VALUE;
+
+
+    for (Map.Entry<String, Long> entry : offlineTimeMap.entrySet()) {
+      String instanceName = entry.getKey();
+      Long offlineTime = entry.getValue();
+      long deregisterTime = offlineTime + deregisterDelay;
+
+      // Skip if instance is still online
+      if (offlineTime == ParticipantHistory.ONLINE) {
+        continue;
+      }
+
+      // If deregister time is in the past, deregister the instance
+      if (deregisterTime <= stageStartTime) {
+        participantsToDeregister.add(instanceName);
+      } else {
+        // Otherwise, find the next earliest deregister time
+        if (deregisterTime < earliestDeregisterTime) {
+          earliestDeregisterTime = deregisterTime;
+        }

Review Comment:
   Adjusted this to remove the if and clarified variable name
   
   the if branch of (deregisterTime <= stageStartTime) should catch all nodes 
that need to be deregistered during this run
   
   the else will include all nodes that need to be deregistered at some point 
in the future. We then look for the node that is next in line to be 
deregistered and use that time to schedule the next deregistration
   
   now logic is:
   ```
         // If deregister time is in the past, deregister the instance
         if (deregisterTime <= stageStartTime) {
           participantsToDeregister.add(instanceName);
         } else {
           // Otherwise, find the next earliest deregister time
           nextDeregisterTime = Math.min(nextDeregisterTime, deregisterTime);
         }
   ```



-- 
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: reviews-unsubscr...@helix.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@helix.apache.org
For additional commands, e-mail: reviews-h...@helix.apache.org

Reply via email to