Flink源码篇 No.3-任务提交之执行用户作业(per-job on yarn)

第1章 执行用户程序

当用户代码最后调用env.execute()时,则开始执行如下程序:

org.apache.flink.streaming.api.environment.StreamExecutionEnvironment#execute(java.lang.String)

public JobExecutionResult execute(String jobName) throws Exception {
	Preconditions.checkNotNull(jobName, "Streaming Job name should not be null.");
	//TODO 获取StreamGraph
	return execute(getStreamGraph(jobName));
}

org.apache.flink.streaming.api.environment.StreamExecutionEnvironment#execute(org.apache.flink.streaming.api.graph.StreamGraph)

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

	//...
}

org.apache.flink.streaming.api.environment.StreamExecutionEnvironment#executeAsync(org.apache.flink.streaming.api.graph.StreamGraph) 

@Internal
public JobClient executeAsync(StreamGraph streamGraph) throws Exception {
	//TODO 检查校验streamGraph
	checkNotNull(streamGraph, "StreamGraph cannot be null.");
	checkNotNull(configuration.get(DeploymentOptions.TARGET), "No execution.target specified in your configuration file.");

	final PipelineExecutorFactory executorFactory =
		executorServiceLoader.getExecutorFactory(configuration);

	checkNotNull(
		executorFactory,
		"Cannot find compatible factory for specified execution.target (=%s)",
		configuration.get(DeploymentOptions.TARGET));

	//TODO 执行
	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);
	}
}

到这里,执行的execute是一个接口函数,我们看YarnJobClusterExecutor的实现(在父类AbstractJobClusterExecutor中)。

org.apache.flink.client.deployment.executors.AbstractJobClusterExecutor#execute

@Override
public CompletableFuture<JobClient> execute(@Nonnull final Pipeline pipeline, @Nonnull final Configuration configuration, @Nonnull final ClassLoader userCodeClassloader) throws Exception {
	//TODO Client端StreamGraph(实现了Pipeline接口的类)转换成JobGraph
	final JobGraph jobGraph = PipelineExecutorUtils.getJobGraph(pipeline, configuration);

	//TODO 集群描述器 Yarn和Flink的集群环境信息
	try (final ClusterDescriptor<ClusterID> clusterDescriptor = clusterClientFactory.createClusterDescriptor(configuration)) {
		final ExecutionConfigAccessor configAccessor = ExecutionConfigAccessor.fromConfiguration(configuration);

		//TODO 获取集群配置 JM内存 TM内存 TM中Slot个数等信息
		final ClusterSpecification clusterSpecification = clusterClientFactory.getClusterSpecification(configuration);

		//TODO 部署并启动集群中的各个进程
		final ClusterClientProvider<ClusterID> clusterClientProvider = clusterDescriptor
				.deployJobCluster(clusterSpecification, jobGraph, configAccessor.getDetachedMode());
		LOG.info("Job has been submitted with JobID " + jobGraph.getJobID());

		return CompletableFuture.completedFuture(
				new ClusterClientJobClientAdapter<>(clusterClientProvider, jobGraph.getJobID(), userCodeClassloader));
	}
}

这就是在用户程序最后调用env.execute(),所做的事情。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

pezynd

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值