Skip to content
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
47 changes: 46 additions & 1 deletion sql/core/src/main/scala/org/apache/spark/sql/SQLContext.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package org.apache.spark.sql

import java.beans.Introspector
import java.util.Properties
import java.util.concurrent.atomic.AtomicReference

import scala.collection.JavaConversions._
import scala.collection.immutable
Expand Down Expand Up @@ -1270,9 +1271,53 @@ class SQLContext(@transient val sparkContext: SparkContext)

////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
// End of eeprecated methods
// End of deprecated methods
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////


// Register a succesfully instantiatd context to the singleton. This should be at the end of
// the class definition so that the singleton is updated only if there is no exception in the
// construction of the instance.
SQLContext.setLastInstantiatedContext(self)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rxin Design question. Should contexts created directly (that is, not through getOrCreate) also be automatically considered for the singleton?

}

/**
* This SQLContext object contains utility functions to create a singleton SQLContext instance,
* or to get the last created SQLContext instance.
*/
object SQLContext {

private val INSTANTIATION_LOCK = new Object()

/**
* Reference to the last created SQLContext.
*/
@transient private val lastInstantiatedContext = new AtomicReference[SQLContext]()

/**
* Get the singleton SQLContext if it exists or create a new one using the given SparkContext.
* This function can be used to create a singleton SQLContext object that can be shared across
* the JVM.
*/
def getOrCreate(sparkContext: SparkContext): SQLContext = {
INSTANTIATION_LOCK.synchronized {
if (lastInstantiatedContext.get() == null) {
new SQLContext(sparkContext)
}
}
lastInstantiatedContext.get()
}

private[sql] def clearLastInstantiatedContext(): Unit = {
INSTANTIATION_LOCK.synchronized {
lastInstantiatedContext.set(null)
}
}

private[sql] def setLastInstantiatedContext(sqlContext: SQLContext): Unit = {
INSTANTIATION_LOCK.synchronized {
lastInstantiatedContext.set(sqlContext)
}
}
}
49 changes: 49 additions & 0 deletions sql/core/src/test/scala/org/apache/spark/sql/SQLContextSuite.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.spark.sql

import org.scalatest.{BeforeAndAfterAll, FunSuite}

import org.apache.spark.sql.test.TestSQLContext

class SQLContextSuite extends FunSuite with BeforeAndAfterAll {

private val testSqlContext = TestSQLContext
private val testSparkContext = TestSQLContext.sparkContext

override def afterAll(): Unit = {
SQLContext.setLastInstantiatedContext(testSqlContext)
}

test("getOrCreate instantiates SQLContext") {
SQLContext.clearLastInstantiatedContext()
val sqlContext = SQLContext.getOrCreate(testSparkContext)
assert(sqlContext != null, "SQLContext.getOrCreate returned null")
assert(SQLContext.getOrCreate(testSparkContext).eq(sqlContext),
"SQLContext created by SQLContext.getOrCreate not returned by SQLContext.getOrCreate")
}

test("getOrCreate gets last explicitly instantiated SQLContext") {
SQLContext.clearLastInstantiatedContext()
val sqlContext = new SQLContext(testSparkContext)
assert(SQLContext.getOrCreate(testSparkContext) != null,
"SQLContext.getOrCreate after explicitly created SQLContext returned null")
assert(SQLContext.getOrCreate(testSparkContext).eq(sqlContext),
"SQLContext.getOrCreate after explicitly created SQLContext did not return the context")
}
}