Search in sources :

Example 1 with PlacementRule

use of com.mesosphere.sdk.offer.evaluate.placement.PlacementRule in project dcos-commons by mesosphere.

the class OfferEvaluatorPlacementTest method testColocateAgents.

@Test
public void testColocateAgents() throws Exception {
    Protos.Resource offeredCpu = ResourceTestUtils.getUnreservedCpus(2.0);
    // Don't launch
    PlacementRule placementRule = PlacementUtils.getAgentPlacementRule(Collections.emptyList(), Arrays.asList("some-random-agent")).get();
    PodSpec podSpec = PodInstanceRequirementTestUtils.getCpuRequirement(1.0).getPodInstance().getPod();
    podSpec = DefaultPodSpec.newBuilder(podSpec).placementRule(placementRule).build();
    PodInstance podInstance = new DefaultPodInstance(podSpec, 0);
    PodInstanceRequirement podInstanceRequirement = PodInstanceRequirement.newBuilder(podInstance, Arrays.asList(TestConstants.TASK_NAME)).build();
    List<OfferRecommendation> recommendations = evaluator.evaluate(podInstanceRequirement, Arrays.asList(OfferTestUtils.getCompleteOffer(offeredCpu)));
    Assert.assertEquals(0, recommendations.size());
    // Launch
    placementRule = PlacementUtils.getAgentPlacementRule(Collections.emptyList(), Arrays.asList(TestConstants.AGENT_ID.getValue())).get();
    podSpec = DefaultPodSpec.newBuilder(podSpec).placementRule(placementRule).build();
    podInstance = new DefaultPodInstance(podSpec, 0);
    podInstanceRequirement = PodInstanceRequirement.newBuilder(podInstance, Arrays.asList(TestConstants.TASK_NAME)).build();
    recommendations = evaluator.evaluate(podInstanceRequirement, Arrays.asList(OfferTestUtils.getCompleteOffer(offeredCpu)));
    Assert.assertEquals(5, recommendations.size());
}
Also used : Protos(org.apache.mesos.Protos) DefaultPodSpec(com.mesosphere.sdk.specification.DefaultPodSpec) PodSpec(com.mesosphere.sdk.specification.PodSpec) DefaultPodInstance(com.mesosphere.sdk.scheduler.plan.DefaultPodInstance) PodInstance(com.mesosphere.sdk.specification.PodInstance) PlacementRule(com.mesosphere.sdk.offer.evaluate.placement.PlacementRule) DefaultPodInstance(com.mesosphere.sdk.scheduler.plan.DefaultPodInstance) PodInstanceRequirement(com.mesosphere.sdk.scheduler.plan.PodInstanceRequirement) OfferRecommendation(com.mesosphere.sdk.offer.OfferRecommendation) Test(org.junit.Test)

Example 2 with PlacementRule

use of com.mesosphere.sdk.offer.evaluate.placement.PlacementRule in project dcos-commons by mesosphere.

the class OfferEvaluatorPlacementTest method testAvoidAgents.

@Test
public void testAvoidAgents() throws Exception {
    Protos.Resource offeredCpu = ResourceTestUtils.getUnreservedCpus(2.0);
    // Don't launch
    PlacementRule placementRule = PlacementUtils.getAgentPlacementRule(Arrays.asList(TestConstants.AGENT_ID.getValue()), Collections.emptyList()).get();
    PodSpec podSpec = PodInstanceRequirementTestUtils.getCpuRequirement(1.0).getPodInstance().getPod();
    podSpec = DefaultPodSpec.newBuilder(podSpec).placementRule(placementRule).build();
    PodInstance podInstance = new DefaultPodInstance(podSpec, 0);
    PodInstanceRequirement podInstanceRequirement = PodInstanceRequirement.newBuilder(podInstance, Arrays.asList(TestConstants.TASK_NAME)).build();
    List<OfferRecommendation> recommendations = evaluator.evaluate(podInstanceRequirement, Arrays.asList(OfferTestUtils.getCompleteOffer(offeredCpu)));
    Assert.assertEquals(0, recommendations.size());
    // Launch
    placementRule = PlacementUtils.getAgentPlacementRule(Arrays.asList("some-random-agent"), Collections.emptyList()).get();
    podSpec = DefaultPodSpec.newBuilder(podSpec).placementRule(placementRule).build();
    podInstance = new DefaultPodInstance(podSpec, 0);
    podInstanceRequirement = PodInstanceRequirement.newBuilder(podInstance, Arrays.asList(TestConstants.TASK_NAME)).build();
    recommendations = evaluator.evaluate(podInstanceRequirement, Arrays.asList(OfferTestUtils.getCompleteOffer(offeredCpu)));
    Assert.assertEquals(5, recommendations.size());
}
Also used : Protos(org.apache.mesos.Protos) DefaultPodSpec(com.mesosphere.sdk.specification.DefaultPodSpec) PodSpec(com.mesosphere.sdk.specification.PodSpec) DefaultPodInstance(com.mesosphere.sdk.scheduler.plan.DefaultPodInstance) PodInstance(com.mesosphere.sdk.specification.PodInstance) PlacementRule(com.mesosphere.sdk.offer.evaluate.placement.PlacementRule) DefaultPodInstance(com.mesosphere.sdk.scheduler.plan.DefaultPodInstance) PodInstanceRequirement(com.mesosphere.sdk.scheduler.plan.PodInstanceRequirement) OfferRecommendation(com.mesosphere.sdk.offer.OfferRecommendation) Test(org.junit.Test)

Example 3 with PlacementRule

use of com.mesosphere.sdk.offer.evaluate.placement.PlacementRule in project dcos-commons by mesosphere.

the class YAMLToInternalMappers method convertPod.

