浏览代码

MAPREDUCE-2903. Map Tasks graph is throwing XML Parse error when Job is executed with 0 maps. Contributed by Devaraj K.

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/branches/branch-1@1358939 13f79535-47bb-0310-9956-ffa450edef68
Matthew Foley 13 年之前
父节点
当前提交
460c64dc09

+ 3 - 0
CHANGES.txt

@@ -340,6 +340,9 @@ Release 1.1.0 - unreleased
     HADOOP-8445. Token should not print the password in toString
     (Ravi Prakash via tgraves)
 
+    MAPREDUCE-2903. Map Tasks graph is throwing XML Parse error when Job is 
+    executed with 0 maps. (Devaraj K via mattf)
+
 Release 1.0.3 - 2012.05.07
 
   NEW FEATURES

+ 2 - 2
src/mapred/org/apache/hadoop/mapred/TaskGraphServlet.java

@@ -72,13 +72,13 @@ public class TaskGraphServlet extends HttpServlet {
     final boolean isMap = "map".equalsIgnoreCase(request.getParameter("type"));
     final TaskReport[] reports = isMap? tracker.getMapTaskReports(jobId) 
                                       : tracker.getReduceTaskReports(jobId);
-    if(reports == null || reports.length == 0) {
+    if(reports == null) {
       return;
     }
 
     final int numTasks = reports.length;     
     int tasksPerBar = (int)Math.ceil(numTasks / 600d);
-    int numBars = (int) Math.ceil((double)numTasks / tasksPerBar);
+    int numBars = (numTasks==0)?600:(int) Math.ceil((double)numTasks / tasksPerBar);
     int w = Math.max(600, numBars);
     int barWidth = Math.min(10,  w / numBars); //min 1px, max 10px
     int barsPerNotch = (int)Math.ceil(10d / barWidth);

+ 68 - 0
src/test/org/apache/hadoop/mapred/TestTaskGraphServlet.java

@@ -0,0 +1,68 @@
+/**
+ * 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 java.io.PrintWriter;
+
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.junit.Test;
+import org.mockito.Mockito;
+
+public class TestTaskGraphServlet {
+  @Test
+  public void testTaskGraphServletShouldNotReturnEmptyContForNotasks()
+      throws Exception {
+    String jobId = "job_201108291216_0002";
+    HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
+    Mockito.doReturn(jobId).when(request).getParameter("jobid");
+    Mockito.doReturn("map").when(request).getParameter("type");
+
+    JobTracker jobTracker = Mockito.mock(JobTracker.class);
+    Mockito.doReturn(new TaskReport[0]).when(jobTracker).getMapTaskReports(
+        JobID.forName(jobId));
+
+    ServletContext servletContext = Mockito.mock(ServletContext.class);
+    Mockito.doReturn(jobTracker).when(servletContext).getAttribute(
+        "job.tracker");
+
+    HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
+    PrintWriter printWriter = Mockito.mock(PrintWriter.class);
+    Mockito.doReturn(printWriter).when(response).getWriter();
+
+    TaskGraphServlet taskGraphServlet = getTaskGraphServlet(servletContext);
+
+    taskGraphServlet.doGet(request, response);
+    Mockito.verify(printWriter, Mockito.atLeastOnce()).print("</svg>");
+  }
+
+  private TaskGraphServlet getTaskGraphServlet(
+      final ServletContext servletContext) {
+    TaskGraphServlet taskGraphServlet = new TaskGraphServlet() {
+      private static final long serialVersionUID = 1L;
+
+      @Override
+      public ServletContext getServletContext() {
+        return servletContext;
+      }
+    };
+    return taskGraphServlet;
+  }
+}