ExecutorAllocationManager的作用已在《Spark2.1.0——SparkContext概述》一文有过介绍,更为准确地说,ExecutorAllocationManager是基于工作负载动态分配和删除Executor的代理。简单讲,ExecutorAllocationManager与集群管理器之间的关系可以用图1来表示。

ExecutorAllocationManager内部会定时根据工作负载计算所需的Executor数量,如果对Executor需求数量大于之前向集群管理器申请的Executor数量,那么向集群管理器申请添加Executor;如果对Executor需求数量小于之前向集群管理器申请的Executor数量,那么向集群管理器申请取消部分Executor。此外,ExecutorAllocationManager内部还会定时向集群管理器申请移除(杀死)过期的Executor。
有了对ExecutorAllocationManager的了解,这里来看看SparkContext是如何创建和启动ExecutorAllocationManager的。创建ExecutorAllocationManager的代码如下:
val dynamicAllocationEnabled = Utils.isDynamicAllocationEnabled(_conf)
_executorAllocationManager =
if (dynamicAllocationEnabled) {
schedulerBackend match {
case b: ExecutorAllocationClient =>
Some(new ExecutorAllocationManager(
schedulerB