private static PodSpec convertPod(RawPod rawPod, ConfigTemplateReader configTemplateReader, String podName, Map<String, String> additionalEnv, String role, String principal, String executorUri, String user) throws Exception {
    DefaultPodSpec.Builder builder = DefaultPodSpec.newBuilder(executorUri).count(rawPod.getCount()).type(podName).user(user).preReservedRole(rawPod.getPreReservedRole()).sharePidNamespace(rawPod.getSharePidNamespace()).allowDecommission(rawPod.getAllowDecommission());
    List<String> networkNames = new ArrayList<>();
    List<RLimitSpec> rlimits = new ArrayList<>();
    for (Map.Entry<String, RawRLimit> entry : rawPod.getRLimits().entrySet()) {
        RawRLimit rawRLimit = entry.getValue();
        rlimits.add(new RLimitSpec(entry.getKey(), rawRLimit.getSoft(), rawRLimit.getHard()));
    }
    WriteOnceLinkedHashMap<String, RawNetwork> rawNetworks = rawPod.getNetworks();
    final Collection<NetworkSpec> networks = new ArrayList<>();
    if (MapUtils.isNotEmpty(rawNetworks)) {
        networks.addAll(rawNetworks.entrySet().stream().map(rawNetworkEntry -> {
            String networkName = rawNetworkEntry.getKey();
            DcosConstants.warnIfUnsupportedNetwork(networkName);
            networkNames.add(networkName);
            RawNetwork rawNetwork = rawNetworks.get(networkName);
            return convertNetwork(networkName, rawNetwork, collatePorts(rawPod));
        }).collect(Collectors.toList()));
    }
    builder.image(rawPod.getImage()).networks(networks).rlimits(rlimits);
    // Collect the resourceSets (if given)
    final Collection<ResourceSet> resourceSets = new ArrayList<>();
    WriteOnceLinkedHashMap<String, RawResourceSet> rawResourceSets = rawPod.getResourceSets();
    if (MapUtils.isNotEmpty(rawResourceSets)) {
        resourceSets.addAll(rawResourceSets.entrySet().stream().map(rawResourceSetEntry -> {
            String rawResourceSetName = rawResourceSetEntry.getKey();
            RawResourceSet rawResourceSet = rawResourceSets.get(rawResourceSetName);
            return convertResourceSet(rawResourceSetName, rawResourceSet.getCpus(), rawResourceSet.getGpus(), rawResourceSet.getMemory(), rawResourceSet.getPorts(), rawResourceSet.getVolume(), rawResourceSet.getVolumes(), role, rawPod.getPreReservedRole(), principal, networkNames);
        }).collect(Collectors.toList()));
    }
    if (!rawPod.getSecrets().isEmpty()) {
        Collection<SecretSpec> secretSpecs = new ArrayList<>();
        secretSpecs.addAll(rawPod.getSecrets().values().stream().map(v -> convertSecret(v)).collect(Collectors.toList()));
        builder.secrets(secretSpecs);
    }
    if (rawPod.getVolume() != null || !rawPod.getVolumes().isEmpty()) {
        Collection<VolumeSpec> volumeSpecs = new ArrayList<>(rawPod.getVolume() == null ? Collections.emptyList() : Arrays.asList(convertVolume(rawPod.getVolume(), role, rawPod.getPreReservedRole(), principal)));
        volumeSpecs.addAll(rawPod.getVolumes().values().stream().map(v -> convertVolume(v, role, rawPod.getPreReservedRole(), principal)).collect(Collectors.toList()));
        builder.volumes(volumeSpecs);
    }
    // Parse the TaskSpecs
    List<TaskSpec> taskSpecs = new ArrayList<>();
    for (Map.Entry<String, RawTask> entry : rawPod.getTasks().entrySet()) {
        taskSpecs.add(convertTask(entry.getValue(), configTemplateReader, entry.getKey(), additionalEnv, resourceSets, role, rawPod.getPreReservedRole(), principal, networkNames));
    }
    builder.tasks(taskSpecs);
    Collection<URI> podUris = new ArrayList<>();
    for (String uriStr : rawPod.getUris()) {
        podUris.add(new URI(uriStr));
    }
    builder.uris(podUris);
    PlacementRule placementRule = MarathonConstraintParser.parse(podName, rawPod.getPlacement());
    if (!(placementRule instanceof PassthroughRule)) {
        builder.placementRule(placementRule);
    }
    return builder.build();
}
Also used : PlacementRule(com.mesosphere.sdk.offer.evaluate.placement.PlacementRule) URI(java.net.URI) PassthroughRule(com.mesosphere.sdk.offer.evaluate.placement.PassthroughRule)

Example 4 with PlacementRule

use of com.mesosphere.sdk.offer.evaluate.placement.PlacementRule in project dcos-commons by mesosphere.

the class RegionValidator method validate.

private static Collection<ConfigValidationError> validate(Optional<ServiceSpec> oldConfig, ServiceSpec newConfig, String podType) {
    if (!oldConfig.isPresent()) {
        return Collections.emptyList();
    }
    Optional<PodSpec> oldPod = getPodSpec(oldConfig.get(), podType);
    if (!oldPod.isPresent()) {
        return Collections.emptyList();
    }
    Optional<PodSpec> newPod = getPodSpec(newConfig, podType);
    if (!newPod.isPresent()) {
        throw new IllegalArgumentException(String.format("Unable to find requested pod=%s, in config: %s", podType, newConfig));
    }
    boolean oldReferencesRegions = PlacementUtils.placementRuleReferencesRegion(oldPod.get());
    boolean newReferencesRegions = PlacementUtils.placementRuleReferencesRegion(newPod.get());
    if (oldReferencesRegions != newReferencesRegions) {
        Optional<PlacementRule> oldRule = oldPod.get().getPlacementRule();
        Optional<PlacementRule> newRule = newPod.get().getPlacementRule();
        ConfigValidationError error = ConfigValidationError.transitionError(String.format("%s.PlacementRule", podType), oldRule.toString(), newRule.toString(), String.format("PlacementRule cannot change from %s to %s", oldRule, newRule));
        return Arrays.asList(error);
    }
    return Collections.emptyList();
}
Also used : PodSpec(com.mesosphere.sdk.specification.PodSpec) ConfigValidationError(com.mesosphere.sdk.config.validate.ConfigValidationError) PlacementRule(com.mesosphere.sdk.offer.evaluate.placement.PlacementRule)

