Search in sources :

Example 6 with SingularityBounceRequest

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

use of com.hubspot.singularity.api.SingularityBounceRequest in project Singularity by HubSpot.

the class SingularityExpiringActionsTest method testExpiringIncrementalBounce.

@Test
public void testExpiringIncrementalBounce() {
    initRequest();
    requestResource.scale(requestId, new SingularityScaleRequest(Optional.of(3), Optional.absent(), Optional.absent(), Optional.absent(), Optional.absent(), Optional.absent(), Optional.absent(), Optional.absent()), singularityUser);
    initFirstDeploy();
    startTask(firstDeploy, 1);
    startTask(firstDeploy, 2);
    startTask(firstDeploy, 3);
    requestResource.bounce(requestId, Optional.of(new SingularityBounceRequest(Optional.of(true), Optional.absent(), Optional.of(1L), Optional.absent(), Optional.of("msg"), Optional.absent())), singularityUser);
    Assert.assertTrue(requestManager.cleanupRequestExists(requestId));
    Assert.assertEquals("msg", requestManager.getCleanupRequests().get(0).getMessage().get());
    Assert.assertTrue(requestManager.getCleanupRequests().get(0).getActionId().isPresent());
    String actionId = requestManager.getCleanupRequests().get(0).getActionId().get();
    cleaner.drainCleanupQueue();
    Assert.assertTrue(!requestManager.cleanupRequestExists(requestId));
    Assert.assertTrue(taskManager.getCleanupTaskIds().size() == 3);
    Assert.assertEquals("msg", taskManager.getCleanupTasks().get(0).getMessage().get());
    Assert.assertEquals(actionId, taskManager.getCleanupTasks().get(0).getActionId().get());
    startTask(firstDeploy, 4);
    // launchTask(request, firstDeploy, 5, TaskState.TASK_STARTING);
    cleaner.drainCleanupQueue();
    Assert.assertEquals(1, taskManager.getKilledTaskIdRecords().size());
    Assert.assertEquals(4, taskManager.getActiveTaskIds().size());
    try {
        Thread.sleep(2);
    } catch (InterruptedException ie) {
    }
    expiringUserActionPoller.runActionOnPoll();
    cleaner.drainCleanupQueue();
    resourceOffers();
    killKilledTasks();
    Assert.assertTrue(!requestManager.getExpiringBounce(requestId).isPresent());
    Assert.assertTrue(requestManager.getPendingRequests().isEmpty());
    Assert.assertTrue(taskManager.getPendingTaskIds().isEmpty());
    Assert.assertTrue(taskManager.getActiveTaskIds().size() == 3);
    Assert.assertTrue(!requestManager.cleanupRequestExists(requestId));
    Assert.assertTrue(taskManager.getCleanupTasks().isEmpty());
}
Also used : SingularityBounceRequest(com.hubspot.singularity.api.SingularityBounceRequest) SingularityScaleRequest(com.hubspot.singularity.api.SingularityScaleRequest) Test(org.junit.Test)

Example 8 with SingularityBounceRequest

use of com.hubspot.singularity.api.SingularityBounceRequest in project Singularity by HubSpot.

the class SingularityHealthchecksTest method testSkipHealthchecksDuringBounce.

@Test
public void testSkipHealthchecksDuringBounce() {
    try {
        initRequest();
        initHCDeploy();
        SingularityTask firstTask = startTask(firstDeploy, 1);
        requestResource.bounce(requestId, Optional.of(new SingularityBounceRequest(Optional.absent(), Optional.of(true), Optional.absent(), Optional.absent(), Optional.absent(), Optional.absent())), singularityUser);
        setConfigurationForNoDelay();
        cleaner.drainCleanupQueue();
        resourceOffers();
        List<SingularityTaskId> taskIds = taskManager.getAllTaskIds();
        taskIds.remove(firstTask.getTaskId());
        SingularityTaskId secondTaskId = taskIds.get(0);
        SingularityTask secondTask = taskManager.getTask(secondTaskId).get();
        statusUpdate(secondTask, TaskState.TASK_RUNNING);
        Assert.assertTrue(healthchecker.cancelHealthcheck(firstTask.getTaskId().getId()));
        newTaskChecker.cancelNewTaskCheck(firstTask.getTaskId().getId());
        finishHealthchecks();
        finishNewTaskChecks();
        Assert.assertTrue(!taskManager.getLastHealthcheck(secondTask.getTaskId()).isPresent());
        cleaner.drainCleanupQueue();
        killKilledTasks();
        Assert.assertEquals(0, taskManager.getNumCleanupTasks());
        Assert.assertEquals(1, taskManager.getNumActiveTasks());
    } finally {
        unsetConfigurationForNoDelay();
    }
}
Also used : SingularityTask(com.hubspot.singularity.SingularityTask) SingularityBounceRequest(com.hubspot.singularity.api.SingularityBounceRequest) SingularityTaskId(com.hubspot.singularity.SingularityTaskId) Test(org.junit.Test)

Example 9 with SingularityBounceRequest

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

Example 10 with SingularityBounceRequest

use of com.hubspot.singularity.api.SingularityBounceRequest in project Singularity by HubSpot.

the class RequestResource method scale.

