|
| 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 |
| 19 | + |
| 20 | +import scala.concurrent.duration._ |
| 21 | +import scala.language.postfixOps |
| 22 | + |
| 23 | +import org.scalatest.BeforeAndAfterEach |
| 24 | + |
| 25 | +import org.apache.spark.rdd.{PartitionPruningRDD, RDD} |
| 26 | +import org.apache.spark.scheduler.DAGScheduler |
| 27 | +import org.apache.spark.util.ThreadUtils |
| 28 | + |
| 29 | +/** |
| 30 | + * This test suite covers all the cases that shall fail fast on job submitted that contains one |
| 31 | + * of more barrier stages. |
| 32 | + */ |
| 33 | +class BarrierStageOnSubmittedSuite extends SparkFunSuite with BeforeAndAfterEach |
| 34 | + with LocalSparkContext { |
| 35 | + |
| 36 | + override def beforeEach(): Unit = { |
| 37 | + super.beforeEach() |
| 38 | + |
| 39 | + val conf = new SparkConf() |
| 40 | + .setMaster("local[4]") |
| 41 | + .setAppName("test") |
| 42 | + sc = new SparkContext(conf) |
| 43 | + } |
| 44 | + |
| 45 | + private def testSubmitJob( |
| 46 | + sc: SparkContext, |
| 47 | + rdd: RDD[Int], |
| 48 | + partitions: Option[Seq[Int]] = None, |
| 49 | + message: String): Unit = { |
| 50 | + val futureAction = sc.submitJob( |
| 51 | + rdd, |
| 52 | + (iter: Iterator[Int]) => iter.toArray, |
| 53 | + partitions.getOrElse(0 until rdd.partitions.length), |
| 54 | + { case (_, _) => return }: (Int, Array[Int]) => Unit, |
| 55 | + { return } |
| 56 | + ) |
| 57 | + |
| 58 | + val error = intercept[SparkException] { |
| 59 | + ThreadUtils.awaitResult(futureAction, 5 seconds) |
| 60 | + }.getCause.getMessage |
| 61 | + assert(error.contains(message)) |
| 62 | + } |
| 63 | + |
| 64 | + test("submit a barrier ResultStage that contains PartitionPruningRDD") { |
| 65 | + val prunedRdd = new PartitionPruningRDD(sc.parallelize(1 to 10, 4), index => index > 1) |
| 66 | + val rdd = prunedRdd |
| 67 | + .barrier() |
| 68 | + .mapPartitions((iter, context) => iter) |
| 69 | + testSubmitJob(sc, rdd, |
| 70 | + message = DAGScheduler.ERROR_MESSAGE_RUN_BARRIER_WITH_UNSUPPORTED_RDD_CHAIN_PATTERN) |
| 71 | + } |
| 72 | + |
| 73 | + test("submit a barrier ShuffleMapStage that contains PartitionPruningRDD") { |
| 74 | + val prunedRdd = new PartitionPruningRDD(sc.parallelize(1 to 10, 4), index => index > 1) |
| 75 | + val rdd = prunedRdd |
| 76 | + .barrier() |
| 77 | + .mapPartitions((iter, context) => iter) |
| 78 | + .repartition(2) |
| 79 | + .map(x => x + 1) |
| 80 | + testSubmitJob(sc, rdd, |
| 81 | + message = DAGScheduler.ERROR_MESSAGE_RUN_BARRIER_WITH_UNSUPPORTED_RDD_CHAIN_PATTERN) |
| 82 | + } |
| 83 | + |
| 84 | + test("submit a barrier stage that doesn't contain PartitionPruningRDD") { |
| 85 | + val prunedRdd = new PartitionPruningRDD(sc.parallelize(1 to 10, 4), index => index > 1) |
| 86 | + val rdd = prunedRdd |
| 87 | + .repartition(2) |
| 88 | + .barrier() |
| 89 | + .mapPartitions((iter, context) => iter) |
| 90 | + // Should be able to submit job and run successfully. |
| 91 | + val result = rdd.collect().sorted |
| 92 | + assert(result === Seq(6, 7, 8, 9, 10)) |
| 93 | + } |
| 94 | + |
| 95 | + test("submit a barrier stage with partial partitions") { |
| 96 | + val rdd = sc.parallelize(1 to 10, 4) |
| 97 | + .barrier() |
| 98 | + .mapPartitions((iter, context) => iter) |
| 99 | + testSubmitJob(sc, rdd, Some(Seq(1, 3)), |
| 100 | + message = DAGScheduler.ERROR_MESSAGE_RUN_BARRIER_WITH_UNSUPPORTED_RDD_CHAIN_PATTERN) |
| 101 | + } |
| 102 | + |
| 103 | + test("submit a barrier stage with union()") { |
| 104 | + val rdd1 = sc.parallelize(1 to 10, 2) |
| 105 | + .barrier() |
| 106 | + .mapPartitions((iter, context) => iter) |
| 107 | + val rdd2 = sc.parallelize(1 to 20, 2) |
| 108 | + val rdd3 = rdd1 |
| 109 | + .union(rdd2) |
| 110 | + .map(x => x * 2) |
| 111 | + // Fail the job on submit because the barrier RDD (rdd1) may be not assigned Task 0. |
| 112 | + testSubmitJob(sc, rdd3, |
| 113 | + message = DAGScheduler.ERROR_MESSAGE_RUN_BARRIER_WITH_UNSUPPORTED_RDD_CHAIN_PATTERN) |
| 114 | + } |
| 115 | + |
| 116 | + test("submit a barrier stage with coalesce()") { |
| 117 | + val rdd = sc.parallelize(1 to 10, 4) |
| 118 | + .barrier() |
| 119 | + .mapPartitions((iter, context) => iter) |
| 120 | + .coalesce(1) |
| 121 | + // Fail the job on submit because the barrier RDD requires to run on 4 tasks, but the stage |
| 122 | + // only launches 1 task. |
| 123 | + testSubmitJob(sc, rdd, |
| 124 | + message = DAGScheduler.ERROR_MESSAGE_RUN_BARRIER_WITH_UNSUPPORTED_RDD_CHAIN_PATTERN) |
| 125 | + } |
| 126 | + |
| 127 | + test("submit a barrier stage that contains an RDD that depends on multiple barrier RDDs") { |
| 128 | + val rdd1 = sc.parallelize(1 to 10, 4) |
| 129 | + .barrier() |
| 130 | + .mapPartitions((iter, context) => iter) |
| 131 | + val rdd2 = sc.parallelize(11 to 20, 4) |
| 132 | + .barrier() |
| 133 | + .mapPartitions((iter, context) => iter) |
| 134 | + val rdd3 = rdd1 |
| 135 | + .zip(rdd2) |
| 136 | + .map(x => x._1 + x._2) |
| 137 | + testSubmitJob(sc, rdd3, |
| 138 | + message = DAGScheduler.ERROR_MESSAGE_RUN_BARRIER_WITH_UNSUPPORTED_RDD_CHAIN_PATTERN) |
| 139 | + } |
| 140 | + |
| 141 | + test("submit a barrier stage with zip()") { |
| 142 | + val rdd1 = sc.parallelize(1 to 10, 4) |
| 143 | + .barrier() |
| 144 | + .mapPartitions((iter, context) => iter) |
| 145 | + val rdd2 = sc.parallelize(11 to 20, 4) |
| 146 | + val rdd3 = rdd1 |
| 147 | + .zip(rdd2) |
| 148 | + .map(x => x._1 + x._2) |
| 149 | + // Should be able to submit job and run successfully. |
| 150 | + val result = rdd3.collect().sorted |
| 151 | + assert(result === Seq(12, 14, 16, 18, 20, 22, 24, 26, 28, 30)) |
| 152 | + } |
| 153 | +} |
0 commit comments