Search in sources :

Example 51 with SingularityTaskId

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

the class TaskTrackerResource method getTaskStateByRunId.

@GET
@Path("/run/{requestId}/{runId}")
@ApiOperation(value = "Get the current state of a task by taskId whether it is pending, active, or inactive")
@ApiResponses({ @ApiResponse(code = 404, message = "Task with this runId does not exist") })
public Optional<SingularityTaskState> getTaskStateByRunId(@Auth SingularityUser user, @PathParam("requestId") String requestId, @PathParam("runId") String runId) {
    authorizationHelper.checkForAuthorizationByRequestId(requestId, user, SingularityAuthorizationScope.READ);
    // Check if it's active or inactive
    Optional<SingularityTaskId> maybeTaskId = taskManager.getTaskByRunId(requestId, runId);
    if (maybeTaskId.isPresent()) {
        Optional<SingularityTaskState> maybeTaskState = getTaskStateFromId(maybeTaskId.get());
        if (maybeTaskState.isPresent()) {
            return maybeTaskState;
        }
    } else {
        Optional<SingularityTaskHistory> maybeTaskHistory = historyManager.getTaskHistoryByRunId(requestId, runId);
        if (maybeTaskHistory.isPresent()) {
            return Optional.of(SingularityTaskState.fromTaskHistory(maybeTaskHistory.get()));
        }
    }
    // Check if it's pending
    for (SingularityPendingTask pendingTask : taskManager.getPendingTasksForRequest(requestId)) {
        if (pendingTask.getRunId().isPresent() && pendingTask.getRunId().get().equals(runId)) {
            return Optional.of(new SingularityTaskState(Optional.absent(), pendingTask.getPendingTaskId(), pendingTask.getRunId(), Optional.absent(), Collections.emptyList(), true));
        }
    }
    for (SingularityPendingRequest pendingRequest : requestManager.getPendingRequests()) {
        if (pendingRequest.getRequestId().equals(requestId) && pendingRequest.getRunId().isPresent() && pendingRequest.getRunId().get().equals(runId)) {
            return Optional.of(new SingularityTaskState(Optional.absent(), Optional.absent(), pendingRequest.getRunId(), Optional.absent(), Collections.emptyList(), true));
        }
    }
    return Optional.absent();
}
Also used : SingularityTaskState(com.hubspot.singularity.SingularityTaskState) SingularityPendingRequest(com.hubspot.singularity.SingularityPendingRequest) SingularityPendingTask(com.hubspot.singularity.SingularityPendingTask) SingularityTaskHistory(com.hubspot.singularity.SingularityTaskHistory) SingularityTaskId(com.hubspot.singularity.SingularityTaskId) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) ApiResponses(com.wordnik.swagger.annotations.ApiResponses)

Example 52 with SingularityTaskId

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

the class SingularityCleaner method drainLBTaskCleanupQueue.

