Search in sources :

Example 26 with SingularityPendingRequest

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

the class RequestHelper method checkReschedule.

private void checkReschedule(SingularityRequest newRequest, Optional<SingularityRequest> maybeOldRequest, Optional<String> user, long timestamp, Optional<Boolean> skipHealthchecks, Optional<String> message, Optional<SingularityBounceRequest> maybeBounceRequest) {
    if (!maybeOldRequest.isPresent()) {
        return;
    }
    if (shouldReschedule(newRequest, maybeOldRequest.get())) {
        Optional<String> maybeDeployId = deployManager.getInUseDeployId(newRequest.getId());
        if (maybeDeployId.isPresent()) {
            if (maybeBounceRequest.isPresent()) {
                Optional<String> actionId = Optional.of(maybeBounceRequest.get().getActionId().orElse(UUID.randomUUID().toString()));
                Optional<Boolean> removeFromLoadBalancer = Optional.empty();
                SingularityCreateResult createResult = requestManager.createCleanupRequest(new SingularityRequestCleanup(user, maybeBounceRequest.get().getIncremental().orElse(true) ? RequestCleanupType.INCREMENTAL_BOUNCE : RequestCleanupType.BOUNCE, System.currentTimeMillis(), Optional.<Boolean>empty(), removeFromLoadBalancer, newRequest.getId(), Optional.of(maybeDeployId.get()), skipHealthchecks, message, actionId, maybeBounceRequest.get().getRunShellCommandBeforeKill()));
                if (createResult != SingularityCreateResult.EXISTED) {
                    requestManager.bounce(newRequest, System.currentTimeMillis(), user, Optional.of("Bouncing due to bounce after scale"));
                    final SingularityBounceRequest validatedBounceRequest = validator.checkBounceRequest(maybeBounceRequest.get());
                    requestManager.saveExpiringObject(new SingularityExpiringBounce(newRequest.getId(), maybeDeployId.get(), user, System.currentTimeMillis(), validatedBounceRequest, actionId.get()));
                } else {
                    requestManager.addToPendingQueue(new SingularityPendingRequest(newRequest.getId(), maybeDeployId.get(), timestamp, user, PendingType.UPDATED_REQUEST, skipHealthchecks, message));
                }
            } else {
                requestManager.addToPendingQueue(new SingularityPendingRequest(newRequest.getId(), maybeDeployId.get(), timestamp, user, PendingType.UPDATED_REQUEST, skipHealthchecks, message));
            }
        }
    }
}
Also used : SingularityRequestCleanup(com.hubspot.singularity.SingularityRequestCleanup) SingularityPendingRequest(com.hubspot.singularity.SingularityPendingRequest) SingularityExpiringBounce(com.hubspot.singularity.expiring.SingularityExpiringBounce) SingularityCreateResult(com.hubspot.singularity.SingularityCreateResult) SingularityBounceRequest(com.hubspot.singularity.api.SingularityBounceRequest)

Example 27 with SingularityPendingRequest

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

the class ZkMigrationTest method testPendingRequestRewriteTest.

@Test
public void testPendingRequestRewriteTest() throws Exception {
    metadataManager.setZkDataVersion("9");
    long now = System.currentTimeMillis();
    SingularityPendingRequest immediateRequest = new SingularityPendingRequest("immediateRequest", "immediateDeploy", now, Optional.empty(), PendingType.IMMEDIATE, Optional.empty(), Optional.empty());
    SingularityPendingRequest newDeploy = new SingularityPendingRequest("newDeployRequest", "newDeploy", now, Optional.empty(), PendingType.NEW_DEPLOY, Optional.empty(), Optional.empty());
    SingularityPendingRequest oneOffRequest = new SingularityPendingRequest("oneOffRequest", "oneOffDeploy", now, Optional.empty(), PendingType.ONEOFF, Optional.empty(), Optional.empty());
    curator.create().creatingParentsIfNeeded().forPath("/requests/pending/immediateRequest-immediateDeploy", objectMapper.writeValueAsBytes(immediateRequest));
    curator.create().creatingParentsIfNeeded().forPath("/requests/pending/newDeployRequest-newDeploy", objectMapper.writeValueAsBytes(newDeploy));
    curator.create().creatingParentsIfNeeded().forPath(String.format("%s%s", "/requests/pending/oneOffRequest-oneOffDeploy", now), objectMapper.writeValueAsBytes(oneOffRequest));
    Assertions.assertThat(requestManager.getPendingRequests().size()).isEqualTo(3);
    System.out.println(curator.getChildren().forPath("/requests/pending"));
    migrationRunner.checkMigrations();
    Assertions.assertThat(requestManager.getPendingRequests().size()).isEqualTo(3);
    System.out.println(curator.getChildren().forPath("/requests/pending"));
    requestManager.deletePendingRequest(newDeploy);
    Assertions.assertThat(requestManager.getPendingRequests()).as("Non-renamed, non-timestamped nodes can be deleted").hasSize(2).extracting(SingularityPendingRequest::toString).doesNotContain(newDeploy.toString()).contains(oneOffRequest.toString(), immediateRequest.toString());
    requestManager.deletePendingRequest(oneOffRequest);
    Assertions.assertThat(requestManager.getPendingRequests()).as("Non-renamed timestamped nodes can be deleted").hasSize(1).extracting(SingularityPendingRequest::toString).doesNotContain(oneOffRequest.toString()).contains(immediateRequest.toString());
    requestManager.deletePendingRequest(immediateRequest);
    Assertions.assertThat(requestManager.getPendingRequests()).as("Renamed nodes can be deleted").hasSize(0);
}
Also used : SingularityPendingRequest(com.hubspot.singularity.SingularityPendingRequest) Test(org.junit.jupiter.api.Test)

