Spark源码分析 – SparkContext 中的例子, 只分析到sc.runJob

那么最终是怎么执行的? 通过DAGScheduler切分成Stage, 封装成taskset, 提交给TaskScheduler, 然后等待调度, 最终到Executor上执行

val sc = new SparkContext(……)
val textFile = sc.textFile("README.md")
textFile.filter(line => line.contains("Spark")).count()

这是一个比较简单的没有shuffle的例子, 看看在Executor上是如何被执行的

首先这个job只有一个stage, 所以只会产生resultTask

最关键的执行语句,

func(context, rdd.iterator(split, context))

对于这个例子, func就是最终产生结果的count(), 而rdd就是count前最后一个rdd, 即filter产生的rdd

可以看到Spark中rdd的执行, 不是从前往后, 而是从后往前推的, 为什么? 因为需要考虑cache和checkpoint

所以对于stage只会保留最后一个rdd, 其他的rdd通过dep去反推, 这里调用rdd.iterator来读取最后一个rdd

 

我可以说iterator是spark中最为核心的一个function吗:-)

  final def iterator(split: Partition, context: TaskContext): Iterator[T] = {
if (storageLevel != StorageLevel.NONE) {
SparkEnv.get.cacheManager.getOrCompute(this, split, context, storageLevel)
} else {
computeOrReadCheckpoint(split, context)
}
}

如果结果被cache在memory或disk中, 则调用cacheManager.getOrCompute来读取, 否则直接从checkpoint读或compute

通过CacheManager来完成从cache中读取数据, 或重新compute数据并且完成cache的过程

private[spark] class CacheManager(blockManager: BlockManager) extends Logging {
private val loading = new HashSet[String] /** Gets or computes an RDD split. Used by RDD.iterator() when an RDD is cached. */
def getOrCompute[T](rdd: RDD[T], split: Partition, context: TaskContext, storageLevel: StorageLevel)
: Iterator[T] = {
val key = "rdd_%d_%d".format(rdd.id, split.index)
blockManager.get(key) match { // 从blockManager中获取cached值
case Some(cachedValues) => // 从blockManager读到数据, 说明之前cache过, 直接返回即可
// Partition is in cache, so just return its values
return cachedValues.asInstanceOf[Iterator[T]] case None => // 没有读到数据说明没有cache过,需要重新load(compute或读cp)
// Mark the split as loading (unless someone else marks it first)
loading.synchronized { // 防止多次load相同的rdd, 加锁
if (loading.contains(key)) {
while (loading.contains(key)) {
try {loading.wait()} catch {case _ : Throwable =>} // 如果已经在loading, 只需要wait
}
// See whether someone else has successfully loaded it. The main way this would fail
// is for the RDD-level cache eviction policy if someone else has loaded the same RDD
// partition but we didn't want to make space for it. However, that case is unlikely
// because it's unlikely that two threads would work on the same RDD partition. One
// downside of the current code is that threads wait serially if this does happen.
blockManager.get(key) match {
case Some(values) =>
return values.asInstanceOf[Iterator[T]]
case None =>
logInfo("Whoever was loading " + key + " failed; we'll try it ourselves")
loading.add(key)
}
} else {
loading.add(key) // 记录当前key, 开始loading
}
}
try {
// If we got here, we have to load the split
logInfo("Computing partition " + split) // loading的过程,就是读cp或重新compute
val computedValues = rdd.computeOrReadCheckpoint(split, context) // compute的结果是iterator, 何处遍历产生真实数据?
// Persist the result, so long as the task is not running locally
if (context.runningLocally) { return computedValues }
val elements = new ArrayBuffer[Any]
elements ++= computedValues // ++会触发iterator的遍历产生data放到elements中
blockManager.put(key, elements, storageLevel, true) // 对新产生的数据经行cache, 调用blockManager.put
return elements.iterator.asInstanceOf[Iterator[T]]
} finally {
loading.synchronized {
loading.remove(key)
loading.notifyAll()
}
}
}
}
}

 

Task执行的结果, 如何传到DAGScheduler

task执行的结果value, 参考Spark 源码分析 -- Task

对于ResultTask是计算的值,比如count值,

对于ShuffleTask为MapStatus(blockManager.blockManagerId, compressedSizes), 其中compressedSizes所有shuffle buckets写到文件中的data size

//TaskRunner
val value = task.run(taskId.toInt)
val result = new TaskResult(value, accumUpdates, task.metrics.getOrElse(null))
context.statusUpdate(taskId, TaskState.FINISHED, serializedResult) //context,StandaloneExecutorBackend //StandaloneExecutorBackend.statusUpdate
driver ! StatusUpdate(executorId, taskId, state, data) //DriverActor.StatusUpdate
scheduler.statusUpdate(taskId, state, data.value) //ClusterScheduler.statusUpdate
var taskSetToUpdate: Option[TaskSetManager] = None
taskSetToUpdate.get.statusUpdate(tid, state, serializedData) //ClusterTaskSetManager.statusUpdate
case TaskState.FINISHED =>
taskFinished(tid, state, serializedData) //ClusterTaskSetManager.taskFinished
val result = ser.deserialize[TaskResult[_]](serializedData)
result.metrics.resultSize = serializedData.limit()
sched.listener.taskEnded(tasks(index), Success, result.value, result.accumUpdates, info, result.metrics)
//tasks = taskSet.tasks
//info为TaskInfo
class TaskInfo(
val taskId: Long,
val index: Int,
val launchTime: Long,
val executorId: String,
val host: String,
val taskLocality: TaskLocality.TaskLocality) //DAGScheduler.taskEnded
override def taskEnded(
task: Task[_],
reason: TaskEndReason,
result: Any,
accumUpdates: Map[Long, Any],
taskInfo: TaskInfo,
taskMetrics: TaskMetrics) {
eventQueue.put(CompletionEvent(task, reason, result, accumUpdates, taskInfo, taskMetrics))
} //DAGScheduler.processEvent
handleTaskCompletion(completion) //DAGScheduler.handleTaskCompletion
......

