Search in sources :

Example 1 with Task

use of org.apache.mesos.v1.scheduler.Protos.Call.Reconcile.Task in project Singularity by HubSpot.

the class SingularityScheduler method deleteScheduledTasks.

private void deleteScheduledTasks(final Collection<SingularityPendingTask> scheduledTasks, SingularityPendingRequest pendingRequest) {
    List<SingularityPendingTask> tasksForDeploy = scheduledTasks.stream().filter(task -> pendingRequest.getRequestId().equals(task.getPendingTaskId().getRequestId())).filter(task -> pendingRequest.getDeployId().equals(task.getPendingTaskId().getDeployId())).collect(Collectors.toList());
    for (SingularityPendingTask task : tasksForDeploy) {
        LOG.debug("Deleting pending task {} in order to reschedule {}", task.getPendingTaskId().getId(), pendingRequest);
        taskManager.deletePendingTask(task.getPendingTaskId());
    }
}
Also used : SingularityTask(com.hubspot.singularity.SingularityTask) DeployManager(com.hubspot.singularity.data.DeployManager) ExtendedTaskState(com.hubspot.singularity.ExtendedTaskState) TaskFailureType(com.hubspot.singularity.TaskFailureType) Date(java.util.Date) Inject(com.google.inject.Inject) SingularityAgent(com.hubspot.singularity.SingularityAgent) 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) TaskFailureEvent(com.hubspot.singularity.TaskFailureEvent) SingularityMesosSchedulerClient(com.hubspot.singularity.mesos.SingularityMesosSchedulerClient) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) Resources(com.hubspot.mesos.Resources) TaskManager(com.hubspot.singularity.data.TaskManager) ParseException(java.text.ParseException) SingularityPendingTaskId(com.hubspot.singularity.SingularityPendingTaskId) SingularityTaskId(com.hubspot.singularity.SingularityTaskId) TimeZone(java.util.TimeZone) SingularityKilledTaskIdRecord(com.hubspot.singularity.SingularityKilledTaskIdRecord) Collection(java.util.Collection) Task(org.apache.mesos.v1.scheduler.Protos.Call.Reconcile.Task) CompletableFutures(com.hubspot.singularity.async.CompletableFutures) 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) JavaUtils(com.hubspot.mesos.JavaUtils) List(java.util.List) AgentManager(com.hubspot.singularity.data.AgentManager) RFC5545Schedule(com.hubspot.singularity.helpers.RFC5545Schedule) Stream(java.util.stream.Stream) SingularityMachineAbstraction(com.hubspot.singularity.SingularityMachineAbstraction) ScheduleType(com.hubspot.singularity.ScheduleType) Entry(java.util.Map.Entry) RackManager(com.hubspot.singularity.data.RackManager) SingularitySchedulerLock(com.hubspot.singularity.mesos.SingularitySchedulerLock) Optional(java.util.Optional) 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) SingularityPendingRequest(com.hubspot.singularity.SingularityPendingRequest) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Singleton(javax.inject.Singleton) AbstractMachineManager(com.hubspot.singularity.data.AbstractMachineManager) ArrayList(java.util.ArrayList) Meter(com.codahale.metrics.Meter) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) SingularityDeployKey(com.hubspot.singularity.SingularityDeployKey) RequestType(com.hubspot.singularity.RequestType) ExecutorService(java.util.concurrent.ExecutorService) SingularityConfiguration(com.hubspot.singularity.config.SingularityConfiguration) RebalancingHelper(com.hubspot.singularity.helpers.RebalancingHelper) SingularityTaskCleanup(com.hubspot.singularity.SingularityTaskCleanup) Logger(org.slf4j.Logger) Reason(org.apache.mesos.v1.Protos.TaskStatus.Reason) AgentID(org.apache.mesos.v1.Protos.AgentID) SingularityPendingDeploy(com.hubspot.singularity.SingularityPendingDeploy) SingularityRack(com.hubspot.singularity.SingularityRack) Maps(com.google.common.collect.Maps) SingularityDeployMarker(com.hubspot.singularity.SingularityDeployMarker) TimeUnit(java.util.concurrent.TimeUnit) 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) SingularityManagedThreadPoolFactory(com.hubspot.singularity.SingularityManagedThreadPoolFactory) TaskID(org.apache.mesos.v1.Protos.TaskID) Named(com.google.inject.name.Named) Comparator(java.util.Comparator) Collections(java.util.Collections) SingularityMainModule(com.hubspot.singularity.SingularityMainModule) SingularityPendingTask(com.hubspot.singularity.SingularityPendingTask)

