flink 调度器概述

调度器是 Flink 作业执行的核心组件,管理作业执行的所有相关过程,包括 JobGraph 到
ExecutionGraph 的转换、作业生命周期管理(作业的发布、取消、停止)、作业的 Task 生命
周期管理(Task 的发布、取消、停止)、资源申请与释放、作业和 Task 的 Failover 等。
调度有几个重要的组件:
⚫ 调度器: SchedulerNG 及其子类、实现类
⚫ 调度策略: SchedulingStrategy 及其实现类
⚫ 调度模式: ScheduleMode 包含流和批的调度,有各自不同的调度模式

调度器作用:
1)作业的生命周期管理,如作业的发布、挂起、取消
2)作业执行资源的申请、分配、释放
3)作业的状态管理,作业发布过程中的状态变化和作业异常时的 FailOver 等
4)作业的信息提供,对外提供作业的详细信息

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

实现类: DefaultScheduler(1.11 移除了 LegacyScheduler)
在这里插入图片描述

调度行为

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

调度模式

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 是单独的资源申请逻辑。

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

调度策略

在这里插入图片描述
调度策略有三种实现:
⚫ EagerSchedulingStrategy: 适用于流计算,同时调度所有的 task
⚫ LazyFromSourcesSchedulingStrategy:适用于批处理,当输入数据准备好时(上游处
理完)进行 vertices 调度。
⚫ PipelinedRegionSchedulingStrategy:以流水线的局部为粒度进行调度
PipelinedRegionSchedulingStrategy 是 1.11 加入的,从 1.12 开始,将以 pipelined region
为单位进行调度。pipelined region 是一组流水线连接的任务。这意味着,对于包含多个 region
的流作业,在开始部署任务之前,它不再等待所有任务获取 slot。取而代之的是,一旦任何
region 获得了足够的任务 slot 就可以部署它。对于批处理作业,将不会为任务分配 slot,
也不会单独部署任务。取而代之的是,一旦某个 region 获得了足够的 slot,则该任务将与
所有其他任务一起部署在同一区域中

猜你喜欢

转载自blog.csdn.net/m0_46449152/article/details/114274142