Search in sources :

Example 1 with SingularityShellCommand

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

the class SingularitySchedulerTest method testIncrementalBounce.

@Test
public void testIncrementalBounce() {
    initRequest();
    // set up slaves so scale validate will pass
    resourceOffers(2);
    SingularityRequest request = requestResource.getRequest(requestId, singularityUser).getRequest();
    requestResource.postRequest(request.toBuilder().setSlavePlacement(Optional.of(SlavePlacement.SEPARATE_BY_REQUEST)).setInstances(Optional.of(2)).build(), singularityUser);
    initHCDeploy();
    SingularityTask taskOne = startSeparatePlacementTask(firstDeploy, 1);
    SingularityTask taskTwo = startSeparatePlacementTask(firstDeploy, 2);
    requestManager.createCleanupRequest(new SingularityRequestCleanup(user, RequestCleanupType.INCREMENTAL_BOUNCE, System.currentTimeMillis(), Optional.<Boolean>absent(), Optional.absent(), requestId, Optional.of(firstDeployId), Optional.<Boolean>absent(), Optional.<String>absent(), Optional.<String>absent(), Optional.<SingularityShellCommand>absent()));
    Assert.assertTrue(requestManager.cleanupRequestExists(requestId));
    cleaner.drainCleanupQueue();
    Assert.assertTrue(!requestManager.cleanupRequestExists(requestId));
    Assert.assertEquals(2, taskManager.getCleanupTaskIds().size());
    resourceOffers(3);
    SingularityTask taskThree = null;
    for (SingularityTask task : taskManager.getActiveTasks()) {
        if (!task.getTaskId().equals(taskOne.getTaskId()) && !task.getTaskId().equals(taskTwo.getTaskId())) {
            taskThree = task;
        }
    }
    statusUpdate(taskThree, TaskState.TASK_RUNNING, Optional.of(1L));
    Assert.assertEquals(3, taskManager.getActiveTaskIds().size());
    cleaner.drainCleanupQueue();
    // No old tasks should be killed before new ones pass healthchecks
    Assert.assertEquals(2, taskManager.getCleanupTaskIds().size());
    taskManager.saveHealthcheckResult(new SingularityTaskHealthcheckResult(Optional.of(200), Optional.of(1000L), System.currentTimeMillis(), Optional.<String>absent(), Optional.<String>absent(), taskThree.getTaskId(), Optional.<Boolean>absent()));
    cleaner.drainCleanupQueue();
    Assert.assertEquals(1, taskManager.getCleanupTaskIds().size());
    statusUpdate(taskOne, TaskState.TASK_KILLED);
    resourceOffers(3);
    SingularityTask taskFour = null;
    for (SingularityTask task : taskManager.getActiveTasks()) {
        if (!task.getTaskId().equals(taskOne.getTaskId()) && !task.getTaskId().equals(taskTwo.getTaskId()) && !task.getTaskId().equals(taskThree.getTaskId())) {
            taskFour = task;
        }
    }
    statusUpdate(taskFour, TaskState.TASK_RUNNING, Optional.of(1L));
    taskManager.saveHealthcheckResult(new SingularityTaskHealthcheckResult(Optional.of(200), Optional.of(1000L), System.currentTimeMillis(), Optional.<String>absent(), Optional.<String>absent(), taskFour.getTaskId(), Optional.<Boolean>absent()));
    cleaner.drainCleanupQueue();
    Assert.assertTrue(taskManager.getCleanupTaskIds().isEmpty());
}
Also used : SingularityRequestCleanup(com.hubspot.singularity.SingularityRequestCleanup) SingularityTaskHealthcheckResult(com.hubspot.singularity.SingularityTaskHealthcheckResult) SingularityTask(com.hubspot.singularity.SingularityTask) SingularityShellCommand(com.hubspot.singularity.SingularityShellCommand) SingularityRequest(com.hubspot.singularity.SingularityRequest) Test(org.junit.Test)

Example 2 with SingularityShellCommand

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

the class SingularitySchedulerTest method badPauseExpires.

