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


##########
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;
+        }
+      }
+    }
+
+    if (!participantsToDeregister.isEmpty()) {
+      Set<String> successfullyDeregisteredParticipants =
+        deregisterParticipants(manager, cache, participantsToDeregister);
+      if (!successfullyDeregisteredParticipants.isEmpty()) {
+        LOG.info("Successfully deregistered {} participants from cluster {}",
+              successfullyDeregisteredParticipants.size(), 
cache.getClusterName());
+      }
+    }
+    // Schedule the next deregister task
+    if (earliestDeregisterTime != Long.MAX_VALUE) {
+      long delay = earliestDeregisterTime - stageStartTime;
+      scheduleOnDemandPipeline(manager.getClusterName(), delay);
+    }

Review Comment:
   Good catch, thank you for calling this out. 
   Updated calculation of delay
   ```
   long delay = nextDeregisterTime - System.currentTimeMillis();
   ```
   
   Do you think there is an argument for the check of whether a participant 
should be deregistered 
   ```
   if (deregisterTime <= stageStartTime) {
   ``` 
   should also utilize currentTime rather than stageStartTime



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to