Search in sources :

Example 11 with SingularityRequestWithState

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

the class RequestResource method unpause.

public SingularityRequestParent unpause(String requestId, Optional<SingularityUnpauseRequest> unpauseRequest, SingularityUser user) {
    SingularityRequestWithState requestWithState = fetchRequestWithState(requestId, user);
    authorizationHelper.checkForAuthorization(requestWithState.getRequest(), user, SingularityAuthorizationScope.WRITE);
    checkConflict(requestWithState.getState() == RequestState.PAUSED, "Request %s is not in PAUSED state, it is in %s", requestId, requestWithState.getState());
    Optional<String> message = Optional.absent();
    Optional<Boolean> skipHealthchecks = Optional.absent();
    if (unpauseRequest.isPresent()) {
        message = unpauseRequest.get().getMessage();
        skipHealthchecks = unpauseRequest.get().getSkipHealthchecks();
    }
    requestManager.deleteExpiringObject(SingularityExpiringPause.class, requestId);
    final long now = requestHelper.unpause(requestWithState.getRequest(), user.getEmail(), message, skipHealthchecks);
    return fillEntireRequest(new SingularityRequestWithState(requestWithState.getRequest(), RequestState.ACTIVE, now));
}
Also used : SingularityRequestWithState(com.hubspot.singularity.SingularityRequestWithState)

Example 12 with SingularityRequestWithState

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

the class TaskResource method deleteScheduledTask.

public Optional<SingularityPendingTask> deleteScheduledTask(String taskId, SingularityUser user) {
    Optional<SingularityPendingTask> maybePendingTask = taskManager.getPendingTask(getPendingTaskIdFromStr(taskId));
    if (maybePendingTask.isPresent()) {
        SingularityPendingTaskId pendingTaskId = maybePendingTask.get().getPendingTaskId();
        Optional<SingularityRequestWithState> maybeRequest = requestManager.getRequest(pendingTaskId.getRequestId());
        checkNotFound(maybeRequest.isPresent(), "Couldn't find: " + taskId);
        SingularityRequest request = maybeRequest.get().getRequest();
        authorizationHelper.checkForAuthorizationByRequestId(request.getId(), user, SingularityAuthorizationScope.WRITE);
        checkBadRequest(request.getRequestType() == RequestType.ON_DEMAND, "Only ON_DEMAND tasks may be deleted.");
        taskManager.markPendingTaskForDeletion(pendingTaskId);
    }
    return maybePendingTask;
}
Also used : SingularityPendingTask(com.hubspot.singularity.SingularityPendingTask) SingularityPendingTaskId(com.hubspot.singularity.SingularityPendingTaskId) SingularityRequestWithState(com.hubspot.singularity.SingularityRequestWithState) SingularityRequest(com.hubspot.singularity.SingularityRequest)

Example 13 with SingularityRequestWithState

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

the class SingularityAutoScaleSpreadAllPoller method runActionOnPoll.

@Override
public void runActionOnPoll() {
    int currentActiveSlaveCount = slaveManager.getNumObjectsAtState(MachineState.ACTIVE);
    for (SingularityRequestWithState requestWithState : requestManager.getActiveRequests()) {
        lock.runWithRequestLock(() -> {
            SingularityRequest request = requestWithState.getRequest();
            SlavePlacement placement = request.getSlavePlacement().or(defaultSlavePlacement);
            if (placement != SlavePlacement.SPREAD_ALL_SLAVES) {
                return;
            }
            int requestInstanceCount = request.getInstancesSafe();
            if (requestInstanceCount == currentActiveSlaveCount) {
                LOG.trace("Active Request {} is already spread to all {} available slaves", request.getId(), currentActiveSlaveCount);
            } else {
                LOG.info("Scaling request {} from {} instances to {} available slaves", request.getId(), requestInstanceCount, currentActiveSlaveCount);
                submitScaleRequest(requestWithState, currentActiveSlaveCount);
            }
        }, requestWithState.getRequest().getId(), getClass().getSimpleName());
    }
}
Also used : SingularityRequestWithState(com.hubspot.singularity.SingularityRequestWithState) SingularityRequest(com.hubspot.singularity.SingularityRequest) SlavePlacement(com.hubspot.singularity.SlavePlacement)

Example 14 with SingularityRequestWithState

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

the class SingularityStartup method checkSchedulerForInconsistentState.

/**
 * We need to run this check for the various situations where the scheduler could get in an inconsistent state due
 * to a crash/network failure during series of state transactions.
 *
 *  1) Unpausing
 *  2) Launching Task
 */
@VisibleForTesting
void checkSchedulerForInconsistentState() {
    final long now = System.currentTimeMillis();
    final Map<SingularityDeployKey, SingularityPendingTaskId> deployKeyToPendingTaskId = getDeployKeyToPendingTaskId();
    for (SingularityRequestWithState requestWithState : requestManager.getRequests()) {
        switch(requestWithState.getState()) {
            case ACTIVE:
            case SYSTEM_COOLDOWN:
            case DEPLOYING_TO_UNPAUSE:
                checkActiveRequest(requestWithState, deployKeyToPendingTaskId, now);
                break;
            case DELETED:
            case PAUSED:
            case FINISHED:
                break;
        }
    }
}
Also used : SingularityDeployKey(com.hubspot.singularity.SingularityDeployKey) SingularityPendingTaskId(com.hubspot.singularity.SingularityPendingTaskId) SingularityRequestWithState(com.hubspot.singularity.SingularityRequestWithState) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 15 with SingularityRequestWithState

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

the class SingularityScheduler method handlePendingRequestsForDeployKey.

private void handlePendingRequestsForDeployKey(AtomicInteger obsoleteRequests, AtomicInteger heldForScheduledActiveTask, AtomicInteger totalNewScheduledTasks, SingularityDeployKey deployKey, List<SingularityPendingRequest> pendingRequestsForDeploy) {
    final String requestId = deployKey.getRequestId();
    final Optional<SingularityRequestWithState> maybeRequest = requestManager.getRequest(requestId);
    final SingularityDeployStatistics deployStatistics = getDeployStatistics(deployKey.getRequestId(), deployKey.getDeployId());
    if (!isRequestActive(maybeRequest)) {
        LOG.debug("Pending request {} was obsolete (request {})", requestId, SingularityRequestWithState.getRequestState(maybeRequest));
        obsoleteRequests.getAndIncrement();
        for (SingularityPendingRequest pendingRequest : pendingRequestsForDeploy) {
            requestManager.deletePendingRequest(pendingRequest);
        }
        return;
    }
    SingularityRequestWithState request = maybeRequest.get();
    Optional<SingularityRequestDeployState> maybeRequestDeployState = deployManager.getRequestDeployState(requestId);
    Optional<SingularityPendingDeploy> maybePendingDeploy = deployManager.getPendingDeploy(requestId);
    List<SingularityTaskId> matchingTaskIds = getMatchingTaskIds(request.getRequest(), deployKey);
    List<SingularityPendingRequest> effectivePendingRequests = new ArrayList<>();
    // Things that are closest to now (ie smaller timestamps) should come first in the queue
    pendingRequestsForDeploy.sort(Comparator.comparingLong(SingularityPendingRequest::getTimestamp));
    int scheduledTasks = 0;
    for (SingularityPendingRequest pendingRequest : pendingRequestsForDeploy) {
        final SingularityRequest updatedRequest = updatedRequest(maybePendingDeploy, pendingRequest, request);
        if (!shouldScheduleTasks(updatedRequest, pendingRequest, maybePendingDeploy, maybeRequestDeployState)) {
            LOG.debug("Pending request {} was obsolete (request {})", pendingRequest, SingularityRequestWithState.getRequestState(maybeRequest));
            obsoleteRequests.getAndIncrement();
            requestManager.deletePendingRequest(pendingRequest);
            continue;
        }
        int missingInstances = getNumMissingInstances(matchingTaskIds, updatedRequest, pendingRequest, maybePendingDeploy);
        if (missingInstances == 0 && !matchingTaskIds.isEmpty() && updatedRequest.isScheduled() && pendingRequest.getPendingType() == PendingType.NEW_DEPLOY) {
            LOG.trace("Holding pending request {} because it is scheduled and has an active task", pendingRequest);
            heldForScheduledActiveTask.getAndIncrement();
            continue;
        }
        if (effectivePendingRequests.isEmpty()) {
            effectivePendingRequests.add(pendingRequest);
            RequestState requestState = checkCooldown(request.getState(), request.getRequest(), deployStatistics);
            scheduledTasks += scheduleTasks(request.getRequest(), requestState, deployStatistics, pendingRequest, matchingTaskIds, maybePendingDeploy);
            requestManager.deletePendingRequest(pendingRequest);
        } else if (pendingRequest.getPendingType() == PendingType.IMMEDIATE) {
            effectivePendingRequests.add(pendingRequest);
            RequestState requestState = checkCooldown(request.getState(), request.getRequest(), deployStatistics);
            scheduledTasks += scheduleTasks(request.getRequest(), requestState, deployStatistics, pendingRequest, matchingTaskIds, maybePendingDeploy);
            requestManager.deletePendingRequest(pendingRequest);
        } else if (pendingRequest.getPendingType() == PendingType.ONEOFF) {
            effectivePendingRequests.add(pendingRequest);
            RequestState requestState = checkCooldown(request.getState(), request.getRequest(), deployStatistics);
            scheduledTasks += scheduleTasks(request.getRequest(), requestState, deployStatistics, pendingRequest, matchingTaskIds, maybePendingDeploy);
            requestManager.deletePendingRequest(pendingRequest);
        } else if (updatedRequest.isScheduled() && (pendingRequest.getPendingType() == PendingType.NEW_DEPLOY || pendingRequest.getPendingType() == PendingType.TASK_DONE)) {
            // If we are here, there is already an immediate of run of the scheduled task launched. Drop anything that would
            // leave a second instance of the request in the pending queue.
            requestManager.deletePendingRequest(pendingRequest);
        }
    // Any other subsequent requests are not honored until after the pending queue is cleared.
    }
    totalNewScheduledTasks.getAndAdd(scheduledTasks);
}
Also used : SingularityPendingRequest(com.hubspot.singularity.SingularityPendingRequest) ArrayList(java.util.ArrayList) SingularityRequest(com.hubspot.singularity.SingularityRequest) SingularityRequestDeployState(com.hubspot.singularity.SingularityRequestDeployState) RequestState(com.hubspot.singularity.RequestState) SingularityPendingDeploy(com.hubspot.singularity.SingularityPendingDeploy) SingularityRequestWithState(com.hubspot.singularity.SingularityRequestWithState) SingularityDeployStatistics(com.hubspot.singularity.SingularityDeployStatistics) SingularityTaskId(com.hubspot.singularity.SingularityTaskId)

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