org.apache.mesos.Protos.Resource Java Examples

The following examples show how to use org.apache.mesos.Protos.Resource. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ComposeRewriteHelper.java    From docker-compose-executor with Apache License 2.0 6 votes vote down vote up
private Iterator<Long> getPortMappingIterator(TaskInfo taskInfo) {
    List<Resource> list = taskInfo.getResourcesList();
    List<Long> ports = new ArrayList<Long>();
    for (Resource resource : list) {
        String name = resource.getName();
        if ("ports".equals(name)) {
            Ranges ranges = resource.getRanges();
            for (Range range : ranges.getRangeList()) {
                long startPort = range.getBegin();
                long endPort = range.getEnd();
                for (int i = 0; i <= endPort - startPort; i++) {
                    ports.add(startPort + i);
                }
            }
        }
    }
    return ports.iterator();
}
 
Example #2
Source File: ValueUtils.java    From dcos-commons with Apache License 2.0 6 votes vote down vote up
public static Value getValue(Resource resource) {
  Type type = resource.getType();
  Value.Builder builder = Value.newBuilder();
  builder.setType(type);

  switch (type) {
    case SCALAR:
      return builder.setScalar(resource.getScalar()).build();
    case RANGES:
      return builder.setRanges(resource.getRanges()).build();
    case SET:
      return builder.setSet(resource.getSet()).build();
    default:
      throw new IllegalArgumentException(String.format("Unsupported value type %s in resource %s",
          type,
          TextFormat.shortDebugString(resource)));
  }
}
 
Example #3
Source File: MesosResourcePool.java    From dcos-commons with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("deprecation")
private void freeAtomicResource(MesosResource mesosResource) {
  Resource.Builder resBuilder = Resource.newBuilder(mesosResource.getResource());
  resBuilder.clearReservation();
  resBuilder.setRole(Constants.ANY_ROLE);

  if (resBuilder.hasDisk()) {
    Resource.DiskInfo.Builder diskBuilder = Resource.DiskInfo.newBuilder(resBuilder.getDisk());
    diskBuilder.clearPersistence();
    diskBuilder.clearVolume();
    resBuilder.setDisk(diskBuilder.build());
  }

  Resource releasedResource = resBuilder.build();

  List<MesosResource> resList = unreservedAtomicPool.get(mesosResource.getName());
  if (resList == null) {
    resList = new ArrayList<>();
  }

  resList.add(new MesosResource(releasedResource));
  unreservedAtomicPool.put(mesosResource.getName(), resList);
}
 
Example #4
Source File: UnreserveOfferRecommendation.java    From dcos-commons with Apache License 2.0 6 votes vote down vote up
public UnreserveOfferRecommendation(Offer offer, Resource resource) {
  this.offer = offer;
  Resource.Builder resourceBuilder = resource.toBuilder();

  Resource resourceR;
  // If non-root disk resource, we want to clear ALL fields except for the field indicating the disk source.
  if (resource.hasDisk() && resource.getDisk().hasSource()) {
    resourceR = resourceBuilder
        .setDisk(Resource.DiskInfo.newBuilder().setSource(resource.getDisk().getSource()))
        .build();
  } else {
    resourceR = resourceBuilder.clearDisk().clearRevocable().build();
  }

  this.operation = Operation.newBuilder()
      .setType(Operation.Type.UNRESERVE)
      .setUnreserve(
          Operation.Unreserve.newBuilder().addAllResources(Collections.singletonList(resourceR))
      )
      .build();
  this.resource = resourceR;
}
 
Example #5
Source File: MesosUtilities.java    From oodt with Apache License 2.0 6 votes vote down vote up
/**
 * Creates string out an offer in a nice format.
 * @param resource - mesos resource to make into string.
 * @return string representing a resource.
 */
public static String getResourceMessage(Resource resource) {
    Type type = resource.getType();
    String ret = resource.getName() +" "+resource.getRole()+ ": ";
    switch (type) {
        case SCALAR:
            ret += resource.getScalar().getValue();
            break;
        case RANGES:
            for (Range range : resource.getRanges().getRangeList())
                ret += range.getBegin() + " - "+range.getEnd()+",";
            break;
        case TEXT:
            ret += " TEXT type...cannot find.";
            break;
        case SET:
            for (String string : resource.getSet().getItemList())
                ret += string + ",";
            break;
    }
    return ret;
}
 