Example 2 with Task

use of org.apache.mesos.v1.scheduler.Protos.Call.Reconcile.Task in project Singularity by HubSpot.

the class SingularityMesosTaskBuilder method prepareEnvironment.

private void prepareEnvironment(final SingularityTaskRequest task, SingularityTaskId taskId, CommandInfo.Builder commandBuilder, final SingularityOfferHolder offerHolder, final Optional<long[]> ports) {
    Map<String, Object> envVars = new HashMap<>();
    envVars.put("INSTANCE_NO", task.getPendingTask().getPendingTaskId().getInstanceNo());
    envVars.put("TASK_HOST", offerHolder.getHostname());
    envVars.put("TASK_RACK_ID", offerHolder.getRackId());
    envVars.put("AVAILABILITY_ZONE", offerHolder.getRackId());
    envVars.put("TASK_REQUEST_ID", task.getPendingTask().getPendingTaskId().getRequestId());
    envVars.put("TASK_DEPLOY_ID", taskId.getDeployId());
    envVars.put("TASK_ID", taskId.getId());
    envVars.put("ESTIMATED_INSTANCE_COUNT", task.getRequest().getInstancesSafe());
    for (Entry<String, String> envEntry : task.getDeploy().getEnv().orElse(Collections.<String, String>emptyMap()).entrySet()) {
        envVars.put(envEntry.getKey(), fillInTaskIdValues(envEntry.getValue(), offerHolder, taskId));
    }
    if (task.getDeploy().getTaskEnv().isPresent() && task.getDeploy().getTaskEnv().get().containsKey(taskId.getInstanceNo()) && !task.getDeploy().getTaskEnv().get().get(taskId.getInstanceNo()).isEmpty()) {
        for (Entry<String, String> envEntry : task.getDeploy().getTaskEnv().get().get(taskId.getInstanceNo()).entrySet()) {
            envVars.put(envEntry.getKey(), fillInTaskIdValues(envEntry.getValue(), offerHolder, taskId));
        }
    }
    if (ports.isPresent()) {
        for (int portNum = 0; portNum < ports.get().length; portNum++) {
            if (portNum == 0) {
                envVars.put("PORT", ports.get()[portNum]);
            }
            envVars.put(String.format("PORT%s", portNum), ports.get()[portNum]);
        }
    }
    if (task.getPendingTask().getResources().isPresent()) {
        Resources override = task.getPendingTask().getResources().get();
        if (override.getCpus() != 0) {
            envVars.put("DEPLOY_CPUS", ((long) override.getCpus()));
        }
        if (override.getMemoryMb() != 0) {
            envVars.put("DEPLOY_MEM", ((long) override.getMemoryMb()));
        }
    }
    for (Entry entry : task.getPendingTask().getEnvOverrides().entrySet()) {
        envVars.put(entry.getKey().toString(), entry.getValue());
    }
    // Set this last so it cannot be overridden by the user
    if (task.getPendingTask().getUser().isPresent()) {
        envVars.put("STARTED_BY_USER", task.getPendingTask().getUser().get());
    }
    Environment.Builder envBldr = Environment.newBuilder();
    for (Entry entry : envVars.entrySet()) {
        setEnv(envBldr, entry.getKey().toString(), entry.getValue());
    }
    commandBuilder.setEnvironment(envBldr.build());
}
Also used : Entry(java.util.Map.Entry) HashMap(java.util.HashMap) Environment(org.apache.mesos.v1.Protos.Environment) ByteString(com.google.protobuf.ByteString) Resources(com.hubspot.mesos.Resources)

Example 3 with Task

use of org.apache.mesos.v1.scheduler.Protos.Call.Reconcile.Task in project Singularity by HubSpot.

the class SingularityMesosTaskBuilder method buildTask.

