Search in sources :

Example 6 with Operation

use of org.apache.mesos.Protos.Offer.Operation in project dcos-commons by mesosphere.

the class OfferEvaluatorTest method testLaunchMultipleTasksPerExecutor.

@Test
public void testLaunchMultipleTasksPerExecutor() throws Exception {
    Resource offeredResource = ResourceTestUtils.getUnreservedCpus(3.0);
    ResourceSet resourceSetA = DefaultResourceSet.newBuilder(TestConstants.ROLE, Constants.ANY_ROLE, TestConstants.PRINCIPAL).cpus(1.0).id("resourceSetA").build();
    ResourceSet resourceSetB = DefaultResourceSet.newBuilder(TestConstants.ROLE, Constants.ANY_ROLE, TestConstants.PRINCIPAL).cpus(2.0).id("resourceSetB").build();
    CommandSpec commandSpec = DefaultCommandSpec.newBuilder(Collections.emptyMap()).value("./cmd").build();
    TaskSpec taskSpecA = DefaultTaskSpec.newBuilder().name("taskA").commandSpec(commandSpec).goalState(GoalState.RUNNING).resourceSet(resourceSetA).build();
    TaskSpec taskSpecB = DefaultTaskSpec.newBuilder().name("taskB").commandSpec(commandSpec).goalState(GoalState.RUNNING).resourceSet(resourceSetB).build();
    PodSpec podSpec = DefaultPodSpec.newBuilder("executor-uri").addTask(taskSpecA).addTask(taskSpecB).count(1).type(TestConstants.POD_TYPE).build();
    PodInstance podInstance = new DefaultPodInstance(podSpec, 0);
    PodInstanceRequirement podInstanceRequirement = PodInstanceRequirement.newBuilder(podInstance, Arrays.asList("taskA", "taskB")).build();
    List<OfferRecommendation> recommendations = evaluator.evaluate(podInstanceRequirement, Arrays.asList(OfferTestUtils.getCompleteOffer(offeredResource)));
    Assert.assertEquals(7, recommendations.size());
    Assert.assertEquals(Operation.Type.RESERVE, recommendations.get(0).getOperation().getType());
    Assert.assertEquals(Operation.Type.RESERVE, recommendations.get(2).getOperation().getType());
    Operation launchOp0 = recommendations.get(4).getOperation();
    Assert.assertEquals(Operation.Type.LAUNCH_GROUP, launchOp0.getType());
    Operation launchOp1 = recommendations.get(6).getOperation();
    Assert.assertEquals(Operation.Type.LAUNCH_GROUP, launchOp1.getType());
    Protos.ExecutorID launch0ExecutorId = launchOp0.getLaunchGroup().getTaskGroup().getTasks(0).getExecutor().getExecutorId();
    Protos.ExecutorID launch1ExecutorId = launchOp1.getLaunchGroup().getTaskGroup().getTasks(0).getExecutor().getExecutorId();
    Assert.assertEquals(launch0ExecutorId, launch1ExecutorId);
}
Also used : Operation(org.apache.mesos.Protos.Offer.Operation) Protos(org.apache.mesos.Protos) Test(org.junit.Test)

Example 7 with Operation

use of org.apache.mesos.Protos.Offer.Operation in project dcos-commons by mesosphere.

the class OfferEvaluatorTest method testLaunchSequencedTasksInPod.