Example 28 with SingularityPendingRequest

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

the class ValidatorTest method whenRunNowIfNoRunIdSetItWillGenerateAnId.

@Test
public void whenRunNowIfNoRunIdSetItWillGenerateAnId() {
    String deployID = "deploy";
    Optional<String> userEmail = Optional.empty();
    SingularityRequest request = new SingularityRequestBuilder("request2", RequestType.ON_DEMAND).build();
    Optional<SingularityRunNowRequest> runNowRequest = Optional.of(runNowRequest());
    SingularityPendingRequest pendingRequest = validator.checkRunNowRequest(deployID, userEmail, request, runNowRequest, 0, 0);
    Assertions.assertTrue(pendingRequest.getRunId().isPresent());
}
Also used : SingularityRunNowRequest(com.hubspot.singularity.api.SingularityRunNowRequest) SingularityRequestBuilder(com.hubspot.singularity.SingularityRequestBuilder) SingularityPendingRequest(com.hubspot.singularity.SingularityPendingRequest) SingularityRequest(com.hubspot.singularity.SingularityRequest) Test(org.junit.jupiter.api.Test)

Example 29 with SingularityPendingRequest

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

the class SingularityUsagePoller method shuffleTasksOnOverloadedHosts.

private void shuffleTasksOnOverloadedHosts(Map<SingularitySlaveUsage, List<TaskIdWithUsage>> overLoadedHosts) {
    List<SingularityTaskCleanup> shuffleCleanups = taskManager.getCleanupTasks().stream().filter((taskCleanup) -> taskCleanup.getCleanupType() == TaskCleanupType.REBALANCE_CPU_USAGE).collect(Collectors.toList());
    long currentShuffleCleanupsTotal = shuffleCleanups.size();
    Set<String> requestsWithShuffledTasks = shuffleCleanups.stream().map((taskCleanup) -> taskCleanup.getTaskId().getRequestId()).collect(Collectors.toSet());
    List<SingularitySlaveUsage> overLoadedSlavesByUsage = overLoadedHosts.keySet().stream().sorted((usage1, usage2) -> Double.compare(getSystemLoadForShuffle(usage2), getSystemLoadForShuffle(usage1))).collect(Collectors.toList());
    for (SingularitySlaveUsage overloadedSlave : overLoadedSlavesByUsage) {
        if (currentShuffleCleanupsTotal >= configuration.getMaxTasksToShuffleTotal()) {
            LOG.debug("Not shuffling any more tasks (totalShuffleCleanups: {})", currentShuffleCleanupsTotal);
            break;
        }
        int shuffledTasksOnSlave = 0;
        List<TaskIdWithUsage> possibleTasksToShuffle = overLoadedHosts.get(overloadedSlave);
        possibleTasksToShuffle.sort((u1, u2) -> Double.compare(u2.getUsage().getCpusUsed() / u2.getRequestedResources().getCpus(), u1.getUsage().getCpusUsed() / u1.getRequestedResources().getCpus()));
        double systemLoad = getSystemLoadForShuffle(overloadedSlave);
        double cpuOverage = systemLoad - overloadedSlave.getSystemCpusTotal();
        for (TaskIdWithUsage taskIdWithUsage : possibleTasksToShuffle) {
            if (requestsWithShuffledTasks.contains(taskIdWithUsage.getTaskId().getRequestId())) {
                LOG.debug("Request {} already has a shuffling task, skipping", taskIdWithUsage.getTaskId().getRequestId());
                continue;
            }
            if (cpuOverage <= 0 || shuffledTasksOnSlave > configuration.getMaxTasksToShufflePerHost() || currentShuffleCleanupsTotal >= configuration.getMaxTasksToShuffleTotal()) {
                LOG.debug("Not shuffling any more tasks (overage: {}, shuffledOnHost: {}, totalShuffleCleanups: {})", cpuOverage, shuffledTasksOnSlave, currentShuffleCleanupsTotal);
                break;
            }
            LOG.debug("Cleaning up task {} to free up cpu on overloaded host (remaining cpu overage: {})", taskIdWithUsage.getTaskId(), cpuOverage);
            Optional<String> message = Optional.of(String.format("Load on slave is %s / %s, shuffling task to less busy host", systemLoad, overloadedSlave.getSystemCpusTotal()));
            taskManager.createTaskCleanup(new SingularityTaskCleanup(Optional.absent(), TaskCleanupType.REBALANCE_CPU_USAGE, System.currentTimeMillis(), taskIdWithUsage.getTaskId(), message, Optional.of(UUID.randomUUID().toString()), Optional.absent(), Optional.absent()));
            requestManager.addToPendingQueue(new SingularityPendingRequest(taskIdWithUsage.getTaskId().getRequestId(), taskIdWithUsage.getTaskId().getDeployId(), System.currentTimeMillis(), Optional.absent(), PendingType.TASK_BOUNCE, Optional.absent(), Optional.absent(), Optional.absent(), message, Optional.of(UUID.randomUUID().toString())));
            cpuOverage -= taskIdWithUsage.getUsage().getCpusUsed();
            shuffledTasksOnSlave++;
            currentShuffleCleanupsTotal++;
            requestsWithShuffledTasks.add(taskIdWithUsage.getTaskId().getRequestId());
        }
    }
}
Also used : SingularityTask(com.hubspot.singularity.SingularityTask) DeployManager(com.hubspot.singularity.data.DeployManager) ExtendedTaskState(com.hubspot.singularity.ExtendedTaskState) MesosTaskMonitorObject(com.hubspot.mesos.json.MesosTaskMonitorObject) Inject(com.google.inject.Inject) RequestManager(com.hubspot.singularity.data.RequestManager) LoggerFactory(org.slf4j.LoggerFactory) InvalidSingularityTaskIdException(com.hubspot.singularity.InvalidSingularityTaskIdException) SingularityDeployStatistics(com.hubspot.singularity.SingularityDeployStatistics) Optional(com.google.common.base.Optional) Map(java.util.Map) SingularitySlaveUsage(com.hubspot.singularity.SingularitySlaveUsage) Resources(com.hubspot.mesos.Resources) TaskManager(com.hubspot.singularity.data.TaskManager) SingularityTaskId(com.hubspot.singularity.SingularityTaskId) SingularityTaskCurrentUsage(com.hubspot.singularity.SingularityTaskCurrentUsage) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) CompletableFutures(com.hubspot.singularity.async.CompletableFutures) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) PendingType(com.hubspot.singularity.SingularityPendingRequest.PendingType) List(java.util.List) SingularityClusterUtilization(com.hubspot.singularity.SingularityClusterUtilization) RequestUtilization(com.hubspot.singularity.RequestUtilization) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) SingularityRequestWithState(com.hubspot.singularity.SingularityRequestWithState) SingularityTaskHistoryUpdate(com.hubspot.singularity.SingularityTaskHistoryUpdate) AtomicDouble(com.google.common.util.concurrent.AtomicDouble) SingularityPendingRequest(com.hubspot.singularity.SingularityPendingRequest) MesosSlaveMetricsSnapshotObject(com.hubspot.mesos.json.MesosSlaveMetricsSnapshotObject) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) SingularitySlave(com.hubspot.singularity.SingularitySlave) SingularityDeleteResult(com.hubspot.singularity.SingularityDeleteResult) SingularityTaskUsage(com.hubspot.singularity.SingularityTaskUsage) SingularityExceptionNotifier(com.hubspot.singularity.sentry.SingularityExceptionNotifier) ExecutorService(java.util.concurrent.ExecutorService) SingularityConfiguration(com.hubspot.singularity.config.SingularityConfiguration) ResourceUsageType(com.hubspot.singularity.SingularitySlaveUsage.ResourceUsageType) SingularityTaskCleanup(com.hubspot.singularity.SingularityTaskCleanup) Logger(org.slf4j.Logger) MesosClient(com.hubspot.mesos.client.MesosClient) SingularityDeploy(com.hubspot.singularity.SingularityDeploy) AsyncSemaphore(com.hubspot.singularity.async.AsyncSemaphore) TimeUnit(java.util.concurrent.TimeUnit) UsageManager(com.hubspot.singularity.data.UsageManager) AtomicLong(java.util.concurrent.atomic.AtomicLong) TaskCleanupType(com.hubspot.singularity.TaskCleanupType) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Comparator(java.util.Comparator) SingularitySlaveUsage(com.hubspot.singularity.SingularitySlaveUsage) SingularityPendingRequest(com.hubspot.singularity.SingularityPendingRequest) SingularityTaskCleanup(com.hubspot.singularity.SingularityTaskCleanup)

