TaskScheduler的作用
TaskScheduler的主要作用是把TaskSet中的每一个task提交到executor上去执行;
其中最主要的就是task的分配算法。
TaskScheduler源码分析
TaskScheduler 提交tasks的入口 submitTasks
- 为每一个taskSet创建一个taskSetManager
- 调用CoarseGrainedSchedulerBackend 的 reviveOffers()方法进行task分配
/**
* TaskScheduler 提交程序的入口
*/
override def submitTasks(taskSet: TaskSet) {
val tasks = taskSet.tasks
logInfo("Adding task set " + taskSet.id + " with " + tasks.length + " tasks")
this.synchronized {
/**
* leen
* 为每一个taskSet创建一个taskSetManager
* taskSetManager实际上,会负责它的TaskSet执行状况的监视与管理
* 当tasks失败的时候重试task,(直到超过重试次数限制)
*/
val manager = createTaskSetManager(taskSet, maxTaskFailures)
val stage = taskSet.stageId
val stageTaskSets =
taskSetsByStageIdAndAttempt.getOrElseUpdate(stage, new HashMap[Int, TaskSetManager])
stageTaskSets(taskSet.stageAttemptId) = manager
val conflictingTaskSet = stageTaskSets.exists { case (_, ts) =>
ts.taskSet != taskSet && !ts.isZombie
}
if (conflictingTaskSet) {
throw new IllegalStateException(s"more than one active taskSet for stage $stage:" +
s" ${stageTaskSets.toSeq.map{_._2.taskSet.id}.mkString(",")}")
}
schedulableBuilder.addTaskSetManager(manager, manager.taskSet.properties)
if (!isLocal && !hasReceivedTask) {
starvationTimer.scheduleAtFixedRate(new TimerTask() {
override def run() {
if (!hasLaunchedTask) {
logWarning("Initial job has not accepted any resources; " +
"check your cluster UI to ensure t