Search in sources :

Example 1 with SingularityUpdatePendingDeployRequest

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

the class SingularityDeployChecker method checkDeploy.

private void checkDeploy(final SingularityPendingDeploy pendingDeploy, final List<SingularityDeployMarker> cancelDeploys, final Map<SingularityPendingDeploy, SingularityDeployKey> pendingDeployToKey, final Map<SingularityDeployKey, SingularityDeploy> deployKeyToDeploy, List<SingularityUpdatePendingDeployRequest> updateRequests) {
    final SingularityDeployKey deployKey = pendingDeployToKey.get(pendingDeploy);
    final Optional<SingularityDeploy> deploy = Optional.fromNullable(deployKeyToDeploy.get(deployKey));
    Optional<SingularityRequestWithState> maybeRequestWithState = requestManager.getRequest(pendingDeploy.getDeployMarker().getRequestId());
    if (!(maybeRequestWithState.isPresent() && maybeRequestWithState.get().getState() == RequestState.FINISHED) && !(configuration.isAllowDeployOfPausedRequests() && maybeRequestWithState.isPresent() && maybeRequestWithState.get().getState() == RequestState.PAUSED) && !SingularityRequestWithState.isActive(maybeRequestWithState)) {
        LOG.warn("Deploy {} request was {}, removing deploy", pendingDeploy, SingularityRequestWithState.getRequestState(maybeRequestWithState));
        if (shouldCancelLoadBalancer(pendingDeploy)) {
            cancelLoadBalancer(pendingDeploy, SingularityDeployFailure.deployRemoved());
        }
        failPendingDeployDueToState(pendingDeploy, maybeRequestWithState, deploy);
        return;
    }
    final SingularityDeployMarker pendingDeployMarker = pendingDeploy.getDeployMarker();
    final Optional<SingularityDeployMarker> cancelRequest = findCancel(cancelDeploys, pendingDeployMarker);
    final Optional<SingularityUpdatePendingDeployRequest> updatePendingDeployRequest = findUpdateRequest(updateRequests, pendingDeploy);
    final SingularityRequestWithState requestWithState = maybeRequestWithState.get();
    final SingularityRequest request = pendingDeploy.getUpdatedRequest().or(requestWithState.getRequest());
    final List<SingularityTaskId> requestTasks = taskManager.getTaskIdsForRequest(request.getId());
    final List<SingularityTaskId> activeTasks = taskManager.filterActiveTaskIds(requestTasks);
    final List<SingularityTaskId> inactiveDeployMatchingTasks = new ArrayList<>(requestTasks.size());
    for (SingularityTaskId taskId : requestTasks) {
        if (taskId.getDeployId().equals(pendingDeployMarker.getDeployId()) && !activeTasks.contains(taskId)) {
            inactiveDeployMatchingTasks.add(taskId);
        }
    }
    final List<SingularityTaskId> deployMatchingTasks = new ArrayList<>(activeTasks.size());
    final List<SingularityTaskId> allOtherMatchingTasks = new ArrayList<>(activeTasks.size());
    for (SingularityTaskId taskId : activeTasks) {
        if (taskId.getDeployId().equals(pendingDeployMarker.getDeployId())) {
            deployMatchingTasks.add(taskId);
        } else {
            allOtherMatchingTasks.add(taskId);
        }
    }
    SingularityDeployResult deployResult = getDeployResult(request, requestWithState.getState(), cancelRequest, pendingDeploy, updatePendingDeployRequest, deploy, deployMatchingTasks, allOtherMatchingTasks, inactiveDeployMatchingTasks);
    LOG.info("Deploy {} had result {} after {}", pendingDeployMarker, deployResult, JavaUtils.durationFromMillis(System.currentTimeMillis() - pendingDeployMarker.getTimestamp()));
    if (deployResult.getDeployState() == DeployState.SUCCEEDED) {
        if (saveNewDeployState(pendingDeployMarker, Optional.of(pendingDeployMarker))) {
            if (!(request.getRequestType() == RequestType.RUN_ONCE)) {
                deleteObsoletePendingTasks(pendingDeploy);
            }
            finishDeploy(requestWithState, deploy, pendingDeploy, allOtherMatchingTasks, deployResult);
            return;
        } else {
            LOG.warn("Failing deploy {} because it failed to save deploy state", pendingDeployMarker);
            deployResult = new SingularityDeployResult(DeployState.FAILED_INTERNAL_STATE, Optional.of(String.format("Deploy had state %s but failed to persist it correctly", deployResult.getDeployState())), deployResult.getLbUpdate(), SingularityDeployFailure.failedToSave(), deployResult.getTimestamp());
        }
    } else if (!deployResult.getDeployState().isDeployFinished()) {
        return;
    }
    // success case is handled, handle failure cases:
    saveNewDeployState(pendingDeployMarker, Optional.<SingularityDeployMarker>absent());
    finishDeploy(requestWithState, deploy, pendingDeploy, deployMatchingTasks, deployResult);
}
Also used : SingularityDeployMarker(com.hubspot.singularity.SingularityDeployMarker) SingularityDeployResult(com.hubspot.singularity.SingularityDeployResult) SingularityRequest(com.hubspot.singularity.SingularityRequest) ArrayList(java.util.ArrayList) SingularityUpdatePendingDeployRequest(com.hubspot.singularity.SingularityUpdatePendingDeployRequest) SingularityDeploy(com.hubspot.singularity.SingularityDeploy) SingularityDeployKey(com.hubspot.singularity.SingularityDeployKey) SingularityRequestWithState(com.hubspot.singularity.SingularityRequestWithState) SingularityTaskId(com.hubspot.singularity.SingularityTaskId)

