private SingularityDeployResult getDeployResult(final SingularityRequest request, final Optional<SingularityDeployMarker> cancelRequest, final SingularityPendingDeploy pendingDeploy, final SingularityDeployKey deployKey,
final Optional<SingularityDeploy> deploy, final Collection<SingularityTaskId> deployActiveTasks, final Collection<SingularityTaskId> otherActiveTasks, final Collection<SingularityTaskId> inactiveDeployMatchingTasks) {
if (!request.isDeployable()) {
LOG.info("Succeeding a deploy {} because the request {} was not deployable", pendingDeploy, request);
return new SingularityDeployResult(DeployState.SUCCEEDED, "Request not deployable");
}
if (!inactiveDeployMatchingTasks.isEmpty()) {
if (request.isLoadBalanced() && shouldCancelLoadBalancer(pendingDeploy)) {
LOG.info("Attempting to cancel pending load balancer request, failing deploy {} regardless", pendingDeploy);
sendCancelToLoadBalancer(pendingDeploy);
}
return new SingularityDeployResult(DeployState.FAILED, String.format("Task(s) %s for this deploy failed", inactiveDeployMatchingTasks));
}
if (shouldCheckLbState(pendingDeploy)) {
final SingularityLoadBalancerUpdate lbUpdate = lbClient.getState(getLoadBalancerRequestId(pendingDeploy.getDeployMarker()));
DeployState deployState = interpretLoadBalancerState(lbUpdate, pendingDeploy.getCurrentDeployState());
updatePendingDeploy(pendingDeploy, lbUpdate, deployState);
if (deployState != DeployState.WAITING) {
return fromLbState(deployState, lbUpdate);
}
}
final boolean isCancelRequestPresent = cancelRequest.isPresent();
final boolean isDeployOverdue = isDeployOverdue(pendingDeploy, deploy);
if (isCancelRequestPresent || isDeployOverdue) {
if (request.isLoadBalanced() && shouldCancelLoadBalancer(pendingDeploy)) {
return cancelLoadBalancer(pendingDeploy);
}
if (isCancelRequestPresent) {
LOG.info("Canceling a deploy {} due to cancel request {}", pendingDeploy, cancelRequest.get());
return new SingularityDeployResult(DeployState.CANCELED, String.format("Canceled due to request by %s at %s", cancelRequest.get().getUser(), cancelRequest.get().getTimestamp()));
}
}
if (pendingDeploy.getLastLoadBalancerUpdate().isPresent()) {
return new SingularityDeployResult(DeployState.WAITING, Optional.of("Waiting on load balancer API"), pendingDeploy.getLastLoadBalancerUpdate(), System.currentTimeMillis());
}
if ((deployActiveTasks.size() < request.getInstancesSafe()) || !deploy.isPresent()) {
String message = null;
if (deploy.isPresent()) {
message = String.format("Deploy was only able to launch %s out of a required %s tasks in %s: it is likely not enough resources or slaves are available and eligible", deployActiveTasks.size(), request.getInstancesSafe(), JavaUtils.durationFromMillis(getAllowedMillis(deploy.get())));
}
return checkOverdue(deploy, isDeployOverdue, message);
}
final DeployHealth deployHealth = deployHealthHelper.getDeployHealth(deploy, deployActiveTasks, true);
switch (deployHealth) {
case WAITING:
String message = null;
if (deploy.isPresent()) {
message = String.format("Deploy was able to launch %s tasks, but not all of them became healthy within %s", deployActiveTasks.size(), JavaUtils.durationFromMillis(getAllowedMillis(deploy.get())));
}
return checkOverdue(deploy, isDeployOverdue, message);
case HEALTHY:
if (request.isLoadBalanced()) {
// don't check overdue here because we want to give it a chance to enqueue the load
// balancer request. the next check will determine its fate.
return enqueueSwitchLoadBalancer(request, deploy.get(), pendingDeploy, deployActiveTasks, otherActiveTasks);
} else {
return new SingularityDeployResult(DeployState.SUCCEEDED);
}
case UNHEALTHY:
}
return new SingularityDeployResult(DeployState.FAILED, "At least one task for this deploy failed");
}