Search in sources :

Example 16 with SingularityRequestWithState

use of com.hubspot.singularity.SingularityRequestWithState in project Singularity by HubSpot.

the class DeployResource method deploy.

public SingularityRequestParent deploy(SingularityDeployRequest deployRequest, SingularityUser user) {
    validator.checkActionEnabled(SingularityAction.DEPLOY);
    SingularityDeploy deploy = deployRequest.getDeploy();
    checkNotNullBadRequest(deploy, "DeployRequest must have a deploy object");
    final Optional<String> deployUser = user.getEmail();
    final String requestId = checkNotNullBadRequest(deploy.getRequestId(), "DeployRequest must have a non-null requestId");
    SingularityRequestWithState requestWithState = fetchRequestWithState(requestId, user);
    authorizationHelper.checkForAuthorization(requestWithState.getRequest(), user, SingularityAuthorizationScope.WRITE);
    SingularityRequest request = requestWithState.getRequest();
    final Optional<SingularityRequest> updatedValidatedRequest;
    if (deployRequest.getUpdatedRequest().isPresent()) {
        authorizationHelper.checkForAuthorizedChanges(deployRequest.getUpdatedRequest().get(), requestWithState.getRequest(), user);
        updatedValidatedRequest = Optional.of(validator.checkSingularityRequest(deployRequest.getUpdatedRequest().get(), Optional.of(requestWithState.getRequest()), Optional.<SingularityDeploy>absent(), Optional.of(deploy)));
    } else {
        updatedValidatedRequest = Optional.absent();
    }
    if (updatedValidatedRequest.isPresent()) {
        request = updatedValidatedRequest.get();
    }
    validator.checkScale(request, Optional.of(taskManager.getActiveTaskIdsForRequest(request.getId()).size()));
    if (!deployRequest.isUnpauseOnSuccessfulDeploy() && !configuration.isAllowDeployOfPausedRequests()) {
        checkConflict(requestWithState.getState() != RequestState.PAUSED, "Request %s is paused. Unable to deploy (it must be manually unpaused first)", requestWithState.getRequest().getId());
    }
    deploy = validator.checkDeploy(request, deploy, taskManager.getActiveTaskIdsForRequest(requestId), taskManager.getPendingTaskIdsForRequest(requestId));
    final long now = System.currentTimeMillis();
    SingularityDeployMarker deployMarker = new SingularityDeployMarker(requestId, deploy.getId(), now, deployUser, deployRequest.getMessage());
    Optional<SingularityDeployProgress> deployProgress = Optional.absent();
    if (request.isLongRunning()) {
        deployProgress = Optional.of(new SingularityDeployProgress(Math.min(deploy.getDeployInstanceCountPerStep().or(request.getInstancesSafe()), request.getInstancesSafe()), 0, deploy.getDeployInstanceCountPerStep().or(request.getInstancesSafe()), deploy.getDeployStepWaitTimeMs().or(configuration.getDefaultDeployStepWaitTimeMs()), false, deploy.getAutoAdvanceDeploySteps().or(true), Collections.emptySet(), System.currentTimeMillis()));
    }
    SingularityPendingDeploy pendingDeployObj = new SingularityPendingDeploy(deployMarker, Optional.<SingularityLoadBalancerUpdate>absent(), DeployState.WAITING, deployProgress, updatedValidatedRequest);
    boolean deployToUnpause = false;
    if (requestWithState.getState() == RequestState.PAUSED && deployRequest.isUnpauseOnSuccessfulDeploy()) {
        deployToUnpause = true;
        requestManager.deployToUnpause(request, now, deployUser, deployRequest.getMessage());
    }
    boolean deployAlreadyInProgress = deployManager.createPendingDeploy(pendingDeployObj) == SingularityCreateResult.EXISTED;
    if (deployAlreadyInProgress && deployToUnpause) {
        requestManager.pause(request, now, deployUser, Optional.absent());
    }
    checkConflict(!deployAlreadyInProgress, "Pending deploy already in progress for %s - cancel it or wait for it to complete (%s)", requestId, deployManager.getPendingDeploy(requestId).orNull());
    deployManager.saveDeploy(request, deployMarker, deploy);
    if (request.isDeployable() && !(requestWithState.getState() == RequestState.PAUSED && configuration.isAllowDeployOfPausedRequests())) {
        requestManager.addToPendingQueue(new SingularityPendingRequest(requestId, deployMarker.getDeployId(), now, deployUser, PendingType.NEW_DEPLOY, deployRequest.getDeploy().getSkipHealthchecksOnDeploy(), deployRequest.getMessage()));
    }
    return fillEntireRequest(requestWithState, Optional.of(request));
}
Also used : SingularityPendingRequest(com.hubspot.singularity.SingularityPendingRequest) SingularityDeployMarker(com.hubspot.singularity.SingularityDeployMarker) SingularityRequest(com.hubspot.singularity.SingularityRequest) SingularityDeployProgress(com.hubspot.singularity.SingularityDeployProgress) SingularityDeploy(com.hubspot.singularity.SingularityDeploy) SingularityPendingDeploy(com.hubspot.singularity.SingularityPendingDeploy) SingularityRequestWithState(com.hubspot.singularity.SingularityRequestWithState)

