java 服务器 Flink 深入理解任务执行计划,即Graph生成过程(源码解读) - vertexID,

📅 2026-11-30 👤 拼多多助力营销技巧 📂 拼多多助力2027 👁️ 487 次阅读

深入理解Graph生成过程

1生成StreamGraph2生成JobGraph3生成ExecutionGraph:4生成物理执行图:5 批处理的物理执行计划同源实例的并行执行

我们先看一下,Flink 是如何描述作业的执行计划的。以这个 DataStream 作业为例,Flink 会基于它先生成一个 StreamGraph。这是一个有向无环图,图中的节点对应着计算逻辑,图中的边则对应着数据的分发方式。

Flink 会根据节点的并行度以及他们之间的连边方式,把一些计算节点进行链接合并,最终形成 JobGraph,从而降低计算节点间的数据传输开销。这个操作的目的是,是为了降低计算节点之间的数据传输开销。StreamGraph 和 JobGraph 都是在编译阶段生成的。JobGraph 会提交给 Flink Job Manager,从而启动和执行作业。

在执行作业前,Flink 会生成 ExecutionGraph。这个 ExecutionGraph 是根据 JobGraph 中的节点并行度,展开生成的。我们知道,Flink 是一个分布式计算框架。而 ExecutionGraph 的每一个节点,都对应着一个需要部署到 TaskManager 上进行执行的任务,每一条边都对应着任务的输入和输出。所以说,它是作业的物理执行计划。

这个物理执行计划,描述了任务的计算逻辑、所需资源和并行度,同时也描述任务产出数据的划分方式,此外还描述了任务对数据的依赖关系以及数据传输方式。

通过它,Flink 就能知道如何创建和调度作业的所有任务,从而完成作业的执行。

1生成StreamGraph

StreamGraph在client中生成,是根据用户通过 Stream API 编写的代码生成的最初的图。用来表示程序的拓扑结构。起始入口是CliFronted也就是客户端类,他会遍历transformations 根据遍历出的算子的类型 ,oneInput类型生成StreamNode,partition类型生成边StreamEdge,顶点和边,分别添加到相对应的list中。之后根据指定的算子分区策略partitioner,和顺序决定每个节点的入和出的StreamNode和partitioner, 创建出edge将该streamEdge添加到上游的输出 下游的输入。最终 中 env.setPipeline(streamGraph) 把streamGraph->pipline 将streamGraph添加到pipline中,供下游使用生成JobGraph另外根据 StreamGraphGenerator函数,也就是 streamGraph的构造函数 ,他把算法由是否做shuffle来进行了 两种不同的构造方法 transformOneInputTransform 和 transformPartition

/**

* Transforms one {@code StreamTransformation}.

*

*

This checks whether we already transformed it and exits early in that case. If not it

* delegates to one of the transformation specific methods.

*/

private Collectiontransform(StreamTransformation transform) {

if (alreadyTransformed.containsKey(transform)) {

return alreadyTransformed.get(transform);

}

LOG.debug("Transforming " + transform);

if (transform.getMaxParallelism() <= 0) {

// if the max parallelism hasn't been set, then first use the job wide max parallelism

// from the ExecutionConfig.

int globalMaxParallelismFromConfig = env.getConfig().getMaxParallelism();

if (globalMaxParallelismFromConfig > 0) {

transform.setMaxParallelism(globalMaxParallelismFromConfig);

}

}

// call at least once to trigger exceptions about MissingTypeInfo

transform.getOutputType();

CollectiontransformedIds;

if (transform instanceof OneInputTransformation) {

transformedIds = transformOneInputTransform((OneInputTransformation) transform);

} else if (transform instanceof TwoInputTransformation) {

transformedIds = transformTwoInputTransform((TwoInputTransformation) transform);

} else if (transform instanceof SourceTransformation) {

transformedIds = transformSource((SourceTransformation) transform);

} else if (transform instanceof SinkTransformation) {

transformedIds = transformSink((SinkTransformation) transform);

} else if (transform instanceof UnionTransformation) {

transformedIds = transformUnion((UnionTransformation) transform);

} else if (transform instanceof SplitTransformation) {

transformedIds = transformSplit((SplitTransformation) transform);

} else if (transform instanceof SelectTransformation) {

transformedIds = transformSelect((SelectTransformation) transform);

} else if (transform instanceof FeedbackTransformation) {

transformedIds = transformFeedback((FeedbackTransformation) transform);

} else if (transform instanceof CoFeedbackTransformation) {

transformedIds = transformCoFeedback((CoFeedbackTransformation) transform);

} else if (transform instanceof PartitionTransformation) {

transformedIds = transformPartition((PartitionTransformation) transform);

} else if (transform instanceof SideOutputTransformation) {

transformedIds = transformSideOutput((SideOutputTransformation) transform);

} else {

throw new IllegalStateException("Unknown transformation: " + transform);

}

// need this check because the iterate transformation adds itself before

// transforming the feedback edges

if (!alreadyTransformed.containsKey(transform)) {

alreadyTransformed.put(transform, transformedIds);

}

if (transform.getBufferTimeout() >= 0) {

streamGraph.setBufferTimeout(transform.getId(), transform.getBufferTimeout());

}

if (transform.getUid() != null) {

streamGraph.setTransformationUID(transform.getId(), transform.getUid());

}

if (transform.getUserProvidedNodeHash() != null) {

streamGraph.setTransformationUserHash(transform.getId(), transform.getUserProvidedNodeHash());

}

if (transform.getMinResources() != null && transform.getPreferredResources() != null) {

streamGraph.setResources(transform.getId(), transform.getMinResources(), transform.getPreferredResources());

}

return transformedIds;

}

