Skip to content

Commit 9fa703e

Browse files
yaooqinncloud-fan
authored andcommitted
[SPARK-22950][SQL] Handle ChildFirstURLClassLoader's parent
## What changes were proposed in this pull request? ChildFirstClassLoader's parent is set to null, so we can't get jars from its parent. This will cause ClassNotFoundException during HiveClient initialization with builtin hive jars, where we may should use spark context loader instead. ## How was this patch tested? add new ut cc cloud-fan gatorsmile Author: Kent Yao <[email protected]> Closes #20145 from yaooqinn/SPARK-22950.
1 parent df95a90 commit 9fa703e

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

sql/hive/src/main/scala/org/apache/spark/sql/hive/HiveUtils.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ import org.apache.spark.sql.internal.SQLConf
4747
import org.apache.spark.sql.internal.SQLConf._
4848
import org.apache.spark.sql.internal.StaticSQLConf.{CATALOG_IMPLEMENTATION, WAREHOUSE_PATH}
4949
import org.apache.spark.sql.types._
50-
import org.apache.spark.util.Utils
50+
import org.apache.spark.util.{ChildFirstURLClassLoader, Utils}
5151

5252

5353
private[spark] object HiveUtils extends Logging {
@@ -312,6 +312,8 @@ private[spark] object HiveUtils extends Logging {
312312
// starting from the given classLoader.
313313
def allJars(classLoader: ClassLoader): Array[URL] = classLoader match {
314314
case null => Array.empty[URL]
315+
case childFirst: ChildFirstURLClassLoader =>
316+
childFirst.getURLs() ++ allJars(Utils.getSparkClassLoader)
315317
case urlClassLoader: URLClassLoader =>
316318
urlClassLoader.getURLs ++ allJars(urlClassLoader.getParent)
317319
case other => allJars(other.getParent)

sql/hive/src/test/scala/org/apache/spark/sql/hive/HiveUtilsSuite.scala

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,16 @@
1717

1818
package org.apache.spark.sql.hive
1919

20+
import java.net.URL
21+
2022
import org.apache.hadoop.hive.conf.HiveConf.ConfVars
2123

24+
import org.apache.spark.SparkConf
25+
import org.apache.spark.deploy.SparkHadoopUtil
2226
import org.apache.spark.sql.QueryTest
2327
import org.apache.spark.sql.hive.test.TestHiveSingleton
2428
import org.apache.spark.sql.test.SQLTestUtils
29+
import org.apache.spark.util.{ChildFirstURLClassLoader, MutableURLClassLoader}
2530

2631
class HiveUtilsSuite extends QueryTest with SQLTestUtils with TestHiveSingleton {
2732

@@ -42,4 +47,19 @@ class HiveUtilsSuite extends QueryTest with SQLTestUtils with TestHiveSingleton
4247
assert(hiveConf("foo") === "bar")
4348
}
4449
}
50+
51+
test("ChildFirstURLClassLoader's parent is null, get spark classloader instead") {
52+
val conf = new SparkConf
53+
val contextClassLoader = Thread.currentThread().getContextClassLoader
54+
val loader = new ChildFirstURLClassLoader(Array(), contextClassLoader)
55+
try {
56+
Thread.currentThread().setContextClassLoader(loader)
57+
HiveUtils.newClientForMetadata(
58+
conf,
59+
SparkHadoopUtil.newConfiguration(conf),
60+
HiveUtils.newTemporaryConfiguration(useInMemoryDerby = true))
61+
} finally {
62+
Thread.currentThread().setContextClassLoader(contextClassLoader)
63+
}
64+
}
4565
}

0 commit comments

Comments
 (0)