Search in sources :

Example 61 with SingularityTaskId

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

the class SingularityScheduler method handleCompletedTask.

@Timed
public void handleCompletedTask(Optional<SingularityTask> task, SingularityTaskId taskId, boolean wasActive, long timestamp, ExtendedTaskState state, SingularityCreateResult taskHistoryUpdateCreateResult, Protos.TaskStatus status) {
    final SingularityDeployStatistics deployStatistics = getDeployStatistics(taskId.getRequestId(), taskId.getDeployId());
    if (wasActive) {
        taskManager.deleteActiveTask(taskId.getId());
    }
    if (!task.isPresent() || task.get().getTaskRequest().getRequest().isLoadBalanced()) {
        taskManager.createLBCleanupTask(taskId);
    }
    if (requestManager.isBouncing(taskId.getRequestId())) {
        List<SingularityTaskId> activeTaskIds = taskManager.getActiveTaskIdsForRequest(taskId.getRequestId());
        boolean foundBouncingTask = false;
        for (SingularityTaskId activeTaskId : activeTaskIds) {
            Optional<SingularityTaskHistoryUpdate> maybeCleaningUpdate = taskManager.getTaskHistoryUpdate(activeTaskId, ExtendedTaskState.TASK_CLEANING);
            if (maybeCleaningUpdate.isPresent()) {
                if (maybeCleaningUpdate.get().getStatusReason().or("").contains("BOUNCE")) {
                    // TaskCleanupType enum is included in status message
                    LOG.debug("Found task {} still waiting for bounce to complete", activeTaskId);
                    foundBouncingTask = true;
                    break;
                } else if (!maybeCleaningUpdate.get().getPrevious().isEmpty()) {
                    for (SingularityTaskHistoryUpdate previousUpdate : maybeCleaningUpdate.get().getPrevious()) {
                        if (previousUpdate.getStatusMessage().or("").contains("BOUNCE")) {
                            LOG.debug("Found task {} still waiting for bounce to complete", activeTaskId);
                            foundBouncingTask = true;
                            break;
                        }
                    }
                }
            }
        }
        if (!foundBouncingTask) {
            LOG.info("Bounce completed for request {}, no cleaning tasks due to bounce found", taskId.getRequestId());
            Optional<SingularityExpiringBounce> expiringBounce = requestManager.getExpiringBounce(taskId.getRequestId());
            if (expiringBounce.isPresent() && expiringBounce.get().getDeployId().equals(taskId.getDeployId())) {
                requestManager.deleteExpiringObject(SingularityExpiringBounce.class, taskId.getRequestId());
            }
            requestManager.markBounceComplete(taskId.getRequestId());
        }
    }
    final Optional<PendingType> scheduleResult = handleCompletedTaskWithStatistics(task, taskId, timestamp, state, deployStatistics, taskHistoryUpdateCreateResult, status);
    if (taskHistoryUpdateCreateResult == SingularityCreateResult.EXISTED) {
        return;
    }
    updateDeployStatistics(deployStatistics, taskId, task, timestamp, state, scheduleResult);
}
Also used : PendingType(com.hubspot.singularity.SingularityPendingRequest.PendingType) SingularityTaskHistoryUpdate(com.hubspot.singularity.SingularityTaskHistoryUpdate) SingularityExpiringBounce(com.hubspot.singularity.expiring.SingularityExpiringBounce) SingularityDeployStatistics(com.hubspot.singularity.SingularityDeployStatistics) SingularityTaskId(com.hubspot.singularity.SingularityTaskId) Timed(com.codahale.metrics.annotation.Timed)

Example 62 with SingularityTaskId

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

the class SingularityScheduler method getScheduledTaskIds.