Example #6
Source File: OfferEvaluatorVolumesTest.java    From dcos-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void testConsumeMultipleMountVolumesFailure() throws Exception {
    Resource offeredResource = ResourceTestUtils.getUnreservedMountVolume(2000, Optional.empty());
    ResourceSet volumeResourceSet = DefaultResourceSet.newBuilder(TestConstants.ROLE, Constants.ANY_ROLE, TestConstants.PRINCIPAL)
            .id(TestConstants.RESOURCE_SET_ID)
            .cpus(1.0)
            .addMountVolume(1000.0, TestConstants.CONTAINER_PATH + "-A", Collections.emptyList())
            .addMountVolume(1000.0, TestConstants.CONTAINER_PATH + "-B", Collections.emptyList())
            .build();
    PodInstanceRequirement podInstanceRequirement =
            PodInstanceRequirementTestUtils.getRequirement(volumeResourceSet, 0);
    Protos.Offer offer = OfferTestUtils.getCompleteOffer(Arrays.asList(offeredResource));

    List<OfferRecommendation> recommendations = evaluator.evaluate(podInstanceRequirement, Arrays.asList(offer));
    Assert.assertEquals(0, recommendations.size());
}
 
Example #7
Source File: ResourceMesosScheduler.java    From oodt with Apache License 2.0 6 votes vote down vote up
/**
 * Builds a TaskInfo from the given jobspec
 * @param job - JobSpec to TaskInfo-ify
 * @param offer - offer add extra data (SlaveId)
 * @return TaskInfo fully formed
 */
private TaskInfo getTaskInfo(JobSpec job,Offer offer) {
    TaskID taskId = TaskID.newBuilder().setValue(job.getJob().getId()).build();
    TaskInfo info = TaskInfo.newBuilder().setName("task " + taskId.getValue())
             .setTaskId(taskId)
             .setSlaveId(offer.getSlaveId())
             .addResources(Resource.newBuilder()
                           .setName("cpus")
                           .setType(Value.Type.SCALAR)
                           .setScalar(Value.Scalar.newBuilder().setValue(job.getJob().getLoadValue()*1.0)))
             .addResources(Resource.newBuilder()
                           .setName("mem")
                           .setType(Value.Type.SCALAR)
                           .setScalar(Value.Scalar.newBuilder().setValue(job.getJob().getLoadValue()*1024.0)))
             .setExecutor(ExecutorInfo.newBuilder(executor)).setData(MesosUtilities.jobSpecToByteString(job)).build();
    return info;
}
 
Example #8
Source File: ResourceTestUtils.java    From dcos-commons with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("deprecation") // for Resource.setRole()
private static Protos.Resource.Builder addReservation(
        Protos.Resource.Builder builder, String resourceId) {
    Protos.Resource.ReservationInfo.Builder reservationBuilder;
    if (Capabilities.getInstance().supportsPreReservedResources()) {
        reservationBuilder = builder.addReservationsBuilder()
                .setRole(TestConstants.ROLE)
                .setPrincipal(TestConstants.PRINCIPAL);
    } else {
        builder.setRole(TestConstants.ROLE);
        reservationBuilder = builder.getReservationBuilder()
                .setPrincipal(TestConstants.PRINCIPAL);
    }
    AuxLabelAccess.setResourceId(reservationBuilder, resourceId);
    AuxLabelAccess.setResourceNamespace(reservationBuilder, TestConstants.SERVICE_NAME);
    AuxLabelAccess.setFrameworkId(reservationBuilder, TestConstants.FRAMEWORK_ID.getValue());
    return builder;
}
 
Example #9
Source File: BdsMesosScheduler.java    From BigDataScript with Apache License 2.0 6 votes vote down vote up
/**
 * Convert offer to hostResources
 */