public SingularityMesosTaskHolder buildTask(SingularityOfferHolder offerHolder, List<Resource> availableResources, SingularityTaskRequest taskRequest, Resources desiredTaskResources, Resources desiredExecutorResources) {
    final String sanitizedRackId = offerHolder.getSanitizedRackId();
    final String sanitizedHost = offerHolder.getSanitizedHost();
    final SingularityTaskId taskId = new SingularityTaskId(taskRequest.getPendingTask().getPendingTaskId().getRequestId(), taskRequest.getDeploy().getId(), System.currentTimeMillis(), taskRequest.getPendingTask().getPendingTaskId().getInstanceNo(), sanitizedHost, sanitizedRackId);
    final TaskInfo.Builder bldr = TaskInfo.newBuilder().setTaskId(TaskID.newBuilder().setValue(taskId.toString()));
    Optional<long[]> ports = Optional.empty();
    Optional<Resource> portsResource = Optional.empty();
    final Optional<SingularityContainerInfo> containerInfo = taskRequest.getDeploy().getContainerInfo();
    if (desiredTaskResources.getNumPorts() > 0 || hasLiteralPortMapping(containerInfo)) {
        List<Long> requestedPorts = new ArrayList<>();
        if (hasLiteralPortMapping(containerInfo)) {
            requestedPorts.addAll(containerInfo.get().getDocker().get().getLiteralHostPorts());
        }
        portsResource = Optional.of(MesosUtils.getPortsResource(desiredTaskResources.getNumPorts(), availableResources, requestedPorts));
        ports = Optional.of(MesosUtils.getPorts(portsResource.get(), desiredTaskResources.getNumPorts()));
    }
    if (containerInfo.isPresent()) {
        prepareContainerInfo(offerHolder, taskId, bldr, containerInfo.get(), ports);
    }
    if (taskRequest.getDeploy().getCustomExecutorCmd().isPresent()) {
        prepareCustomExecutor(bldr, taskId, taskRequest, offerHolder, ports, desiredExecutorResources);
    } else {
        prepareCommand(bldr, taskId, taskRequest, offerHolder, ports);
    }
    if (portsResource.isPresent()) {
        bldr.addResources(portsResource.get());
    }
    Optional<String> requiredRole = taskRequest.getRequest().getRequiredRole();
    bldr.addResources(MesosUtils.getCpuResource(desiredTaskResources.getCpus(), requiredRole));
    bldr.addResources(MesosUtils.getMemoryResource(desiredTaskResources.getMemoryMb(), requiredRole));
    bldr.addResources(MesosUtils.getDiskResource(desiredTaskResources.getDiskMb(), requiredRole));
    bldr.setAgentId(offerHolder.getOffers().get(0).getAgentId());
    bldr.setName(taskRequest.getRequest().getId());
    final Builder labelsBuilder = Labels.newBuilder();
    // apply request-specific labels, if any
    if (taskRequest.getDeploy().getMesosLabels().isPresent() && !taskRequest.getDeploy().getMesosLabels().get().isEmpty()) {
        for (SingularityMesosTaskLabel label : taskRequest.getDeploy().getMesosLabels().get()) {
            org.apache.mesos.v1.Protos.Label.Builder labelBuilder = Label.newBuilder();
            labelBuilder.setKey(label.getKey());
            if ((label.getValue().isPresent())) {
                labelBuilder.setValue(label.getValue().get());
            }
            labelsBuilder.addLabels(labelBuilder.build());
        }
    }
    // apply task-specific labels, if any
    final int taskInstanceNo = taskRequest.getPendingTask().getPendingTaskId().getInstanceNo();
    if (taskRequest.getDeploy().getMesosTaskLabels().isPresent() && taskRequest.getDeploy().getMesosTaskLabels().get().containsKey(taskInstanceNo) && !taskRequest.getDeploy().getMesosTaskLabels().get().get(taskInstanceNo).isEmpty()) {
        for (SingularityMesosTaskLabel label : taskRequest.getDeploy().getMesosTaskLabels().get().get(taskInstanceNo)) {
            org.apache.mesos.v1.Protos.Label.Builder labelBuilder = Label.newBuilder();
            labelBuilder.setKey(label.getKey());
            if ((label.getValue().isPresent())) {
                labelBuilder.setValue(label.getValue().get());
            }
            labelsBuilder.addLabels(labelBuilder.build());
        }
    }
    bldr.setLabels(labelsBuilder);
    TaskInfo task = bldr.build();
    return new SingularityMesosTaskHolder(new SingularityTask(taskRequest, taskId, offerHolder.getOffers().stream().map(o -> mesosProtosUtils.offerFromProtos(o)).collect(Collectors.toList()), mesosProtosUtils.taskFromProtos(task), Optional.of(offerHolder.getRackId())), task);
}
Also used : SingularityTask(com.hubspot.singularity.SingularityTask) ExecutorInfo(org.apache.mesos.v1.Protos.ExecutorInfo) Parameter(org.apache.mesos.v1.Protos.Parameter) Inject(com.google.inject.Inject) LoggerFactory(org.slf4j.LoggerFactory) Labels(org.apache.mesos.v1.Protos.Labels) SingularityContainerInfo(com.hubspot.mesos.SingularityContainerInfo) SingularityMesosImage(com.hubspot.mesos.SingularityMesosImage) CommandInfo(org.apache.mesos.v1.Protos.CommandInfo) Map(java.util.Map) SingularityDockerInfo(com.hubspot.mesos.SingularityDockerInfo) MesosProtosUtils(com.hubspot.singularity.helpers.MesosProtosUtils) NetworkInfo(org.apache.mesos.v1.Protos.NetworkInfo) Variable(org.apache.mesos.v1.Protos.Environment.Variable) Resources(com.hubspot.mesos.Resources) SingularityDockerNetworkType(com.hubspot.mesos.SingularityDockerNetworkType) SingularityPortMapping(com.hubspot.mesos.SingularityPortMapping) SingularityTaskId(com.hubspot.singularity.SingularityTaskId) SingularityDockerVolume(com.hubspot.mesos.SingularityDockerVolume) SingularityVolumeSource(com.hubspot.mesos.SingularityVolumeSource) Singularity(com.hubspot.singularity.Singularity) Collectors(java.util.stream.Collectors) MesosInfo(org.apache.mesos.v1.Protos.ContainerInfo.MesosInfo) ExecutorID(org.apache.mesos.v1.Protos.ExecutorID) ExecutorDataBuilder(com.hubspot.deploy.ExecutorDataBuilder) ByteString(com.google.protobuf.ByteString) List(java.util.List) MesosUtils(com.hubspot.singularity.helpers.MesosUtils) SingularityDockerImage(com.hubspot.mesos.SingularityDockerImage) SingularityMesosInfo(com.hubspot.mesos.SingularityMesosInfo) Entry(java.util.Map.Entry) Optional(java.util.Optional) ContainerInfo(org.apache.mesos.v1.Protos.ContainerInfo) Image(org.apache.mesos.v1.Protos.Image) DockerInfo(org.apache.mesos.v1.Protos.ContainerInfo.DockerInfo) Supplier(com.google.common.base.Supplier) SingularityTaskExecutorData(com.hubspot.singularity.SingularityTaskExecutorData) HashMap(java.util.HashMap) Singleton(javax.inject.Singleton) ArrayList(java.util.ArrayList) Resource(org.apache.mesos.v1.Protos.Resource) Strings(com.google.common.base.Strings) SingularityDockerPortMapping(com.hubspot.mesos.SingularityDockerPortMapping) SingularityS3UploaderFile(com.hubspot.singularity.SingularityS3UploaderFile) ImmutableList(com.google.common.collect.ImmutableList) ExecutorIdGenerator(com.hubspot.singularity.data.ExecutorIdGenerator) Volume(org.apache.mesos.v1.Protos.Volume) URI(org.apache.mesos.v1.Protos.CommandInfo.URI) SingularityAppcImage(com.hubspot.mesos.SingularityAppcImage) SingularityConfiguration(com.hubspot.singularity.config.SingularityConfiguration) SingularityNetworkInfo(com.hubspot.mesos.SingularityNetworkInfo) Logger(org.slf4j.Logger) SingularityDockerParameter(com.hubspot.mesos.SingularityDockerParameter) Parameters(org.apache.mesos.v1.Protos.Parameters) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Builder(org.apache.mesos.v1.Protos.Labels.Builder) HealthcheckOptions(com.hubspot.deploy.HealthcheckOptions) Ints(com.google.common.primitives.Ints) SingularityMesosTaskHolder(com.hubspot.singularity.helpers.SingularityMesosTaskHolder) Environment(org.apache.mesos.v1.Protos.Environment) SingularityMesosArtifact(com.hubspot.mesos.SingularityMesosArtifact) SingularityTaskRequest(com.hubspot.singularity.SingularityTaskRequest) SingularityVolume(com.hubspot.mesos.SingularityVolume) TaskID(org.apache.mesos.v1.Protos.TaskID) TaskInfo(org.apache.mesos.v1.Protos.TaskInfo) Collections(java.util.Collections) SingularityMesosTaskLabel(com.hubspot.mesos.SingularityMesosTaskLabel) Label(org.apache.mesos.v1.Protos.Label) ExecutorDataBuilder(com.hubspot.deploy.ExecutorDataBuilder) Builder(org.apache.mesos.v1.Protos.Labels.Builder) Resource(org.apache.mesos.v1.Protos.Resource) ArrayList(java.util.ArrayList) SingularityMesosTaskLabel(com.hubspot.mesos.SingularityMesosTaskLabel) Label(org.apache.mesos.v1.Protos.Label) SingularityMesosTaskHolder(com.hubspot.singularity.helpers.SingularityMesosTaskHolder) ByteString(com.google.protobuf.ByteString) TaskInfo(org.apache.mesos.v1.Protos.TaskInfo) SingularityContainerInfo(com.hubspot.mesos.SingularityContainerInfo) SingularityTask(com.hubspot.singularity.SingularityTask) SingularityTaskId(com.hubspot.singularity.SingularityTaskId) SingularityMesosTaskLabel(com.hubspot.mesos.SingularityMesosTaskLabel)