private void drainLBTaskCleanupQueue(List<SingularityTaskId> lbCleanupTasks) {
    final long start = System.currentTimeMillis();
    if (lbCleanupTasks.isEmpty()) {
        LOG.trace("LB task cleanup queue is empty");
        return;
    }
    LOG.info("LB task cleanup queue had {} tasks", lbCleanupTasks.size());
    AtomicInteger cleanedTasks = new AtomicInteger(0);
    AtomicInteger ignoredTasks = new AtomicInteger(0);
    lbCleanupTasks.stream().collect(Collectors.groupingBy(SingularityTaskId::getRequestId)).entrySet().parallelStream().forEach((lbCleanupsForRequest) -> {
        lock.runWithRequestLock(() -> {
            for (SingularityTaskId taskId : lbCleanupsForRequest.getValue()) {
                final long checkStart = System.currentTimeMillis();
                final CheckLBState checkLbState = checkLbState(taskId);
                LOG.debug("LB cleanup for task {} had state {} after {}", taskId, checkLbState, JavaUtils.duration(checkStart));
                switch(checkLbState) {
                    case WAITING:
                    case RETRY:
                        continue;
                    case DONE:
                    case MISSING_TASK:
                        cleanedTasks.getAndIncrement();
                        break;
                    case NOT_LOAD_BALANCED:
                    case LOAD_BALANCE_FAILED:
                        ignoredTasks.getAndIncrement();
                }
                taskManager.deleteLBCleanupTask(taskId);
            }
        }, lbCleanupsForRequest.getKey(), String.format("%s#%s", getClass().getSimpleName(), "drainLBTaskCleanupQueue"));
    });
    LOG.info("LB cleaned {} tasks ({} left, {} obsolete) in {}", cleanedTasks, lbCleanupTasks.size() - (ignoredTasks.get() + cleanedTasks.get()), ignoredTasks, JavaUtils.duration(start));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SingularityTaskId(com.hubspot.singularity.SingularityTaskId)

Example 53 with SingularityTaskId

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

the class SingularityCleaner method getNumHealthyTasks.

private int getNumHealthyTasks(SingularityRequest request, String deployId, List<SingularityTaskId> matchingTasks) {
    Optional<SingularityDeploy> deploy = deployManager.getDeploy(request.getId(), deployId);
    List<SingularityTaskId> healthyTasks = deployHealthHelper.getHealthyTasks(request, deploy, matchingTasks, false);
    int numHealthyTasks = 0;
    for (SingularityTaskId taskId : healthyTasks) {
        DeployHealth lbHealth = getLbHealth(request, taskId);
        if (lbHealth == DeployHealth.HEALTHY) {
            numHealthyTasks++;
        }
    }
    return numHealthyTasks;
}
Also used : DeployHealth(com.hubspot.singularity.scheduler.SingularityDeployHealthHelper.DeployHealth) SingularityDeploy(com.hubspot.singularity.SingularityDeploy) SingularityTaskId(com.hubspot.singularity.SingularityTaskId)

Example 54 with SingularityTaskId

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

the class SingularityCleaner method processTaskCleanupsForRequest.

private void processTaskCleanupsForRequest(String requestId, List<SingularityTaskCleanup> cleanupTasks, AtomicInteger killedTasks) {
    final Multiset<SingularityDeployKey> incrementalCleaningTasks = HashMultiset.create(cleanupTasks.size());
    final List<String> taskIdsForDeletedRequest = new ArrayList<>();
    boolean isRequestDeleting = false;
    // TODO - Better check for deleting request state
    final Set<SingularityTaskId> cleaningTasks = new HashSet<>(cleanupTasks.size());
    for (SingularityTaskCleanup cleanupTask : cleanupTasks) {
        cleaningTasks.add(cleanupTask.getTaskId());
        if (isIncrementalDeployCleanup(cleanupTask) || cleanupTask.getCleanupType() == TaskCleanupType.INCREMENTAL_BOUNCE) {
            incrementalCleaningTasks.add(SingularityDeployKey.fromTaskId(cleanupTask.getTaskId()));
        }
        if (cleanupTask.getCleanupType() == TaskCleanupType.REQUEST_DELETING) {
            taskIdsForDeletedRequest.add(cleanupTask.getTaskId().getId());
            isRequestDeleting = true;
        }
    }
    LOG.info("Cleaning up {} tasks for request {}", cleanupTasks.size(), requestId);
    final List<SingularityTaskId> activeTaskIds = taskManager.getActiveTaskIds();
    for (SingularityTaskCleanup cleanupTask : cleanupTasks) {
        SingularityTaskId taskId = cleanupTask.getTaskId();
        if (!isValidTask(cleanupTask)) {
            LOG.info("Couldn't find a matching active task for cleanup task {}, deleting..", cleanupTask);
            taskManager.deleteCleanupTask(taskId.getId());
        } else if (shouldKillTask(cleanupTask, activeTaskIds, cleaningTasks, incrementalCleaningTasks) && checkLBStateAndShouldKillTask(cleanupTask)) {
            scheduler.killAndRecord(taskId, cleanupTask.getCleanupType(), cleanupTask.getUser());
            taskManager.deleteCleanupTask(taskId.getId());
            killedTasks.getAndIncrement();
        }
        cleanupRequestIfNoRemainingTasks(cleanupTask, taskIdsForDeletedRequest, isRequestDeleting);
    }
}
Also used : SingularityDeployKey(com.hubspot.singularity.SingularityDeployKey) ArrayList(java.util.ArrayList) SingularityTaskCleanup(com.hubspot.singularity.SingularityTaskCleanup) SingularityTaskId(com.hubspot.singularity.SingularityTaskId) HashSet(java.util.HashSet)

Example 55 with SingularityTaskId

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

the class SingularityCleaner method bounce.

private void bounce(SingularityRequestCleanup requestCleanup, final List<SingularityTaskId> activeTaskIds) {
    final long start = System.currentTimeMillis();
    final List<SingularityTaskId> matchingTaskIds = new ArrayList<>();
    for (SingularityTaskId activeTaskId : activeTaskIds) {
        if (activeTaskId.getRequestId().equals(requestCleanup.getRequestId()) && activeTaskId.getDeployId().equals(requestCleanup.getDeployId().get())) {
            matchingTaskIds.add(activeTaskId);
        }
    }
    for (SingularityTaskId matchingTaskId : matchingTaskIds) {
        LOG.debug("Adding task {} to cleanup (bounce)", matchingTaskId.getId());
        Optional<SingularityTaskShellCommandRequestId> runBeforeKillId = Optional.absent();
        if (requestCleanup.getRunShellCommandBeforeKill().isPresent()) {
            SingularityTaskShellCommandRequest shellRequest = new SingularityTaskShellCommandRequest(matchingTaskId, requestCleanup.getUser(), System.currentTimeMillis(), requestCleanup.getRunShellCommandBeforeKill().get());
            taskManager.saveTaskShellCommandRequestToQueue(shellRequest);
            runBeforeKillId = Optional.of(shellRequest.getId());
        }
        taskManager.createTaskCleanup(new SingularityTaskCleanup(requestCleanup.getUser(), requestCleanup.getCleanupType().getTaskCleanupType().get(), start, matchingTaskId, requestCleanup.getMessage(), requestCleanup.getActionId(), runBeforeKillId));
    }
    if (matchingTaskIds.isEmpty() && requestCleanup.getDeployId().isPresent()) {
        Optional<SingularityExpiringBounce> expiringBounce = requestManager.getExpiringBounce(requestCleanup.getRequestId());
        if (expiringBounce.isPresent() && expiringBounce.get().getDeployId().equalsIgnoreCase(requestCleanup.getDeployId().get())) {
            LOG.info("No running tasks for request {}. Marking bounce {} complete and starting new tasks", expiringBounce.get().getRequestId(), expiringBounce.get());
            requestManager.deleteExpiringObject(SingularityExpiringBounce.class, requestCleanup.getRequestId());
        }
        requestManager.markBounceComplete(requestCleanup.getRequestId());
    }
    requestManager.addToPendingQueue(new SingularityPendingRequest(requestCleanup.getRequestId(), requestCleanup.getDeployId().get(), requestCleanup.getTimestamp(), requestCleanup.getUser(), PendingType.BOUNCE, Optional.absent(), Optional.absent(), requestCleanup.getSkipHealthchecks(), requestCleanup.getMessage(), requestCleanup.getActionId()));
    LOG.info("Added {} tasks for request {} to cleanup bounce queue in {}", matchingTaskIds.size(), requestCleanup.getRequestId(), JavaUtils.duration(start));
}
Also used : SingularityPendingRequest(com.hubspot.singularity.SingularityPendingRequest) SingularityExpiringBounce(com.hubspot.singularity.expiring.SingularityExpiringBounce) ArrayList(java.util.ArrayList) SingularityTaskShellCommandRequest(com.hubspot.singularity.SingularityTaskShellCommandRequest) SingularityTaskCleanup(com.hubspot.singularity.SingularityTaskCleanup) SingularityTaskShellCommandRequestId(com.hubspot.singularity.SingularityTaskShellCommandRequestId) SingularityTaskId(com.hubspot.singularity.SingularityTaskId)

Aggregations

SingularityTaskId (com.hubspot.singularity.SingularityTaskId)106 Test (org.junit.Test)44 SingularityTask (com.hubspot.singularity.SingularityTask)34 SingularityRequest (com.hubspot.singularity.SingularityRequest)26 ArrayList (java.util.ArrayList)26 SingularityPendingTaskId (com.hubspot.singularity.SingularityPendingTaskId)14 SingularityTaskHistoryUpdate (com.hubspot.singularity.SingularityTaskHistoryUpdate)13 MesosTaskMonitorObject (com.hubspot.mesos.json.MesosTaskMonitorObject)12 List (java.util.List)11 SingularityRequestWithState (com.hubspot.singularity.SingularityRequestWithState)10 SingularityDeployProgress (com.hubspot.singularity.SingularityDeployProgress)9 SingularityPendingRequest (com.hubspot.singularity.SingularityPendingRequest)9 SingularityRunNowRequest (com.hubspot.singularity.api.SingularityRunNowRequest)9 SingularityDeploy (com.hubspot.singularity.SingularityDeploy)8 SingularityRequestBuilder (com.hubspot.singularity.SingularityRequestBuilder)7 SingularityTaskCleanup (com.hubspot.singularity.SingularityTaskCleanup)7 SingularityTaskShellCommandRequestId (com.hubspot.singularity.SingularityTaskShellCommandRequestId)7 HashMap (java.util.HashMap)7 SingularityRequestDeployState (com.hubspot.singularity.SingularityRequestDeployState)6 SingularitySlaveUsage (com.hubspot.singularity.SingularitySlaveUsage)6