HostResources parseOffer(Offer offer) {
	HostResources hr = new HostResources();

	hr.setMem(0);
	hr.setCpus(0);

	for (Resource r : offer.getResourcesList()) {
		String resourceName = r.getName();
		int value = (int) r.getScalar().getValue();

		switch (resourceName) {
		case OFFER_MEM:
			hr.setMem(MB * value);
			break;
		case OFFER_CPUS:
			hr.setCpus(value);
			break;
		}
	}

	return hr;
}
 
Example #10
Source File: MesosResourcePoolTest.java    From dcos-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void testConsumeUnreservedAtomicResource() {
    Resource offerResource = ResourceTestUtils.getUnreservedMountVolume(1000, Optional.empty());
    VolumeSpec spec = DefaultVolumeSpec.createMountVolume(
            ValueUtils.getValue(offerResource).getScalar().getValue(),
            TestConstants.CONTAINER_PATH,
            Collections.emptyList(),
            TestConstants.ROLE,
            Constants.ANY_ROLE,
            TestConstants.PRINCIPAL);
    Offer offer = OfferTestUtils.getOffer(offerResource);
    MesosResourcePool pool = new MesosResourcePool(offer, Optional.of(Constants.ANY_ROLE));

    Assert.assertEquals(1, pool.getUnreservedAtomicPool().size());
    MesosResource resourceToConsume = pool.consumeAtomic(offerResource.getName(), spec).get();
    Assert.assertEquals(offerResource, resourceToConsume.getResource());
    Assert.assertEquals(0, pool.getUnreservedAtomicPool().size());
}
 
Example #11
Source File: MesosResourcePoolTest.java    From dcos-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void testConsumeUnreservedAtomicResourceMatchedProfile() {
    Resource offerResource = ResourceTestUtils.getUnreservedMountVolume(1000, Optional.of("bar"));
    VolumeSpec spec = DefaultVolumeSpec.createMountVolume(
            ValueUtils.getValue(offerResource).getScalar().getValue(),
            TestConstants.CONTAINER_PATH,
            Arrays.asList("foo", "bar"),
            TestConstants.ROLE,
            Constants.ANY_ROLE,
            TestConstants.PRINCIPAL);
    Offer offer = OfferTestUtils.getOffer(offerResource);
    MesosResourcePool pool = new MesosResourcePool(offer, Optional.of(Constants.ANY_ROLE));

    Assert.assertEquals(1, pool.getUnreservedAtomicPool().size());
    MesosResource resourceToConsume = pool.consumeAtomic(offerResource.getName(), spec).get();
    Assert.assertEquals(offerResource, resourceToConsume.getResource());
    Assert.assertEquals(0, pool.getUnreservedAtomicPool().size());
}
 
Example #12
Source File: MesosResourcePoolTest.java    From dcos-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void testConsumeUnreservedAtomicResourceUnmatchedProfile1() {
    Resource offerResource = ResourceTestUtils.getUnreservedMountVolume(1000, Optional.of("bar"));
    VolumeSpec spec = DefaultVolumeSpec.createMountVolume(
            ValueUtils.getValue(offerResource).getScalar().getValue(),
            TestConstants.CONTAINER_PATH,
            Arrays.asList("foo"),
            TestConstants.ROLE,
            Constants.ANY_ROLE,
            TestConstants.PRINCIPAL);
    Offer offer = OfferTestUtils.getOffer(offerResource);
    MesosResourcePool pool = new MesosResourcePool(offer, Optional.of(Constants.ANY_ROLE));

    Assert.assertEquals(1, pool.getUnreservedAtomicPool().size());
    Assert.assertFalse(pool.consumeAtomic(offerResource.getName(), spec).isPresent());
}
 
Example #13
Source File: MesosResourcePoolTest.java    From dcos-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void testConsumeUnreservedAtomicResourceUnmatchedProfile3() {
    Resource offerResource = ResourceTestUtils.getUnreservedMountVolume(1000, Optional.of("bar"));
    VolumeSpec spec = DefaultVolumeSpec.createMountVolume(
            ValueUtils.getValue(offerResource).getScalar().getValue(),
            TestConstants.CONTAINER_PATH,
            Collections.emptyList(),
            TestConstants.ROLE,
            Constants.ANY_ROLE,
            TestConstants.PRINCIPAL);
    Offer offer = OfferTestUtils.getOffer(offerResource);
    MesosResourcePool pool = new MesosResourcePool(offer, Optional.of(Constants.ANY_ROLE));

    Assert.assertEquals(1, pool.getUnreservedAtomicPool().size());
    Assert.assertFalse(pool.consumeAtomic(offerResource.getName(), spec).isPresent());
}
 
