|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * <p> |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * <p> |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity; |
| 20 | + |
| 21 | +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueueCapacityVector.ResourceUnitCapacityType; |
| 22 | +import org.apache.hadoop.yarn.util.UnitsConversionUtil; |
| 23 | + |
| 24 | +import java.util.Map; |
| 25 | + |
| 26 | +import static org.apache.hadoop.yarn.api.records.ResourceInformation.MEMORY_URI; |
| 27 | +import static org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueueUpdateWarning.QueueUpdateWarningType.BRANCH_DOWNSCALED; |
| 28 | +import static org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.ResourceCalculationDriver.MB_UNIT; |
| 29 | + |
| 30 | +public class AbsoluteResourceCapacityCalculator extends AbstractQueueCapacityCalculator { |
| 31 | + |
| 32 | + @Override |
| 33 | + public void calculateResourcePrerequisites(ResourceCalculationDriver resourceCalculationDriver) { |
| 34 | + setNormalizedResourceRatio(resourceCalculationDriver); |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public double calculateMinimumResource( |
| 39 | + ResourceCalculationDriver resourceCalculationDriver, CalculationContext context, |
| 40 | + String label) { |
| 41 | + String resourceName = context.getResourceName(); |
| 42 | + double normalizedRatio = resourceCalculationDriver.getNormalizedResourceRatios().getOrDefault( |
| 43 | + label, ResourceVector.of(1)).getValue(resourceName); |
| 44 | + double remainingResourceRatio = resourceCalculationDriver.getRemainingRatioOfResource( |
| 45 | + label, resourceName); |
| 46 | + |
| 47 | + return normalizedRatio * remainingResourceRatio * context.getCurrentMinimumCapacityEntry( |
| 48 | + label).getResourceValue(); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public double calculateMaximumResource( |
| 53 | + ResourceCalculationDriver resourceCalculationDriver, CalculationContext context, |
| 54 | + String label) { |
| 55 | + return context.getCurrentMaximumCapacityEntry(label).getResourceValue(); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public void updateCapacitiesAfterCalculation( |
| 60 | + ResourceCalculationDriver resourceCalculationDriver, CSQueue queue, String label) { |
| 61 | + CapacitySchedulerQueueCapacityHandler.setQueueCapacities( |
| 62 | + resourceCalculationDriver.getUpdateContext().getUpdatedClusterResource(label), queue, label); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public ResourceUnitCapacityType getCapacityType() { |
| 67 | + return ResourceUnitCapacityType.ABSOLUTE; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Calculates the normalized resource ratio of a parent queue, under which children are defined |
| 72 | + * with absolute capacity type. If the effective resource of the parent is less, than the |
| 73 | + * aggregated configured absolute resource of its children, the resource ratio will be less, |
| 74 | + * than 1. |
| 75 | + * |
| 76 | + * @param calculationDriver the driver, which contains the parent queue that will form the base |
| 77 | + * of the normalization calculation |
| 78 | + */ |
| 79 | + public static void setNormalizedResourceRatio(ResourceCalculationDriver calculationDriver) { |
| 80 | + CSQueue queue = calculationDriver.getQueue(); |
| 81 | + |
| 82 | + for (String label : queue.getConfiguredNodeLabels()) { |
| 83 | + // ManagedParents assign zero capacity to queues in case of overutilization, downscaling is |
| 84 | + // turned off for their children |
| 85 | + if (queue instanceof ManagedParentQueue) { |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + for (String resourceName : queue.getConfiguredCapacityVector(label).getResourceNames()) { |
| 90 | + long childrenConfiguredResource = 0; |
| 91 | + long effectiveMinResource = queue.getQueueResourceQuotas().getEffectiveMinResource( |
| 92 | + label).getResourceValue(resourceName); |
| 93 | + |
| 94 | + // Total configured min resources of direct children of the queue |
| 95 | + for (CSQueue childQueue : queue.getChildQueues()) { |
| 96 | + if (!childQueue.getConfiguredNodeLabels().contains(label)) { |
| 97 | + continue; |
| 98 | + } |
| 99 | + QueueCapacityVector capacityVector = childQueue.getConfiguredCapacityVector(label); |
| 100 | + if (capacityVector.isResourceOfType(resourceName, ResourceUnitCapacityType.ABSOLUTE)) { |
| 101 | + childrenConfiguredResource += capacityVector.getResource(resourceName) |
| 102 | + .getResourceValue(); |
| 103 | + } |
| 104 | + } |
| 105 | + // If no children is using ABSOLUTE capacity type, normalization is not needed |
| 106 | + if (childrenConfiguredResource == 0) { |
| 107 | + continue; |
| 108 | + } |
| 109 | + // Factor to scale down effective resource: When cluster has sufficient |
| 110 | + // resources, effective_min_resources will be same as configured |
| 111 | + // min_resources. |
| 112 | + float numeratorForMinRatio = childrenConfiguredResource; |
| 113 | + if (effectiveMinResource < childrenConfiguredResource) { |
| 114 | + numeratorForMinRatio = queue.getQueueResourceQuotas().getEffectiveMinResource(label) |
| 115 | + .getResourceValue(resourceName); |
| 116 | + calculationDriver.getUpdateContext().addUpdateWarning(BRANCH_DOWNSCALED.ofQueue( |
| 117 | + queue.getQueuePath())); |
| 118 | + } |
| 119 | + |
| 120 | + String unit = resourceName.equals(MEMORY_URI) ? MB_UNIT : ""; |
| 121 | + long convertedValue = UnitsConversionUtil.convert(unit, calculationDriver.getUpdateContext() |
| 122 | + .getUpdatedClusterResource(label).getResourceInformation(resourceName).getUnits(), |
| 123 | + childrenConfiguredResource); |
| 124 | + |
| 125 | + if (convertedValue != 0) { |
| 126 | + Map<String, ResourceVector> normalizedResourceRatios = |
| 127 | + calculationDriver.getNormalizedResourceRatios(); |
| 128 | + normalizedResourceRatios.putIfAbsent(label, ResourceVector.newInstance()); |
| 129 | + normalizedResourceRatios.get(label).setValue(resourceName, numeratorForMinRatio / |
| 130 | + convertedValue); |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments