|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.spark.deploy.yarn |
| 19 | + |
| 20 | +import java.io.File |
| 21 | + |
| 22 | +import scala.collection.JavaConversions._ |
| 23 | + |
| 24 | +import com.google.common.base.Charsets |
| 25 | +import com.google.common.io.Files |
| 26 | +import org.scalatest.{BeforeAndAfterAll, FunSuite, Matchers} |
| 27 | + |
| 28 | +import org.apache.hadoop.yarn.conf.YarnConfiguration |
| 29 | +import org.apache.hadoop.yarn.server.MiniYARNCluster |
| 30 | + |
| 31 | +import org.apache.spark.{Logging, SparkConf, SparkContext} |
| 32 | +import org.apache.spark.deploy.SparkHadoopUtil |
| 33 | +import org.apache.spark.util.Utils |
| 34 | + |
| 35 | +class YarnClusterSuite extends FunSuite with BeforeAndAfterAll with Matchers { |
| 36 | + |
| 37 | + // log4j configuration for the Yarn containers, so that their output is collected |
| 38 | + // by Yarn instead of trying to overwrite unit-tests.log. |
| 39 | + private val LOG4J_CONF = """ |
| 40 | + |log4j.rootCategory=DEBUG, console |
| 41 | + |log4j.appender.console=org.apache.log4j.ConsoleAppender |
| 42 | + |log4j.appender.console.target=System.err |
| 43 | + |log4j.appender.console.layout=org.apache.log4j.PatternLayout |
| 44 | + |log4j.appender.console.layout.ConversionPattern=%d{yy/MM/dd HH:mm:ss} %p %c{1}: %m%n |
| 45 | + """.stripMargin |
| 46 | + |
| 47 | + private var yarnCluster: MiniYARNCluster = _ |
| 48 | + private var tempDir: File = _ |
| 49 | + private var fakeSparkJar: File = _ |
| 50 | + private var oldConf: Map[String, String] = _ |
| 51 | + |
| 52 | + override def beforeAll() { |
| 53 | + tempDir = Utils.createTempDir() |
| 54 | + |
| 55 | + val logConfDir = new File(tempDir, "log4j") |
| 56 | + logConfDir.mkdir() |
| 57 | + |
| 58 | + val logConfFile = new File(logConfDir, "log4j.properties") |
| 59 | + Files.write(LOG4J_CONF, logConfFile, Charsets.UTF_8) |
| 60 | + |
| 61 | + val childClasspath = logConfDir.getAbsolutePath() + File.pathSeparator + |
| 62 | + sys.props("java.class.path") |
| 63 | + |
| 64 | + oldConf = sys.props.filter { case (k, v) => k.startsWith("spark.") }.toMap |
| 65 | + |
| 66 | + yarnCluster = new MiniYARNCluster(getClass().getName(), 1, 1, 1) |
| 67 | + yarnCluster.init(new YarnConfiguration()) |
| 68 | + yarnCluster.start() |
| 69 | + yarnCluster.getConfig().foreach { e => |
| 70 | + sys.props += ("spark.hadoop." + e.getKey() -> e.getValue()) |
| 71 | + } |
| 72 | + |
| 73 | + fakeSparkJar = File.createTempFile("sparkJar", null, tempDir) |
| 74 | + sys.props += ("spark.yarn.jar" -> ("local:" + fakeSparkJar.getAbsolutePath())) |
| 75 | + sys.props += ("spark.executor.instances" -> "1") |
| 76 | + sys.props += ("spark.driver.extraClassPath" -> childClasspath) |
| 77 | + sys.props += ("spark.executor.extraClassPath" -> childClasspath) |
| 78 | + |
| 79 | + super.beforeAll() |
| 80 | + } |
| 81 | + |
| 82 | + override def afterAll() { |
| 83 | + yarnCluster.stop() |
| 84 | + sys.props.retain { case (k, v) => !k.startsWith("spark.") } |
| 85 | + sys.props ++= oldConf |
| 86 | + super.afterAll() |
| 87 | + } |
| 88 | + |
| 89 | + test("run Spark in yarn-client mode") { |
| 90 | + var result = File.createTempFile("result", null, tempDir) |
| 91 | + YarnClusterDriver.main(Array("yarn-client", result.getAbsolutePath())) |
| 92 | + checkResult(result) |
| 93 | + } |
| 94 | + |
| 95 | + test("run Spark in yarn-cluster mode") { |
| 96 | + val main = YarnClusterDriver.getClass.getName().stripSuffix("$") |
| 97 | + var result = File.createTempFile("result", null, tempDir) |
| 98 | + |
| 99 | + // The Client object will call System.exit() after the job is done, and we don't want |
| 100 | + // that because it messes up the scalatest monitoring. So replicate some of what main() |
| 101 | + // does here. |
| 102 | + val args = Array("--class", main, |
| 103 | + "--jar", "file:" + fakeSparkJar.getAbsolutePath(), |
| 104 | + "--arg", "yarn-cluster", |
| 105 | + "--arg", result.getAbsolutePath(), |
| 106 | + "--num-executors", "1") |
| 107 | + val sparkConf = new SparkConf() |
| 108 | + val yarnConf = SparkHadoopUtil.get.newConfiguration(sparkConf) |
| 109 | + val clientArgs = new ClientArguments(args, sparkConf) |
| 110 | + new Client(clientArgs, yarnConf, sparkConf).run() |
| 111 | + checkResult(result) |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * This is a workaround for an issue with yarn-cluster mode: the Client class will not provide |
| 116 | + * any sort of error when the job process finishes successfully, but the job itself fails. So |
| 117 | + * the tests enforce that something is written to a file after everything is ok to indicate |
| 118 | + * that the job succeeded. |
| 119 | + */ |
| 120 | + private def checkResult(result: File) = { |
| 121 | + var resultString = Files.toString(result, Charsets.UTF_8) |
| 122 | + resultString should be ("success") |
| 123 | + } |
| 124 | + |
| 125 | +} |
| 126 | + |
| 127 | +private object YarnClusterDriver extends Logging with Matchers { |
| 128 | + |
| 129 | + def main(args: Array[String]) = { |
| 130 | + if (args.length != 2) { |
| 131 | + System.err.println( |
| 132 | + s""" |
| 133 | + |Invalid command line: ${args.mkString(" ")} |
| 134 | + | |
| 135 | + |Usage: YarnClusterDriver [master] [result file] |
| 136 | + """.stripMargin) |
| 137 | + System.exit(1) |
| 138 | + } |
| 139 | + |
| 140 | + val sc = new SparkContext(new SparkConf().setMaster(args(0)) |
| 141 | + .setAppName("yarn \"test app\" 'with quotes' and \\back\\slashes and $dollarSigns")) |
| 142 | + val status = new File(args(1)) |
| 143 | + var result = "failure" |
| 144 | + try { |
| 145 | + val data = sc.parallelize(1 to 4, 4).collect().toSet |
| 146 | + data should be (Set(1, 2, 3, 4)) |
| 147 | + result = "success" |
| 148 | + } finally { |
| 149 | + sc.stop() |
| 150 | + Files.write(result, status, Charsets.UTF_8) |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | +} |
0 commit comments