Search in sources :

Example 11 with PlacementRule

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

the class ZoneValidator 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()) {
        // Maybe the pod or task was renamed? Lets avoid enforcing whether those are rename- able and assume it's OK
        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 oldReferencesZones = PlacementUtils.placementRuleReferencesZone(oldPod.get());
    boolean newReferencesZones = PlacementUtils.placementRuleReferencesZone(newPod.get());
    if (oldReferencesZones != newReferencesZones) {
        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 12 with PlacementRule

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

the class PlacementRuleEvaluationStageTest method testOfferFailsPlacementRule.

@Test
public void testOfferFailsPlacementRule() throws Exception {
    String agent = "test-agent";
    Protos.Resource offered = ResourceTestUtils.getUnreservedCpus(1.0);
    PlacementRule rule = AgentRule.require(agent);
    Protos.Offer offer = offerWithAgent("other-agent", offered);
    MesosResourcePool mesosResourcePool = new MesosResourcePool(offer, Optional.of(Constants.ANY_ROLE));
    PodSpec podSpec = PodInstanceRequirementTestUtils.getCpuRequirement(1.0).getPodInstance().getPod();
    DefaultPodSpec.newBuilder(podSpec).placementRule(rule);
    PodInstance podInstance = new DefaultPodInstance(podSpec, 0);
    List<String> taskNames = TaskUtils.getTaskNames(podInstance);
    PodInstanceRequirement podInstanceRequirement = PodInstanceRequirement.newBuilder(podInstance, taskNames).build();
    PlacementRuleEvaluationStage placementRuleEvaluationStage = new PlacementRuleEvaluationStage(Collections.emptyList(), rule);
    EvaluationOutcome outcome = placementRuleEvaluationStage.evaluate(mesosResourcePool, new PodInfoBuilder(podInstanceRequirement, TestConstants.SERVICE_NAME, UUID.randomUUID(), ArtifactResource.getUrlFactory(TestConstants.SERVICE_NAME), SchedulerConfigTestUtils.getTestSchedulerConfig(), Collections.emptyList(), TestConstants.FRAMEWORK_ID, true, Collections.emptyMap()));
    Assert.assertFalse(outcome.isPassing());
    Assert.assertEquals(3, mesosResourcePool.getUnreservedMergedPool().size());
    Assert.assertTrue(Math.abs(mesosResourcePool.getUnreservedMergedPool().get("cpus").getScalar().getValue() - 1.1) < 0.01);
}
Also used : 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) PodInstanceRequirement(com.mesosphere.sdk.scheduler.plan.PodInstanceRequirement) MesosResourcePool(com.mesosphere.sdk.offer.MesosResourcePool) Protos(org.apache.mesos.Protos) DefaultPodInstance(com.mesosphere.sdk.scheduler.plan.DefaultPodInstance) Test(org.junit.Test)

Example 13 with PlacementRule

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

the class SchedulerBuilder method build.

/**
 * Creates a new Mesos scheduler instance with the provided values or their defaults, or an empty {@link Optional}
 * if no Mesos scheduler should be registered for this run.
 *
 * @return a new Mesos scheduler instance to be registered, or an empty {@link Optional}
 * @throws IllegalArgumentException if validating the provided configuration failed
 */
public AbstractScheduler build() {
    // If region awareness is enabled (via java bit or via env) and the cluster supports it, update the ServiceSpec
    // to include region constraints.
    final ServiceSpec serviceSpec;
    if (Capabilities.getInstance().supportsDomains()) {
        // This cluster supports domains. We need to update pod placement with region configuration, for any pods
        // that weren't already configured by the developer (expected to be rare, but possible).
        // Whether region awareness is enabled for the service (via env or via java).
        boolean regionAwarenessEnabled = isRegionAwarenessEnabled();
        // A region to target, as specified in env, if any.
        Optional<String> schedulerRegion = schedulerConfig.getSchedulerRegion();
        // Target the specified region, or use the local region.
        // Local region is determined at framework registration, see IsLocalRegionRule.setLocalDomain().
        final PlacementRule placementRuleToAdd;
        if (regionAwarenessEnabled && schedulerRegion.isPresent()) {
            logger.info("Updating pods with placement rule for region={}", schedulerRegion.get());
            placementRuleToAdd = RegionRuleFactory.getInstance().require(ExactMatcher.create(schedulerRegion.get()));
        } else {
            logger.info("Updating pods with local region placement rule: region awareness={}, scheduler region={}", regionAwarenessEnabled, schedulerRegion);
            placementRuleToAdd = new IsLocalRegionRule();
        }
        List<PodSpec> updatedPodSpecs = new ArrayList<>();
        for (PodSpec podSpec : originalServiceSpec.getPods()) {
            if (PlacementUtils.placementRuleReferencesRegion(podSpec)) {
                // Pod already has a region constraint (specified by developer?). Leave it as-is.
                logger.info("Pod {} already has a region rule defined, leaving as-is", podSpec.getType());
                updatedPodSpecs.add(podSpec);
            } else {
                // Combine the new rule with any existing rules:
                PlacementRule mergedRule = podSpec.getPlacementRule().isPresent() ? new AndRule(placementRuleToAdd, podSpec.getPlacementRule().get()) : placementRuleToAdd;
                updatedPodSpecs.add(DefaultPodSpec.newBuilder(podSpec).placementRule(mergedRule).build());
            }
        }
        DefaultServiceSpec.Builder builder = DefaultServiceSpec.newBuilder(originalServiceSpec).pods(updatedPodSpecs);
        if (schedulerRegion.isPresent()) {
            builder.region(schedulerRegion.get());
        }
        serviceSpec = builder.build();
    } else {
        serviceSpec = originalServiceSpec;
    }
    // NOTE: we specifically avoid accessing the provided persister before build() is called.
    // This is to ensure that upstream has a chance to e.g. lock it via CuratorLocker.
    // When multi-service is enabled, state/configs are stored within a namespace matching the service name.
    // Otherwise use an empty namespace, which indicates single-service mode.
    String namespaceStr = namespace.orElse("");
    FrameworkStore frameworkStore = new FrameworkStore(persister);
    StateStore stateStore = new StateStore(persister, namespaceStr);
    ConfigStore<ServiceSpec> configStore = new ConfigStore<>(DefaultServiceSpec.getConfigurationFactory(serviceSpec), persister, namespaceStr);
    if (schedulerConfig.isUninstallEnabled()) {
        // uninstall mode. UninstallScheduler will internally flag the stateStore with an uninstall bit if needed.
        return new UninstallScheduler(serviceSpec, frameworkStore, stateStore, configStore, FrameworkConfig.fromServiceSpec(serviceSpec), schedulerConfig, Optional.ofNullable(planCustomizer));
    }
    if (StateStoreUtils.isUninstalling(stateStore)) {
        // SERVICE UNINSTALL: The service has an uninstall bit set in its (potentially namespaced) state store.
        if (namespace.isPresent()) {
            // Launch the service in uninstall mode so that it can continue with whatever may be left.
            return new UninstallScheduler(serviceSpec, frameworkStore, stateStore, configStore, FrameworkConfig.fromServiceSpec(serviceSpec), schedulerConfig, Optional.ofNullable(planCustomizer));
        } else {
            // This is an illegal state for a single-service scheduler. SchedulerConfig's uninstall bit should have
            // also been enabled. If we got here, it means that the user likely tampered with the scheduler env
            // after having previously triggered an uninstall, which had set the bit in stateStore. Just exit,
            // because the service is likely now in an inconsistent state resulting from the incomplete uninstall.
            logger.error("Service has been previously told to uninstall, this cannot be reversed. " + "Reenable the uninstall flag to complete the process.");
            SchedulerUtils.hardExit(SchedulerErrorCode.SCHEDULER_ALREADY_UNINSTALLING);
        }
    }
    try {
        return getDefaultScheduler(serviceSpec, frameworkStore, stateStore, configStore);
    } catch (ConfigStoreException e) {
        logger.error("Failed to construct scheduler.", e);
        SchedulerUtils.hardExit(SchedulerErrorCode.INITIALIZATION_FAILURE);
        // This is so the compiler doesn't complain.  The scheduler is going down anyway.
        return null;
    }
}
Also used : AndRule(com.mesosphere.sdk.offer.evaluate.placement.AndRule) ConfigStoreException(com.mesosphere.sdk.state.ConfigStoreException) PlacementRule(com.mesosphere.sdk.offer.evaluate.placement.PlacementRule) RawServiceSpec(com.mesosphere.sdk.specification.yaml.RawServiceSpec) StateStore(com.mesosphere.sdk.state.StateStore) ConfigStore(com.mesosphere.sdk.state.ConfigStore) IsLocalRegionRule(com.mesosphere.sdk.offer.evaluate.placement.IsLocalRegionRule) UninstallScheduler(com.mesosphere.sdk.scheduler.uninstall.UninstallScheduler) FrameworkStore(com.mesosphere.sdk.state.FrameworkStore)

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