-
Notifications
You must be signed in to change notification settings - Fork 228
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add auto deregistration of offline participants after timeout #2932
Open
GrantPSpencer
wants to merge
2
commits into
apache:master
Choose a base branch
from
GrantPSpencer:participant-auto-deregistration
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
...core/src/main/java/org/apache/helix/controller/stages/ParticipantDeregistrationStage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
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 nextDeregisterTime = 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 (cache.getLiveInstances().containsKey(instanceName)) { | ||
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 | ||
nextDeregisterTime = Math.min(nextDeregisterTime, 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 (nextDeregisterTime != Long.MAX_VALUE) { | ||
long delay = Math.max(nextDeregisterTime - System.currentTimeMillis(), 0); | ||
scheduleOnDemandPipeline(manager.getClusterName(), delay); | ||
} | ||
} | ||
|
||
private Set<String> deregisterParticipants(HelixManager manager, ResourceControllerDataProvider cache, | ||
Set<String> instancesToDeregister) { | ||
Set<String> successfullyDeregisteredInstances = new HashSet<>(); | ||
|
||
if (manager == null || !manager.isConnected() || cache == null || instancesToDeregister == null) { | ||
LOG.info("ParticipantDeregistrationStage failed due to HelixManager being null or not connected!"); | ||
return successfullyDeregisteredInstances; | ||
} | ||
|
||
// Perform safety checks before deregistering the instances | ||
for (String instanceName : instancesToDeregister) { | ||
InstanceConfig instanceConfig = cache.getInstanceConfigMap().get(instanceName); | ||
LiveInstance liveInstance = cache.getLiveInstances().get(instanceName); | ||
|
||
if (instanceConfig == null) { | ||
LOG.debug("Instance config is null for instance {}, skip deregistering the instance", instanceName); | ||
continue; | ||
} | ||
|
||
if (liveInstance != null) { | ||
LOG.debug("Instance {} is still alive, skip deregistering the instance", instanceName); | ||
continue; | ||
} | ||
|
||
try { | ||
manager.getClusterManagmentTool().dropInstance(cache.getClusterName(), instanceConfig); | ||
successfullyDeregisteredInstances.add(instanceName); | ||
} catch (HelixException e) { | ||
LOG.warn("Failed to deregister instance {} from cluster {}", instanceName, cache.getClusterName(), e); | ||
} | ||
} | ||
|
||
return successfullyDeregisteredInstances; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should rely on LiveInstances instead of history.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adjusted this check to be: