@Override public ResourceInformation getResourceInformation(String resource) throws ResourceNotFoundException { ResourceInformation ri; try { ri = super.getResourceInformation(resource); } catch (ResourceNotFoundException e) { // Retry once to reinitialize resource information. initResourceMap(); try { return super.getResourceInformation(resource); } catch (ResourceNotFoundException ee) { throw ee; } } return ri; }
@Override public ResourceInformation getResourceInformation(String resource) { initResources(); return super.getResourceInformation(resource); }
@Override public float ratio(Resource a, Resource b) { float ratio = 0.0f; int maxLength = ResourceUtils.getNumberOfKnownResourceTypes(); for (int i = 0; i < maxLength; i++) { ResourceInformation aResourceInformation = a.getResourceInformation(i); ResourceInformation bResourceInformation = b.getResourceInformation(i); float tmp = (float) aResourceInformation.getValue() / (float) bResourceInformation.getValue(); ratio = ratio > tmp ? ratio : tmp; } return ratio; }
@Override public boolean fitsIn(Resource smaller, Resource bigger) { int maxLength = ResourceUtils.getNumberOfKnownResourceTypes(); for (int i = 0; i < maxLength; i++) { ResourceInformation sResourceInformation = smaller .getResourceInformation(i); ResourceInformation bResourceInformation = bigger .getResourceInformation(i); if (sResourceInformation.getValue() > bResourceInformation.getValue()) { return false; } } return true; }
@Override public void setResourceInformation(String resource, ResourceInformation resourceInformation) { maybeInitBuilder(); if (resource == null || resourceInformation == null) { throw new IllegalArgumentException( "resource and/or resourceInformation cannot be null"); } ResourceInformation storedResourceInfo = super.getResourceInformation( resource); ResourceInformation.copy(resourceInformation, storedResourceInfo); }
@Override public boolean isAnyMajorResourceZeroOrNegative(Resource resource) { int maxLength = ResourceUtils.getNumberOfKnownResourceTypes(); for (int i = 0; i < maxLength; i++) { ResourceInformation resourceInformation = resource.getResourceInformation( i); if (resourceInformation.getValue() <= 0L) { return true; } } return false; }
@Override public boolean isAnyMajorResourceAboveZero(Resource resource) { int maxLength = ResourceUtils.getNumberOfKnownResourceTypes(); for (int i = 0; i < maxLength; i++) { ResourceInformation resourceInformation = resource.getResourceInformation( i); if (resourceInformation.getValue() > 0) { return true; } } return false; } }
@Override public long computeAvailableContainers(Resource available, Resource required) { long min = Long.MAX_VALUE; int maxLength = ResourceUtils.getNumberOfKnownResourceTypes(); for (int i = 0; i < maxLength; i++) { ResourceInformation availableResource = available .getResourceInformation(i); ResourceInformation requiredResource = required.getResourceInformation(i); if (requiredResource.getValue() != 0) { long tmp = availableResource.getValue() / requiredResource.getValue(); min = min < tmp ? min : tmp; } } return min > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) min; }
private static Map<String, ResourceInformation> getZeroResources( Resource resource) { Map<String, ResourceInformation> resourceInformations = Maps.newHashMap(); int maxLength = ResourceUtils.getNumberOfKnownResourceTypes(); for (int i = 0; i < maxLength; i++) { ResourceInformation resourceInformation = resource.getResourceInformation(i); if (resourceInformation.getValue() == 0L) { resourceInformations.put(resourceInformation.getName(), resourceInformation); } } return resourceInformations; }
public static Resource addTo(Resource lhs, Resource rhs) { int maxLength = ResourceUtils.getNumberOfKnownResourceTypes(); for (int i = 0; i < maxLength; i++) { try { ResourceInformation rhsValue = rhs.getResourceInformation(i); ResourceInformation lhsValue = lhs.getResourceInformation(i); lhs.setResourceValue(i, lhsValue.getValue() + rhsValue.getValue()); } catch (ResourceNotFoundException ye) { LOG.warn("Resource is missing:" + ye.getMessage()); continue; } } return lhs; }
public static Resource subtractFrom(Resource lhs, Resource rhs) { int maxLength = ResourceUtils.getNumberOfKnownResourceTypes(); for (int i = 0; i < maxLength; i++) { try { ResourceInformation rhsValue = rhs.getResourceInformation(i); ResourceInformation lhsValue = lhs.getResourceInformation(i); lhs.setResourceValue(i, lhsValue.getValue() - rhsValue.getValue()); } catch (ResourceNotFoundException ye) { LOG.warn("Resource is missing:" + ye.getMessage()); continue; } } return lhs; }
public static Resource multiplyTo(Resource lhs, double by) { int maxLength = ResourceUtils.getNumberOfKnownResourceTypes(); for (int i = 0; i < maxLength; i++) { try { ResourceInformation lhsValue = lhs.getResourceInformation(i); lhs.setResourceValue(i, (long) (lhsValue.getValue() * by)); } catch (ResourceNotFoundException ye) { LOG.warn("Resource is missing:" + ye.getMessage()); continue; } } return lhs; }
/** * Get the value for a specified resource. No information about the units is * returned. * * @param resource name of the resource * @return the value for the resource */ @Public @InterfaceStability.Unstable public long getResourceValue(String resource) { return getResourceInformation(resource).getValue(); }
@Override public Resource multiplyAndNormalizeUp(Resource r, double[] by, Resource stepFactor) { Resource ret = Resource.newInstance(r); int maxLength = ResourceUtils.getNumberOfKnownResourceTypes(); for (int i = 0; i < maxLength; i++) { ResourceInformation rResourceInformation = r.getResourceInformation(i); ResourceInformation stepFactorResourceInformation = stepFactor .getResourceInformation(i); long rValue = rResourceInformation.getValue(); long stepFactorValue = stepFactorResourceInformation.getValue(); ret.setResourceValue(i, ResourceCalculator .roundUp((long) Math.ceil(rValue * by[i]), stepFactorValue)); } return ret; }
public static Resource multiplyAndRoundDown(Resource lhs, double by) { Resource out = clone(lhs); int maxLength = ResourceUtils.getNumberOfKnownResourceTypes(); for (int i = 0; i < maxLength; i++) { try { ResourceInformation lhsValue = lhs.getResourceInformation(i); out.setResourceValue(i, (long) (lhsValue.getValue() * by)); } catch (ResourceNotFoundException ye) { LOG.warn("Resource is missing:" + ye.getMessage()); continue; } } return out; }
public Resource divideAndCeil(Resource numerator, long denominator) { Resource ret = Resource.newInstance(numerator); int maxLength = ResourceUtils.getNumberOfKnownResourceTypes(); for (int i = 0; i < maxLength; i++) { ResourceInformation resourceInformation = ret.getResourceInformation(i); resourceInformation .setValue(divideAndCeil(resourceInformation.getValue(), denominator)); } return ret; }
@Test public void testGetVirtualCores() { Resource res = new ResourcePBImpl(); long vcores = Integer.MAX_VALUE + 1L; res.getResourceInformation("vcores").setValue(vcores); assertEquals("No need to cast if both are long", vcores, res.getResourceInformation("vcores").getValue()); assertEquals("Cast to Integer.MAX_VALUE if the long is greater than " + "Integer.MAX_VALUE", Integer.MAX_VALUE, res.getVirtualCores()); }
@Test public void testEmptyResourcePBInit() throws Exception { Resource res = new ResourcePBImpl(); // Assert to check it sets resource value and unit to default. Assert.assertEquals(0, res.getMemorySize()); Assert.assertEquals(ResourceInformation.MEMORY_MB.getUnits(), res.getResourceInformation(ResourceInformation.MEMORY_MB.getName()) .getUnits()); Assert.assertEquals(ResourceInformation.VCORES.getUnits(), res.getResourceInformation(ResourceInformation.VCORES.getName()) .getUnits()); }
@Test public void testCreateResourceWithSameLongValue() throws Exception { unsetExtraResourceType(); setupExtraResourceType(); Resource res = Resources.createResourceWithSameValue(11L); assertEquals(11L, res.getMemorySize()); assertEquals(11, res.getVirtualCores()); assertEquals(11L, res.getResourceInformation(EXTRA_RESOURCE_TYPE).getValue()); }
@Test public void testCreateResourceWithSameIntValue() throws Exception { unsetExtraResourceType(); setupExtraResourceType(); Resource res = Resources.createResourceWithSameValue(11); assertEquals(11, res.getMemorySize()); assertEquals(11, res.getVirtualCores()); assertEquals(11, res.getResourceInformation(EXTRA_RESOURCE_TYPE).getValue()); }