|
| 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 | +* |
| 10 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +* |
| 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 | +package org.apache.hadoop.yarn.util.resource; |
| 19 | + |
| 20 | +import org.apache.hadoop.classification.InterfaceAudience.Private; |
| 21 | +import org.apache.hadoop.classification.InterfaceStability.Unstable; |
| 22 | +import org.apache.hadoop.yarn.api.records.Resource; |
| 23 | + |
| 24 | +/** |
| 25 | + * A {@link ResourceCalculator} which uses the concept of |
| 26 | + * <em>dominant resource</em> to compare multi-dimensional resources. |
| 27 | + * |
| 28 | + * Essentially the idea is that the in a multi-resource environment, |
| 29 | + * the resource allocation should be determined by the dominant share |
| 30 | + * of an entity (user or queue), which is the maximum share that the |
| 31 | + * entity has been allocated of any resource. |
| 32 | + * |
| 33 | + * In a nutshell, it seeks to maximize the minimum dominant share across |
| 34 | + * all entities. |
| 35 | + * |
| 36 | + * For example, if user A runs CPU-heavy tasks and user B runs |
| 37 | + * memory-heavy tasks, it attempts to equalize CPU share of user A |
| 38 | + * with Memory-share of user B. |
| 39 | + * |
| 40 | + * In the single resource case, it reduces to max-min fairness for that resource. |
| 41 | + * |
| 42 | + * See the Dominant Resource Fairness paper for more details: |
| 43 | + * www.cs.berkeley.edu/~matei/papers/2011/nsdi_drf.pdf |
| 44 | + */ |
| 45 | +@Private |
| 46 | +@Unstable |
| 47 | +public class DominantResourceCalculator extends ResourceCalculator { |
| 48 | + |
| 49 | + @Override |
| 50 | + public int compare(Resource clusterResource, Resource lhs, Resource rhs) { |
| 51 | + |
| 52 | + if (lhs.equals(rhs)) { |
| 53 | + return 0; |
| 54 | + } |
| 55 | + |
| 56 | + float l = getResourceAsValue(clusterResource, lhs, true); |
| 57 | + float r = getResourceAsValue(clusterResource, rhs, true); |
| 58 | + |
| 59 | + if (l < r) { |
| 60 | + return -1; |
| 61 | + } else if (l > r) { |
| 62 | + return 1; |
| 63 | + } else { |
| 64 | + l = getResourceAsValue(clusterResource, lhs, false); |
| 65 | + r = getResourceAsValue(clusterResource, rhs, false); |
| 66 | + if (l < r) { |
| 67 | + return -1; |
| 68 | + } else if (l > r) { |
| 69 | + return 1; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + return 0; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Use 'dominant' for now since we only have 2 resources - gives us a slight |
| 78 | + * performance boost. |
| 79 | + * |
| 80 | + * Once we add more resources, we'll need a more complicated (and slightly |
| 81 | + * less performant algorithm). |
| 82 | + */ |
| 83 | + protected float getResourceAsValue( |
| 84 | + Resource clusterResource, Resource resource, boolean dominant) { |
| 85 | + // Just use 'dominant' resource |
| 86 | + return (dominant) ? |
| 87 | + Math.max( |
| 88 | + (float)resource.getMemory() / clusterResource.getMemory(), |
| 89 | + (float)resource.getVirtualCores() / clusterResource.getVirtualCores() |
| 90 | + ) |
| 91 | + : |
| 92 | + Math.min( |
| 93 | + (float)resource.getMemory() / clusterResource.getMemory(), |
| 94 | + (float)resource.getVirtualCores() / clusterResource.getVirtualCores() |
| 95 | + ); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public int computeAvailableContainers(Resource available, Resource required) { |
| 100 | + return Math.min( |
| 101 | + available.getMemory() / required.getMemory(), |
| 102 | + available.getVirtualCores() / required.getVirtualCores()); |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public float divide(Resource clusterResource, |
| 107 | + Resource numerator, Resource denominator) { |
| 108 | + return |
| 109 | + getResourceAsValue(clusterResource, numerator, true) / |
| 110 | + getResourceAsValue(clusterResource, denominator, true); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public float ratio(Resource a, Resource b) { |
| 115 | + return Math.max( |
| 116 | + (float)a.getMemory()/b.getMemory(), |
| 117 | + (float)a.getVirtualCores()/b.getVirtualCores() |
| 118 | + ); |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public Resource divideAndCeil(Resource numerator, int denominator) { |
| 123 | + return Resources.createResource( |
| 124 | + divideAndCeil(numerator.getMemory(), denominator), |
| 125 | + divideAndCeil(numerator.getVirtualCores(), denominator) |
| 126 | + ); |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public Resource normalize(Resource r, Resource minimumResource, |
| 131 | + Resource maximumResource, Resource stepFactor) { |
| 132 | + int normalizedMemory = Math.min( |
| 133 | + roundUp( |
| 134 | + Math.max(r.getMemory(), minimumResource.getMemory()), |
| 135 | + stepFactor.getMemory()), |
| 136 | + maximumResource.getMemory()); |
| 137 | + int normalizedCores = Math.min( |
| 138 | + roundUp( |
| 139 | + Math.max(r.getVirtualCores(), minimumResource.getVirtualCores()), |
| 140 | + stepFactor.getVirtualCores()), |
| 141 | + maximumResource.getVirtualCores()); |
| 142 | + return Resources.createResource(normalizedMemory, |
| 143 | + normalizedCores); |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + public Resource roundUp(Resource r, Resource stepFactor) { |
| 148 | + return Resources.createResource( |
| 149 | + roundUp(r.getMemory(), stepFactor.getMemory()), |
| 150 | + roundUp(r.getVirtualCores(), stepFactor.getVirtualCores()) |
| 151 | + ); |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public Resource roundDown(Resource r, Resource stepFactor) { |
| 156 | + return Resources.createResource( |
| 157 | + roundDown(r.getMemory(), stepFactor.getMemory()), |
| 158 | + roundDown(r.getVirtualCores(), stepFactor.getVirtualCores()) |
| 159 | + ); |
| 160 | + } |
| 161 | + |
| 162 | + @Override |
| 163 | + public Resource multiplyAndNormalizeUp(Resource r, double by, |
| 164 | + Resource stepFactor) { |
| 165 | + return Resources.createResource( |
| 166 | + roundUp( |
| 167 | + (int)Math.ceil(r.getMemory() * by), stepFactor.getMemory()), |
| 168 | + roundUp( |
| 169 | + (int)Math.ceil(r.getVirtualCores() * by), |
| 170 | + stepFactor.getVirtualCores()) |
| 171 | + ); |
| 172 | + } |
| 173 | + |
| 174 | + @Override |
| 175 | + public Resource multiplyAndNormalizeDown(Resource r, double by, |
| 176 | + Resource stepFactor) { |
| 177 | + return Resources.createResource( |
| 178 | + roundDown( |
| 179 | + (int)(r.getMemory() * by), |
| 180 | + stepFactor.getMemory() |
| 181 | + ), |
| 182 | + roundDown( |
| 183 | + (int)(r.getVirtualCores() * by), |
| 184 | + stepFactor.getVirtualCores() |
| 185 | + ) |
| 186 | + ); |
| 187 | + } |
| 188 | + |
| 189 | +} |
0 commit comments