Search in sources :

Example 31 with SingularityPendingTaskId

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

the class SingularityMesosTaskBuilderTest method testEnvironmentVariableOverrides.

@Test
public void testEnvironmentVariableOverrides() {
    Map<String, String> overrideVariables = new HashMap<>();
    overrideVariables.put("MY_NEW_ENV_VAR", "test");
    overrideVariables.put("STARTED_BY_USER", "notTestUser");
    final SingularityRequest request = new SingularityRequestBuilder("test", RequestType.WORKER).build();
    final SingularityDeploy deploy = new SingularityDeployBuilder("test", "1").setCommand(Optional.of("/bin/echo hi")).build();
    final SingularityPendingTask pendingTask = new SingularityPendingTaskBuilder().setPendingTaskId(new SingularityPendingTaskId("test", "1", 0, 1, PendingType.IMMEDIATE, 0)).setUser(user).setEnvOverrides(overrideVariables).build();
    final SingularityTaskRequest taskRequest = new SingularityTaskRequest(request, deploy, pendingTask);
    final TaskInfo task = builder.buildTask(offerHolder, null, taskRequest, taskResources, executorResources).getMesosTask();
    Map<String, String> environmentVariables = task.getCommand().getEnvironment().getVariablesList().stream().collect(Collectors.toMap(Variable::getName, Variable::getValue));
    for (String key : overrideVariables.keySet()) {
        assertEquals("Environment variable " + key + " not overridden.", environmentVariables.get(key), overrideVariables.get(key));
    }
}
Also used : TaskInfo(org.apache.mesos.v1.Protos.TaskInfo) SingularityRequestBuilder(com.hubspot.singularity.SingularityRequestBuilder) HashMap(java.util.HashMap) SingularityPendingTask(com.hubspot.singularity.SingularityPendingTask) SingularityRequest(com.hubspot.singularity.SingularityRequest) SingularityPendingTaskId(com.hubspot.singularity.SingularityPendingTaskId) SingularityDeployBuilder(com.hubspot.singularity.SingularityDeployBuilder) SingularityTaskRequest(com.hubspot.singularity.SingularityTaskRequest) SingularityDeploy(com.hubspot.singularity.SingularityDeploy) SingularityPendingTaskBuilder(com.hubspot.singularity.SingularityPendingTaskBuilder) Test(org.junit.Test)

Example 32 with SingularityPendingTaskId

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

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

the class SingularitySlaveAndRackManager method doesOfferMatch.

