当前位置:   article > 正文

flink streamGraph生成_flink streamgraph select 虚拟节点

flink streamgraph select 虚拟节点

当完成DataStream的配置后,调用其中环境上下文StreamExecutionEnvironment的getStreamGrahph()方法即可生成关于该DataStream的streamGraph。

  1. @Internal
  2. public StreamGraph getStreamGraph() {
  3. if (transformations.size() <= 0) {
  4. throw new IllegalStateException("No operators defined in streaming topology. Cannot execute.");
  5. }
  6. return StreamGraphGenerator.generate(this, transformations);
  7. }
  8. public static StreamGraph generate(StreamExecutionEnvironment env, List<StreamTransformation<?>> transformations) {
  9. return new StreamGraphGenerator(env).generateInternal(transformations);
  10. }

生成streamGraph实则是在StreamGraphGenerator的generate()方法中,参数除了调用的env本身之外还需要env当中表示被转换的DataStream中所有操作streamTransformation的数组。

之后会在generate()方法中会生成一个StreamGraphGenerator并调用generateInternal()方法根据DataStream中的所有transformation开始生成streamGraph。

  1. private StreamGraph generateInternal(List<StreamTransformation<?>> transformations) {
  2. for (StreamTransformation<?> transformation: transformations) {
  3. transform(transformation);
  4. }
  5. return streamGraph;
  6. }

此处会遍历所有的streamTransformation,依次调用transform()方法来转换至streamGraph。

 

在transform()方法中,会根据所有类型的transform分别处理,具体的代码如下。

  1. if (transform instanceof OneInputTransformation<?, ?>) {
  2. transformedIds = transformOneInputTransform((OneInputTransformation<?, ?>) transform);
  3. } else if (transform instanceof TwoInputTransformation<?, ?, ?>) {
  4. transformedIds = transformTwoInputTransform((TwoInputTransformation<?, ?, ?>) transform);
  5. } else if (transform instanceof SourceTransformation<?>) {
  6. transformedIds = transformSource((SourceTransformation<?>) transform);
  7. } else if (transform instanceof SinkTransformation<?>) {
  8. transformedIds = transformSink((SinkTransformation<?>) transform);
  9. } else if (transform instanceof UnionTransformation<?>) {
  10. transformedIds = transformUnion((UnionTransformation<?>) transform);
  11. } else if (transform instanceof SplitTransformation<?>) {
  12. transformedIds = transformSplit((SplitTransformation<?>) transform);
  13. } else if (transform instanceof SelectTransformation<?>) {
  14. transformedIds = transformSelect((SelectTransformation<?>) transform);
  15. } else if (transform instanceof FeedbackTransformation<?>) {
  16. transformedIds = transformFeedback((FeedbackTransformation<?>) transform);
  17. } else if (transform instanceof CoFeedbackTransformation<?>) {
  18. transformedIds = transformCoFeedback((CoFeedbackTransformation<?>) transform);
  19. } else if (transform instanceof PartitionTransformation<?>) {
  20. transformedIds = transformPartition((PartitionTransformation<?>) transform);
  21. } else if (transform instanceof SideOutputTransformation<?>) {
  22. transformedIds = transformSideOutput((SideOutputTransformation<?>) transform);
  23. } else {
  24. throw new IllegalStateException("Unknown transformation: " + transform);
  25. }

以一个map操作节点为例子,如果是map类型的transformation,那么将会在此处通过transformOneInputTransform()方法构造成streamGraph中的streamNode。

  1. private <IN, OUT> Collection<Integer> transformOneInputTransform(OneInputTransformation<IN, OUT> transform) {
  2. Collection<Integer> inputIds = transform(transform.getInput());
  3. // the recursive call might have already transformed this
  4. if (alreadyTransformed.containsKey(transform)) {
  5. return alreadyTransformed.get(transform);
  6. }
  7. String slotSharingGroup = determineSlotSharingGroup(transform.getSlotSharingGroup(), inputIds);
  8. streamGraph.addOperator(transform.getId(),
  9. slotSharingGroup,
  10. transform.getCoLocationGroupKey(),
  11. transform.getOperator(),
  12. transform.getInputType(),
  13. transform.getOutputType(),
  14. transform.getName());
  15. if (transform.getStateKeySelector() != null) {
  16. TypeSerializer<?> keySerializer = transform.getStateKeyType().createSerializer(env.getConfig());
  17. streamGraph.setOneInputStateKey(transform.getId(), transform.getStateKeySelector(), keySerializer);
  18. }
  19. streamGraph.setParallelism(transform.getId(), transform.getParallelism());
  20. streamGraph.setMaxParallelism(transform.getId(), transform.getMaxParallelism());
  21. for (Integer inputId: inputIds) {
  22. streamGraph.addEdge(inputId, transform.getId(), 0);
  23. }
  24. return Collections.singleton(transform.getId());
  25. }