@Test
public void testLaunchSequencedTasksInPod() throws Exception {
    ServiceSpec serviceSpec = getServiceSpec("resource-set-seq.yml");
    PodSpec podSpec = serviceSpec.getPods().get(0);
    PodInstance podInstance = new DefaultPodInstance(podSpec, 0);
    PodInstanceRequirement podInstanceRequirement = PodInstanceRequirement.newBuilder(podInstance, Arrays.asList("format")).build();
    Offer sufficientOffer = OfferTestUtils.getCompleteOffer(Arrays.asList(ResourceTestUtils.getUnreservedCpus(3.0), ResourceTestUtils.getUnreservedDisk(500.0)));
    // Launch Task with ONCE goal state, for first time.
    List<OfferRecommendation> recommendations = evaluator.evaluate(podInstanceRequirement, Arrays.asList(sufficientOffer));
    Assert.assertEquals(recommendations.toString(), 9, recommendations.size());
    // Validate node task operations
    Operation operation = recommendations.get(0).getOperation();
    Assert.assertEquals(Operation.Type.RESERVE, operation.getType());
    LaunchOfferRecommendation launchOfferRecommendation = (LaunchOfferRecommendation) recommendations.get(4);
    operation = launchOfferRecommendation.getOperation();
    Assert.assertEquals(Operation.Type.LAUNCH_GROUP, operation.getType());
    Assert.assertEquals("name-0-backup", operation.getLaunchGroup().getTaskGroup().getTasks(0).getName());
    Assert.assertFalse(launchOfferRecommendation.shouldLaunch());
    // Validate format task operations
    operation = recommendations.get(5).getOperation();
    Assert.assertEquals(Operation.Type.RESERVE, operation.getType());
    operation = recommendations.get(6).getOperation();
    Assert.assertEquals(Operation.Type.RESERVE, operation.getType());
    operation = recommendations.get(7).getOperation();
    Assert.assertEquals(Operation.Type.CREATE, operation.getType());
    launchOfferRecommendation = (LaunchOfferRecommendation) recommendations.get(8);
    operation = launchOfferRecommendation.getOperation();
    Assert.assertEquals(Operation.Type.LAUNCH_GROUP, operation.getType());
    Assert.assertEquals("name-0-format", operation.getLaunchGroup().getTaskGroup().getTasks(0).getName());
    Assert.assertTrue(launchOfferRecommendation.shouldLaunch());
    recordOperations(recommendations);
    // Launch Task with RUNNING goal state, later.
    podInstanceRequirement = PodInstanceRequirement.newBuilder(podInstance, Arrays.asList("node")).build();
    recommendations = evaluator.evaluate(podInstanceRequirement, Arrays.asList(sufficientOffer));
    // Providing sufficient, but unreserved resources should result in no operations.
    Assert.assertEquals(0, recommendations.size());
    Resource cpuResource = operation.getLaunchGroup().getTaskGroup().getTasks(0).getResources(0);
    Resource diskResource = operation.getLaunchGroup().getTaskGroup().getTasks(0).getResources(1);
    String cpuResourceId = ResourceTestUtils.getResourceId(cpuResource);
    String diskResourceId = ResourceTestUtils.getResourceId(diskResource);
    String persistenceId = ResourceTestUtils.getPersistenceId(diskResource);
    ExecutorInfo executorInfo = stateStore.fetchTasks().iterator().next().getExecutor();
    Collection<Resource> expectedResources = getExpectedExecutorResources(executorInfo);
    expectedResources.addAll(Arrays.asList(ResourceTestUtils.getReservedCpus(1.0, cpuResourceId), ResourceTestUtils.getReservedRootVolume(50.0, diskResourceId, persistenceId)));
    Offer offer = OfferTestUtils.getOffer(expectedResources).toBuilder().addExecutorIds(executorInfo.getExecutorId()).build();
    recommendations = evaluator.evaluate(podInstanceRequirement, Arrays.asList(offer));
    // Providing the expected reserved resources should result in a LAUNCH operation.
    Assert.assertEquals(1, recommendations.size());
    operation = recommendations.get(0).getOperation();
    Assert.assertEquals(Operation.Type.LAUNCH_GROUP, operation.getType());
    Assert.assertEquals("name-0-node", operation.getLaunchGroup().getTaskGroup().getTasks(0).getName());
}
Also used : Operation(org.apache.mesos.Protos.Offer.Operation) Test(org.junit.Test)

Example 8 with Operation

use of org.apache.mesos.Protos.Offer.Operation in project dcos-commons by mesosphere.

the class OfferEvaluatorTest method testReserveLaunchScalar.

