概述
本文分析任务调度器TaskScheduler中的调度实体的实现合约的原理。
TaskScheduler中的调度对象
在TaskScheduler中调度对象是实现了合约(即:接口)Schedulable的类的对象。
Schedulable是是可调度实体的合约,可以在这里查看该接口的实现代码。
在spark2中有两个类实现了调度实体接口:Schedulable。也就是说有两种调度实体:
- Pool
- TaskSetManager
在后面的文章中会对这两种调度实体进行详细分析。
调度对象接口分析
通过查看源码我们知道,该接口包括以下一些成员,下面分别讲解这些成员变量的意义。
- name
def name: String
每个可调度实体都有一个名称。
- parent
var parent: Pool
该成员表示依赖的Pool可以调度实体,通过parent可以构建一颗树
- schedulableQueue
def schedulableQueue: ConcurrentLinkedQueue[Schedulable]
管理一套可调度的实体集,是一个同步队列,所以和该队列对应的有两个操作函数:
def addSchedulable(schedulable: Schedulable): Unit
def removeSchedulable(schedulable: Schedulable): Unit
用来向队列中添加和删除调度对象。
- schedulingMode
def schedulingMode: SchedulingMode
该成员变量代表调度模式,不同调度模式对应不同的调度算法。
- stageId
def stageId: Int
表示该调度对象对应的stage id。
- weight,minShare,priority
def weight: Int
def minShare: Int
def priority: Int
权重,时间,优先级,这些成员在进行任务调度时,用来计算任务的优先级。
- getSchedulableByName
def getSchedulableByName(name: String): Schedulable
通过该函数可以根据调度实体的名称,来查询该对象。
- executorLost
def executorLost(executorId: String, host: String, reason: ExecutorLossReason): Unit
当执行任务的执行器(executor)失败时,会调用该函数进行处理。TaskSchedulerImpl调用该函数来通知TaskSetManagers执行失败。
- checkSpeculatableTasks
def checkSpeculatableTasks(minTimeToSpeculation: Int): Boolean
该函数用来检查要推测的任务,如果有,则返回true。 由TaskScheduler定期调用。
但注意:不能推测我们是否只有一个任务,也不能推测任务集是否是一个僵尸任务集。
- getSortedTaskSetQueue
def getSortedTaskSetQueue: ArrayBuffer[TaskSetManager]
在TaskSchedulerImpl中使用该函数来处理资源的供给(让每个TaskSetManager知道新的执行器已经准备好执行任务)。
总结
本文分析了任务调度对象的实现合约的原理,对其各个成员进行了分析和说明。接下来会继续分析实现该合约的两种调度实体的实现。