Search in sources :

Example 11 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>absent();
            taskRequests.add(new SingularityTaskRequest(updatedRequest.or(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 12 with SingularityPendingDeploy

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

the class RequestHelper method getTaskIdsByStatusForRequest.

private Optional<SingularityTaskIdsByStatus> getTaskIdsByStatusForRequest(SingularityRequestWithState requestWithState) {
    String requestId = requestWithState.getRequest().getId();
    Optional<SingularityPendingDeploy> pendingDeploy = deployManager.getPendingDeploy(requestId);
    List<SingularityTaskId> cleaningTaskIds = taskManager.getCleanupTaskIds().stream().filter((t) -> t.getRequestId().equals(requestId)).collect(Collectors.toList());
    List<SingularityPendingTaskId> pendingTaskIds = taskManager.getPendingTaskIdsForRequest(requestId);
    List<SingularityTaskId> activeTaskIds = taskManager.getActiveTaskIdsForRequest(requestId);
    activeTaskIds.removeAll(cleaningTaskIds);
    List<SingularityTaskId> healthyTaskIds = new ArrayList<>();
    List<SingularityTaskId> notYetHealthyTaskIds = new ArrayList<>();
    Map<String, List<SingularityTaskId>> taskIdsByDeployId = activeTaskIds.stream().collect(Collectors.groupingBy(SingularityTaskId::getDeployId));
    for (Map.Entry<String, List<SingularityTaskId>> entry : taskIdsByDeployId.entrySet()) {
        Optional<SingularityDeploy> deploy = deployManager.getDeploy(requestId, entry.getKey());
        List<SingularityTaskId> healthyTasksIdsForDeploy = deployHealthHelper.getHealthyTasks(requestWithState.getRequest(), deploy, entry.getValue(), pendingDeploy.isPresent() && pendingDeploy.get().getDeployMarker().getDeployId().equals(entry.getKey()));
        for (SingularityTaskId taskId : entry.getValue()) {
            if (healthyTasksIdsForDeploy.contains(taskId)) {
                healthyTaskIds.add(taskId);
            } else {
                notYetHealthyTaskIds.add(taskId);
            }
        }
    }
    return Optional.of(new SingularityTaskIdsByStatus(healthyTaskIds, notYetHealthyTaskIds, pendingTaskIds, cleaningTaskIds));
}
Also used : DeployManager(com.hubspot.singularity.data.DeployManager) Inject(com.google.inject.Inject) SingularityRequestCleanup(com.hubspot.singularity.SingularityRequestCleanup) RequestManager(com.hubspot.singularity.data.RequestManager) LoggerFactory(org.slf4j.LoggerFactory) SingularityUserSettings(com.hubspot.singularity.SingularityUserSettings) SingularityExpiringScale(com.hubspot.singularity.expiring.SingularityExpiringScale) SingularityBounceRequest(com.hubspot.singularity.api.SingularityBounceRequest) RequestHistoryType(com.hubspot.singularity.SingularityRequestHistory.RequestHistoryType) SingularityExpiringBounce(com.hubspot.singularity.expiring.SingularityExpiringBounce) Optional(com.google.common.base.Optional) Map(java.util.Map) SingularityValidator(com.hubspot.singularity.data.SingularityValidator) TaskManager(com.hubspot.singularity.data.TaskManager) SingularityPendingTaskId(com.hubspot.singularity.SingularityPendingTaskId) SingularityTaskId(com.hubspot.singularity.SingularityTaskId) UserManager(com.hubspot.singularity.data.UserManager) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SingularityExpiringSkipHealthchecks(com.hubspot.singularity.expiring.SingularityExpiringSkipHealthchecks) UUID(java.util.UUID) SingularityExpiringPause(com.hubspot.singularity.expiring.SingularityExpiringPause) Collectors(java.util.stream.Collectors) RequestState(com.hubspot.singularity.RequestState) PendingType(com.hubspot.singularity.SingularityPendingRequest.PendingType) List(java.util.List) JavaUtils(com.hubspot.mesos.JavaUtils) TaskHistoryHelper(com.hubspot.singularity.data.history.TaskHistoryHelper) RequestCleanupType(com.hubspot.singularity.RequestCleanupType) Singleton(com.google.inject.Singleton) SingularityCreateResult(com.hubspot.singularity.SingularityCreateResult) SingularityRequestWithState(com.hubspot.singularity.SingularityRequestWithState) SingularityTaskHistoryQuery(com.hubspot.singularity.SingularityTaskHistoryQuery) SingularityRequest(com.hubspot.singularity.SingularityRequest) SingularityTaskHistoryUpdate(com.hubspot.singularity.SingularityTaskHistoryUpdate) SingularityRequestHistory(com.hubspot.singularity.SingularityRequestHistory) SingularityTaskIdHistory(com.hubspot.singularity.SingularityTaskIdHistory) SingularityUser(com.hubspot.singularity.SingularityUser) SingularityPendingRequest(com.hubspot.singularity.SingularityPendingRequest) CompletableFuture(java.util.concurrent.CompletableFuture) SingularityTaskIdsByStatus(com.hubspot.singularity.SingularityTaskIdsByStatus) ArrayList(java.util.ArrayList) RequestType(com.hubspot.singularity.RequestType) SingularityDeployHealthHelper(com.hubspot.singularity.scheduler.SingularityDeployHealthHelper) Logger(org.slf4j.Logger) SingularityDeploy(com.hubspot.singularity.SingularityDeploy) SingularityPendingDeploy(com.hubspot.singularity.SingularityPendingDeploy) SingularityMailer(com.hubspot.singularity.smtp.SingularityMailer) SingularityRequestDeployState(com.hubspot.singularity.SingularityRequestDeployState) SingularityRequestParent(com.hubspot.singularity.SingularityRequestParent) Collections(java.util.Collections) SingularityPendingTaskId(com.hubspot.singularity.SingularityPendingTaskId) ArrayList(java.util.ArrayList) SingularityDeploy(com.hubspot.singularity.SingularityDeploy) SingularityPendingDeploy(com.hubspot.singularity.SingularityPendingDeploy) List(java.util.List) ArrayList(java.util.ArrayList) SingularityTaskIdsByStatus(com.hubspot.singularity.SingularityTaskIdsByStatus) SingularityTaskId(com.hubspot.singularity.SingularityTaskId) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 13 with SingularityPendingDeploy

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

the class SingularityStartup method enqueueHealthAndNewTaskChecks.

private void enqueueHealthAndNewTaskChecks() {
    final long start = System.currentTimeMillis();
    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);
    final Map<String, SingularityRequestWithState> idToRequest = Maps.uniqueIndex(requestManager.getRequests(), SingularityRequestWithState.REQUEST_STATE_TO_REQUEST_ID);
    requestManager.getActiveRequests();
    int enqueuedNewTaskChecks = 0;
    int enqueuedHealthchecks = 0;
    for (Map.Entry<SingularityTaskId, SingularityTask> entry : activeTaskMap.entrySet()) {
        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.fromNullable(pendingDeploys.get(deployKey));
            Optional<SingularityRequestWithState> request = Optional.fromNullable(idToRequest.get(taskId.getRequestId()));
            if (!pendingDeploy.isPresent()) {
                newTaskChecker.enqueueNewTaskCheck(task, request, healthchecker);
                enqueuedNewTaskChecks++;
            }
            if (simplifiedTaskState == SimplifiedTaskState.RUNNING) {
                if (healthchecker.enqueueHealthcheck(task, pendingDeploy, request)) {
                    enqueuedHealthchecks++;
                }
            }
        }
    }
    LOG.info("Enqueued {} health checks and {} new task checks (out of {} active tasks) in {}", enqueuedHealthchecks, enqueuedNewTaskChecks, activeTasks.size(), JavaUtils.duration(start));
}
Also used : SingularityDeployKey(com.hubspot.singularity.SingularityDeployKey) SingularityTask(com.hubspot.singularity.SingularityTask) SingularityPendingDeploy(com.hubspot.singularity.SingularityPendingDeploy) SingularityRequestWithState(com.hubspot.singularity.SingularityRequestWithState) List(java.util.List) SimplifiedTaskState(com.hubspot.singularity.SingularityTaskHistoryUpdate.SimplifiedTaskState) SingularityTaskId(com.hubspot.singularity.SingularityTaskId) Map(java.util.Map)

Example 14 with SingularityPendingDeploy

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

the class SingularityTaskHistoryPersister method runActionOnPoll.

@Override
public void runActionOnPoll() {
    LOG.info("Checking inactive task ids for task history persistence");
    final long start = System.currentTimeMillis();
    final List<SingularityTaskId> allTaskIds = taskManager.getAllTaskIds();
    final Set<SingularityTaskId> activeTaskIds = Sets.newHashSet(taskManager.getActiveTaskIds());
    final Set<SingularityTaskId> lbCleaningTaskIds = Sets.newHashSet(taskManager.getLBCleanupTasks());
    final List<SingularityPendingDeploy> pendingDeploys = deployManager.getPendingDeploys();
    int numTotal = 0;
    int numTransferred = 0;
    final Multimap<String, SingularityTaskId> eligibleTaskIdByRequestId = TreeMultimap.create(Ordering.natural(), SingularityTaskId.STARTED_AT_COMPARATOR_DESC);
    for (SingularityTaskId taskId : allTaskIds) {
        if (activeTaskIds.contains(taskId) || lbCleaningTaskIds.contains(taskId) || isPartOfPendingDeploy(pendingDeploys, taskId)) {
            continue;
        }
        eligibleTaskIdByRequestId.put(taskId.getRequestId(), taskId);
    }
    for (Map.Entry<String, Collection<SingularityTaskId>> entry : eligibleTaskIdByRequestId.asMap().entrySet()) {
        int i = 0;
        for (SingularityTaskId taskId : entry.getValue()) {
            final long age = start - taskId.getStartedAt();
            if (age < configuration.getTaskPersistAfterStartupBufferMillis()) {
                LOG.debug("Not persisting {}, it has started up too recently {} (buffer: {}) - this prevents race conditions with ZK tx", taskId, JavaUtils.durationFromMillis(age), JavaUtils.durationFromMillis(configuration.getTaskPersistAfterStartupBufferMillis()));
                continue;
            }
            if (moveToHistoryOrCheckForPurge(taskId, i++)) {
                numTransferred++;
            }
            numTotal++;
        }
    }
    LOG.info("Transferred {} out of {} inactive task ids (total {}) in {}", numTransferred, numTotal, allTaskIds.size(), JavaUtils.duration(start));
}
Also used : SingularityPendingDeploy(com.hubspot.singularity.SingularityPendingDeploy) Collection(java.util.Collection) SingularityTaskId(com.hubspot.singularity.SingularityTaskId) Map(java.util.Map)

Aggregations

SingularityPendingDeploy (com.hubspot.singularity.SingularityPendingDeploy)14 SingularityRequest (com.hubspot.singularity.SingularityRequest)9 SingularityRequestWithState (com.hubspot.singularity.SingularityRequestWithState)9 SingularityTaskId (com.hubspot.singularity.SingularityTaskId)6 RequestState (com.hubspot.singularity.RequestState)5 SingularityPendingRequest (com.hubspot.singularity.SingularityPendingRequest)5 ArrayList (java.util.ArrayList)5 SingularityDeploy (com.hubspot.singularity.SingularityDeploy)4 SingularityDeployProgress (com.hubspot.singularity.SingularityDeployProgress)4 SingularityRequestDeployState (com.hubspot.singularity.SingularityRequestDeployState)4 SingularityTask (com.hubspot.singularity.SingularityTask)4 List (java.util.List)4 SingularityDeployKey (com.hubspot.singularity.SingularityDeployKey)3 SingularityDeployMarker (com.hubspot.singularity.SingularityDeployMarker)3 PendingType (com.hubspot.singularity.SingularityPendingRequest.PendingType)3 Map (java.util.Map)3 Optional (com.google.common.base.Optional)2 Inject (com.google.inject.Inject)2 JavaUtils (com.hubspot.mesos.JavaUtils)2 RequestType (com.hubspot.singularity.RequestType)2