其中,在一开始,会得到该transformation的输入,尝试先于当前节点之前先转换其输入节点,由此,会一路往上回溯到source节点开始转换。

当其上游节点全部转换为streamGraph中的节点之后,就会开始当前节点的转换,而后,会根据determineSlotSharingGroup()方法确定该节点的slot共享名。

  1. private String determineSlotSharingGroup(String specifiedGroup, Collection<Integer> inputIds) {
  2. if (specifiedGroup != null) {
  3. return specifiedGroup;
  4. } else {
  5. String inputGroup = null;
  6. for (int id: inputIds) {
  7. String inputGroupCandidate = streamGraph.getSlotSharingGroup(id);
  8. if (inputGroup == null) {
  9. inputGroup = inputGroupCandidate;
  10. } else if (!inputGroup.equals(inputGroupCandidate)) {
  11. return "default";
  12. }
  13. }
  14. return inputGroup == null ? "default" : inputGroup;
  15. }
  16. }

在这里,如果没有专门制定相应的共享名,那么则会根据其上游输入节点进行确定,如果上游不一致,则会采用默认的default来指定。

 

在确定完共享名之后,streamGraph就会通过addOperaor()方法来将该节点加入到streamGraph中。在该方法中首先根据节点类型加入到stramGraph的streanNodes数组中,之后根据输入输出的类型得到相应的类型序列化对象设置到相应的节点中。

 

之后,会遍历当前节点的所有输入节点,调用addEdge()方法依次生成节点的边streamEdge。

  1. public void addEdge(Integer upStreamVertexID, Integer downStreamVertexID, int typeNumber) {
  2. addEdgeInternal(upStreamVertexID,
  3. downStreamVertexID,
  4. typeNumber,
  5. null,
  6. new ArrayList<String>(),
  7. null);
  8. }
  9. private void addEdgeInternal(Integer upStreamVertexID,
  10. Integer downStreamVertexID,
  11. int typeNumber,
  12. StreamPartitioner<?> partitioner,
  13. List<String> outputNames,
  14. OutputTag outputTag) {
  15. if (virtualSideOutputNodes.containsKey(upStreamVertexID)) {
  16. int virtualId = upStreamVertexID;
  17. upStreamVertexID = virtualSideOutputNodes.get(virtualId).f0;
  18. if (outputTag == null) {
  19. outputTag = virtualSideOutputNodes.get(virtualId).f1;
  20. }
  21. addEdgeInternal(upStreamVertexID, downStreamVertexID, typeNumber, partitioner, null, outputTag);
  22. } else if (virtualSelectNodes.containsKey(upStreamVertexID)) {
  23. int virtualId = upStreamVertexID;
  24. upStreamVertexID = virtualSelectNodes.get(virtualId).f0;
  25. if (outputNames.isEmpty()) {
  26. // selections that happen downstream override earlier selections
  27. outputNames = virtualSelectNodes.get(virtualId).f1;
  28. }
  29. addEdgeInternal(upStreamVertexID, downStreamVertexID, typeNumber, partitioner, outputNames, outputTag);
  30. } else if (virtualPartitionNodes.containsKey(upStreamVertexID)) {
  31. int virtualId = upStreamVertexID;
  32. upStreamVertexID = virtualPartitionNodes.get(virtualId).f0;
  33. if (partitioner == null) {
  34. partitioner = virtualPartitionNodes.get(virtualId).f1;
  35. }
  36. addEdgeInternal(upStreamVertexID, downStreamVertexID, typeNumber, partitioner, outputNames, outputTag);
  37. } else {
  38. StreamNode upstreamNode = getStreamNode(upStreamVertexID);
  39. StreamNode downstreamNode = getStreamNode(downStreamVertexID);
  40. // If no partitioner was specified and the parallelism of upstream and downstream
  41. // operator matches use forward partitioning, use rebalance otherwise.
  42. if (partitioner == null && upstreamNode.getParallelism() == downstreamNode.getParallelism()) {
  43. partitioner = new ForwardPartitioner<Object>();
  44. } else if (partitioner == null) {
  45. partitioner = new RebalancePartitioner<Object>();
  46. }
  47. if (partitioner instanceof ForwardPartitioner) {
  48. if (upstreamNode.getParallelism() != downstreamNode.getParallelism()) {
  49. throw new UnsupportedOperationException("Forward partitioning does not allow " +
  50. "change of parallelism. Upstream operation: " + upstreamNode + " parallelism: " + upstreamNode.getParallelism() +
  51. ", downstream operation: " + downstreamNode + " parallelism: " + downstreamNode.getParallelism() +
  52. " You must use another partitioning strategy, such as broadcast, rebalance, shuffle or global.");
  53. }
  54. }
  55. StreamEdge edge = new StreamEdge(upstreamNode, downstreamNode, typeNumber, outputNames, partitioner, outputTag);
  56. getStreamNode(edge.getSourceId()).addOutEdge(edge);
  57. getStreamNode(edge.getTargetId()).addInEdge(edge);
  58. }
  59. }

