瀏覽代碼

YARN-5753. fix NPE in AMRMClientImpl.getMatchingRequests() (haibochen via rkanter)

Robert Kanter 8 年之前
父節點
當前提交
44fdf00964

+ 14 - 9
hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/main/java/org/apache/hadoop/yarn/client/api/impl/AMRMClientImpl.java

@@ -664,15 +664,20 @@ public class AMRMClientImpl<T extends ContainerRequest> extends AMRMClient<T> {
     List<LinkedHashSet<T>> list = new LinkedList<LinkedHashSet<T>>();
 
     RemoteRequestsTable remoteRequestsTable = getTable(0);
-    List<ResourceRequestInfo<T>> matchingRequests =
-        remoteRequestsTable.getMatchingRequests(priority, resourceName,
-            executionType, capability);
-    // If no exact match. Container may be larger than what was requested.
-    // get all resources <= capability. map is reverse sorted.
-    for (ResourceRequestInfo<T> resReqInfo : matchingRequests) {
-      if (canFit(resReqInfo.remoteRequest.getCapability(), capability) &&
-        !resReqInfo.containerRequests.isEmpty()) {
-        list.add(resReqInfo.containerRequests);
+
+    if (null != remoteRequestsTable) {
+      List<ResourceRequestInfo<T>> matchingRequests =
+          remoteRequestsTable.getMatchingRequests(priority, resourceName,
+              executionType, capability);
+      if (null != matchingRequests) {
+        // If no exact match. Container may be larger than what was requested.
+        // get all resources <= capability. map is reverse sorted.
+        for (ResourceRequestInfo<T> resReqInfo : matchingRequests) {
+          if (canFit(resReqInfo.remoteRequest.getCapability(), capability) &&
+              !resReqInfo.containerRequests.isEmpty()) {
+            list.add(resReqInfo.containerRequests);
+          }
+        }
       }
     }
     // no match found

+ 14 - 0
hadoop-yarn-project/hadoop-yarn/hadoop-yarn-client/src/test/java/org/apache/hadoop/yarn/client/api/impl/TestAMRMClient.java

@@ -235,6 +235,20 @@ public class TestAMRMClient {
       yarnCluster.stop();
     }
   }
+
+  @Test (timeout = 60000)
+  public void testAMRMClientNoMatchingRequests()
+      throws IOException, YarnException {
+    AMRMClient<ContainerRequest> amClient =  AMRMClient.createAMRMClient();
+    amClient.init(conf);
+    amClient.start();
+    amClient.registerApplicationMaster("Host", 10000, "");
+
+    Resource testCapability1 = Resource.newInstance(1024,  2);
+    List<? extends Collection<ContainerRequest>> matches =
+        amClient.getMatchingRequests(priority, node, testCapability1);
+    assertEquals("Expected no macthing requests.", matches.size(), 0);
+  }
   
   @Test (timeout=60000)
   public void testAMRMClientMatchingFit() throws YarnException, IOException {