case SCALAR: resources.add(resource); aggregatedScalarResourceMap.merge(resource.getName(), resource.getScalar().getValue(), Double::sum); break; case RANGES: resources.add(resource); rangesResourceMap.computeIfAbsent(resource.getName(), k -> new ArrayList<>(2)).add(resource); break; default: logger.debug("Unknown resource type " + resource.getType() + " for resource " + resource.getName() + " in offer, hostname=" + hostname + ", offerId=" + offer.getId());
if (!resourceName.equals(available.getName()) || !available.hasScalar()) { continue;
if (!resourceName.equals(available.getName()) || !available.hasRanges()) { continue;
private Protos.Resource getResourceByName(List<Protos.Resource> resourceList, String name) { return resourceList.stream().filter(resource -> resource.getName().equals(name)).findFirst().get(); }
public static boolean isDisk(final Resource resource) { return resource.hasName() && resource.getName().equalsIgnoreCase(DISK); }
public static boolean isCpus(final Resource resource) { return resource.hasName() && resource.getName().equalsIgnoreCase(CPUS); }
@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(); }
public static boolean isPorts(final Resource resource) { return resource.hasName() && resource.getName().equalsIgnoreCase(PORTS); }
static double scalarSum(Protos.Offer offer, String name) { return offer.getResourcesList().stream().filter(resource -> resource.getName().equals(name)).mapToDouble(resource -> resource.getScalar().getValue()).sum(); } }
@Override public boolean apply(final Protos.Resource input) { return input.getName().equals(type); } })).setScalar(Protos.Value.Scalar.newBuilder().setValue(resourceValue)).build();
private Double getScalar(List<Protos.Resource> resources, String name) { for (Protos.Resource resource : resources) { if (resource.getName().equals(name)) { return resource.getScalar().getValue(); } } return null; }
@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()); }
private static Set<Integer> getPortsInResource(Protos.Resource resource) { if (!resource.getName().equals(Constants.PORTS_RESOURCE_TYPE)) { return Collections.emptySet(); } return resource.getRanges().getRangeList().stream() .flatMap(r -> IntStream.rangeClosed((int) r.getBegin(), (int) r.getEnd()).boxed()) .filter(p -> p != 0) .collect(Collectors.toSet()); }
@Test public void testConsumeInsufficientUnreservedMergedResource() { Resource desiredUnreservedResource = ResourceTestUtils.getUnreservedCpus(2.0); Protos.Value resourceValue = ValueUtils.getValue(desiredUnreservedResource); Resource offeredUnreservedResource = ResourceTestUtils.getUnreservedCpus(1.0); Offer offer = OfferTestUtils.getOffer(offeredUnreservedResource); MesosResourcePool pool = new MesosResourcePool(offer, Optional.of(Constants.ANY_ROLE)); Assert.assertFalse( pool.consumeReservableMerged(desiredUnreservedResource.getName(), resourceValue, Constants.ANY_ROLE) .isPresent()); }
@Test public void testConsumeReservedMergedResource() { Resource resource = ResourceTestUtils.getReservedCpus(1.0, TestConstants.RESOURCE_ID); Protos.Value resourceValue = ValueUtils.getValue(resource); String resourceId = ResourceTestUtils.getResourceId(resource); Offer offer = OfferTestUtils.getOffer(resource); MesosResourcePool pool = new MesosResourcePool(offer, Optional.of(Constants.ANY_ROLE)); Assert.assertEquals(1, pool.getDynamicallyReservedPoolByResourceId().size()); MesosResource resourceToConsume = pool.consumeReserved(resource.getName(), resourceValue, resourceId).get(); Assert.assertEquals(resource, resourceToConsume.getResource()); Assert.assertEquals(0, pool.getDynamicallyReservedPoolByResourceId().size()); }
private int getCpu(final Offer offer) { for (final Resource resource : offer.getResourcesList()) { if (resource.getName().equals("cpus")) { return (int)resource.getScalar().getValue(); } } return 0; }
private int getMemory(final Offer offer) { for (final Resource resource : offer.getResourcesList()) { if (resource.getName().equals("mem")) { return (int)resource.getScalar().getValue(); } } return 0; }
@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; }
if (resource.getName().equals(CPU)) { if (resource.getType().equals(Protos.Value.Type.SCALAR)) cpus = resource.getScalar().getValue(); log.log(Level.FINE, "Cpus resource was not a scalar: {0}" + resource.getType()); else if (resource.getName().equals(MEM)) { if (resource.getType().equals(Protos.Value.Type.SCALAR)) mem = resource.getScalar().getValue(); log.log(Level.FINE, "Mem resource was not a scalar: {0}", resource.getType()); else if (resource.getName().equals(DISK)) if (resource.getType().equals(Protos.Value.Type.SCALAR)) disk = resource.getScalar().getValue();
/** * 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(); }