aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/scala/spark/ui/jobs/StagePage.scala
blob: 292966f23a05a4cc884b4868b657dc383e88d4c7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
 * 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 spark.ui.jobs

import java.util.Date

import javax.servlet.http.HttpServletRequest

import scala.xml.Node

import spark.ui.UIUtils._
import spark.ui.Page._
import spark.util.Distribution
import spark.{ExceptionFailure, Utils}
import spark.scheduler.cluster.TaskInfo
import spark.executor.TaskMetrics

/** Page showing statistics and task list for a given stage */
private[spark] class StagePage(parent: JobProgressUI) {
  def listener = parent.listener
  val dateFmt = parent.dateFmt

  def render(request: HttpServletRequest): Seq[Node] = {
    val stageId = request.getParameter("id").toInt

    if (!listener.stageToTaskInfos.contains(stageId)) {
      val content =
        <div>
          <h2>Summary Metrics</h2> No tasks have finished yet
          <h2>Tasks</h2> No tasks have finished yet
        </div>
      return headerSparkPage(content, parent.sc, "Stage Details: %s".format(stageId), Jobs)
    }

    val tasks = listener.stageToTaskInfos(stageId)

    val shuffleRead = listener.hasShuffleRead(stageId)
    val shuffleWrite = listener.hasShuffleWrite(stageId)

    val taskHeaders: Seq[String] =
      Seq("Task ID", "Duration", "Locality Level", "Worker", "Launch Time") ++
        {if (shuffleRead) Seq("Shuffle Read")  else Nil} ++
        {if (shuffleWrite) Seq("Shuffle Write") else Nil} ++
      Seq("Details")

    val taskTable = listingTable(taskHeaders, taskRow, tasks)

    // Excludes tasks which failed and have incomplete metrics
    val validTasks = tasks.filter(t => Option(t._2).isDefined)

    val summaryTable: Option[Seq[Node]] =
      if (validTasks.size == 0) {
        None
      }
      else {
        val serviceTimes = validTasks.map{case (info, metrics, exception) =>
          metrics.get.executorRunTime.toDouble}
        val serviceQuantiles = "Duration" +: Distribution(serviceTimes).get.getQuantiles().map(
          ms => parent.formatDuration(ms.toLong))

        def getQuantileCols(data: Seq[Double]) =
          Distribution(data).get.getQuantiles().map(d => Utils.memoryBytesToString(d.toLong))

        val shuffleReadSizes = validTasks.map {
          case(info, metrics, exception) =>
            metrics.get.shuffleReadMetrics.map(_.remoteBytesRead).getOrElse(0L).toDouble
        }
        val shuffleReadQuantiles = "Shuffle Read (Remote)" +: getQuantileCols(shuffleReadSizes)

        val shuffleWriteSizes = validTasks.map {
          case(info, metrics, exception) =>
            metrics.get.shuffleWriteMetrics.map(_.shuffleBytesWritten).getOrElse(0L).toDouble
        }
        val shuffleWriteQuantiles = "Shuffle Write" +: getQuantileCols(shuffleWriteSizes)

        val listings: Seq[Seq[String]] = Seq(serviceQuantiles,
          if (shuffleRead) shuffleReadQuantiles else Nil,
          if (shuffleWrite) shuffleWriteQuantiles else Nil)

        val quantileHeaders = Seq("Metric", "Min", "25%", "50%", "75%", "Max")
        def quantileRow(data: Seq[String]): Seq[Node] = <tr> {data.map(d => <td>{d}</td>)} </tr>
        Some(listingTable(quantileHeaders, quantileRow, listings))
      }

    val content =
      <h2>Summary Metrics</h2> ++ summaryTable.getOrElse(Nil) ++ <h2>Tasks</h2> ++ taskTable;

    headerSparkPage(content, parent.sc, "Stage Details: %s".format(stageId), Jobs)
  }


  def taskRow(taskData: (TaskInfo, Option[TaskMetrics], Option[ExceptionFailure])): Seq[Node] = {
    def fmtStackTrace(trace: Seq[StackTraceElement]): Seq[Node] =
      trace.map(e => <span style="display:block;">{e.toString}</span>)
    val (info, metrics, exception) = taskData
    <tr>
      <td>{info.taskId}</td>
      <td sorttable_customkey={metrics.map{m => m.executorRunTime.toString}.getOrElse("1")}>
        {metrics.map{m => parent.formatDuration(m.executorRunTime)}.getOrElse("")}
      </td>
      <td>{info.taskLocality}</td>
      <td>{info.hostPort}</td>
      <td>{dateFmt.format(new Date(info.launchTime))}</td>
      {metrics.flatMap{m => m.shuffleReadMetrics}.map{s =>
        <td>{Utils.memoryBytesToString(s.remoteBytesRead)}</td>}.getOrElse("")}
      {metrics.flatMap{m => m.shuffleWriteMetrics}.map{s =>
        <td>{Utils.memoryBytesToString(s.shuffleBytesWritten)}</td>}.getOrElse("")}
      <td>{exception.map(e =>
        <span>
          {e.className} ({e.description})<br/>
          {fmtStackTrace(e.stackTrace)}
        </span>).getOrElse("")}
      </td>
    </tr>
  }
}