Example #14
Source File: MesosNimbus.java    From storm with Apache License 2.0 6 votes vote down vote up
private Resource createMesosRangeResource(ResourceType resourceType, RangeResourceEntry rangeResourceEntry) {
  Ranges.Builder rangesBuilder = Ranges.newBuilder();
  Range rangeBuilder = Range.newBuilder()
                            .setBegin(rangeResourceEntry.getBegin())
                            .setEnd(rangeResourceEntry.getEnd()).build();
  rangesBuilder.addRange(rangeBuilder);

  Resource.Builder resourceBuilder = Resource.newBuilder()
                                             .setName(resourceType.toString())
                                             .setType(Protos.Value.Type.RANGES)
                                             .setRanges(rangesBuilder.build());
  if (rangeResourceEntry.getReservationType() != null && rangeResourceEntry.getReservationType().equals(ReservationType.STATIC)) {
    resourceBuilder.setRole(MesosCommon.getRole(mesosStormConf));
  }
  return resourceBuilder.build();
}
 
Example #15
Source File: MesosResourcePool.java    From dcos-commons with Apache License 2.0 5 votes vote down vote up
private static boolean consumableResource(Optional<String> podRole, Resource resource) {
  if (!podRole.isPresent()
      || !resource.hasAllocationInfo()
      || !resource.getAllocationInfo().hasRole())
  {
    return true;
  }

  String allocationRole = resource.getAllocationInfo().getRole();
  return podRole.get().equals(allocationRole);
}
 
Example #16
Source File: ResourceTestUtils.java    From dcos-commons with Apache License 2.0 5 votes vote down vote up
public static Protos.Resource getReservedMountVolume(
        double diskSize,
        Optional<String> profile,
        String resourceId,
        String persistenceId) {
    Protos.Resource.Builder builder = getUnreservedMountVolume(diskSize, profile).toBuilder();
    builder.getDiskBuilder().getPersistenceBuilder()
            .setId(persistenceId)
            .setPrincipal(TestConstants.PRINCIPAL);
    builder.getDiskBuilder().getVolumeBuilder()
            .setContainerPath(TestConstants.CONTAINER_PATH)
            .setMode(Volume.Mode.RW);
    return addReservation(builder, resourceId).build();
}
 
Example #17
Source File: OfferEvaluatorPortsTest.java    From dcos-commons with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("PMD.AvoidUsingHardCodedIP")
@Test
public void testReserveTaskNamedVIPPort() throws Exception {
    List<OfferRecommendation> recommendations = evaluator.evaluate(
            PodInstanceRequirementTestUtils.getVIPRequirement(80, 10000),
            OfferTestUtils.getCompleteOffers(ResourceTestUtils.getUnreservedPorts(10000, 10000)));

    Assert.assertEquals(6, recommendations.size());

    Operation launchOperation = recommendations.get(4).getOperation().get();
    Assert.assertFalse(recommendations.get(5).getOperation().isPresent());

    TaskInfo taskInfo = launchOperation.getLaunchGroup().getTaskGroup().getTasks(0);
    Resource fulfilledPortResource = taskInfo.getResources(0);
    Assert.assertEquals(10000, fulfilledPortResource.getRanges().getRange(0).getBegin());
    Assert.assertFalse(getResourceId(fulfilledPortResource).isEmpty());

    DiscoveryInfo discoveryInfo = taskInfo.getDiscovery();
    Assert.assertEquals(discoveryInfo.getName(), taskInfo.getName());
    Assert.assertEquals(discoveryInfo.getVisibility(), DiscoveryInfo.Visibility.CLUSTER);

    Port discoveryPort = discoveryInfo.getPorts().getPorts(0);
    Assert.assertEquals(discoveryPort.getProtocol(), "tcp");
    Assert.assertEquals(discoveryPort.getVisibility(), DiscoveryInfo.Visibility.EXTERNAL);
    Assert.assertEquals(discoveryPort.getNumber(), 10000);
    Label vipLabel = discoveryPort.getLabels().getLabels(0);
    Assert.assertTrue(vipLabel.getKey().startsWith("VIP_"));
    Assert.assertEquals(vipLabel.getValue(), TestConstants.VIP_NAME + "-10000:80");

    Map<String, Protos.Environment.Variable> envvars = EnvUtils.toMap(taskInfo.getCommand().getEnvironment());
    Assert.assertEquals(String.valueOf(10000), envvars.get(TestConstants.PORT_ENV_NAME + "_VIP_10000").getValue());
}
 
Example #18
Source File: MesosUtilities.java    From oodt with Apache License 2.0 5 votes vote down vote up
/**
 * Makes a string message from resources list.
 * @param resources - resource list to make into string.
 * @return string representing the resource list.
 */
public static String getResourceMessage(Collection<Resource> resources) {
    String ret = "";
    for (Resource res : resources)
        ret += "\n\t"+getResourceMessage(res);
    return ret;
}
 
Example #19
Source File: ResourceTestUtils.java    From dcos-commons with Apache License 2.0 5 votes vote down vote up
public static Protos.Resource getUnreservedMountVolume(double diskSize, Optional<String> profile) {
    Protos.Resource.Builder builder = getUnreservedDisk(diskSize).toBuilder();
    builder.getDiskBuilder().setSource(TestConstants.MOUNT_DISK_SOURCE);
    if (profile.isPresent()) {
        builder.getDiskBuilder().getSourceBuilder().setProfile(profile.get());
    }
    return builder.build();
}
 
Example #20
Source File: OfferEvaluatorPortsTest.java    From dcos-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void testLaunchExpectedMultiplePorts() throws Exception {
    // Launch for the first time: get ports 10000,10001
    PodInstanceRequirement podInstanceRequirement = PodInstanceRequirementTestUtils.getPortRequirement(10000, 10001);
    List<Resource> reserveResources = recordLaunchWithCompleteOfferedResources(
            podInstanceRequirement, ResourceTestUtils.getUnreservedPorts(10000, 10001));
    Assert.assertEquals(reserveResources.toString(), 5, reserveResources.size());
    String resourceId0 = getResourceId(reserveResources.get(3));
    String resourceId1 = getResourceId(reserveResources.get(4));
    Collection<Resource> expectedResources = getExpectedExecutorResources(
            stateStore.fetchTasks().iterator().next().getExecutor());
    expectedResources.addAll(Arrays.asList(
            ResourceTestUtils.getReservedPorts(10000, 10000, resourceId0),
            ResourceTestUtils.getReservedPorts(10001, 10001, resourceId1)));

    // Now try relaunch:
    List<OfferRecommendation> recommendations = evaluator.evaluate(
            PodInstanceRequirementTestUtils.getPortRequirement(10000, 10001),
            Arrays.asList(OfferTestUtils.getOffer(expectedResources)));
    Assert.assertEquals(2, recommendations.size());

    // Validate LAUNCH Operation
    Operation launchOperation = recommendations.get(0).getOperation().get();
    Assert.assertEquals(Operation.Type.LAUNCH_GROUP, launchOperation.getType());

    List<Resource> launchResources = launchOperation.getLaunchGroup().getTaskGroup().getTasks(0).getResourcesList();
    Assert.assertEquals(launchResources.toString(), 2, launchResources.size());

    Assert.assertEquals(resourceId0, getResourceId(launchResources.get(0)));
    Assert.assertEquals(resourceId1, getResourceId(launchResources.get(1)));

    Assert.assertFalse(recommendations.get(1).getOperation().isPresent());
}
 
