Search in sources :

Example 16 with SingularityPendingDeploy

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

the class SingularityDeployChecker method enqueueLbRevertToActiveTasks.

private SingularityDeployResult enqueueLbRevertToActiveTasks(SingularityRequest request, SingularityPendingDeploy pendingDeploy, SingularityDeployProgress updatedProgress, Collection<SingularityTaskId> deployActiveTasks, Collection<SingularityTaskId> otherActiveTasks) {
    Optional<String> activeDeployIdToRevertTo = deployManager.getActiveDeployId(request.getId());
    if (!activeDeployIdToRevertTo.isPresent()) {
        return new SingularityDeployResult(DeployState.FAILED_INTERNAL_STATE, "No active deploy id  found to revert to");
    }
    Optional<SingularityDeploy> activeDeployToRevertTo = deployManager.getDeploy(request.getId(), activeDeployIdToRevertTo.get());
    if (!activeDeployToRevertTo.isPresent()) {
        return new SingularityDeployResult(DeployState.FAILED_INTERNAL_STATE, "No active deploy found to revert to");
    }
    SingularityDeploy toRevertTo = activeDeployToRevertTo.get();
    Set<SingularityTaskId> toAddBack = otherActiveTasks.stream().filter(t -> t.getDeployId().equals(toRevertTo.getId())).collect(Collectors.toSet());
    LOG.info("Attempting to roll back load balancer to previous active tasks");
    // this to make it as if the task was a net-new add to the LB, making cleanup act as before
    for (SingularityTaskId toAddId : toAddBack) {
        taskManager.clearLoadBalancerHistory(toAddId);
    }
    Map<SingularityTaskId, SingularityTask> tasks = taskManager.getTasks(Iterables.concat(deployActiveTasks, toAddBack));
    final LoadBalancerRequestId lbRequestId = SingularityDeployCheckHelper.getRequestIdForRevert(request.getId(), toRevertTo.getId());
    SingularityLoadBalancerUpdate enqueueResult = lbClient.enqueue(lbRequestId, request, toRevertTo, SingularityDeployCheckHelper.getTasks(toAddBack, tasks), SingularityDeployCheckHelper.getTasks(deployActiveTasks, tasks));
    SingularityDeployProgress deployProgress = updatedProgress.withPendingLbUpdate(enqueueResult, toAddBack, deployActiveTasks, true);
    updatePendingDeploy(pendingDeploy, DeployState.WAITING, deployProgress);
    return new SingularityDeployResult(DeployState.WAITING);
}
Also used : SingularityTask(com.hubspot.singularity.SingularityTask) DeployManager(com.hubspot.singularity.data.DeployManager) SingularityLoadBalancerUpdate(com.hubspot.singularity.SingularityLoadBalancerUpdate) DeployAcceptanceResult(com.hubspot.singularity.DeployAcceptanceResult) Inject(com.google.inject.Inject) RequestManager(com.hubspot.singularity.data.RequestManager) LoggerFactory(org.slf4j.LoggerFactory) SingularityExpiringScale(com.hubspot.singularity.expiring.SingularityExpiringScale) DeployState(com.hubspot.singularity.DeployState) RequestHistoryType(com.hubspot.singularity.SingularityRequestHistory.RequestHistoryType) Map(java.util.Map) TaskManager(com.hubspot.singularity.data.TaskManager) SingularityPendingTaskId(com.hubspot.singularity.SingularityPendingTaskId) SingularityTaskId(com.hubspot.singularity.SingularityTaskId) Collection(java.util.Collection) CompletableFutures(com.hubspot.singularity.async.CompletableFutures) Set(java.util.Set) SingularityUpdatePendingDeployRequest(com.hubspot.singularity.SingularityUpdatePendingDeployRequest) SingularityExpiringPause(com.hubspot.singularity.expiring.SingularityExpiringPause) Collectors(java.util.stream.Collectors) RequestState(com.hubspot.singularity.RequestState) SingularityDeployResult(com.hubspot.singularity.SingularityDeployResult) PendingType(com.hubspot.singularity.SingularityPendingRequest.PendingType) DeployHealth(com.hubspot.singularity.scheduler.SingularityDeployHealthHelper.DeployHealth) JavaUtils(com.hubspot.mesos.JavaUtils) List(java.util.List) LoadBalancerClient(com.hubspot.singularity.hooks.LoadBalancerClient) UsageManager(com.hubspot.singularity.data.usage.UsageManager) DeployProgressLbUpdateHolder(com.hubspot.singularity.DeployProgressLbUpdateHolder) SingularitySchedulerLock(com.hubspot.singularity.mesos.SingularitySchedulerLock) Optional(java.util.Optional) SingularityRequestWithState(com.hubspot.singularity.SingularityRequestWithState) SingularityRequest(com.hubspot.singularity.SingularityRequest) Iterables(com.google.common.collect.Iterables) SingularityPendingTask(com.hubspot.singularity.SingularityPendingTask) SingularityPendingRequest(com.hubspot.singularity.SingularityPendingRequest) CompletableFuture(java.util.concurrent.CompletableFuture) Singleton(javax.inject.Singleton) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) LoadBalancerRequestId(com.hubspot.singularity.LoadBalancerRequestType.LoadBalancerRequestId) SingularityDeployKey(com.hubspot.singularity.SingularityDeployKey) LoadBalancerRequestState(com.hubspot.singularity.LoadBalancerRequestState) RequestType(com.hubspot.singularity.RequestType) ExecutorService(java.util.concurrent.ExecutorService) SingularityConfiguration(com.hubspot.singularity.config.SingularityConfiguration) SingularityTaskCleanup(com.hubspot.singularity.SingularityTaskCleanup) Logger(org.slf4j.Logger) SingularityDeploy(com.hubspot.singularity.SingularityDeploy) SingularityDeployFailure(com.hubspot.singularity.SingularityDeployFailure) SingularityPendingDeploy(com.hubspot.singularity.SingularityPendingDeploy) SingularityDeployMarker(com.hubspot.singularity.SingularityDeployMarker) SingularityRequestDeployState(com.hubspot.singularity.SingularityRequestDeployState) LoadBalancerRequestType(com.hubspot.singularity.LoadBalancerRequestType) SingularityManagedThreadPoolFactory(com.hubspot.singularity.SingularityManagedThreadPoolFactory) SingularityRunNowRequest(com.hubspot.singularity.api.SingularityRunNowRequest) SingularityPendingRequestBuilder(com.hubspot.singularity.SingularityPendingRequestBuilder) Collections(java.util.Collections) SingularityDeployProgress(com.hubspot.singularity.SingularityDeployProgress) SingularityLoadBalancerUpdate(com.hubspot.singularity.SingularityLoadBalancerUpdate) SingularityDeployResult(com.hubspot.singularity.SingularityDeployResult) LoadBalancerRequestId(com.hubspot.singularity.LoadBalancerRequestType.LoadBalancerRequestId) SingularityDeployProgress(com.hubspot.singularity.SingularityDeployProgress) SingularityDeploy(com.hubspot.singularity.SingularityDeploy) SingularityTask(com.hubspot.singularity.SingularityTask) SingularityTaskId(com.hubspot.singularity.SingularityTaskId)

