Spark中的Scheduler

Spark中的Schedulerscheduler分成两个类型,一个是TaskScheduler与其实现,一个是DAGScheduler。TaskScheduler:主要负责各stage中传入的task的执行与调度。DAGScheduler:主要负责对JOB中的各种依赖进行解析,根据RDD的依赖生成stage并通知TaskScheduler执行。实例生成TaskSchedule
摘要由CSDN通过智能技术生成

Spark中的Scheduler

scheduler分成两个类型,一个是TaskScheduler与其实现,一个是DAGScheduler

TaskScheduler:主要负责各stage中传入的task的执行与调度。

DAGScheduler:主要负责对JOB中的各种依赖进行解析,根据RDD的依赖生成stage并通知TaskScheduler执行。

实例生成

TaskScheduler实例生成:

scheduler实例生成,我目前主要是针对onyarnspark进行的相关分析,

appmaster启动后,通过调用startUserClass()启动线程来调用用户定义的spark分析程序。

传入的第一个参数为appmastername(master),可传入的如:yarn-cluster等。

在用户定义的spark分析程序中,生成SparkContext实例。

通过SparkContext.createTaskScheduler函数。如果是yarn-cluster,生成YarnClusterScheduler实例。

此部分生成的schedulerTaskScheduler实例。

defthis(sc:SparkContext) = this(sc,newConfiguration())

同时YarnClusterSchduler实现TaskSchedulerImpl

defthis(sc:SparkContext) = this(sc,sc.conf.getInt("spark.task.maxFailures",4))

生成TaskScheduler中的SchedulerBackend属性引用,yarn-clusterCoarseGrainedSchedulerBackend

valbackend =newCoarseGrainedSchedulerBackend(scheduler,sc.env.actorSystem)

scheduler.initialize(backend)



DAGScheduler实例生成:

classDAGScheduler(

taskSched: TaskScheduler,

mapOutputTracker:MapOutputTrackerMaster,

blockManagerMaster:BlockManagerMaster,

env: SparkEnv)

extendsLogging {


defthis(taskSched:TaskScheduler){

this(taskSched,SparkEnv.get.mapOutputTracker.asInstanceOf[MapOutputTrackerMaster],

SparkEnv.get.blockManager.master,SparkEnv.get)

}

taskSched.setDAGScheduler(this)


scheduler调度过程分析

1.rdd执行action操作,如saveAsHadoopFile

2.调用SparkContext.runJob

3.调用DAGScheduler.runJob-->此函数调用submitJob,并等job执行完成。

Waiter.awaitResult()中通过_jobFinished检查job运行是否完成,如果完成,此传为true,否则为false.

_jobFinished的值通过resultHandler函数,每调用一次finishedTasks的值加一,

如果finishedTasks的个数等于totalTasks的个数时,表示完成。或者出现exception.

defrunJob[T, U: ClassTag](

rdd: RDD[T],

func: (TaskContext, Iterator[T])=> U,

partitions: Seq[Int],

callSite: String,

allowLocal: Boolean,

resultHandler: (Int, U) =>Unit,

properties: Properties = null)

{

valwaiter =submitJob(rdd, func, partitions, callSite, allowLocal, resultHandler,properties)

waiter.awaitResult()match{

caseJobSucceeded => {}

caseJobFailed(exception:Exception, _) =>

logInfo("Failedto run " + callSite)

throwexception

}

}


4.调用DAGScheduler.submitJob函数,

部分代码:生成JobWaiter实例,并传入此实例,发送消息,调用JobSubmitted事件。并返回waiter实例。

JobWaiterJobListener的实现。

valwaiter =newJobWaiter(this,jobId,partitions.size, resultHandler)

eventProcessActor! JobSubmitted(

jobId,rdd, func2,partitions.toArray, allowLocal, callSite, waiter,properties)

waiter


5.处理DAGSchedulerJobSubmitted事件消息,通过processEvent处理消息接收的事件。

defreceive = {

caseevent:DAGSchedulerEvent =>

logTrace("Gotevent of type " +event.getClass.getName)

if(!processEvent(event)){

submitWaitingStages()

} else{

resubmissionTask.cancel()

context.stop(self)

}

}

}))


6.processEvent函数中处理JobSubmitted事件部分代码:

caseJobSubmitted(jobId,rdd, func,partitions,allowLocal,callSite,listener,properties)=>

varfinalStage:Stage = null

try{

生成stage实例,stageid通过nextStageId的值加一得到,task的个数就是partitions的分区个数,

根据job对应的rdd,得到如果parentrddshufflerdd时生成ShuffleMapStage,通过getParentStages函数,

此处去拿到parentrdd时,如果currentrddparentrdd不是shuffle,递归调用parentrdd,

如果parendrdd中没有shufflerdd,不生成新的stage,否则有多少个,生成多少个。此处是处理DAG类的依赖

finalStage= newStage(rdd,partitions.size,None, jobId,Some(callSite))

} catch{

casee:Exception =>

logWarning("Creatingnew stage failed due to exception - job: "+ jobId, e)

listener.jobFailed(e)

returnfalse

}

生成ActiveJob实例。设置numFinished的值为0,表示job中有0个完成的task.

设置所有task个数的arrayfinished.并把所有元素的值设置为false.JobWaiterlistener传入ActiveJob.

valjob = newActiveJob(jobId,finalStage,func,partitions,callSite,listener,properties)


对已经cache过的TaskLocation进行清理。

clearCacheLocs()

logInfo("Gotjob " + job.jobId+ " ("+ callSite+ ") with "+ partitions.length+

"output partitions (allowLocal=" +allowLocal+ ")")

logInfo("Finalstage: " + finalStage+ " ("+ finalStage.name+ ")")

logInfo("Parentsof final stage: " +finalStage.parents)

logInfo("Missingparents: " +getMissingParentStages(finalStage))

如果runJob时传入的allowLocal的值为true,同时没有需要shufflerdd,同时partitions的长度为1

也就是

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值