|
| 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 scala.collection.mutable.{HashMap, HashSet, Set} |
| 21 | + |
| 22 | +import org.apache.hadoop.conf.Configuration |
| 23 | +import org.apache.hadoop.yarn.api.records._ |
| 24 | +import org.apache.hadoop.yarn.client.api.AMRMClient.ContainerRequest |
| 25 | +import org.mockito.Mockito._ |
| 26 | + |
| 27 | +import org.apache.spark.{SparkConf, SparkFunSuite} |
| 28 | + |
| 29 | +class LocalityPlacementStrategySuite extends SparkFunSuite { |
| 30 | + |
| 31 | + test("handle large number of containers and tasks (SPARK-18750)") { |
| 32 | + // Run the test in a thread with a small stack size, since the original issue |
| 33 | + // surfaced as a StackOverflowError. |
| 34 | + var error: Throwable = null |
| 35 | + |
| 36 | + val runnable = new Runnable() { |
| 37 | + override def run(): Unit = try { |
| 38 | + runTest() |
| 39 | + } catch { |
| 40 | + case e: Throwable => error = e |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + val thread = new Thread(new ThreadGroup("test"), runnable, "test-thread", 32 * 1024) |
| 45 | + thread.start() |
| 46 | + thread.join() |
| 47 | + |
| 48 | + assert(error === null) |
| 49 | + } |
| 50 | + |
| 51 | + private def runTest(): Unit = { |
| 52 | + val resource = Resource.newInstance(8 * 1024, 4) |
| 53 | + val strategy = new LocalityPreferredContainerPlacementStrategy(new SparkConf(), |
| 54 | + new Configuration(), resource) |
| 55 | + |
| 56 | + val totalTasks = 32 * 1024 |
| 57 | + val totalContainers = totalTasks / 16 |
| 58 | + val totalHosts = totalContainers / 16 |
| 59 | + |
| 60 | + val hosts = (1 to totalHosts).map { i => (s"host_$i", totalTasks % i) }.toMap |
| 61 | + val containers = (1 to totalContainers).map { i => |
| 62 | + ContainerId.fromString(s"container_12345678_0001_01_$i") |
| 63 | + } |
| 64 | + val count = containers.size / hosts.size / 2 |
| 65 | + |
| 66 | + val hostToContainerMap = new HashMap[String, Set[ContainerId]]() |
| 67 | + hosts.keys.take(hosts.size / 2).zipWithIndex.foreach { case (host, i) => |
| 68 | + val hostContainers = new HashSet[ContainerId]() |
| 69 | + containers.drop(count * i).take(i).foreach { c => hostContainers += c } |
| 70 | + hostToContainerMap(host) = hostContainers |
| 71 | + } |
| 72 | + |
| 73 | + strategy.localityOfRequestedContainers(containers.size * 2, totalTasks, hosts, |
| 74 | + hostToContainerMap, Nil) |
| 75 | + } |
| 76 | + |
| 77 | +} |
0 commit comments