-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-6209] Clean up connections in ExecutorClassLoader after failing to load classes #4935
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
@@ -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 | ||
|
|
||
|
|
@@ -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) { | ||
| connection.disconnect() | ||
| throw new ClassNotFoundException(s"Class file not found at URL $url") | ||
| } else { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If a class couldn't be found, then this would throw |
||
| 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) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 aHEADrequest? I know, that might require libraries.