|
| 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 | + |
| 19 | +package org.apache.hadoop.yarn.server.resourcemanager.placement; |
| 20 | + |
| 21 | +import com.google.common.annotations.VisibleForTesting; |
| 22 | +import org.apache.hadoop.classification.InterfaceAudience; |
| 23 | +import org.apache.hadoop.classification.InterfaceStability; |
| 24 | +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.ResourceScheduler; |
| 25 | +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.fair.FSQueue; |
| 26 | +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.fair.FairScheduler; |
| 27 | +import org.apache.hadoop.yarn.server.resourcemanager.scheduler.fair.QueueManager; |
| 28 | +import org.slf4j.Logger; |
| 29 | +import org.slf4j.LoggerFactory; |
| 30 | +import org.w3c.dom.Element; |
| 31 | + |
| 32 | +import java.io.IOException; |
| 33 | + |
| 34 | +/** |
| 35 | + * Abstract base for all {@link FairScheduler} Placement Rules. |
| 36 | + */ |
| 37 | +@InterfaceAudience.Private |
| 38 | +@InterfaceStability.Unstable |
| 39 | +public abstract class FSPlacementRule extends PlacementRule { |
| 40 | + private static final Logger LOG = |
| 41 | + LoggerFactory.getLogger(FSPlacementRule.class); |
| 42 | + |
| 43 | + // Flag to show if the rule can create a queue |
| 44 | + @VisibleForTesting |
| 45 | + protected boolean createQueue = true; |
| 46 | + private QueueManager queueManager; |
| 47 | + private PlacementRule parentRule; |
| 48 | + |
| 49 | + /** |
| 50 | + * Get the {@link QueueManager} loaded from the scheduler when the rule is |
| 51 | + * initialised. All rules are initialised before the can be called to place |
| 52 | + * an application. |
| 53 | + * @return The queue manager from the scheduler, this can never be |
| 54 | + * <code>null</code> for an initialised rule. |
| 55 | + */ |
| 56 | + QueueManager getQueueManager() { |
| 57 | + return queueManager; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Set a rule to generate the parent queue dynamically. |
| 62 | + * @param parent A PlacementRule |
| 63 | + */ |
| 64 | + void setParentRule(PlacementRule parent) { |
| 65 | + this.parentRule = parent; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Get the rule that is set to generate the parent queue dynamically. |
| 70 | + * @return The rule set or <code>null</code> if not set. |
| 71 | + */ |
| 72 | + PlacementRule getParentRule() { |
| 73 | + return parentRule; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Set the config based on the type of object passed in. |
| 78 | + * @param initArg the config to be set |
| 79 | + */ |
| 80 | + @Override |
| 81 | + public void setConfig(Object initArg) { |
| 82 | + if (null == initArg) { |
| 83 | + LOG.debug("Null object passed in: no config set"); |
| 84 | + return; |
| 85 | + } |
| 86 | + if (initArg instanceof Element) { |
| 87 | + LOG.debug("Setting config from XML"); |
| 88 | + setConfig((Element) initArg); |
| 89 | + } else if (initArg instanceof Boolean) { |
| 90 | + LOG.debug("Setting config from Boolean"); |
| 91 | + setConfig((Boolean) initArg); |
| 92 | + } else { |
| 93 | + LOG.info("Unknown object type passed in as config for rule {}: {}", |
| 94 | + getName(), initArg.getClass()); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Set the rule config from the xml config. |
| 100 | + * @param conf An xml element from the {@link FairScheduler#conf} |
| 101 | + */ |
| 102 | + protected void setConfig(Element conf) { |
| 103 | + // Get the flag from the config (defaults to true if not set) |
| 104 | + createQueue = getCreateFlag(conf); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Set the rule config just setting the create flag. |
| 109 | + * @param create flag to allow queue creation for this rule |
| 110 | + */ |
| 111 | + protected void setConfig(Boolean create) { |
| 112 | + createQueue = create; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Standard initialisation for {@link FairScheduler} rules, shared by all |
| 117 | + * rules. Each rule that extends this abstract and overrides this method must |
| 118 | + * call <code>super.initialize()</code> to run this basic initialisation. |
| 119 | + * @param scheduler the scheduler using the rule |
| 120 | + * @return <code>true</code> in all cases |
| 121 | + * @throws IOException for any errors |
| 122 | + */ |
| 123 | + @Override |
| 124 | + public boolean initialize(ResourceScheduler scheduler) throws IOException { |
| 125 | + if (!(scheduler instanceof FairScheduler)) { |
| 126 | + throw new IOException(getName() + |
| 127 | + " rule can only be configured for the FairScheduler"); |
| 128 | + } |
| 129 | + if (getParentRule() != null && |
| 130 | + getParentRule().getName().equals(getName())) { |
| 131 | + throw new IOException("Parent rule may not be the same type as the " + |
| 132 | + "child rule: " + getName()); |
| 133 | + } |
| 134 | + |
| 135 | + FairScheduler fs = (FairScheduler) scheduler; |
| 136 | + queueManager = fs.getQueueManager(); |
| 137 | + |
| 138 | + return true; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Check if the queue exists and is part of the configuration i.e. not |
| 143 | + * a {@link FSQueue#isDynamic()} queue. |
| 144 | + * @param queueName name of the queue to check |
| 145 | + * @return <code>true</code> if the queue exists and is a "configured" queue |
| 146 | + */ |
| 147 | + boolean configuredQueue(String queueName) { |
| 148 | + FSQueue queue = queueManager.getQueue(queueName); |
| 149 | + return (queue != null && !queue.isDynamic()); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Get the create flag from the xml configuration element. |
| 154 | + * @param conf The FS configuration element for the queue |
| 155 | + * @return <code>false</code> only if the flag is set in the configuration to |
| 156 | + * a text that is not case ignored "true", <code>true</code> in all other |
| 157 | + * cases |
| 158 | + */ |
| 159 | + boolean getCreateFlag(Element conf) { |
| 160 | + if (conf != null) { |
| 161 | + String create = conf.getAttribute("create"); |
| 162 | + return Boolean.parseBoolean(create); |
| 163 | + } |
| 164 | + return true; |
| 165 | + } |
| 166 | +} |
0 commit comments