Example 4 with Task

use of org.apache.mesos.v1.scheduler.Protos.Call.Reconcile.Task in project Singularity by HubSpot.

the class SingularityMesosOfferScheduler method checkOffers.

Collection<SingularityOfferHolder> checkOffers(final Map<String, Offer> offers, long start) {
    if (offers.isEmpty()) {
        LOG.debug("No offers to check");
        return Collections.emptyList();
    }
    final List<SingularityTaskRequestHolder> sortedTaskRequestHolders = getSortedDueTaskRequests();
    final int numDueTasks = sortedTaskRequestHolders.size();
    final Map<String, SingularityOfferHolder> offerHolders = offers.values().stream().collect(Collectors.groupingBy(o -> o.getAgentId().getValue())).entrySet().stream().filter(e -> e.getValue().size() > 0).map(e -> {
        List<Offer> offersList = e.getValue();
        String agentId = e.getKey();
        return new SingularityOfferHolder(offersList, numDueTasks, agentAndRackHelper.getRackIdOrDefault(offersList.get(0)), agentId, offersList.get(0).getHostname(), agentAndRackHelper.getTextAttributes(offersList.get(0)), agentAndRackHelper.getReservedAgentAttributes(offersList.get(0)));
    }).collect(Collectors.toMap(SingularityOfferHolder::getAgentId, Function.identity()));
    if (sortedTaskRequestHolders.isEmpty()) {
        return offerHolders.values();
    }
    final AtomicInteger tasksScheduled = new AtomicInteger(0);
    Map<String, RequestUtilization> requestUtilizations = usageManager.getRequestUtilizations(false);
    List<SingularityTaskId> activeTaskIds = taskManager.getActiveTaskIds();
    Map<String, SingularityAgentUsageWithId> currentUsages = usageManager.getAllCurrentAgentUsage();
    List<CompletableFuture<Void>> currentUsagesFutures = new ArrayList<>();
    for (SingularityOfferHolder offerHolder : offerHolders.values()) {
        currentUsagesFutures.add(runAsync(() -> {
            String agentId = offerHolder.getAgentId();
            Optional<SingularityAgentUsageWithId> maybeUsage = Optional.ofNullable(currentUsages.get(agentId));
            if (configuration.isReCheckMetricsForLargeNewTaskCount() && maybeUsage.isPresent()) {
                long newTaskCount = taskManager.getActiveTaskIds().stream().filter(t -> t.getStartedAt() > maybeUsage.get().getTimestamp() && t.getSanitizedHost().equals(offerHolder.getSanitizedHost())).count();
                if (newTaskCount >= maybeUsage.get().getNumTasks() / 2) {
                    try {
                        MesosAgentMetricsSnapshotObject metricsSnapshot = usageHelper.getMetricsSnapshot(offerHolder.getHostname());
                        if (metricsSnapshot.getSystemLoad5Min() / metricsSnapshot.getSystemCpusTotal() > mesosConfiguration.getRecheckMetricsLoad1Threshold() || metricsSnapshot.getSystemLoad1Min() / metricsSnapshot.getSystemCpusTotal() > mesosConfiguration.getRecheckMetricsLoad5Threshold()) {
                            // Come back to this agent after we have collected more metrics
                            LOG.info("Skipping evaluation of {} until new metrics are collected. Current load is load1: {}, load5: {}", offerHolder.getHostname(), metricsSnapshot.getSystemLoad1Min(), metricsSnapshot.getSystemLoad5Min());
                            currentUsages.remove(agentId);
                        }
                    } catch (Throwable t) {
                        LOG.warn("Could not check metrics for host {}, skipping", offerHolder.getHostname());
                        currentUsages.remove(agentId);
                    }
                }
            }
        }));
    }
    CompletableFutures.allOf(currentUsagesFutures).join();
    List<CompletableFuture<Void>> usagesWithScoresFutures = new ArrayList<>();
    Map<String, SingularityAgentUsageWithCalculatedScores> currentUsagesById = new ConcurrentHashMap<>();
    for (SingularityAgentUsageWithId usage : currentUsages.values()) {
        if (offerHolders.containsKey(usage.getAgentId())) {
            usagesWithScoresFutures.add(runAsync(() -> currentUsagesById.put(usage.getAgentId(), new SingularityAgentUsageWithCalculatedScores(usage, mesosConfiguration.getScoreUsingSystemLoad(), getMaxProbableUsageForAgent(activeTaskIds, requestUtilizations, offerHolders.get(usage.getAgentId()).getSanitizedHost()), mesosConfiguration.getLoad5OverloadedThreshold(), mesosConfiguration.getLoad1OverloadedThreshold(), usage.getTimestamp()))));
        }
    }
    CompletableFutures.allOf(usagesWithScoresFutures).join();
    long startCheck = System.currentTimeMillis();
    LOG.debug("Found agent usages and scores after {}ms", startCheck - start);
    Map<SingularityDeployKey, Optional<SingularityDeployStatistics>> deployStatsCache = new ConcurrentHashMap<>();
    Set<String> overloadedHosts = Sets.newConcurrentHashSet();
    AtomicInteger noMatches = new AtomicInteger();
    // We spend much of the offer check loop for request level locks. Wait for the locks in parallel, but ensure that actual offer checks
    // are done in serial to not over commit a single offer
    ReentrantLock offerCheckTempLock = new ReentrantLock(false);
    CompletableFutures.allOf(sortedTaskRequestHolders.stream().collect(Collectors.groupingBy(t -> t.getTaskRequest().getRequest().getId())).entrySet().stream().map(entry -> runAsync(() -> {
        lock.tryRunWithRequestLock(() -> {
            offerCheckTempLock.lock();
            try {
                long startRequest = System.currentTimeMillis();
                int evaluated = 0;
                for (SingularityTaskRequestHolder taskRequestHolder : entry.getValue()) {
                    long now = System.currentTimeMillis();
                    boolean isOfferLoopTakingTooLong = now - startCheck > mesosConfiguration.getOfferLoopTimeoutMillis();
                    boolean isRequestInOfferLoopTakingTooLong = (now - startRequest > mesosConfiguration.getOfferLoopRequestTimeoutMillis() && evaluated > 1);
                    if (isOfferLoopTakingTooLong || isRequestInOfferLoopTakingTooLong) {
                        LOG.warn("{} is holding the offer lock for too long, skipping remaining {} tasks for scheduling", taskRequestHolder.getTaskRequest().getRequest().getId(), entry.getValue().size() - evaluated);
                        break;
                    }
                    evaluated++;
                    List<SingularityTaskId> activeTaskIdsForRequest = leaderCache.getActiveTaskIdsForRequest(taskRequestHolder.getTaskRequest().getRequest().getId());
                    if (isTooManyInstancesForRequest(taskRequestHolder.getTaskRequest(), activeTaskIdsForRequest)) {
                        LOG.debug("Skipping pending task {}, too many instances already running", taskRequestHolder.getTaskRequest().getPendingTask().getPendingTaskId());
                        continue;
                    }
                    Map<String, Double> scorePerOffer = new ConcurrentHashMap<>();
                    for (SingularityOfferHolder offerHolder : offerHolders.values()) {
                        if (!isOfferFull(offerHolder)) {
                            if (calculateScore(requestUtilizations, currentUsagesById, taskRequestHolder, scorePerOffer, activeTaskIdsForRequest, offerHolder, deployStatsCache, overloadedHosts) > mesosConfiguration.getGoodEnoughScoreThreshold()) {
                                break;
                            }
                        }
                    }
                    if (!scorePerOffer.isEmpty()) {
                        SingularityOfferHolder bestOffer = offerHolders.get(Collections.max(scorePerOffer.entrySet(), Map.Entry.comparingByValue()).getKey());
                        LOG.info("Best offer {}/1 is on {}", scorePerOffer.get(bestOffer.getAgentId()), bestOffer.getSanitizedHost());
                        acceptTask(bestOffer, taskRequestHolder);
                        tasksScheduled.getAndIncrement();
                        updateAgentUsageScores(taskRequestHolder, currentUsagesById, bestOffer.getAgentId(), requestUtilizations);
                    } else {
                        noMatches.getAndIncrement();
                    }
                }
            } finally {
                offerCheckTempLock.unlock();
            }
        }, entry.getKey(), String.format("%s#%s", getClass().getSimpleName(), "checkOffers"), mesosConfiguration.getOfferLoopRequestTimeoutMillis(), TimeUnit.MILLISECONDS);
    })).collect(Collectors.toList())).join();
    LOG.info("{} tasks scheduled, {} tasks remaining after examining {} offers ({} overloaded hosts, {} had no offer matches)", tasksScheduled, numDueTasks - tasksScheduled.get(), offers.size(), overloadedHosts.size(), noMatches.get());
    return offerHolders.values();
}
Also used : CachedOffer(com.hubspot.singularity.mesos.SingularityOfferCache.CachedOffer) SingularityTask(com.hubspot.singularity.SingularityTask) DeployManager(com.hubspot.singularity.data.DeployManager) MaxProbableUsage(com.hubspot.singularity.mesos.SingularityAgentUsageWithCalculatedScores.MaxProbableUsage) Inject(com.google.inject.Inject) LoggerFactory(org.slf4j.LoggerFactory) Offer(org.apache.mesos.v1.Protos.Offer) SingularityDeployStatistics(com.hubspot.singularity.SingularityDeployStatistics) SingularityAgentUsage(com.hubspot.singularity.SingularityAgentUsage) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) Resources(com.hubspot.mesos.Resources) TaskManager(com.hubspot.singularity.data.TaskManager) SingularityUsageHelper(com.hubspot.singularity.scheduler.SingularityUsageHelper) SingularityPendingTaskId(com.hubspot.singularity.SingularityPendingTaskId) SingularityTaskId(com.hubspot.singularity.SingularityTaskId) CustomExecutorConfiguration(com.hubspot.singularity.config.CustomExecutorConfiguration) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) CompletableFutures(com.hubspot.singularity.async.CompletableFutures) Set(java.util.Set) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) SingularityAgentUsageWithId(com.hubspot.singularity.SingularityAgentUsageWithId) JavaUtils(com.hubspot.mesos.JavaUtils) List(java.util.List) MesosUtils(com.hubspot.singularity.helpers.MesosUtils) RequestUtilization(com.hubspot.singularity.RequestUtilization) UsageManager(com.hubspot.singularity.data.usage.UsageManager) Optional(java.util.Optional) CheckResult(com.hubspot.singularity.mesos.SingularityAgentAndRackManager.CheckResult) AgentMatchState(com.hubspot.singularity.AgentMatchState) SingularityLeaderCache(com.hubspot.singularity.scheduler.SingularityLeaderCache) DisasterManager(com.hubspot.singularity.data.DisasterManager) MesosAgentMetricsSnapshotObject(com.hubspot.mesos.json.MesosAgentMetricsSnapshotObject) SingularityScheduler(com.hubspot.singularity.scheduler.SingularityScheduler) CompletableFuture(java.util.concurrent.CompletableFuture) Singleton(javax.inject.Singleton) Function(java.util.function.Function) ArrayList(java.util.ArrayList) SingularityDeployKey(com.hubspot.singularity.SingularityDeployKey) RequestType(com.hubspot.singularity.RequestType) ExecutorService(java.util.concurrent.ExecutorService) SingularityConfiguration(com.hubspot.singularity.config.SingularityConfiguration) SingularityAction(com.hubspot.singularity.SingularityAction) Logger(org.slf4j.Logger) ReentrantLock(java.util.concurrent.locks.ReentrantLock) OfferID(org.apache.mesos.v1.Protos.OfferID) SingularityMesosTaskHolder(com.hubspot.singularity.helpers.SingularityMesosTaskHolder) TimeUnit(java.util.concurrent.TimeUnit) SingularityTaskRequest(com.hubspot.singularity.SingularityTaskRequest) SingularityManagedThreadPoolFactory(com.hubspot.singularity.SingularityManagedThreadPoolFactory) VisibleForTesting(com.google.common.annotations.VisibleForTesting) MesosConfiguration(com.hubspot.singularity.config.MesosConfiguration) Collections(java.util.Collections) RequestUtilization(com.hubspot.singularity.RequestUtilization) ArrayList(java.util.ArrayList) CompletableFuture(java.util.concurrent.CompletableFuture) List(java.util.List) ArrayList(java.util.ArrayList) MesosAgentMetricsSnapshotObject(com.hubspot.mesos.json.MesosAgentMetricsSnapshotObject) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SingularityTaskId(com.hubspot.singularity.SingularityTaskId) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Optional(java.util.Optional) SingularityDeployKey(com.hubspot.singularity.SingularityDeployKey) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SingularityAgentUsageWithId(com.hubspot.singularity.SingularityAgentUsageWithId)