Example 30 with SingularityPendingRequest

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

the class SingularityDeployChecker method checkCanMoveToNextDeployStep.

private SingularityDeployResult checkCanMoveToNextDeployStep(SingularityRequest request, Optional<SingularityDeploy> deploy, SingularityPendingDeploy pendingDeploy, Optional<SingularityUpdatePendingDeployRequest> updatePendingDeployRequest) {
    SingularityDeployProgress deployProgress = pendingDeploy.getDeployProgress().get();
    if (canMoveToNextStep(deployProgress) || updatePendingDeployRequest.isPresent()) {
        SingularityDeployProgress newProgress = deployProgress.withNewTargetInstances(getNewTargetInstances(deployProgress, request, updatePendingDeployRequest));
        updatePendingDeploy(pendingDeploy, pendingDeploy.getLastLoadBalancerUpdate(), DeployState.WAITING, Optional.of(newProgress));
        requestManager.addToPendingQueue(new SingularityPendingRequest(request.getId(), pendingDeploy.getDeployMarker().getDeployId(), System.currentTimeMillis(), pendingDeploy.getDeployMarker().getUser(), PendingType.NEXT_DEPLOY_STEP, deploy.isPresent() ? deploy.get().getSkipHealthchecksOnDeploy() : Optional.<Boolean>absent(), pendingDeploy.getDeployMarker().getMessage()));
    }
    return new SingularityDeployResult(DeployState.WAITING);
}
Also used : SingularityPendingRequest(com.hubspot.singularity.SingularityPendingRequest) SingularityDeployResult(com.hubspot.singularity.SingularityDeployResult) SingularityDeployProgress(com.hubspot.singularity.SingularityDeployProgress)

