Skip to content

[Spark-14685] [CORE] Document heritability of localProperties #12455

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions core/src/main/scala/org/apache/spark/SparkContext.scala
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,11 @@ class SparkContext(config: SparkConf) extends Logging with ExecutorAllocationCli
* scheduler pool. User-defined properties may also be set here. These properties are propagated
* through to worker tasks and can be accessed there via
* [[org.apache.spark.TaskContext#getLocalProperty]].
*
* These properties are inherited by child threads spawned from this thread. This
* may have unexpected consequences when working with thread pools. The standard java
* implementation of thread pools have worker threads spawn other worker threads.
* As a result, local properties may propagate unpredictably.
*/
def setLocalProperty(key: String, value: String) {
if (value == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -712,8 +712,13 @@ class JavaSparkContext(val sc: SparkContext)
}

/**
* Set a local property that affects jobs submitted from this thread, such as the
* Spark fair scheduler pool.
* Set a local property that affects jobs submitted from this thread, and all child
* threads, such as the Spark fair scheduler pool.
*
* These properties are inherited by child threads spawned from this thread. This
* may have unexpected consequences when working with thread pools. The standard java
* implementation of thread pools have worker threads spawn other worker threads.
* As a result, local properties may propagate unpredictably.
*/
def setLocalProperty(key: String, value: String): Unit = sc.setLocalProperty(key, value)

Expand Down
28 changes: 28 additions & 0 deletions core/src/test/scala/org/apache/spark/SparkContextSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -319,4 +319,32 @@ class SparkContextSuite extends SparkFunSuite with LocalSparkContext {
assert(sc.getConf.getInt("spark.executor.instances", 0) === 6)
}
}


test("localProperties are inherited by spawned threads.") {
sc = new SparkContext(new SparkConf().setAppName("test").setMaster("local"))
sc.setLocalProperty("testProperty", "testValue")
var result = "unset";
val thread = new Thread() { override def run() = {result = sc.getLocalProperty("testProperty")}}
thread.start()
thread.join()
sc.stop()
assert(result == "testValue")
}

test("localProperties do not cross-talk between threads.") {
sc = new SparkContext(new SparkConf().setAppName("test").setMaster("local"))
var result = "unset";
val thread1 = new Thread() {
override def run() = {sc.setLocalProperty("testProperty", "testValue")}}
// testProperty should be unset and thus return null
val thread2 = new Thread() {
override def run() = {result = sc.getLocalProperty("testProperty")}}
thread1.start()
thread1.join()
thread2.start()
thread2.join()
sc.stop()
assert(result == null)
}
}