Search in sources :

Example 1 with PodSpec

use of com.mesosphere.sdk.specification.PodSpec 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 PodSpec

use of com.mesosphere.sdk.specification.PodSpec 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 PodSpec

use of com.mesosphere.sdk.specification.PodSpec 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 4 with PodSpec

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

the class CassandraRecoveryPlanOverrider method getNodeRecoveryPhase.

private Phase getNodeRecoveryPhase(Plan inputPlan, int index) {
    Phase inputPhase = inputPlan.getChildren().get(0);
    Step inputLaunchStep = inputPhase.getChildren().get(index);
    // Dig all the way down into the command, so we can append the replace_address option to it.
    PodInstance podInstance = inputLaunchStep.start().get().getPodInstance();
    PodSpec podSpec = podInstance.getPod();
    TaskSpec taskSpec = podSpec.getTasks().stream().filter(t -> t.getName().equals("server")).findFirst().get();
    CommandSpec command = taskSpec.getCommand().get();
    // Get IP address for the pre-existing node.
    Optional<Protos.TaskStatus> status = StateStoreUtils.getTaskStatusFromProperty(stateStore, TaskSpec.getInstanceName(podInstance, taskSpec));
    if (!status.isPresent()) {
        logger.error("No previously stored TaskStatus to pull IP address from in Cassandra recovery");
        return null;
    }
    String replaceIp = status.get().getContainerStatus().getNetworkInfos(0).getIpAddresses(0).getIpAddress();
    DefaultCommandSpec.Builder builder = DefaultCommandSpec.newBuilder(command);
    builder.value(String.format("%s -Dcassandra.replace_address=%s -Dcassandra.consistent.rangemovement=false%n", command.getValue().trim(), replaceIp));
    // Rebuild a new PodSpec with the modified command, and add it to the phase we return.
    TaskSpec newTaskSpec = DefaultTaskSpec.newBuilder(taskSpec).commandSpec(builder.build()).build();
    List<TaskSpec> tasks = podSpec.getTasks().stream().map(t -> {
        if (t.getName().equals(newTaskSpec.getName())) {
            return newTaskSpec;
        }
        return t;
    }).collect(Collectors.toList());
    PodSpec newPodSpec = DefaultPodSpec.newBuilder(podSpec).tasks(tasks).build();
    PodInstance newPodInstance = new DefaultPodInstance(newPodSpec, index);
    PodInstanceRequirement replacePodInstanceRequirement = PodInstanceRequirement.newBuilder(newPodInstance, inputLaunchStep.getPodInstanceRequirement().get().getTasksToLaunch()).recoveryType(RecoveryType.PERMANENT).build();
    Step replaceStep = new RecoveryStep(inputLaunchStep.getName(), replacePodInstanceRequirement, new UnconstrainedLaunchConstrainer(), stateStore);
    List<Step> steps = new ArrayList<>();
    steps.add(replaceStep);
    // Restart all other nodes if replacing a seed node to refresh IP resolution
    int replaceIndex = replaceStep.getPodInstanceRequirement().get().getPodInstance().getIndex();
    if (CassandraSeedUtils.isSeedNode(replaceIndex)) {
        logger.info("Scheduling restart of all nodes other than 'node-{}' to refresh seed node address.", replaceIndex);
        List<Step> restartSteps = inputPhase.getChildren().stream().filter(step -> step.getPodInstanceRequirement().get().getPodInstance().getIndex() != replaceIndex).map(step -> {
            PodInstanceRequirement restartPodInstanceRequirement = PodInstanceRequirement.newBuilder(step.getPodInstanceRequirement().get().getPodInstance(), step.getPodInstanceRequirement().get().getTasksToLaunch()).recoveryType(RecoveryType.TRANSIENT).build();
            return new RecoveryStep(step.getName(), restartPodInstanceRequirement, new UnconstrainedLaunchConstrainer(), stateStore);
        }).collect(Collectors.toList());
        steps.addAll(restartSteps);
    }
    return new DefaultPhase(RECOVERY_PHASE_NAME, steps, new SerialStrategy<>(), Collections.emptyList());
}
Also used : Protos(org.apache.mesos.Protos) java.util(java.util) Logger(org.slf4j.Logger) TaskSpec(com.mesosphere.sdk.specification.TaskSpec) LoggerFactory(org.slf4j.LoggerFactory) StateStoreUtils(com.mesosphere.sdk.state.StateStoreUtils) RecoveryType(com.mesosphere.sdk.scheduler.recovery.RecoveryType) DefaultPodSpec(com.mesosphere.sdk.specification.DefaultPodSpec) RecoveryPlanOverrider(com.mesosphere.sdk.scheduler.recovery.RecoveryPlanOverrider) DefaultTaskSpec(com.mesosphere.sdk.specification.DefaultTaskSpec) Collectors(java.util.stream.Collectors) PodSpec(com.mesosphere.sdk.specification.PodSpec) CommandSpec(com.mesosphere.sdk.specification.CommandSpec) SerialStrategy(com.mesosphere.sdk.scheduler.plan.strategy.SerialStrategy) UnconstrainedLaunchConstrainer(com.mesosphere.sdk.scheduler.recovery.constrain.UnconstrainedLaunchConstrainer) StateStore(com.mesosphere.sdk.state.StateStore) com.mesosphere.sdk.scheduler.plan(com.mesosphere.sdk.scheduler.plan) RecoveryStep(com.mesosphere.sdk.scheduler.recovery.RecoveryStep) PodInstance(com.mesosphere.sdk.specification.PodInstance) DefaultCommandSpec(com.mesosphere.sdk.specification.DefaultCommandSpec) DefaultPodSpec(com.mesosphere.sdk.specification.DefaultPodSpec) PodSpec(com.mesosphere.sdk.specification.PodSpec) PodInstance(com.mesosphere.sdk.specification.PodInstance) TaskSpec(com.mesosphere.sdk.specification.TaskSpec) DefaultTaskSpec(com.mesosphere.sdk.specification.DefaultTaskSpec) RecoveryStep(com.mesosphere.sdk.scheduler.recovery.RecoveryStep) RecoveryStep(com.mesosphere.sdk.scheduler.recovery.RecoveryStep) CommandSpec(com.mesosphere.sdk.specification.CommandSpec) DefaultCommandSpec(com.mesosphere.sdk.specification.DefaultCommandSpec) UnconstrainedLaunchConstrainer(com.mesosphere.sdk.scheduler.recovery.constrain.UnconstrainedLaunchConstrainer) DefaultCommandSpec(com.mesosphere.sdk.specification.DefaultCommandSpec)