private List<SingularityPendingTask> getScheduledTaskIds(int numMissingInstances, List<SingularityTaskId> matchingTaskIds, SingularityRequest request, RequestState state, SingularityDeployStatistics deployStatistics, String deployId, SingularityPendingRequest pendingRequest, Optional<SingularityPendingDeploy> maybePendingDeploy) {
    final Optional<Long> nextRunAt = getNextRunAt(request, state, deployStatistics, pendingRequest, maybePendingDeploy);
    if (!nextRunAt.isPresent()) {
        return Collections.emptyList();
    }
    final Set<Integer> inuseInstanceNumbers = Sets.newHashSetWithExpectedSize(matchingTaskIds.size());
    for (SingularityTaskId matchingTaskId : matchingTaskIds) {
        inuseInstanceNumbers.add(matchingTaskId.getInstanceNo());
    }
    final List<SingularityPendingTask> newTasks = Lists.newArrayListWithCapacity(numMissingInstances);
    int nextInstanceNumber = 1;
    for (int i = 0; i < numMissingInstances; i++) {
        while (inuseInstanceNumbers.contains(nextInstanceNumber)) {
            nextInstanceNumber++;
        }
        newTasks.add(new SingularityPendingTask(new SingularityPendingTaskId(request.getId(), deployId, nextRunAt.get(), nextInstanceNumber, pendingRequest.getPendingType(), pendingRequest.getTimestamp()), pendingRequest.getCmdLineArgsList(), pendingRequest.getUser(), pendingRequest.getRunId(), pendingRequest.getSkipHealthchecks(), pendingRequest.getMessage(), pendingRequest.getResources(), pendingRequest.getS3UploaderAdditionalFiles(), pendingRequest.getRunAsUserOverride(), pendingRequest.getEnvOverrides(), pendingRequest.getExtraArtifacts(), pendingRequest.getActionId()));
        nextInstanceNumber++;
    }
    return newTasks;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SingularityPendingTask(com.hubspot.singularity.SingularityPendingTask) SingularityPendingTaskId(com.hubspot.singularity.SingularityPendingTaskId) SingularityTaskId(com.hubspot.singularity.SingularityTaskId)

Example 63 with SingularityTaskId

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

the class SingularityScheduler method checkForDecomissions.

@Timed
public void checkForDecomissions() {
    final long start = System.currentTimeMillis();
    final Map<String, Optional<String>> requestIdsToUserToReschedule = Maps.newHashMap();
    final Set<SingularityTaskId> matchingTaskIds = Sets.newHashSet();
    final Collection<SingularityTaskId> activeTaskIds = leaderCache.getActiveTaskIds();
    final Map<SingularitySlave, MachineState> slaves = getDefaultMap(slaveManager.getObjectsFiltered(MachineState.STARTING_DECOMMISSION));
    for (SingularitySlave slave : slaves.keySet()) {
        boolean foundTask = false;
        for (SingularityTask activeTask : taskManager.getTasksOnSlave(activeTaskIds, slave)) {
            cleanupTaskDueToDecomission(requestIdsToUserToReschedule, matchingTaskIds, activeTask, slave);
            foundTask = true;
        }
        if (!foundTask) {
            slaves.put(slave, MachineState.DECOMMISSIONED);
        }
    }
    final Map<SingularityRack, MachineState> racks = getDefaultMap(rackManager.getObjectsFiltered(MachineState.STARTING_DECOMMISSION));
    for (SingularityRack rack : racks.keySet()) {
        final String sanitizedRackId = JavaUtils.getReplaceHyphensWithUnderscores(rack.getId());
        boolean foundTask = false;
        for (SingularityTaskId activeTaskId : activeTaskIds) {
            if (sanitizedRackId.equals(activeTaskId.getSanitizedRackId())) {
                foundTask = true;
            }
            if (matchingTaskIds.contains(activeTaskId)) {
                continue;
            }
            if (sanitizedRackId.equals(activeTaskId.getSanitizedRackId())) {
                Optional<SingularityTask> maybeTask = taskManager.getTask(activeTaskId);
                cleanupTaskDueToDecomission(requestIdsToUserToReschedule, matchingTaskIds, maybeTask.get(), rack);
            }
        }
        if (!foundTask) {
            racks.put(rack, MachineState.DECOMMISSIONED);
        }
    }
    for (Entry<String, Optional<String>> requestIdAndUser : requestIdsToUserToReschedule.entrySet()) {
        final String requestId = requestIdAndUser.getKey();
        LOG.trace("Rescheduling request {} due to decomissions", requestId);
        Optional<String> maybeDeployId = deployManager.getInUseDeployId(requestId);
        if (maybeDeployId.isPresent()) {
            requestManager.addToPendingQueue(new SingularityPendingRequest(requestId, maybeDeployId.get(), start, requestIdAndUser.getValue(), PendingType.DECOMISSIONED_SLAVE_OR_RACK, Optional.<Boolean>absent(), Optional.<String>absent()));
        } else {
            LOG.warn("Not rescheduling a request ({}) because of no active deploy", requestId);
        }
    }
    changeState(slaves, slaveManager);
    changeState(racks, rackManager);
    if (slaves.isEmpty() && racks.isEmpty() && requestIdsToUserToReschedule.isEmpty() && matchingTaskIds.isEmpty()) {
        LOG.trace("Decomission check found nothing");
    } else {
        LOG.info("Found {} decomissioning slaves, {} decomissioning racks, rescheduling {} requests and scheduling {} tasks for cleanup in {}", slaves.size(), racks.size(), requestIdsToUserToReschedule.size(), matchingTaskIds.size(), JavaUtils.duration(start));
    }
}
Also used : Optional(com.google.common.base.Optional) SingularityPendingRequest(com.hubspot.singularity.SingularityPendingRequest) SingularityRack(com.hubspot.singularity.SingularityRack) SingularitySlave(com.hubspot.singularity.SingularitySlave) SingularityTask(com.hubspot.singularity.SingularityTask) SingularityTaskId(com.hubspot.singularity.SingularityTaskId) MachineState(com.hubspot.singularity.MachineState) Timed(com.codahale.metrics.annotation.Timed)

Example 64 with SingularityTaskId

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

the class S3LogResource method getServiceToPrefixes.

private Map<SingularityS3Service, Set<String>> getServiceToPrefixes(SingularityS3SearchRequest search, SingularityUser user) {
    Map<SingularityS3Service, Set<String>> servicesToPrefixes = new HashMap<>();
    if (!search.getTaskIds().isEmpty()) {
        for (String taskId : search.getTaskIds()) {
            SingularityTaskId taskIdObject = getTaskIdObject(taskId);
            String group = getRequestGroupForTask(taskIdObject, user).or(SingularityS3FormatHelper.DEFAULT_GROUP_NAME);
            Set<String> s3Buckets = getBuckets(group);
            Collection<String> prefixes = getS3PrefixesForTask(configuration.get(), taskIdObject, search.getStart(), search.getEnd(), group, user);
            for (String s3Bucket : s3Buckets) {
                SingularityS3Service s3Service = s3Services.getServiceByGroupAndBucketOrDefault(group, s3Bucket);
                if (!servicesToPrefixes.containsKey(s3Service)) {
                    servicesToPrefixes.put(s3Service, new HashSet<String>());
                }
                servicesToPrefixes.get(s3Service).addAll(prefixes);
            }
        }
    }
    if (!search.getRequestsAndDeploys().isEmpty()) {
        for (Map.Entry<String, List<String>> entry : search.getRequestsAndDeploys().entrySet()) {
            String group = getRequestGroup(entry.getKey(), user).or(SingularityS3FormatHelper.DEFAULT_GROUP_NAME);
            Set<String> s3Buckets = getBuckets(group);
            List<String> prefixes = new ArrayList<>();
            if (!entry.getValue().isEmpty()) {
                for (String deployId : entry.getValue()) {
                    prefixes.addAll(getS3PrefixesForDeploy(configuration.get(), entry.getKey(), deployId, search.getStart(), search.getEnd(), group, user));
                }
            } else {
                prefixes.addAll(getS3PrefixesForRequest(configuration.get(), entry.getKey(), search.getStart(), search.getEnd(), group));
            }
            for (String s3Bucket : s3Buckets) {
                SingularityS3Service s3Service = s3Services.getServiceByGroupAndBucketOrDefault(group, s3Bucket);
                if (!servicesToPrefixes.containsKey(s3Service)) {
                    servicesToPrefixes.put(s3Service, new HashSet<String>());
                }
                servicesToPrefixes.get(s3Service).addAll(prefixes);
            }
        }
    }
    // Trim prefixes to search. Less specific prefixes will contain all results of matching + more specific ones
    for (Map.Entry<SingularityS3Service, Set<String>> entry : servicesToPrefixes.entrySet()) {
        Set<String> results = new HashSet<>();
        boolean contains = false;
        for (String prefix : entry.getValue()) {
            for (String unique : results) {
                if (prefix.startsWith(unique) && prefix.length() > unique.length()) {
                    contains = true;
                    break;
                } else if (unique.startsWith(prefix) && unique.length() > prefix.length()) {
                    results.remove(unique);
                    results.add(prefix);
                    contains = true;
                    break;
                }
            }
            if (!contains) {
                results.add(prefix);
            }
        }
        entry.getValue().retainAll(results);
    }
    return servicesToPrefixes;
}
Also used : SingularityS3Service(com.hubspot.singularity.helpers.SingularityS3Service) Set(java.util.Set) HashSet(java.util.HashSet) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) SingularityTaskId(com.hubspot.singularity.SingularityTaskId) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) HashSet(java.util.HashSet)

Example 65 with SingularityTaskId

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

the class SandboxResource method checkHistory.

private SingularityTaskHistory checkHistory(String taskId, SingularityUser user) {
    final SingularityTaskId taskIdObj = getTaskIdObject(taskId);
    final SingularityTaskHistory taskHistory = getTaskHistoryRequired(taskIdObj, user);
    if (!taskHistory.getDirectory().isPresent()) {
        logSupport.checkDirectoryAndContainerId(taskIdObj);
        throw badRequest("Task %s does not have a directory yet - check again soon (enqueued request to refetch)", taskId);
    }
    return taskHistory;
}
Also used : SingularityTaskHistory(com.hubspot.singularity.SingularityTaskHistory) SingularityTaskId(com.hubspot.singularity.SingularityTaskId)

Aggregations

SingularityTaskId (com.hubspot.singularity.SingularityTaskId)106 Test (org.junit.Test)44 SingularityTask (com.hubspot.singularity.SingularityTask)34 SingularityRequest (com.hubspot.singularity.SingularityRequest)26 ArrayList (java.util.ArrayList)26 SingularityPendingTaskId (com.hubspot.singularity.SingularityPendingTaskId)14 SingularityTaskHistoryUpdate (com.hubspot.singularity.SingularityTaskHistoryUpdate)13 MesosTaskMonitorObject (com.hubspot.mesos.json.MesosTaskMonitorObject)12 List (java.util.List)11 SingularityRequestWithState (com.hubspot.singularity.SingularityRequestWithState)10 SingularityDeployProgress (com.hubspot.singularity.SingularityDeployProgress)9 SingularityPendingRequest (com.hubspot.singularity.SingularityPendingRequest)9 SingularityRunNowRequest (com.hubspot.singularity.api.SingularityRunNowRequest)9 SingularityDeploy (com.hubspot.singularity.SingularityDeploy)8 SingularityRequestBuilder (com.hubspot.singularity.SingularityRequestBuilder)7 SingularityTaskCleanup (com.hubspot.singularity.SingularityTaskCleanup)7 SingularityTaskShellCommandRequestId (com.hubspot.singularity.SingularityTaskShellCommandRequestId)7 HashMap (java.util.HashMap)7 SingularityRequestDeployState (com.hubspot.singularity.SingularityRequestDeployState)6 SingularitySlaveUsage (com.hubspot.singularity.SingularitySlaveUsage)6