@Test
public void testReserveLaunchScalar() throws Exception {
    PodInstanceRequirement podInstanceRequirement = PodInstanceRequirementTestUtils.getCpuRequirement(1.0);
    Resource offeredResource = ResourceTestUtils.getUnreservedCpus(2.0);
    List<OfferRecommendation> recommendations = evaluator.evaluate(podInstanceRequirement, Arrays.asList(OfferTestUtils.getCompleteOffer(offeredResource)));
    Assert.assertEquals(5, recommendations.size());
    // Validate RESERVE Operation
    Operation reserveOperation = recommendations.get(0).getOperation();
    Resource reserveResource = reserveOperation.getReserve().getResources(0);
    Resource.ReservationInfo reservation = ResourceUtils.getReservation(reserveResource).get();
    Assert.assertEquals(Operation.Type.RESERVE, reserveOperation.getType());
    Assert.assertEquals(1.0, reserveResource.getScalar().getValue(), 0.0);
    validateRole(reserveResource);
    Assert.assertEquals(TestConstants.ROLE, ResourceUtils.getRole(reserveResource));
    Assert.assertEquals(TestConstants.PRINCIPAL, reservation.getPrincipal());
    Assert.assertEquals(36, getResourceId(reserveResource).length());
    Assert.assertFalse(reserveResource.hasDisk());
    // Validate LAUNCH Operation
    Operation launchOperation = recommendations.get(4).getOperation();
    Resource launchResource = launchOperation.getLaunchGroup().getTaskGroup().getTasks(0).getResources(0);
    Assert.assertEquals(Operation.Type.LAUNCH_GROUP, launchOperation.getType());
    Assert.assertEquals(getResourceId(reserveResource), getResourceId(launchResource));
    Protos.ExecutorID executorId = launchOperation.getLaunchGroup().getExecutor().getExecutorId();
    Assert.assertEquals(TestConstants.POD_TYPE, CommonIdUtils.toExecutorName(executorId));
}
Also used : Protos(org.apache.mesos.Protos) Operation(org.apache.mesos.Protos.Offer.Operation) Test(org.junit.Test)

Example 9 with Operation

use of org.apache.mesos.Protos.Offer.Operation in project dcos-commons by mesosphere.

the class OfferEvaluatorTest method testResourceRefinementSucceeds.

@SuppressWarnings("deprecated")
@Test
public void testResourceRefinementSucceeds() throws Exception {
    ResourceRefinementCapabilityContext context = new ResourceRefinementCapabilityContext(Capabilities.getInstance());
    try {
        ServiceSpec serviceSpec = getServiceSpec("resource-refinement.yml");
        Assert.assertEquals(TestConstants.PRE_RESERVED_ROLE, serviceSpec.getPods().get(0).getPreReservedRole());
        Offer sufficientOffer = OfferTestUtils.getCompleteOffer(Arrays.asList(// Include executor resources.
        ResourceTestUtils.getUnreservedCpus(0.1), ResourceTestUtils.getUnreservedMem(256), ResourceTestUtils.getUnreservedDisk(512), ResourceTestUtils.getUnreservedCpus(3.0)).stream().map(r -> r.toBuilder().setRole(Constants.ANY_ROLE).addReservations(Resource.ReservationInfo.newBuilder().setRole(TestConstants.PRE_RESERVED_ROLE).setType(Resource.ReservationInfo.Type.STATIC)).build()).collect(Collectors.toList()));
        PodSpec podSpec = serviceSpec.getPods().get(0);
        PodInstance podInstance = new DefaultPodInstance(podSpec, 0);
        List<String> tasksToLaunch = Arrays.asList("test-task");
        PodInstanceRequirement podInstanceRequirement = PodInstanceRequirement.newBuilder(podInstance, tasksToLaunch).build();
        List<OfferRecommendation> recommendations = evaluator.evaluate(podInstanceRequirement, Arrays.asList(sufficientOffer));
        Assert.assertEquals(5, recommendations.size());
        Operation reserveOperation = recommendations.get(0).getOperation();
        Resource reserveResource = reserveOperation.getReserve().getResources(0);
        Assert.assertEquals(2, reserveResource.getReservationsCount());
        Resource.ReservationInfo preReservation = reserveResource.getReservations(0);
        Assert.assertEquals(Resource.ReservationInfo.Type.STATIC, preReservation.getType());
        Assert.assertEquals(TestConstants.PRE_RESERVED_ROLE, preReservation.getRole());
        Assert.assertFalse(preReservation.hasLabels());
        Resource.ReservationInfo dynamicReservation = reserveResource.getReservations(1);
        Assert.assertEquals(Resource.ReservationInfo.Type.DYNAMIC, dynamicReservation.getType());
        Assert.assertEquals(TestConstants.PRE_RESERVED_ROLE + "/hello-world-role", dynamicReservation.getRole());
        Assert.assertTrue(dynamicReservation.hasLabels());
    } finally {
        context.reset();
    }
}
Also used : Operation(org.apache.mesos.Protos.Offer.Operation) ResourceRefinementCapabilityContext(com.mesosphere.sdk.dcos.ResourceRefinementCapabilityContext) Test(org.junit.Test)

Example 10 with Operation

use of org.apache.mesos.Protos.Offer.Operation in project dcos-commons by mesosphere.

the class OfferEvaluatorPortsTest method testUpdateStaticToStaticPortCustomExecutor.

@Test
public void testUpdateStaticToStaticPortCustomExecutor() throws Exception {
    useCustomExecutor();
    // Launch for the first time: get port 555
    Resource reserveResource = recordLaunchWithOfferedResources(PodInstanceRequirementTestUtils.getPortRequirement(555), ResourceTestUtils.getUnreservedPorts(555, 555)).get(0);
    String resourceId = getResourceId(reserveResource);
    // Now lets move to port 666:
    List<OfferRecommendation> recommendations = evaluator.evaluate(PodInstanceRequirementTestUtils.getPortRequirement(666), OfferTestUtils.getOffers(Arrays.asList(ResourceTestUtils.getReservedPorts(555, 555, resourceId), ResourceTestUtils.getUnreservedPorts(666, 666))));
    // UNRESERVE, RESERVE, LAUNCH
    Assert.assertEquals(recommendations.toString(), 3, recommendations.size());
    Assert.assertEquals(Operation.Type.UNRESERVE, recommendations.get(0).getOperation().getType());
    Assert.assertEquals(Operation.Type.RESERVE, recommendations.get(1).getOperation().getType());
    Assert.assertEquals(Operation.Type.LAUNCH, recommendations.get(2).getOperation().getType());
    Operation launchOperation = recommendations.get(2).getOperation();
    TaskInfo taskInfo = launchOperation.getLaunch().getTaskInfos(0);
    Map<String, String> envvars = EnvUtils.toMap(TaskPackingUtils.unpack(taskInfo).getCommand().getEnvironment());
    Assert.assertEquals(String.valueOf(666), envvars.get(TestConstants.PORT_ENV_NAME + "_666"));
}
Also used : TaskInfo(org.apache.mesos.Protos.TaskInfo) Resource(org.apache.mesos.Protos.Resource) Operation(org.apache.mesos.Protos.Offer.Operation) OfferRecommendation(com.mesosphere.sdk.offer.OfferRecommendation) Test(org.junit.Test)

Aggregations

Operation (org.apache.mesos.Protos.Offer.Operation)34 Test (org.junit.Test)34 OfferRecommendation (com.mesosphere.sdk.offer.OfferRecommendation)22 Resource (org.apache.mesos.Protos.Resource)22 PodInstanceRequirement (com.mesosphere.sdk.scheduler.plan.PodInstanceRequirement)11 Protos (org.apache.mesos.Protos)9 TaskInfo (org.apache.mesos.Protos.TaskInfo)9 DefaultVolumeSpec (com.mesosphere.sdk.specification.DefaultVolumeSpec)4 ResourceRefinementCapabilityContext (com.mesosphere.sdk.dcos.ResourceRefinementCapabilityContext)3 DefaultResourceSet (com.mesosphere.sdk.specification.DefaultResourceSet)3 ResourceSet (com.mesosphere.sdk.specification.ResourceSet)3 DiscoveryInfo (org.apache.mesos.Protos.DiscoveryInfo)3 Label (org.apache.mesos.Protos.Label)3 Port (org.apache.mesos.Protos.Port)3 Capabilities (com.mesosphere.sdk.dcos.Capabilities)2 Constants (com.mesosphere.sdk.offer.Constants)2 ResourceUtils (com.mesosphere.sdk.offer.ResourceUtils)2 PodInstanceRequirementTestUtils (com.mesosphere.sdk.scheduler.plan.PodInstanceRequirementTestUtils)2 VolumeSpec (com.mesosphere.sdk.specification.VolumeSpec)2 OfferTestUtils (com.mesosphere.sdk.testutils.OfferTestUtils)2