动态分配executor的实例初始化部分
如果spark.executor.instances配置项设置为0或者没有设置,这个默认情况下是一个未设置的值,yarn的运行模式时,这个配置通过--num-executors来得到.
同时spark.dynamicAllocation.enabled配置项设置为true时.默认值为false,表示启用了动态分配executor.
在driver端SparkContext生成时,会检查上面两个配置项,如果这两个配置满足动态executor分配的要求时,会生成一个ExecutorAllocationManager实例.
_executorAllocationManager =
if (dynamicAllocationEnabled) {
Some(new ExecutorAllocationManager(this, listenerBus, _conf))
} else {
None
}
_executorAllocationManager.foreach(_.start())
必要的配置项:
1,配置项spark.dynamicAllocation.minExecutors,默认值0,最少分配的executor的个数.
2,配置项spark.dynamicAllocation.maxExecutors,默认值int.maxvalue.最大可分配的executor的个数.
3,配置项spark.dynamicAllocation.initialExecutors,默认值为配置项1的值,初始化时启用的executor的个数,
4,1,配置项spark.dynamicAllocation.schedulerBacklogTimeout,默认值1s,如果未分配的task等待分配的时间超过了这个配置的时间,表示需要新启动executor.
4,2,配置项spark.dynamicAllocation.sustainedSchedulerBacklogTimeout,默认是4,1,配置项的值,这个配置用于设置在初始调度的executor调度延时后,每次的等待超时时间.
5,配置项spark.dynamicAllocation.executorIdleTimeout,默认值60s,executor的空闲回收时间.
6,配置项spark.executor.cores的配置(executor-cores)必须大于或等于配置项spark.task.cpus的值(这个配置默认是1,这是每个task需要的cpu的个数).
7,配置项spark.shuffle.service.enabled必须配置为true,默认为false.如果这个配置设置为true时,BlockManager实例生成时,需要读取spark.shuffle.service.port配置项配置的shuffle的端口,同时对应BlockManager的shuffleClient不在是默认的BlockTransferService实例,而是ExternalShuffleClient实例.
8,初始化时,ExecutorAllocationManager中的属性initializing默认值为true,表示定时调度时,什么都不做.
在执行ExecutorAllocationManager中的start函数时:
def start(): Unit = {
这里把ExecutorAllocationListener实例(内部实现类)添加到sparkContext中的listenerBus中,用于监听stage,task的启动与完成,并做对应的操作.
listenerBus.addListener(listener)
val scheduleTask = new Runnable() {
override def run(): Unit = {
try {
schedule()
} catch {
case ct: ControlThrowable =>
throw ct
case t: Throwable =>
logWarning(s"Uncaught exception in thread
${Thread.currentThread().getName}", t)
}
}
}
定时100ms执行一次schedule的调度函数,来进行task的分析.
executor.scheduleAtFixedRate(scheduleTask, 0, intervalMillis,
TimeUnit.MILLISECONDS)
}
对executor的个数分配的计算
针对task的调度主要由一个定时器每100ms进行一次schedule函数的调用.
private def schedule(): Unit = synchronized {
在这个函数中,首先得到当前的时间,
val now = clock.getTimeMillis
在调用这个函数时,初始情况下,initializing的属性值为true,这个时候,这个函数什么也不做.
这个函数的内容,后面在进行分析.
updateAndSyncNumExecutorsTarget(now)
这个removeTimes集合中记录有每一个executor没有被task占用后的时间,如果这个时间超过了上面配置的idle的时间,会移出掉这个executor,同时设置initializing属性为false,表示可以继续进行task的调度.retain函数只保留未超时的executor.
removeTimes.retain { case (executorId, expireTime) =>
val expired = now >= expireTime
if (expired) {
initializing = false
removeExecutor(executorId)
}
!expired
}
}
如何知道stage被提交?看下面,
在SparkContext中,执行runJob命令时,针对一个stage进行submit操作时,会调用listenerBus中所有的listener对应的onStageSubmitted函数.
而在ExecutorAllocationManager进行start操作时,生成了一个listener,实例为ExecutorAllocationListener,并把这个listener添加到了listenerBus中.
接下来看看ExecutorAllocationListener中对应stage提交的监听处理:
override def onStageSubmitted(stageSubmitted: SparkListenerStageSubmitted)
: Unit = {
这里首先把initializing的属性值设置为false,表示下次定时调度时,需要执行executor的分配操作.
initializing = false
得到进行submit操作对应的stage的id与stage中对应的task的个数.
val stageId = stageSubmitted.stageInfo.stageId
val numTasks = stageSubmitted.stageInfo.numTasks
allocationManager.synchronized {
通过对应的stageId设置这个stage的task的个数,存储到stageIdToNumTasks集合中.
stageIdToNumTasks(stageId) = numTasks
这里更新allocationManager中的addTime的时间,
由当前时间加上配置spark.dynamicAllocation.schedulerBacklogTimeout的超时时间.
allocationManager.onSchedulerBacklogged()
这里根据每个task对应的host,计算出每个host对应的task的个数,numTasksPending的个数原则上应该与stage中numTask的个数相同.
// Compute the number of tasks requested by the stage on each host
var numTasksPending = 0
val hostToLocalTaskCountPerStage = new mutable.HashMap[String, Int]()
stageSubmitted.stageInfo.taskLocalityPreferences.foreach { locality =>
if (!locality.isEmpty) {
numTasksPending += 1
locality.foreach { location =>
val count = hostToLocalTaskCountPerStage.getOrElse(location.host, 0) + 1
hostToLocalTaskCountPerStage(location.host) = count
}
}
}
在对应的集合中,根据stageid与pending的task的个数,对应的host与host对应的task的个数进行存储.
stageIdToExecutorPlacementHints.put(stageId,
(numTasksPending, hostToLocalTaskCountPerStage.toMap))
下面的函数迭代stageIdToExecutorPlacementHints集合中的values,并更新allocationManager中localityAwareTasks属性(存储待启动的task的个数)与hostToLocalTaskCount集合属性(存储host对应的task的个数)的值.添加到这里,主要是executor启动时对应的调度启动task
// Update the executor placement hints
updateExecutorPlacementHints()
}
}
接面看看allocationManager中定时调度的updateAndSyncNumExecutorsTarget函数:
现在来说说updateAndSyncNumExecutorsTarget函数与addExecutors函数的作用:
示例说明:
假定这次的stage需要的executor的个数为5,numExecutorsTarget的配置保持默认值0,
如果是第一次调度启动时,在updateAndSyncNumExecutorsTarget函数中:
1,先计算出这个stage需要的executor的个数,
val