Search in sources :

Example 6 with ResourceSpec

use of com.mesosphere.sdk.specification.ResourceSpec in project dcos-commons by mesosphere.

the class SendOffer method getOfferForPod.

private Protos.Offer getOfferForPod(ClusterState state) {
    Optional<PodSpec> matchingSpec = state.getServiceSpec().getPods().stream().filter(podSpec -> podType.equals(podSpec.getType())).findAny();
    if (!matchingSpec.isPresent()) {
        throw new IllegalArgumentException(String.format("No PodSpec found with type=%s: types=%s", podType, state.getServiceSpec().getPods().stream().map(podSpec -> podSpec.getType()).collect(Collectors.toList())));
    }
    PodSpec podSpec = matchingSpec.get();
    Protos.Offer.Builder offerBuilder = Protos.Offer.newBuilder().setFrameworkId(TestConstants.FRAMEWORK_ID).setSlaveId(TestConstants.AGENT_ID).setHostname(hostname);
    offerBuilder.getIdBuilder().setValue(UUID.randomUUID().toString());
    // Include pod/executor-level volumes:
    for (VolumeSpec volumeSpec : podSpec.getVolumes()) {
        offerBuilder.addResources(toUnreservedResource(volumeSpec));
    }
    // Include task-level resources (note: resources are not merged, e.g. 1.5cpu+1.0 cpu instead of 2.5cpu):
    for (TaskSpec taskSpec : podSpec.getTasks()) {
        if (podToReuse.isPresent()) {
            // Copy executor id and resources from prior pod launch:
            LaunchedPod pod = state.getLastLaunchedPod(podToReuse.get());
            offerBuilder.addExecutorIds(pod.getExecutor().getExecutorId()).addAllResources(pod.getExecutor().getResourcesList());
            for (Protos.TaskInfo task : pod.getTasks()) {
                offerBuilder.addAllResources(task.getResourcesList());
            }
        } else {
            // Create new unreserved resources:
            for (ResourceSpec resourceSpec : taskSpec.getResourceSet().getResources()) {
                offerBuilder.addResources(toUnreservedResource(resourceSpec));
            }
            for (VolumeSpec volumeSpec : taskSpec.getResourceSet().getVolumes()) {
                offerBuilder.addResources(toUnreservedResource(volumeSpec));
            }
        }
    }
    // just ignored.
    if (!podToReuse.isPresent()) {
        offerBuilder.addAllResources(DEFAULT_EXECUTOR_RESOURCES);
    }
    return offerBuilder.build();
}
Also used : Protos(org.apache.mesos.Protos) Arrays(java.util.Arrays) SchedulerDriver(org.apache.mesos.SchedulerDriver) TestConstants(com.mesosphere.sdk.testutils.TestConstants) TaskSpec(com.mesosphere.sdk.specification.TaskSpec) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) PodSpec(com.mesosphere.sdk.specification.PodSpec) List(java.util.List) VolumeSpec(com.mesosphere.sdk.specification.VolumeSpec) Scheduler(org.apache.mesos.Scheduler) ResourceSpec(com.mesosphere.sdk.specification.ResourceSpec) Optional(java.util.Optional) Constants(com.mesosphere.sdk.offer.Constants) VolumeSpec(com.mesosphere.sdk.specification.VolumeSpec) PodSpec(com.mesosphere.sdk.specification.PodSpec) Protos(org.apache.mesos.Protos) TaskSpec(com.mesosphere.sdk.specification.TaskSpec) ResourceSpec(com.mesosphere.sdk.specification.ResourceSpec)

Example 7 with ResourceSpec

use of com.mesosphere.sdk.specification.ResourceSpec in project dcos-commons by mesosphere.

the class ExecutorResourceMapper method findMatchingDiskSpec.

private Optional<ResourceLabels> findMatchingDiskSpec(Protos.Resource executorResource, Collection<ResourceSpec> resourceSpecs) {
    for (ResourceSpec resourceSpec : resourceSpecs) {
        if (!(resourceSpec instanceof VolumeSpec)) {
            continue;
        }
        if (executorResource.getDisk().getVolume().getContainerPath().equals(((VolumeSpec) resourceSpec).getContainerPath())) {
            Optional<String> resourceId = ResourceUtils.getResourceId(executorResource);
            if (!resourceId.isPresent()) {
                logger.error("Failed to find resource ID for resource: {}", executorResource);
                continue;
            }
            double diskSize = executorResource.getScalar().getValue();
            VolumeSpec updatedSpec = OfferEvaluationUtils.updateVolumeSpec((VolumeSpec) resourceSpec, diskSize);
            return Optional.of(new ResourceLabels(resourceSpec, updatedSpec, resourceId.get(), Optional.of(executorResource.getDisk().getPersistence().getId()), ResourceUtils.getSourceRoot(executorResource)));
        }
    }
    return Optional.empty();
}
Also used : VolumeSpec(com.mesosphere.sdk.specification.VolumeSpec) ResourceSpec(com.mesosphere.sdk.specification.ResourceSpec)

