Skip to content

Commit e4d439c

Browse files
Hemant BhanawatMarcelo Vanzin
authored andcommitted
[SPARK-14729][SCHEDULER] Refactored YARN scheduler creation code to use newly added ExternalClusterManager
## What changes were proposed in this pull request? With the addition of ExternalClusterManager(ECM) interface in PR apache#11723, any cluster manager can now be integrated with Spark. It was suggested in ExternalClusterManager PR that one of the existing cluster managers should start using the new interface to ensure that the API is correct. Ideally, all the existing cluster managers should eventually use the ECM interface but as a first step yarn will now use the ECM interface. This PR refactors YARN code from SparkContext.createTaskScheduler function into YarnClusterManager that implements ECM interface. ## How was this patch tested? Since this is refactoring, no new tests has been added. Existing tests have been run. Basic manual testing with YARN was done too. Author: Hemant Bhanawat <[email protected]> Closes apache#12641 from hbhanawat/yarnClusterMgr.
1 parent 607f503 commit e4d439c

File tree

4 files changed

+58
-67
lines changed

4 files changed

+58
-67
lines changed

core/src/main/scala/org/apache/spark/SparkContext.scala

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2389,53 +2389,6 @@ object SparkContext extends Logging {
23892389
}
23902390
(backend, scheduler)
23912391

2392-
case "yarn" if deployMode == "cluster" =>
2393-
val scheduler = try {
2394-
val clazz = Utils.classForName("org.apache.spark.scheduler.cluster.YarnClusterScheduler")
2395-
val cons = clazz.getConstructor(classOf[SparkContext])
2396-
cons.newInstance(sc).asInstanceOf[TaskSchedulerImpl]
2397-
} catch {
2398-
// TODO: Enumerate the exact reasons why it can fail
2399-
// But irrespective of it, it means we cannot proceed !
2400-
case e: Exception =>
2401-
throw new SparkException("YARN mode not available ?", e)
2402-
}
2403-
val backend = try {
2404-
val clazz =
2405-
Utils.classForName("org.apache.spark.scheduler.cluster.YarnClusterSchedulerBackend")
2406-
val cons = clazz.getConstructor(classOf[TaskSchedulerImpl], classOf[SparkContext])
2407-
cons.newInstance(scheduler, sc).asInstanceOf[CoarseGrainedSchedulerBackend]
2408-
} catch {
2409-
case e: Exception =>
2410-
throw new SparkException("YARN mode not available ?", e)
2411-
}
2412-
scheduler.initialize(backend)
2413-
(backend, scheduler)
2414-
2415-
case "yarn" if deployMode == "client" =>
2416-
val scheduler = try {
2417-
val clazz = Utils.classForName("org.apache.spark.scheduler.cluster.YarnScheduler")
2418-
val cons = clazz.getConstructor(classOf[SparkContext])
2419-
cons.newInstance(sc).asInstanceOf[TaskSchedulerImpl]
2420-
2421-
} catch {
2422-
case e: Exception =>
2423-
throw new SparkException("YARN mode not available ?", e)
2424-
}
2425-
2426-
val backend = try {
2427-
val clazz =
2428-
Utils.classForName("org.apache.spark.scheduler.cluster.YarnClientSchedulerBackend")
2429-
val cons = clazz.getConstructor(classOf[TaskSchedulerImpl], classOf[SparkContext])
2430-
cons.newInstance(scheduler, sc).asInstanceOf[CoarseGrainedSchedulerBackend]
2431-
} catch {
2432-
case e: Exception =>
2433-
throw new SparkException("YARN mode not available ?", e)
2434-
}
2435-
2436-
scheduler.initialize(backend)
2437-
(backend, scheduler)
2438-
24392392
case MESOS_REGEX(mesosUrl) =>
24402393
MesosNativeLibrary.load()
24412394
val scheduler = new TaskSchedulerImpl(sc)
@@ -2464,6 +2417,7 @@ object SparkContext extends Logging {
24642417
cm.initialize(scheduler, backend)
24652418
(backend, scheduler)
24662419
} catch {
2420+
case se: SparkException => throw se
24672421
case NonFatal(e) =>
24682422
throw new SparkException("External scheduler cannot be instantiated", e)
24692423
}

core/src/test/scala/org/apache/spark/SparkContextSchedulerCreationSuite.scala

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -129,26 +129,6 @@ class SparkContextSchedulerCreationSuite
129129
}
130130
}
131131

132-
def testYarn(master: String, deployMode: String, expectedClassName: String) {
133-
try {
134-
val sched = createTaskScheduler(master, deployMode)
135-
assert(sched.getClass === Utils.classForName(expectedClassName))
136-
} catch {
137-
case e: SparkException =>
138-
assert(e.getMessage.contains("YARN mode not available"))
139-
logWarning("YARN not available, could not test actual YARN scheduler creation")
140-
case e: Throwable => fail(e)
141-
}
142-
}
143-
144-
test("yarn-cluster") {
145-
testYarn("yarn", "cluster", "org.apache.spark.scheduler.cluster.YarnClusterScheduler")
146-
}
147-
148-
test("yarn-client") {
149-
testYarn("yarn", "client", "org.apache.spark.scheduler.cluster.YarnScheduler")
150-
}
151-
152132
def testMesos(master: String, expectedClass: Class[_], coarse: Boolean) {
153133
val conf = new SparkConf().set("spark.mesos.coarse", coarse.toString)
154134
try {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.apache.spark.scheduler.cluster.YarnClusterManager
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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.scheduler.cluster
19+
20+
import org.apache.spark.{SparkContext, SparkException}
21+
import org.apache.spark.scheduler.{ExternalClusterManager, SchedulerBackend, TaskScheduler, TaskSchedulerImpl}
22+
23+
/**
24+
* Cluster Manager for creation of Yarn scheduler and backend
25+
*/
26+
private[spark] class YarnClusterManager extends ExternalClusterManager {
27+
28+
override def canCreate(masterURL: String): Boolean = {
29+
masterURL == "yarn"
30+
}
31+
32+
override def createTaskScheduler(sc: SparkContext, masterURL: String): TaskScheduler = {
33+
sc.deployMode match {
34+
case "cluster" => new YarnClusterScheduler(sc)
35+
case "client" => new YarnScheduler(sc)
36+
case _ => throw new SparkException(s"Unknown deploy mode '${sc.deployMode}' for Yarn")
37+
}
38+
}
39+
40+
override def createSchedulerBackend(sc: SparkContext,
41+
masterURL: String,
42+
scheduler: TaskScheduler): SchedulerBackend = {
43+
sc.deployMode match {
44+
case "cluster" =>
45+
new YarnClusterSchedulerBackend(scheduler.asInstanceOf[TaskSchedulerImpl], sc)
46+
case "client" =>
47+
new YarnClientSchedulerBackend(scheduler.asInstanceOf[TaskSchedulerImpl], sc)
48+
case _ =>
49+
throw new SparkException(s"Unknown deploy mode '${sc.deployMode}' for Yarn")
50+
}
51+
}
52+
53+
override def initialize(scheduler: TaskScheduler, backend: SchedulerBackend): Unit = {
54+
scheduler.asInstanceOf[TaskSchedulerImpl].initialize(backend)
55+
}
56+
}

0 commit comments

Comments
 (0)