Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPARK-22950][SQL]Handle ChildFirstURLClassLoader's parent #20145

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.internal.SQLConf._
import org.apache.spark.sql.internal.StaticSQLConf.{CATALOG_IMPLEMENTATION, WAREHOUSE_PATH}
import org.apache.spark.sql.types._
import org.apache.spark.util.Utils
import org.apache.spark.util.{ChildFirstURLClassLoader, Utils}


private[spark] object HiveUtils extends Logging {
Expand Down Expand Up @@ -312,6 +312,8 @@ private[spark] object HiveUtils extends Logging {
// starting from the given classLoader.
def allJars(classLoader: ClassLoader): Array[URL] = classLoader match {
case null => Array.empty[URL]
case childFirst: ChildFirstURLClassLoader =>
childFirst.getURLs() ++ allJars(Utils.getSparkClassLoader)
case urlClassLoader: URLClassLoader =>
urlClassLoader.getURLs ++ allJars(urlClassLoader.getParent)
case other => allJars(other.getParent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@

package org.apache.spark.sql.hive

import java.net.URL

import org.apache.hadoop.hive.conf.HiveConf.ConfVars

import org.apache.spark.SparkConf
import org.apache.spark.deploy.SparkHadoopUtil
import org.apache.spark.sql.QueryTest
import org.apache.spark.sql.hive.test.TestHiveSingleton
import org.apache.spark.sql.test.SQLTestUtils
import org.apache.spark.util.{ChildFirstURLClassLoader, MutableURLClassLoader}

class HiveUtilsSuite extends QueryTest with SQLTestUtils with TestHiveSingleton {

Expand All @@ -42,4 +47,41 @@ class HiveUtilsSuite extends QueryTest with SQLTestUtils with TestHiveSingleton
assert(hiveConf("foo") === "bar")
}
}

test("ChildFirstURLClassLoader's parent is null") {
Copy link
Contributor

Choose a reason for hiding this comment

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

what is this test trying to test?

Copy link
Member Author

Choose a reason for hiding this comment

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

just indicates that if we don't handle child first classloader's parent, we will get caught in exception. if it bothers, i can del it

Copy link
Contributor

Choose a reason for hiding this comment

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

I think we can remove it. The below tests case would fail without your PR, that proves your patch already.

val conf = new SparkConf
val contextClassLoader = Thread.currentThread().getContextClassLoader
val loader = new FakeChildFirstURLClassLoader(Array(), contextClassLoader)
try {
Thread.currentThread().setContextClassLoader(loader)
intercept[IllegalArgumentException](
HiveUtils.newClientForMetadata(
conf,
SparkHadoopUtil.newConfiguration(conf),
HiveUtils.newTemporaryConfiguration(useInMemoryDerby = true)))
} finally {
Thread.currentThread().setContextClassLoader(contextClassLoader)
}
}

test("ChildFirstURLClassLoader's parent is null, get spark classloader instead") {
val conf = new SparkConf
val contextClassLoader = Thread.currentThread().getContextClassLoader
val loader = new ChildFirstURLClassLoader(Array(), contextClassLoader)
try {
Thread.currentThread().setContextClassLoader(loader)
HiveUtils.newClientForMetadata(
conf,
SparkHadoopUtil.newConfiguration(conf),
HiveUtils.newTemporaryConfiguration(useInMemoryDerby = true))
} finally {
Thread.currentThread().setContextClassLoader(contextClassLoader)
}
}
}

/**
* A Fake [[ChildFirstURLClassLoader]] used for test
*/
private[spark] class FakeChildFirstURLClassLoader(urls: Array[URL], parent: ClassLoader)
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not a ChildFirstURLClassLoader, shall we make it extend ChildFirstURLClassLoader?

Copy link
Member Author

Choose a reason for hiding this comment

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

inheritation maketh case match

extends MutableURLClassLoader(urls, null)