@Test
public void badPauseExpires() {
    initRequest();
    requestManager.createCleanupRequest(new SingularityRequestCleanup(Optional.<String>absent(), RequestCleanupType.PAUSING, System.currentTimeMillis(), Optional.<Boolean>absent(), Optional.absent(), requestId, Optional.<String>absent(), Optional.<Boolean>absent(), Optional.<String>absent(), Optional.<String>absent(), Optional.<SingularityShellCommand>absent()));
    cleaner.drainCleanupQueue();
    Assert.assertTrue(!requestManager.getCleanupRequests().isEmpty());
    configuration.setCleanupEverySeconds(0);
    sleep(1);
    cleaner.drainCleanupQueue();
    Assert.assertTrue(requestManager.getCleanupRequests().isEmpty());
}
Also used : SingularityRequestCleanup(com.hubspot.singularity.SingularityRequestCleanup) SingularityShellCommand(com.hubspot.singularity.SingularityShellCommand) Test(org.junit.Test)

Example 3 with SingularityShellCommand

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

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

the class SingularityTaskShellCommandTest method testRunCommandBeforeBounceKill.

@Test
public void testRunCommandBeforeBounceKill() {
    setShellCommandsConfiguration();
    initRequest();
    initFirstDeploy();
    launchTask(request, firstDeploy, 1, TaskState.TASK_RUNNING);
    requestResource.bounce(requestId, Optional.of(new SingularityBounceRequest(Optional.absent(), Optional.absent(), Optional.absent(), Optional.absent(), Optional.absent(), Optional.of(new SingularityShellCommand("d1", Optional.of(Arrays.asList("o1", "o2")), user, Optional.absent())))), singularityUser);
    cleaner.drainCleanupQueue();
    List<SingularityTaskCleanup> taskCleanups = taskManager.getCleanupTasks();
    Assert.assertTrue(taskCleanups.get(0).getRunBeforeKillId().isPresent());
    SingularityTaskShellCommandRequestId shellCommandRequestId = taskCleanups.get(0).getRunBeforeKillId().get();
    cleaner.drainCleanupQueue();
    Assert.assertEquals(1, taskManager.getCleanupTaskIds().size());
    launchTask(request, firstDeploy, 1, TaskState.TASK_RUNNING);
    Assert.assertEquals(2, taskManager.getActiveTaskIdsForRequest(requestId).size());
    cleaner.drainCleanupQueue();
    Assert.assertEquals(2, taskManager.getActiveTaskIdsForRequest(requestId).size());
    taskManager.saveTaskShellCommandUpdate(new SingularityTaskShellCommandUpdate(shellCommandRequestId, System.currentTimeMillis(), Optional.<String>absent(), Optional.<String>absent(), UpdateType.ACKED));
    cleaner.drainCleanupQueue();
    Assert.assertEquals(2, taskManager.getActiveTaskIdsForRequest(requestId).size());
    taskManager.saveTaskShellCommandUpdate(new SingularityTaskShellCommandUpdate(shellCommandRequestId, System.currentTimeMillis(), Optional.<String>absent(), Optional.<String>absent(), UpdateType.FINISHED));
    cleaner.drainCleanupQueue();
    Assert.assertEquals(1, taskManager.getKilledTaskIdRecords().size());
}
Also used : SingularityShellCommand(com.hubspot.singularity.SingularityShellCommand) SingularityTaskCleanup(com.hubspot.singularity.SingularityTaskCleanup) SingularityTaskShellCommandUpdate(com.hubspot.singularity.SingularityTaskShellCommandUpdate) SingularityBounceRequest(com.hubspot.singularity.api.SingularityBounceRequest) SingularityTaskShellCommandRequestId(com.hubspot.singularity.SingularityTaskShellCommandRequestId) ByteString(com.google.protobuf.ByteString) Test(org.junit.Test)

Example 5 with SingularityShellCommand

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

the class RequestResource method bounce.

public SingularityRequestParent bounce(String requestId, Optional<SingularityBounceRequest> bounceRequest, SingularityUser user) {
    SingularityRequestWithState requestWithState = fetchRequestWithState(requestId, user);
    authorizationHelper.checkForAuthorization(requestWithState.getRequest(), user, SingularityAuthorizationScope.WRITE);
    validator.checkActionEnabled(SingularityAction.BOUNCE_REQUEST);
    checkBadRequest(requestWithState.getRequest().isLongRunning(), "Can not bounce a %s request (%s)", requestWithState.getRequest().getRequestType(), requestWithState);
    checkConflict(requestWithState.getState() != RequestState.PAUSED, "Request %s is paused. Unable to bounce (it must be manually unpaused first)", requestWithState.getRequest().getId());
    final boolean isIncrementalBounce = bounceRequest.isPresent() && bounceRequest.get().getIncremental().or(false);
    validator.checkResourcesForBounce(requestWithState.getRequest(), isIncrementalBounce);
    validator.checkRequestForPriorityFreeze(requestWithState.getRequest());
    final Optional<Boolean> skipHealthchecks = bounceRequest.isPresent() ? bounceRequest.get().getSkipHealthchecks() : Optional.<Boolean>absent();
    Optional<String> message = Optional.absent();
    Optional<String> actionId = Optional.absent();
    Optional<SingularityShellCommand> runBeforeKill = Optional.absent();
    if (bounceRequest.isPresent()) {
        actionId = bounceRequest.get().getActionId();
        message = bounceRequest.get().getMessage();
        if (bounceRequest.get().getRunShellCommandBeforeKill().isPresent()) {
            validator.checkValidShellCommand(bounceRequest.get().getRunShellCommandBeforeKill().get());
            runBeforeKill = bounceRequest.get().getRunShellCommandBeforeKill();
        }
    }
    if (!actionId.isPresent()) {
        actionId = Optional.of(UUID.randomUUID().toString());
    }
    final String deployId = getAndCheckDeployId(requestId);
    checkConflict(!(requestManager.markAsBouncing(requestId) == SingularityCreateResult.EXISTED), "%s is already bouncing", requestId);
    requestManager.createCleanupRequest(new SingularityRequestCleanup(user.getEmail(), isIncrementalBounce ? RequestCleanupType.INCREMENTAL_BOUNCE : RequestCleanupType.BOUNCE, System.currentTimeMillis(), Optional.absent(), Optional.absent(), requestId, Optional.of(deployId), skipHealthchecks, message, actionId, runBeforeKill));
    requestManager.bounce(requestWithState.getRequest(), System.currentTimeMillis(), Optional.of(user.getId()), message);
    final SingularityBounceRequest validatedBounceRequest = validator.checkBounceRequest(bounceRequest.or(SingularityBounceRequest.defaultRequest()));
    requestManager.saveExpiringObject(new SingularityExpiringBounce(requestId, deployId, Optional.of(user.getId()), System.currentTimeMillis(), validatedBounceRequest, actionId.get()));
    return fillEntireRequest(requestWithState);
}
Also used : SingularityRequestCleanup(com.hubspot.singularity.SingularityRequestCleanup) SingularityShellCommand(com.hubspot.singularity.SingularityShellCommand) SingularityExpiringBounce(com.hubspot.singularity.expiring.SingularityExpiringBounce) SingularityRequestWithState(com.hubspot.singularity.SingularityRequestWithState) SingularityBounceRequest(com.hubspot.singularity.api.SingularityBounceRequest)

Aggregations

SingularityShellCommand (com.hubspot.singularity.SingularityShellCommand)7 SingularityRequestCleanup (com.hubspot.singularity.SingularityRequestCleanup)5 Test (org.junit.Test)4 ByteString (com.google.protobuf.ByteString)2 SingularityRequestWithState (com.hubspot.singularity.SingularityRequestWithState)2 SingularityTask (com.hubspot.singularity.SingularityTask)2 SingularityTaskShellCommandRequestId (com.hubspot.singularity.SingularityTaskShellCommandRequestId)2 SingularityTaskShellCommandUpdate (com.hubspot.singularity.SingularityTaskShellCommandUpdate)2 SingularityBounceRequest (com.hubspot.singularity.api.SingularityBounceRequest)2 SingularityCreateResult (com.hubspot.singularity.SingularityCreateResult)1 SingularityRequest (com.hubspot.singularity.SingularityRequest)1 SingularityTaskCleanup (com.hubspot.singularity.SingularityTaskCleanup)1 SingularityTaskHealthcheckResult (com.hubspot.singularity.SingularityTaskHealthcheckResult)1 SingularityTaskId (com.hubspot.singularity.SingularityTaskId)1 SingularityTaskShellCommandRequest (com.hubspot.singularity.SingularityTaskShellCommandRequest)1 SingularityExpiringBounce (com.hubspot.singularity.expiring.SingularityExpiringBounce)1 SingularityExpiringPause (com.hubspot.singularity.expiring.SingularityExpiringPause)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 WebApplicationException (javax.ws.rs.WebApplicationException)1