ソースを参照

MAPREDUCE-2539. Fixed NPE in getMapTaskReports in JobClient. Contributed by Robert Evans.

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/branches/branch-0.22@1236961 13f79535-47bb-0310-9956-ffa450edef68
Konstantin Shvachko 13 年 前
コミット
26eedfd384

+ 3 - 0
mapreduce/CHANGES.txt

@@ -14,6 +14,9 @@ Release 0.22.1 - Unreleased
 
     MAPREDUCE-3593. Fix user impersonation. (Mayank Bansal via shv)
 
+    MAPREDUCE-2539. Fixed NPE in getMapTaskReports in JobClient. 
+    (Robert Evans via shv)
+
 Release 0.22.0 - 2011-11-29
 
   INCOMPATIBLE CHANGES

+ 14 - 20
mapreduce/src/java/org/apache/hadoop/mapred/JobClient.java

@@ -573,6 +573,8 @@ public class JobClient extends CLI {
     return getJob(JobID.forName(jobid));
   }
   
+  private static final TaskReport[] EMPTY_TASK_REPORTS = new TaskReport[0];
+  
   /**
    * Get the information of the current state of the map tasks of a job.
    * 
@@ -581,9 +583,16 @@ public class JobClient extends CLI {
    * @throws IOException
    */
   public TaskReport[] getMapTaskReports(JobID jobId) throws IOException {
+    return getTaskReports(jobId, TaskType.MAP);
+  }
+  
+  private TaskReport[] getTaskReports(JobID jobId, TaskType type) throws IOException {
     try {
-      return TaskReport.downgradeArray(
-        cluster.getJob(jobId).getTaskReports(TaskType.MAP));
+      Job j = cluster.getJob(jobId);
+      if(j == null) {
+        return EMPTY_TASK_REPORTS;
+      }
+      return TaskReport.downgradeArray(j.getTaskReports(type));
     } catch (InterruptedException ie) {
       throw new IOException(ie);
     }
@@ -603,12 +612,7 @@ public class JobClient extends CLI {
    * @throws IOException
    */    
   public TaskReport[] getReduceTaskReports(JobID jobId) throws IOException {
-    try {
-      return TaskReport.downgradeArray(
-        cluster.getJob(jobId).getTaskReports(TaskType.REDUCE));
-    } catch (InterruptedException ie) {
-      throw new IOException(ie);
-    }
+    return getTaskReports(jobId, TaskType.REDUCE);
   }
 
   /**
@@ -619,12 +623,7 @@ public class JobClient extends CLI {
    * @throws IOException
    */    
   public TaskReport[] getCleanupTaskReports(JobID jobId) throws IOException {
-    try {
-      return TaskReport.downgradeArray(
-        cluster.getJob(jobId).getTaskReports(TaskType.JOB_CLEANUP));
-    } catch (InterruptedException ie) {
-      throw new IOException(ie);
-    }
+    return getTaskReports(jobId, TaskType.JOB_CLEANUP);
   }
 
   /**
@@ -635,12 +634,7 @@ public class JobClient extends CLI {
    * @throws IOException
    */    
   public TaskReport[] getSetupTaskReports(JobID jobId) throws IOException {
-    try {
-      return TaskReport.downgradeArray(
-        cluster.getJob(jobId).getTaskReports(TaskType.JOB_SETUP));
-    } catch (InterruptedException ie) {
-      throw new IOException(ie);
-    }
+    return getTaskReports(jobId, TaskType.JOB_SETUP);
   }
 
   

+ 94 - 0
mapreduce/src/test/mapred/org/apache/hadoop/mapred/JobClientUnitTest.java

@@ -0,0 +1,94 @@
+/**
+ * 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.mapred;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import org.apache.hadoop.mapreduce.Cluster;
+import org.junit.Test;
+
+public class JobClientUnitTest {
+  
+  @SuppressWarnings("deprecation")
+  @Test
+  public void testMapTaskReportsWithNullJob() throws Exception {
+    JobClient client = new JobClient();
+    Cluster mockCluster = mock(Cluster.class);
+    client.cluster = mockCluster;
+    JobID id = new JobID("test",0);
+    
+    when(mockCluster.getJob(id)).thenReturn(null);
+    
+    TaskReport[] result = client.getMapTaskReports(id);
+    assertEquals(0, result.length);
+    
+    verify(mockCluster).getJob(id);
+  }
+  
+  @SuppressWarnings("deprecation")
+  @Test
+  public void testReduceTaskReportsWithNullJob() throws Exception {
+    JobClient client = new JobClient();
+    Cluster mockCluster = mock(Cluster.class);
+    client.cluster = mockCluster;
+    JobID id = new JobID("test",0);
+    
+    when(mockCluster.getJob(id)).thenReturn(null);
+    
+    TaskReport[] result = client.getReduceTaskReports(id);
+    assertEquals(0, result.length);
+    
+    verify(mockCluster).getJob(id);
+  }
+  
+  @SuppressWarnings("deprecation")
+  @Test
+  public void testSetupTaskReportsWithNullJob() throws Exception {
+    JobClient client = new JobClient();
+    Cluster mockCluster = mock(Cluster.class);
+    client.cluster = mockCluster;
+    JobID id = new JobID("test",0);
+    
+    when(mockCluster.getJob(id)).thenReturn(null);
+    
+    TaskReport[] result = client.getSetupTaskReports(id);
+    assertEquals(0, result.length);
+    
+    verify(mockCluster).getJob(id);
+  }
+  
+  @SuppressWarnings("deprecation")
+  @Test
+  public void testCleanupTaskReportsWithNullJob() throws Exception {
+    JobClient client = new JobClient();
+    Cluster mockCluster = mock(Cluster.class);
+    client.cluster = mockCluster;
+    JobID id = new JobID("test",0);
+    
+    when(mockCluster.getJob(id)).thenReturn(null);
+    
+    TaskReport[] result = client.getCleanupTaskReports(id);
+    assertEquals(0, result.length);
+    
+    verify(mockCluster).getJob(id);
+  }
+}