Aggregations

SingularityPendingRequest (com.hubspot.singularity.SingularityPendingRequest)53 Test (org.junit.jupiter.api.Test)18 SingularityRequest (com.hubspot.singularity.SingularityRequest)12 SingularityRequestWithState (com.hubspot.singularity.SingularityRequestWithState)11 SingularityTask (com.hubspot.singularity.SingularityTask)9 SingularityTaskId (com.hubspot.singularity.SingularityTaskId)9 SingularityCreateResult (com.hubspot.singularity.SingularityCreateResult)8 SingularityTaskCleanup (com.hubspot.singularity.SingularityTaskCleanup)8 SingularityRunNowRequest (com.hubspot.singularity.api.SingularityRunNowRequest)8 ArrayList (java.util.ArrayList)8 List (java.util.List)8 SingularityDeploy (com.hubspot.singularity.SingularityDeploy)7 SingularityPendingDeploy (com.hubspot.singularity.SingularityPendingDeploy)7 SingularityPendingTask (com.hubspot.singularity.SingularityPendingTask)7 SingularityRequestDeployState (com.hubspot.singularity.SingularityRequestDeployState)7 Resources (com.hubspot.mesos.Resources)6 SingularityDeployKey (com.hubspot.singularity.SingularityDeployKey)6 SingularityDeployProgress (com.hubspot.singularity.SingularityDeployProgress)6 PendingType (com.hubspot.singularity.SingularityPendingRequest.PendingType)6 SingularityRequestBuilder (com.hubspot.singularity.SingularityRequestBuilder)6