-
Notifications
You must be signed in to change notification settings - Fork 28.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-10548] [SPARK-10563] [SQL] Fix concurrent SQL executions
*Note: this is for master branch only.* The fix for branch-1.5 is at #8721. The query execution ID is currently passed from a thread to its children, which is not the intended behavior. This led to `IllegalArgumentException: spark.sql.execution.id is already set` when running queries in parallel, e.g.: ``` (1 to 100).par.foreach { _ => sc.parallelize(1 to 5).map { i => (i, i) }.toDF("a", "b").count() } ``` The cause is `SparkContext`'s local properties are inherited by default. This patch adds a way to exclude keys we don't want to be inherited, and makes SQL go through that code path. Author: Andrew Or <[email protected]> Closes #8710 from andrewor14/concurrent-sql-executions.
- Loading branch information
Andrew Or
committed
Sep 15, 2015
1 parent
be52faa
commit b6e9986
Showing
3 changed files
with
132 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
sql/core/src/test/scala/org/apache/spark/sql/execution/SQLExecutionSuite.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
* 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.Properties | ||
|
||
import scala.collection.parallel.CompositeThrowable | ||
|
||
import org.apache.spark.{SparkConf, SparkContext, SparkFunSuite} | ||
import org.apache.spark.sql.SQLContext | ||
|
||
class SQLExecutionSuite extends SparkFunSuite { | ||
|
||
test("concurrent query execution (SPARK-10548)") { | ||
// Try to reproduce the issue with the old SparkContext | ||
val conf = new SparkConf() | ||
.setMaster("local[*]") | ||
.setAppName("test") | ||
val badSparkContext = new BadSparkContext(conf) | ||
try { | ||
testConcurrentQueryExecution(badSparkContext) | ||
fail("unable to reproduce SPARK-10548") | ||
} catch { | ||
case e: IllegalArgumentException => | ||
assert(e.getMessage.contains(SQLExecution.EXECUTION_ID_KEY)) | ||
} finally { | ||
badSparkContext.stop() | ||
} | ||
|
||
// Verify that the issue is fixed with the latest SparkContext | ||
val goodSparkContext = new SparkContext(conf) | ||
try { | ||
testConcurrentQueryExecution(goodSparkContext) | ||
} finally { | ||
goodSparkContext.stop() | ||
} | ||
} | ||
|
||
/** | ||
* Trigger SPARK-10548 by mocking a parent and its child thread executing queries concurrently. | ||
*/ | ||
private def testConcurrentQueryExecution(sc: SparkContext): Unit = { | ||
val sqlContext = new SQLContext(sc) | ||
import sqlContext.implicits._ | ||
|
||
// Initialize local properties. This is necessary for the test to pass. | ||
sc.getLocalProperties | ||
|
||
// Set up a thread that runs executes a simple SQL query. | ||
// Before starting the thread, mutate the execution ID in the parent. | ||
// The child thread should not see the effect of this change. | ||
var throwable: Option[Throwable] = None | ||
val child = new Thread { | ||
override def run(): Unit = { | ||
try { | ||
sc.parallelize(1 to 100).map { i => (i, i) }.toDF("a", "b").collect() | ||
} catch { | ||
case t: Throwable => | ||
throwable = Some(t) | ||
} | ||
|
||
} | ||
} | ||
sc.setLocalProperty(SQLExecution.EXECUTION_ID_KEY, "anything") | ||
child.start() | ||
child.join() | ||
|
||
// The throwable is thrown from the child thread so it doesn't have a helpful stack trace | ||
throwable.foreach { t => | ||
t.setStackTrace(t.getStackTrace ++ Thread.currentThread.getStackTrace) | ||
throw t | ||
} | ||
} | ||
|
||
} | ||
|
||
/** | ||
* A bad [[SparkContext]] that does not clone the inheritable thread local properties | ||
* when passing them to children threads. | ||
*/ | ||
private class BadSparkContext(conf: SparkConf) extends SparkContext(conf) { | ||
protected[spark] override val localProperties = new InheritableThreadLocal[Properties] { | ||
override protected def childValue(parent: Properties): Properties = new Properties(parent) | ||
override protected def initialValue(): Properties = new Properties() | ||
} | ||
} |