Example 17 with SingularityPendingDeploy

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

the class StateManager method generateState.

public SingularityState generateState(boolean includeRequestIds) {
    final int launchingTasks = taskManager.getNumLaunchingTasks();
    final int activeTasks = taskManager.getNumActiveTasks() - launchingTasks;
    final int scheduledTasks = taskManager.getNumScheduledTasks();
    final int cleaningTasks = taskManager.getNumCleanupTasks();
    final int lbCleanupTasks = taskManager.getNumLbCleanupTasks();
    final int lbCleanupRequests = requestManager.getNumLbCleanupRequests();
    final SingularityScheduledTasksInfo scheduledTasksInfo = getScheduledTasksInfo();
    final List<String> overProvisionedRequestIds = new ArrayList<>();
    final Set<String> possiblyUnderProvisionedRequestIds = new HashSet<>();
    final List<SingularityRequestWithState> requests = requestManager.getRequests();
    final Map<String, Long> numInstances = getNumTasks(requests);
    int numActiveRequests = 0;
    int numPausedRequests = 0;
    int cooldownRequests = 0;
    int numFinishedRequests = 0;
    for (SingularityRequestWithState requestWithState : requests) {
        switch(requestWithState.getState()) {
            case DEPLOYING_TO_UNPAUSE:
            case ACTIVE:
                numActiveRequests++;
                break;
            case FINISHED:
                numFinishedRequests++;
                break;
            case PAUSED:
                numPausedRequests++;
                break;
            case SYSTEM_COOLDOWN:
                cooldownRequests++;
                break;
            case DELETED:
                break;
        }
        updatePossiblyUnderProvisionedAndOverProvisionedIds(requestWithState, numInstances, overProvisionedRequestIds, possiblyUnderProvisionedRequestIds);
    }
    filterForPendingRequests(possiblyUnderProvisionedRequestIds);
    final List<String> underProvisionedRequestIds = getUnderProvisionedRequestIds(possiblyUnderProvisionedRequestIds);
    final int pendingRequests = requestManager.getSizeOfPendingQueue();
    final int cleaningRequests = requestManager.getSizeOfCleanupQueue();
    List<SingularityRack> racks = rackManager.getObjects();
    int activeRacks = 0;
    int deadRacks = 0;
    int decommissioningRacks = 0;
    int unknownRacks = 0;
    for (SingularityRack rack : racks) {
        switch(rack.getCurrentState().getState()) {
            case ACTIVE:
                activeRacks++;
                break;
            case DEAD:
                deadRacks++;
                break;
            case MISSING_ON_STARTUP:
                unknownRacks++;
                break;
            case DECOMMISSIONED:
            case STARTING_DECOMMISSION:
            case DECOMMISSIONING:
                decommissioningRacks++;
                break;
            default:
                unknownRacks++;
                break;
        }
    }
    List<SingularityAgent> slaves = agentManager.getObjects();
    int activeSlaves = 0;
    int deadSlaves = 0;
    int decommissioningSlaves = 0;
    int unknownSlaves = 0;
    for (SingularityAgent slave : slaves) {
        switch(slave.getCurrentState().getState()) {
            case ACTIVE:
                activeSlaves++;
                break;
            case DEAD:
                deadSlaves++;
                break;
            case MISSING_ON_STARTUP:
                unknownSlaves++;
                break;
            case DECOMMISSIONED:
            case STARTING_DECOMMISSION:
            case DECOMMISSIONING:
                decommissioningSlaves++;
                break;
            default:
                unknownSlaves++;
                break;
        }
    }
    final List<SingularityHostState> states = getHostStates();
    int numDeploys = 0;
    long oldestDeploy = 0;
    long oldestDeployStep = 0;
    List<SingularityDeployMarker> activeDeploys = new ArrayList<>();
    final long now = System.currentTimeMillis();
    for (SingularityPendingDeploy pendingDeploy : deployManager.getPendingDeploys()) {
        activeDeploys.add(pendingDeploy.getDeployMarker());
        if (!pendingDeploy.getDeployProgress().isStepLaunchComplete()) {
            long deployStepDelta = now - pendingDeploy.getDeployProgress().getTimestamp();
            if (deployStepDelta > oldestDeployStep) {
                oldestDeployStep = deployStepDelta;
            }
        }
        long delta = now - pendingDeploy.getDeployMarker().getTimestamp();
        if (delta > oldestDeploy) {
            oldestDeploy = delta;
        }
        numDeploys++;
    }
    final Optional<Boolean> authDatastoreHealthy = authDatastore.isHealthy();
    final Optional<Double> minimumPriorityLevel = getMinimumPriorityLevel();
    return new SingularityState(activeTasks, launchingTasks, numActiveRequests, cooldownRequests, numPausedRequests, scheduledTasks, pendingRequests, lbCleanupTasks, lbCleanupRequests, cleaningRequests, activeSlaves, deadSlaves, decommissioningSlaves, activeRacks, deadRacks, decommissioningRacks, cleaningTasks, states, oldestDeploy, numDeploys, oldestDeployStep, activeDeploys, scheduledTasksInfo.getLateTasks().size(), scheduledTasksInfo.getLateTasks(), scheduledTasksInfo.getOnDemandLateTasks().size(), scheduledTasksInfo.getOnDemandLateTasks(), scheduledTasksInfo.getNumFutureTasks(), scheduledTasksInfo.getMaxTaskLag(), System.currentTimeMillis(), includeRequestIds ? overProvisionedRequestIds : null, includeRequestIds ? underProvisionedRequestIds : null, overProvisionedRequestIds.size(), underProvisionedRequestIds.size(), numFinishedRequests, unknownRacks, unknownSlaves, authDatastoreHealthy, minimumPriorityLevel, (long) statusUpdateDeltas.getSnapshot().getMean(), lastHeartbeatTime.get(), disasterManager.getFireAlarm());
}
Also used : SingularityDeployMarker(com.hubspot.singularity.SingularityDeployMarker) ArrayList(java.util.ArrayList) SingularityRack(com.hubspot.singularity.SingularityRack) SingularityState(com.hubspot.singularity.SingularityState) SingularityHostState(com.hubspot.singularity.SingularityHostState) SingularityRequestWithState(com.hubspot.singularity.SingularityRequestWithState) SingularityAgent(com.hubspot.singularity.SingularityAgent) HashSet(java.util.HashSet) SingularityScheduledTasksInfo(com.hubspot.singularity.SingularityScheduledTasksInfo) SingularityPendingDeploy(com.hubspot.singularity.SingularityPendingDeploy) AtomicLong(java.util.concurrent.atomic.AtomicLong)

Example 18 with SingularityPendingDeploy

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

the class TaskRequestManager method getTaskRequests.

public List<SingularityTaskRequest> getTaskRequests(List<SingularityPendingTask> tasks) {
    final Multimap<String, SingularityPendingTask> requestIdToPendingTaskId = ArrayListMultimap.create(tasks.size(), 1);
    for (SingularityPendingTask task : tasks) {
        requestIdToPendingTaskId.put(task.getPendingTaskId().getRequestId(), task);
    }
    final List<SingularityRequestWithState> matchingRequests = requestManager.getRequests(requestIdToPendingTaskId.keySet());
    final Map<SingularityPendingTask, SingularityDeployKey> deployKeys = SingularityDeployKey.fromPendingTasks(requestIdToPendingTaskId.values());
    final Map<SingularityDeployKey, SingularityDeploy> matchingDeploys = deployManager.getDeploysForKeys(Sets.newHashSet(deployKeys.values()));
    final List<SingularityTaskRequest> taskRequests = Lists.newArrayListWithCapacity(matchingRequests.size());
    for (SingularityRequestWithState request : matchingRequests) {
        Optional<SingularityPendingDeploy> maybePendingDeploy = deployManager.getPendingDeploy(request.getRequest().getId());
        for (SingularityPendingTask task : requestIdToPendingTaskId.get(request.getRequest().getId())) {
            SingularityDeploy foundDeploy = matchingDeploys.get(deployKeys.get(task));
            if (foundDeploy == null) {
                LOG.warn("Couldn't find a matching deploy for pending task {}", task);
                continue;
            }
            if (!request.getState().isRunnable()) {
                LOG.warn("Request was in state {} for pending task {}", request.getState(), task);
                continue;
            }
            Optional<SingularityRequest> updatedRequest = maybePendingDeploy.isPresent() && maybePendingDeploy.get().getDeployMarker().getDeployId().equals(task.getPendingTaskId().getDeployId()) ? maybePendingDeploy.get().getUpdatedRequest() : Optional.<SingularityRequest>empty();
            taskRequests.add(new SingularityTaskRequest(updatedRequest.orElse(request.getRequest()), foundDeploy, task));
        }
    }
    return taskRequests;
}
Also used : SingularityRequest(com.hubspot.singularity.SingularityRequest) SingularityDeploy(com.hubspot.singularity.SingularityDeploy) SingularityDeployKey(com.hubspot.singularity.SingularityDeployKey) SingularityPendingTask(com.hubspot.singularity.SingularityPendingTask) SingularityPendingDeploy(com.hubspot.singularity.SingularityPendingDeploy) SingularityRequestWithState(com.hubspot.singularity.SingularityRequestWithState) SingularityTaskRequest(com.hubspot.singularity.SingularityTaskRequest)

Example 19 with SingularityPendingDeploy

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

the class SingularityStartup method enqueueHealthAndNewTaskChecks.

private List<CompletableFuture<Void>> enqueueHealthAndNewTaskChecks(ExecutorService startupExecutor) {
    final List<SingularityTask> activeTasks = taskManager.getActiveTasks();
    final Map<SingularityTaskId, SingularityTask> activeTaskMap = Maps.uniqueIndex(activeTasks, SingularityTaskIdHolder.getTaskIdFunction());
    final Map<SingularityTaskId, List<SingularityTaskHistoryUpdate>> taskUpdates = taskManager.getTaskHistoryUpdates(activeTaskMap.keySet());
    final Map<SingularityDeployKey, SingularityPendingDeploy> pendingDeploys = Maps.uniqueIndex(deployManager.getPendingDeploys(), SingularityDeployKey.FROM_PENDING_TO_DEPLOY_KEY::apply);
    final Map<String, SingularityRequestWithState> idToRequest = Maps.uniqueIndex(requestManager.getRequests(), SingularityRequestWithState.REQUEST_STATE_TO_REQUEST_ID::apply);
    AtomicInteger enqueuedNewTaskChecks = new AtomicInteger(0);
    AtomicInteger enqueuedHealthchecks = new AtomicInteger(0);
    List<CompletableFuture<Void>> enqueueFutures = new ArrayList<>();
    for (Entry<SingularityTaskId, SingularityTask> entry : activeTaskMap.entrySet()) {
        enqueueFutures.add(CompletableFuture.runAsync(() -> {
            SingularityTaskId taskId = entry.getKey();
            SingularityTask task = entry.getValue();
            SimplifiedTaskState simplifiedTaskState = SingularityTaskHistoryUpdate.getCurrentState(taskUpdates.get(taskId));
            if (simplifiedTaskState != SimplifiedTaskState.DONE) {
                SingularityDeployKey deployKey = new SingularityDeployKey(taskId.getRequestId(), taskId.getDeployId());
                Optional<SingularityPendingDeploy> pendingDeploy = Optional.ofNullable(pendingDeploys.get(deployKey));
                Optional<SingularityRequestWithState> request = Optional.ofNullable(idToRequest.get(taskId.getRequestId()));
                if (!pendingDeploy.isPresent()) {
                    newTaskChecker.enqueueNewTaskCheck(task, request, healthchecker);
                    enqueuedNewTaskChecks.getAndIncrement();
                }
                if (simplifiedTaskState == SimplifiedTaskState.RUNNING) {
                    if (healthchecker.enqueueHealthcheck(task, pendingDeploy, request)) {
                        enqueuedHealthchecks.getAndIncrement();
                    }
                }
            }
        }, startupExecutor));
    }
    LOG.info("Enqueued {} health checks and {} new task checks (out of {} active tasks)", enqueuedHealthchecks.get(), enqueuedNewTaskChecks.get(), activeTasks.size());
    return enqueueFutures;
}
Also used : Optional(java.util.Optional) ArrayList(java.util.ArrayList) SingularityDeployKey(com.hubspot.singularity.SingularityDeployKey) CompletableFuture(java.util.concurrent.CompletableFuture) SingularityTask(com.hubspot.singularity.SingularityTask) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SingularityPendingDeploy(com.hubspot.singularity.SingularityPendingDeploy) SingularityRequestWithState(com.hubspot.singularity.SingularityRequestWithState) ArrayList(java.util.ArrayList) List(java.util.List) SimplifiedTaskState(com.hubspot.singularity.SingularityTaskHistoryUpdate.SimplifiedTaskState) SingularityTaskId(com.hubspot.singularity.SingularityTaskId)

