Search in sources :

Example 6 with SingularityTaskCleanup

use of com.hubspot.singularity.SingularityTaskCleanup 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 7 with SingularityTaskCleanup

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

the class SingularityCleaner method drainTaskCleanupQueue.

private int drainTaskCleanupQueue() {
    final long start = System.currentTimeMillis();
    final Map<String, List<SingularityTaskCleanup>> cleanupTasks = taskManager.getCleanupTasks().stream().collect(Collectors.groupingBy((taskCleanup) -> taskCleanup.getTaskId().getRequestId()));
    if (cleanupTasks.isEmpty()) {
        LOG.trace("Task cleanup queue is empty");
        return 0;
    }
    AtomicInteger killedTasks = new AtomicInteger(0);
    cleanupTasks.entrySet().parallelStream().forEach((taskCleanupsForRequest) -> {
        lock.runWithRequestLock(() -> {
            processTaskCleanupsForRequest(taskCleanupsForRequest.getKey(), taskCleanupsForRequest.getValue(), killedTasks);
        }, taskCleanupsForRequest.getKey(), String.format("%s#%s", getClass().getSimpleName(), "drainTaskCleanupQueue"));
    });
    LOG.info("Killed {} tasks in {}", killedTasks, JavaUtils.duration(start));
    return cleanupTasks.size();
}
Also used : SingularityTask(com.hubspot.singularity.SingularityTask) DeployManager(com.hubspot.singularity.data.DeployManager) SingularityLoadBalancerUpdate(com.hubspot.singularity.SingularityLoadBalancerUpdate) Inject(com.google.inject.Inject) SingularityRequestCleanup(com.hubspot.singularity.SingularityRequestCleanup) RequestManager(com.hubspot.singularity.data.RequestManager) LoggerFactory(org.slf4j.LoggerFactory) SingularityRequestLbCleanup(com.hubspot.singularity.SingularityRequestLbCleanup) SingularityTaskShellCommandRequestId(com.hubspot.singularity.SingularityTaskShellCommandRequestId) SingularityExpiringBounce(com.hubspot.singularity.expiring.SingularityExpiringBounce) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HashMultiset(com.google.common.collect.HashMultiset) Optional(com.google.common.base.Optional) Map(java.util.Map) TaskManager(com.hubspot.singularity.data.TaskManager) SingularityTaskId(com.hubspot.singularity.SingularityTaskId) ImmutableMap(com.google.common.collect.ImmutableMap) SingularityKilledTaskIdRecord(com.hubspot.singularity.SingularityKilledTaskIdRecord) Set(java.util.Set) Collectors(java.util.stream.Collectors) RequestState(com.hubspot.singularity.RequestState) PendingType(com.hubspot.singularity.SingularityPendingRequest.PendingType) DeployHealth(com.hubspot.singularity.scheduler.SingularityDeployHealthHelper.DeployHealth) List(java.util.List) JavaUtils(com.hubspot.mesos.JavaUtils) SingularityTaskShellCommandRequest(com.hubspot.singularity.SingularityTaskShellCommandRequest) SingularityTaskShellCommandUpdate(com.hubspot.singularity.SingularityTaskShellCommandUpdate) LoadBalancerClient(com.hubspot.singularity.hooks.LoadBalancerClient) SingularitySchedulerLock(com.hubspot.singularity.mesos.SingularitySchedulerLock) RequestCleanupType(com.hubspot.singularity.RequestCleanupType) SingularityMesosScheduler(com.hubspot.singularity.mesos.SingularityMesosScheduler) SingularityRequestWithState(com.hubspot.singularity.SingularityRequestWithState) SingularityRequest(com.hubspot.singularity.SingularityRequest) Iterables(com.google.common.collect.Iterables) SingularityPendingTask(com.hubspot.singularity.SingularityPendingTask) SingularityRequestHistory(com.hubspot.singularity.SingularityRequestHistory) Multiset(com.google.common.collect.Multiset) SingularityPendingRequest(com.hubspot.singularity.SingularityPendingRequest) Singleton(javax.inject.Singleton) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LoadBalancerRequestId(com.hubspot.singularity.LoadBalancerRequestType.LoadBalancerRequestId) SingularityDeleteResult(com.hubspot.singularity.SingularityDeleteResult) SingularityDeployKey(com.hubspot.singularity.SingularityDeployKey) SingularityExceptionNotifier(com.hubspot.singularity.sentry.SingularityExceptionNotifier) SingularityConfiguration(com.hubspot.singularity.config.SingularityConfiguration) SingularityTaskCleanup(com.hubspot.singularity.SingularityTaskCleanup) Logger(org.slf4j.Logger) BaragonRequestState(com.hubspot.baragon.models.BaragonRequestState) SingularityDeploy(com.hubspot.singularity.SingularityDeploy) TimeUnit(java.util.concurrent.TimeUnit) SingularityRequestDeployState(com.hubspot.singularity.SingularityRequestDeployState) TaskCleanupType(com.hubspot.singularity.TaskCleanupType) LoadBalancerRequestType(com.hubspot.singularity.LoadBalancerRequestType) RequestHistoryHelper(com.hubspot.singularity.data.history.RequestHistoryHelper) Collections(java.util.Collections) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) List(java.util.List) ArrayList(java.util.ArrayList)