Example 5 with PodSpec

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

the class ConfigurationUpdaterTest method testPodsUsersUnsetInPreviousServiceSpecButSetToRootInNewServiceSpec.

@Test
public void testPodsUsersUnsetInPreviousServiceSpecButSetToRootInNewServiceSpec() throws ConfigStoreException {
    final ConfigurationUpdater<ServiceSpec> configurationUpdater = new DefaultConfigurationUpdater(mockStateStore, mockConfigStore, DefaultServiceSpec.getComparatorInstance(), DefaultConfigValidators.getValidators(SchedulerConfigTestUtils.getTestSchedulerConfig()));
    when(mockConfigStore.getTargetConfig()).thenReturn(TARGET_ID);
    DefaultServiceSpec.Builder serviceSpecWithUser = DefaultServiceSpec.newBuilder(ORIGINAL_SERVICE_SPECIFICATION).user(DcosConstants.DEFAULT_SERVICE_USER);
    List<PodSpec> podsWithoutUsers = new ArrayList<>();
    for (PodSpec podSpec : ORIGINAL_SERVICE_SPECIFICATION.getPods()) {
        podsWithoutUsers.add(DefaultPodSpec.newBuilder(podSpec).user(null).build());
    }
    serviceSpecWithUser.pods(podsWithoutUsers);
    when(mockConfigStore.fetch(TARGET_ID)).thenReturn(serviceSpecWithUser.build());
    // set the pod users for the new service spec to "root" to match the default of the non-set user from
    // previous service spec
    List<PodSpec> podsWithUsers = new ArrayList<>();
    for (PodSpec podSpec : ORIGINAL_SERVICE_SPECIFICATION_WITH_USER.getPods()) {
        podsWithUsers.add(DefaultPodSpec.newBuilder(podSpec).user(DcosConstants.DEFAULT_SERVICE_USER).build());
    }
    ServiceSpec SERVICE_SPECIFICATION_WITH_USER = DefaultServiceSpec.newBuilder(ORIGINAL_SERVICE_SPECIFICATION_WITH_USER).pods(podsWithUsers).build();
    ConfigurationUpdater.UpdateResult result = configurationUpdater.updateConfiguration(SERVICE_SPECIFICATION_WITH_USER);
    Assert.assertEquals(TARGET_ID, result.getTargetId());
    Assert.assertEquals(0, result.getErrors().size());
}
Also used : DefaultPodSpec(com.mesosphere.sdk.specification.DefaultPodSpec) PodSpec(com.mesosphere.sdk.specification.PodSpec) DefaultServiceSpec(com.mesosphere.sdk.specification.DefaultServiceSpec) ServiceSpec(com.mesosphere.sdk.specification.ServiceSpec) DefaultServiceSpec(com.mesosphere.sdk.specification.DefaultServiceSpec) Test(org.junit.Test)

Aggregations

PodSpec (com.mesosphere.sdk.specification.PodSpec)35 DefaultPodSpec (com.mesosphere.sdk.specification.DefaultPodSpec)21 Test (org.junit.Test)16 PlacementRule (com.mesosphere.sdk.offer.evaluate.placement.PlacementRule)10 PodInstance (com.mesosphere.sdk.specification.PodInstance)10 DefaultPodInstance (com.mesosphere.sdk.scheduler.plan.DefaultPodInstance)9 Protos (org.apache.mesos.Protos)8 Collectors (java.util.stream.Collectors)7 PodInstanceRequirement (com.mesosphere.sdk.scheduler.plan.PodInstanceRequirement)6 ServiceSpec (com.mesosphere.sdk.specification.ServiceSpec)6 OfferRecommendation (com.mesosphere.sdk.offer.OfferRecommendation)4 TaskLabelReader (com.mesosphere.sdk.offer.taskdata.TaskLabelReader)4 DefaultServiceSpec (com.mesosphere.sdk.specification.DefaultServiceSpec)4 List (java.util.List)4 InvalidRequirementException (com.mesosphere.sdk.offer.InvalidRequirementException)3 TaskException (com.mesosphere.sdk.offer.TaskException)3 ResourceSet (com.mesosphere.sdk.specification.ResourceSet)3 ArrayList (java.util.ArrayList)3 Optional (java.util.Optional)3 ConfigValidationError (com.mesosphere.sdk.config.validate.ConfigValidationError)2