Search in sources :

Example 6 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>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)

Example 7 with SingularityTaskShellCommandRequestId

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

the class SingularityScheduler method cleanupTaskDueToDecomission.

private void cleanupTaskDueToDecomission(final Map<String, Optional<String>> requestIdsToUserToReschedule, final Set<SingularityTaskId> matchingTaskIds, SingularityTask task, SingularityMachineAbstraction<?> decommissioningObject) {
    requestIdsToUserToReschedule.put(task.getTaskRequest().getRequest().getId(), decommissioningObject.getCurrentState().getUser());
    matchingTaskIds.add(task.getTaskId());
    LOG.trace("Scheduling a cleanup task for {} due to decomissioning {}", task.getTaskId(), decommissioningObject);
    taskManager.createTaskCleanup(new SingularityTaskCleanup(decommissioningObject.getCurrentState().getUser(), TaskCleanupType.DECOMISSIONING, System.currentTimeMillis(), task.getTaskId(), Optional.of(String.format("%s %s is decomissioning", decommissioningObject.getTypeName(), decommissioningObject.getName())), Optional.<String>absent(), Optional.<SingularityTaskShellCommandRequestId>absent()));
}
Also used : SingularityTaskCleanup(com.hubspot.singularity.SingularityTaskCleanup) SingularityTaskShellCommandRequestId(com.hubspot.singularity.SingularityTaskShellCommandRequestId)

Example 8 with SingularityTaskShellCommandRequestId

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

the class TaskResource method killTask.

public SingularityTaskCleanup killTask(String taskId, Optional<SingularityKillTaskRequest> killTaskRequest, SingularityUser user) {
    final SingularityTask task = checkActiveTask(taskId, SingularityAuthorizationScope.WRITE, user);
    Optional<String> message = Optional.absent();
    Optional<Boolean> override = Optional.absent();
    Optional<String> actionId = Optional.absent();
    Optional<Boolean> waitForReplacementTask = Optional.absent();
    Optional<SingularityTaskShellCommandRequestId> runBeforeKillId = Optional.absent();
    if (killTaskRequest.isPresent()) {
        actionId = killTaskRequest.get().getActionId();
        message = killTaskRequest.get().getMessage();
        override = killTaskRequest.get().getOverride();
        waitForReplacementTask = killTaskRequest.get().getWaitForReplacementTask();
        if (killTaskRequest.get().getRunShellCommandBeforeKill().isPresent()) {
            SingularityTaskShellCommandRequest shellCommandRequest = startShellCommand(task.getTaskId(), killTaskRequest.get().getRunShellCommandBeforeKill().get(), user);
            runBeforeKillId = Optional.of(shellCommandRequest.getId());
        }
    }
    TaskCleanupType cleanupType = TaskCleanupType.USER_REQUESTED;
    if (waitForReplacementTask.or(Boolean.FALSE)) {
        cleanupType = TaskCleanupType.USER_REQUESTED_TASK_BOUNCE;
        validator.checkActionEnabled(SingularityAction.BOUNCE_TASK);
    } else {
        validator.checkActionEnabled(SingularityAction.KILL_TASK);
    }
    final long now = System.currentTimeMillis();
    final SingularityTaskCleanup taskCleanup;
    if (override.isPresent() && override.get()) {
        LOG.debug("Requested destroy of {}", taskId);
        cleanupType = TaskCleanupType.USER_REQUESTED_DESTROY;
        taskCleanup = new SingularityTaskCleanup(user.getEmail(), cleanupType, now, task.getTaskId(), message, actionId, runBeforeKillId);
        taskManager.saveTaskCleanup(taskCleanup);
    } else {
        taskCleanup = new SingularityTaskCleanup(user.getEmail(), cleanupType, now, task.getTaskId(), message, actionId, runBeforeKillId);
        SingularityCreateResult result = taskManager.createTaskCleanup(taskCleanup);
        if (result == SingularityCreateResult.EXISTED && userRequestedKillTakesPriority(taskId)) {
            taskManager.saveTaskCleanup(taskCleanup);
        } else {
            while (result == SingularityCreateResult.EXISTED) {
                Optional<SingularityTaskCleanup> cleanup = taskManager.getTaskCleanup(taskId);
                if (cleanup.isPresent()) {
                    throw new WebApplicationException(Response.status(Status.CONFLICT).entity(cleanup.get()).type(MediaType.APPLICATION_JSON).build());
                }
                result = taskManager.createTaskCleanup(taskCleanup);
            }
        }
    }
    if (cleanupType == TaskCleanupType.USER_REQUESTED_TASK_BOUNCE) {
        requestManager.addToPendingQueue(new SingularityPendingRequest(task.getTaskId().getRequestId(), task.getTaskId().getDeployId(), now, user.getEmail(), PendingType.TASK_BOUNCE, Optional.<List<String>>absent(), Optional.<String>absent(), Optional.<Boolean>absent(), message, actionId));
    }
    return taskCleanup;
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) SingularityPendingRequest(com.hubspot.singularity.SingularityPendingRequest) TaskCleanupType(com.hubspot.singularity.TaskCleanupType) SingularityCreateResult(com.hubspot.singularity.SingularityCreateResult) SingularityTask(com.hubspot.singularity.SingularityTask) SingularityTaskShellCommandRequest(com.hubspot.singularity.SingularityTaskShellCommandRequest) SingularityTaskCleanup(com.hubspot.singularity.SingularityTaskCleanup) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) SingularityTaskShellCommandRequestId(com.hubspot.singularity.SingularityTaskShellCommandRequestId)

