9uapaw commented on a change in pull request #3358:
URL: https://github.com/apache/hadoop/pull/3358#discussion_r732534390



##########
File path: 
hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/scheduler/capacity/conf/QueueCapacityConfigParser.java
##########
@@ -0,0 +1,210 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.conf;
+
+import 
org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.CapacitySchedulerConfiguration;
+import 
org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueueCapacityVector;
+import 
org.apache.hadoop.yarn.server.resourcemanager.scheduler.capacity.QueueCapacityVector.QueueCapacityType;
+import org.apache.hadoop.yarn.util.UnitsConversionUtil;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.function.Function;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * A class that parses {@code QueueCapacityVector} from the capacity
+ * configuration property set for a queue.
+ *
+ * A new syntax for capacity property could be implemented, by creating a 
parser
+ * with a regex to match the pattern and a method that creates a
+ * {@code QueueCapacityVector} from the matched pattern
+ * eg. root.capacity 20-50
+ *
+ * A new capacity type for the existing parsers could be added by extending
+ * the {@code QueueCapacityVector.QueueCapacityType} with a new type and its
+ * associated postfix symbol.
+ * eg. root.capacity 20g
+ */
+public class QueueCapacityConfigParser {
+  private static final String UNIFORM_REGEX = "^([0-9.]+)(.*)";
+  private static final String RESOURCE_REGEX = "^\\[([\\w\\.,\\-_%\\ 
/]+=[\\w\\.,\\-_%\\ /]+)+\\]$";
+
+  private static final Pattern RESOURCE_PATTERN = 
Pattern.compile(RESOURCE_REGEX);
+  private static final Pattern UNIFORM_PATTERN = 
Pattern.compile(UNIFORM_REGEX);
+
+  private final List<Parser> parsers = new ArrayList<>();
+
+  public QueueCapacityConfigParser() {
+    parsers.add(new Parser(RESOURCE_PATTERN, this::heterogeneousParser));
+    parsers.add(new Parser(UNIFORM_PATTERN, this::uniformParser));
+  }
+
+  /**
+   * Creates a {@code QueueCapacityVector} parsed from the capacity 
configuration
+   * property set for a queue.
+   * @param conf configuration object
+   * @param queuePath queue for which the capacity property is parsed
+   * @param label node label
+   * @return a parsed capacity vector
+   */
+  public QueueCapacityVector parse(CapacitySchedulerConfiguration conf,
+                                   String queuePath, String label) {
+
+    if (queuePath.equals(CapacitySchedulerConfiguration.ROOT)) {
+      return QueueCapacityVector.of(100f, QueueCapacityType.PERCENTAGE);
+    }
+
+    String propertyName = CapacitySchedulerConfiguration.getNodeLabelPrefix(
+        queuePath, label) + CapacitySchedulerConfiguration.CAPACITY;
+    String capacityString = conf.get(propertyName);
+
+    if (capacityString == null) {
+      return new QueueCapacityVector();
+    }
+
+    for (Parser parser : parsers) {
+      Matcher matcher = parser.regex.matcher(capacityString);
+      if (matcher.find()) {
+        return parser.parser.apply(matcher);
+      }
+    }
+
+    return new QueueCapacityVector();
+  }
+
+  /**
+   * A parser method that is usable on uniform capacity values eg. percentage 
or
+   * weight.
+   * @param matcher a regex matcher that contains parsed value and its possible
+   *                suffix
+   * @return a parsed resource vector
+   */
+  private QueueCapacityVector uniformParser(Matcher matcher) {
+    QueueCapacityType capacityType = QueueCapacityType.PERCENTAGE;
+    String value = matcher.group(1);
+    if (matcher.groupCount() == 2) {
+      String matchedSuffix = matcher.group(2);
+      if (!matchedSuffix.isEmpty()) {
+        for (QueueCapacityType suffix : QueueCapacityType.values()) {
+          // when capacity is given in percentage, we do not need % symbol
+          String uniformSuffix = suffix.getPostfix().replaceAll("%", "");
+          if (uniformSuffix.equals(matchedSuffix)) {
+            capacityType = suffix;
+          }
+        }
+      }
+    }

Review comment:
       Its an invalid config, added a test case to cover this.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to