Example 8 with ResourceSpec

use of com.mesosphere.sdk.specification.ResourceSpec in project dcos-commons by mesosphere.

the class OfferEvaluationUtilsTest method testResourceDecrease.

private void testResourceDecrease(Optional<String> namespace) {
    String resourceId = UUID.randomUUID().toString();
    Protos.Value current = getValue(5);
    Protos.Value desired = getValue(4);
    Protos.Value toSubtract = ValueUtils.subtract(current, desired);
    ResourceSpec resourceSpec = getResourceSpec(desired);
    when(mockPool.consumeReserved(RESOURCE_NAME, desired, resourceId)).thenReturn(Optional.of(getMesosResource(getResourceSpec(current), resourceId, namespace)));
    when(mockPool.consumeReservableMerged(RESOURCE_NAME, desired, Constants.ANY_ROLE)).thenReturn(Optional.of(getMesosResource(toSubtract)));
    ReserveEvaluationOutcome outcome = OfferEvaluationUtils.evaluateSimpleResource(mockStage, resourceSpec, Optional.of(resourceId), namespace, mockPool);
    Assert.assertTrue(outcome.getEvaluationOutcome().isPassing());
    OfferRecommendation recommendation = outcome.getEvaluationOutcome().getOfferRecommendations().get(0);
    Assert.assertTrue(recommendation instanceof UnreserveOfferRecommendation);
    Assert.assertTrue(outcome.getResourceId().isPresent());
    Protos.Resource resource = recommendation.getOperation().getUnreserve().getResources(0);
    Assert.assertEquals(toSubtract.getScalar(), resource.getScalar());
    if (namespace.isPresent()) {
        Assert.assertEquals(namespace.get(), ResourceUtils.getNamespace(resource).get());
    } else {
        Assert.assertFalse(ResourceUtils.getNamespace(resource).isPresent());
    }
}
Also used : Protos(org.apache.mesos.Protos) DefaultResourceSpec(com.mesosphere.sdk.specification.DefaultResourceSpec) ResourceSpec(com.mesosphere.sdk.specification.ResourceSpec) UnreserveOfferRecommendation(com.mesosphere.sdk.offer.UnreserveOfferRecommendation) ReserveEvaluationOutcome(com.mesosphere.sdk.offer.evaluate.OfferEvaluationUtils.ReserveEvaluationOutcome) UnreserveOfferRecommendation(com.mesosphere.sdk.offer.UnreserveOfferRecommendation) OfferRecommendation(com.mesosphere.sdk.offer.OfferRecommendation) ReserveOfferRecommendation(com.mesosphere.sdk.offer.ReserveOfferRecommendation)

Example 9 with ResourceSpec

use of com.mesosphere.sdk.specification.ResourceSpec in project dcos-commons by mesosphere.

the class OfferEvaluationUtilsTest method testResourceIncreaseSufficient.

private void testResourceIncreaseSufficient(Optional<String> namespace) {
    String resourceId = UUID.randomUUID().toString();
    Protos.Value current = getValue(4);
    Protos.Value desired = getValue(5);
    Protos.Value toAdd = ValueUtils.subtract(desired, current);
    ResourceSpec resourceSpec = getResourceSpec(desired);
    when(mockPool.consumeReserved(RESOURCE_NAME, desired, resourceId)).thenReturn(Optional.of(getMesosResource(getResourceSpec(current), resourceId, namespace)));
    when(mockPool.consumeReservableMerged(RESOURCE_NAME, toAdd, Constants.ANY_ROLE)).thenReturn(Optional.of(getMesosResource(toAdd)));
    ReserveEvaluationOutcome outcome = OfferEvaluationUtils.evaluateSimpleResource(mockStage, resourceSpec, Optional.of(resourceId), namespace, mockPool);
    Assert.assertTrue(outcome.getEvaluationOutcome().isPassing());
    OfferRecommendation recommendation = outcome.getEvaluationOutcome().getOfferRecommendations().get(0);
    Assert.assertTrue(recommendation instanceof ReserveOfferRecommendation);
    Assert.assertTrue(outcome.getResourceId().isPresent());
    Protos.Resource resource = recommendation.getOperation().getReserve().getResources(0);
    Assert.assertEquals(toAdd.getScalar(), resource.getScalar());
    if (namespace.isPresent()) {
        Assert.assertEquals(namespace.get(), ResourceUtils.getNamespace(resource).get());
    } else {
        Assert.assertFalse(ResourceUtils.getNamespace(resource).isPresent());
    }
}
Also used : Protos(org.apache.mesos.Protos) DefaultResourceSpec(com.mesosphere.sdk.specification.DefaultResourceSpec) ResourceSpec(com.mesosphere.sdk.specification.ResourceSpec) ReserveEvaluationOutcome(com.mesosphere.sdk.offer.evaluate.OfferEvaluationUtils.ReserveEvaluationOutcome) ReserveOfferRecommendation(com.mesosphere.sdk.offer.ReserveOfferRecommendation) UnreserveOfferRecommendation(com.mesosphere.sdk.offer.UnreserveOfferRecommendation) OfferRecommendation(com.mesosphere.sdk.offer.OfferRecommendation) ReserveOfferRecommendation(com.mesosphere.sdk.offer.ReserveOfferRecommendation)