Example 9 with SingularityTaskShellCommandRequestId

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

the class TaskResource method getShellCommandHisotryUpdates.

@GET
@Path("/task/{taskId}/command/{commandName}/{commandTimestamp}")
@ApiOperation(value = "Retrieve a list of shell commands updates for a particular shell command on a task")
public List<SingularityTaskShellCommandUpdate> getShellCommandHisotryUpdates(@Auth SingularityUser user, @PathParam("taskId") String taskId, @PathParam("commandName") String commandName, @PathParam("commandTimestamp") Long commandTimestamp) {
    authorizationHelper.checkForAuthorizationByTaskId(taskId, user, SingularityAuthorizationScope.READ);
    SingularityTaskId taskIdObj = getTaskIdFromStr(taskId);
    return taskManager.getTaskShellCommandUpdates(new SingularityTaskShellCommandRequestId(taskIdObj, commandName, commandTimestamp));
}
Also used : SingularityTaskShellCommandRequestId(com.hubspot.singularity.SingularityTaskShellCommandRequestId) SingularityTaskId(com.hubspot.singularity.SingularityTaskId) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET) ApiOperation(com.wordnik.swagger.annotations.ApiOperation)

Example 10 with SingularityTaskShellCommandRequestId

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

the class SingularityCleaner method delete.

private void delete(SingularityRequestCleanup requestCleanup, Iterable<SingularityTaskId> activeTaskIds) {
    final long start = System.currentTimeMillis();
    for (SingularityTaskId taskId : activeTaskIds) {
        LOG.debug("Adding task {} to cleanup (delete)", 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(), TaskCleanupType.REQUEST_DELETING, start, taskId, requestCleanup.getMessage(), requestCleanup.getActionId(), runBeforeKillId, requestCleanup.getRemoveFromLoadBalancer()));
    }
}
Also used : SingularityTaskShellCommandRequest(com.hubspot.singularity.SingularityTaskShellCommandRequest) SingularityTaskCleanup(com.hubspot.singularity.SingularityTaskCleanup) SingularityTaskShellCommandRequestId(com.hubspot.singularity.SingularityTaskShellCommandRequestId) SingularityTaskId(com.hubspot.singularity.SingularityTaskId)

Aggregations

SingularityTaskShellCommandRequestId (com.hubspot.singularity.SingularityTaskShellCommandRequestId)10 SingularityTaskCleanup (com.hubspot.singularity.SingularityTaskCleanup)8 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.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