算子类型分为两种,通过算子是否有做shuffle来讲算子分为 oneInput类型 与 partition类型 两类; oneInput类型对应operator; partition对应keyby

[(transformations是一个list,依次存放了 用户代码里的算法), 所有的算子(如map)都会调用名为transform的一个方法,会把算子存到transformations中,从而构建成一棵树]

/**

* Method for passing user defined operators created by the given factory along with the type 拼多多帮助力入口

* information that will transform the DataStream.

*

*

This method uses the rather new operator factories and should only be used when custom

* factories are needed.

*

* @param operatorName name of the operator, for logging purposes

* @param outTypeInfo the output type of the operator

* @param operatorFactory the factory for the operator.

* @paramtype of the return stream

* @return the data stream constructed.

*/

@PublicEvolving

publicSingleOutputStreamOperatortransform(

String operatorName,

TypeInformationoutTypeInfo,

OneInputStreamOperatorFactoryoperatorFactory) {

return doTransform(operatorName, outTypeInfo, operatorFactory);

}

protectedSingleOutputStreamOperatordoTransform(

String operatorName,

TypeInformationoutTypeInfo,

StreamOperatorFactoryoperatorFactory) {

// read the output type of the input Transform to coax out errors about MissingTypeInfo

transformation.getOutputType();

OneInputTransformationresultTransform =

new OneInputTransformation<>(

this.transformation,

operatorName,

operatorFactory,

outTypeInfo,

environment.getParallelism());

@SuppressWarnings({"unchecked", "rawtypes"})

SingleOutputStreamOperatorreturnStream =

new SingleOutputStreamOperator(environment, resultTransform);

getExecutionEnvironment().addOperator(resultTransform);

return returnStream;

}

/**

* Adds an operator to the list of operators that should be executed when calling {@link

* #execute}.

*

*

When calling {@link #execute()} only the operators that where previously added to the list

* are executed.

*

*

This is not meant to be used by users. The API methods that create operators must call

* this method.

*/

@Internal

public void addOperator(Transformation transformation) {

Preconditions.checkNotNull(transformation, "transformation must not be null.");

this.transformations.add(transformation);

}

可以看到上述所有算子都被按执行顺序添加进了transformations 这个 List 中,接下来就是根据算子是否有座shuffle操作来把算子分成两类,分别是StreamNode的非shuffle算子, 以及上下游做shuffle的streamEdge操作,并且将streamEdge操作 与 StreamNode 绑定,来形成一个有前后顺序的DAG图

protected StreamNode addNode(

Integer vertexID,

@Nullable String slotSharingGroup,

@Nullable String coLocationGroup,

ClassvertexClass,

StreamOperatorFactory operatorFactory,

String operatorName) {

if (streamNodes.containsKey(vertexID)) {

throw new RuntimeException("Duplicate vertexID " + vertexID);

}

StreamNode vertex =

new StreamNode(

vertexID,

slotSharingGroup,

coLocationGroup,

operatorFactory,

operatorName,

vertexClass);

streamNodes.put(vertexID, vertex);

return vertex;

}

public void addEdge(Integer upStreamVertexID, Integer downStreamVertexID, int typeNumber) {

addEdgeInternal(

upStreamVertexID,

downStreamVertexID,

typeNumber,

null,

new ArrayList(),

null,

null);

}

private void addEdgeInternal(

Integer upStreamVertexID,

Integer downStreamVertexID,

int typeNumber,

StreamPartitioner partitioner,

ListoutputNames,

OutputTag outputTag,

ShuffleMode shuffleMode) {

if (virtualSideOutputNodes.containsKey(upStreamVertexID)) {

int virtualId = upStreamVertexID;

upStreamVertexID = virtualSideOutputNodes.get(virtualId).f0;

if (outputTag == null) {

outputTag = virtualSideOutputNodes.get(virtualId).f1;

}

addEdgeInternal(

upStreamVertexID,

downStreamVertexID,

typeNumber,

partitioner,

null,

outputTag,

shuffleMode);

} else if (virtualPartitionNodes.containsKey(upStreamVertexID)) {

int virtualId = upStreamVertexID;

upStreamVertexID = virtualPartitionNodes.get(virtualId).f0;

if (partitioner == null) {

partitioner = virtualPartitionNodes.get(virtualId).f1;

}

shuffleMode = virtualPartitionNodes.get(virtualId).f2;

addEdgeInternal(

upStreamVertexID,

downStreamVertexID,

typeNumber,

partitioner,

outputNames,

outputTag,

shuffleMode);

} else {

StreamNode upstreamNode = getStreamNode(upStreamVertexID);

StreamNode downstreamNode = getStreamNode(downStreamVertexID);

// If no partitioner was specified and the parallelism of upstream and downstream

// operator matches use forward partitioning, use rebalance otherwise.

if (partitioner == null

&& upstreamNode.getParallelism() == downstreamNode.getParallelism()) {

partitioner = new ForwardPartitioner

标签: 拼多多助力网站平台 赚钱项目 拼多多助力网 首码项目 手赚APP
拼多多助力营销技巧