Example #21
Source File: OfferEvaluatorPortsTest.java    From dcos-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void testUpdateDynamicToStaticPort() throws Exception {
    // Launch for the first time: get port 555 from dynamic port
    Resource reserveResource = recordLaunchWithCompleteOfferedResources(
            PodInstanceRequirementTestUtils.getPortRequirement(0),
            ResourceTestUtils.getUnreservedPorts(555, 555)).get(3);
    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(Arrays.asList(
            Protos.Offer.Operation.Type.UNRESERVE,
            Protos.Offer.Operation.Type.RESERVE,
            Offer.Operation.Type.LAUNCH_GROUP,
            null),
            recommendations.stream()
                    .map(rec -> rec.getOperation().isPresent() ? rec.getOperation().get().getType() : null)
                    .collect(Collectors.toList()));

    Operation launchOperation = recommendations.get(2).getOperation().get();
    TaskInfo taskInfo = launchOperation.getLaunchGroup().getTaskGroup().getTasks(0);

    Map<String, Protos.Environment.Variable> envvars = EnvUtils.toMap(taskInfo.getCommand().getEnvironment());
    Assert.assertEquals(String.valueOf(666), envvars.get(TestConstants.PORT_ENV_NAME + "_666").getValue());
}
 
Example #22
Source File: OfferEvaluatorPortsTest.java    From dcos-commons with Apache License 2.0 5 votes vote down vote up
@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(3);
    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(Arrays.asList(
            Protos.Offer.Operation.Type.UNRESERVE,
            Protos.Offer.Operation.Type.RESERVE,
            Offer.Operation.Type.LAUNCH_GROUP,
            null),
            recommendations.stream()
                    .map(rec -> rec.getOperation().isPresent() ? rec.getOperation().get().getType() : null)
                    .collect(Collectors.toList()));

    Operation launchOperation = recommendations.get(2).getOperation().get();
    TaskInfo taskInfo = launchOperation.getLaunchGroup().getTaskGroup().getTasks(0);

    Map<String, Protos.Environment.Variable> envvars = EnvUtils.toMap(taskInfo.getCommand().getEnvironment());
    Assert.assertEquals(String.valueOf(666), envvars.get(TestConstants.PORT_ENV_NAME + "_666").getValue());
}
 
Example #23
Source File: OfferTestUtils.java    From dcos-commons with Apache License 2.0 5 votes vote down vote up
public static Protos.Offer getCompleteOffer(Protos.ExecutorID executorId, List<Protos.Resource> resources) {
    Protos.Offer.Builder builder = getEmptyOfferBuilder();
    builder.addAllResources(resources);

    if (executorId != null) {
        builder.addExecutorIds(executorId);
    }

    return builder.build();
}
 
Example #24
Source File: OfferEvaluatorPortsTest.java    From dcos-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void testLaunchExpectedDynamicPort() throws Exception {
    // Launch for the first time: get port 10000
    PodInstanceRequirement podInstanceRequirement = PodInstanceRequirementTestUtils.getPortRequirement(0);
    Resource reserveResource = recordLaunchWithCompleteOfferedResources(
            podInstanceRequirement, ResourceTestUtils.getUnreservedPorts(10000, 10000)).get(3);
    String resourceId = getResourceId(reserveResource);
    Collection<Resource> expectedResources = getExpectedExecutorResources(
            stateStore.fetchTasks().iterator().next().getExecutor());
    expectedResources.add(ResourceTestUtils.getReservedPorts(10000, 10000, resourceId));


    // Relaunch: detect (from envvar) and reuse previously reserved dynamic port 10000
    List<OfferRecommendation> recommendations = evaluator.evaluate(
            podInstanceRequirement,
            Arrays.asList(OfferTestUtils.getOffer(expectedResources)));
    Assert.assertEquals(2, recommendations.size());

    // Validate LAUNCH Operation
    Operation launchOperation = recommendations.get(0).getOperation().get();
    Assert.assertEquals(Operation.Type.LAUNCH_GROUP, launchOperation.getType());

    Resource launchResource = launchOperation.getLaunchGroup().getTaskGroup().getTasks(0).getResources(0);
    Assert.assertEquals(resourceId, getResourceId(launchResource));

    Assert.assertFalse(recommendations.get(1).getOperation().isPresent());
}
 
