Skip to content

Commit ace2fbc

Browse files
committed
simplify the config setting logic in SparkSession.getOrCreate
1 parent 1f5dddf commit ace2fbc

File tree

2 files changed

+7
-17
lines changed

2 files changed

+7
-17
lines changed

mllib/src/test/scala/org/apache/spark/ml/tree/impl/TreeTests.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ private[ml] object TreeTests extends SparkFunSuite {
4343
categoricalFeatures: Map[Int, Int],
4444
numClasses: Int): DataFrame = {
4545
val spark = SparkSession.builder()
46-
.master("local[2]")
4746
.appName("TreeTests")
4847
.sparkContext(data.sparkContext)
4948
.getOrCreate()

sql/core/src/main/scala/org/apache/spark/sql/SparkSession.scala

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -854,7 +854,7 @@ object SparkSession {
854854
*
855855
* @since 2.2.0
856856
*/
857-
def withExtensions(f: SparkSessionExtensions => Unit): Builder = {
857+
def withExtensions(f: SparkSessionExtensions => Unit): Builder = synchronized {
858858
f(extensions)
859859
this
860860
}
@@ -899,22 +899,14 @@ object SparkSession {
899899

900900
// No active nor global default session. Create a new one.
901901
val sparkContext = userSuppliedContext.getOrElse {
902-
// set app name if not given
903-
val randomAppName = java.util.UUID.randomUUID().toString
904902
val sparkConf = new SparkConf()
905-
options.foreach { case (k, v) => sparkConf.set(k, v) }
906-
if (!sparkConf.contains("spark.app.name")) {
907-
sparkConf.setAppName(randomAppName)
908-
}
909-
val sc = SparkContext.getOrCreate(sparkConf)
910-
// maybe this is an existing SparkContext, update its SparkConf which maybe used
911-
// by SparkSession
912-
options.foreach { case (k, v) => sc.conf.set(k, v) }
913-
if (!sc.conf.contains("spark.app.name")) {
914-
sc.conf.setAppName(randomAppName)
915-
}
916-
sc
903+
options.get("spark.master").foreach(sparkConf.setMaster)
904+
// set a random app name if not given.
905+
sparkConf.setAppName(options.getOrElse("spark.app.name",
906+
java.util.UUID.randomUUID().toString))
907+
SparkContext.getOrCreate(sparkConf)
917908
}
909+
options.foreach { case (k, v) => sparkContext.conf.set(k, v) }
918910

919911
// Initialize extensions if the user has defined a configurator class.
920912
val extensionConfOption = sparkContext.conf.get(StaticSQLConf.SPARK_SESSION_EXTENSIONS)
@@ -935,7 +927,6 @@ object SparkSession {
935927
}
936928

937929
session = new SparkSession(sparkContext, None, None, extensions)
938-
options.foreach { case (k, v) => session.sessionState.conf.setConfString(k, v) }
939930
defaultSession.set(session)
940931

941932
// Register a successfully instantiated context to the singleton. This should be at the

0 commit comments

Comments
 (0)