/** * 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()))); }
.setName("cpus") .setType(Value.Type.SCALAR) .setRole(r.getRole()) .setScalar( Value.Scalar.newBuilder() .setName("mem") .setType(Value.Type.SCALAR) .setRole(r.getRole()) .setScalar( Value.Scalar.newBuilder() .setName("disk") .setType(Value.Type.SCALAR) .setRole(r.getRole()) .setScalar( Value.Scalar.newBuilder()
ReservationType reservationType = (r.getRole().equals("*")) ? ReservationType.UNRESERVED : ReservationType.STATIC;
String value = ""; if (r.hasRole()) { name = String.format("%s(%s)", r.getName(), r.getRole()); } else { name = r.getName();
private void parseOffer(Offer offer) { // Pull out the cpus, memory, disk, and 2 ports from the offer. for (Resource resource : offer.getResourcesList()) { if (resource.getName().equals("cpus") && resource.getType() == Value.Type.SCALAR) { cpus = resource.getScalar().getValue(); cpuRole = resource.getRole(); } else if (resource.getName().equals("mem") && resource.getType() == Value.Type.SCALAR) { mem = resource.getScalar().getValue(); memRole = resource.getRole(); } else if (resource.getName().equals("disk") && resource.getType() == Value.Type.SCALAR) { disk = resource.getScalar().getValue(); diskRole = resource.getRole(); } else if (resource.getName().equals("ports") && resource.getType() == Value.Type.RANGES) { portRole = resource.getRole(); ports = resource.getRanges().getRangeList(); } } }
@Override public OfferEvaluation check(String requirement, String taskId, Protos.Offer offer) { List<Protos.Resource> roleResources = offer.getResourcesList().stream() .filter(Protos.Resource::hasRole) .filter(resource -> resource.getRole().equals(mesosConfig.getRole())) .collect(Collectors.toList()); if (!roleResources.isEmpty()) { return OfferEvaluation.accept( requirement, taskId, offer, Collections.emptyMap(), Collections.emptyList(), Collections.emptyList(), roleResources ); } return OfferEvaluation.decline(requirement, taskId, offer, "No resources for role " + mesosConfig.getRole()); } }
@SuppressWarnings("deprecation") private static VolumeSpec getVolumeSpec(Protos.Resource resource) { return resource.getDisk().hasSource() ? DefaultVolumeSpec.createMountVolume( resource.getScalar().getValue(), resource.getDisk().getVolume().getContainerPath(), resource.getDisk().getSource().hasProfile() ? Collections.singletonList(resource.getDisk().getSource().getProfile()) : Collections.emptyList(), ResourceUtils.getRole(resource), resource.getRole(), resource.getDisk().getPersistence().getPrincipal()) : DefaultVolumeSpec.createRootVolume( resource.getScalar().getValue(), resource.getDisk().getVolume().getContainerPath(), ResourceUtils.getRole(resource), resource.getRole(), resource.getDisk().getPersistence().getPrincipal()); }
/** * Returns the roles used to reserve this resource. */ @SuppressWarnings("deprecation") private static Set<String> getReservationRoles(Protos.Resource resource) { Set<String> roles = getDynamicReservations(resource) .stream() .map(Protos.Resource.ReservationInfo::getRole) .collect(Collectors.toSet()); if (resource.hasRole()) { roles.add(resource.getRole()); } // Omit the "*" role if present roles.remove(Constants.ANY_ROLE); return roles; }
public String getRole(){ return info.getResources(0).getRole(); }
@SuppressWarnings("deprecation") private static ResourceSpec getResourceSpec(Protos.Resource resource) { if (!ResourceUtils.hasResourceId(resource)) { throw new IllegalStateException( "Cannot generate resource spec from resource which has not been reserved by the SDK."); } return DefaultResourceSpec.newBuilder() .name(resource.getName()) .value(ValueUtils.getValue(resource)) .role(ResourceUtils.getRole(resource)) .preReservedRole(resource.getRole()) .principal(ResourceUtils.getPrincipal(resource).get()) .build(); }
@SuppressWarnings("deprecation") private String getRefinedPreviousRole() { if (resource.getReservationsCount() <= 1) { return resource.getRole(); } else { return resource.getReservations(resource.getReservationsCount() - 2).getRole(); } }
@VisibleForTesting String findRoleForPorts(Offer offer) { String role = MESOS_DEFAULT_ROLE; // Locate the port resource in the offer for (Resource resource : offer.getResourcesList()) { if (resource.getName().equals(PORT_RESOURCE_NAME)) { role = resource.getRole(); } } return role; }
@SuppressWarnings("deprecation") @Test public void testUnreservedResource() { ResourceBuilder resourceBuilder = ResourceBuilder.fromUnreservedValue("cpus", VALUE); Protos.Resource resource = resourceBuilder.build(); Assert.assertEquals("cpus", resource.getName()); Assert.assertEquals(Protos.Value.Type.SCALAR, resource.getType()); Assert.assertEquals(1.0, resource.getScalar().getValue(), 0.0); Assert.assertEquals(Constants.ANY_ROLE, resource.getRole()); Assert.assertFalse(resource.hasReservation()); Assert.assertEquals(0, resource.getReservationsCount()); }
@SuppressWarnings("deprecation") private static void validateScalarResourceRefined( Protos.Resource resource, Optional<String> resourceId, Optional<String> namespace) { Assert.assertEquals(Protos.Value.Type.SCALAR, resource.getType()); Assert.assertEquals(Constants.ANY_ROLE, resource.getRole()); Assert.assertFalse(resource.hasReservation()); Protos.Resource.ReservationInfo reservationInfo = resource.getReservations(resource.getReservationsCount() - 1); Assert.assertEquals(TestConstants.PRINCIPAL, reservationInfo.getPrincipal()); Assert.assertEquals(TestConstants.ROLE, reservationInfo.getRole()); validateLabels(reservationInfo, resourceId, namespace); }
@SuppressWarnings("deprecation") private static void validateScalarResourceLegacy(Protos.Resource resource, Optional<String> resourceId, Optional<String> namespace) { Assert.assertEquals(Protos.Value.Type.SCALAR, resource.getType()); Assert.assertEquals(TestConstants.ROLE, resource.getRole()); Assert.assertTrue(resource.hasReservation()); Assert.assertEquals(0, resource.getReservationsCount()); Protos.Resource.ReservationInfo reservationInfo = resource.getReservation(); Assert.assertEquals(TestConstants.PRINCIPAL, reservationInfo.getPrincipal()); Assert.assertFalse(reservationInfo.hasRole()); validateLabels(reservationInfo, resourceId, namespace); }
/** * Gets a string representation of a resource. */ public static String toString(Protos.Resource resource) { checkNotNull(resource); if (resource.hasScalar()) { return String.format("%s(%s):%.1f", resource.getName(), resource.getRole(), resource.getScalar().getValue()); } if (resource.hasRanges()) { return String.format("%s(%s):%s", resource.getName(), resource.getRole(), toString(resource.getRanges())); } return resource.toString(); }