Example 17 with SingularityRequestWithState

use of com.hubspot.singularity.SingularityRequestWithState in project Singularity by HubSpot.

the class RequestResource method deleteExpiringObject.

private <T extends SingularityExpiringRequestActionParent<?>> SingularityRequestParent deleteExpiringObject(Class<T> clazz, String requestId, SingularityUser user) {
    SingularityRequestWithState requestWithState = fetchRequestWithState(requestId, user);
    SingularityDeleteResult deleteResult = requestManager.deleteExpiringObject(clazz, requestId);
    WebExceptions.checkNotFound(deleteResult == SingularityDeleteResult.DELETED, "%s didn't have an expiring %s request", clazz.getSimpleName(), requestId);
    return fillEntireRequest(requestWithState);
}
Also used : SingularityRequestWithState(com.hubspot.singularity.SingularityRequestWithState) SingularityDeleteResult(com.hubspot.singularity.SingularityDeleteResult)

Example 18 with SingularityRequestWithState

use of com.hubspot.singularity.SingularityRequestWithState in project Singularity by HubSpot.

the class SingularityJobPoller method runActionOnPoll.

@Override
public void runActionOnPoll() {
    final long now = System.currentTimeMillis();
    final List<SingularityTaskId> activeTaskIds = taskManager.getActiveTaskIds();
    final Set<String> requestIdsToLookup = Sets.newHashSetWithExpectedSize(activeTaskIds.size());
    for (SingularityTaskId taskId : activeTaskIds) {
        requestIdsToLookup.add(taskId.getRequestId());
    }
    final Map<String, SingularityRequestWithState> idToRequest = Maps.uniqueIndex(requestManager.getRequests(requestIdsToLookup), SingularityRequestWithState.REQUEST_STATE_TO_REQUEST_ID);
    for (SingularityTaskId taskId : activeTaskIds) {
        SingularityRequestWithState requestWithState = idToRequest.get(taskId.getRequestId());
        if (requestWithState == null) {
            LOG.warn("Active request not found for task ID {}", taskId);
            continue;
        }
        SingularityRequest request = requestWithState.getRequest();
        if (!request.isLongRunning()) {
            checkForOverdueScheduledJob(now - taskId.getStartedAt(), taskId, request);
            checkTaskExecutionTimeLimit(now, taskId, request);
        }
    }
}
Also used : SingularityRequestWithState(com.hubspot.singularity.SingularityRequestWithState) SingularityRequest(com.hubspot.singularity.SingularityRequest) SingularityTaskId(com.hubspot.singularity.SingularityTaskId)

Example 19 with SingularityRequestWithState

use of com.hubspot.singularity.SingularityRequestWithState in project Singularity by HubSpot.

the class SingularityPriorityKillPoller method runActionOnPoll.

@Override
public void runActionOnPoll() {
    if (!priorityManager.checkPriorityKillExists()) {
        LOG.trace("No priority freeze to process.");
        return;
    }
    final Optional<SingularityPriorityFreezeParent> maybePriorityFreeze = priorityManager.getActivePriorityFreeze();
    if (!maybePriorityFreeze.isPresent() || !maybePriorityFreeze.get().getPriorityFreeze().isKillTasks()) {
        LOG.trace("Priority freeze does not exist.");
        priorityManager.clearPriorityKill();
        return;
    }
    LOG.info("Handling priority freeze {}", maybePriorityFreeze.get());
    final long now = System.currentTimeMillis();
    int cancelledPendingTaskCount = 0;
    int killedTaskCount = 0;
    try {
        final double minPriorityLevel = maybePriorityFreeze.get().getPriorityFreeze().getMinimumPriorityLevel();
        // map request ID to priority level
        final Map<String, Double> requestIdToTaskPriority = new HashMap<>();
        for (SingularityRequestWithState requestWithState : requestManager.getRequests()) {
            requestIdToTaskPriority.put(requestWithState.getRequest().getId(), priorityManager.getTaskPriorityLevelForRequest(requestWithState.getRequest()));
        }
        // kill active tasks below minimum priority level
        for (SingularityTaskId taskId : taskManager.getActiveTaskIds()) {
            if (!requestIdToTaskPriority.containsKey(taskId.getRequestId())) {
                LOG.trace("Unable to lookup priority level for task {}, skipping...", taskId);
                continue;
            }
            final double taskPriorityLevel = requestIdToTaskPriority.get(taskId.getRequestId());
            if (taskPriorityLevel < minPriorityLevel) {
                LOG.info("Killing active task {} since priority level {} is less than {}", taskId.getId(), taskPriorityLevel, minPriorityLevel);
                taskManager.createTaskCleanup(new SingularityTaskCleanup(maybePriorityFreeze.get().getUser(), TaskCleanupType.PRIORITY_KILL, now, taskId, maybePriorityFreeze.get().getPriorityFreeze().getMessage(), maybePriorityFreeze.get().getPriorityFreeze().getActionId(), Optional.<SingularityTaskShellCommandRequestId>absent()));
                killedTaskCount++;
            }
        }
    } finally {
        priorityManager.clearPriorityKill();
        LOG.info("Finished killing active tasks for priority freeze {} in {} for {} active tasks, {} pending tasks", maybePriorityFreeze, JavaUtils.duration(now), killedTaskCount, cancelledPendingTaskCount);
    }
}
Also used : SingularityPriorityFreezeParent(com.hubspot.singularity.SingularityPriorityFreezeParent) HashMap(java.util.HashMap) SingularityRequestWithState(com.hubspot.singularity.SingularityRequestWithState) SingularityTaskCleanup(com.hubspot.singularity.SingularityTaskCleanup) SingularityTaskShellCommandRequestId(com.hubspot.singularity.SingularityTaskShellCommandRequestId) SingularityTaskId(com.hubspot.singularity.SingularityTaskId)

