Search in sources :

Example 11 with SingularityMesosTaskHolder

use of com.hubspot.singularity.helpers.SingularityMesosTaskHolder in project Singularity by HubSpot.

the class SingularityOfferHolder method launchTasksAndGetUnusedOffers.

public List<Offer> launchTasksAndGetUnusedOffers(SingularityMesosSchedulerClient schedulerClient) {
    final List<TaskInfo> toLaunch = Lists.newArrayListWithCapacity(acceptedTasks.size());
    final List<SingularityTaskId> taskIds = Lists.newArrayListWithCapacity(acceptedTasks.size());
    for (SingularityMesosTaskHolder taskHolder : acceptedTasks) {
        taskIds.add(taskHolder.getTask().getTaskId());
        toLaunch.add(taskHolder.getMesosTask());
        LOG.debug("Launching {} with offer {}", taskHolder.getTask().getTaskId(), offers.get(0).getId());
        LOG.trace("Launching {} mesos task: {}", taskHolder.getTask().getTaskId(), MesosUtils.formatForLogging(taskHolder.getMesosTask()));
    }
    // At this point, `currentResources` contains a list of unused resources, because we subtracted out the required resources of every task we accepted.
    // Let's try and reclaim offers by trying to pull each offer's list of resources out of the combined pool of leftover resources.
    // n.b., This is currently not optimal. We just look through the offers in this instance and try to reclaim them with no particular priority or order.
    Map<Boolean, List<Offer>> partitionedOffers = offers.stream().collect(Collectors.partitioningBy(offer -> {
        List<Long> ports = MesosUtils.getAllPorts(offer.getResourcesList());
        boolean offerCanBeReclaimedFromUnusedResources = offer.getResourcesList().stream().collect(Collectors.groupingBy(Resource::getRole)).entrySet().stream().map((entry) -> {
            // Now, for each set of offer Resources grouped by role...
            String role = entry.getKey();
            List<Resource> offerResources = entry.getValue();
            Optional<String> maybeRole = (!role.equals("") && !role.equals("*")) ? Optional.of(role) : Optional.absent();
            // ...Check if we can pull the Resources belonging to this offer out of the pool of `currentResources`.
            return MesosUtils.doesOfferMatchResources(maybeRole, MesosUtils.buildResourcesFromMesosResourceList(offerResources, maybeRole), currentResources, ports);
        }).reduce(true, (x, y) -> x && y);
        if (offerCanBeReclaimedFromUnusedResources) {
            // We can reclaim this offer in its entirety! Pull all of its resources out of the combined pool for this SingularityOfferHolder instance.
            LOG.trace("Able to reclaim offer {} from unused resources in OfferHolder from host {}. cpu: {}, mem: {}, disk: {}", offer.getId().getValue(), offer.getHostname(), MesosUtils.getNumCpus(offer), MesosUtils.getMemory(offer), MesosUtils.getDisk(offer));
            currentResources = MesosUtils.subtractResources(currentResources, offer.getResourcesList());
        }
        return offerCanBeReclaimedFromUnusedResources;
    }));
    List<Offer> leftoverOffers = partitionedOffers.get(true);
    List<Offer> neededOffers = partitionedOffers.get(false);
    schedulerClient.accept(neededOffers.stream().map(Offer::getId).collect(Collectors.toList()), Collections.singletonList(Operation.newBuilder().setType(Type.LAUNCH).setLaunch(Launch.newBuilder().addAllTaskInfos(toLaunch).build()).build()));
    LOG.info("{} tasks ({}) launched", taskIds.size(), taskIds);
    return leftoverOffers;
}
Also used : Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) Offer(org.apache.mesos.v1.Protos.Offer) Protos(org.apache.mesos.v1.Protos) Collectors(java.util.stream.Collectors) SingularityMesosTaskHolder(com.hubspot.singularity.helpers.SingularityMesosTaskHolder) Objects(java.util.Objects) Resource(org.apache.mesos.v1.Protos.Resource) List(java.util.List) JavaUtils(com.hubspot.mesos.JavaUtils) Launch(org.apache.mesos.v1.Protos.Offer.Operation.Launch) Lists(com.google.common.collect.Lists) MesosUtils(com.hubspot.singularity.helpers.MesosUtils) Operation(org.apache.mesos.v1.Protos.Offer.Operation) Optional(com.google.common.base.Optional) Map(java.util.Map) TaskInfo(org.apache.mesos.v1.Protos.TaskInfo) Type(org.apache.mesos.v1.Protos.Offer.Operation.Type) Collections(java.util.Collections) SingularityTaskId(com.hubspot.singularity.SingularityTaskId) Resource(org.apache.mesos.v1.Protos.Resource) SingularityMesosTaskHolder(com.hubspot.singularity.helpers.SingularityMesosTaskHolder) TaskInfo(org.apache.mesos.v1.Protos.TaskInfo) Offer(org.apache.mesos.v1.Protos.Offer) List(java.util.List) SingularityTaskId(com.hubspot.singularity.SingularityTaskId)

Example 12 with SingularityMesosTaskHolder

use of com.hubspot.singularity.helpers.SingularityMesosTaskHolder in project Singularity by HubSpot.

the class SingularityMesosOfferScheduler method acceptTask.

private SingularityMesosTaskHolder acceptTask(SingularityOfferHolder offerHolder, Map<String, Integer> tasksPerOffer, SingularityTaskRequestHolder taskRequestHolder) {
    final SingularityTaskRequest taskRequest = taskRequestHolder.getTaskRequest();
    final SingularityMesosTaskHolder taskHolder = mesosTaskBuilder.buildTask(offerHolder, offerHolder.getCurrentResources(), taskRequest, taskRequestHolder.getTaskResources(), taskRequestHolder.getExecutorResources());
    final SingularityTask zkTask = taskSizeOptimizer.getSizeOptimizedTask(taskHolder);
    LOG.trace("Accepted and built task {}", zkTask);
    LOG.info("Launching task {} slot on slave {} ({})", taskHolder.getTask().getTaskId(), offerHolder.getSlaveId(), offerHolder.getHostname());
    taskManager.createTaskAndDeletePendingTask(zkTask);
    addRequestToMapByOfferHost(tasksPerOffer, offerHolder.getHostname(), taskRequest.getRequest().getId());
    return taskHolder;
}
Also used : SingularityTask(com.hubspot.singularity.SingularityTask) SingularityMesosTaskHolder(com.hubspot.singularity.helpers.SingularityMesosTaskHolder) SingularityTaskRequest(com.hubspot.singularity.SingularityTaskRequest)

Aggregations

SingularityMesosTaskHolder (com.hubspot.singularity.helpers.SingularityMesosTaskHolder)12 SingularityTaskRequest (com.hubspot.singularity.SingularityTaskRequest)11 SingularityDeploy (com.hubspot.singularity.SingularityDeploy)8 SingularityDeployBuilder (com.hubspot.singularity.SingularityDeployBuilder)8 SingularityRequest (com.hubspot.singularity.SingularityRequest)8 SingularityRequestBuilder (com.hubspot.singularity.SingularityRequestBuilder)8 Test (org.junit.Test)8 Resources (com.hubspot.mesos.Resources)7 SingularityContainerInfo (com.hubspot.mesos.SingularityContainerInfo)5 SingularityDockerInfo (com.hubspot.mesos.SingularityDockerInfo)4 SingularityDockerPortMapping (com.hubspot.mesos.SingularityDockerPortMapping)4 Protos (org.apache.mesos.v1.Protos)4 Optional (com.google.common.base.Optional)3 SingularityPortMapping (com.hubspot.mesos.SingularityPortMapping)3 SingularityVolume (com.hubspot.mesos.SingularityVolume)3 SingularityTask (com.hubspot.singularity.SingularityTask)3 SingularityTaskId (com.hubspot.singularity.SingularityTaskId)3 MesosUtils (com.hubspot.singularity.helpers.MesosUtils)3 Collections (java.util.Collections)3 List (java.util.List)3