Example 5 with Task

use of org.apache.mesos.v1.scheduler.Protos.Call.Reconcile.Task in project Singularity by HubSpot.

the class SingularityMesosSchedulerClient method accept.

/**
 * Sent by the scheduler when it accepts offer(s) sent by the master. The ACCEPT request includes the type of
 * operations (e.g., launch task, launch task group, reserve resources, create volumes) that the scheduler wants to
 * perform on the offers. Note that until the scheduler replies (accepts or declines) to an offer, the offer’s
 * resources are considered allocated to the offer’s role and to the framework.
 *
 * @param offerIds
 * @param offerOperations
 */
public void accept(List<OfferID> offerIds, List<Offer.Operation> offerOperations) {
    Builder accept = build().setAccept(Accept.newBuilder().addAllOfferIds(offerIds).addAllOperations(offerOperations));
    sendCall(accept, Type.ACCEPT);
}
Also used : MesosClientBuilder(com.hubspot.mesos.rx.java.MesosClientBuilder) Builder(org.apache.mesos.v1.scheduler.Protos.Call.Builder) ProtobufMesosClientBuilder(com.hubspot.mesos.rx.java.protobuf.ProtobufMesosClientBuilder)

Aggregations

SingularityTaskRequest (com.hubspot.singularity.SingularityTaskRequest)11 Test (org.junit.jupiter.api.Test)11 SingularityTask (com.hubspot.singularity.SingularityTask)9 Resources (com.hubspot.mesos.Resources)8 SingularityTaskId (com.hubspot.singularity.SingularityTaskId)8 SingularityMesosTaskHolder (com.hubspot.singularity.helpers.SingularityMesosTaskHolder)8 SingularityDeployBuilder (com.hubspot.singularity.SingularityDeployBuilder)7 SingularityRequest (com.hubspot.singularity.SingularityRequest)7 SingularityDeploy (com.hubspot.singularity.SingularityDeploy)6 SingularityRequestBuilder (com.hubspot.singularity.SingularityRequestBuilder)6 Protos (org.apache.mesos.v1.Protos)5 SingularityContainerInfo (com.hubspot.mesos.SingularityContainerInfo)4 SingularityDockerPortMapping (com.hubspot.mesos.SingularityDockerPortMapping)4 TaskInfo (org.apache.mesos.v1.Protos.TaskInfo)4 ImmutableList (com.google.common.collect.ImmutableList)3 Inject (com.google.inject.Inject)3 ByteString (com.google.protobuf.ByteString)3 JavaUtils (com.hubspot.mesos.JavaUtils)3 SingularityDockerInfo (com.hubspot.mesos.SingularityDockerInfo)3 SingularityPortMapping (com.hubspot.mesos.SingularityPortMapping)3