美文网首页
spark源码阅读——Master&Worker&

spark源码阅读——Master&Worker&

作者: WJL3333 | 来源:发表于2018-07-14 03:42 被阅读25次

    这篇分析一下standalone集群

    启动standalone集群需要做两件事。
    在master机器上./sbin/start-master.sh
    启动一个或多个worker节点并把它注册到master节点上
    ./sbin/start-slave.sh <master-spark-URL>

    那么都分别发生了什么呢?(这里忽略shell脚本的部分,因为我不会看)

    首先看Worker部分,Worker是运行在从节点上的一个java进程,所以直接看main方法

    private[deploy] class Worker(
        override val rpcEnv: RpcEnv,
        webUiPort: Int,
        cores: Int,
        memory: Int,
        masterRpcAddresses: Array[RpcAddress],
        endpointName: String,
        workDirPath: String = null,
        val conf: SparkConf,
        val securityMgr: SecurityManager)
      extends ThreadSafeRpcEndpoint with Logging {
      ...
    
     }
    
    private[deploy] object Worker extends Logging {
      val SYSTEM_NAME = "sparkWorker"
      val ENDPOINT_NAME = "Worker"
    
      def main(argStrings: Array[String]) {
        Utils.initDaemon(log)
        val conf = new SparkConf
        val args = new WorkerArguments(argStrings, conf)
        val rpcEnv = startRpcEnvAndEndpoint(args.host, args.port, args.webUiPort, args.cores,
          args.memory, args.masters, args.workDir, conf = conf)
        rpcEnv.awaitTermination()
      }
    
      def startRpcEnvAndEndpoint(
          host: String,
          port: Int,
          webUiPort: Int,
          cores: Int,
          memory: Int,
          masterUrls: Array[String],
          workDir: String,
          workerNumber: Option[Int] = None,
          conf: SparkConf = new SparkConf): RpcEnv = {
    
        // The LocalSparkCluster runs multiple local sparkWorkerX RPC Environments
        val systemName = SYSTEM_NAME + workerNumber.map(_.toString).getOrElse("")
        val securityMgr = new SecurityManager(conf)
        val rpcEnv = RpcEnv.create(systemName, host, port, conf, securityMgr)
        val masterAddresses = masterUrls.map(RpcAddress.fromSparkURL(_))
        rpcEnv.setupEndpoint(ENDPOINT_NAME, new Worker(rpcEnv, webUiPort, cores, memory,
          masterAddresses, ENDPOINT_NAME, workDir, conf, securityMgr))
        rpcEnv
      }
    
    

    可以看到启动Worker的第一件事就是创建一个RpcEnv并注册了
    Worker对象做为一个 RpcEndpoint
    可以看到Worker这个类继承ThreadSafeRpcEndpoint这个trait,也就具备了发送RPC请求的能力。
    不过这好像跟master没什么关系啊,因为我们知道Worker是需要注册到Master节点上才能组成集群的。
    我们看一下RpcEndpoint.onStart方法。这个方法在一个Endpoint启动时就会被调用

    override def onStart() {
        ...
        createWorkDir()
        shuffleService.startIfEnabled()
        webUi = new WorkerWebUI(this, workDir, webUiPort)
        webUi.bind()
    
        workerWebUiUrl = s"http://$publicAddress:${webUi.boundPort}"
        registerWithMaster()
    
        metricsSystem.registerSource(workerSource)
        metricsSystem.start()
        // Attach the worker metrics servlet handler to the web ui after the metrics system is started.
        metricsSystem.getServletHandlers.foreach(webUi.attachHandler)
      }
    

    这个方法调用了registerWithMaster方法

    private def registerWithMaster() {
        // onDisconnected may be triggered multiple times, so don't attempt registration
        // if there are outstanding registration attempts scheduled.
        registrationRetryTimer match {
          case None =>
            registered = false
            registerMasterFutures = tryRegisterAllMasters()
            connectionAttemptCount = 0
            registrationRetryTimer = Some(forwordMessageScheduler.scheduleAtFixedRate(
              new Runnable {
                override def run(): Unit = Utils.tryLogNonFatalError {
                  Option(self).foreach(_.send(ReregisterWithMaster))
                }
              },
              INITIAL_REGISTRATION_RETRY_INTERVAL_SECONDS,
              INITIAL_REGISTRATION_RETRY_INTERVAL_SECONDS,
              TimeUnit.SECONDS))
          case Some(_) =>
            logInfo("Not spawning another attempt to register with the master, since there is an" +
              " attempt scheduled already.")
        }
      }
    

    这个方法首先尝试与所有的master注册,并且起了一个定时任务不断的重新注册Worker信息给Master

    private def tryRegisterAllMasters(): Array[JFuture[_]] = {
        masterRpcAddresses.map { masterAddress =>
          registerMasterThreadPool.submit(new Runnable {
            override def run(): Unit = {
              try {
                logInfo("Connecting to master " + masterAddress + "...")
                val masterEndpoint = rpcEnv.setupEndpointRef(masterAddress, Master.ENDPOINT_NAME)
                sendRegisterMessageToMaster(masterEndpoint)
              } catch {
                case ie: InterruptedException => // Cancelled
                case NonFatal(e) => logWarning(s"Failed to connect to master $masterAddress", e)
              }
            }
          })
        }
      }
     private def sendRegisterMessageToMaster(masterEndpoint: RpcEndpointRef): Unit = {
        masterEndpoint.send(RegisterWorker(
          workerId,
          host,
          port,
          self,
          cores,
          memory,
          workerWebUiUrl,
          masterEndpoint.address))
      }
    

    这个方法通过命令行传入的master地址信息,创建了一个Master的EndpointRef并发送了RegisterWorker信息给Master
    那么master那边是怎么处理的呢?

    Master 也是主节点上的一个java进程,同样创建了一个RpcEnv

    def startRpcEnvAndEndpoint(
          host: String,
          port: Int,
          webUiPort: Int,
          conf: SparkConf): (RpcEnv, Int, Option[Int]) = {
        val securityMgr = new SecurityManager(conf)
        val rpcEnv = RpcEnv.create(SYSTEM_NAME, host, port, conf, securityMgr)
        val masterEndpoint = rpcEnv.setupEndpoint(ENDPOINT_NAME,
          new Master(rpcEnv, rpcEnv.address, webUiPort, securityMgr, conf))
        val portsResponse = masterEndpoint.askSync[BoundPortsResponse](BoundPortsRequest)
        (rpcEnv, portsResponse.webUIPort, portsResponse.restPort)
      }
    

    我们直接看代码,master收到了注册信息之后会在自己的数据结构中保存当前注册到集群上的Worker信息,并且向Worker发送RegisteredWorker信息

    override def receive: PartialFunction[Any, Unit] = {
        ...
        case RegisterWorker(
          id, workerHost, workerPort, workerRef, cores, memory, workerWebUiUrl, masterAddress) =>
          logInfo("Registering worker %s:%d with %d cores, %s RAM".format(
            workerHost, workerPort, cores, Utils.megabytesToString(memory)))
          if (state == RecoveryState.STANDBY) {
            workerRef.send(MasterInStandby)
          } else if (idToWorker.contains(id)) {
            workerRef.send(RegisterWorkerFailed("Duplicate worker ID"))
          } else {
            val worker = new WorkerInfo(id, workerHost, workerPort, cores, memory,
              workerRef, workerWebUiUrl)
            if (registerWorker(worker)) {
              persistenceEngine.addWorker(worker)
              workerRef.send(RegisteredWorker(self, masterWebUiUrl, masterAddress))
              schedule()
            } else { ... }
          }
         ...
    }
    

    Woker收到RegisteredWorker信息后

    private def handleRegisterResponse(msg: RegisterWorkerResponse): Unit = synchronized {
        msg match {
          case RegisteredWorker(masterRef, masterWebUiUrl, masterAddress) =>
            ...
    
            registered = true
            changeMaster(masterRef, masterWebUiUrl, masterAddress)
            forwordMessageScheduler.scheduleAtFixedRate(new Runnable {
              override def run(): Unit = Utils.tryLogNonFatalError {
                self.send(SendHeartbeat)
              }
            }, 0, HEARTBEAT_MILLIS, TimeUnit.MILLISECONDS)
    
            ....
    
            val execs = executors.values.map { e =>
              new ExecutorDescription(e.appId, e.execId, e.cores, e.state)
            }
            masterRef.send(WorkerLatestState(workerId, execs.toList, drivers.keys.toSeq))
    
          case RegisterWorkerFailed(message) =>
            if (!registered) {
              logError("Worker registration failed: " + message)
              System.exit(1)
            }
    
          case MasterInStandby =>
            // Ignore. Master not yet ready.
        }
      }
    

    启动了一个定时任务一直会发送心跳信息给master
    而且会定时的上报当前Worker节点运行的Executor信息。


    上面有一个部分值得注意,master收到RegisterWorker 信息后 调用了schedule方法
    我们来看一下

    /**
       * Schedule the currently available resources among waiting apps. This method will be called
       * every time a new app joins or resource availability changes.
       */
      private def schedule(): Unit = {
         ...
        // Drivers take strict precedence over executors
        val shuffledAliveWorkers = Random.shuffle(workers.toSeq.filter(_.state == WorkerState.ALIVE))
        val numWorkersAlive = shuffledAliveWorkers.size
        var curPos = 0
        for (driver <- waitingDrivers.toList) { // iterate over a copy of waitingDrivers
          // We assign workers to each waiting driver in a round-robin fashion. For each driver, we
          // start from the last worker that was assigned a driver, and continue onwards until we have
          // explored all alive workers.
          var launched = false
          var numWorkersVisited = 0
          while (numWorkersVisited < numWorkersAlive && !launched) {
            val worker = shuffledAliveWorkers(curPos)
            numWorkersVisited += 1
            if (worker.memoryFree >= driver.desc.mem && worker.coresFree >= driver.desc.cores) {
              launchDriver(worker, driver)
              waitingDrivers -= driver
              launched = true
            }
            curPos = (curPos + 1) % numWorkersAlive
          }
        }
        startExecutorsOnWorkers()
      }
    
    

    这个方法打散了Master保存的Worker信息,并遍历保存等待调度的Driver的队列,如果哪个Worker能运行Driver则启动Driver,并在集群的Worker节点中启动Executor。

    那么问题来了waitingDrivers这个队列是谁往里放的东西呢?

    override def receiveAndReply(context: RpcCallContext): PartialFunction[Any, Unit] = {
        case RequestSubmitDriver(description) =>
          if (state != RecoveryState.ALIVE) { ... } else {
            logInfo("Driver submitted " + description.command.mainClass)
            val driver = createDriver(description)
            persistenceEngine.addDriver(driver)
            waitingDrivers += driver
            drivers.add(driver)
            schedule()
    
            // TODO: It might be good to instead have the submission client poll the master to determine
            //       the current status of the driver. For now it's simply "fire and forget".
    
            context.reply(SubmitDriverResponse(self, true, Some(driver.id),
              s"Driver successfully submitted as ${driver.id}"))
          }
    

    Master 收到了RequestSubmitDriver信息,将Driver放入队列,之后同样调度了schedule方法,方法返回后Driver已经启动了。返回信息。

    那么问题又来了谁发的RequestSubmitDriver信息呢?有一个类叫Client,这个类创建ClientEndpoint时调用了onStart回调,发送了这个信息个Master

    object Client {
      def main(args: Array[String]) {
        // scalastyle:off println
        if (!sys.props.contains("SPARK_SUBMIT")) {
          println("WARNING: This client is deprecated and will be removed in a future version of Spark")
          println("Use ./bin/spark-submit with \"--master spark://host:port\"")
        }
        // scalastyle:on println
    
        val conf = new SparkConf()
        val driverArgs = new ClientArguments(args)
    
        if (!conf.contains("spark.rpc.askTimeout")) {
          conf.set("spark.rpc.askTimeout", "10s")
        }
        Logger.getRootLogger.setLevel(driverArgs.logLevel)
    
        val rpcEnv =
          RpcEnv.create("driverClient", Utils.localHostName(), 0, conf, new SecurityManager(conf))
    
        val masterEndpoints = driverArgs.masters.map(RpcAddress.fromSparkURL).
          map(rpcEnv.setupEndpointRef(_, Master.ENDPOINT_NAME))
        rpcEnv.setupEndpoint("client", new ClientEndpoint(rpcEnv, driverArgs, masterEndpoints, conf))
    
        rpcEnv.awaitTermination()
      }
    }
    

    问题又来了,谁启动了这个类的main方法呢?

    从spark-submit脚本里看,这个脚本启动了org.apache.spark.deploy.SparkSubmit这个类,其中有一部分代码

    if (args.isStandaloneCluster) {
          if (args.useRest) {
            childMainClass = "org.apache.spark.deploy.rest.RestSubmissionClient"
            childArgs += (args.primaryResource, args.mainClass)
          } else {
            // In legacy standalone cluster mode, use Client as a wrapper around the user class
            childMainClass = "org.apache.spark.deploy.Client"
            if (args.supervise) { childArgs += "--supervise" }
            Option(args.driverMemory).foreach { m => childArgs += ("--memory", m) }
            Option(args.driverCores).foreach { c => childArgs += ("--cores", c) }
            childArgs += "launch"
            childArgs += (args.master, args.primaryResource, args.mainClass)
          }
          if (args.childArgs != null) {
            childArgs ++= args.childArgs
          }
        }
    

    之后启动了这个Client类的main方法,将运行Driver上传给了master去调度,来决定用户提交的spark程序在哪个Worker节点上运行。


    我们看一下launchDriver这个方法,schedule方法选好了运行Driver的Worker,会发送LaunchDriver给Worker

    private def launchDriver(worker: WorkerInfo, driver: DriverInfo) {
        logInfo("Launching driver " + driver.id + " on worker " + worker.id)
        worker.addDriver(driver)
        driver.worker = Some(worker)
        worker.endpoint.send(LaunchDriver(driver.id, driver.desc))
        driver.state = DriverState.RUNNING
      }
    

    Worker收到信息之后,创建了一个DriverRunner对象,这个对象会下载用户上传的jar包,并运行shell命令启动一个进程运行org.apache.spark.deploy.workerDriverWrapper这个对象的main方法,这样用户提交的spark程序就运行起来了。


    让我们回到schedule方法的最后,调用了startExecutorsOnWorkers方法。
    这个时候用户程序已经启动起来了,可能会提交Application到集群中,集群收到任务会调度到Worker上,Worker会启动Executor来执行任务。

    private def startExecutorsOnWorkers(): Unit = {
        // Right now this is a very simple FIFO scheduler. We keep trying to fit in the first app
        // in the queue, then the second app, etc.
        for (app <- waitingApps if app.coresLeft > 0) {
          val coresPerExecutor: Option[Int] = app.desc.coresPerExecutor
          // Filter out workers that don't have enough resources to launch an executor
          val usableWorkers = workers.toArray.filter(_.state == WorkerState.ALIVE)
            .filter(worker => worker.memoryFree >= app.desc.memoryPerExecutorMB &&
              worker.coresFree >= coresPerExecutor.getOrElse(1))
            .sortBy(_.coresFree).reverse
          val assignedCores = scheduleExecutorsOnWorkers(app, usableWorkers, spreadOutApps)
    
          // Now that we've decided how many cores to allocate on each worker, let's allocate them
          for (pos <- 0 until usableWorkers.length if assignedCores(pos) > 0) {
            allocateWorkerResourceToExecutors(
              app, assignedCores(pos), coresPerExecutor, usableWorkers(pos))
          }
        }
      }
    
    

    这里先不说waitingApps这个队列里面怎么有内容的。
    计算好了要在哪个Worker上运行Executor,要运行多少个Executor之后。
    调用了launchExecutor方法,分别通知Worker启动Executor,Master改变了自身保存的Application的运行状态,也通知了Driver,ExecutorAdded的信息。

    private def launchExecutor(worker: WorkerInfo, exec: ExecutorDesc): Unit = {
        logInfo("Launching executor " + exec.fullId + " on worker " + worker.id)
        worker.addExecutor(exec)
        worker.endpoint.send(LaunchExecutor(masterUrl,
          exec.application.id, exec.id, exec.application.desc, exec.cores, exec.memory))
        exec.application.driver.send(
          ExecutorAdded(exec.id, worker.id, worker.hostPort, exec.cores, exec.memory))
      }
    

    Worker收到LaunchExecutor信息之后会启动一个org.apache.spark.executor.CoarseGrainedExecutorBackend进程,这样调度在Worker上的Executor进程便启动起来了。随着Spark程序的执行,RDD被拆解成TaskSet会被提交到Executor上,整个spark程序也就运行起来了。

    相关文章

      网友评论

          本文标题:spark源码阅读——Master&Worker&

          本文链接:https://www.haomeiwen.com/subject/dndepftx.html