Search in sources :

Example 11 with SingularityTaskShellCommandRequestId

use of com.hubspot.singularity.SingularityTaskShellCommandRequestId 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>empty()));
                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

SingularityTaskShellCommandRequestId (com.hubspot.singularity.SingularityTaskShellCommandRequestId)11 SingularityTaskCleanup (com.hubspot.singularity.SingularityTaskCleanup)9 SingularityTaskId (com.hubspot.singularity.SingularityTaskId)7 SingularityTaskShellCommandRequest (com.hubspot.singularity.SingularityTaskShellCommandRequest)5 ByteString (com.google.protobuf.ByteString)2 SingularityPendingRequest (com.hubspot.singularity.SingularityPendingRequest)2 SingularityShellCommand (com.hubspot.singularity.SingularityShellCommand)2 SingularityTask (com.hubspot.singularity.SingularityTask)2 SingularityTaskShellCommandUpdate (com.hubspot.singularity.SingularityTaskShellCommandUpdate)2 TaskCleanupType (com.hubspot.singularity.TaskCleanupType)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 Test (org.junit.jupiter.api.Test)2 ImmutableList (com.google.common.collect.ImmutableList)1 DeployState (com.hubspot.singularity.DeployState)1 SingularityCreateResult (com.hubspot.singularity.SingularityCreateResult)1 SingularityDeployProgress (com.hubspot.singularity.SingularityDeployProgress)1 SingularityDeployResult (com.hubspot.singularity.SingularityDeployResult)1 SingularityPriorityFreezeParent (com.hubspot.singularity.SingularityPriorityFreezeParent)1