Example 8 with SingularityTaskCleanup

use of com.hubspot.singularity.SingularityTaskCleanup 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)

Example 9 with SingularityTaskCleanup

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

the class SingularityCleaner method pause.

private TaskCleanupType pause(SingularityRequestCleanup requestCleanup, Iterable<SingularityTaskId> activeTaskIds) {
    final long start = System.currentTimeMillis();
    boolean killTasks = requestCleanup.getKillTasks().or(configuration.isDefaultValueForKillTasksOfPausedRequests());
    if (requestCleanup.getRunShellCommandBeforeKill().isPresent()) {
        killTasks = false;
    }
    TaskCleanupType cleanupType = killTasks ? TaskCleanupType.PAUSE : TaskCleanupType.PAUSING;
    for (SingularityTaskId taskId : activeTaskIds) {
        LOG.debug("Adding task {} to cleanup (pause)", taskId.getId());
        Optional<SingularityTaskShellCommandRequestId> runBeforeKillId = Optional.absent();
        if (requestCleanup.getRunShellCommandBeforeKill().isPresent()) {
            SingularityTaskShellCommandRequest shellRequest = new SingularityTaskShellCommandRequest(taskId, requestCleanup.getUser(), System.currentTimeMillis(), requestCleanup.getRunShellCommandBeforeKill().get());
            taskManager.saveTaskShellCommandRequestToQueue(shellRequest);
            runBeforeKillId = Optional.of(shellRequest.getId());
        }
        taskManager.createTaskCleanup(new SingularityTaskCleanup(requestCleanup.getUser(), cleanupType, start, taskId, requestCleanup.getMessage(), requestCleanup.getActionId(), runBeforeKillId));
    }
    return cleanupType;
}
Also used : SingularityTaskShellCommandRequest(com.hubspot.singularity.SingularityTaskShellCommandRequest) SingularityTaskCleanup(com.hubspot.singularity.SingularityTaskCleanup) TaskCleanupType(com.hubspot.singularity.TaskCleanupType) SingularityTaskShellCommandRequestId(com.hubspot.singularity.SingularityTaskShellCommandRequestId) SingularityTaskId(com.hubspot.singularity.SingularityTaskId)

Example 10 with SingularityTaskCleanup

use of com.hubspot.singularity.SingularityTaskCleanup 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)

Aggregations

SingularityTaskCleanup (com.hubspot.singularity.SingularityTaskCleanup)16 SingularityTaskId (com.hubspot.singularity.SingularityTaskId)9 SingularityTaskShellCommandRequestId (com.hubspot.singularity.SingularityTaskShellCommandRequestId)9 SingularityTask (com.hubspot.singularity.SingularityTask)5 SingularityTaskShellCommandRequest (com.hubspot.singularity.SingularityTaskShellCommandRequest)5 Test (org.junit.Test)5 SingularityPendingRequest (com.hubspot.singularity.SingularityPendingRequest)4 TaskCleanupType (com.hubspot.singularity.TaskCleanupType)4 ArrayList (java.util.ArrayList)3 Optional (com.google.common.base.Optional)2 Inject (com.google.inject.Inject)2 SingularityDeleteResult (com.hubspot.singularity.SingularityDeleteResult)2 SingularityDeploy (com.hubspot.singularity.SingularityDeploy)2 SingularityDeployKey (com.hubspot.singularity.SingularityDeployKey)2 PendingType (com.hubspot.singularity.SingularityPendingRequest.PendingType)2 SingularityRequest (com.hubspot.singularity.SingularityRequest)2 SingularityRequestDeployState (com.hubspot.singularity.SingularityRequestDeployState)2 SingularityRequestWithState (com.hubspot.singularity.SingularityRequestWithState)2 SingularityTaskShellCommandUpdate (com.hubspot.singularity.SingularityTaskShellCommandUpdate)2 SingularityBounceRequest (com.hubspot.singularity.api.SingularityBounceRequest)2