aboutsummaryrefslogtreecommitdiff
path: root/yarn/src/main/scala/org/apache/spark/scheduler/cluster/YarnClientSchedulerBackend.scala
blob: 350fc760a428a5733eeeadf474b341f9a2341ccb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.spark.scheduler.cluster

import org.apache.hadoop.yarn.api.records.{ApplicationId, YarnApplicationState}
import org.apache.spark.{SparkException, Logging, SparkContext}
import org.apache.spark.deploy.yarn.{Client, ClientArguments}
import org.apache.spark.scheduler.TaskSchedulerImpl

private[spark] class YarnClientSchedulerBackend(
    scheduler: TaskSchedulerImpl,
    sc: SparkContext)
  extends CoarseGrainedSchedulerBackend(scheduler, sc.env.actorSystem)
  with Logging {

  var client: Client = null
  var appId: ApplicationId = null

  override def start() {
    super.start()

    val defalutWorkerCores = "2"
    val defalutWorkerMemory = "512m"
    val defaultWorkerNumber = "1"

    val userJar = System.getenv("SPARK_YARN_APP_JAR")
    var workerCores = System.getenv("SPARK_WORKER_CORES")
    var workerMemory = System.getenv("SPARK_WORKER_MEMORY")
    var workerNumber = System.getenv("SPARK_WORKER_INSTANCES")

    if (userJar == null)
      throw new SparkException("env SPARK_YARN_APP_JAR is not set")

    if (workerCores == null)
      workerCores = defalutWorkerCores
    if (workerMemory == null)
      workerMemory = defalutWorkerMemory
    if (workerNumber == null)
      workerNumber = defaultWorkerNumber

    val driverHost = System.getProperty("spark.driver.host")
    val driverPort = System.getProperty("spark.driver.port")
    val hostport = driverHost + ":" + driverPort

    val argsArray = Array[String](
      "--class", "notused",
      "--jar", userJar,
      "--args", hostport,
      "--worker-memory", workerMemory,
      "--worker-cores", workerCores,
      "--num-workers", workerNumber,
      "--master-class", "org.apache.spark.deploy.yarn.WorkerLauncher"
    )

    val args = new ClientArguments(argsArray)
    client = new Client(args)
    appId = client.runApp()
    waitForApp()
  }

  def waitForApp() {

    // TODO : need a better way to find out whether the workers are ready or not
    // maybe by resource usage report?
    while(true) {
      val report = client.getApplicationReport(appId)

      logInfo("Application report from ASM: \n" +
        "\t appMasterRpcPort: " + report.getRpcPort() + "\n" +
        "\t appStartTime: " + report.getStartTime() + "\n" +
        "\t yarnAppState: " + report.getYarnApplicationState() + "\n"
      )

      // Ready to go, or already gone.
      val state = report.getYarnApplicationState()
      if (state == YarnApplicationState.RUNNING) {
        return
      } else if (state == YarnApplicationState.FINISHED ||
        state == YarnApplicationState.FAILED ||
        state == YarnApplicationState.KILLED) {
        throw new SparkException("Yarn application already ended," +
          "might be killed or not able to launch application master.")
      }

      Thread.sleep(1000)
    }
  }

  override def stop() {
    super.stop()
    client.stop()
    logInfo("Stoped")
  }

}