Search in sources :

Example 11 with SingularityPendingTask

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

the class SingularityScheduler method checkForStaleScheduledTasks.

private List<SingularityTaskRequest> checkForStaleScheduledTasks(List<SingularityPendingTask> pendingTasks, List<SingularityTaskRequest> taskRequests) {
    final Set<String> foundPendingTaskId = Sets.newHashSetWithExpectedSize(taskRequests.size());
    final Set<String> requestIds = Sets.newHashSetWithExpectedSize(taskRequests.size());
    for (SingularityTaskRequest taskRequest : taskRequests) {
        foundPendingTaskId.add(taskRequest.getPendingTask().getPendingTaskId().getId());
        requestIds.add(taskRequest.getRequest().getId());
    }
    for (SingularityPendingTask pendingTask : pendingTasks) {
        if (!foundPendingTaskId.contains(pendingTask.getPendingTaskId().getId())) {
            LOG.info("Removing stale pending task {}", pendingTask.getPendingTaskId());
            taskManager.deletePendingTask(pendingTask.getPendingTaskId());
        }
    }
    // TODO this check isn't necessary if we keep track better during deploys
    final Map<String, SingularityRequestDeployState> deployStates = deployManager.getRequestDeployStatesByRequestIds(requestIds);
    final List<SingularityTaskRequest> taskRequestsWithValidDeploys = Lists.newArrayListWithCapacity(taskRequests.size());
    for (SingularityTaskRequest taskRequest : taskRequests) {
        SingularityRequestDeployState requestDeployState = deployStates.get(taskRequest.getRequest().getId());
        if (!matchesDeploy(requestDeployState, taskRequest) && !(taskRequest.getRequest().getRequestType() == RequestType.RUN_ONCE)) {
            LOG.info("Removing stale pending task {} because the deployId did not match active/pending deploys {}", taskRequest.getPendingTask().getPendingTaskId(), requestDeployState);
            taskManager.deletePendingTask(taskRequest.getPendingTask().getPendingTaskId());
        } else {
            taskRequestsWithValidDeploys.add(taskRequest);
        }
    }
    return taskRequestsWithValidDeploys;
}
Also used : SingularityPendingTask(com.hubspot.singularity.SingularityPendingTask) SingularityRequestDeployState(com.hubspot.singularity.SingularityRequestDeployState) SingularityTaskRequest(com.hubspot.singularity.SingularityTaskRequest)

Example 12 with SingularityPendingTask

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

the class SingularityCleaner method processRequestCleanup.

