Skip to content
Closed
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
68 changes: 53 additions & 15 deletions repl/src/main/scala/org/apache/spark/repl/ExecutorClassLoader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
package org.apache.spark.repl

import java.io.{ByteArrayOutputStream, InputStream}
import java.net.{URI, URL, URLEncoder}
import java.net.{HttpURLConnection, URI, URL, URLEncoder}
import java.util.concurrent.{Executors, ExecutorService}

import org.apache.hadoop.fs.{FileSystem, Path}

import org.apache.spark.{SparkConf, SparkEnv}
import org.apache.spark.{Logging, SparkConf, SparkEnv}
import org.apache.spark.deploy.SparkHadoopUtil
import org.apache.spark.util.Utils
import org.apache.spark.util.ParentClassLoader
Expand All @@ -37,7 +37,8 @@ import com.esotericsoftware.reflectasm.shaded.org.objectweb.asm.Opcodes._
* Allows the user to specify if user class path should be first
*/
class ExecutorClassLoader(conf: SparkConf, classUri: String, parent: ClassLoader,
userClassPathFirst: Boolean) extends ClassLoader {
userClassPathFirst: Boolean) extends ClassLoader with Logging {

val uri = new URI(classUri)
val directory = uri.getPath

Expand Down Expand Up @@ -71,27 +72,64 @@ class ExecutorClassLoader(conf: SparkConf, classUri: String, parent: ClassLoader
}
}

private def getClassFileInputStreamFromHttpServer(pathInDirectory: String): InputStream = {
val url: URL = if (SparkEnv.get.securityManager.isAuthenticationEnabled()) {
val uri = new URI(classUri + "/" + urlEncode(pathInDirectory))
Utils.constructURIForAuthentication(uri, SparkEnv.get.securityManager).toURL
} else {
new URL(classUri + "/" + urlEncode(pathInDirectory))
}
val connection = url.openConnection().asInstanceOf[HttpURLConnection]
if (connection.asInstanceOf[HttpURLConnection].getResponseCode != 200) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If a class cannot be found at the driver, then Jetty will return a 404 status code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, this still might be a subtly-wrong use of HttpUrlConnection; see

http://stackoverflow.com/a/39449/590203
http://www.tbray.org/ongoing/When/201x/2012/01/17/HttpURLConnection

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, even this is still subtly-unsafe due to HttpUrlConnection's weird KeepAlive behavior:

https://plumbr.eu/blog/12-year-old-bug-in-jdk-still-out-there-leaking-memory
https://news.ycombinator.com/item?id=4931911

Copy link
Member

Choose a reason for hiding this comment

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

(The second cast is redundant?) Can this be mitigated with Connection: close, or by just making a HEAD request? I know, that might require libraries.

connection.disconnect()
throw new ClassNotFoundException(s"Class file not found at URL $url")
} else {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If a class couldn't be found, then this would throw FileNotFoundException.

connection.getInputStream
}
}

private def getClassFileInputStreamFromFileSystem(pathInDirectory: String): InputStream = {
val path = new Path(directory, pathInDirectory)
if (fileSystem.exists(path)) {
fileSystem.open(path)
} else {
throw new ClassNotFoundException(s"Class file not found at path $path")
}
}

def findClassLocally(name: String): Option[Class[_]] = {
val pathInDirectory = name.replace('.', '/') + ".class"
var inputStream: InputStream = null
try {
val pathInDirectory = name.replace('.', '/') + ".class"
val inputStream = {
inputStream = {
if (fileSystem != null) {
fileSystem.open(new Path(directory, pathInDirectory))
getClassFileInputStreamFromFileSystem(pathInDirectory)
} else {
if (SparkEnv.get.securityManager.isAuthenticationEnabled()) {
val uri = new URI(classUri + "/" + urlEncode(pathInDirectory))
val newuri = Utils.constructURIForAuthentication(uri, SparkEnv.get.securityManager)
newuri.toURL().openStream()
} else {
new URL(classUri + "/" + urlEncode(pathInDirectory)).openStream()
}
getClassFileInputStreamFromHttpServer(pathInDirectory)
}
}
val bytes = readAndTransformClass(name, inputStream)
inputStream.close()
Some(defineClass(name, bytes, 0, bytes.length))
} catch {
case e: Exception => None
case cnfe: ClassNotFoundException =>
// This is an expected exception due to not being able to find a class.
// Therefore, we'll log it at debug level to avoid polluting the logs with noise
logDebug(s"Class $name not found", cnfe)
None
case e: Exception =>
// This is an unexpected exception; maybe something went wrong while processing the
// class file. Therefore, log an error:
logError(s"Exception while loading class $name", e)
None
} finally {
if (inputStream != null) {
try {
inputStream.close()
} catch {
case e: Exception =>
logError("Exception while closing inputStream", e)
}
}
}
}

Expand Down