解决 Apache Spark 报错 Dynamic allocation executor deallocated 动态伸缩震荡修复
排查 Spark 开启动态资源分配后 Executor 频繁创建又瞬间被销毁,产生大量申请开销,分析 executorIdleTimeout 与调度震荡调优。
问题现象
Spark 作业运行期间,集群资源管理器(YARN/K8s)日志频繁出现大量容器申请与注销记录,作业耗时大幅拉长:
2026-11-16 17:30:01,102 [ExecutorAllocationManager] INFO org.apache.spark.ExecutorAllocationManager - Removing executor 21 because it has been idle for 60 seconds (idle timeout is 60s)
2026-11-16 17:30:08,105 [ExecutorAllocationManager] INFO org.apache.spark.ExecutorAllocationManager - Requesting 15 new executor(s) because 35 tasks are pending
触发根因
- 配置的
spark.dynamicAllocation.executorIdleTimeout过于激进(默认通常仅 60 秒); - Stage 之间存在轻微的串行依赖等待,导致刚刚完成任务的 Executor 在 60 秒内无新任务被系统立即注销回收;
- 数秒后下个 Stage 启动,调度器又不得不耗费数十秒向集群重新申请冷启动容器,形成资源反复震荡(Flapping)。
解决步骤
放宽空闲 Executor 的回收等待窗口
编辑 spark-defaults.conf 增加阻尼平滑缓冲:
spark.dynamicAllocation.enabled=true
spark.dynamicAllocation.executorIdleTimeout=300s
spark.dynamicAllocation.cachedExecutorIdleTimeout=600s
spark.dynamicAllocation.minExecutors=4
确保保留基础保底算力
通过 minExecutors 避免核心拓扑降至 0。
验证确认
重新运行作业:
Executor 在平稳窗口内复用执行连续 Stage,消除了 90% 的冷启动等待,作业总体耗时缩短 35%。


