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

    https://github.com/apache/spark/pull/7774#discussion_r36125578
  
    --- Diff: 
sql/core/src/main/scala/org/apache/spark/sql/execution/SQLExecution.scala ---
    @@ -0,0 +1,93 @@
    +/*
    + * 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.execution
    +
    +import java.util.concurrent.atomic.AtomicLong
    +
    +import org.apache.spark.SparkContext
    +import org.apache.spark.sql.{DataFrame, SQLContext}
    +import org.apache.spark.util.Utils
    +
    +private[sql] object SQLExecution {
    +
    +  val EXECUTION_ID_KEY = "spark.sql.execution.id"
    +
    +  private val _nextExecutionId = new AtomicLong(0)
    +
    +  private def nextExecutionId: Long = _nextExecutionId.getAndIncrement
    +
    +  /**
    +   * Wrap a DataFrame action to track all Spark jobs in the body so that 
we can connect them with
    +   * an execution.
    +   */
    +  def withNewExecutionId[T](sqlContext: SQLContext, df: DataFrame)(body: 
=> T): T = {
    +    val sc = sqlContext.sparkContext
    +    val oldExecutionId = sc.getLocalProperty(EXECUTION_ID_KEY)
    +    if (oldExecutionId == null) {
    +      val executionId = SQLExecution.nextExecutionId
    +      sc.setLocalProperty(EXECUTION_ID_KEY, executionId.toString)
    +      val r = try {
    +        val callSite = Utils.getCallSite()
    +        sqlContext.listener.onExecutionStart(
    +          executionId, callSite.shortForm, callSite.longForm, df, 
System.currentTimeMillis())
    +        try {
    +          body
    +        } finally {
    +          // Ideally, we need to make sure onExecutionEnd happens after 
onJobStart and onJobEnd.
    +          // However, onJobStart and onJobEnd run in the listener thread. 
Because we cannot add new
    +          // SQL event types to SparkListener since it's a public API, we 
cannot guarantee that.
    +          //
    +          // SQLListener should handle the case that onExecutionEnd 
happens before onJobEnd.
    +          //
    +          // The worst case is onExecutionEnd may happen before onJobStart 
when the listener thread
    +          // is very busy. If so, we cannot track the jobs for the 
execution. It seems acceptable.
    --- End diff --
    
    For this patch, I think the existing behavior is acceptable, since the only 
consumer of `onExecutionStart/End` is the SQL tab listener, which is completely 
internal. So as long as your downstream listener handles this case correctly 
then that's OK. In a separate patch, we can attempt to fix this as follows:
    
    I think the right approach is to post these events in a single thread such 
that the ordering is guaranteed. We can do this through a `SQLListenerBus`, 
which will represent the entry point to all listeners in SQL. The 
`SQLListenerBus` is a listener itself, and it will simply forward Spark core 
events to its children listeners. There you can generate the `ExecutionEnd` 
event only when all jobs associated with the particular execution have 
completed.


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

Reply via email to