Example #25
Source File: OfferEvaluatorTestBase.java    From dcos-commons with Apache License 2.0 5 votes vote down vote up
protected List<Resource> recordLaunchWithOfferedResources(
        PodInstanceRequirement podInstanceRequirement, Resource... offeredResources)
        throws InvalidRequirementException, IOException {
    return recordLaunchWithOfferedResources(
            OfferTestUtils.getOffer(Arrays.asList(offeredResources)),
            podInstanceRequirement,
            offeredResources);
}
 
Example #26
Source File: MesosNimbus.java    From storm with Apache License 2.0 5 votes vote down vote up
private Resource createMesosScalarResource(ResourceType resourceType, ScalarResourceEntry scalarResourceEntry) {
  return Resource.newBuilder()
                 .setName(resourceType.toString())
                 .setType(Protos.Value.Type.SCALAR)
                 .setScalar(Scalar.newBuilder().setValue(scalarResourceEntry.getValue()))
                 .build();
}
 
Example #27
Source File: MesosResourcePoolTest.java    From dcos-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void testConsumeUnreservedWhenNoUnreservedResources() {
    Resource reservedCpu = ResourceTestUtils.getReservedCpus(1.0, UUID.randomUUID().toString());
    Offer offer = OfferTestUtils.getOffer(reservedCpu);
    MesosResourcePool pool = new MesosResourcePool(offer, Optional.of(Constants.ANY_ROLE));

    Map<String, Protos.Value> map = pool.getUnreservedMergedPool();
    Assert.assertTrue(map != null);
    Assert.assertTrue(map.isEmpty());
}
 
Example #28
Source File: OfferEvaluatorTestBase.java    From dcos-commons with Apache License 2.0 5 votes vote down vote up
protected List<Resource> recordLaunchWithCompleteOfferedResources(
        PodInstanceRequirement podInstanceRequirement, String preReservedRole, Resource... offeredResources)
        throws InvalidRequirementException, IOException {
    return recordLaunchWithOfferedResources(
            OfferTestUtils.getCompleteOffer(Arrays.asList(offeredResources), preReservedRole),
            podInstanceRequirement,
            offeredResources);
}
 
Example #29
Source File: OfferEvaluatorPortsTest.java    From dcos-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void testReserveStaticPort() throws Exception {
    PodInstanceRequirement podInstanceRequirement = PodInstanceRequirementTestUtils.getPortRequirement(555);
    Protos.Resource offeredPorts = ResourceTestUtils.getUnreservedPorts(555, 555);

    List<OfferRecommendation> recommendations =
            evaluator.evaluate(podInstanceRequirement, OfferTestUtils.getCompleteOffers(offeredPorts));

    Assert.assertEquals(Arrays.asList(
            Protos.Offer.Operation.Type.RESERVE,
            Protos.Offer.Operation.Type.RESERVE,
            Protos.Offer.Operation.Type.RESERVE,
            Protos.Offer.Operation.Type.RESERVE,
            Offer.Operation.Type.LAUNCH_GROUP,
            null),
            recommendations.stream()
                    .map(rec -> rec.getOperation().isPresent() ? rec.getOperation().get().getType() : null)
                    .collect(Collectors.toList()));

    Protos.Offer.Operation launchOperation = recommendations.get(4).getOperation().get();
    Protos.TaskInfo taskInfo = launchOperation.getLaunchGroup().getTaskGroup().getTasks(0);
    Protos.Resource fulfilledPortResource = taskInfo.getResources(0);
    Assert.assertFalse(getResourceId(fulfilledPortResource).isEmpty());

    Map<String, Protos.Environment.Variable> envvars = EnvUtils.toMap(taskInfo.getCommand().getEnvironment());
    Assert.assertEquals(String.valueOf(555), envvars.get(TestConstants.PORT_ENV_NAME + "_555").getValue());
}
 
Example #30
Source File: OfferEvaluatorVolumesTest.java    From dcos-commons with Apache License 2.0 5 votes vote down vote up
private static void validatePrincipal(Resource resource) {
    if (Capabilities.getInstance().supportsPreReservedResources()) {
        Assert.assertEquals(TestConstants.PRINCIPAL, resource.getReservations(0).getPrincipal());
    } else {
        Assert.assertEquals(TestConstants.PRINCIPAL, resource.getReservation().getPrincipal());
    }
}