@@ -66,14 +66,17 @@ import org.apache.spark.util._
6666
6767class SparkContext (config : SparkConf ) extends SparkStatusAPI with Logging {
6868
69+ // The call site where this SparkContext was constructed.
70+ private val creationSite : CallSite = Utils .getCallSite()
71+
72+ // If true, log warnings instead of throwing exceptions when multiple SparkContexts are active
73+ private val allowMultipleContexts : Boolean =
74+ config.getBoolean(" spark.driver.allowMultipleContexts" , false )
75+
76+
6977 // In order to prevent multiple SparkContexts from being active at the same time, mark this
7078 // context as having started construction
71- SparkContext .markPartiallyConstructed(this , config)
72-
73- /**
74- * The call site where this SparkContext was constructed.
75- */
76- private val creationSite : CallSite = Utils .getCallSite()
79+ SparkContext .markPartiallyConstructed(this , allowMultipleContexts)
7780
7881 // This is used only by YARN for now, but should be relevant to other cluster types (Mesos,
7982 // etc) too. This is typically generated from InputFormatInfo.computePreferredLocations. It
@@ -1427,7 +1430,7 @@ class SparkContext(config: SparkConf) extends SparkStatusAPI with Logging {
14271430
14281431 // In order to prevent multiple SparkContexts from being active at the same time, mark this
14291432 // context as having finished construction
1430- SparkContext .setActiveContext(this , config )
1433+ SparkContext .setActiveContext(this , allowMultipleContexts )
14311434}
14321435
14331436/**
@@ -1464,7 +1467,9 @@ object SparkContext extends Logging {
14641467 * prevents us from reliably distinguishing between cases where another context is being
14651468 * constructed and cases where another constructor threw an exception.
14661469 */
1467- private def assertNoOtherContextIsRunning (sc : SparkContext , conf : SparkConf ): Unit = {
1470+ private def assertNoOtherContextIsRunning (
1471+ sc : SparkContext ,
1472+ allowMultipleContexts : Boolean ): Unit = {
14681473 SPARK_CONTEXT_CONSTRUCTOR_LOCK .synchronized {
14691474 contextBeingConstructed.foreach { otherContext =>
14701475 if (otherContext ne sc) { // checks for reference equality
@@ -1479,7 +1484,7 @@ object SparkContext extends Logging {
14791484 " To ignore this error, set spark.driver.allowMultipleContexts = true. " +
14801485 s " The currently running SparkContext was created at: \n ${ctx.creationSite.longForm}"
14811486 val exception = new SparkException (errMsg)
1482- if (conf.getBoolean( " spark.driver. allowMultipleContexts" , false ) ) {
1487+ if (allowMultipleContexts) {
14831488 logWarning(" Multiple running SparkContexts detected in the same JVM!" , exception)
14841489 } else {
14851490 throw exception
@@ -1496,9 +1501,11 @@ object SparkContext extends Logging {
14961501 * scheme prevents us from reliably distinguishing between cases where another context is being
14971502 * constructed and cases where another constructor threw an exception.
14981503 */
1499- private [spark] def markPartiallyConstructed (sc : SparkContext , conf : SparkConf ): Unit = {
1504+ private [spark] def markPartiallyConstructed (
1505+ sc : SparkContext ,
1506+ allowMultipleContexts : Boolean ): Unit = {
15001507 SPARK_CONTEXT_CONSTRUCTOR_LOCK .synchronized {
1501- assertNoOtherContextIsRunning(sc, conf )
1508+ assertNoOtherContextIsRunning(sc, allowMultipleContexts )
15021509 contextBeingConstructed = Some (sc)
15031510 }
15041511 }
@@ -1507,9 +1514,11 @@ object SparkContext extends Logging {
15071514 * Called at the end of the SparkContext constructor to ensure that no other SparkContext has
15081515 * raced with this constructor and started.
15091516 */
1510- private [spark] def setActiveContext (sc : SparkContext , conf : SparkConf ): Unit = {
1517+ private [spark] def setActiveContext (
1518+ sc : SparkContext ,
1519+ allowMultipleContexts : Boolean ): Unit = {
15111520 SPARK_CONTEXT_CONSTRUCTOR_LOCK .synchronized {
1512- assertNoOtherContextIsRunning(sc, conf )
1521+ assertNoOtherContextIsRunning(sc, allowMultipleContexts )
15131522 contextBeingConstructed = None
15141523 activeContext = Some (sc)
15151524 }
0 commit comments