Github user marmbrus commented on a diff in the pull request:
https://github.com/apache/spark/pull/8909#discussion_r41422438
--- Diff:
sql/hive/src/main/scala/org/apache/spark/sql/hive/client/IsolatedClientLoader.scala
---
@@ -148,53 +150,88 @@ private[hive] class IsolatedClientLoader(
name.replaceAll("\\.", "/") + ".class"
/** The classloader that is used to load an isolated version of Hive. */
- protected val classLoader: ClassLoader = new URLClassLoader(allJars,
rootClassLoader) {
- override def loadClass(name: String, resolve: Boolean): Class[_] = {
- val loaded = findLoadedClass(name)
- if (loaded == null) doLoadClass(name, resolve) else loaded
- }
+ private[hive] var classLoader: ClassLoader = if (isolationOn) {
+ new URLClassLoader(allJars, rootClassLoader) {
+ val cache = new java.util.concurrent.ConcurrentHashMap[String,
Class[_]]
+
+ override def loadClass(name: String, resolve: Boolean): Class[_] = {
+ var clazz = findLoadedClass(name)
+ if (clazz == null) {
+ clazz = cache.get(name)
+ if (clazz == null) {
+ // always assume resolve is false at this point - we'll
resolve later
+ clazz = doLoadClass(name, resolve = false)
+ val clazz2 = cache.putIfAbsent(name, clazz)
+ // check if someone else beat us to updating the cache entry
+ if (clazz2 != null) {
+ // if so then we should use the cached entry to be consistent
+ clazz = clazz2
+ }
+ }
+ if (resolve) {
+ resolveClass(clazz)
+ }
+ }
+ clazz
+ }
- def doLoadClass(name: String, resolve: Boolean): Class[_] = {
- val classFileName = name.replaceAll("\\.", "/") + ".class"
- if (isBarrierClass(name) && isolationOn) {
- // For barrier classes, we construct a new copy of the class.
- val bytes =
IOUtils.toByteArray(baseClassLoader.getResourceAsStream(classFileName))
- logDebug(s"custom defining: $name -
${util.Arrays.hashCode(bytes)}")
- defineClass(name, bytes, 0, bytes.length)
- } else if (!isSharedClass(name)) {
- logDebug(s"hive class: $name - ${getResource(classToPath(name))}")
- super.loadClass(name, resolve)
- } else {
- // For shared classes, we delegate to baseClassLoader.
- logDebug(s"shared class: $name")
- baseClassLoader.loadClass(name)
+ def doLoadClass(name: String, resolve: Boolean): Class[_] = {
+ val classFileName = name.replaceAll("\\.", "/") + ".class"
+ if (isBarrierClass(name)) {
+ // For barrier classes, we construct a new copy of the class.
+ val bytes =
IOUtils.toByteArray(baseClassLoader.getResourceAsStream(classFileName))
+ logDebug(s"custom defining: $name -
${util.Arrays.hashCode(bytes)}")
+ defineClass(name, bytes, 0, bytes.length)
+ } else if (!isSharedClass(name)) {
+ logDebug(s"hive class: $name -
${getResource(classToPath(name))}")
+ super.loadClass(name, resolve)
+ } else {
+ // For shared classes, we delegate to baseClassLoader.
+ logDebug(s"shared class: $name")
+ baseClassLoader.loadClass(name)
+ }
}
}
+ } else {
+ baseClassLoader
}
- // Pre-reflective instantiation setup.
- logDebug("Initializing the logger to avoid disaster...")
- Thread.currentThread.setContextClassLoader(classLoader)
+ private[hive] def addJar(path: String): Unit = synchronized {
+ val jarURL = new java.io.File(path).toURI.toURL
+ classLoader = new java.net.URLClassLoader(Array(jarURL), classLoader)
+ }
/** The isolated client interface to Hive. */
- val client: ClientInterface = try {
- classLoader
- .loadClass(classOf[ClientWrapper].getName)
- .getConstructors.head
- .newInstance(version, config, classLoader)
- .asInstanceOf[ClientInterface]
- } catch {
- case e: InvocationTargetException =>
- if (e.getCause().isInstanceOf[NoClassDefFoundError]) {
- val cnf = e.getCause().asInstanceOf[NoClassDefFoundError]
- throw new ClassNotFoundException(
- s"$cnf when creating Hive client using classpath:
${execJars.mkString(", ")}\n" +
- "Please make sure that jars for your version of hive and hadoop
are included in the " +
- s"paths passed to ${HiveContext.HIVE_METASTORE_JARS}.")
- } else {
- throw e
- }
- } finally {
- Thread.currentThread.setContextClassLoader(baseClassLoader)
+ private[hive] def createClient(): ClientInterface = {
+ if (!isolationOn) {
+ return new ClientWrapper(version, config, baseClassLoader, this)
+ }
+ // Pre-reflective instantiation setup.
+ logDebug("Initializing the logger to avoid disaster...")
+ val origLoader = Thread.currentThread().getContextClassLoader
+ Thread.currentThread.setContextClassLoader(classLoader)
+
+ try {
+ classLoader
+ .loadClass(classOf[ClientWrapper].getName)
+ .getConstructors.head
+ .newInstance(version, config, classLoader, this)
+ .asInstanceOf[ClientInterface]
+ } catch {
+ case e: InvocationTargetException =>
+ if (e.getCause().isInstanceOf[NoClassDefFoundError]) {
+ val cnf = e.getCause().asInstanceOf[NoClassDefFoundError]
+ throw new ClassNotFoundException(
+ s"$cnf when creating Hive client using classpath:
${execJars.mkString(", ")}\n" +
+ "Please make sure that jars for your version of hive and
hadoop are included in the " +
+ s"paths passed to ${HiveContext.HIVE_METASTORE_JARS}.")
+ } else {
+ throw e
+ }
+ } finally {
+ Thread.currentThread.setContextClassLoader(origLoader)
+ }
}
+
+ private[hive] var cachedHive: Any = null
--- End diff --
Can you document what this is used for.
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]