Github user andrewor14 commented on a diff in the pull request:
https://github.com/apache/spark/pull/433#discussion_r11745730
--- Diff:
yarn/common/src/main/scala/org/apache/spark/deploy/yarn/ClientBase.scala ---
@@ -378,55 +378,48 @@ object ClientBase {
val APP_JAR: String = "app.jar"
val LOG4J_PROP: String = "log4j.properties"
- // Based on code from org.apache.hadoop.mapreduce.v2.util.MRApps
- def populateHadoopClasspath(conf: Configuration, env: HashMap[String,
String]) {
- val classpathEntries = Option(conf.getStrings(
- YarnConfiguration.YARN_APPLICATION_CLASSPATH)).getOrElse(
- getDefaultYarnApplicationClasspath())
- for (c <- classpathEntries) {
- YarnSparkHadoopUtil.addToEnvironment(env,
Environment.CLASSPATH.name, c.trim,
- File.pathSeparator)
- }
+ def populateHadoopClasspath(conf: Configuration, env: HashMap[String,
String]) = {
+ val classPathElementsToAdd = getYarnAppClasspath(conf) ++
getMRAppClasspath(conf)
+ addToAppClasspath(env, classPathElementsToAdd)
+ classPathElementsToAdd
+ }
- val mrClasspathEntries = Option(conf.getStrings(
- "mapreduce.application.classpath")).getOrElse(
- getDefaultMRApplicationClasspath())
- if (mrClasspathEntries != null) {
- for (c <- mrClasspathEntries) {
- YarnSparkHadoopUtil.addToEnvironment(env,
Environment.CLASSPATH.name, c.trim,
- File.pathSeparator)
- }
- }
+ protected[yarn] def getYarnAppClasspath(conf: Configuration) =
+ getAppClasspathForKey(YarnConfiguration.YARN_APPLICATION_CLASSPATH,
conf)(getDefaultYarnApplicationClasspath)
+
+ protected[yarn] def getMRAppClasspath(conf: Configuration) =
+ getAppClasspathForKey("mapreduce.application.classpath",
conf)(getDefaultMRApplicationClasspath)
+
+ protected[yarn] def addToAppClasspath(env: HashMap[String, String],
elements : Iterable[String]) {
+ for ( c <- elements )
+ yield (YarnSparkHadoopUtil.addToEnvironment(env,
Environment.CLASSPATH.name, c.trim, File.pathSeparator))
}
- def getDefaultYarnApplicationClasspath(): Array[String] = {
- try {
- val field =
classOf[MRJobConfig].getField("DEFAULT_YARN_APPLICATION_CLASSPATH")
- field.get(null).asInstanceOf[Array[String]]
- } catch {
- case err: NoSuchFieldError => null
- case err: NoSuchFieldException => null
+ protected[yarn] def getAppClasspathForKey(key:String, conf:Configuration)
+ (f: => Array[String]) :
Array[String] =
+ Option(conf.getStrings(key)) match {
+ case Some(s) => s
+ case None => f
}
- }
+
+ def getDefaultYarnApplicationClasspath : Array[String] = Try {
+ val field =
classOf[YarnConfiguration].getField("DEFAULT_YARN_APPLICATION_CLASSPATH")
+ field.get(null).asInstanceOf[Array[String]]
+ }.getOrElse(Array.empty[String])
/**
* In Hadoop 0.23, the MR application classpath comes with the YARN
application
* classpath. In Hadoop 2.0, it's an array of Strings, and in 2.2+ it's
a String.
* So we need to use reflection to retrieve it.
*/
- def getDefaultMRApplicationClasspath(): Array[String] = {
- try {
- val field =
classOf[MRJobConfig].getField("DEFAULT_MAPREDUCE_APPLICATION_CLASSPATH")
- if (field.getType == classOf[String]) {
+ def getDefaultMRApplicationClasspath : Array[String] = Try {
+ val field =
classOf[MRJobConfig].getField("DEFAULT_MAPREDUCE_APPLICATION_CLASSPATH")
+ if( field.getType == classOf[String] )
StringUtils.getStrings(field.get(null).asInstanceOf[String])
- } else {
+ else
field.get(null).asInstanceOf[Array[String]]
- }
- } catch {
- case err: NoSuchFieldError => null
- case err: NoSuchFieldException => null
- }
- }
+ }.getOrElse(Array.empty[String])
--- End diff --
I prefer the old try-catch to the scala.util.Try, which hides the specific
exceptions you catch
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---