private String getResourceNameForResourceIndex(int resourceIndex) { for (Map.Entry<String, Integer> entry : RESOURCE_MAP_ARRAY_BRIDGE.getResourceNamesToArrayIndex().entrySet()) { int index = entry.getValue(); if (index == resourceIndex) { return entry.getKey(); } } return null; }
/** * Return a Map of the normalized resource name to a double. This should only be used when returning thrift resource requests to the end * user. */ public Map<String, Double> toNormalizedMap() { Map<String, Double> ret = RESOURCE_MAP_ARRAY_BRIDGE.translateFromResourceArray(otherResources); ret.put(Constants.COMMON_CPU_RESOURCE_NAME, cpu); return ret; }
/** * Create a new normalized set of resources. Note that memory is not managed by this class, as it is not consistent in requests vs * offers because of how on heap vs off heap is used. * * @param normalizedResources the normalized resource map */ public NormalizedResources(Map<String, Double> normalizedResources) { cpu = normalizedResources.getOrDefault(Constants.COMMON_CPU_RESOURCE_NAME, 0.0); otherResources = RESOURCE_MAP_ARRAY_BRIDGE.translateToResourceArray(normalizedResources); }
@Test public void testCanTranslateBackAndForthBetweenMapAndArrayConsistently() { ResourceMapArrayBridge bridge = new ResourceMapArrayBridge(); Map<String, Double> allResources = new HashMap<>(); allResources.put(gpuResourceName, 2.0); allResources.put(disksResourceName, 64.0); Map<String, Double> normalizedResources = normalize(allResources); double[] resources = bridge.translateToResourceArray(normalizedResources); Map<String, Integer> resourceNamesToArrayIndex = bridge.getResourceNamesToArrayIndex(); assertThat(resourceNamesToArrayIndex.size(), is(2)); int gpuIndex = resourceNamesToArrayIndex.get(gpuResourceName); int disksIndex = resourceNamesToArrayIndex.get(disksResourceName); assertThat(resources.length, is(2)); assertThat(resources[gpuIndex], is(2.0)); assertThat(resources[disksIndex], is(64.0)); Map<String, Double> roundTrippedResources = bridge.translateFromResourceArray(resources); assertThat(roundTrippedResources, is(normalizedResources)); double[] roundTrippedResourceArray = bridge.translateToResourceArray(roundTrippedResources); assertThat(roundTrippedResourceArray, equalTo(resources)); }
/** * This is for testing only. It allows a test to reset the static state relating to resource names. We reset the mapping because some * algorithms sadly have different behavior if a resource exists or not. */ @VisibleForTesting public static void resetResourceNames() { RESOURCE_NAME_NORMALIZER = new ResourceNameNormalizer(); RESOURCE_MAP_ARRAY_BRIDGE = new ResourceMapArrayBridge(); }
/** * Create an empty NormalizedResources. */ NormalizedResources() { cpu = 0.0; otherResources = RESOURCE_MAP_ARRAY_BRIDGE.empty(); }
/** * Add the resources from a worker to this. * * @param value the worker resources that should be added to this. */ public void add(WorkerResources value) { Map<String, Double> workerNormalizedResources = value.get_resources(); cpu += workerNormalizedResources.getOrDefault(Constants.COMMON_CPU_RESOURCE_NAME, 0.0); add(RESOURCE_MAP_ARRAY_BRIDGE.translateToResourceArray(workerNormalizedResources)); }
/** * Remove the resources of a worker from this. * * @param value the worker resources that should be removed from this. */ public boolean remove(WorkerResources value) { Map<String, Double> workerNormalizedResources = value.get_resources(); cpu -= workerNormalizedResources.getOrDefault(Constants.COMMON_CPU_RESOURCE_NAME, 0.0); return remove(RESOURCE_MAP_ARRAY_BRIDGE.translateToResourceArray(workerNormalizedResources)) || cpu < 0; }