Skip to content

Commit 685780e

Browse files
SPARK-5425: Use synchronised methods in system properties to create SparkConf
1 parent 3bb5d13 commit 685780e

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

core/src/main/scala/org/apache/spark/SparkConf.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ class SparkConf(loadDefaults: Boolean) extends Cloneable with Logging {
4949

5050
if (loadDefaults) {
5151
// Load any spark.* system properties
52-
for ((k, v) <- System.getProperties.asScala if k.startsWith("spark.")) {
53-
settings(k) = v
52+
val propNames = System.getProperties.stringPropertyNames().asScala
53+
for (k <- propNames if k.startsWith("spark.")) {
54+
settings(k) = System.getProperty(k)
5455
}
5556
}
5657

core/src/test/scala/org/apache/spark/SparkConfSuite.scala

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717

1818
package org.apache.spark
1919

20+
import java.util.concurrent.{TimeUnit, Executors}
21+
22+
import scala.util.{Try, Random}
23+
2024
import org.scalatest.FunSuite
2125

2226
import org.apache.spark.util.ResetSystemProperties
@@ -121,4 +125,25 @@ class SparkConfSuite extends FunSuite with LocalSparkContext with ResetSystemPro
121125
assert(conf.get("spark.test.a.b") === "A.B")
122126
assert(conf.get("spark.test.a.b.c") === "a.b.c")
123127
}
128+
129+
test("Thread safeness - SPARK-5425") {
130+
import scala.collection.JavaConversions._
131+
val executor = Executors.newSingleThreadScheduledExecutor()
132+
val sf = executor.scheduleAtFixedRate(new Runnable {
133+
override def run(): Unit =
134+
System.setProperty("spark.5425." + Random.nextInt(), Random.nextInt().toString)
135+
}, 0, 1, TimeUnit.MILLISECONDS)
136+
137+
try {
138+
val t0 = System.currentTimeMillis()
139+
while ((System.currentTimeMillis() - t0) < 1000) {
140+
val conf = Try(new SparkConf(loadDefaults = true))
141+
assert(conf.isSuccess === true)
142+
}
143+
} finally {
144+
executor.shutdownNow()
145+
for (key <- System.getProperties.stringPropertyNames() if key.startsWith("spark.5425."))
146+
System.getProperties.remove(key)
147+
}
148+
}
124149
}

0 commit comments

Comments
 (0)