public SingularityRequestParent scale(String requestId, SingularityScaleRequest scaleRequest, SingularityUser user) {
    SingularityRequestWithState oldRequestWithState = fetchRequestWithState(requestId, user);
    SingularityRequest oldRequest = oldRequestWithState.getRequest();
    authorizationHelper.checkForAuthorization(oldRequest, user, SingularityAuthorizationScope.WRITE);
    validator.checkActionEnabled(SingularityAction.SCALE_REQUEST);
    SingularityRequest newRequest = oldRequest.toBuilder().setInstances(scaleRequest.getInstances()).build();
    validator.checkScale(newRequest, Optional.<Integer>absent());
    checkBadRequest(oldRequest.getInstancesSafe() != newRequest.getInstancesSafe(), "Scale request has no affect on the # of instances (%s)", newRequest.getInstancesSafe());
    String scaleMessage = String.format("Scaling from %d -> %d", oldRequest.getInstancesSafe(), newRequest.getInstancesSafe());
    if (scaleRequest.getMessage().isPresent()) {
        scaleMessage = String.format("%s -- %s", scaleRequest.getMessage().get(), scaleMessage);
    } else {
        scaleMessage = String.format("%s", scaleMessage);
    }
    if (scaleRequest.getBounce().or(newRequest.getBounceAfterScale().or(false))) {
        validator.checkActionEnabled(SingularityAction.BOUNCE_REQUEST);
        checkBadRequest(newRequest.isLongRunning(), "Can not bounce a %s request (%s)", newRequest.getRequestType(), newRequest);
        checkConflict(oldRequestWithState.getState() != RequestState.PAUSED, "Request %s is paused. Unable to bounce (it must be manually unpaused first)", newRequest.getId());
        checkConflict(!requestManager.cleanupRequestExists(newRequest.getId(), RequestCleanupType.BOUNCE), "Request %s is already bouncing cannot bounce again", newRequest.getId());
        final boolean isIncrementalBounce = scaleRequest.getIncremental().or(true);
        validator.checkResourcesForBounce(newRequest, isIncrementalBounce);
        validator.checkRequestForPriorityFreeze(newRequest);
        SingularityBounceRequest bounceRequest = new SingularityBounceRequest(Optional.of(isIncrementalBounce), scaleRequest.getSkipHealthchecks(), Optional.<Long>absent(), Optional.of(UUID.randomUUID().toString()), Optional.<String>absent(), Optional.<SingularityShellCommand>absent());
        submitRequest(newRequest, Optional.of(oldRequestWithState), Optional.of(RequestHistoryType.SCALED), scaleRequest.getSkipHealthchecks(), Optional.of(scaleMessage), Optional.of(bounceRequest), user);
    } else {
        submitRequest(newRequest, Optional.of(oldRequestWithState), Optional.of(RequestHistoryType.SCALED), scaleRequest.getSkipHealthchecks(), Optional.of(scaleMessage), Optional.absent(), user);
    }
    if (scaleRequest.getDurationMillis().isPresent()) {
        requestManager.saveExpiringObject(new SingularityExpiringScale(requestId, user.getEmail(), System.currentTimeMillis(), scaleRequest, oldRequest.getInstances(), scaleRequest.getActionId().or(UUID.randomUUID().toString()), scaleRequest.getBounce()));
    } else {
        requestManager.deleteExpiringObject(SingularityExpiringScale.class, requestId);
    }
    if (!scaleRequest.getSkipEmailNotification().isPresent() || !scaleRequest.getSkipEmailNotification().get()) {
        mailer.sendRequestScaledMail(newRequest, Optional.of(scaleRequest), oldRequest.getInstances(), user.getEmail());
    }
    return fillEntireRequest(fetchRequestWithState(requestId, user));
}
Also used : SingularityRequestWithState(com.hubspot.singularity.SingularityRequestWithState) SingularityRequest(com.hubspot.singularity.SingularityRequest) SingularityBounceRequest(com.hubspot.singularity.api.SingularityBounceRequest) SingularityExpiringScale(com.hubspot.singularity.expiring.SingularityExpiringScale)

Aggregations

SingularityBounceRequest (com.hubspot.singularity.api.SingularityBounceRequest)10 Test (org.junit.Test)7 SingularityTaskId (com.hubspot.singularity.SingularityTaskId)4 SingularityTask (com.hubspot.singularity.SingularityTask)3 SingularityTaskHistoryUpdate (com.hubspot.singularity.SingularityTaskHistoryUpdate)3 SingularityRequestCleanup (com.hubspot.singularity.SingularityRequestCleanup)2 SingularityRequestWithState (com.hubspot.singularity.SingularityRequestWithState)2 SingularityShellCommand (com.hubspot.singularity.SingularityShellCommand)2 SingularityTaskCleanup (com.hubspot.singularity.SingularityTaskCleanup)2 SingularityScaleRequest (com.hubspot.singularity.api.SingularityScaleRequest)2 SingularityExpiringBounce (com.hubspot.singularity.expiring.SingularityExpiringBounce)2 ByteString (com.google.protobuf.ByteString)1 SingularityCreateResult (com.hubspot.singularity.SingularityCreateResult)1 SingularityPendingRequest (com.hubspot.singularity.SingularityPendingRequest)1 SingularityRequest (com.hubspot.singularity.SingularityRequest)1 SingularityTaskShellCommandRequestId (com.hubspot.singularity.SingularityTaskShellCommandRequestId)1 SingularityTaskShellCommandUpdate (com.hubspot.singularity.SingularityTaskShellCommandUpdate)1 SingularityExpiringScale (com.hubspot.singularity.expiring.SingularityExpiringScale)1