flink启动job_Flink源码第一篇:Flink之Job启动流程

本文详细介绍了Flink Job的启动过程,从`./bin/flink run`命令开始,探讨了配置初始化及CliFrontend在调度中的作用,揭示了Flink内部的执行流程。

Flink启动Job

$ ./bin/flink run examples/streaming/SocketWindowWordCount.jar

先看一下flink脚本内部调度流程

cat flink

4acd81b301b1c2706a06b761a1c3a207.png

大家可以看到flink内部执行流程

  • 初始化config

  • 调用CliFrontend

ef671f1b24e49b2df3ca43d929963b43.png

CliFrontend

类目录:package org.apache.flink.client.cli;打开这个类大家可以看到熟悉的命令

0c7607c704cbcf7d8d067c7cc07ec2ac.png

然后观察main函数:

a67068651a071b5b7fd9147103beee10.png

parseParameters执行了什么?

e08e7ea049609403e89572c46d720ae6.png

run(params)内部时如何调用的?最后调用了runProgram(customCommandLine, commandLine, runOptions, program);

f877296108b71c7de68889a81ad7c7f9.png

runProgram()执行流程

private <T> void runProgram(

CustomCommandLine<T> customCommandLine,

CommandLine commandLine,

RunOptions runOptions,

PackagedProgram program) throws ProgramInvocationException, FlinkException {

final ClusterDescriptor<T> clusterDescriptor = customCommandLine.createClusterDescriptor(commandLine);

try {

//如果命令中制定了clusterid,则会返回,否则返回空

final T clusterId = customCommandLine.getClusterId(commandLine);

//初始化客户端

final ClusterClient<T> client;

// directly deploy the job if the cluster is started in job mode and detached

// 如果集群以作业模式启动并分离,则直接部署作业

if (clusterId == null && runOptions.getDetachedMode()) {

int parallelism = runOptions.getParallelism() == -1 ? defaultParallelism : runOptions.getParallelism();

//初始化jobGraph

final JobGraph jobGraph = PackagedProgramUtils.createJobGraph(program, configuration, parallelism);

final ClusterSpecification clusterSpecification = customCommandLine.getClusterSpecification(commandLine);

//开始执行jobGraph

client = clusterDescriptor.deployJobCluster(

clusterSpecification,

jobGraph,

runOptions.getDetachedMode());

//job已经提交,并且jobid打印出来

logAndSysout("Job has been submitted with JobID " + jobGraph.getJobID());

try {

client.shutdown();

} catch (Exception e) {

LOG.info("Could not properly shut down the client.", e);

}

} else {

final Thread shutdownHook;

if (clusterId != null) {

client = clusterDescriptor.retrieve(clusterId);

shutdownHook = null;

} else {

// also in job mode we have to deploy a session cluster because the job

// might consist of multiple parts (e.g. when using collect)

final ClusterSpecification clusterSpecification = customCommandLine.getClusterSpecification(commandLine);

client = clusterDescriptor.deploySessionCluster(clusterSpecification);

// if not running in detached mode, add a shutdown hook to shut down cluster if client exits

// there's a race-condition here if cli is killed before shutdown hook is installed

if (!runOptions.getDetachedMode() && runOptions.isShutdownOnAttachedExit()) {

shutdownHook = ShutdownHookUtil.addShutdownHook(client::shutDownCluster, client.getClass().getSimpleName(), LOG);

} else {

shutdownHook = null;

}

}

try {

client.setPrintStatusDuringExecution(runOptions.getStdoutLogging());

client.setDetached(runOptions.getDetachedMode());

LOG.debug("{}", runOptions.getSavepointRestoreSettings());

int userParallelism = runOptions.getParallelism();

LOG.debug("User parallelism is set to {}", userParallelism);

if (ExecutionConfig.PARALLELISM_DEFAULT == userParallelism) {

userParallelism = defaultParallelism;

}

executeProgram(program, client, userParallelism);

} finally {

if (clusterId == null && !client.isDetached()) {

// terminate the cluster only if we have started it before and if it's not detached

try {

client.shutDownCluster();

} catch (final Exception e) {

LOG.info("Could not properly terminate the Flink cluster.", e);

}

if (shutdownHook != null) {

// we do not need the hook anymore as we have just tried to shutdown the cluster.

ShutdownHookUtil.removeShutdownHook(shutdownHook, client.getClass().getSimpleName(), LOG);

}

}

try {

client.shutdown();

} catch (Exception e) {

LOG.info("Could not properly shut down the client.", e);

}

}

}

} finally {

try {

clusterDescriptor.close();

} catch (Exception e) {

LOG.info("Could not properly close the cluster descriptor.", e);

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值