|
| 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.server.api.protocolrecords; |
| 19 | + |
| 20 | +import org.apache.commons.lang3.StringUtils; |
| 21 | +import org.apache.commons.lang3.math.NumberUtils; |
| 22 | +import org.apache.hadoop.classification.InterfaceAudience.Private; |
| 23 | +import org.apache.hadoop.classification.InterfaceAudience.Public; |
| 24 | +import org.apache.hadoop.classification.InterfaceStability.Unstable; |
| 25 | +import org.apache.hadoop.yarn.exceptions.YarnException; |
| 26 | +import org.apache.hadoop.yarn.util.Records; |
| 27 | + |
| 28 | +import java.util.LinkedHashMap; |
| 29 | +import java.util.Map; |
| 30 | + |
| 31 | +/** |
| 32 | + * Queue weights for representing Federation. |
| 33 | + */ |
| 34 | +@Private |
| 35 | +@Unstable |
| 36 | +public abstract class FederationQueueWeight { |
| 37 | + |
| 38 | + /** |
| 39 | + * The FederationQueueWeight object consists of three parts: |
| 40 | + * routerWeight, amrmWeight, and headRoomAlpha. |
| 41 | + * |
| 42 | + * @param routerWeight Weight for routing applications to different subclusters. |
| 43 | + * We will route the application to different subclusters based on the configured weights. |
| 44 | + * Assuming we have two subclusters, SC-1 and SC-2, |
| 45 | + * with a weight of 0.7 for SC-1 and 0.3 for SC-2, |
| 46 | + * the application will be allocated in such a way |
| 47 | + * that 70% of the applications will be assigned to SC-1 and 30% to SC-2. |
| 48 | + * |
| 49 | + * @param amrmWeight Weight for resource request from ApplicationMaster (AM) to |
| 50 | + * different subclusters' Resource Manager (RM). |
| 51 | + * Assuming we have two subclusters, SC-1 and SC-2, |
| 52 | + * with a weight of 0.6 for SC-1 and 0.4 for SC-2, |
| 53 | + * When AM requesting resources, |
| 54 | + * 60% of the requests will be made to the Resource Manager (RM) of SC-1 |
| 55 | + * and 40% to the RM of SC-2. |
| 56 | + * |
| 57 | + * @param headRoomAlpha |
| 58 | + * used by policies that balance weight-based and load-based considerations in their decisions. |
| 59 | + * For policies that use this parameter, |
| 60 | + * values close to 1 indicate that most of the decision |
| 61 | + * should be based on currently observed headroom from various sub-clusters, |
| 62 | + * values close to zero, indicate that the decision should be |
| 63 | + * mostly based on weights and practically ignore current load. |
| 64 | + * |
| 65 | + * @return FederationQueueWeight |
| 66 | + */ |
| 67 | + @Private |
| 68 | + @Unstable |
| 69 | + public static FederationQueueWeight newInstance(String routerWeight, |
| 70 | + String amrmWeight, String headRoomAlpha) { |
| 71 | + FederationQueueWeight federationQueueWeight = Records.newRecord(FederationQueueWeight.class); |
| 72 | + federationQueueWeight.setRouterWeight(routerWeight); |
| 73 | + federationQueueWeight.setAmrmWeight(amrmWeight); |
| 74 | + federationQueueWeight.setHeadRoomAlpha(headRoomAlpha); |
| 75 | + return federationQueueWeight; |
| 76 | + } |
| 77 | + |
| 78 | + @Public |
| 79 | + @Unstable |
| 80 | + public abstract String getRouterWeight(); |
| 81 | + |
| 82 | + @Public |
| 83 | + @Unstable |
| 84 | + public abstract void setRouterWeight(String routerWeight); |
| 85 | + |
| 86 | + @Public |
| 87 | + @Unstable |
| 88 | + public abstract String getAmrmWeight(); |
| 89 | + |
| 90 | + @Public |
| 91 | + @Unstable |
| 92 | + public abstract void setAmrmWeight(String amrmWeight); |
| 93 | + |
| 94 | + @Public |
| 95 | + @Unstable |
| 96 | + public abstract String getHeadRoomAlpha(); |
| 97 | + |
| 98 | + @Public |
| 99 | + @Unstable |
| 100 | + public abstract void setHeadRoomAlpha(String headRoomAlpha); |
| 101 | + |
| 102 | + private static final String COMMA = ","; |
| 103 | + private static final String COLON = ":"; |
| 104 | + |
| 105 | + /** |
| 106 | + * Check if the subCluster Queue Weight Ratio are valid. |
| 107 | + * |
| 108 | + * This method can be used to validate RouterPolicyWeight and AMRMPolicyWeight. |
| 109 | + * |
| 110 | + * @param subClusterWeight the weight ratios of subClusters. |
| 111 | + * @throws YarnException exceptions from yarn servers. |
| 112 | + */ |
| 113 | + public static void checkSubClusterQueueWeightRatioValid(String subClusterWeight) |
| 114 | + throws YarnException { |
| 115 | + // The subClusterWeight cannot be empty. |
| 116 | + if (StringUtils.isBlank(subClusterWeight)) { |
| 117 | + throw new YarnException("subClusterWeight can't be empty!"); |
| 118 | + } |
| 119 | + |
| 120 | + // SC-1:0.7,SC-2:0.3 -> [SC-1:0.7,SC-2:0.3] |
| 121 | + String[] subClusterWeights = subClusterWeight.split(COMMA); |
| 122 | + Map<String, Double> subClusterWeightMap = new LinkedHashMap<>(); |
| 123 | + for (String subClusterWeightItem : subClusterWeights) { |
| 124 | + // SC-1:0.7 -> [SC-1,0.7] |
| 125 | + // We require that the parsing result is not empty and must have a length of 2. |
| 126 | + String[] subClusterWeightItems = subClusterWeightItem.split(COLON); |
| 127 | + if (subClusterWeightItems == null || subClusterWeightItems.length != 2) { |
| 128 | + throw new YarnException("The subClusterWeight cannot be empty," + |
| 129 | + " and the subClusterWeight size must be 2. (eg.SC-1,0.2)"); |
| 130 | + } |
| 131 | + subClusterWeightMap.put(subClusterWeightItems[0], Double.valueOf(subClusterWeightItems[1])); |
| 132 | + } |
| 133 | + |
| 134 | + // The sum of weight ratios for subClusters must be equal to 1. |
| 135 | + double sum = subClusterWeightMap.values().stream().mapToDouble(Double::doubleValue).sum(); |
| 136 | + boolean isValid = Math.abs(sum - 1.0) < 1e-6; // Comparing with a tolerance of 1e-6 |
| 137 | + |
| 138 | + if (!isValid) { |
| 139 | + throw new YarnException("The sum of ratios for all subClusters must be equal to 1."); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Check if HeadRoomAlpha is a number and is between 0 and 1. |
| 145 | + * |
| 146 | + * @param headRoomAlpha headroomalpha. |
| 147 | + * @throws YarnException exceptions from yarn servers. |
| 148 | + */ |
| 149 | + public static void checkHeadRoomAlphaValid(String headRoomAlpha) throws YarnException { |
| 150 | + if (!isNumeric(headRoomAlpha)) { |
| 151 | + throw new YarnException("HeadRoomAlpha must be a number."); |
| 152 | + } |
| 153 | + |
| 154 | + double dHeadRoomAlpha = Double.parseDouble(headRoomAlpha); |
| 155 | + if (!(dHeadRoomAlpha >= 0 && dHeadRoomAlpha <= 1)) { |
| 156 | + throw new YarnException("HeadRoomAlpha must be between 0-1."); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Determines whether the given value is a number. |
| 162 | + * |
| 163 | + * @param value given value. |
| 164 | + * @return true, is a number, false, not a number. |
| 165 | + */ |
| 166 | + protected static boolean isNumeric(String value) { |
| 167 | + return NumberUtils.isCreatable(value); |
| 168 | + } |
| 169 | +} |
0 commit comments