基于案例贯通Spark Streaming流计算框架运行源码5

先贴下案例源码

import org.apache.spark.SparkConf
import org.apache.spark.streaming.{Durations, StreamingContext}

object StreamingWordCountSelfScala {
  def main(args: Array[String]) {
    val sparkConf = new SparkConf().setMaster("spark://master:7077").setAppName("StreamingWordCountSelfScala")
    val ssc = new StreamingContext(sparkConf, Durations.seconds(5)) // 每5秒收割一次数据
    val lines = ssc.socketTextStream("localhost", 9999) // 监听 本地9999 socket 端口
    val words = lines.flatMap(_.split(" ")).map((_, 1)).reduceByKey(_ + _) // flat map 后 reduce
    words.print() // 打印结果
    ssc.start() // 启动
    ssc.awaitTermination()
    ssc.stop(true)
  }
}

 

上文已经从源码分析了ReceiverTracker的实例化过程,下一步是

// JobScheduler.scala line 82
receiverTracker.start()

 

还是以源码为基础,正如王家林老师所说:

一切问题都源于源码,一切问题也都终结于源码

数据正式流起来。

这里注册了一个Endpoint。RPC通信可以看之前的博客。

// ReceiverTracker.scala line 149
def start(): Unit = synchronized {
  if (isTrackerStarted) {
    throw new SparkException("ReceiverTracker already started") // 如果已启动就抛异常
  }

  if (!receiverInputStreams.isEmpty) {
    endpoint = ssc.env.rpcEnv.setupEndpoint(
      "ReceiverTracker", new ReceiverTrackerEndpoint(ssc.env.rpcEnv))
    if (!skipReceiverLaunch) launchReceivers()
    logInfo("ReceiverTracker started")
    trackerState = Started
  }
}

 

  1. 调用了runDummySparkJob()

    1. 从注释上看出,是为了确认所有的slave都注册了。从而尽量避免receiver在相同的节点上。

      如果共10个slave,此时才注册了3个,那么就只会再3个中分配receiver,相比分配在10个slave中,肯定是10个更均匀。

  2. 获取所有的ReceiverInputDStream的Receiver。将ReceiverInputDStream的ID赋值给Receiver,作为ReceiverID

  3. 发送消息,指示启动所有的Receiver。战斗正式拉开序幕。

private def launchReceivers(): Unit = {
  val receivers = receiverInputStreams.map(nis => {
    val rcvr = nis.getReceiver()
    rcvr.setReceiverId(nis.id)
    rcvr
  })

  runDummySparkJob() // 运行一个虚拟的SparkJob

  logInfo("Starting " + receivers.length + " receivers")
  endpoint.send(StartAllReceivers(receivers))
}

 

runDummySparkJob

// ReceiverTracker.scala line 402
/**
 * Run the dummy Spark job to ensure that all slaves have registered. This avoids all the
 * receivers to be scheduled on the same node.
 *
 * TODO Should poll the executor number and wait for executors according to
 * "spark.scheduler.minRegisteredResourcesRatio" and
 * "spark.scheduler.maxRegisteredResourcesWaitingTime" rather than running a dummy job.
 */
private def runDummySparkJob(): Unit = {
  if (!ssc.sparkContext.isLocal) {
    ssc.sparkContext.makeRDD(1 to 50, 50).map(x => (x, 1)).reduceByKey(_ + _, 20).collect()
  }
  assert(getExecutors.nonEmpty)
}

 

case class StartAllReceivers的定义。注释说明,首次启动receiver时发送

// ReceiverTracker.scala line 76
/**
 * This message is sent to ReceiverTrackerEndpoint when we start to launch Spark jobs for receivers
 * at the first time.
 */
private[streaming] case class StartAllReceivers(receiver: Seq[Receiver[_]])
  extends ReceiverTrackerLocalMessage

 

消息接收方

  1. 按照调度策略匹配最优的receiver和executor

  2. 按照最新的receiver和executor匹配数据,更新数据结构

  3. 启动receiver

// ReceiverTracker.scala line 447
override def receive: PartialFunction[Any, Unit] = {
  // Local messages
  case StartAllReceivers(receivers) =>
    val scheduledLocations = schedulingPolicy.scheduleReceivers(receivers, getExecutors)
    for (receiver <- receivers) {
      val executors = scheduledLocations(receiver.streamId)
      updateReceiverScheduledExecutors(receiver.streamId, executors)
      receiverPreferredLocations(receiver.streamId) = receiver.preferredLocation
      startReceiver(receiver, executors)
    }
  ...
  // 其他case class 逻辑
}

 

先看调度策略,具体算法后续细节。我们先从注释中了解。

// ReceiverSchedulingPolicy.scala line 59
/**
 * Try our best to schedule receivers with evenly distributed. However, if the
 * `preferredLocation`s of receivers are not even, we may not be able to schedule them evenly
 * because we have to respect them.
 * 尽量均匀的分布receiver。若数据本地性不是均匀的,则偏向数据本地性。
 * Here is the approach to schedule executors:
 * <ol>
 *   <li>First, schedule all the receivers with preferred locations (hosts), evenly among the
 *       executors running on those host.</li>
 *   <li>Then, schedule all other receivers evenly among all the executors such that overall
 *       distribution over all the receivers is even.</li>
 * </ol>
 * 本地性优先的receiver优先均匀的分配到优先的host。剩余的再均匀的分配到全部的host
 * This method is called when we start to launch receivers at the first time.
 *
 * @return a map for receivers and their scheduled locations
 */