此处,可以看到一条StreamEdge的组成主要有以下几个要素,起始节点,终点节点,输出分区类型,output选择器选择的字段名。

在生成了相应的streamEdge会分别加在起始节点的 out边和终点节点的input边上。

至此,一个节点被加入到streamGraph的流程结束。

 

之后以spilt,select类型的transform为例子。

  1. private <T> Collection<Integer> transformSplit(SplitTransformation<T> split) {
  2. StreamTransformation<T> input = split.getInput();
  3. Collection<Integer> resultIds = transform(input);
  4. // the recursive transform call might have transformed this already
  5. if (alreadyTransformed.containsKey(split)) {
  6. return alreadyTransformed.get(split);
  7. }
  8. for (int inputId : resultIds) {
  9. streamGraph.addOutputSelector(inputId, split.getOutputSelector());
  10. }
  11. return resultIds;
  12. }

spilt类型的transformation不会加入到streamGraph中,而是会将split中的outputSelector加入到上游节点中。

  1. private <T> Collection<Integer> transformSelect(SelectTransformation<T> select) {
  2. StreamTransformation<T> input = select.getInput();
  3. Collection<Integer> resultIds = transform(input);
  4. // the recursive transform might have already transformed this
  5. if (alreadyTransformed.containsKey(select)) {
  6. return alreadyTransformed.get(select);
  7. }
  8. List<Integer> virtualResultIds = new ArrayList<>();
  9. for (int inputId : resultIds) {
  10. int virtualId = StreamTransformation.getNewNodeId();
  11. streamGraph.addVirtualSelectNode(inputId, virtualId, select.getSelectedNames());
  12. virtualResultIds.add(virtualId);
  13. }
  14. return virtualResultIds;
  15. }

而select类型的节点则会加入到streamGraph中的虚拟节点中,在之前的edge构造中,就将根据实际节点的id得到虚拟select节点,将相应的outputName加入到edge之中。

分区策略也将类似select的形式生成虚拟节点。

  1. private <T> Collection<Integer> transformPartition(PartitionTransformation<T> partition) {
  2. StreamTransformation<T> input = partition.getInput();
  3. List<Integer> resultIds = new ArrayList<>();
  4. Collection<Integer> transformedIds = transform(input);
  5. for (Integer transformedId: transformedIds) {
  6. int virtualId = StreamTransformation.getNewNodeId();
  7. streamGraph.addVirtualPartitionNode(transformedId, virtualId, partition.getPartitioner());
  8. resultIds.add(virtualId);
  9. }
  10. return resultIds;
  11. }

类似的union也不会引起新的节点生成,只是会统计所有输入节点id,交由统一在下一个实际节点中一次获取。

 

 

最后生成的streamGraph可以转化为下方类似的json。

{"nodes":[{"id":1,"type":"Source: Collection Source","pact":"Data Source","contents":"Source: Collection Source","parallelism":1},{"id":3,"type":"Map","pact":"Operator","contents":"Map","parallelism":8,"predecessors":[{"id":1,"ship_strategy":"REBALANCE","side":"second"}]},{"id":7,"type":"Map","pact":"Operator","contents":"Map","parallelism":8,"predecessors":[{"id":3,"ship_strategy":"BROADCAST","side":"second"}]},{"id":9,"type":"Map","pact":"Operator","contents":"Map","parallelism":8,"predecessors":[{"id":3,"ship_strategy":"FORWARD","side":"second"}]},{"id":13,"type":"Map","pact":"Operator","contents":"Map","parallelism":8,"predecessors":[{"id":3,"ship_strategy":"FORWARD","side":"second"}]},{"id":17,"type":"Map","pact":"Operator","contents":"Map","parallelism":8,"predecessors":[{"id":3,"ship_strategy":"FORWARD","side":"second"}]},{"id":24,"type":"Map","pact":"Operator","contents":"Map","parallelism":8,"predecessors":[{"id":9,"ship_strategy":"BROADCAST","side":"second"},{"id":13,"ship_strategy":"GLOBAL","side":"second"},{"id":17,"ship_strategy":"SHUFFLE","side":"second"}]},{"id":8,"type":"Sink: Unnamed","pact":"Data Sink","contents":"Sink: Unnamed","parallelism":8,"predecessors":[{"id":7,"ship_strategy":"FORWARD","side":"second"}]},{"id":25,"type":"Sink: Unnamed","pact":"Data Sink","contents":"Sink: Unnamed","parallelism":8,"predecessors":[{"id":24,"ship_strategy":"FORWARD","side":"second"}]}]}

在flink给出的https://flink.apache.org/visualizer/ 上可以转化为可视化图。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/794413
推荐阅读
相关标签
  

闽ICP备14008679号