private void processRequestCleanup(long start, AtomicInteger numTasksKilled, AtomicInteger numScheduledTasksRemoved, SingularityRequestCleanup requestCleanup) {
    final List<SingularityTaskId> activeTaskIds = taskManager.getActiveTaskIdsForRequest(requestCleanup.getRequestId());
    final List<SingularityPendingTask> pendingTasks = taskManager.getPendingTasksForRequest(requestCleanup.getRequestId());
    final String requestId = requestCleanup.getRequestId();
    final Optional<SingularityRequestWithState> requestWithState = requestManager.getRequest(requestId);
    boolean killActiveTasks = requestCleanup.getKillTasks().or(configuration.isDefaultValueForKillTasksOfPausedRequests());
    boolean killScheduledTasks = true;
    switch(requestCleanup.getCleanupType()) {
        case PAUSING:
            if (SingularityRequestWithState.isActive(requestWithState)) {
                if (isObsolete(start, requestCleanup.getTimestamp())) {
                    killScheduledTasks = false;
                    killActiveTasks = false;
                    LOG.info("Ignoring {}, because {} is {}", requestCleanup, requestCleanup.getRequestId(), requestWithState.get().getState());
                } else {
                    LOG.debug("Waiting on {} (it will expire after {}), because {} is {}", requestCleanup, JavaUtils.durationFromMillis(getObsoleteExpirationTime()), requestCleanup.getRequestId(), requestWithState.get().getState());
                    return;
                }
            } else {
                if (pause(requestCleanup, activeTaskIds) == TaskCleanupType.PAUSING) {
                    killActiveTasks = false;
                }
            }
            break;
        case DELETING:
            if (!Iterables.isEmpty(activeTaskIds)) {
                killActiveTasks = false;
                killScheduledTasks = false;
                delete(requestCleanup, activeTaskIds);
            } else {
                Optional<SingularityRequestHistory> maybeHistory = requestHistoryHelper.getLastHistory(requestId);
                if (maybeHistory.isPresent()) {
                    if (maybeHistory.get().getRequest().isLoadBalanced() && configuration.isDeleteRemovedRequestsFromLoadBalancer() && requestCleanup.getRemoveFromLoadBalancer().or(true)) {
                        createLbCleanupRequest(requestId, activeTaskIds);
                    }
                    requestManager.markDeleted(maybeHistory.get().getRequest(), start, requestCleanup.getUser(), requestCleanup.getMessage());
                }
                cleanupRequestData(requestCleanup);
            }
            break;
        case BOUNCE:
        case INCREMENTAL_BOUNCE:
            killActiveTasks = false;
            killScheduledTasks = false;
            bounce(requestCleanup, activeTaskIds);
            break;
    }
    if (killActiveTasks) {
        for (SingularityTaskId matchingTaskId : activeTaskIds) {
            LOG.debug("Killing task {} due to {}", matchingTaskId, requestCleanup);
            scheduler.killAndRecord(matchingTaskId, requestCleanup.getCleanupType(), Optional.absent());
            numTasksKilled.getAndIncrement();
        }
    } else {
        LOG.info("Active tasks for {} not killed", requestCleanup);
    }
    if (killScheduledTasks) {
        for (SingularityPendingTask matchingTask : Iterables.filter(pendingTasks, SingularityPendingTask.matchingRequest(requestId))) {
            LOG.debug("Deleting scheduled task {} due to {}", matchingTask, requestCleanup);
            taskManager.deletePendingTask(matchingTask.getPendingTaskId());
            numScheduledTasksRemoved.getAndIncrement();
        }
    }
    requestManager.deleteCleanRequest(requestId, requestCleanup.getCleanupType());
}
Also used : SingularityRequestHistory(com.hubspot.singularity.SingularityRequestHistory) SingularityPendingTask(com.hubspot.singularity.SingularityPendingTask) SingularityRequestWithState(com.hubspot.singularity.SingularityRequestWithState) SingularityTaskId(com.hubspot.singularity.SingularityTaskId)

Example 13 with SingularityPendingTask

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

the class SingularityScheduler method schedule.

private void schedule(int numMissingInstances, List<SingularityTaskId> matchingTaskIds, SingularityRequest request, RequestState state, SingularityDeployStatistics deployStatistics, SingularityPendingRequest pendingRequest, Optional<SingularityPendingDeploy> maybePendingDeploy) {
    final List<SingularityPendingTask> scheduledTasks = getScheduledTaskIds(numMissingInstances, matchingTaskIds, request, state, deployStatistics, pendingRequest.getDeployId(), pendingRequest, maybePendingDeploy);
    if (!scheduledTasks.isEmpty()) {
        LOG.trace("Scheduling tasks: {}", scheduledTasks);
        for (SingularityPendingTask scheduledTask : scheduledTasks) {
            taskManager.savePendingTask(scheduledTask);
        }
    } else {
        LOG.info("No new scheduled tasks found for {}, setting state to {}", request.getId(), RequestState.FINISHED);
        requestManager.finish(request, System.currentTimeMillis());
    }
}
Also used : SingularityPendingTask(com.hubspot.singularity.SingularityPendingTask)

Example 14 with SingularityPendingTask

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

the class SingularitySchedulerTest method testImmediateRunReplacesScheduledTask.

@Test
public void testImmediateRunReplacesScheduledTask() {
    initScheduledRequest();
    SingularityDeploy deploy = SingularityDeploy.newBuilder(requestId, firstDeployId).setCommand(Optional.of("sleep 100")).build();
    SingularityDeployRequest singularityDeployRequest = new SingularityDeployRequest(deploy, Optional.absent(), Optional.absent(), Optional.absent());
    deployResource.deploy(singularityDeployRequest, singularityUser);
    scheduler.drainPendingQueue();
    SingularityPendingTask task1 = createAndSchedulePendingTask(firstDeployId);
    Assert.assertEquals(1, taskManager.getPendingTaskIds().size());
    Assert.assertEquals(PendingType.NEW_DEPLOY, taskManager.getPendingTaskIds().get(0).getPendingType());
    requestManager.addToPendingQueue(new SingularityPendingRequest(requestId, deploy.getId(), System.currentTimeMillis(), Optional.absent(), PendingType.IMMEDIATE, deploy.getSkipHealthchecksOnDeploy(), Optional.absent()));
    scheduler.drainPendingQueue();
    Assert.assertEquals(1, taskManager.getPendingTaskIds().size());
    Assert.assertEquals(PendingType.IMMEDIATE, taskManager.getPendingTaskIds().get(0).getPendingType());
}
Also used : SingularityDeployRequest(com.hubspot.singularity.api.SingularityDeployRequest) SingularityPendingRequest(com.hubspot.singularity.SingularityPendingRequest) SingularityPendingTask(com.hubspot.singularity.SingularityPendingTask) SingularityDeploy(com.hubspot.singularity.SingularityDeploy) Test(org.junit.Test)

Example 15 with SingularityPendingTask

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

the class SingularitySchedulerTest method testNotAcceptOfferWithRoleForRequestWithoutRole.

@Test
public void testNotAcceptOfferWithRoleForRequestWithoutRole() {
    SingularityRequestBuilder bldr = new SingularityRequestBuilder(requestId, RequestType.ON_DEMAND);
    requestResource.postRequest(bldr.build(), singularityUser);
    deploy("d2");
    SingularityRunNowRequest runNowRequest = new SingularityRunNowRequestBuilder().setResources(new Resources(2, 2, 0)).build();
    requestResource.scheduleImmediately(singularityUser, requestId, runNowRequest);
    scheduler.drainPendingQueue();
    SingularityPendingTask pendingTaskWithResources = taskManager.getPendingTasks().get(0);
    Assert.assertTrue(pendingTaskWithResources.getResources().isPresent());
    Assert.assertEquals(pendingTaskWithResources.getResources().get().getCpus(), 2, 0.0);
    sms.resourceOffers(Arrays.asList(createOffer(5, 5, 5, Optional.of("test-role"))));
    pendingTaskWithResources = taskManager.getPendingTasks().get(0);
    Assert.assertTrue(pendingTaskWithResources.getResources().isPresent());
    Assert.assertEquals(pendingTaskWithResources.getResources().get().getCpus(), 2, 0.0);
}
Also used : SingularityRunNowRequest(com.hubspot.singularity.api.SingularityRunNowRequest) SingularityRunNowRequestBuilder(com.hubspot.singularity.SingularityRunNowRequestBuilder) SingularityRequestBuilder(com.hubspot.singularity.SingularityRequestBuilder) SingularityPendingTask(com.hubspot.singularity.SingularityPendingTask) Resources(com.hubspot.mesos.Resources) Test(org.junit.Test)

Aggregations

SingularityPendingTask (com.hubspot.singularity.SingularityPendingTask)22 SingularityPendingTaskId (com.hubspot.singularity.SingularityPendingTaskId)9 Test (org.junit.Test)8 SingularityTask (com.hubspot.singularity.SingularityTask)6 SingularityTaskId (com.hubspot.singularity.SingularityTaskId)6 SingularityTaskRequest (com.hubspot.singularity.SingularityTaskRequest)6 SingularityPendingRequest (com.hubspot.singularity.SingularityPendingRequest)4 SingularityPendingTaskBuilder (com.hubspot.singularity.SingularityPendingTaskBuilder)4 SingularityRequest (com.hubspot.singularity.SingularityRequest)4 SingularityRequestWithState (com.hubspot.singularity.SingularityRequestWithState)4 Resources (com.hubspot.mesos.Resources)3 SingularityDeploy (com.hubspot.singularity.SingularityDeploy)3 SingularityRequestBuilder (com.hubspot.singularity.SingularityRequestBuilder)3 Timed (com.codahale.metrics.annotation.Timed)2 MachineState (com.hubspot.singularity.MachineState)2 SingularityDeployKey (com.hubspot.singularity.SingularityDeployKey)2 SingularityPendingDeploy (com.hubspot.singularity.SingularityPendingDeploy)2 SingularityRunNowRequestBuilder (com.hubspot.singularity.SingularityRunNowRequestBuilder)2 SingularityRunNowRequest (com.hubspot.singularity.api.SingularityRunNowRequest)2 TaskInfo (org.apache.mesos.v1.Protos.TaskInfo)2