Java Code Examples for org.apache.mesos.Protos#Resource

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: MesosNimbusTest.java    From storm with Apache License 2.0 6 votes vote down vote up
private boolean hasResources(String role, Protos.TaskInfo taskInfo, Double cpus, Double mem) {
  Double actualCpu = 0.0d, actualMem = 0.0d;

  for (Protos.Resource resource : taskInfo.getResourcesList()) {
    ResourceType r = ResourceType.of(resource.getName());
    if (!resource.getRole().equals(role)) {
      continue;
    }
    switch (r) {
      case CPU:
        actualCpu += resource.getScalar().getValue();
        break;
      case MEM:
        actualMem += resource.getScalar().getValue();
        break;
    }
  }

  return actualCpu.equals(cpus) && actualMem.equals(mem);
}
 
Example 2
Source File: ResourceUtils.java    From dcos-commons with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a list of all the resources associated with a task, including {@link Executor} resources.
 *
 * @param taskInfo The {@link Protos.TaskInfo} containing the {@link Protos.Resource}.
 * @return a list of {@link Protos.Resource}s.
 */
public static List<Protos.Resource> getAllResources(Protos.TaskInfo taskInfo) {
  // Get all resources from both the task level and the executor level
  List<Protos.Resource> resources = new ArrayList<>(taskInfo.getResourcesList());
  if (taskInfo.hasExecutor()) {
    resources.addAll(taskInfo.getExecutor().getResourcesList());
  }
  return resources;
}
 
Example 3
Source File: ResourceUtils.java    From dcos-commons with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a list of unique framework IDs associated with {@link Resource}s.
 *
 * @param resources Collection of resources from which to extract the unique resource IDs
 * @return List of unique framework IDs
 */
public static List<String> getFrameworkIds(Collection<Protos.Resource> resources) {
  return resources.stream()
      .map(ResourceUtils::getFrameworkId)
      .filter(Optional::isPresent)
      .map(Optional::get)
      .distinct()
      .collect(Collectors.toList());
}
 
Example 4
Source File: TestUtils.java    From storm with Apache License 2.0 5 votes vote down vote up
public static List<Protos.Resource> buildRangeResourceList(int begin, int end) {
  List<Protos.Resource> resourceList = new ArrayList<>();
  resourceList.addAll(
    Arrays.asList(
      buildRangeResource("ports", begin, end),
      buildRangeResourceWithRole("ports", begin, end, "reserved"),
      buildScalarResource("cpus", 1),
      buildScalarResource("mem", 2),
      buildScalarResourceWithRole("cpus", 3, "reserved"),
      buildScalarResourceWithRole("mem", 4, "reserved")
    )
  );
  return resourceList;
}
 
Example 5
Source File: MesosScheduler.java    From twister2 with Apache License 2.0 5 votes vote down vote up
private Protos.Resource buildRangeResource(String name, int begin, int end) {
  Protos.Value.Range range = Protos.Value.Range.newBuilder().setBegin(begin).setEnd(end).build();
  Protos.Value.Ranges ranges = Protos.Value.Ranges.newBuilder().addRange(range).build();
  return Protos.Resource.newBuilder().setName(name)
      .setType(Protos.Value.Type.RANGES)
      .setRanges(ranges).build();
}
 
Example 6
Source File: OfferTest.java    From flink with Apache License 2.0 5 votes vote down vote up
private static Protos.Offer offer(List<Protos.Resource> resources, List<Protos.Attribute> attributes) {
	return Protos.Offer.newBuilder()
		.setId(OFFER_ID)
		.setFrameworkId(FRAMEWORK_ID)
		.setHostname(HOSTNAME)
		.setSlaveId(AGENT_ID)
		.addAllAttributes(attributes)
		.addAllResources(resources)
		.build();
}
 
Example 7
Source File: OfferEvaluatorTest.java    From dcos-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void testReserveLaunchScalar() throws Exception {
    PodInstanceRequirement podInstanceRequirement = PodInstanceRequirementTestUtils.getCpuRequirement(1.0);
    Protos.Resource offeredResource = ResourceTestUtils.getUnreservedCpus(2.0);

    List<OfferRecommendation> recommendations = evaluator.evaluate(
            podInstanceRequirement,
            Arrays.asList(OfferTestUtils.getCompleteOffer(offeredResource)));
    Assert.assertEquals(6, recommendations.size());

    // Validate RESERVE Operation
    Protos.Offer.Operation reserveOperation = recommendations.get(3).getOperation().get();
    Protos.Resource reserveResource = reserveOperation.getReserve().getResources(0);

    Protos.Resource.ReservationInfo reservation = ResourceUtils.getReservation(reserveResource).get();
    Assert.assertEquals(Protos.Offer.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());
    Assert.assertEquals(TestConstants.FRAMEWORK_ID.getValue(), getFrameworkId(reserveResource));

    // Validate LAUNCH Operation
    Protos.Offer.Operation launchOperation = recommendations.get(4).getOperation().get();
    Protos.Resource launchResource = launchOperation.getLaunchGroup().getTaskGroup().getTasks(0).getResources(0);

    Assert.assertEquals(Protos.Offer.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));
}
 
Example 8
Source File: MesosResourceAllocation.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
/**
 * Creates an allocation of resources for tasks to take.
 *
 * @param resources the resources to add to the allocation.
 */
public MesosResourceAllocation(Collection<Protos.Resource> resources) {
	this.resources = new ArrayList<>(checkNotNull(resources));

	// sort the resources to prefer reserved resources
	this.resources.sort(Comparator.comparing(r -> UNRESERVED_ROLE.equals(r.getRole())));
}
 
Example 9
Source File: PortEvaluationStageTest.java    From dcos-commons with Apache License 2.0 4 votes vote down vote up
@Test
public void testDynamicPortWithRangesFails() throws Exception {
    Protos.Resource offeredPorts = ResourceTestUtils.getPrereservedPort(23, 5050, "slave_public");
    Protos.Offer offer = OfferTestUtils.getCompleteOffer(Arrays.asList(offeredPorts), "slave_public");

    //no port in offer in range
    RangeSpec spec = new RangeSpec(6000, 8000);

    PortSpec.Builder builder = PortSpec.newBuilder()
        .envKey("PORT_TEST")
        .portName("TEST")
        .ranges(Arrays.asList(spec))
        .visibility(TestConstants.PORT_VISIBILITY)
        .networkNames(Collections.emptyList());
    builder
        .value(getPort(0))
        .role(TestConstants.ROLE)
        .preReservedRole("slave_public")
        .principal(TestConstants.PRINCIPAL);

    PortSpec portSpec = builder.build();

    PodInstanceRequirement podInstanceRequirement = getPodInstanceWithPrereservedRole(portSpec);

    PodInfoBuilder podInfoBuilder = new PodInfoBuilder(
        podInstanceRequirement,
        TestConstants.SERVICE_NAME,
        UUID.randomUUID(),
        PodTestUtils.getTemplateUrlFactory(),
        SchedulerConfigTestUtils.getTestSchedulerConfig(),
        Collections.emptyList(),
        TestConstants.FRAMEWORK_ID,
        Collections.emptyMap());

    PortEvaluationStage portEvaluationStage = new PortEvaluationStage(
        portSpec,
        Collections.singleton(TestConstants.TASK_NAME),
        Optional.empty(),
        Optional.empty(),
        Optional.of(TestConstants.FRAMEWORK_ID.getValue()));

    MesosResourcePool mesosResourcePool = new MesosResourcePool(offer, Optional.of("slave_public"));
    EvaluationOutcome outcome = portEvaluationStage.evaluate(mesosResourcePool, podInfoBuilder);
    Assert.assertEquals(false, outcome.isPassing());
}
 
