/** * 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; }
/** * Remove the resources in other from this. * @param other the resources to be removed. * @param resourceMetrics The resource related metrics * @return true if one or more resources in other were larger than available resources in this, else false. */ public boolean remove(WorkerResources other, ResourceMetrics resourceMetrics) { boolean negativeResources = normalizedResources.remove(other); totalMemoryMb -= (other.get_mem_off_heap() + other.get_mem_on_heap()); if (totalMemoryMb < 0.0) { negativeResources = true; resourceMetrics.getNegativeResourceEventsMeter().mark(); totalMemoryMb = 0.0; } return negativeResources; }
/** * Remove the resources in other from this. * @param other the resources to be removed. * @param resourceMetrics The resource related metrics * @return true if one or more resources in other were larger than available resources in this, else false. */ public boolean remove(NormalizedResourcesWithMemory other, ResourceMetrics resourceMetrics) { boolean negativeResources = normalizedResources.remove(other.getNormalizedResources(), resourceMetrics); totalMemoryMb -= other.getTotalMemoryMb(); if (totalMemoryMb < 0.0) { negativeResources = true; resourceMetrics.getNegativeResourceEventsMeter().mark(); totalMemoryMb = 0.0; } return negativeResources; }
@Test public void testRemoveFromCpu() { NormalizedResources resources = new NormalizedResources(normalize(Collections.singletonMap(Constants.COMMON_CPU_RESOURCE_NAME, 2))); NormalizedResources removedResources = new NormalizedResources(normalize(Collections.singletonMap(Constants.COMMON_CPU_RESOURCE_NAME, 1))); resources.remove(removedResources, new ResourceMetrics(new StormMetricsRegistry())); Map<String, Double> normalizedMap = resources.toNormalizedMap(); assertThat(normalizedMap.get(Constants.COMMON_CPU_RESOURCE_NAME), is(1.0)); assertThat(resources.getTotalCpu(), is(1.0)); }
@Test public void testRemoveZeroesWhenResourcesBecomeNegative() { NormalizedResources resources = new NormalizedResources(normalize(Collections.singletonMap(gpuResourceName, 1))); NormalizedResources removedResources = new NormalizedResources(normalize(Collections.singletonMap(gpuResourceName, 2))); resources.remove(removedResources, new ResourceMetrics(new StormMetricsRegistry())); Map<String, Double> normalizedMap = resources.toNormalizedMap(); assertThat(normalizedMap.get(gpuResourceName), is(0.0)); }
@Test public void testRemoveFromExistingResources() { NormalizedResources resources = new NormalizedResources(normalize(Collections.singletonMap(gpuResourceName, 15))); NormalizedResources removedResources = new NormalizedResources(normalize(Collections.singletonMap(gpuResourceName, 1))); resources.remove(removedResources, new ResourceMetrics(new StormMetricsRegistry())); Map<String, Double> normalizedMap = resources.toNormalizedMap(); assertThat(normalizedMap.get(gpuResourceName), is(14.0)); }
@Test public void testRemoveZeroesWhenCpuBecomesNegative() { NormalizedResources resources = new NormalizedResources(normalize(Collections.singletonMap(Constants.COMMON_CPU_RESOURCE_NAME, 1))); NormalizedResources removedResources = new NormalizedResources(normalize(Collections.singletonMap(Constants.COMMON_CPU_RESOURCE_NAME, 2))); resources.remove(removedResources, new ResourceMetrics(new StormMetricsRegistry())); assertThat(resources.getTotalCpu(), is(0.0)); }