Flink 任务调度机制几个重要概念

调度器是 Flink 作业执行的核心组件,管理作业执行的所有相关过程,包括 JobGraph 到 ExecutionGraph 的转换、作业生命周期管理(作业的发布、取消、停止)、作业的 Task 生命周期管理(Task 的发布、取消、停止)、资源申请与释放、作业和 Task 的 Failover 等。

调度有几个重要的组件:

  • 调度器:SchedulerNG 及其子类、实现类

  • 调度策略:SchedulingStrategy 及其实现类

  • 调度模式:ScheduleMode 包含流和批的调度,有各自不同的调度模式

1、调度器

调度器作用:

1)作业的生命周期管理,如作业的发布、挂起、取消

2)作业执行资源的申请、分配、释放

3)作业的状态管理,作业发布过程中的状态变化和作业异常时的 FailOver 等

4)作业的信息提供,对外提供作业的详细信息

看一下源码结构:

SchedulerNG.java

public interface SchedulerNG { void setMainThreadExecutor(ComponentMainThreadExecutor mainThreadExecutor); void registerJobStatusListener(JobStatusListener jobStatusListener); void startScheduling(); void suspend(Throwable cause); void cancel(); CompletableFuture<Void> getTerminationFuture(); void handleGlobalFailure(Throwablecause); default boolean updateTaskExecutionState(TaskExecutionState taskExecutionState) { return updateTaskExecutionState(new TaskExecutionStateTransition(taskExecutionState)); } boolean updateTaskExecutionState(TaskExecutionStateTransition taskExecutionState); SerializedInputSplitrequestNextInputSplit(JobVertexID vertexID, ExecutionAttemptIDexecutionAttempt) throws IOException; ExecutionState requestPartitionState(IntermediateDataSetIDintermediateResultId, ResultPartitionID resultPartitionId) throws PartitionProducerDisposedException; void scheduleOrUpdateConsumers(ResultPartitionID partitionID); ArchivedExecutionGraph requestJob(); JobStatus requestJobStatus(); JobDetails requestJobDetails(); }

实现类:DefaultScheduler(1.11 移除了 LegacyScheduler)

2、调度行为

SchedulingStrategy.java

/** * Component which encapsulates the schedulinglogic. * It can react to execution state changes andpartition consumable events. * Moreover, it is responsible for resolvingtask failures. */public interface SchedulingStrategy { /** * Called when the scheduling is started(initial scheduling operation). *调度入口,触发调度器的调度行为 */ void startScheduling(); /** * Called whenever vertices need to berestarted (due to task failure). * 重启执行失败的Task,一般是Task执行异常导致 * @param verticesToRestart The tasks need tobe restarted */ void restartTasks(Set<ExecutionVertexID> verticesToRestart); /** * Called whenever an {@link Execution} changesits state. * 当Execution改变状态时调用 * @param executionVertexId The id of the task * @param executionState The new state of theexecution */ void onExecutionStateChange(ExecutionVertexID executionVertexId, ExecutionStateexecutionState); /** * Called whenever an {@linkIntermediateResultPartition} becomes consumable. * 当IntermediateResultPartition中的数据可以消费时调用 * @param resultPartitionId The id of theresult partition */ void onPartitionConsumable(IntermediateResultPartitionID resultPartitionId);}

3、调度模式

ScheduleMode 决定如何启动 ExecutionGraph 中的 Task。大数据培训Flink 提供 3 种调度模式:

1)Eager 调度

适用于流计算。一次性申请需要的所有资源,如果资源不足,则作业启动失败。

2)分阶段调度

LAZY_FROM_SOURCES 适用于批处理。从 SourceTask 开始分阶段调度,申请资源的时候,一次性申请本阶段所需要的所有资源。上游 Task 执行完毕后开始调度执行下游的 Task,读取上游的数据,执行本阶段的计算任务,执行完毕之后,调度后一个阶段的 Task,依次进行调度,直到作业完成。

3)分阶段 Slot 重用调度

LAZY_FROM_SOURCES_WITH_BATCH_SLOT_REQUEST 适用于批处理。与分阶段调度基本一样,区别在于该模式下使用批处理资源申请模式,可以在资源不足的情况下执行作业,但是需要确保在本阶段的作业执行中没有 Shuffle 行为。

目前视线中的 Eager 模式和 LAZY_FROM_SOURCES 模式的资源申请逻辑一样,LAZY_FROM_SOURCES_WITH_BATCH_SLOT_REQUEST 是单独的资源申请逻辑。

ScheduleMode.java

public enum ScheduleMode { LAZY_FROM_SOURCES(true), LAZY_FROM_SOURCES_WITH_BATCH_SLOT_REQUEST(true), EAGER(false); private final boolean allowLazyDeployment; ScheduleMode(booleanallowLazyDeployment) { this.allowLazyDeployment = allowLazyDeployment; } public boolean allowLazyDeployment() { return allowLazyDeployment; }}

4、调度策略

调度策略有三种实现:

  • EagerSchedulingStrategy:适用于流计算,同时调度所有的 task

  • LazyFromSourcesSchedulingStrategy:适用于批处理,当输入数据准备好时(上游处理完)进行 vertices 调度。

  • PipelinedRegionSchedulingStrategy:以流水线的局部为粒度进行调度

PipelinedRegionSchedulingStrategy 是 1.11 加入的,从 1.12 开始,将以 pipelined region 为单位进行调度。pipelined region 是一组流水线连接的任务。

这意味着,对于包含多个 region 的流作业,在开始部署任务之前,它不再等待所有任务获取 slot。取而代之的是,一旦任何 region 获得了足够的任务 slot 就可以部署它。对于批处理作业,将不会为任务分配 slot,也不会单独部署任务。

取而代之的是,一旦某个 region 获得了足够的 slot,则该任务将与所有其他任务一起部署在同一区域中。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值