Search in sources :

Example 1 with SingularityTaskShellCommandRequest

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

the class TaskManager method getTaskShellCommandHistory.

public List<SingularityTaskShellCommandHistory> getTaskShellCommandHistory(SingularityTaskId taskId) {
    List<SingularityTaskShellCommandRequest> shellRequests = getTaskShellCommandRequestsForTask(taskId);
    List<SingularityTaskShellCommandHistory> shellCommandHistory = new ArrayList<>(shellRequests.size());
    for (SingularityTaskShellCommandRequest shellRequest : shellRequests) {
        shellCommandHistory.add(new SingularityTaskShellCommandHistory(shellRequest, getTaskShellCommandUpdates(shellRequest.getId())));
    }
    return shellCommandHistory;
}
Also used : SingularityTaskShellCommandRequest(com.hubspot.singularity.SingularityTaskShellCommandRequest) ArrayList(java.util.ArrayList) SingularityTaskShellCommandHistory(com.hubspot.singularity.SingularityTaskShellCommandHistory)

Example 2 with SingularityTaskShellCommandRequest

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

the class SingularityTaskShellCommandTest method testTaskShellCommandPersistence.

@Test
public void testTaskShellCommandPersistence() {
    initRequest();
    initFirstDeploy();
    SingularityTask task = launchTask(request, firstDeploy, 1, TaskState.TASK_RUNNING);
    // test bad command first:
    setShellCommandsConfiguration();
    // bad shell cmd
    try {
        taskResource.runShellCommand(singularityUser, task.getTaskId().getId(), new SingularityShellCommand("test-cmd", Optional.of(Arrays.asList("one", "two")), user, Optional.absent()));
    } catch (WebApplicationException exception) {
        assertEquals(400, exception.getResponse().getStatus());
    }
    // bad option
    try {
        taskResource.runShellCommand(singularityUser, task.getTaskId().getId(), new SingularityShellCommand("d1", Optional.of(Arrays.asList("one", "two")), user, Optional.absent()));
    } catch (WebApplicationException exception) {
        assertEquals(400, exception.getResponse().getStatus());
    }
    SingularityTaskShellCommandRequest firstShellRequest = taskResource.runShellCommand(singularityUser, task.getTaskId().getId(), new SingularityShellCommand("d1", Optional.of(Arrays.asList("o1", "o2")), user, Optional.absent()));
    try {
        Thread.sleep(3);
    } catch (Exception e) {
    }
    SingularityTaskShellCommandRequest secondShellRequest = taskResource.runShellCommand(singularityUser, task.getTaskId().getId(), new SingularityShellCommand("d2", Optional.<List<String>>absent(), user, Optional.absent()));
    assertEquals(2, taskManager.getAllQueuedTaskShellCommandRequests().size());
    dispatchPoller.runActionOnPoll();
    assertEquals(0, taskManager.getAllQueuedTaskShellCommandRequests().size());
    assertEquals(2, taskManager.getTaskShellCommandRequestsForTask(task.getTaskId()).size());
    mesosScheduler.message(Event.Message.newBuilder().setExecutorId(MesosProtosUtils.toExecutorId(task.getMesosTask().getExecutor().getExecutorId())).setAgentId(MesosProtosUtils.toAgentId(task.getMesosTask().getAgentId())).setData(ByteString.copyFrom(updateTranscoder.toBytes(new SingularityTaskShellCommandUpdate(firstShellRequest.getId(), System.currentTimeMillis(), Optional.<String>of("hi"), Optional.<String>absent(), UpdateType.STARTED)))).build());
    mesosScheduler.message(Event.Message.newBuilder().setExecutorId(MesosProtosUtils.toExecutorId(task.getMesosTask().getExecutor().getExecutorId())).setAgentId(MesosProtosUtils.toAgentId(task.getMesosTask().getAgentId())).setData(ByteString.copyFrom(updateTranscoder.toBytes(new SingularityTaskShellCommandUpdate(new SingularityTaskShellCommandRequestId(task.getTaskId(), "wat", System.currentTimeMillis()), System.currentTimeMillis(), Optional.<String>of("hi"), Optional.<String>absent(), UpdateType.STARTED)))).build());
    mesosScheduler.message(Event.Message.newBuilder().setExecutorId(MesosProtosUtils.toExecutorId(task.getMesosTask().getExecutor().getExecutorId())).setAgentId(MesosProtosUtils.toAgentId(task.getMesosTask().getAgentId())).setData(ByteString.copyFrom(updateTranscoder.toBytes(new SingularityTaskShellCommandUpdate(new SingularityTaskShellCommandRequestId(new SingularityTaskId("makingitup", "did", System.currentTimeMillis(), 1, "host", "rack"), "wat", System.currentTimeMillis()), System.currentTimeMillis(), Optional.<String>of("hi"), Optional.<String>absent(), UpdateType.STARTED)))).build());
    assertEquals(true, taskManager.getTaskHistory(task.getTaskId()).get().getShellCommandHistory().get(1).getShellUpdates().get(0).getUpdateType() == UpdateType.STARTED);
    assertEquals(1, taskManager.getTaskShellCommandUpdates(firstShellRequest.getId()).size());
    assertEquals(0, taskManager.getTaskShellCommandUpdates(secondShellRequest.getId()).size());
}
Also used : SingularityTask(com.hubspot.singularity.SingularityTask) WebApplicationException(javax.ws.rs.WebApplicationException) SingularityShellCommand(com.hubspot.singularity.SingularityShellCommand) SingularityTaskShellCommandRequest(com.hubspot.singularity.SingularityTaskShellCommandRequest) SingularityTaskShellCommandUpdate(com.hubspot.singularity.SingularityTaskShellCommandUpdate) ArrayList(java.util.ArrayList) List(java.util.List) ByteString(com.google.protobuf.ByteString) SingularityTaskShellCommandRequestId(com.hubspot.singularity.SingularityTaskShellCommandRequestId) SingularityTaskId(com.hubspot.singularity.SingularityTaskId) WebApplicationException(javax.ws.rs.WebApplicationException) Test(org.junit.Test)

Example 3 with SingularityTaskShellCommandRequest

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

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

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

Aggregations

SingularityTaskShellCommandRequest (com.hubspot.singularity.SingularityTaskShellCommandRequest)8 SingularityTaskShellCommandRequestId (com.hubspot.singularity.SingularityTaskShellCommandRequestId)5 SingularityTaskCleanup (com.hubspot.singularity.SingularityTaskCleanup)4 SingularityTaskId (com.hubspot.singularity.SingularityTaskId)4 SingularityTask (com.hubspot.singularity.SingularityTask)3 ArrayList (java.util.ArrayList)3 SingularityPendingRequest (com.hubspot.singularity.SingularityPendingRequest)2 TaskCleanupType (com.hubspot.singularity.TaskCleanupType)2 List (java.util.List)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 ImmutableList (com.google.common.collect.ImmutableList)1 ByteString (com.google.protobuf.ByteString)1 SingularityCreateResult (com.hubspot.singularity.SingularityCreateResult)1 SingularityShellCommand (com.hubspot.singularity.SingularityShellCommand)1 SingularityTaskShellCommandHistory (com.hubspot.singularity.SingularityTaskShellCommandHistory)1 SingularityTaskShellCommandUpdate (com.hubspot.singularity.SingularityTaskShellCommandUpdate)1 SingularityExpiringBounce (com.hubspot.singularity.expiring.SingularityExpiringBounce)1 AgentID (org.apache.mesos.v1.Protos.AgentID)1 ExecutorID (org.apache.mesos.v1.Protos.ExecutorID)1 Test (org.junit.Test)1