aboutsummaryrefslogtreecommitdiff
path: root/core/src/main/scala/org/apache/spark/ui/jobs/StagePage.scala
blob: 08107a3f62232646523ef9acc7024930fe13dff6 (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
 * 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.spark.ui.jobs

import java.util.Date

import javax.servlet.http.HttpServletRequest

import scala.xml.Node

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

/** 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] = {
    listener.synchronized {
      val stageId = request.getParameter("id").toInt
      val now = System.currentTimeMillis()

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

      val tasks = listener.stageIdToTaskInfos(stageId).toSeq.sortBy(_._1.launchTime)

      val numCompleted = tasks.count(_._1.finished)
      val shuffleReadBytes = listener.stageIdToShuffleRead.getOrElse(stageId, 0L)
      val hasShuffleRead = shuffleReadBytes > 0
      val shuffleWriteBytes = listener.stageIdToShuffleWrite.getOrElse(stageId, 0L)
      val hasShuffleWrite = shuffleWriteBytes > 0
      val memoryBytesSpilled = listener.stageIdToMemoryBytesSpilled.getOrElse(stageId, 0L)
      val diskBytesSpilled = listener.stageIdToDiskBytesSpilled.getOrElse(stageId, 0L)
      val hasBytesSpilled = (memoryBytesSpilled > 0 && diskBytesSpilled > 0)

      var activeTime = 0L
      listener.stageIdToTasksActive(stageId).foreach(activeTime += _.timeRunning(now))

      val finishedTasks = listener.stageIdToTaskInfos(stageId).filter(_._1.finished)
      // scalastyle:off
      val summary =
        <div>
          <ul class="unstyled">
            <li>
              <strong>Total task time across all tasks: </strong>
              {parent.formatDuration(listener.stageIdToTime.getOrElse(stageId, 0L) + activeTime)}
            </li>
            {if (hasShuffleRead)
              <li>
                <strong>Shuffle read: </strong>
                {Utils.bytesToString(shuffleReadBytes)}
              </li>
            }
            {if (hasShuffleWrite)
              <li>
                <strong>Shuffle write: </strong>
                {Utils.bytesToString(shuffleWriteBytes)}
              </li>
            }
            {if (hasBytesSpilled)
            <li>
              <strong>Shuffle spill (memory): </strong>
              {Utils.bytesToString(memoryBytesSpilled)}
            </li>
            <li>
              <strong>Shuffle spill (disk): </strong>
              {Utils.bytesToString(diskBytesSpilled)}
            </li>
            }
          </ul>
        </div>
        // scalastyle:on
      val taskHeaders: Seq[String] =
        Seq("Task Index", "Task ID", "Status", "Locality Level", "Executor", "Launch Time") ++
        Seq("Duration", "GC Time", "Result Ser Time") ++
        {if (hasShuffleRead) Seq("Shuffle Read")  else Nil} ++
        {if (hasShuffleWrite) Seq("Write Time", "Shuffle Write") else Nil} ++
        {if (hasBytesSpilled) Seq("Shuffle Spill (Memory)", "Shuffle Spill (Disk)") else Nil} ++
        Seq("Errors")

      val taskTable = listingTable(
        taskHeaders, taskRow(hasShuffleRead, hasShuffleWrite, hasBytesSpilled), tasks)

      // Excludes tasks which failed and have incomplete metrics
      val validTasks = tasks.filter(t => t._1.status == "SUCCESS" && (t._2.isDefined))

      val summaryTable: Option[Seq[Node]] =
        if (validTasks.size == 0) {
          None
        }
        else {
          val serializationTimes = validTasks.map{case (info, metrics, exception) =>
            metrics.get.resultSerializationTime.toDouble}
          val serializationQuantiles =
            "Result serialization time" +: Distribution(serializationTimes).
              get.getQuantiles().map(ms => parent.formatDuration(ms.toLong))

          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))

          val gettingResultTimes = validTasks.map{case (info, metrics, exception) =>
            if (info.gettingResultTime > 0) {
              (info.finishTime - info.gettingResultTime).toDouble
            } else {
              0.0
            }
          }
          val gettingResultQuantiles = ("Time spent fetching task results" +:
            Distribution(gettingResultTimes).get.getQuantiles().map(
              millis => parent.formatDuration(millis.toLong)))
          // The scheduler delay includes the network delay to send the task to the worker
          // machine and to send back the result (but not the time to fetch the task result,
          // if it needed to be fetched from the block manager on the worker).
          val schedulerDelays = validTasks.map{case (info, metrics, exception) =>
            val totalExecutionTime = {
              if (info.gettingResultTime > 0) {
                (info.gettingResultTime - info.launchTime).toDouble
              } else {
                (info.finishTime - info.launchTime).toDouble
              }
            }
            totalExecutionTime - metrics.get.executorRunTime
          }
          val schedulerDelayQuantiles = ("Scheduler delay" +:
            Distribution(schedulerDelays).get.getQuantiles().map(
              millis => parent.formatDuration(millis.toLong)))

          def getQuantileCols(data: Seq[Double]) =
            Distribution(data).get.getQuantiles().map(d => Utils.bytesToString(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 memoryBytesSpilledSizes = validTasks.map {
            case(info, metrics, exception) =>
              metrics.get.memoryBytesSpilled.toDouble
          }
          val memoryBytesSpilledQuantiles = "Shuffle spill (memory)" +:
            getQuantileCols(memoryBytesSpilledSizes)

          val diskBytesSpilledSizes = validTasks.map {
            case(info, metrics, exception) =>
              metrics.get.diskBytesSpilled.toDouble
          }
          val diskBytesSpilledQuantiles = "Shuffle spill (disk)" +:
            getQuantileCols(diskBytesSpilledSizes)

          val listings: Seq[Seq[String]] = Seq(
            serializationQuantiles,
            serviceQuantiles,
            gettingResultQuantiles,
            schedulerDelayQuantiles,
            if (hasShuffleRead) shuffleReadQuantiles else Nil,
            if (hasShuffleWrite) shuffleWriteQuantiles else Nil,
            if (hasBytesSpilled) memoryBytesSpilledQuantiles else Nil,
            if (hasBytesSpilled) diskBytesSpilledQuantiles else Nil)

          val quantileHeaders = Seq("Metric", "Min", "25th percentile",
            "Median", "75th percentile", "Max")
          def quantileRow(data: Seq[String]): Seq[Node] = <tr> {data.map(d => <td>{d}</td>)} </tr>
          Some(listingTable(quantileHeaders, quantileRow, listings, fixedWidth = true))
        }
      val executorTable = new ExecutorTable(parent, stageId)
      val content =
        summary ++
        <h4>Summary Metrics for {numCompleted} Completed Tasks</h4> ++
        <div>{summaryTable.getOrElse("No tasks have reported metrics yet.")}</div> ++
        <h4>Aggregated Metrics by Executor</h4> ++ executorTable.toNodeSeq() ++
        <h4>Tasks</h4> ++ taskTable

      headerSparkPage(content, parent.sc, "Details for Stage %d".format(stageId), Stages)
    }
  }

  def taskRow(shuffleRead: Boolean, shuffleWrite: Boolean, bytesSpilled: Boolean)
             (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("")
    val gcTime = metrics.map(m => m.jvmGCTime).getOrElse(0L)
    val serializationTime = metrics.map(m => m.resultSerializationTime).getOrElse(0L)

    val maybeShuffleRead = metrics.flatMap{m => m.shuffleReadMetrics}.map{s => s.remoteBytesRead}
    val shuffleReadSortable = maybeShuffleRead.map(_.toString).getOrElse("")
    val shuffleReadReadable = maybeShuffleRead.map{Utils.bytesToString(_)}.getOrElse("")

    val maybeShuffleWrite =
      metrics.flatMap{m => m.shuffleWriteMetrics}.map{s => s.shuffleBytesWritten}
    val shuffleWriteSortable = maybeShuffleWrite.map(_.toString).getOrElse("")
    val shuffleWriteReadable = maybeShuffleWrite.map{Utils.bytesToString(_)}.getOrElse("")

    val maybeWriteTime = metrics.flatMap{m => m.shuffleWriteMetrics}.map{s => s.shuffleWriteTime}
    val writeTimeSortable = maybeWriteTime.map(_.toString).getOrElse("")
    val writeTimeReadable = maybeWriteTime.map{ t => t / (1000 * 1000)}.map{ ms =>
      if (ms == 0) "" else parent.formatDuration(ms)}.getOrElse("")

    val maybeMemoryBytesSpilled = metrics.map{m => m.memoryBytesSpilled}
    val memoryBytesSpilledSortable = maybeMemoryBytesSpilled.map(_.toString).getOrElse("")
    val memoryBytesSpilledReadable = maybeMemoryBytesSpilled.map{Utils.bytesToString(_)}
      .getOrElse("")

    val maybeDiskBytesSpilled = metrics.map{m => m.diskBytesSpilled}
    val diskBytesSpilledSortable = maybeDiskBytesSpilled.map(_.toString).getOrElse("")
    val diskBytesSpilledReadable = maybeDiskBytesSpilled.map{Utils.bytesToString(_)}.getOrElse("")

    <tr>
      <td>{info.index}</td>
      <td>{info.taskId}</td>
      <td>{info.status}</td>
      <td>{info.taskLocality}</td>
      <td>{info.host}</td>
      <td>{dateFmt.format(new Date(info.launchTime))}</td>
      <td sorttable_customkey={duration.toString}>
        {formatDuration}
      </td>
      <td sorttable_customkey={gcTime.toString}>
        {if (gcTime > 0) parent.formatDuration(gcTime) else ""}
      </td>
      <td sorttable_customkey={serializationTime.toString}>
        {if (serializationTime > 0) parent.formatDuration(serializationTime) else ""}
      </td>
      {if (shuffleRead) {
         <td sorttable_customkey={shuffleReadSortable}>
           {shuffleReadReadable}
         </td>
      }}
      {if (shuffleWrite) {
         <td sorttable_customkey={writeTimeSortable}>
           {writeTimeReadable}
         </td>
         <td sorttable_customkey={shuffleWriteSortable}>
           {shuffleWriteReadable}
         </td>
      }}
      {if (bytesSpilled) {
        <td sorttable_customkey={memoryBytesSpilledSortable}>
          {memoryBytesSpilledReadable}
        </td>
        <td sorttable_customkey={diskBytesSpilledSortable}>
          {diskBytesSpilledReadable}
        </td>
      }}
      <td>{exception.map(e =>
        <span>
          {e.className} ({e.description})<br/>
          {fmtStackTrace(e.stackTrace)}
        </span>).getOrElse("")}
      </td>
    </tr>
  }
}