Github user andrewor14 commented on a diff in the pull request:

    https://github.com/apache/spark/pull/7774#discussion_r36039885
  
    --- 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 = {
    --- End diff --
    
    (also other methods in this class)


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to