Example 10
Source File: CassandraTemplateTask.java    From dcos-cassandra-service with Apache License 2.0 4 votes vote down vote up
private static Protos.Resource getMemResource(
    String role,
    String principal,
    ClusterTaskConfig clusterTaskConfig) {
    return getScalar(role, principal, "mem", (double) clusterTaskConfig.getMemoryMb());
}
 
Example 11
Source File: CassandraTemplateTask.java    From dcos-cassandra-service with Apache License 2.0 4 votes vote down vote up
private static Protos.Resource getScalar(String role, String principal, String name, Double value) {
    return ResourceUtils.getDesiredScalar(role, principal, name, value);
}
 
Example 12
Source File: SendOffer.java    From dcos-commons with Apache License 2.0 4 votes vote down vote up
private static Protos.Resource toUnreservedResource(ResourceSpec resourceSpec) {
    boolean isMountDisk = resourceSpec instanceof VolumeSpec &&
            ((VolumeSpec) resourceSpec).getType() == VolumeSpec.Type.MOUNT;
    return toUnreservedResource(resourceSpec.getName(), resourceSpec.getValue(), isMountDisk);
}
 
Example 13
Source File: PortEvaluationStageTest.java    From dcos-commons with Apache License 2.0 4 votes vote down vote up
@Test
public void testDynamicPortWithRangeInclusiveUpperbound() throws Exception {
    Protos.Resource offeredPorts = ResourceTestUtils.getPrereservedPort(3000, 5050, "slave_public");
    Protos.Offer offer = OfferTestUtils.getCompleteOffer(Arrays.asList(offeredPorts), "slave_public");

    //only the upperbound port of 3000 should match
    RangeSpec spec1 = new RangeSpec(2000, 3000);

    PortSpec.Builder builder = PortSpec.newBuilder()
        .envKey("PORT_TEST")
        .portName("TEST")
        .ranges(Arrays.asList(spec1))
        .visibility(TestConstants.PORT_VISIBILITY)
        .networkNames(Collections.emptyList());
    builder
        .value(getPort(0))
        .role(TestConstants.ROLE)
        .preReservedRole("slave_public")
        .principal(TestConstants.PRINCIPAL);
    PortSpec portSpec = builder.build();

    PodInstanceRequirement podInstanceRequirement = getPodInstanceWithPrereservedRole(portSpec);

    PodInfoBuilder podInfoBuilder = new PodInfoBuilder(
        podInstanceRequirement,
        TestConstants.SERVICE_NAME,
        UUID.randomUUID(),
        PodTestUtils.getTemplateUrlFactory(),
        SchedulerConfigTestUtils.getTestSchedulerConfig(),
        Collections.emptyList(),
        TestConstants.FRAMEWORK_ID,
        Collections.emptyMap());

    PortEvaluationStage portEvaluationStage = new PortEvaluationStage(
        portSpec,
        Collections.singleton(TestConstants.TASK_NAME),
        Optional.empty(),
        Optional.empty(),
        Optional.of(TestConstants.FRAMEWORK_ID.getValue()));

    MesosResourcePool mesosResourcePool = new MesosResourcePool(offer, Optional.of("slave_public"));
    EvaluationOutcome outcome = portEvaluationStage.evaluate(mesosResourcePool, podInfoBuilder);
    Assert.assertEquals(true, outcome.isPassing());
}
 
Example 14
Source File: Utils.java    From Flink-CEPplus with Apache License 2.0 4 votes vote down vote up
/**
 * Construct a mem resource.
 */
public static Protos.Resource mem(String role, double amount) {
	return scalar("mem", role, amount);
}
 
Example 15
Source File: CassandraSchedulerTest.java    From cassandra-mesos-deprecated with Apache License 2.0 4 votes vote down vote up
@Test
public void testResourceList_doesReturnResourcesWithTheRoleFromResourceOfferThatMatchesRequriements() {
    Protos.Offer offer = Protos.Offer.newBuilder()
            .setFrameworkId(frameworkId)
            .setHostname("somehost.name")
            .setId(Protos.OfferID.newBuilder().setValue(randomID()))
            .setSlaveId(Protos.SlaveID.newBuilder().setValue("someslave").build())
            .addResources(Protos.Resource.newBuilder()
                    .setName("cpus")
                    .setRole("*")
                    .setType(Protos.Value.Type.SCALAR)
                    .setScalar(Protos.Value.Scalar.newBuilder().setValue(8d)))
            .addResources(Protos.Resource.newBuilder()
                    .setName("mem")
                    .setRole("someRole")
                    .setType(Protos.Value.Type.SCALAR)
                    .setScalar(Protos.Value.Scalar.newBuilder().setValue(100)))
            .addResources(Protos.Resource.newBuilder()
                    .setName("mem")
                    .setRole("*")
                    .setType(Protos.Value.Type.SCALAR)
                    .setScalar(Protos.Value.Scalar.newBuilder().setValue(8192)))
            .addResources(Protos.Resource.newBuilder()
                    .setName("disk")
                    .setRole("*")
                    .setType(Protos.Value.Type.SCALAR)
                    .setScalar(Protos.Value.Scalar.newBuilder().setValue(8192)))
            .build();

    final List<Protos.Resource> resources = CassandraScheduler.resourceList(
            TaskResources.newBuilder()
                    .setCpuCores(0.5)
                    .setMemMb(512)
                    .setDiskMb(1000)
                    .build(),
            "someRole",
            offer);

    assertThat(resources).hasSize(3);
    assertThat(resources).contains(ProtoUtils.cpu(0.5, "*"));
    assertThat(resources).contains(ProtoUtils.mem(512, "*"));
    assertThat(resources).contains(ProtoUtils.disk(1000, "*"));
}
 
Example 16
Source File: TaskTestUtils.java    From dcos-commons with Apache License 2.0 4 votes vote down vote up
public static Protos.ExecutorInfo getExistingExecutorInfo(Protos.Resource resource) {
    return getExecutorInfoBuilder()
            .addResources(resource)
            .setExecutorId(TestConstants.EXECUTOR_ID)
            .build();
}
 
Example 17
Source File: OfferStrategyNormal.java    From elasticsearch with Apache License 2.0 4 votes vote down vote up
private boolean isEnoughDisk(Configuration configuration, List<Protos.Resource> resourcesList) {
    return new ResourceCheck(Resources.RESOURCE_DISK).isEnough(resourcesList, configuration.getDisk());
}
 
Example 18
Source File: MesosResourceAllocationTest.java    From flink with Apache License 2.0 4 votes vote down vote up
/**
 * Tests range resource accounting.
 */
@Test
public void testRangeResourceAccounting() {
	MesosResourceAllocation allocation;
	List<Protos.Resource> ports = resources(
		ports(ROLE_A, range(80, 81), range(443, 444)),
		ports(UNRESERVED_ROLE, range(1024, 1025), range(8080, 8081)));

	// take a partial range of one resource
	allocation = new MesosResourceAllocation(ports);
	Assert.assertEquals(
		resources(ports(ROLE_A, range(80, 80))),
		allocation.takeRanges("ports", 1, AS_ROLE_A));
	Assert.assertEquals(
		resources(
			ports(ROLE_A, range(81, 81), range(443, 444)),
			ports(UNRESERVED_ROLE, range(1024, 1025), range(8080, 8081))),
		allocation.getRemaining());

	// take a whole range of one resource
	allocation = new MesosResourceAllocation(ports);
	Assert.assertEquals(
		resources(ports(ROLE_A, range(80, 81))),
		allocation.takeRanges("ports", 2, AS_ROLE_A));
	Assert.assertEquals(
		resources(
			ports(ROLE_A, range(443, 444)),
			ports(UNRESERVED_ROLE, range(1024, 1025), range(8080, 8081))),
		allocation.getRemaining());

	// take numerous ranges of one resource
	allocation = new MesosResourceAllocation(ports);
	Assert.assertEquals(
		resources(ports(ROLE_A, range(80, 81), range(443, 443))),
		allocation.takeRanges("ports", 3, AS_ROLE_A));
	Assert.assertEquals(
		resources(
			ports(ROLE_A, range(444, 444)),
			ports(UNRESERVED_ROLE, range(1024, 1025), range(8080, 8081))),
		allocation.getRemaining());

	// take a whole resource
	allocation = new MesosResourceAllocation(ports);
	Assert.assertEquals(
		resources(ports(ROLE_A, range(80, 81), range(443, 444))),
		allocation.takeRanges("ports", 4, AS_ROLE_A));
	Assert.assertEquals(
		resources(ports(UNRESERVED_ROLE, range(1024, 1025), range(8080, 8081))),
		allocation.getRemaining());

	// take numerous resources
	allocation = new MesosResourceAllocation(ports);
	Assert.assertEquals(
		resources(
			ports(ROLE_A, range(80, 81), range(443, 444)),
			ports(UNRESERVED_ROLE, range(1024, 1024))),
		allocation.takeRanges("ports", 5, AS_ROLE_A));
	Assert.assertEquals(
		resources(ports(UNRESERVED_ROLE, range(1025, 1025), range(8080, 8081))),
		allocation.getRemaining());
}
 
