spark-reviews mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From andrewor14 <...@git.apache.org>
Subject [GitHub] spark pull request: [SPARK-8861][SPARK-8862][SQL] Add basic instru...
Date Mon, 03 Aug 2015 21:20:49 GMT
Github user andrewor14 commented on a diff in the pull request:

    https://github.com/apache/spark/pull/7774#discussion_r36133254
  
    --- Diff: sql/core/src/main/scala/org/apache/spark/sql/ui/SQLListener.scala ---
    @@ -0,0 +1,340 @@
    +/*
    + * 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.SQLExecution
    +
    +private[sql] class SQLListener(sqlContext: SQLContext) extends SparkListener {
    +
    +  private val retainedExecutions =
    +    sqlContext.sparkContext.conf.getInt("spark.sql.ui.retainedExecutions", 1000)
    +
    +  private val activeExecutions = mutable.HashMap[Long, SQLExecutionUIData]()
    +
    +  // Old data in the following fields must be removed in "trimExecutionsIfNecessary".
    +  // If adding new fields, make sure "trimExecutionsIfNecessary" can clean up old data
    +
    +  // VisibleForTesting
    +  val executionIdToData = mutable.HashMap[Long, SQLExecutionUIData]()
    +
    +  /**
    +   * 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[SQLExecutionUIData]()
    +
    +  private val completedExecutions = mutable.ListBuffer[SQLExecutionUIData]()
    +
    +  // VisibleForTesting
    +  def executionIdToDataSize: Int = synchronized {
    +    executionIdToData.size
    +  }
    +
    +  // VisibleForTesting
    +  def jobIdToExecutionIdSize: Int = synchronized {
    +    jobIdToExecutionId.size
    +  }
    +
    +  // VisibleForTesting
    +  def stageIdToStageMetricsSize: Int = synchronized {
    +    stageIdToStageMetrics.size
    +  }
    +
    +  private def trimExecutionsIfNecessary(
    +      executions: mutable.ListBuffer[SQLExecutionUIData]): 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(SQLExecution.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
    +      }
    +      if (executionUIData.completionTime.nonEmpty && !executionUIData.hasRunningJobs)
{
    +        // onExecutionEnd happens before this onJobEnd and we are the last job, so we
should update
    +        // the execution lists.
    +        updateExecutionLists(executionId)
    +      }
    +    }
    +  }
    +
    +  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 = {
    +    if (metrics == null) {
    +      return
    +    }
    +
    +    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 = SQLExecutionUIData(executionId, description, details,
    +      physicalPlanDescription, physicalPlanGraph, metrics.toMap, time)
    +
    +    synchronized {
    +      activeExecutions(executionId) = executionUIData
    +      executionIdToData(executionId) = executionUIData
    +    }
    +  }
    +
    +  def onExecutionEnd(executionId: Long, time: Long): Unit = synchronized {
    +    executionIdToData.get(executionId).foreach { executionUIData =>
    +      executionUIData.completionTime = Some(time)
    +      if (!executionUIData.hasRunningJobs) {
    +        // onExecutionEnd happens after all "onJobEnd"s
    +        // So we should update the execution lists.
    +        updateExecutionLists(executionId)
    +      } else {
    +        // There are some running jobs, onExecutionEnd happens before some "onJobEnd"s.
    +        // Then we don't if the execution is successful, so let the last onJobEnd updates
the
    +        // execution lists.
    +      }
    +    }
    +  }
    +
    +  private def updateExecutionLists(executionId: Long): Unit = {
    +    activeExecutions.remove(executionId).foreach { executionUIData =>
    +      if (executionUIData.isFailed) {
    +        failedExecutions += executionUIData
    +        trimExecutionsIfNecessary(failedExecutions)
    +      } else {
    +        completedExecutions += executionUIData
    +        trimExecutionsIfNecessary(completedExecutions)
    +      }
    +    }
    +  }
    +
    +  def getRunningExecutions: Seq[SQLExecutionUIData] = synchronized {
    +    activeExecutions.values.toSeq
    +  }
    +
    +  def getFailedExecutions: Seq[SQLExecutionUIData] = synchronized {
    +    failedExecutions
    +  }
    +
    +  def getCompletedExecutions: Seq[SQLExecutionUIData] = synchronized {
    --- End diff --
    
    are these `VisibleForTesting` as well?


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

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


Mime
View raw message