本期内容:
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,处理数据。