Skip to content

Instantly share code, notes, and snippets.

@loren
Created June 19, 2017 16:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loren/0fa450da3ebd23d05fd5460870d72aa2 to your computer and use it in GitHub Desktop.
Save loren/0fa450da3ebd23d05fd5460870d72aa2 to your computer and use it in GitHub Desktop.
package com.mesosphere.sdk.scheduler.plan.strategy;
import com.mesosphere.sdk.scheduler.plan.Interruptible;
import com.mesosphere.sdk.scheduler.plan.PodInstanceRequirement;
import com.mesosphere.sdk.scheduler.plan.Step;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
public class SafeStrategy implements Strategy<Step> {
private Collection<Step> safeSteps;
public SafeStrategy(List<Step> steps) {
this.safeSteps = buildSafeSteps(steps);
}
private List<Step> buildSafeSteps(List<Step> steps) {
return steps.stream()
.filter(this::isIncomplete)
.peek(this::interruptPendingStep)
.collect(Collectors.toList());
}
private boolean isIncomplete(Step step) {
return step.isPending() || step.isInterrupted();
}
private void interruptPendingStep(Step step) {
if (step.isPending()) {
step.interrupt();
}
}
@Override
public Collection<Step> getCandidates(Collection<Step> steps, Collection<PodInstanceRequirement> dirtyAssets) {
return safeSteps.stream()
.filter(step -> step.isEligible(dirtyAssets))
.collect(Collectors.toList());
}
@Override
public void interrupt() {
}
@Override
public void proceed() {
getNextInterruptedStep().ifPresent(Interruptible::proceed);
}
@Override
public boolean isInterrupted() {
// 1 or more Steps is interrupted and 1 or more Steps is ready to proceed
return (getNextInterruptedStep().isPresent() && hasNextProceedStep());
}
private boolean hasNextProceedStep() {
return safeSteps.stream().anyMatch(this::readyToProceed);
}
private Optional<Step> getNextInterruptedStep() {
return safeSteps.stream().filter(Interruptible::isInterrupted).findFirst();
}
private boolean readyToProceed(Step step) {
return !step.isInterrupted() && !step.isComplete();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment