Search in sources :

Example 6 with SingularityDeployKey

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

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

the class SingularityScheduler method drainPendingQueue.

@Timed
public void drainPendingQueue() {
    final long start = System.currentTimeMillis();
    final ImmutableList<SingularityPendingRequest> pendingRequests = ImmutableList.copyOf(requestManager.getPendingRequests());
    if (pendingRequests.isEmpty()) {
        LOG.trace("Pending queue was empty");
        return;
    }
    LOG.info("Pending queue had {} requests", pendingRequests.size());
    Map<SingularityDeployKey, List<SingularityPendingRequest>> deployKeyToPendingRequests = pendingRequests.stream().collect(Collectors.groupingBy((request) -> new SingularityDeployKey(request.getRequestId(), request.getDeployId())));
    AtomicInteger totalNewScheduledTasks = new AtomicInteger(0);
    AtomicInteger heldForScheduledActiveTask = new AtomicInteger(0);
    AtomicInteger obsoleteRequests = new AtomicInteger(0);
    deployKeyToPendingRequests.forEach((deployKey, pendingRequestsForDeployKey) -> {
        lock.runWithRequestLock(() -> handlePendingRequestsForDeployKey(obsoleteRequests, heldForScheduledActiveTask, totalNewScheduledTasks, deployKey, pendingRequestsForDeployKey), deployKey.getRequestId(), String.format("%s#%s", getClass().getSimpleName(), "drainPendingQueue"));
    });
    LOG.info("Scheduled {} new tasks ({} obsolete requests, {} held) in {}", totalNewScheduledTasks.get(), obsoleteRequests.get(), heldForScheduledActiveTask.get(), JavaUtils.duration(start));
}
Also used : SingularityDeployKey(com.hubspot.singularity.SingularityDeployKey) SingularityTask(com.hubspot.singularity.SingularityTask) DeployManager(com.hubspot.singularity.data.DeployManager) ExtendedTaskState(com.hubspot.singularity.ExtendedTaskState) ListMultimap(com.google.common.collect.ListMultimap) Date(java.util.Date) Inject(com.google.inject.Inject) RequestManager(com.hubspot.singularity.data.RequestManager) CronExpression(org.quartz.CronExpression) LoggerFactory(org.slf4j.LoggerFactory) InvalidRecurrenceRuleException(org.dmfs.rfc5545.recur.InvalidRecurrenceRuleException) SingularityDeployStatistics(com.hubspot.singularity.SingularityDeployStatistics) SingularityDeployStatisticsBuilder(com.hubspot.singularity.SingularityDeployStatisticsBuilder) DeployState(com.hubspot.singularity.DeployState) SingularityTaskShellCommandRequestId(com.hubspot.singularity.SingularityTaskShellCommandRequestId) TaskRequestManager(com.hubspot.singularity.data.TaskRequestManager) SingularityExpiringBounce(com.hubspot.singularity.expiring.SingularityExpiringBounce) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HashMultiset(com.google.common.collect.HashMultiset) Optional(com.google.common.base.Optional) Map(java.util.Map) TaskManager(com.hubspot.singularity.data.TaskManager) ParseException(java.text.ParseException) SingularityPendingTaskId(com.hubspot.singularity.SingularityPendingTaskId) SingularityTaskId(com.hubspot.singularity.SingularityTaskId) TimeZone(java.util.TimeZone) Collection(java.util.Collection) SingularityKilledTaskIdRecord(com.hubspot.singularity.SingularityKilledTaskIdRecord) Set(java.util.Set) Protos(org.apache.mesos.v1.Protos) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) RequestState(com.hubspot.singularity.RequestState) PendingType(com.hubspot.singularity.SingularityPendingRequest.PendingType) Timed(com.codahale.metrics.annotation.Timed) List(java.util.List) JavaUtils(com.hubspot.mesos.JavaUtils) RFC5545Schedule(com.hubspot.singularity.helpers.RFC5545Schedule) SingularityMachineAbstraction(com.hubspot.singularity.SingularityMachineAbstraction) Entry(java.util.Map.Entry) ScheduleType(com.hubspot.singularity.ScheduleType) RackManager(com.hubspot.singularity.data.RackManager) SingularitySchedulerLock(com.hubspot.singularity.mesos.SingularitySchedulerLock) SingularityCreateResult(com.hubspot.singularity.SingularityCreateResult) SingularityRequestWithState(com.hubspot.singularity.SingularityRequestWithState) SingularityRequest(com.hubspot.singularity.SingularityRequest) SingularityTaskHistoryUpdate(com.hubspot.singularity.SingularityTaskHistoryUpdate) SingularityPendingTask(com.hubspot.singularity.SingularityPendingTask) Multiset(com.google.common.collect.Multiset) SingularityPendingRequest(com.hubspot.singularity.SingularityPendingRequest) Singleton(javax.inject.Singleton) AbstractMachineManager(com.hubspot.singularity.data.AbstractMachineManager) ArrayList(java.util.ArrayList) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) SingularitySlave(com.hubspot.singularity.SingularitySlave) SingularityDeployKey(com.hubspot.singularity.SingularityDeployKey) RequestType(com.hubspot.singularity.RequestType) SlaveManager(com.hubspot.singularity.data.SlaveManager) SingularityConfiguration(com.hubspot.singularity.config.SingularityConfiguration) SingularityTaskCleanup(com.hubspot.singularity.SingularityTaskCleanup) Logger(org.slf4j.Logger) Reason(org.apache.mesos.v1.Protos.TaskStatus.Reason) Throwables(com.google.common.base.Throwables) SingularityPendingDeploy(com.hubspot.singularity.SingularityPendingDeploy) SingularityRack(com.hubspot.singularity.SingularityRack) Maps(com.google.common.collect.Maps) TimeUnit(java.util.concurrent.TimeUnit) SingularityDeployMarker(com.hubspot.singularity.SingularityDeployMarker) SingularityTaskRequest(com.hubspot.singularity.SingularityTaskRequest) MachineState(com.hubspot.singularity.MachineState) SingularityMailer(com.hubspot.singularity.smtp.SingularityMailer) SingularityRequestDeployState(com.hubspot.singularity.SingularityRequestDeployState) TaskCleanupType(com.hubspot.singularity.TaskCleanupType) Comparator(java.util.Comparator) Collections(java.util.Collections) SingularityDeployProgress(com.hubspot.singularity.SingularityDeployProgress) SingularityPendingRequest(com.hubspot.singularity.SingularityPendingRequest) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) Timed(com.codahale.metrics.annotation.Timed)

Example 8 with SingularityDeployKey

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

the class SingularityCleaner method processTaskCleanupsForRequest.

private void processTaskCleanupsForRequest(String requestId, List<SingularityTaskCleanup> cleanupTasks, AtomicInteger killedTasks) {
    final Multiset<SingularityDeployKey> incrementalCleaningTasks = HashMultiset.create(cleanupTasks.size());
    final List<String> taskIdsForDeletedRequest = new ArrayList<>();
    boolean isRequestDeleting = false;
    // TODO - Better check for deleting request state
    final Set<SingularityTaskId> cleaningTasks = new HashSet<>(cleanupTasks.size());
    for (SingularityTaskCleanup cleanupTask : cleanupTasks) {
        cleaningTasks.add(cleanupTask.getTaskId());
        if (isIncrementalDeployCleanup(cleanupTask) || cleanupTask.getCleanupType() == TaskCleanupType.INCREMENTAL_BOUNCE) {
            incrementalCleaningTasks.add(SingularityDeployKey.fromTaskId(cleanupTask.getTaskId()));
        }
        if (cleanupTask.getCleanupType() == TaskCleanupType.REQUEST_DELETING) {
            taskIdsForDeletedRequest.add(cleanupTask.getTaskId().getId());
            isRequestDeleting = true;
        }
    }
    LOG.info("Cleaning up {} tasks for request {}", cleanupTasks.size(), requestId);
    final List<SingularityTaskId> activeTaskIds = taskManager.getActiveTaskIds();
    for (SingularityTaskCleanup cleanupTask : cleanupTasks) {
        SingularityTaskId taskId = cleanupTask.getTaskId();
        if (!isValidTask(cleanupTask)) {
            LOG.info("Couldn't find a matching active task for cleanup task {}, deleting..", cleanupTask);
            taskManager.deleteCleanupTask(taskId.getId());
        } else if (shouldKillTask(cleanupTask, activeTaskIds, cleaningTasks, incrementalCleaningTasks) && checkLBStateAndShouldKillTask(cleanupTask)) {
            scheduler.killAndRecord(taskId, cleanupTask.getCleanupType(), cleanupTask.getUser());
            taskManager.deleteCleanupTask(taskId.getId());
            killedTasks.getAndIncrement();
        }
        cleanupRequestIfNoRemainingTasks(cleanupTask, taskIdsForDeletedRequest, isRequestDeleting);
    }
}
Also used : SingularityDeployKey(com.hubspot.singularity.SingularityDeployKey) ArrayList(java.util.ArrayList) SingularityTaskCleanup(com.hubspot.singularity.SingularityTaskCleanup) SingularityTaskId(com.hubspot.singularity.SingularityTaskId) HashSet(java.util.HashSet)

Example 9 with SingularityDeployKey

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

the class SingularityStartup method getDeployKeyToPendingTaskId.

private Map<SingularityDeployKey, SingularityPendingTaskId> getDeployKeyToPendingTaskId() {
    final List<SingularityPendingTaskId> pendingTaskIds = taskManager.getPendingTaskIds();
    final Map<SingularityDeployKey, SingularityPendingTaskId> deployKeyToPendingTaskId = Maps.newHashMapWithExpectedSize(pendingTaskIds.size());
    for (SingularityPendingTaskId taskId : pendingTaskIds) {
        SingularityDeployKey deployKey = new SingularityDeployKey(taskId.getRequestId(), taskId.getDeployId());
        deployKeyToPendingTaskId.put(deployKey, taskId);
    }
    return deployKeyToPendingTaskId;
}
Also used : SingularityDeployKey(com.hubspot.singularity.SingularityDeployKey) SingularityPendingTaskId(com.hubspot.singularity.SingularityPendingTaskId)

Example 10 with SingularityDeployKey

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

the class SingularityStartup method checkSchedulerForInconsistentState.

/**
 * We need to run this check for the various situations where the scheduler could get in an inconsistent state due
 * to a crash/network failure during series of state transactions.
 *
 *  1) Unpausing
 *  2) Launching Task
 */
@VisibleForTesting
void checkSchedulerForInconsistentState() {
    final long now = System.currentTimeMillis();
    final Map<SingularityDeployKey, SingularityPendingTaskId> deployKeyToPendingTaskId = getDeployKeyToPendingTaskId();
    for (SingularityRequestWithState requestWithState : requestManager.getRequests()) {
        switch(requestWithState.getState()) {
            case ACTIVE:
            case SYSTEM_COOLDOWN:
            case DEPLOYING_TO_UNPAUSE:
                checkActiveRequest(requestWithState, deployKeyToPendingTaskId, now);
                break;
            case DELETED:
            case PAUSED:
            case FINISHED:
                break;
        }
    }
}
Also used : SingularityDeployKey(com.hubspot.singularity.SingularityDeployKey) SingularityPendingTaskId(com.hubspot.singularity.SingularityPendingTaskId) SingularityRequestWithState(com.hubspot.singularity.SingularityRequestWithState) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

SingularityDeployKey (com.hubspot.singularity.SingularityDeployKey)19 SingularityRequest (com.hubspot.singularity.SingularityRequest)7 SingularityTaskId (com.hubspot.singularity.SingularityTaskId)7 SingularityDeploy (com.hubspot.singularity.SingularityDeploy)6 SingularityPendingTaskId (com.hubspot.singularity.SingularityPendingTaskId)6 SingularityRequestWithState (com.hubspot.singularity.SingularityRequestWithState)6 ArrayList (java.util.ArrayList)6 SingularityPendingRequest (com.hubspot.singularity.SingularityPendingRequest)5 SingularityRequestDeployState (com.hubspot.singularity.SingularityRequestDeployState)5 Optional (com.google.common.base.Optional)3 ImmutableList (com.google.common.collect.ImmutableList)3 Inject (com.google.inject.Inject)3 HttpResponse (com.hubspot.horizon.HttpResponse)3 ExtendedTaskState (com.hubspot.singularity.ExtendedTaskState)3 MachineState (com.hubspot.singularity.MachineState)3 SingularityTask (com.hubspot.singularity.SingularityTask)3 SingularityTaskRequest (com.hubspot.singularity.SingularityTaskRequest)3 SingularityTaskShellCommandUpdate (com.hubspot.singularity.SingularityTaskShellCommandUpdate)3 List (java.util.List)3 Map (java.util.Map)3