aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/scala/spark/ui/jobs/StagePage.scala
blob: e327cb3947889c3d67105f9743f380f0f7318f4c (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
 * 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
    val now = System.currentTimeMillis()

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

    val tasks = listener.stageToTaskInfos(stageId)

    val shuffleRead = listener.stageToShuffleRead(stageId) > 0
    val shuffleWrite = listener.stageToShuffleWrite(stageId) > 0

    var activeTime = 0L
    listener.stageToTasksActive(stageId).foreach { t =>
      activeTime += t.timeRunning(now)
    }

    val summary =
      <div>
        <ul class="unstyled">
          <li>
            <strong>CPU time: </strong>
            {parent.formatDuration(listener.stageToTime(stageId) + activeTime)}
          </li>
          {if (shuffleRead)
            <li>
              <strong>Shuffle read: </strong>
              {Utils.memoryBytesToString(listener.stageToShuffleRead(stageId))}
            </li>
          }
          {if (shuffleWrite)
            <li>
              <strong>Shuffle write: </strong>
              {Utils.memoryBytesToString(listener.stageToShuffleWrite(stageId))}
            </li>
          }
        </ul>
      </div>

    val taskHeaders: Seq[String] =
      Seq("Task ID", "Status", "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 => t._1.status == "SUCCESS" && (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 =
      summary ++ <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

    val duration = if (info.status == "RUNNING") info.timeRunning(System.currentTimeMillis())
      else metrics.map(m => m.executorRunTime).getOrElse(1)
    val formatDuration = if (info.status == "RUNNING") parent.formatDuration(duration)
      else metrics.map(m => parent.formatDuration(m.executorRunTime)).getOrElse("")

    <tr>
      <td>{info.taskId}</td>
      <td>{info.status}</td>
      <td sorttable_customkey={duration.toString}>
        {formatDuration}
      </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>
  }
}