Github user andrewor14 commented on a diff in the pull request:
https://github.com/apache/spark/pull/7774#discussion_r36039772
--- Diff:
sql/core/src/main/scala/org/apache/spark/sql/ui/SQLSparkListener.scala ---
@@ -0,0 +1,281 @@
+/*
+ * 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.sql.ui
+
+import scala.collection.mutable
+
+import org.apache.spark.{AccumulatorParam, JobExecutionStatus}
+import org.apache.spark.executor.TaskMetrics
+import org.apache.spark.scheduler._
+import org.apache.spark.sql.{DataFrame, SQLContext}
+import org.apache.spark.sql.execution.SparkSQLExecution
+
+private[sql] class SQLSparkListener(sqlContext: SQLContext) extends
SparkListener {
+
+ private val retainedExecutions =
+ sqlContext.sparkContext.conf.getInt("spark.sql.ui.retainedExecutions",
1000)
+
+ private val activeExecutions = mutable.HashMap[Long,
SparkSQLExecutionUIData]()
+
+ // Old data in the following fields must be removed in
"trimExecutionsIfNecessary".
+ // If adding new fields, make sure "trimExecutionsIfNecessary" can clean
up old data
+ private val executionIdToData = mutable.HashMap[Long,
SparkSQLExecutionUIData]()
+
+ /**
+ * Maintain the relation between job id and execution id so that we can
get the execution id in
+ * the "onJobEnd" method.
+ */
+ private val jobIdToExecutionId = mutable.HashMap[Long, Long]()
+
+ private val stageIdToStageMetrics = mutable.HashMap[Long,
SQLStageMetrics]()
+
+ private val failedExecutions =
mutable.ListBuffer[SparkSQLExecutionUIData]()
+
+ private val completedExecutions =
mutable.ListBuffer[SparkSQLExecutionUIData]()
+
+ private def trimExecutionsIfNecessary(
+ executions: mutable.ListBuffer[SparkSQLExecutionUIData]): Unit = {
+ if (executions.size > retainedExecutions) {
+ val toRemove = math.max(retainedExecutions / 10, 1)
+ executions.take(toRemove).foreach { execution =>
+ for (executionUIData <-
executionIdToData.remove(execution.executionId)) {
+ for (jobId <- executionUIData.jobs.keys) {
+ jobIdToExecutionId.remove(jobId)
+ }
+ for (stageId <- executionUIData.stages) {
+ stageIdToStageMetrics.remove(stageId)
+ }
+ }
+ }
+ executions.trimStart(toRemove)
+ }
+ }
+
+ override def onJobStart(jobStart: SparkListenerJobStart): Unit = {
+ val executionId =
jobStart.properties.getProperty(SparkSQLExecution.EXECUTION_ID_KEY)
+ if (executionId == null) {
+ // This is not a job created by SQL
+ return
+ }
+ val jobId = jobStart.jobId
+ val stageIds = jobStart.stageIds
+
+ synchronized {
+ activeExecutions.get(executionId.toLong).foreach { executionUIData =>
+ executionUIData.jobs(jobId) = JobExecutionStatus.RUNNING
+ executionUIData.stages ++= stageIds
+ // attemptId must be 0. Right?
+ stageIds.foreach(stageId =>
+ stageIdToStageMetrics(stageId) = SQLStageMetrics(stageAttemptId
= 0))
+ jobIdToExecutionId(jobId) = executionUIData.executionId
+ }
+ }
+ }
+
+ override def onJobEnd(jobEnd: SparkListenerJobEnd): Unit = synchronized {
+ val jobId = jobEnd.jobId
+ for (executionId <- jobIdToExecutionId.get(jobId);
+ executionUIData <- executionIdToData.get(executionId)) {
+ jobEnd.jobResult match {
+ case JobSucceeded => executionUIData.jobs(jobId) =
JobExecutionStatus.SUCCEEDED
+ case JobFailed(_) => executionUIData.jobs(jobId) =
JobExecutionStatus.FAILED
+ }
+ }
+ }
+
+ override def onExecutorMetricsUpdate(
+ executorMetricsUpdate: SparkListenerExecutorMetricsUpdate): Unit =
synchronized {
+ for ((taskId, stageId, stageAttemptID, metrics) <-
executorMetricsUpdate.taskMetrics) {
+ updateTaskMetrics(taskId, stageId, stageAttemptID, metrics, false)
+ }
+ }
+
+ override def onStageSubmitted(stageSubmitted:
SparkListenerStageSubmitted): Unit = synchronized {
+ val stageId = stageSubmitted.stageInfo.stageId
+ val stageAttemptId = stageSubmitted.stageInfo.attemptId
+ // Always override metrics for old stage attempt
+ stageIdToStageMetrics(stageId) = SQLStageMetrics(stageAttemptId)
+ }
+
+ override def onTaskEnd(taskEnd: SparkListenerTaskEnd): Unit =
synchronized {
+ updateTaskMetrics(
+ taskEnd.taskInfo.taskId, taskEnd.stageId, taskEnd.stageAttemptId,
taskEnd.taskMetrics, true)
+ }
+
+ private def updateTaskMetrics(taskId: Long, stageId: Int,
stageAttemptID: Int,
+ metrics: TaskMetrics, finishTask: Boolean): Unit = {
+ stageIdToStageMetrics.get(stageId) match {
+ case Some(stageMetrics) =>
+ if (stageAttemptID < stageMetrics.stageAttemptId) {
+ // A task of an old stage attempt. Because a new stage is
submitted, we can ignore it.
+ } else if (stageAttemptID > stageMetrics.stageAttemptId) {
+ // TODO A running task with a higher stageAttemptID??
+ } else {
+ // TODO We don't know the attemptId. Currently, what we can do
is overriding the
+ // accumulator updates. However, if there are two same task are
running, such as
+ // speculation, the accumulator updates will be overriding by
different task attempts,
+ // the results will be weird.
+ stageMetrics.taskIdToMetricUpdates.get(taskId) match {
+ case Some(taskMetrics) =>
+ if (finishTask) {
+ taskMetrics.finished = true
+ taskMetrics.accumulatorUpdates =
metrics.accumulatorUpdates()
+ } else if (!taskMetrics.finished){
+ // If a task is finished, we should not override with
accumulator updates from
+ // heartbeat reports
+ taskMetrics.accumulatorUpdates =
metrics.accumulatorUpdates()
+ }
+ case None =>
+ // TODO Now just set attemptId to 0. Should fix here when we
can get the attempt
+ // id from SparkListenerExecutorMetricsUpdate
+ stageMetrics.taskIdToMetricUpdates(taskId) =
+ SQLTaskMetrics(attemptId = 0, finished = finishTask,
metrics.accumulatorUpdates())
+ }
+ }
+ case None =>
+ // This execution and its stage have been dropped
+ }
+ }
+
+ def onExecutionStart(
+ executionId: Long, description: String, details: String, df:
DataFrame, time: Long): Unit = {
+ val physicalPlanDescription = df.queryExecution.toString
+ val physicalPlanGraph = SparkPlanGraph(df.queryExecution.executedPlan)
+ val metrics = physicalPlanGraph.nodes.flatMap { node =>
+ node.metrics.map(metric => metric.accumulatorId -> metric)
+ }
+
+ val executionUIData = SparkSQLExecutionUIData(executionId,
description, details,
+ physicalPlanDescription, physicalPlanGraph, metrics.toMap, time)
+
+ synchronized {
+ activeExecutions(executionId) = executionUIData
+ executionIdToData(executionId) = executionUIData
+ }
+ }
+
+ def onExecutionEnd(executionId: Long, time: Long): Unit = synchronized {
+ activeExecutions.remove(executionId).foreach { executionUIData =>
+ executionUIData.completionTime = Some(time)
+ if (executionUIData.isFailed) {
+ failedExecutions += executionUIData
+ trimExecutionsIfNecessary(failedExecutions)
+ } else {
+ completedExecutions += executionUIData
+ trimExecutionsIfNecessary(completedExecutions)
+ }
+ }
+ }
+
+ def getRunningExecutions: Seq[SparkSQLExecutionUIData] = synchronized {
+ activeExecutions.values.toSeq
+ }
+
+ def getFailedExecutions: Seq[SparkSQLExecutionUIData] = synchronized {
+ failedExecutions
+ }
+
+ def getCompletedExecutions: Seq[SparkSQLExecutionUIData] = synchronized {
+ completedExecutions
+ }
+
+ def getExecution(executionId: Long): Option[SparkSQLExecutionUIData] =
synchronized {
+ executionIdToData.get(executionId)
+ }
+
+ def getExecutionMetrics(executionId: Long): Map[Long, Any] =
synchronized {
+ executionIdToData.get(executionId) match {
+ case Some(executionUIData) =>
+ // Get all accumulator updates from all tasks which belong to this
execution and merge them
+ val accumulatorUpdates = {
+ for (stageId <- executionUIData.stages;
+ stageMetrics <-
stageIdToStageMetrics.get(stageId).toIterable;
+ taskMetrics <- stageMetrics.taskIdToMetricUpdates.values;
+ accumulatorUpdate <- taskMetrics.accumulatorUpdates.toSeq)
+ yield accumulatorUpdate
+ }
+ mergeAccumulatorUpdates(accumulatorUpdates, accumulatorId =>
+
executionUIData.accumulatorMetrics(accumulatorId).accumulatorParam)
+ case None =>
+ // This execution has been dropped
+ Map.empty
+ }
+ }
+
+ private def mergeAccumulatorUpdates(
+ accumulatorUpdates: Seq[(Long, Any)],
+ paramFunc: Long => AccumulatorParam[Any]): Map[Long, Any] = {
+ accumulatorUpdates.groupBy(_._1).map { case (accumulatorId, values) =>
+ val param = paramFunc(accumulatorId)
+ (accumulatorId, values.map(_._2).reduceLeft(param.addInPlace))
+ }
+ }
+
+}
+
+/**
+ * Represent all necessary data for an execution that will be used in Web
UI.
+ */
+private[ui] case class SparkSQLExecutionUIData(
--- End diff --
`SQLExecutionUIData` is OK
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]