Search in sources :

Example 21 with Resource

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

the class OfferEvaluatorPortsTest method testUpdateStaticToStaticPort.

@Test
public void testUpdateStaticToStaticPort() throws Exception {
    // Launch for the first time: get port 555
    Resource reserveResource = recordLaunchWithCompleteOfferedResources(PodInstanceRequirementTestUtils.getPortRequirement(555), ResourceTestUtils.getUnreservedPorts(555, 555)).get(0);
    String resourceId = getResourceId(reserveResource);
    Collection<Resource> expectedResources = getExpectedExecutorResources(stateStore.fetchTasks().iterator().next().getExecutor());
    expectedResources.addAll(Arrays.asList(ResourceTestUtils.getReservedPorts(555, 555, resourceId), ResourceTestUtils.getUnreservedPorts(666, 666)));
    // Now lets move to port 666:
    List<OfferRecommendation> recommendations = evaluator.evaluate(PodInstanceRequirementTestUtils.getPortRequirement(666), Arrays.asList(OfferTestUtils.getOffer(expectedResources)));
    // 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_GROUP, recommendations.get(2).getOperation().getType());
    Operation launchOperation = recommendations.get(2).getOperation();
    TaskInfo taskInfo = launchOperation.getLaunchGroup().getTaskGroup().getTasks(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)

Example 22 with Resource

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

the class VolumeEvaluationStage method evaluate.

@Override
public EvaluationOutcome evaluate(MesosResourcePool mesosResourcePool, PodInfoBuilder podInfoBuilder) {
    String detailsClause = resourceId.isPresent() ? "previously reserved " : "";
    List<OfferRecommendation> offerRecommendations = new ArrayList<>();
    Resource resource;
    final MesosResource mesosResource;
    boolean isRunningExecutor = OfferEvaluationUtils.isRunningExecutor(podInfoBuilder, mesosResourcePool.getOffer());
    if (!taskName.isPresent() && isRunningExecutor && resourceId.isPresent() && persistenceId.isPresent()) {
        // This is a volume on a running executor, so it isn't present in the offer, but we need to make sure to
        // add it to the ExecutorInfo.
        podInfoBuilder.setExecutorVolume(volumeSpec);
        Resource volume = PodInfoBuilder.getExistingExecutorVolume(volumeSpec, resourceId, resourceNamespace, persistenceId, sourceRoot, useDefaultExecutor);
        podInfoBuilder.getExecutorBuilder().get().addResources(volume);
        return pass(this, Collections.emptyList(), "Setting info for already running Executor with existing volume " + "with resourceId: '%s' and persistenceId: '%s'", resourceId, persistenceId).build();
    }
    if (volumeSpec.getType().equals(VolumeSpec.Type.ROOT)) {
        OfferEvaluationUtils.ReserveEvaluationOutcome reserveEvaluationOutcome = OfferEvaluationUtils.evaluateSimpleResource(this, volumeSpec, resourceId, resourceNamespace, mesosResourcePool);
        EvaluationOutcome evaluationOutcome = reserveEvaluationOutcome.getEvaluationOutcome();
        if (!evaluationOutcome.isPassing()) {
            return evaluationOutcome;
        }
        offerRecommendations.addAll(evaluationOutcome.getOfferRecommendations());
        mesosResource = evaluationOutcome.getMesosResource().get();
        resource = ResourceBuilder.fromSpec(volumeSpec, reserveEvaluationOutcome.getResourceId(), resourceNamespace, persistenceId, Optional.empty()).setMesosResource(mesosResource).build();
    } else {
        Optional<MesosResource> mesosResourceOptional;
        if (!resourceId.isPresent()) {
            mesosResourceOptional = mesosResourcePool.consumeAtomic(Constants.DISK_RESOURCE_TYPE, volumeSpec.getValue());
        } else {
            mesosResourceOptional = mesosResourcePool.getReservedResourceById(resourceId.get());
        }
        if (!mesosResourceOptional.isPresent()) {
            return fail(this, "Failed to find MOUNT volume for '%s'.", volumeSpec).build();
        }
        mesosResource = mesosResourceOptional.get();
        resource = ResourceBuilder.fromSpec(volumeSpec, resourceId, resourceNamespace, persistenceId, Optional.of(mesosResource.getResource().getDisk().getSource().getMount().getRoot())).setValue(mesosResource.getValue()).setMesosResource(mesosResource).build();
        if (!resourceId.isPresent()) {
            // Initial reservation of resources
            logger.info("Resource '{}' requires a RESERVE operation", volumeSpec.getName());
            offerRecommendations.add(new ReserveOfferRecommendation(mesosResourcePool.getOffer(), resource));
        }
    }
    if (createsVolume()) {
        logger.info("Resource '{}' requires a CREATE operation", volumeSpec.getName());
        offerRecommendations.add(new CreateOfferRecommendation(mesosResourcePool.getOffer(), resource));
    }
    logger.info("Generated '{}' resource for task: [{}]", volumeSpec.getName(), TextFormat.shortDebugString(resource));
    OfferEvaluationUtils.setProtos(podInfoBuilder, resource, taskName);
    if (!taskName.isPresent() && useDefaultExecutor) {
        podInfoBuilder.setExecutorVolume(volumeSpec);
    }
    return pass(this, offerRecommendations, "Offer contains sufficient %s'disk': for resource: '%s' with resourceId: '%s' and persistenceId: '%s'", detailsClause, volumeSpec, resourceId, persistenceId).mesosResource(mesosResource).build();
}
Also used : Resource(org.apache.mesos.Protos.Resource)

Example 23 with Resource

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

the class OfferEvaluatorVolumesTest method testReserveCreateLaunchMountVolume.

@Test
public void testReserveCreateLaunchMountVolume() throws Exception {
    Resource offeredCpuResource = ResourceTestUtils.getUnreservedCpus(1.0);
    Resource offeredDiskResource = ResourceTestUtils.getUnreservedMountVolume(2000);
    List<OfferRecommendation> recommendations = evaluator.evaluate(PodInstanceRequirementTestUtils.getMountVolumeRequirement(1.0, 1500), Arrays.asList(OfferTestUtils.getCompleteOffer(Arrays.asList(offeredCpuResource, offeredDiskResource))));
    // RESERVE, RESERVE, CREATE, RESERVE, RESERVE, RESERVE, LAUNCH
    Assert.assertEquals(7, recommendations.size());
    // Validate RESERVE Operation
    Operation reserveOperation = recommendations.get(1).getOperation();
    Resource reserveResource = reserveOperation.getReserve().getResources(0);
    Resource.ReservationInfo reservation = ResourceUtils.getReservation(reserveResource).get();
    Assert.assertEquals(Operation.Type.RESERVE, reserveOperation.getType());
    Assert.assertEquals(2000, reserveResource.getScalar().getValue(), 0.0);
    Assert.assertEquals(TestConstants.MOUNT_ROOT, reserveResource.getDisk().getSource().getMount().getRoot());
    Assert.assertEquals(TestConstants.PRINCIPAL, reservation.getPrincipal());
    Assert.assertEquals(36, getResourceId(reserveResource).length());
    Assert.assertTrue(reserveResource.hasDisk());
    Assert.assertFalse(reserveResource.getDisk().hasPersistence());
    Assert.assertFalse(reserveResource.getDisk().hasVolume());
    // Validate CREATE Operation
    String resourceId = getResourceId(reserveResource);
    Operation createOperation = recommendations.get(2).getOperation();
    Resource createResource = createOperation.getCreate().getVolumes(0);
    Assert.assertEquals(resourceId, getResourceId(createResource));
    Assert.assertEquals(36, createResource.getDisk().getPersistence().getId().length());
    Assert.assertEquals(TestConstants.MOUNT_ROOT, createResource.getDisk().getSource().getMount().getRoot());
    Assert.assertEquals(TestConstants.PRINCIPAL, createResource.getDisk().getPersistence().getPrincipal());
    Assert.assertTrue(createResource.getDisk().hasVolume());
    // Validate LAUNCH Operation
    String persistenceId = createResource.getDisk().getPersistence().getId();
    Operation launchOperation = recommendations.get(6).getOperation();
    Resource launchResource = launchOperation.getLaunchGroup().getTaskGroup().getTasks(0).getResources(1);
    Assert.assertEquals(Operation.Type.LAUNCH_GROUP, launchOperation.getType());
    Assert.assertEquals(resourceId, getResourceId(launchResource));
    Assert.assertEquals(persistenceId, launchResource.getDisk().getPersistence().getId());
    Assert.assertEquals(TestConstants.MOUNT_ROOT, launchResource.getDisk().getSource().getMount().getRoot());
    Assert.assertEquals(TestConstants.PRINCIPAL, launchResource.getDisk().getPersistence().getPrincipal());
    Assert.assertEquals(2000, launchResource.getScalar().getValue(), 0.0);
}
Also used : Resource(org.apache.mesos.Protos.Resource) Operation(org.apache.mesos.Protos.Offer.Operation) OfferRecommendation(com.mesosphere.sdk.offer.OfferRecommendation) Test(org.junit.Test)

Example 24 with Resource

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

the class OfferEvaluatorVolumesTest method testExpectedRootVolume.

@Test
public void testExpectedRootVolume() throws Exception {
    // Launch for the first time.
    Resource offeredCpuResource = ResourceTestUtils.getUnreservedCpus(1.0);
    Resource offeredDiskResource = ResourceTestUtils.getUnreservedDisk(2000);
    PodInstanceRequirement podInstanceRequirement = PodInstanceRequirementTestUtils.getRootVolumeRequirement(1.0, 1500);
    List<OfferRecommendation> recommendations = evaluator.evaluate(podInstanceRequirement, Arrays.asList(OfferTestUtils.getCompleteOffer(Arrays.asList(offeredDiskResource, offeredCpuResource))));
    String cpuResourceId = ResourceTestUtils.getResourceId(recommendations.get(0).getOperation().getReserve().getResources(0));
    Operation createOperation = recommendations.get(2).getOperation();
    Resource createResource = createOperation.getCreate().getVolumes(0);
    String diskResourceId = ResourceTestUtils.getResourceId(createResource);
    String persistenceId = ResourceTestUtils.getPersistenceId(createResource);
    String executorCpuResourceId = ResourceTestUtils.getResourceId(recommendations.get(3).getOperation().getReserve().getResources(0));
    String executorMemResourceId = ResourceTestUtils.getResourceId(recommendations.get(4).getOperation().getReserve().getResources(0));
    String executorDiskResourceId = ResourceTestUtils.getResourceId(recommendations.get(5).getOperation().getReserve().getResources(0));
    Operation launchOperation = recommendations.get(recommendations.size() - 1).getOperation();
    Protos.ExecutorInfo executorInfo = launchOperation.getLaunchGroup().getExecutor();
    Collection<Protos.TaskInfo> taskInfos = launchOperation.getLaunchGroup().getTaskGroup().getTasksList().stream().map(t -> t.toBuilder().setExecutor(executorInfo).build()).collect(Collectors.toList());
    stateStore.storeTasks(taskInfos);
    // Launch again on expected resources.
    Resource expectedCpu = ResourceTestUtils.getReservedCpus(1.0, cpuResourceId);
    Resource expectedDisk = ResourceTestUtils.getReservedRootVolume(1500, diskResourceId, persistenceId);
    Resource expectedExecutorCpu = ResourceTestUtils.getReservedCpus(0.1, executorCpuResourceId);
    Resource expectedExecutorMem = ResourceTestUtils.getReservedMem(32, executorMemResourceId);
    Resource expectedExecutorDisk = ResourceTestUtils.getReservedDisk(256, executorDiskResourceId);
    recommendations = evaluator.evaluate(podInstanceRequirement, Arrays.asList(OfferTestUtils.getOffer(Arrays.asList(expectedCpu, expectedDisk, expectedExecutorCpu, expectedExecutorMem, expectedExecutorDisk))));
    Assert.assertEquals(1, recommendations.size());
    launchOperation = recommendations.get(0).getOperation();
    Protos.TaskInfo launchTask = launchOperation.getLaunchGroup().getTaskGroup().getTasks(0);
    Assert.assertEquals(recommendations.toString(), 2, launchTask.getResourcesCount());
    Resource launchResource = launchTask.getResources(1);
    Resource.ReservationInfo reservation = ResourceUtils.getReservation(launchResource).get();
    Assert.assertEquals(Operation.Type.LAUNCH_GROUP, launchOperation.getType());
    Assert.assertEquals(1500, launchResource.getScalar().getValue(), 0.0);
    OfferEvaluatorTest.validateRole(launchResource);
    Assert.assertEquals(TestConstants.ROLE, ResourceUtils.getRole(launchResource));
    Assert.assertEquals(persistenceId, launchResource.getDisk().getPersistence().getId());
    Assert.assertEquals(TestConstants.PRINCIPAL, launchResource.getDisk().getPersistence().getPrincipal());
    Assert.assertEquals(TestConstants.PRINCIPAL, reservation.getPrincipal());
    Assert.assertEquals(diskResourceId, getResourceId(launchResource));
}
Also used : Protos(org.apache.mesos.Protos) Arrays(java.util.Arrays) TestConstants(com.mesosphere.sdk.testutils.TestConstants) Collection(java.util.Collection) Operation(org.apache.mesos.Protos.Offer.Operation) PodInstanceRequirementTestUtils(com.mesosphere.sdk.scheduler.plan.PodInstanceRequirementTestUtils) Test(org.junit.Test) OfferTestUtils(com.mesosphere.sdk.testutils.OfferTestUtils) OfferRecommendation(com.mesosphere.sdk.offer.OfferRecommendation) Collectors(java.util.stream.Collectors) Resource(org.apache.mesos.Protos.Resource) ResourceSet(com.mesosphere.sdk.specification.ResourceSet) Capabilities(com.mesosphere.sdk.dcos.Capabilities) PodInstanceRequirement(com.mesosphere.sdk.scheduler.plan.PodInstanceRequirement) List(java.util.List) ResourceRefinementCapabilityContext(com.mesosphere.sdk.dcos.ResourceRefinementCapabilityContext) DefaultVolumeSpec(com.mesosphere.sdk.specification.DefaultVolumeSpec) VolumeSpec(com.mesosphere.sdk.specification.VolumeSpec) ResourceUtils(com.mesosphere.sdk.offer.ResourceUtils) DefaultResourceSet(com.mesosphere.sdk.specification.DefaultResourceSet) Constants(com.mesosphere.sdk.offer.Constants) Assert(org.junit.Assert) ResourceTestUtils(com.mesosphere.sdk.testutils.ResourceTestUtils) Resource(org.apache.mesos.Protos.Resource) Operation(org.apache.mesos.Protos.Offer.Operation) PodInstanceRequirement(com.mesosphere.sdk.scheduler.plan.PodInstanceRequirement) OfferRecommendation(com.mesosphere.sdk.offer.OfferRecommendation) Protos(org.apache.mesos.Protos) Test(org.junit.Test)

Example 25 with Resource

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

the class OfferEvaluatorVolumesTest method testRelaunchExecutorVolumeFailure.

@Test
public void testRelaunchExecutorVolumeFailure() throws Exception {
    // Create for the first time.
    List<Resource> offeredResources = Arrays.asList(ResourceTestUtils.getUnreservedMountVolume(2000), ResourceTestUtils.getUnreservedCpus(1.0));
    Protos.Offer offer = OfferTestUtils.getCompleteOffer(offeredResources);
    PodInstanceRequirement podInstanceRequirement = PodInstanceRequirementTestUtils.getExecutorRequirement(PodInstanceRequirementTestUtils.getCpuResourceSet(1.0), Arrays.asList(new DefaultVolumeSpec(1000, VolumeSpec.Type.MOUNT, TestConstants.CONTAINER_PATH, TestConstants.ROLE, Constants.ANY_ROLE, TestConstants.PRINCIPAL)), TestConstants.POD_TYPE, 0);
    List<OfferRecommendation> recommendations = evaluator.evaluate(podInstanceRequirement, Arrays.asList(offer));
    Assert.assertEquals(7, recommendations.size());
    // Validate RESERVE Operation
    Operation reserveOperation = recommendations.get(0).getOperation();
    Resource reserveResource = reserveOperation.getReserve().getResources(0);
    String resourceId = getResourceId(reserveResource);
    // Validate CREATE Operation
    Operation createOperation = recommendations.get(1).getOperation();
    Resource createResource = createOperation.getCreate().getVolumes(0);
    String persistenceId = createResource.getDisk().getPersistence().getId();
    // Evaluation for a second time
    offeredResources = Arrays.asList(ResourceTestUtils.getReservedMountVolume(2000, resourceId, persistenceId), ResourceTestUtils.getReservedCpus(1.0, resourceId));
    offer = OfferTestUtils.getCompleteOffer(offeredResources);
    recommendations = evaluator.evaluate(podInstanceRequirement, Arrays.asList(offer));
    Assert.assertEquals(0, recommendations.size());
}
Also used : Protos(org.apache.mesos.Protos) Resource(org.apache.mesos.Protos.Resource) DefaultVolumeSpec(com.mesosphere.sdk.specification.DefaultVolumeSpec) Operation(org.apache.mesos.Protos.Offer.Operation) PodInstanceRequirement(com.mesosphere.sdk.scheduler.plan.PodInstanceRequirement) OfferRecommendation(com.mesosphere.sdk.offer.OfferRecommendation) Test(org.junit.Test)

Aggregations

Resource (org.apache.mesos.Protos.Resource)40 Test (org.junit.Test)35 OfferRecommendation (com.mesosphere.sdk.offer.OfferRecommendation)26 Operation (org.apache.mesos.Protos.Offer.Operation)25 PodInstanceRequirement (com.mesosphere.sdk.scheduler.plan.PodInstanceRequirement)13 Protos (org.apache.mesos.Protos)13 TaskInfo (org.apache.mesos.Protos.TaskInfo)12 Offer (org.apache.mesos.Protos.Offer)9 DefaultResourceSet (com.mesosphere.sdk.specification.DefaultResourceSet)4 DefaultVolumeSpec (com.mesosphere.sdk.specification.DefaultVolumeSpec)4 ResourceSet (com.mesosphere.sdk.specification.ResourceSet)4 DiscoveryInfo (org.apache.mesos.Protos.DiscoveryInfo)4 Label (org.apache.mesos.Protos.Label)4 Port (org.apache.mesos.Protos.Port)4 ResourceUtils (com.mesosphere.sdk.offer.ResourceUtils)3 PodInstanceRequirementTestUtils (com.mesosphere.sdk.scheduler.plan.PodInstanceRequirementTestUtils)3 OfferTestUtils (com.mesosphere.sdk.testutils.OfferTestUtils)3 ResourceTestUtils (com.mesosphere.sdk.testutils.ResourceTestUtils)3 TestConstants (com.mesosphere.sdk.testutils.TestConstants)3 Arrays (java.util.Arrays)3