def scheduleReceivers(
    receivers: Seq[Receiver[_]],
    executors: Seq[ExecutorCacheTaskLocation]): Map[Int, Seq[TaskLocation]] = {
 ...   
}

 

startReceiver

  1. 将配置文件序列化

  2. 按照Receiver的本地性,将Receiver转变成RDD的数据源。因为Receiver都是实现序列化接口的。

  3. 创建在worker中启动receiver的函数,此方法暂时还未使用到,且慢分析

  4. 提交Spark Job

// ReceiverTracker.scala line 545
/**
 * Start a receiver along with its scheduled executors
 */
private def startReceiver(
    receiver: Receiver[_],
    scheduledLocations: Seq[TaskLocation]): Unit = {
  def shouldStartReceiver: Boolean = {
    // It's okay to start when trackerState is Initialized or Started
    !(isTrackerStopping || isTrackerStopped)
  }

  val receiverId = receiver.streamId
  if (!shouldStartReceiver) {
    onReceiverJobFinish(receiverId)
    return
  }

  val checkpointDirOption = Option(ssc.checkpointDir)
  val serializableHadoopConf =
    new SerializableConfiguration(ssc.sparkContext.hadoopConfiguration) // 将配置序列化

  // Function to start the receiver on the worker node
  val startReceiverFunc: Iterator[Receiver[_]] => Unit =
    (iterator: Iterator[Receiver[_]]) => {
      if (!iterator.hasNext) {
        throw new SparkException(
          "Could not start receiver as object not found.")
      }
      if (TaskContext.get().attemptNumber() == 0) {
        val receiver = iterator.next()
        assert(iterator.hasNext == false)
        val supervisor = new ReceiverSupervisorImpl(
          receiver, SparkEnv.get, serializableHadoopConf.value, checkpointDirOption)
        supervisor.start()
        supervisor.awaitTermination()
      } else {
        // It's restarted by TaskScheduler, but we want to reschedule it again. So exit it.
      }
    }

  // Create the RDD using the scheduledLocations to run the receiver in a Spark job
  val receiverRDD: RDD[Receiver[_]] =
    if (scheduledLocations.isEmpty) {
      ssc.sc.makeRDD(Seq(receiver), 1)
    } else {
      val preferredLocations = scheduledLocations.map(_.toString).distinct
      ssc.sc.makeRDD(Seq(receiver -> preferredLocations))
    }
  receiverRDD.setName(s"Receiver $receiverId")
  ssc.sparkContext.setJobDescription(s"Streaming job running receiver $receiverId")
  ssc.sparkContext.setCallSite(Option(ssc.getStartSite()).getOrElse(Utils.getCallSite()))

  val future = ssc.sparkContext.submitJob[Receiver[_], Unit, Unit](
    receiverRDD, startReceiverFunc, Seq(0), (_, _) => Unit, ())
  // We will keep restarting the receiver job until ReceiverTracker is stopped
  future.onComplete {
    case Success(_) =>
      if (!shouldStartReceiver) {
        onReceiverJobFinish(receiverId)
      } else {
        logInfo(s"Restarting Receiver $receiverId")
        self.send(RestartReceiver(receiver))
      }
    case Failure(e) =>
      if (!shouldStartReceiver) {
        onReceiverJobFinish(receiverId)
      } else {
        logError("Receiver has been stopped. Try to restart it.", e)
        logInfo(s"Restarting Receiver $receiverId")
        self.send(RestartReceiver(receiver))
      }
  }(submitJobThreadPool)
  logInfo(s"Receiver ${receiver.streamId} started")
}

 

看下提交Spark Job,其中第二个参数传入的方法,此方法接收TaskContext类型和一个迭代器,方法体里就是调用startReceiverFunc(迭代器对象)。至于为什么TaskContext对象传进去,但是没使用。那只是为了匹配此方法签名而已。不用多考虑

// SparkContext.scala line 1980
def submitJob[T, U, R](
    rdd: RDD[T],
    processPartition: Iterator[T] => U, // 此参数为 startReceiverFunc
    partitions: Seq[Int],
    resultHandler: (Int, U) => Unit,
    resultFunc: => R): SimpleFutureAction[R] =
{
  assertNotStopped()
  val cleanF = clean(processPartition) // 加工
  val callSite = getCallSite
  val waiter = dagScheduler.submitJob(
    rdd,
    (context: TaskContext, iter: Iterator[T]) => cleanF(iter), // 传入
    partitions,
    callSite,
    resultHandler,
    localProperties.get)
  new SimpleFutureAction(waiter, resultFunc)
}

 

至此,Receiver已经以RDD的数据源的形式提交给Spark集群了。

不过此时,还是没有启动receiver。由于篇幅有限,下一篇细细分析。

转载于:https://my.oschina.net/corleone/blog/671533

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值