flink-start源码

jobSubmit

testCse

java 复制代码
final StreamExecutionEnvironment env = new StreamExecutionEnvironment(configuration);
//将算子添加进transformArrayList中
        env.fromCollection(Collections.singletonList(42))
            .addSink(new DiscardingSink<>());
        return env.execute();

StreamExecutionEnvironment

构建streamGraph(进行相关配置初始化,并未真正进行构建)并异步执行

java 复制代码
    public JobExecutionResult execute(String jobName) throws Exception {
        Preconditions.checkNotNull(jobName, "Streaming Job name should not be null.");
        final StreamGraph streamGraph = getStreamGraph();
        streamGraph.setJobName(jobName);
        return execute(streamGraph);
    }

public JobExecutionResult execute(StreamGraph streamGraph) throws Exception {
    //异步执行streamGraph
     final JobClient jobClient = executeAsync(streamGraph);

            try {
                final JobExecutionResult jobExecutionResult;

                if (configuration.getBoolean(DeploymentOptions.ATTACHED)) {
                    jobExecutionResult = jobClient.getJobExecutionResult().get();
                } else {
                    jobExecutionResult = new DetachedJobExecutionResult(jobClient.getJobID());
                }

                jobListeners.forEach(
                        jobListener -> jobListener.onJobExecuted(jobExecutionResult, null));

                return jobExecutionResult;
            } catch (Throwable t) {
                ...
        }
    
    
    public JobClient executeAsync(StreamGraph streamGraph) throws Exception {
            final PipelineExecutorFactory executorFactory =
                executorServiceLoader.getExecutorFactory(configuration);


        CompletableFuture<JobClient> jobClientFuture =
                executorFactory
            //获取执行器,具体实现类如下图
                        .getExecutor(configuration)
            //实例化执行器并执行
                        .execute(streamGraph, configuration, userClassloader);

        try {
            JobClient jobClient = jobClientFuture.get();
            //提交成功
            jobListeners.forEach(jobListener -> jobListener.onJobSubmitted(jobClient, null));
            return jobClient;
        } catch (ExecutionException executionException) {
            final Throwable strippedException =
                    ExceptionUtils.stripExecutionException(executionException);
            jobListeners.forEach(
                //提交失败
                    jobListener -> jobListener.onJobSubmitted(null, strippedException));

            throw new FlinkException(
                    String.format("Failed to execute job '%s'.", streamGraph.getJobName()),
                    strippedException);
        }
    }

executor实现:

LocalExecutorFactory

java 复制代码
public class LocalExecutorFactory implements PipelineExecutorFactory {


@Override
public PipelineExecutor getExecutor(final Configuration configuration) {
    //创建执行器
    return LocalExecutor.create(configuration);
}
 ...   
}

LocalExecutor

java 复制代码
public static LocalExecutor create(Configuration configuration) {
//创建cluster
    return new LocalExecutor(configuration, MiniCluster::new);
}

LocalExecutor

java 复制代码
    @Override
    public CompletableFuture<JobClient> execute(
            Pipeline pipeline, Configuration configuration, ClassLoader userCodeClassloader)
            throws Exception {
        checkNotNull(pipeline);
        checkNotNull(configuration);

        Configuration effectiveConfig = new Configuration();
        effectiveConfig.addAll(this.configuration);
        effectiveConfig.addAll(configuration);

        // we only support attached execution with the local executor.
        checkState(configuration.getBoolean(DeploymentOptions.ATTACHED));

        final JobGraph jobGraph = getJobGraph(pipeline, effectiveConfig);

        return PerJobMiniClusterFactory.createWithFactory(effectiveConfig, miniClusterFactory)
            //提交作业
                .submitJob(jobGraph, userCodeClassloader);
    }

 /** Starts a {@link MiniCluster} and submits a job. */
    public CompletableFuture<JobClient> submitJob(
            JobGraph jobGraph, ClassLoader userCodeClassloader) throws Exception {
        MiniClusterConfiguration miniClusterConfig =
                getMiniClusterConfig(jobGraph.getMaximumParallelism());
        MiniCluster miniCluster = miniClusterFactory.apply(miniClusterConfig);
        
        //启动cluster
        miniCluster.start();

        return miniCluster
                .submitJob(jobGraph)
                .thenApplyAsync(
                        FunctionUtils.uncheckedFunction(
                                submissionResult -> {
                                    org.apache.flink.client.ClientUtils
                                            .waitUntilJobInitializationFinished(
                                                    () ->
                                                            miniCluster
                                                                    .getJobStatus(
                                                                            submissionResult
                                                                                    .getJobID())
                                                                    .get(),
                                                    () ->
                                                            miniCluster
                                                                    .requestJobResult(
                                                                            submissionResult
                                                                                    .getJobID())
                                                                    .get(),
                                                    userCodeClassloader);
                                    return submissionResult;
                                }))
                .thenApply(
                        result ->
                                new MiniClusterJobClient(
                                        result.getJobID(),
                                        miniCluster,
                                        userCodeClassloader,
                                        MiniClusterJobClient.JobFinalizationBehavior
                                                .SHUTDOWN_CLUSTER))
                .whenComplete(
                        (ignored, throwable) -> {
                            if (throwable != null) {
                                // We failed to create the JobClient and must shutdown to ensure
                                // cleanup.
                                shutDownCluster(miniCluster);
                            }
                        })
                .thenApply(Function.identity());
    }

MiniCluster

java 复制代码
public CompletableFuture<JobSubmissionResult> submitJob(JobGraph jobGraph) {
    final CompletableFuture<DispatcherGateway> dispatcherGatewayFuture =
            getDispatcherGatewayFuture();
    final CompletableFuture<InetSocketAddress> blobServerAddressFuture =
            createBlobServerAddress(dispatcherGatewayFuture);
    final CompletableFuture<Void> jarUploadFuture =
            uploadAndSetJobFiles(blobServerAddressFuture, jobGraph);
    final CompletableFuture<Acknowledge> acknowledgeCompletableFuture =
            jarUploadFuture
                    .thenCombine(
                            dispatcherGatewayFuture,
                            (Void ack, DispatcherGateway dispatcherGateway) ->
                                    dispatcherGateway.submitJob(jobGraph, rpcTimeout))
                    .thenCompose(Function.identity());
    return acknowledgeCompletableFuture.thenApply(
            (Acknowledge ignored) -> new JobSubmissionResult(jobGraph.getJobID()));
}
相关推荐
Elastic 中国社区官方博客4 小时前
JavaScript 中的 ES|QL:利用 Apache Arrow 工具
大数据·开发语言·javascript·elasticsearch·搜索引擎·全文检索·apache
lifallen6 小时前
Flink task、Operator 和 UDF 之间的关系
java·大数据·flink
源码宝8 小时前
智慧工地云平台源码,基于微服务架构+Java+Spring Cloud +UniApp +MySql
java·大数据·源码·智慧工地·智能监测·智能施工
XiaoQiong.Zhang8 小时前
简历模板3——数据挖掘工程师5年经验
大数据·人工智能·机器学习·数据挖掘
Faith_xzc10 小时前
Apache Doris FE 问题排查与故障分析全景指南
大数据·数据仓库·apache·doris
潘小磊10 小时前
高频面试之6Hive
大数据·hive·面试·职场和发展
数据与人工智能律师12 小时前
当机床开始“思考”,传统“制造”到“智造”升级路上的法律暗礁
大数据·网络·算法·云计算·区块链
摘星编程13 小时前
华为云Flexus+DeepSeek征文 | 模型即服务(MaaS)安全攻防:企业级数据隔离方案
大数据·人工智能·安全·华为云·deepseek
木鱼时刻13 小时前
从大数据到大模型:我们是否在重蹈覆覆辙
大数据
liuze40814 小时前
VMware虚拟机集群上部署HDFS集群
大数据·hadoop·hdfs