SlaveMatchState doesOfferMatch(SingularityOfferHolder offerHolder, SingularityTaskRequest taskRequest, List<SingularityTaskId> activeTaskIdsForRequest) {
    final String host = offerHolder.getHostname();
    final String rackId = offerHolder.getRackId();
    final String slaveId = offerHolder.getSlaveId();
    final MachineState currentSlaveState = slaveManager.getSlave(slaveId).get().getCurrentState().getState();
    if (currentSlaveState == MachineState.FROZEN) {
        return SlaveMatchState.SLAVE_FROZEN;
    }
    if (currentSlaveState.isDecommissioning()) {
        return SlaveMatchState.SLAVE_DECOMMISSIONING;
    }
    final MachineState currentRackState = rackManager.getRack(rackId).get().getCurrentState().getState();
    if (currentRackState == MachineState.FROZEN) {
        return SlaveMatchState.RACK_FROZEN;
    }
    if (currentRackState.isDecommissioning()) {
        return SlaveMatchState.RACK_DECOMMISSIONING;
    }
    if (!taskRequest.getRequest().getRackAffinity().or(Collections.emptyList()).isEmpty()) {
        if (!taskRequest.getRequest().getRackAffinity().get().contains(rackId)) {
            LOG.trace("Task {} requires a rack in {} (current rack {})", taskRequest.getPendingTask().getPendingTaskId(), taskRequest.getRequest().getRackAffinity().get(), rackId);
            return SlaveMatchState.RACK_AFFINITY_NOT_MATCHING;
        }
    }
    if (!isSlaveAttributesMatch(offerHolder, taskRequest)) {
        return SlaveMatchState.SLAVE_ATTRIBUTES_DO_NOT_MATCH;
    }
    final SlavePlacement slavePlacement = taskRequest.getRequest().getSlavePlacement().or(configuration.getDefaultSlavePlacement());
    if (!taskRequest.getRequest().isRackSensitive() && slavePlacement == SlavePlacement.GREEDY) {
        // todo: account for this or let this behavior continue?
        return SlaveMatchState.NOT_RACK_OR_SLAVE_PARTICULAR;
    }
    final int numDesiredInstances = taskRequest.getRequest().getInstancesSafe();
    boolean allowBounceToSameHost = isAllowBounceToSameHost(taskRequest.getRequest());
    Multiset<String> countPerRack = HashMultiset.create(slaveManager.getNumActive());
    double numOnSlave = 0;
    double numCleaningOnSlave = 0;
    double numFromSameBounceOnSlave = 0;
    double numOtherDeploysOnSlave = 0;
    boolean taskLaunchedFromBounceWithActionId = taskRequest.getPendingTask().getPendingTaskId().getPendingType() == PendingType.BOUNCE && taskRequest.getPendingTask().getActionId().isPresent();
    final String sanitizedHost = offerHolder.getSanitizedHost();
    final String sanitizedRackId = offerHolder.getSanitizedRackId();
    Collection<SingularityTaskId> cleaningTasks = leaderCache.getCleanupTaskIds();
    for (SingularityTaskId taskId : activeTaskIdsForRequest) {
        if (!cleaningTasks.contains(taskId) && taskRequest.getDeploy().getId().equals(taskId.getDeployId())) {
            countPerRack.add(taskId.getSanitizedRackId());
        }
        if (!taskId.getSanitizedHost().equals(sanitizedHost)) {
            continue;
        }
        if (taskRequest.getDeploy().getId().equals(taskId.getDeployId())) {
            if (cleaningTasks.contains(taskId)) {
                numCleaningOnSlave++;
            } else {
                numOnSlave++;
            }
            if (taskLaunchedFromBounceWithActionId) {
                Optional<SingularityTask> maybeTask = taskManager.getTask(taskId);
                boolean errorInTaskData = false;
                if (maybeTask.isPresent()) {
                    SingularityPendingTask pendingTask = maybeTask.get().getTaskRequest().getPendingTask();
                    if (pendingTask.getPendingTaskId().getPendingType() == PendingType.BOUNCE) {
                        if (pendingTask.getActionId().isPresent()) {
                            if (pendingTask.getActionId().get().equals(taskRequest.getPendingTask().getActionId().get())) {
                                numFromSameBounceOnSlave++;
                            }
                        } else {
                            // No actionId present on bounce, fall back to more restrictive placement strategy
                            errorInTaskData = true;
                        }
                    }
                } else {
                    // Could not find appropriate task data, fall back to more restrictive placement strategy
                    errorInTaskData = true;
                }
                if (errorInTaskData) {
                    allowBounceToSameHost = false;
                }
            }
        } else {
            numOtherDeploysOnSlave++;
        }
    }
    if (taskRequest.getRequest().isRackSensitive()) {
        final boolean isRackOk = isRackOk(countPerRack, sanitizedRackId, numDesiredInstances, taskRequest.getRequest().getId(), slaveId, host, numCleaningOnSlave, leaderCache);
        if (!isRackOk) {
            return SlaveMatchState.RACK_SATURATED;
        }
    }
    switch(slavePlacement) {
        case SEPARATE:
        case SEPARATE_BY_DEPLOY:
        case SPREAD_ALL_SLAVES:
            if (allowBounceToSameHost && taskLaunchedFromBounceWithActionId) {
                if (numFromSameBounceOnSlave > 0) {
                    LOG.trace("Rejecting SEPARATE task {} from slave {} ({}) due to numFromSameBounceOnSlave {}", taskRequest.getRequest().getId(), slaveId, host, numFromSameBounceOnSlave);
                    return SlaveMatchState.SLAVE_SATURATED;
                }
            } else {
                if (numOnSlave > 0 || numCleaningOnSlave > 0) {
                    LOG.trace("Rejecting {} task {} from slave {} ({}) due to numOnSlave {} numCleaningOnSlave {}", slavePlacement.name(), taskRequest.getRequest().getId(), slaveId, host, numOnSlave, numCleaningOnSlave);
                    return SlaveMatchState.SLAVE_SATURATED;
                }
            }
            break;
        case SEPARATE_BY_REQUEST:
            if (numOnSlave > 0 || numCleaningOnSlave > 0 || numOtherDeploysOnSlave > 0) {
                LOG.trace("Rejecting SEPARATE_BY_REQUEST task {} from slave {} ({}) due to numOnSlave {} numCleaningOnSlave {} numOtherDeploysOnSlave {}", taskRequest.getRequest().getId(), slaveId, host, numOnSlave, numCleaningOnSlave, numOtherDeploysOnSlave);
                return SlaveMatchState.SLAVE_SATURATED;
            }
            break;
        case OPTIMISTIC:
            // If no tasks are active for this request yet, we can fall back to greedy.
            if (activeTaskIdsForRequest.size() > 0) {
                Collection<SingularityPendingTaskId> pendingTasksForRequestClusterwide = leaderCache.getPendingTaskIdsForRequest(taskRequest.getRequest().getId());
                Set<String> currentHostsForRequest = activeTaskIdsForRequest.stream().map(SingularityTaskId::getSanitizedHost).collect(Collectors.toSet());
                final double numPerSlave = activeTaskIdsForRequest.size() / (double) currentHostsForRequest.size();
                final double leniencyCoefficient = configuration.getPlacementLeniency();
                final double threshold = numPerSlave * (1 + (pendingTasksForRequestClusterwide.size() * leniencyCoefficient));
                final boolean isSlaveOk = numOnSlave <= threshold;
                if (!isSlaveOk) {
                    LOG.trace("Rejecting OPTIMISTIC task {} from slave {} ({}) because numOnSlave {} violates threshold {} (based on active tasks for request {}, current hosts for request {}, pending tasks for request {})", taskRequest.getRequest().getId(), slaveId, host, numOnSlave, threshold, activeTaskIdsForRequest.size(), currentHostsForRequest.size(), pendingTasksForRequestClusterwide.size());
                    return SlaveMatchState.SLAVE_SATURATED;
                }
            }
            break;
        case GREEDY:
    }
    return SlaveMatchState.OK;
}
Also used : SingularityPendingTaskId(com.hubspot.singularity.SingularityPendingTaskId) SingularityTask(com.hubspot.singularity.SingularityTask) SingularityPendingTask(com.hubspot.singularity.SingularityPendingTask) SlavePlacement(com.hubspot.singularity.SlavePlacement) SingularityTaskId(com.hubspot.singularity.SingularityTaskId) MachineState(com.hubspot.singularity.MachineState)

Example 34 with SingularityPendingTaskId

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

the class SingularityStartup method checkActiveRequest.

private void checkActiveRequest(SingularityRequestWithState requestWithState, Map<SingularityDeployKey, SingularityPendingTaskId> deployKeyToPendingTaskId, final long timestamp) {
    final SingularityRequest request = requestWithState.getRequest();
    if (request.getRequestType() == RequestType.ON_DEMAND || request.getRequestType() == RequestType.RUN_ONCE) {
        // There's no situation where we'd want to schedule an On Demand or Run Once request at startup, so don't even bother with them.
        return;
    }
    Optional<SingularityRequestDeployState> requestDeployState = deployManager.getRequestDeployState(request.getId());
    if (!requestDeployState.isPresent() || !requestDeployState.get().getActiveDeploy().isPresent()) {
        LOG.debug("No active deploy for {} - not scheduling on startup", request.getId());
        return;
    }
    final String activeDeployId = requestDeployState.get().getActiveDeploy().get().getDeployId();
    if (request.isScheduled()) {
        SingularityDeployKey deployKey = new SingularityDeployKey(request.getId(), activeDeployId);
        SingularityPendingTaskId pendingTaskId = deployKeyToPendingTaskId.get(deployKey);
        if (pendingTaskId != null && pendingTaskId.getCreatedAt() >= requestWithState.getTimestamp()) {
            LOG.info("Not rescheduling {} because {} is newer than {}", request.getId(), pendingTaskId, requestWithState.getTimestamp());
            return;
        }
    }
    requestManager.addToPendingQueue(new SingularityPendingRequest(request.getId(), activeDeployId, timestamp, Optional.<String>absent(), PendingType.STARTUP, Optional.<Boolean>absent(), Optional.<String>absent()));
}
Also used : SingularityDeployKey(com.hubspot.singularity.SingularityDeployKey) SingularityPendingRequest(com.hubspot.singularity.SingularityPendingRequest) SingularityRequest(com.hubspot.singularity.SingularityRequest) SingularityPendingTaskId(com.hubspot.singularity.SingularityPendingTaskId) SingularityRequestDeployState(com.hubspot.singularity.SingularityRequestDeployState)

