Spark定製班第19課:Spark Streaming架構設計和運行機制大總結

本期內容:
1. Spark Streaming中的架構設計和運行機制
2. Spark Streaming的深度思考

1. Spark Streaming中的架構設計和運行機制

前面討論過,Spark Streaming就是RDD加上了時間維度。RDD模板是DStream,DAG的模板是DStreamGraph。
但實際上DStream上的操作和RDD上的操作並不是一一對應的。RDD中的一些操作,在DStream裏沒有。
DStream、DStreamGraph是幻象,只是在時間維度下爲RDD的時間週期管理提供方便而已。
這個時間維度,說到底,就是用到了定時器。生成Block,生成Job,都用了定時器。

BlockGenerator中的RecurringTimer是成員blockIntervalTimer 。定時產生Block。
BlockGenerator:

  private val blockIntervalTimer =
    new RecurringTimer(clock, blockIntervalMs, updateCurrentBuffer, "BlockGenerator")

BlockGenerator.updateCurrentBuffer:

  /** Change the buffer to which single records are added to. */
  private def updateCurrentBuffer(time: Long): Unit = {
    try {
      var newBlock: Block = null
      synchronized {
        if (currentBuffer.nonEmpty) {
          val newBlockBuffer = currentBuffer
          currentBuffer = new ArrayBuffer[Any]
          val blockId = StreamBlockId(receiverId, time - blockIntervalMs)
          listener.onGenerateBlock(blockId)
          newBlock = new Block(blockId, newBlockBuffer)
        }
      }

      if (newBlock != null) {
        blocksForPushing.put(newBlock)  // put is blocking when queue is full
      }
    } catch {
      case ie: InterruptedException =>
        logInfo("Block updating timer thread was interrupted")
      case e: Exception =>
        reportError("Error in block updating thread", e)
    }
  }

定期產生Block。具體流程參考第10課。

JobGenerator中的RecurringTimer是成員timer。
JobGenerator:

  private val timer = new RecurringTimer(clock, ssc.graph.batchDuration.milliseconds,
    longTime => eventLoop.post(GenerateJobs(new Time(longTime))), "JobGenerator")

定期的發送生成Job的消息。具體流程參考第7課。

2. Spark Streaming的深度思考

Spark Streaming的本質,就是在RRD的基礎上,增加了Timer,Timer不斷觸發,周而復始的產生Block,產生Job,處理數據。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章