Example 20 with SingularityRequestWithState

use of com.hubspot.singularity.SingularityRequestWithState in project Singularity by HubSpot.

the class RequestResource method exitCooldown.

public SingularityRequestParent exitCooldown(String requestId, Optional<SingularityExitCooldownRequest> exitCooldownRequest, SingularityUser user) {
    final SingularityRequestWithState requestWithState = fetchRequestWithState(requestId, user);
    authorizationHelper.checkForAuthorization(requestWithState.getRequest(), user, SingularityAuthorizationScope.WRITE);
    checkConflict(requestWithState.getState() == RequestState.SYSTEM_COOLDOWN, "Request %s is not in SYSTEM_COOLDOWN state, it is in %s", requestId, requestWithState.getState());
    final Optional<String> maybeDeployId = deployManager.getInUseDeployId(requestId);
    final long now = System.currentTimeMillis();
    Optional<String> message = Optional.absent();
    Optional<Boolean> skipHealthchecks = Optional.absent();
    if (exitCooldownRequest.isPresent()) {
        message = exitCooldownRequest.get().getMessage();
        skipHealthchecks = exitCooldownRequest.get().getSkipHealthchecks();
    }
    requestManager.exitCooldown(requestWithState.getRequest(), now, Optional.of(user.getId()), message);
    if (maybeDeployId.isPresent() && !requestWithState.getRequest().isOneOff()) {
        requestManager.addToPendingQueue(new SingularityPendingRequest(requestId, maybeDeployId.get(), now, Optional.of(user.getId()), PendingType.IMMEDIATE, skipHealthchecks, message));
    }
    return fillEntireRequest(requestWithState);
}
Also used : SingularityPendingRequest(com.hubspot.singularity.SingularityPendingRequest) SingularityRequestWithState(com.hubspot.singularity.SingularityRequestWithState)

Aggregations

SingularityRequestWithState (com.hubspot.singularity.SingularityRequestWithState)37 SingularityRequest (com.hubspot.singularity.SingularityRequest)16 SingularityTaskId (com.hubspot.singularity.SingularityTaskId)11 SingularityPendingDeploy (com.hubspot.singularity.SingularityPendingDeploy)9 SingularityPendingRequest (com.hubspot.singularity.SingularityPendingRequest)7 SingularityRequestDeployState (com.hubspot.singularity.SingularityRequestDeployState)7 ArrayList (java.util.ArrayList)7 SingularityDeploy (com.hubspot.singularity.SingularityDeploy)6 SingularityCreateResult (com.hubspot.singularity.SingularityCreateResult)5 SingularityDeployKey (com.hubspot.singularity.SingularityDeployKey)5 SingularityPendingTaskId (com.hubspot.singularity.SingularityPendingTaskId)5 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)5 Path (javax.ws.rs.Path)5 RequestState (com.hubspot.singularity.RequestState)4 SingularityDeployMarker (com.hubspot.singularity.SingularityDeployMarker)4 SingularityRequestCleanup (com.hubspot.singularity.SingularityRequestCleanup)4 SingularityRequestHistory (com.hubspot.singularity.SingularityRequestHistory)4 SingularityBounceRequest (com.hubspot.singularity.api.SingularityBounceRequest)4 ApiResponses (com.wordnik.swagger.annotations.ApiResponses)4 List (java.util.List)4