Example 5 with PlacementRule

use of com.mesosphere.sdk.offer.evaluate.placement.PlacementRule in project dcos-commons by mesosphere.

the class PlacementRuleIsValid method validate.

@Override
public Collection<ConfigValidationError> validate(Optional<ServiceSpec> oldConfig, ServiceSpec newConfig) {
    List<PodSpec> podSpecs = newConfig.getPods().stream().filter(podSpec -> podSpec.getPlacementRule().isPresent()).collect(Collectors.toList());
    List<ConfigValidationError> errors = new ArrayList<>();
    for (final PodSpec podSpec : podSpecs) {
        PlacementRule placementRule = podSpec.getPlacementRule().get();
        if (!isValid(placementRule)) {
            String errMsg = String.format("The PlacementRule for PodSpec '%s' had invalid constraints", podSpec.getType());
            errors.add(ConfigValidationError.valueError("PlacementRule", placementRule.toString(), errMsg));
        }
    }
    return errors;
}
Also used : PodSpec(com.mesosphere.sdk.specification.PodSpec) List(java.util.List) OrRule(com.mesosphere.sdk.offer.evaluate.placement.OrRule) InvalidPlacementRule(com.mesosphere.sdk.offer.evaluate.placement.InvalidPlacementRule) PlacementRule(com.mesosphere.sdk.offer.evaluate.placement.PlacementRule) Collection(java.util.Collection) Optional(java.util.Optional) Collectors(java.util.stream.Collectors) AndRule(com.mesosphere.sdk.offer.evaluate.placement.AndRule) ServiceSpec(com.mesosphere.sdk.specification.ServiceSpec) ArrayList(java.util.ArrayList) PodSpec(com.mesosphere.sdk.specification.PodSpec) InvalidPlacementRule(com.mesosphere.sdk.offer.evaluate.placement.InvalidPlacementRule) PlacementRule(com.mesosphere.sdk.offer.evaluate.placement.PlacementRule) ArrayList(java.util.ArrayList)

Aggregations

PlacementRule (com.mesosphere.sdk.offer.evaluate.placement.PlacementRule)13 PodSpec (com.mesosphere.sdk.specification.PodSpec)10 DefaultPodInstance (com.mesosphere.sdk.scheduler.plan.DefaultPodInstance)6 PodInstanceRequirement (com.mesosphere.sdk.scheduler.plan.PodInstanceRequirement)6 DefaultPodSpec (com.mesosphere.sdk.specification.DefaultPodSpec)6 PodInstance (com.mesosphere.sdk.specification.PodInstance)6 Protos (org.apache.mesos.Protos)6 Test (org.junit.Test)6 OfferRecommendation (com.mesosphere.sdk.offer.OfferRecommendation)4 ConfigValidationError (com.mesosphere.sdk.config.validate.ConfigValidationError)2 MesosResourcePool (com.mesosphere.sdk.offer.MesosResourcePool)2 AndRule (com.mesosphere.sdk.offer.evaluate.placement.AndRule)2 ServiceSpec (com.mesosphere.sdk.specification.ServiceSpec)2 Collectors (java.util.stream.Collectors)2 Capabilities (com.mesosphere.sdk.dcos.Capabilities)1 HostnameRule (com.mesosphere.sdk.offer.evaluate.placement.HostnameRule)1 InvalidPlacementRule (com.mesosphere.sdk.offer.evaluate.placement.InvalidPlacementRule)1 IsLocalRegionRule (com.mesosphere.sdk.offer.evaluate.placement.IsLocalRegionRule)1 OrRule (com.mesosphere.sdk.offer.evaluate.placement.OrRule)1 PassthroughRule (com.mesosphere.sdk.offer.evaluate.placement.PassthroughRule)1