Example 2 with SingularityUpdatePendingDeployRequest

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

the class SingularityClient method updateIncrementalDeployInstanceCount.

public SingularityRequestParent updateIncrementalDeployInstanceCount(SingularityUpdatePendingDeployRequest updateRequest) {
    final Function<String, String> requestUri = (host) -> String.format(UPDATE_DEPLOY_FORMAT, getApiBase(host));
    HttpResponse response = post(requestUri, String.format("update deploy %s", new SingularityDeployKey(updateRequest.getRequestId(), updateRequest.getDeployId())), Optional.of(updateRequest));
    return getAndLogRequestAndDeployStatus(response.getAs(SingularityRequestParent.class));
}
Also used : SingularityTask(com.hubspot.singularity.SingularityTask) Arrays(java.util.Arrays) Method(com.hubspot.horizon.HttpRequest.Method) ExtendedTaskState(com.hubspot.singularity.ExtendedTaskState) SingularityTaskCleanupResult(com.hubspot.singularity.SingularityTaskCleanupResult) SingularityPaginatedResponse(com.hubspot.singularity.SingularityPaginatedResponse) SingularityWebhook(com.hubspot.singularity.SingularityWebhook) Provider(javax.inject.Provider) Retryer(com.github.rholder.retry.Retryer) RetryerBuilder(com.github.rholder.retry.RetryerBuilder) SingularityDeployRequest(com.hubspot.singularity.api.SingularityDeployRequest) RetryException(com.github.rholder.retry.RetryException) Inject(com.google.inject.Inject) SingularityRequestCleanup(com.hubspot.singularity.SingularityRequestCleanup) SingularityState(com.hubspot.singularity.SingularityState) LoggerFactory(org.slf4j.LoggerFactory) Random(java.util.Random) SingularityTaskReconciliationStatistics(com.hubspot.singularity.SingularityTaskReconciliationStatistics) SingularityRequestGroup(com.hubspot.singularity.SingularityRequestGroup) SingularityDisasterType(com.hubspot.singularity.SingularityDisasterType) SingularityBounceRequest(com.hubspot.singularity.api.SingularityBounceRequest) SingularityPauseRequest(com.hubspot.singularity.api.SingularityPauseRequest) SingularityUpdateGroupsRequest(com.hubspot.singularity.api.SingularityUpdateGroupsRequest) Optional(com.google.common.base.Optional) Map(java.util.Map) HttpRequest(com.hubspot.horizon.HttpRequest) HttpClient(com.hubspot.horizon.HttpClient) TypeReference(com.fasterxml.jackson.core.type.TypeReference) SingularityPriorityFreeze(com.hubspot.singularity.api.SingularityPriorityFreeze) SingularityDisabledAction(com.hubspot.singularity.SingularityDisabledAction) SingularityPendingTaskId(com.hubspot.singularity.SingularityPendingTaskId) SingularityTaskId(com.hubspot.singularity.SingularityTaskId) SingularityUnpauseRequest(com.hubspot.singularity.api.SingularityUnpauseRequest) SingularityMachineChangeRequest(com.hubspot.singularity.api.SingularityMachineChangeRequest) SingularityDisastersData(com.hubspot.singularity.SingularityDisastersData) ImmutableMap(com.google.common.collect.ImmutableMap) SingularityPriorityFreezeParent(com.hubspot.singularity.SingularityPriorityFreezeParent) Predicate(java.util.function.Predicate) Collection(java.util.Collection) SingularityUpdatePendingDeployRequest(com.hubspot.singularity.SingularityUpdatePendingDeployRequest) List(java.util.List) SingularityTaskShellCommandRequest(com.hubspot.singularity.SingularityTaskShellCommandRequest) SingularityTaskShellCommandUpdate(com.hubspot.singularity.SingularityTaskShellCommandUpdate) StopStrategies(com.github.rholder.retry.StopStrategies) SingularityClusterUtilization(com.hubspot.singularity.SingularityClusterUtilization) OrderDirection(com.hubspot.singularity.OrderDirection) SingularityAuthorizationScope(com.hubspot.singularity.SingularityAuthorizationScope) Entry(java.util.Map.Entry) SingularitySandbox(com.hubspot.singularity.SingularitySandbox) SingularityS3Log(com.hubspot.singularity.SingularityS3Log) SingularityDisabledActionRequest(com.hubspot.singularity.api.SingularityDisabledActionRequest) SingularityCreateResult(com.hubspot.singularity.SingularityCreateResult) SingularityRequest(com.hubspot.singularity.SingularityRequest) SingularityTaskHistoryUpdate(com.hubspot.singularity.SingularityTaskHistoryUpdate) SingularityShellCommand(com.hubspot.singularity.SingularityShellCommand) SingularityRequestHistory(com.hubspot.singularity.SingularityRequestHistory) SingularityTaskIdHistory(com.hubspot.singularity.SingularityTaskIdHistory) SingularityPendingRequest(com.hubspot.singularity.SingularityPendingRequest) SingularityTaskShellCommandHistory(com.hubspot.singularity.SingularityTaskShellCommandHistory) HashMap(java.util.HashMap) SingularityClientCredentials(com.hubspot.singularity.SingularityClientCredentials) SingularityDeployUpdate(com.hubspot.singularity.SingularityDeployUpdate) Function(java.util.function.Function) SingularityTaskIdsByStatus(com.hubspot.singularity.SingularityTaskIdsByStatus) SingularityTaskState(com.hubspot.singularity.SingularityTaskState) SingularityExitCooldownRequest(com.hubspot.singularity.api.SingularityExitCooldownRequest) ArrayList(java.util.ArrayList) Builder(com.google.common.collect.ImmutableMap.Builder) ImmutableList(com.google.common.collect.ImmutableList) SingularitySlave(com.hubspot.singularity.SingularitySlave) SingularityDeleteResult(com.hubspot.singularity.SingularityDeleteResult) SingularityDeployKey(com.hubspot.singularity.SingularityDeployKey) RetryStrategy(com.hubspot.horizon.RetryStrategy) HttpResponse(com.hubspot.horizon.HttpResponse) SingularityPendingRequestParent(com.hubspot.singularity.SingularityPendingRequestParent) WaitStrategies(com.github.rholder.retry.WaitStrategies) SingularityAction(com.hubspot.singularity.SingularityAction) SingularityDeployHistory(com.hubspot.singularity.SingularityDeployHistory) Logger(org.slf4j.Logger) SingularityTaskHistory(com.hubspot.singularity.SingularityTaskHistory) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) SingularityDeploy(com.hubspot.singularity.SingularityDeploy) SingularityRack(com.hubspot.singularity.SingularityRack) ExecutionException(java.util.concurrent.ExecutionException) SingularityKillTaskRequest(com.hubspot.singularity.api.SingularityKillTaskRequest) SingularityTaskRequest(com.hubspot.singularity.SingularityTaskRequest) MachineState(com.hubspot.singularity.MachineState) SingularityRequestParent(com.hubspot.singularity.SingularityRequestParent) SingularityDeleteRequestRequest(com.hubspot.singularity.api.SingularityDeleteRequestRequest) SingularityRunNowRequest(com.hubspot.singularity.api.SingularityRunNowRequest) Named(com.google.inject.name.Named) SingularityScaleRequest(com.hubspot.singularity.api.SingularityScaleRequest) MesosFileChunkObject(com.hubspot.mesos.json.MesosFileChunkObject) Collections(java.util.Collections) SingularityDeployKey(com.hubspot.singularity.SingularityDeployKey) HttpResponse(com.hubspot.horizon.HttpResponse) SingularityRequestParent(com.hubspot.singularity.SingularityRequestParent)

