• 【源码】Flink 算子 chain 在一起的条件


    Flink 任务的图结构中,部分算子是 chain 在一起的,因为 chain 在一起有很多好处(减少序列化和网络开销,提高效率),而算子 chain 在一起是需要条件的

    Flink 任务在生成 JobGraph 的时候,会加入 chain 的概念,会判断算子能否 chain 在一起

    首先在 env 中不能 禁用 算子chain

    env.disableOperatorChaining()

    其次chain 在一起的算子,还需要满足如下 7 个条件:

    1、下游节点只有一个输入边
    2、上下游算子在同一个 slotSharingGroup
    3、上下游算子可以 chain 在一起
    4、上下游算子数据分发策略是 ForwardPartitioner
    5、上下游算子数据 shuffleMode 不是 BATCH(是: PIPELINED,UNDEFINED)
    6、上下游算子并发度一样
    7、StreamGraph 允许 算子 chain 在一起 (env 允许)

    // 检测上下游是否能 chain 在一起
    public static boolean isChainable(StreamEdge edge, StreamGraph streamGraph) {
      StreamNode upStreamVertex = streamGraph.getSourceVertex(edge);
      StreamNode downStreamVertex = streamGraph.getTargetVertex(edge);
    
      // 下游节点输入边为 1
      return downStreamVertex.getInEdges().size() == 1
        // 上下游是同一个 sharingGroup
        && upStreamVertex.isSameSlotSharingGroup(downStreamVertex)
        // 上下游算子策略能 chain 在一起
        && areOperatorsChainable(upStreamVertex, downStreamVertex, streamGraph)
        // 上下游边的分区策略是 ForwardPartitioner
        && (edge.getPartitioner() instanceof ForwardPartitioner)
        // shuffleMode 不是 batch
        && edge.getShuffleMode() != ShuffleMode.BATCH
        // 上下游并发度一样
        && upStreamVertex.getParallelism() == downStreamVertex.getParallelism()
        // streamGraph 是可以 chain
        && streamGraph.isChainingEnabled();
    }
    
    @VisibleForTesting
    static boolean areOperatorsChainable(
      StreamNode upStreamVertex,
      StreamNode downStreamVertex,
      StreamGraph streamGraph) {
      StreamOperatorFactory<?> upStreamOperator = upStreamVertex.getOperatorFactory();
      StreamOperatorFactory<?> downStreamOperator = downStreamVertex.getOperatorFactory();
      // 上下游算子 有工厂类
      if (downStreamOperator == null || upStreamOperator == null) {
        return false;
      }
      // 上游的策略是 NEVER , 下游的策略不是 ALWAYS
      if (upStreamOperator.getChainingStrategy() == ChainingStrategy.NEVER ||
        downStreamOperator.getChainingStrategy() != ChainingStrategy.ALWAYS) {
        return false;
      }
    
      // yielding operators cannot be chained to legacy sources
      if (downStreamOperator instanceof YieldingOperatorFactory) {
        // unfortunately the information that vertices have been chained is not preserved at this point
        return !getHeadOperator(upStreamVertex, streamGraph).isStreamSource();
      }
      // 其他 都是可以 chain 的
      return true;
    }

    欢迎关注Flink菜鸟公众号,会不定期更新Flink(开发技术)相关的推文

  • 相关阅读:
    用track给视频添加字幕,浏览器被拦截 ,怎么解决?
    小区内公共车位物业管理公司是否有出租权 纵一苇之所如
    使用自动精灵在Android设备上重复播放语音500次
    移动端 持久化数据在客户端
    [go每日一库] go借助net/http包实现客户端get、post请求 Marathon
    [go每日一库] golang gin框架路由设置(全面) Marathon
    [go每日一库] golang中借助json等库实现struct和map互转 Marathon
    [go每日一库] golang validator参数校验 Marathon
    [go每日一库] go语言文件处理 Marathon
    [go每日一库] golang 通过os/exec执行shell命令 Marathon
  • 原文地址:https://www.cnblogs.com/Springmoon-venn/p/14110351.html
Copyright © 2020-2023  润新知