-
Notifications
You must be signed in to change notification settings - Fork 28.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SPARK-10548] [SPARK-10563] [SQL] Fix concurrent SQL executions #8710
Closed
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
8ceae42
Exclude certain local properties from being inherited
3ec715c
Fix remove from Properties + add tests
d48c114
Fix style
bbda199
Merge branch 'master' of github.com:apache/spark into concurrent-sql-…
5297f79
Always clone parent properties
3c00cc6
Merge branch 'master' of github.com:apache/spark into concurrent-sql-…
801fbe0
Merge branch 'master' of github.com:apache/spark into concurrent-sql-…
35bb6f0
Limit scope of mutable set
984a92f
Simplify fix by cloning properties on inherit
fce3819
Merge branch 'master' of github.com:apache/spark into concurrent-sql-…
75a8d90
Merge branch 'master' of github.com:apache/spark into concurrent-sql-…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@andrewor14 I'm thinking maybe we should not use
new Properties(parent)
here. Instead, always copy the parent's Properties to the child's Properties. Do you think if the child thread needs to see the further changes to the parent thread's Properties after creating?This is really confusing when using
Executor
likeForkJoinPool
(scala.concurrent.ExecutionContext.Implicits.global), in which thread A creates thread B but thread B is not a child of thread A. But thread B still can see the changes in thread A./cc @jerryshao since you added this line.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @zsxwing , reasons to use
InheritableThreadLocal
can be seen here (mesos/spark#937). mainly it is used for Spark Streaming with FIFO scheduling strategy.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. However,
new Properties(parent)
keeps a reference to parent rather than copying them. So If we make any change to the parent thread's properties after creating the child thread, the child thread will see them.Is it necessary that the child thread keeps track of the further updates of the parent thread's properties? I think copying them would be more reasonable.
I didn't mean removing this line. I mean changing it to
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes I agree with you that copying is more reasonable, for now I cannot image any scenario which requires to keep track of parent's properties, I think it is OK for me to change it, we can always fix this if there's any special scenario.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jerryshao Thanks :)
@andrewor14 how about just copying the parent properties rather than adding
nonInheritedLocalProperties
? It looks simpler.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, I was actually going to do it in a separate patch. Incidentally @tdas @JoshRosen and I just talked about this last night and we all agreed to make it do a clone instead so the semantics are simpler.
However, my one concern is that doing so will change semantics for non-SQL users in 1.5.1, so my proposal is the following: I will make the changes in this patch and merge this patch ONLY into master. Then I'll create a new patch for branch 1.5 that will have the current changes (the ones where we don't clone except for SQL). I think that's the safest way forward.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I have updated this in the latest commit, and filed SPARK-10563 for this issue.