Example 10 with ResourceSpec

use of com.mesosphere.sdk.specification.ResourceSpec in project dcos-commons by mesosphere.

the class PortEvaluationStage method selectDynamicPort.

private static Optional<Integer> selectDynamicPort(MesosResourcePool mesosResourcePool, PodInfoBuilder podInfoBuilder) {
    Set<Integer> consumedPorts = new HashSet<>();
    // compile a list of those to check against the offered ports.
    for (TaskSpec task : podInfoBuilder.getPodInstance().getPod().getTasks()) {
        for (ResourceSpec resourceSpec : task.getResourceSet().getResources()) {
            if (resourceSpec instanceof PortSpec) {
                PortSpec portSpec = (PortSpec) resourceSpec;
                if (portSpec.getPort() != 0) {
                    consumedPorts.add((int) portSpec.getPort());
                }
            }
        }
    }
    // Also check other dynamically allocated ports which had been taken by earlier stages of this evaluation round.
    for (Protos.Resource.Builder resourceBuilder : podInfoBuilder.getTaskResourceBuilders()) {
        consumedPorts.addAll(getPortsInResource(resourceBuilder.build()));
    }
    for (Protos.Resource.Builder resourceBuilder : podInfoBuilder.getExecutorResourceBuilders()) {
        consumedPorts.addAll(getPortsInResource(resourceBuilder.build()));
    }
    Protos.Value availablePorts = mesosResourcePool.getUnreservedMergedPool().get(Constants.PORTS_RESOURCE_TYPE);
    Optional<Integer> dynamicPort = Optional.empty();
    if (availablePorts != null) {
        dynamicPort = availablePorts.getRanges().getRangeList().stream().flatMap(r -> IntStream.rangeClosed((int) r.getBegin(), (int) r.getEnd()).boxed()).filter(p -> !consumedPorts.contains(p)).findFirst();
    }
    return dynamicPort;
}
Also used : Protos(org.apache.mesos.Protos) IntStream(java.util.stream.IntStream) java.util(java.util) com.mesosphere.sdk.offer(com.mesosphere.sdk.offer) Logger(org.slf4j.Logger) TaskSpec(com.mesosphere.sdk.specification.TaskSpec) ResourceSpec(com.mesosphere.sdk.specification.ResourceSpec) com.mesosphere.sdk.offer.taskdata(com.mesosphere.sdk.offer.taskdata) TextFormat(com.google.protobuf.TextFormat) Collectors(java.util.stream.Collectors) DcosConstants(com.mesosphere.sdk.dcos.DcosConstants) PortSpec(com.mesosphere.sdk.specification.PortSpec) Protos(org.apache.mesos.Protos) TaskSpec(com.mesosphere.sdk.specification.TaskSpec) ResourceSpec(com.mesosphere.sdk.specification.ResourceSpec) PortSpec(com.mesosphere.sdk.specification.PortSpec)

Aggregations

ResourceSpec (com.mesosphere.sdk.specification.ResourceSpec)13 Protos (org.apache.mesos.Protos)10 DefaultResourceSpec (com.mesosphere.sdk.specification.DefaultResourceSpec)8 ReserveEvaluationOutcome (com.mesosphere.sdk.offer.evaluate.OfferEvaluationUtils.ReserveEvaluationOutcome)4 OfferRecommendation (com.mesosphere.sdk.offer.OfferRecommendation)3 ReserveOfferRecommendation (com.mesosphere.sdk.offer.ReserveOfferRecommendation)3 UnreserveOfferRecommendation (com.mesosphere.sdk.offer.UnreserveOfferRecommendation)3 VolumeSpec (com.mesosphere.sdk.specification.VolumeSpec)3 TaskSpec (com.mesosphere.sdk.specification.TaskSpec)2 Collectors (java.util.stream.Collectors)2 TextFormat (com.google.protobuf.TextFormat)1 DcosConstants (com.mesosphere.sdk.dcos.DcosConstants)1 ResourceRefinementCapabilityContext (com.mesosphere.sdk.dcos.ResourceRefinementCapabilityContext)1 com.mesosphere.sdk.offer (com.mesosphere.sdk.offer)1 Constants (com.mesosphere.sdk.offer.Constants)1 com.mesosphere.sdk.offer.taskdata (com.mesosphere.sdk.offer.taskdata)1 PodSpec (com.mesosphere.sdk.specification.PodSpec)1 PortSpec (com.mesosphere.sdk.specification.PortSpec)1 TestConstants (com.mesosphere.sdk.testutils.TestConstants)1 java.util (java.util)1