Example 19
Source File: TaskTestUtils.java    From dcos-commons with Apache License 2.0 4 votes vote down vote up
public static Protos.TaskInfo getTaskInfo(List<Protos.Resource> resources, Integer index) {
    Protos.TaskInfo.Builder builder = Protos.TaskInfo.newBuilder()
            .setTaskId(TestConstants.TASK_ID)
            .setName(TestConstants.TASK_NAME)
            .setSlaveId(TestConstants.AGENT_ID)
            .setCommand(TestConstants.COMMAND_INFO)
            .setContainer(TestConstants.CONTAINER_INFO);
    builder.setLabels(new TaskLabelWriter(builder)
            .setType(TestConstants.TASK_TYPE)
            .setIndex(index)
            .toProto());
    for (Protos.Resource r : resources) {
        String resourceId = "";
        String dynamicPortAssignment = null;
        String vipAssignment = null;
        for (Protos.Label l : r.getReservation().getLabels().getLabelsList()) {
            if (Objects.equals(l.getKey(), "resource_id")) {
               resourceId = l.getValue();
            } else if (Objects.equals(l.getKey(), TestConstants.HAS_DYNAMIC_PORT_ASSIGNMENT_LABEL)) {
                dynamicPortAssignment = l.getValue();
            } else if (Objects.equals(l.getKey(), TestConstants.HAS_VIP_LABEL)) {
                vipAssignment = l.getValue();
            }
        }

        if (Objects.equals(r.getName(), "ports")) {
            String portValue = dynamicPortAssignment == null ?
                    Long.toString(r.getRanges().getRange(0).getBegin()) : dynamicPortAssignment;

            if (!resourceId.isEmpty()) {
                builder.getCommandBuilder()
                        .getEnvironmentBuilder()
                        .addVariablesBuilder()
                        .setName(TestConstants.PORT_ENV_NAME)
                        .setValue(portValue);
            }
            if (!resourceId.isEmpty() && vipAssignment != null) {
                Protos.DiscoveryInfo.Builder discoveryBuilder = builder.getDiscoveryBuilder();
                discoveryBuilder.setVisibility(Protos.DiscoveryInfo.Visibility.CLUSTER);
                discoveryBuilder.setName(builder.getName());
                discoveryBuilder.getPortsBuilder()
                        .addPortsBuilder()
                        .setNumber(Integer.parseInt(portValue))
                        .getLabelsBuilder()
                        .addLabelsBuilder()
                        .setKey("VIP_" + UUID.randomUUID().toString())
                        .setValue(vipAssignment);
            }
        }
    }
    return builder.addAllResources(resources).build();
}
 
Example 20
Source File: AcceptEntry.java    From dcos-commons with Apache License 2.0 4 votes vote down vote up
public Collection<Protos.Resource> getReservations() {
  return reservations;
}