Spark 源码分析 -- task实际执行过程的更多相关文章

  1. MyBatis 源码分析 - SQL 的执行过程

    * 本文速览 本篇文章较为详细的介绍了 MyBatis 执行 SQL 的过程.该过程本身比较复杂,牵涉到的技术点比较多.包括但不限于 Mapper 接口代理类的生成.接口方法的解析.SQL 语句的解析 ...

  2. Spark源码分析之Checkpoint的过程

    概述 checkpoint 的机制保证了需要访问重复数据的应用 Spark 的DAG执行图可能很庞大,task 中计算链可能会很长,这时如果 task 中途运行出错,那么 task 的整个需要重算非常 ...

  3. Spark 源码分析 -- Task

    Task是介于DAGScheduler和TaskScheduler中间的接口 在DAGScheduler, 需要把DAG中的每个stage的每个partitions封装成task 最终把taskset ...

  4. 通过前端控制器源码分析springmvc的执行过程

    第一步:前端控制器接收请求调用doDiapatch 第二步:前端控制器调用处理器映射器查找 Handler 第三步:调用处理器适配器执行Handler,得到执行结果ModelAndView 第四步:视 ...

  5. Spark源码分析 – 汇总索引

    http://jerryshao.me/categories.html#architecture-ref http://blog.csdn.net/pelick/article/details/172 ...

  6. Spark源码分析 – DAGScheduler

    DAGScheduler的架构其实非常简单, 1. eventQueue, 所有需要DAGScheduler处理的事情都需要往eventQueue中发送event 2. eventLoop Threa ...

  7. MyBatis 源码分析 - 映射文件解析过程

    1.简介 在上一篇文章中,我详细分析了 MyBatis 配置文件的解析过程.由于上一篇文章的篇幅比较大,加之映射文件解析过程也比较复杂的原因.所以我将映射文件解析过程的分析内容从上一篇文章中抽取出来, ...

  8. spark 源码分析之二十一 -- Task的执行流程

    引言 在上两篇文章 spark 源码分析之十九 -- DAG的生成和Stage的划分 和 spark 源码分析之二十 -- Stage的提交 中剖析了Spark的DAG的生成,Stage的划分以及St ...

  9. Spark源码分析之八:Task运行(二)

    在<Spark源码分析之七:Task运行(一)>一文中,我们详细叙述了Task运行的整体流程,最终Task被传输到Executor上,启动一个对应的TaskRunner线程,并且在线程池中 ...

随机推荐

  1. linux命令(6)crontab的用法和解析,修改编辑器

    注意: 如果不是vim打开的,可以先: crontab -e 命令将检查环境变量$ EDITOR和$ VISUAL以覆盖默认文本编辑器,所以... export VISUAL=vim or expor ...

  2. Django rest_framework 认证源码流程

    一.请求到来后,都要先执行dispatch方法 dispatch根据请求方式的不同触发get/post/put/delete等方法 注意,APIView中的dispatch方法有很多的功能 def d ...

  3. Mockjs生成Vue数据表格

    1.npm install mockjs --save 2.在src文件下建mock.js文件 3.mock.js文件文件内容 //引入mockjs import Mock from 'mockjs' ...

  4. 深入浅出MFC--第一章

    Windows程序的生与死 当使用者按下系统菜单中的Close命令项,系统送出WM_CLOSE.通常程序的窗口函数不拦截次消息,于是DefWindowProc函数处理它.DefWindowProc收到 ...

  5. SNP密度分布模式

    1. window=100k,step=2k 统计每个window的snp密度,然后用mixtools的normalmixEM(两个组分的混合模型)统计snp的分布模式. R command: lib ...

  6. nginx解析漏洞

    一个比较老的漏洞了,但是今天在一个交流群里大佬们有那么一个案例.就深入学习了一下其原理. Nginx当检查url最后的文件名为脚本的时候,他就会把整个程序当作脚本来执行,否则就当作非脚本执行. 正确上 ...

  7. [Kernel]理解System call系统调用

    转自:http://os.51cto.com/art/200512/13510.htm 现在,您或许正在查看设备驱动程序,并感到奇怪:“函数 foo_read() 是如何被调用的?”或者可能疑惑: “ ...

  8. asp.net 列表样式

    找了好一段时间,找到一个不错的文章列表样式,留起来备用 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN&qu ...

  9. _BV()

    #define _BV(bit) (1 << (bit)) _BV()是把1左移N位的函数._BV(7)相当于(1<<7) 常用于位的置位或清零 示例解析: PC7=7; PO ...

  10. kettle的日志

    http://blog.sina.com.cn/s/blog_76a8411a01010u2h.html