Browse Source

MAPREDUCE-6762. ControlledJob#toString failed with NPE when job status is not successfully updated (Weiwei Yang via Varun Saxena)

Varun Saxena 8 years ago
parent
commit
d37b45d613

+ 1 - 1
hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/main/java/org/apache/hadoop/mapreduce/Job.java

@@ -422,7 +422,7 @@ public class Job extends JobContextImpl implements JobContext {
    * The user-specified job name.
    */
   public String getJobName() {
-    if (state == JobState.DEFINE) {
+    if (state == JobState.DEFINE || status == null) {
       return super.getJobName();
     }
     ensureState(JobState.RUNNING);

+ 36 - 0
hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-core/src/test/java/org/apache/hadoop/mapreduce/TestJob.java

@@ -26,6 +26,7 @@ import java.io.IOException;
 import org.apache.hadoop.io.Text;
 import org.apache.hadoop.mapred.JobConf;
 import org.apache.hadoop.mapreduce.JobStatus.State;
+import org.apache.hadoop.mapreduce.lib.jobcontrol.ControlledJob;
 import org.apache.hadoop.mapreduce.protocol.ClientProtocol;
 import org.apache.hadoop.security.Credentials;
 import org.apache.hadoop.security.UserGroupInformation;
@@ -54,6 +55,41 @@ public class TestJob {
     Assert.assertNotNull(job.toString());
   }
 
+  @Test
+  public void testUnexpectedJobStatus() throws Exception {
+    Cluster cluster = mock(Cluster.class);
+    JobID jobid = new JobID("1014873536921", 6);
+    ClientProtocol clientProtocol = mock(ClientProtocol.class);
+    when(cluster.getClient()).thenReturn(clientProtocol);
+    JobStatus status = new JobStatus(jobid, 0f, 0f, 0f, 0f,
+        State.RUNNING, JobPriority.DEFAULT, "root",
+        "testUnexpectedJobStatus", "job file", "tracking URL");
+    when(clientProtocol.getJobStatus(jobid)).thenReturn(status);
+    Job job = Job.getInstance(cluster, status, new JobConf());
+
+    // ensurer job status is RUNNING
+    Assert.assertNotNull(job.getStatus());
+    Assert.assertTrue(job.getStatus().getState() == State.RUNNING);
+
+    // when updating job status, job client could not retrieve
+    // job status, and status reset to null
+    when(clientProtocol.getJobStatus(jobid)).thenReturn(null);
+
+    try {
+      job.updateStatus();
+    } catch (IOException e) {
+      Assert.assertTrue(e != null
+          && e.getMessage().contains("Job status not available"));
+    }
+
+    try {
+      ControlledJob cj = new ControlledJob(job, null);
+      Assert.assertNotNull(cj.toString());
+    } catch (NullPointerException e) {
+      Assert.fail("job API fails with NPE");
+    }
+  }
+
   @Test
   public void testUGICredentialsPropogation() throws Exception {
     Credentials creds = new Credentials();