Example 3 with SingularityUpdatePendingDeployRequest

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

the class SingularityDeploysTest method testDeployWithManualStep.

@Test
public void testDeployWithManualStep() {
    initRequest();
    SingularityRequest request = requestResource.getRequest(requestId, singularityUser).getRequest();
    requestResource.postRequest(request.toBuilder().setInstances(Optional.of(2)).build(), singularityUser);
    initFirstDeploy();
    SingularityTask firstTask = launchTask(request, firstDeploy, 1, TaskState.TASK_RUNNING);
    SingularityTask secondTask = launchTask(request, firstDeploy, 2, TaskState.TASK_RUNNING);
    deploy(secondDeployId, Optional.<Boolean>absent(), Optional.of(1), Optional.of(false), false);
    deployChecker.checkDeploys();
    scheduler.drainPendingQueue();
    Assert.assertEquals(1, taskManager.getPendingTaskIds().size());
    resourceOffers();
    Assert.assertEquals(1, taskManager.getActiveTaskIdsForDeploy(requestId, secondDeployId).size());
    SingularityTaskId firstNewTaskId = taskManager.getActiveTaskIdsForDeploy(requestId, secondDeployId).get(0);
    statusUpdate(taskManager.getTask(firstNewTaskId).get(), TaskState.TASK_RUNNING);
    deployChecker.checkDeploys();
    Assert.assertEquals(1, taskManager.getCleanupTaskIds().size());
    Assert.assertTrue(taskManager.getCleanupTaskIds().contains(firstTask.getTaskId()));
    SingularityDeployProgress deployProgressStepOne = deployManager.getPendingDeploys().get(0).getDeployProgress().get();
    Assert.assertTrue(deployProgressStepOne.isStepComplete());
    Assert.assertEquals(1, deployProgressStepOne.getTargetActiveInstances());
    cleaner.drainCleanupQueue();
    statusUpdate(firstTask, TaskState.TASK_KILLED);
    deployChecker.checkDeploys();
    // Deploy should not have moved to next step even though instances are launched
    deployProgressStepOne = deployManager.getPendingDeploys().get(0).getDeployProgress().get();
    Assert.assertTrue(deployProgressStepOne.isStepComplete());
    Assert.assertEquals(1, deployProgressStepOne.getTargetActiveInstances());
    // Add the 'ok' to move to the next step
    deployResource.updatePendingDeploy(singularityUser, new SingularityUpdatePendingDeployRequest(requestId, secondDeployId, 2));
    deployChecker.checkDeploys();
    SingularityDeployProgress deployProgressStepTwo = deployManager.getPendingDeploys().get(0).getDeployProgress().get();
    Assert.assertFalse(deployProgressStepTwo.isStepComplete());
    Assert.assertEquals(2, deployProgressStepTwo.getTargetActiveInstances());
    scheduler.drainPendingQueue();
    Assert.assertEquals(1, taskManager.getPendingTaskIds().size());
    resourceOffers();
    Assert.assertEquals(2, taskManager.getActiveTaskIdsForDeploy(requestId, secondDeployId).size());
    for (SingularityTaskId taskId : taskManager.getActiveTaskIdsForDeploy(requestId, secondDeployId)) {
        statusUpdate(taskManager.getTask(taskId).get(), TaskState.TASK_RUNNING);
    }
    deployChecker.checkDeploys();
    Assert.assertEquals(2, taskManager.getActiveTaskIdsForDeploy(requestId, secondDeployId).size());
    Assert.assertEquals(DeployState.SUCCEEDED, deployManager.getDeployResult(requestId, secondDeployId).get().getDeployState());
}
Also used : SingularityTask(com.hubspot.singularity.SingularityTask) SingularityRequest(com.hubspot.singularity.SingularityRequest) SingularityUpdatePendingDeployRequest(com.hubspot.singularity.SingularityUpdatePendingDeployRequest) SingularityDeployProgress(com.hubspot.singularity.SingularityDeployProgress) SingularityTaskId(com.hubspot.singularity.SingularityTaskId) Test(org.junit.Test)

Aggregations

SingularityRequest (com.hubspot.singularity.SingularityRequest)3 SingularityTaskId (com.hubspot.singularity.SingularityTaskId)3 SingularityUpdatePendingDeployRequest (com.hubspot.singularity.SingularityUpdatePendingDeployRequest)3 SingularityDeploy (com.hubspot.singularity.SingularityDeploy)2 SingularityDeployKey (com.hubspot.singularity.SingularityDeployKey)2 SingularityTask (com.hubspot.singularity.SingularityTask)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 RetryException (com.github.rholder.retry.RetryException)1 Retryer (com.github.rholder.retry.Retryer)1 RetryerBuilder (com.github.rholder.retry.RetryerBuilder)1 StopStrategies (com.github.rholder.retry.StopStrategies)1 WaitStrategies (com.github.rholder.retry.WaitStrategies)1 Optional (com.google.common.base.Optional)1 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Builder (com.google.common.collect.ImmutableMap.Builder)1 Inject (com.google.inject.Inject)1 Named (com.google.inject.name.Named)1 HttpClient (com.hubspot.horizon.HttpClient)1