From 0aaaeea268a1808a41f9209186db5d441895d7db Mon Sep 17 00:00:00 2001 From: Karthik Kambatla Date: Wed, 7 Dec 2016 13:36:49 -0800 Subject: [PATCH] MAPREDUCE-6817. The format of job start time in JHS is different from those of submit and finish time. (Haibo Chen via kasha) (cherry picked from commit a793cec2c9a7ad80aaf67be9dec2245e8ecce63c) --- .../mapreduce/v2/hs/webapp/HsJobsBlock.java | 2 +- .../mapreduce/v2/hs/webapp/dao/JobInfo.java | 15 +++++++++- .../v2/hs/webapp/dao/TestJobInfo.java | 29 ++++++++++++++++++- 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-hs/src/main/java/org/apache/hadoop/mapreduce/v2/hs/webapp/HsJobsBlock.java b/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-hs/src/main/java/org/apache/hadoop/mapreduce/v2/hs/webapp/HsJobsBlock.java index 92c5e2b9559..b234ca3df03 100644 --- a/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-hs/src/main/java/org/apache/hadoop/mapreduce/v2/hs/webapp/HsJobsBlock.java +++ b/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-hs/src/main/java/org/apache/hadoop/mapreduce/v2/hs/webapp/HsJobsBlock.java @@ -79,7 +79,7 @@ public class HsJobsBlock extends HtmlBlock { JobInfo job = new JobInfo(j); jobsTableData.append("[\"") .append(dateFormat.format(new Date(job.getSubmitTime()))).append("\",\"") - .append(job.getStartTimeStr()).append("\",\"") + .append(job.getFormattedStartTimeStr(dateFormat)).append("\",\"") .append(dateFormat.format(new Date(job.getFinishTime()))).append("\",\"") .append("") .append(job.getId()).append("\",\"") diff --git a/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-hs/src/main/java/org/apache/hadoop/mapreduce/v2/hs/webapp/dao/JobInfo.java b/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-hs/src/main/java/org/apache/hadoop/mapreduce/v2/hs/webapp/dao/JobInfo.java index eddbccf8851..d12a7295e6a 100644 --- a/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-hs/src/main/java/org/apache/hadoop/mapreduce/v2/hs/webapp/dao/JobInfo.java +++ b/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-hs/src/main/java/org/apache/hadoop/mapreduce/v2/hs/webapp/dao/JobInfo.java @@ -17,6 +17,7 @@ */ package org.apache.hadoop.mapreduce.v2.hs.webapp.dao; +import java.text.DateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; @@ -27,6 +28,7 @@ import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlTransient; +import com.google.common.annotations.VisibleForTesting; import org.apache.hadoop.mapreduce.JobACL; import org.apache.hadoop.mapreduce.v2.api.records.JobReport; import org.apache.hadoop.mapreduce.v2.api.records.TaskAttemptId; @@ -44,7 +46,8 @@ import org.apache.hadoop.security.authorize.AccessControlList; @XmlRootElement(name = "job") @XmlAccessorType(XmlAccessType.FIELD) public class JobInfo { - private static final String NA = "N/A"; + @VisibleForTesting + static final String NA = "N/A"; protected long submitTime; protected long startTime; @@ -228,6 +231,16 @@ public class JobInfo { return this.startTime; } + public String getFormattedStartTimeStr(final DateFormat dateFormat) { + String str = NA; + + if (startTime >= 0) { + str = dateFormat.format(new Date(startTime)); + } + + return str; + } + public String getStartTimeStr() { String str = NA; diff --git a/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-hs/src/test/java/org/apache/hadoop/mapreduce/v2/hs/webapp/dao/TestJobInfo.java b/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-hs/src/test/java/org/apache/hadoop/mapreduce/v2/hs/webapp/dao/TestJobInfo.java index 51f87e06a1e..c8b749f79c0 100644 --- a/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-hs/src/test/java/org/apache/hadoop/mapreduce/v2/hs/webapp/dao/TestJobInfo.java +++ b/hadoop-mapreduce-project/hadoop-mapreduce-client/hadoop-mapreduce-client-hs/src/test/java/org/apache/hadoop/mapreduce/v2/hs/webapp/dao/TestJobInfo.java @@ -22,6 +22,8 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import java.io.IOException; +import java.text.DateFormat; +import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; @@ -153,7 +155,7 @@ public class TestJobInfo { when(job.getID()).thenReturn(jobId); JobInfo jobInfo = new JobInfo(job); - Assert.assertEquals("N/A", jobInfo.getStartTimeStr()); + Assert.assertEquals(JobInfo.NA, jobInfo.getStartTimeStr()); Date date = new Date(); when(jobReport.getStartTime()).thenReturn(date.getTime()); @@ -161,4 +163,29 @@ public class TestJobInfo { jobInfo = new JobInfo(job); Assert.assertEquals(date.toString(), jobInfo.getStartTimeStr()); } + + @Test + public void testGetFormattedStartTimeStr() { + JobReport jobReport = mock(JobReport.class); + when(jobReport.getStartTime()).thenReturn(-1L); + + Job job = mock(Job.class); + when(job.getReport()).thenReturn(jobReport); + when(job.getName()).thenReturn("TestJobInfo"); + when(job.getState()).thenReturn(JobState.SUCCEEDED); + + JobId jobId = MRBuilderUtils.newJobId(1L, 1, 1); + when(job.getID()).thenReturn(jobId); + DateFormat dateFormat = new SimpleDateFormat(); + + JobInfo jobInfo = new JobInfo(job); + Assert.assertEquals( + JobInfo.NA, jobInfo.getFormattedStartTimeStr(dateFormat)); + + Date date = new Date(); + when(jobReport.getStartTime()).thenReturn(date.getTime()); + jobInfo = new JobInfo(job); + Assert.assertEquals( + dateFormat.format(date), jobInfo.getFormattedStartTimeStr(dateFormat)); + } }