hadoop-common-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From templ...@apache.org
Subject hadoop git commit: YARN-7442. [YARN-7069] Limit format of resource type name (Contributed by Wangda Tan via Daniel Templeton)
Date Mon, 13 Nov 2017 19:04:05 GMT
Repository: hadoop
Updated Branches:
  refs/heads/branch-3.0 f90f77238 -> 5b55a74ba


YARN-7442. [YARN-7069] Limit format of resource type name (Contributed by Wangda Tan via Daniel
Templeton)

(cherry picked from commit 2c6213a44280f5b3950167131293ff83f48ff56f)


Project: http://git-wip-us.apache.org/repos/asf/hadoop/repo
Commit: http://git-wip-us.apache.org/repos/asf/hadoop/commit/5b55a74b
Tree: http://git-wip-us.apache.org/repos/asf/hadoop/tree/5b55a74b
Diff: http://git-wip-us.apache.org/repos/asf/hadoop/diff/5b55a74b

Branch: refs/heads/branch-3.0
Commit: 5b55a74bad2fe02bc41ffc3e13d46468d4c92d22
Parents: f90f772
Author: Daniel Templeton <templedf@apache.org>
Authored: Mon Nov 13 10:37:30 2017 -0800
Committer: Daniel Templeton <templedf@apache.org>
Committed: Mon Nov 13 11:03:56 2017 -0800

----------------------------------------------------------------------
 .../yarn/api/records/ResourceInformation.java   |  5 +++
 .../yarn/util/resource/ResourceUtils.java       | 26 ++++++++++++++
 .../yarn/util/resource/TestResourceUtils.java   | 37 ++++++++++++++++++++
 3 files changed, 68 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/hadoop/blob/5b55a74b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/api/records/ResourceInformation.java
----------------------------------------------------------------------
diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/api/records/ResourceInformation.java
b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/api/records/ResourceInformation.java
index cc61d86..e8280ba 100644
--- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/api/records/ResourceInformation.java
+++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/api/records/ResourceInformation.java
@@ -55,6 +55,11 @@ public class ResourceInformation implements Comparable<ResourceInformation>
{
   /**
    * Set the name for the resource.
    *
+   * A valid resource name must begin with a letter and contain only letters,
+   * numbers, and any of: '.', '_', or '-'. A valid resource name may also be
+   * optionally preceded by a name space followed by a slash. A valid name space
+   * consists of period-separated groups of letters, numbers, and dashes."
+   *
    * @param rName name for the resource
    */
   public void setName(String rName) {

http://git-wip-us.apache.org/repos/asf/hadoop/blob/5b55a74b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/util/resource/ResourceUtils.java
----------------------------------------------------------------------
diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/util/resource/ResourceUtils.java
b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/util/resource/ResourceUtils.java
index 002a6de..540cd9e 100644
--- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/util/resource/ResourceUtils.java
+++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/main/java/org/apache/hadoop/yarn/util/resource/ResourceUtils.java
@@ -62,6 +62,10 @@ public class ResourceUtils {
   private static final Pattern RESOURCE_REQUEST_VALUE_PATTERN =
       Pattern.compile("^([0-9]+) ?([a-zA-Z]*)$");
 
+  private static final Pattern RESOURCE_NAME_PATTERN = Pattern.compile(
+      "^(((\\p{Alnum}([\\p{Alnum}-]*\\p{Alnum})?\\.)*"
+          + "\\p{Alnum}([\\p{Alnum}-]*\\p{Alnum})?)/)?\\p{Alpha}([\\w.-]*)$");
+
   private static volatile boolean initializedResources = false;
   private static final Map<String, Integer> RESOURCE_NAME_TO_INDEX =
       new ConcurrentHashMap<String, Integer>();
@@ -210,6 +214,23 @@ public class ResourceUtils {
   }
 
   @VisibleForTesting
+  static void validateNameOfResourceNameAndThrowException(String resourceName)
+      throws YarnRuntimeException {
+    Matcher matcher = RESOURCE_NAME_PATTERN.matcher(resourceName);
+    if (!matcher.matches()) {
+      String message = String.format(
+          "'%s' is not a valid resource name. A valid resource name must"
+              + " begin with a letter and contain only letters, numbers, "
+              + "and any of: '.', '_', or '-'. A valid resource name may also"
+              + " be optionally preceded by a name space followed by a slash."
+              + " A valid name space consists of period-separated groups of"
+              + " letters, numbers, and dashes.",
+          resourceName);
+      throw new YarnRuntimeException(message);
+    }
+  }
+
+  @VisibleForTesting
   static void initializeResourcesMap(Configuration conf) {
 
     Map<String, ResourceInformation> resourceInformationMap = new HashMap<>();
@@ -247,6 +268,11 @@ public class ResourceUtils {
       }
     }
 
+    // Validate names of resource information map.
+    for (String name : resourceInformationMap.keySet()) {
+      validateNameOfResourceNameAndThrowException(name);
+    }
+
     checkMandatoryResources(resourceInformationMap);
     addMandatoryResources(resourceInformationMap);
 

http://git-wip-us.apache.org/repos/asf/hadoop/blob/5b55a74b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/util/resource/TestResourceUtils.java
----------------------------------------------------------------------
diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/util/resource/TestResourceUtils.java
b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/util/resource/TestResourceUtils.java
index d6bab92..b316892 100644
--- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/util/resource/TestResourceUtils.java
+++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-common/src/test/java/org/apache/hadoop/yarn/util/resource/TestResourceUtils.java
@@ -24,6 +24,7 @@ import org.apache.hadoop.yarn.api.protocolrecords.ResourceTypes;
 import org.apache.hadoop.yarn.api.records.Resource;
 import org.apache.hadoop.yarn.api.records.ResourceInformation;
 import org.apache.hadoop.yarn.conf.YarnConfiguration;
+import org.apache.hadoop.yarn.exceptions.YarnRuntimeException;
 import org.junit.After;
 import org.junit.Assert;
 import org.junit.Before;
@@ -296,6 +297,42 @@ public class TestResourceUtils {
     }
   }
 
+  @Test
+  public void testResourceNameFormatValidation() throws Exception {
+    String[] validNames = new String[] {
+        "yarn.io/gpu",
+        "gpu",
+        "g_1_2",
+        "123.io/gpu",
+        "prefix/resource_1",
+        "a___-3",
+        "a....b",
+    };
+
+    String[] invalidNames = new String[] {
+        "asd/resource/-name",
+        "prefix/-resource_1",
+        "prefix/0123resource",
+        "0123resource",
+        "-resource_1",
+        "........abc"
+    };
+
+    for (String validName : validNames) {
+      ResourceUtils.validateNameOfResourceNameAndThrowException(validName);
+    }
+
+    for (String invalidName : invalidNames) {
+      try {
+        ResourceUtils.validateNameOfResourceNameAndThrowException(invalidName);
+        Assert.fail("Expected to fail name check, the name=" + invalidName
+            + " is illegal.");
+      } catch (YarnRuntimeException e) {
+        // Expected
+      }
+    }
+  }
+
   public static String setupResourceTypes(Configuration conf, String filename)
       throws Exception {
     File source = new File(


---------------------------------------------------------------------
To unsubscribe, e-mail: common-commits-unsubscribe@hadoop.apache.org
For additional commands, e-mail: common-commits-help@hadoop.apache.org


Mime
View raw message