Example 35 with SingularityPendingTaskId

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

the class SingularityMesosOfferScheduler method score.

private double score(SingularityOfferHolder offerHolder, Map<String, Integer> tasksPerOffer, SingularityTaskRequestHolder taskRequestHolder, Optional<SingularitySlaveUsageWithCalculatedScores> maybeSlaveUsage, List<SingularityTaskId> activeTaskIdsForRequest) {
    final SingularityTaskRequest taskRequest = taskRequestHolder.getTaskRequest();
    final SingularityPendingTaskId pendingTaskId = taskRequest.getPendingTask().getPendingTaskId();
    if (tooManyTasksPerOfferHostForRequest(tasksPerOffer, offerHolder.getHostname(), taskRequestHolder.getTaskRequest())) {
        LOG.debug("Skipping task request for request id {}, too many tasks already scheduled using offer {}", taskRequest.getRequest().getId(), offerHolder.getHostname());
        return 0;
    }
    if (isTooManyInstancesForRequest(taskRequest, activeTaskIdsForRequest)) {
        LOG.debug("Skipping pending task {}, too many instances already running", pendingTaskId);
        return 0;
    }
    if (LOG.isTraceEnabled()) {
        LOG.trace("Attempting to match task {} resources {} with required role '{}' ({} for task + {} for executor) with remaining offer resources {}", pendingTaskId, taskRequestHolder.getTotalResources(), taskRequest.getRequest().getRequiredRole().or("*"), taskRequestHolder.getTaskResources(), taskRequestHolder.getExecutorResources(), MesosUtils.formatForLogging(offerHolder.getCurrentResources()));
    }
    final boolean matchesResources = MesosUtils.doesOfferMatchResources(taskRequest.getRequest().getRequiredRole(), taskRequestHolder.getTotalResources(), offerHolder.getCurrentResources(), taskRequestHolder.getRequestedPorts());
    if (!matchesResources) {
        return 0;
    }
    final SlaveMatchState slaveMatchState = slaveAndRackManager.doesOfferMatch(offerHolder, taskRequest, activeTaskIdsForRequest);
    if (slaveMatchState.isMatchAllowed()) {
        return score(offerHolder.getHostname(), taskRequest, maybeSlaveUsage);
    } else if (LOG.isTraceEnabled()) {
        LOG.trace("Ignoring offer on host {} with roles {} on {} for task {}; matched resources: {}, slave match state: {}", offerHolder.getHostname(), offerHolder.getRoles(), offerHolder.getHostname(), pendingTaskId, matchesResources, slaveMatchState);
    }
    return 0;
}
Also used : SingularityPendingTaskId(com.hubspot.singularity.SingularityPendingTaskId) SingularityTaskRequest(com.hubspot.singularity.SingularityTaskRequest) SlaveMatchState(com.hubspot.singularity.SlaveMatchState)

Aggregations

SingularityPendingTaskId (com.hubspot.singularity.SingularityPendingTaskId)36 SingularityRequest (com.hubspot.singularity.SingularityRequest)16 Test (org.junit.Test)16 SingularityTaskId (com.hubspot.singularity.SingularityTaskId)15 SingularityRequestBuilder (com.hubspot.singularity.SingularityRequestBuilder)9 SingularityPendingTask (com.hubspot.singularity.SingularityPendingTask)8 SingularityRunNowRequest (com.hubspot.singularity.api.SingularityRunNowRequest)8 SingularityPendingRequest (com.hubspot.singularity.SingularityPendingRequest)7 SingularityPendingTaskBuilder (com.hubspot.singularity.SingularityPendingTaskBuilder)7 SingularityDeploy (com.hubspot.singularity.SingularityDeploy)6 SingularityTask (com.hubspot.singularity.SingularityTask)6 SingularityRequestWithState (com.hubspot.singularity.SingularityRequestWithState)5 SingularityDeployKey (com.hubspot.singularity.SingularityDeployKey)4 PendingType (com.hubspot.singularity.SingularityPendingRequest.PendingType)4 SingularityRequestDeployState (com.hubspot.singularity.SingularityRequestDeployState)4 Optional (com.google.common.base.Optional)3 Inject (com.google.inject.Inject)3 RequestType (com.hubspot.singularity.RequestType)3 SingularityDeployBuilder (com.hubspot.singularity.SingularityDeployBuilder)3 ArrayList (java.util.ArrayList)3