|
| 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 | +import scala.collection.mutable.HashMap |
| 24 | + |
| 25 | +import com.google.common.base.Charsets |
| 26 | +import com.google.common.io.Files |
| 27 | +import org.scalatest.{BeforeAndAfterAll, FunSuite, Matchers} |
| 28 | + |
| 29 | +import org.apache.hadoop.yarn.conf.YarnConfiguration |
| 30 | +import org.apache.hadoop.yarn.server.MiniYARNCluster |
| 31 | + |
| 32 | +import org.apache.spark.{Logging, SparkConf, SparkContext} |
| 33 | +import org.apache.spark.deploy.SparkHadoopUtil |
| 34 | +import org.apache.spark.util.Utils |
| 35 | + |
| 36 | +class YarnClusterSuite extends FunSuite with BeforeAndAfterAll with Matchers { |
| 37 | + |
| 38 | + private val oldConf = new HashMap[String, String]() |
| 39 | + private var yarnCluster: MiniYARNCluster = _ |
| 40 | + private var tempDir: File = _ |
| 41 | + private var fakeSparkJar: File = _ |
| 42 | + |
| 43 | + override def beforeAll() { |
| 44 | + tempDir = Utils.createTempDir() |
| 45 | + |
| 46 | + yarnCluster = new MiniYARNCluster(getClass().getName(), 1, 1, 1, 1, false) |
| 47 | + yarnCluster.init(new YarnConfiguration()) |
| 48 | + yarnCluster.start() |
| 49 | + |
| 50 | + val sysProps = sys.props.map { case (k, v) => (k, v) } |
| 51 | + sysProps.foreach { case (k, v) => |
| 52 | + if (k.startsWith("spark.")) { |
| 53 | + oldConf += (k -> v) |
| 54 | + sys.props -= k |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + yarnCluster.getConfig().foreach { e => |
| 59 | + sys.props += ("spark.hadoop." + e.getKey() -> e.getValue()) |
| 60 | + } |
| 61 | + |
| 62 | + fakeSparkJar = File.createTempFile("sparkJar", null, tempDir) |
| 63 | + sys.props += ("spark.yarn.jar" -> ("local:" + fakeSparkJar.getAbsolutePath())) |
| 64 | + sys.props += ("spark.executor.instances" -> "1") |
| 65 | + sys.props += ("spark.driver.extraClassPath" -> sys.props("java.class.path")) |
| 66 | + sys.props += ("spark.executor.extraClassPath" -> sys.props("java.class.path")) |
| 67 | + |
| 68 | + super.beforeAll() |
| 69 | + } |
| 70 | + |
| 71 | + override def afterAll() { |
| 72 | + yarnCluster.stop() |
| 73 | + |
| 74 | + val sysProps = sys.props.map { case (k, v) => (k, v) } |
| 75 | + sysProps.foreach { case (k, v) => |
| 76 | + if (k.startsWith("spark.")) { |
| 77 | + sys.props -= k |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + oldConf.foreach { case (k, v) => sys.props += (k -> v) } |
| 82 | + |
| 83 | + super.afterAll() |
| 84 | + } |
| 85 | + |
| 86 | + test("run Spark in yarn-client mode") { |
| 87 | + var result = File.createTempFile("result", null, tempDir) |
| 88 | + YarnClusterDriver.main(Array("yarn-client", result.getAbsolutePath())) |
| 89 | + checkResult(result) |
| 90 | + } |
| 91 | + |
| 92 | + test("run Spark in yarn-cluster mode") { |
| 93 | + val main = YarnClusterDriver.getClass.getName().stripSuffix("$") |
| 94 | + var result = File.createTempFile("result", null, tempDir) |
| 95 | + |
| 96 | + // The Client object will call System.exit() after the job is done, and we don't want |
| 97 | + // that because it messes up the scalatest monitoring. So replicate some of what main() |
| 98 | + // does here. |
| 99 | + val args = Array("--class", main, |
| 100 | + "--jar", "file:" + fakeSparkJar.getAbsolutePath(), |
| 101 | + "--arg", "yarn-cluster", |
| 102 | + "--arg", result.getAbsolutePath(), |
| 103 | + "--num-executors", "4") |
| 104 | + val sparkConf = new SparkConf() |
| 105 | + val yarnConf = SparkHadoopUtil.get.newConfiguration(sparkConf) |
| 106 | + val clientArgs = new ClientArguments(args, sparkConf) |
| 107 | + new Client(clientArgs, yarnConf, sparkConf).run() |
| 108 | + checkResult(result) |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * This is a workaround for an issue with yarn-cluster mode: the Client class will not provide |
| 113 | + * any sort of error when the job process finishes successfully, but the job itself fails. So |
| 114 | + * the tests enforce that something is written to a file after everything is ok to indicate |
| 115 | + * that the job succeeded. |
| 116 | + */ |
| 117 | + private def checkResult(result: File) = { |
| 118 | + var resultString = Files.toString(result, Charsets.UTF_8) |
| 119 | + resultString should be ("success") |
| 120 | + } |
| 121 | + |
| 122 | +} |
| 123 | + |
| 124 | +private object YarnClusterDriver extends Logging with Matchers { |
| 125 | + |
| 126 | + def main(args: Array[String]) = { |
| 127 | + val sc = new SparkContext(new SparkConf().setMaster(args(0)) |
| 128 | + .setAppName("yarn \"test app\" 'with quotes'")) |
| 129 | + val status = new File(args(1)) |
| 130 | + var result = "failure" |
| 131 | + try { |
| 132 | + val data = sc.parallelize(1 to 4).map(i => i).collect().toSet |
| 133 | + data should be (Set(1, 2, 3, 4)) |
| 134 | + result = "success" |
| 135 | + } finally { |
| 136 | + sc.stop() |
| 137 | + Files.write(result, status, Charsets.UTF_8) |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | +} |
0 commit comments