Aggregations

SingularityPendingDeploy (com.hubspot.singularity.SingularityPendingDeploy)19 SingularityRequestWithState (com.hubspot.singularity.SingularityRequestWithState)13 SingularityRequest (com.hubspot.singularity.SingularityRequest)11 SingularityTaskId (com.hubspot.singularity.SingularityTaskId)10 ArrayList (java.util.ArrayList)10 RequestState (com.hubspot.singularity.RequestState)8 SingularityPendingRequest (com.hubspot.singularity.SingularityPendingRequest)8 SingularityTask (com.hubspot.singularity.SingularityTask)8 List (java.util.List)8 SingularityRequestDeployState (com.hubspot.singularity.SingularityRequestDeployState)7 Optional (java.util.Optional)7 SingularityDeploy (com.hubspot.singularity.SingularityDeploy)6 SingularityDeployKey (com.hubspot.singularity.SingularityDeployKey)6 SingularityDeployProgress (com.hubspot.singularity.SingularityDeployProgress)6 Map (java.util.Map)6 Inject (com.google.inject.Inject)5 RequestType (com.hubspot.singularity.RequestType)5 SingularityDeployMarker (com.hubspot.singularity.SingularityDeployMarker)5 PendingType (com.hubspot.singularity.SingularityPendingRequest.PendingType)5 SingularityTaskCleanup (com.hubspot.singularity.SingularityTaskCleanup)5