Skip to content
Snippets Groups Projects
Commit 40c7add3 authored by Wenchen Fan's avatar Wenchen Fan
Browse files

[SPARK-20946][SQL] Do not update conf for existing SparkContext in SparkSession.getOrCreate

## What changes were proposed in this pull request?

SparkContext is shared by all sessions, we should not update its conf for only one session.

## How was this patch tested?

existing tests

Author: Wenchen Fan <wenchen@databricks.com>

Closes #18536 from cloud-fan/config.
parent 0217dfd2
No related branches found
No related tags found
No related merge requests found
...@@ -818,15 +818,13 @@ class ALSCleanerSuite extends SparkFunSuite { ...@@ -818,15 +818,13 @@ class ALSCleanerSuite extends SparkFunSuite {
FileUtils.listFiles(localDir, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE).asScala.toSet FileUtils.listFiles(localDir, TrueFileFilter.INSTANCE, TrueFileFilter.INSTANCE).asScala.toSet
try { try {
conf.set("spark.local.dir", localDir.getAbsolutePath) conf.set("spark.local.dir", localDir.getAbsolutePath)
val sc = new SparkContext("local[2]", "test", conf) val sc = new SparkContext("local[2]", "ALSCleanerSuite", conf)
try { try {
sc.setCheckpointDir(checkpointDir.getAbsolutePath) sc.setCheckpointDir(checkpointDir.getAbsolutePath)
// Generate test data // Generate test data
val (training, _) = ALSSuite.genImplicitTestData(sc, 20, 5, 1, 0.2, 0) val (training, _) = ALSSuite.genImplicitTestData(sc, 20, 5, 1, 0.2, 0)
// Implicitly test the cleaning of parents during ALS training // Implicitly test the cleaning of parents during ALS training
val spark = SparkSession.builder val spark = SparkSession.builder
.master("local[2]")
.appName("ALSCleanerSuite")
.sparkContext(sc) .sparkContext(sc)
.getOrCreate() .getOrCreate()
import spark.implicits._ import spark.implicits._
......
...@@ -43,8 +43,6 @@ private[ml] object TreeTests extends SparkFunSuite { ...@@ -43,8 +43,6 @@ private[ml] object TreeTests extends SparkFunSuite {
categoricalFeatures: Map[Int, Int], categoricalFeatures: Map[Int, Int],
numClasses: Int): DataFrame = { numClasses: Int): DataFrame = {
val spark = SparkSession.builder() val spark = SparkSession.builder()
.master("local[2]")
.appName("TreeTests")
.sparkContext(data.sparkContext) .sparkContext(data.sparkContext)
.getOrCreate() .getOrCreate()
import spark.implicits._ import spark.implicits._
......
...@@ -867,7 +867,7 @@ object SparkSession { ...@@ -867,7 +867,7 @@ object SparkSession {
* *
* @since 2.2.0 * @since 2.2.0
*/ */
def withExtensions(f: SparkSessionExtensions => Unit): Builder = { def withExtensions(f: SparkSessionExtensions => Unit): Builder = synchronized {
f(extensions) f(extensions)
this this
} }
...@@ -912,21 +912,16 @@ object SparkSession { ...@@ -912,21 +912,16 @@ object SparkSession {
// No active nor global default session. Create a new one. // No active nor global default session. Create a new one.
val sparkContext = userSuppliedContext.getOrElse { val sparkContext = userSuppliedContext.getOrElse {
// set app name if not given
val randomAppName = java.util.UUID.randomUUID().toString
val sparkConf = new SparkConf() val sparkConf = new SparkConf()
options.foreach { case (k, v) => sparkConf.set(k, v) } options.foreach { case (k, v) => sparkConf.set(k, v) }
// set a random app name if not given.
if (!sparkConf.contains("spark.app.name")) { if (!sparkConf.contains("spark.app.name")) {
sparkConf.setAppName(randomAppName) sparkConf.setAppName(java.util.UUID.randomUUID().toString)
}
val sc = SparkContext.getOrCreate(sparkConf)
// maybe this is an existing SparkContext, update its SparkConf which maybe used
// by SparkSession
options.foreach { case (k, v) => sc.conf.set(k, v) }
if (!sc.conf.contains("spark.app.name")) {
sc.conf.setAppName(randomAppName)
} }
sc
SparkContext.getOrCreate(sparkConf)
// Do not update `SparkConf` for existing `SparkContext`, as it's shared by all sessions.
} }
// Initialize extensions if the user has defined a configurator class. // Initialize extensions if the user has defined a configurator class.
......
...@@ -102,11 +102,9 @@ class SparkSessionBuilderSuite extends SparkFunSuite { ...@@ -102,11 +102,9 @@ class SparkSessionBuilderSuite extends SparkFunSuite {
assert(session.conf.get("key1") == "value1") assert(session.conf.get("key1") == "value1")
assert(session.conf.get("key2") == "value2") assert(session.conf.get("key2") == "value2")
assert(session.sparkContext == sparkContext2) assert(session.sparkContext == sparkContext2)
assert(session.sparkContext.conf.get("key1") == "value1") // We won't update conf for existing `SparkContext`
// If the created sparkContext is not passed through the Builder's API sparkContext, assert(!sparkContext2.conf.contains("key2"))
// the conf of this sparkContext will also contain the conf set through the API config. assert(sparkContext2.conf.get("key1") == "value1")
assert(session.sparkContext.conf.get("key2") == "value2")
assert(session.sparkContext.conf.get("spark.app.name") == "test")
session.stop() session.stop()
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment