Skip to content

Commit

Permalink
Fix fall back with custom class loader and add a test for it
Browse files Browse the repository at this point in the history
  • Loading branch information
holdenk committed Apr 8, 2014
1 parent 4919bf9 commit d90d217
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public ParentClassLoader(ClassLoader myParent) {

@Override
public Class<?> findClass(String name) throws ClassNotFoundException {
return super.loadClass(name);
return super.findClass(name);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private[spark] class ChildExecutorURLClassLoader(urls: Array[URL], parent: Class
userClassLoader.findClass(name)
} catch {
case e: ClassNotFoundException => {
parentClassLoader.findClass(name)
parentClassLoader.loadClass(name)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,13 @@ class ExecutorURLClassLoaderSuite extends FunSuite {
assert(fakeClassVersion === "2")
}

test("child first can fail") {
val parentLoader = new URLClassLoader(urls2, null)
val classLoader = new ChildExecutorURLClassLoader(urls, parentLoader)
intercept[java.lang.ClassNotFoundException] {
classLoader.loadClass("org.apache.spark.test.FakeClassDoesNotExist").newInstance()
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ extends ClassLoader {

override def findClass(name: String): Class[_] = {
userClassPathFirst match {
case true => findClassLocally(name).getOrElse(parentLoader.findClass(name))
case true => findClassLocally(name).getOrElse(parentLoader.loadClass(name))
case false => {
try {
parentLoader.loadClass(name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ class ExecutorClassLoaderSuite extends FunSuite {
val spark_home = sys.env.get("SPARK_HOME").orElse(sys.props.get("spark.home")).get
val url1 = "file://" + spark_home + "/core/src/test/resources/classes1/"
val urls2 = List(new File(spark_home + "/core/src/test/resources/classes2/").toURI.toURL).toArray

test("child first") {
println("url1 "+url1)
val parentLoader = new URLClassLoader(urls2, null)
val classLoader = new ExecutorClassLoader(url1, parentLoader, true)
val fakeClass = classLoader.loadClass("org.apache.spark.test.FakeClass2").newInstance()
Expand All @@ -45,12 +45,19 @@ class ExecutorClassLoaderSuite extends FunSuite {
}

test("child first can fall back") {
println("url1 "+url1)
val parentLoader = new URLClassLoader(urls2, null)
val classLoader = new ExecutorClassLoader(url1, parentLoader, true)
val fakeClass = classLoader.loadClass("org.apache.spark.test.FakeClass3").newInstance()
val fakeClassVersion = fakeClass.toString
assert(fakeClassVersion === "2")
}

test("child first can fail") {
val parentLoader = new URLClassLoader(urls2, null)
val classLoader = new ExecutorClassLoader(url1, parentLoader, true)
intercept[java.lang.ClassNotFoundException] {
classLoader.loadClass("org.apache.spark.test.FakeClassDoesNotExist").newInstance()
}
}

}

0 comments on commit d90d217

Please sign in to comment.