datax(10): 源码解读Communication(Datax通讯类)「建议收藏」

datax(10): 源码解读Communication(Datax通讯类)「建议收藏」前面看了datax的通讯机制,继续看源码—具体的通讯类Communication。根据datax的运行模式的区别,数据的收集会有些区别,这篇文章都是讲的在standalone模式下。一、communication概述DataX所有的统计信息都会保存到Communication类里面。Communication支持下列数据的统计计数器,比如读取的字节速度,写入成功的数据条数/***所有的数值key-value对**/privateMap<String.

大家好,又见面了,我是你们的朋友全栈君。

前面看了datax的通讯机制,继续看源码—具体的通讯类 Communication。根据datax的运行模式的区别, 数据的收集会有些区别,这篇文章都是讲的在standalone模式下。


一、communication概述

DataX所有的统计信息都会保存到Communication类里面。

Communication支持下列数据的统计

  1. 计数器,比如读取的字节速度,写入成功的数据条数
  2. 统计的时间点 字符串类型的消息
  3. 执行时的异常
  4. 执行的状态, 比如成功或失败

  /** * 所有的数值key-value对 * */
  private Map<String, Number> counter;

  /** * 运行状态 * */
  private State state;

  /** * 异常记录 * */
  private Throwable throwable;

  /** * 记录的timestamp * */
  private long timestamp;

  /** * task给job的信息 * */
  Map<String, List<String>> message;
  

communication继承关系
在这里插入图片描述

如果需要汇总多个Communication的数据,Communication提供了mergeFrom方法。根据不同的数据类型,对应着不同的操作计数器类型,相同的key的数值累加

  • 合并异常,当自身的异常为null,才合并别的异常

  • 合并状态,如果有任意一个的状态失败了,那么返回失败的状态。如果有任意一个的状态正在运行,那么返回正在运行的状态

  • 合并消息, 相同的key的消息添加到同一个列表


二、communication主要方法

在这里插入图片描述


三、Communication的管理类

对于每个task组都有一个单独的Communication,用来存储这个组的统计数据。对于这些Communication,在LocalTGCommunicationManager类实现了集中管理。接下来看看LocalTGCommunicationManager的原理。

LocalTGCommunicationManager有个重要的属性taskGroupCommunicationMap,它是一个Map,保存了每个task组的统计数据。


public final class LocalTGCommunicationManager { 
   

  private static Map<Integer, Communication> taskGroupCommunicationMap = new ConcurrentHashMap<>();

  /** * 根据tgId注册comm * 当task组在初始化的时候,都会向LocalTGCommunicationManager这里注册。// 这里只是简单保存到taskGroupCommunicationMap变量里 * @param taskGroupId * @param communication */
  public static void registerTaskGroupCommunication(int taskGroupId, Communication communication) { 
   
    taskGroupCommunicationMap.put(taskGroupId, communication);
  }

  /** * 获取(合并)tg里面所有的comm * * @return Communication */
  public static Communication getJobCommunication() { 
   
    Communication communication = new Communication();
    communication.setState(State.SUCCEEDED);

    for (Communication taskGroupCommunication : taskGroupCommunicationMap.values()) { 
   
      communication.mergeFrom(taskGroupCommunication);
    }
    return communication;
  }

  /** * 采用获取taskGroupId后再获取对应communication的方式, * 防止map遍历时修改,同时也防止对map key-value对的修改 * * @return */
  public static Set<Integer> getTaskGroupIdSet() { 
   
    return taskGroupCommunicationMap.keySet();
  }

  public static Communication getTaskGroupCommunication(int taskGroupId) { 
   
    Validate.isTrue(taskGroupId >= 0, "taskGroupId不能小于0");
    return taskGroupCommunicationMap.get(taskGroupId);
  }


  /** * 根据tgId 将taskGroupCommunicationMap中没有的comm 插入 * @param taskGroupId * @param comm */
  public static void updateTaskGroupCommunication(final int taskGroupId, final Communication comm) { 
   
    Validate.isTrue(taskGroupCommunicationMap.containsKey(
        taskGroupId), String.format("taskGroupCommunicationMap中没有注册taskGroupId[%d]的Communication," +
        "无法更新该taskGroup的信息", taskGroupId));
    taskGroupCommunicationMap.put(taskGroupId, comm);
  }

  public static void clear() { 
   
    taskGroupCommunicationMap.clear();
  }

  public static Map<Integer, Communication> getTaskGroupCommunicationMap() { 
   
    return taskGroupCommunicationMap;
  }
}

四、谁会注册Communication

AbstractScheduler会根据切分后的任务,为每个task组注册一个Communication。registerCommunication接收task配置列表,里面每个配置都包含了task group id。

进行注册communication的类

  • AbstractScheduler的schedule方法里 registerCommunication
  • TaskGroupContainer的start方法里 registerCommunication
  • AbstractTGContainerCommunicator的registerCommunication方法
  • AbstractContainerCommunicator的registerCommunication方法
  • StandAloneJobContainerCommunicator的registerCommunication方法

在这里插入图片描述


五、更新communication统计数据

主要更新communication的类
在这里插入图片描述

每个任务执行都会对应着Channel,Channel当每处理一条数据时,都会更新对应Communication的统计信息。
例如下面的pull方法是Writer从Channel拉取数据,每次pull的时候,都会调用statPull函数,会更新写入数据条数和字节数的信息。


public abstract class Channel{ 
   

    private Communication currentCommunication;

    public Record pull() { 
   
        Record record = this.doPull();
        this.statPull(1L, record.getByteSize());
        return record;
    }
    
    /** * statPull方法,并没有限速。因为数据的整个流程是Reader -》 Channle -》 Writer, Reader的push速度限制了, * Writer的pull速度也就没必要限速 * * @param recordSize * @param byteSize */
    private void statPull(long recordSize, long byteSize) { 
   
        currentCommunication.increaseCounter(CommunicationTool.WRITE_RECEIVED_RECORDS, recordSize);
        currentCommunication.increaseCounter(CommunicationTool.WRITE_RECEIVED_BYTES, byteSize);
    }
    

六、收集communication统计数据

  1. AbstractScheduler想统计汇总后的数据,需要调用AbstractContainerCommunicator的collect方法

  2. StandAloneJobContainerCommunicator继承AbstractContainerCommunicator,实现了collect方法,它会调用AbstractCollector的collectFromTaskGroup方法获取数据

  3. ProcessInnerCollector实现了AbstractCollector的collectFromTaskGroup方法,它会调用LocalTGCommunicationManager的getJobCommunication方法, getJobCommunication方法会统计所有task的数据,然后返回。

在这里插入图片描述


注:

  1. 对源码进行略微改动,主要修改为 1 阿里代码规约扫描出来的,2 clean code;

  2. 所有代码都已经上传到github(master分支和dev),可以免费白嫖

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/145489.html原文链接:https://javaforall.net

(0)
上一篇 2022年5月17日 上午10:00
下一篇 2022年5月17日 上午10:20


相关推荐

  • 软件测试人员应该如何介绍自己测试过的项目工作_软件测试项目介绍

    软件测试人员应该如何介绍自己测试过的项目工作_软件测试项目介绍测试人员在找工作的过程中,通常有一个问题是很难绕开的。就是要如何向别人介绍自己之前做过的项目。下面我们就这个问题简单的做一些分析。要解决这个问题,大体上可以分为如下几个步骤:1、对项目进行基本介绍2、说明自己负责测试的模块3、针对部分模块展开进行说明一.对项目进行基本介绍以下就以一个简单的项目进行介绍说明:最近测试的Tpshop项目是一个B/S架构的Web项…

    2022年10月20日
    3
  • ajax长轮询 spring mvc,springmvc ajax 长轮询

    ajax长轮询 spring mvc,springmvc ajax 长轮询前台代码:$(function(){functionpoll(){varparam={“searchType”:”1″,”key”:”0100008″,”timestamp”:”1409382910″,”sign”:”123″};$.ajax({type:”POST”,contentType:”application/json;charset=utf-8″,url:”xxxx”,da…

    2022年10月10日
    6
  • 在Origin绘图和表格中插入Latex公式

    在Origin绘图和表格中插入Latex公式关于Origin与Latex结合应用下载:originlatexapp下载使用教程origin安装app教程官方教程参考https://baijiahao.baidu.com/s?id=1666395737690093701&wfr=spider&for=pc

    2022年5月31日
    68
  • 激光slam综述_激光slam原理

    激光slam综述_激光slam原理本篇是记录曾书格老师的课程《激光slam理论与实践》先贴一下个人总结(有理解的不正确的,麻烦指出来):第一章:激光SLAM简要介绍1、输出Metricalmap尺度地图,slam分为两种:基于滤波的filter-based的SLAM,和Graph-based的SLAM。2、(1)基于Graph-based的代表是cartographer,可以修复t时刻之前的误差分为两部…

    2022年8月23日
    11
  • docker(8)Dockerfile指令介绍「建议收藏」

    docker(8)Dockerfile指令介绍「建议收藏」前言Dockerfile是一个用来构建镜像的文本文件,文本内容包含了一条条构建镜像所需的指令和说明。Dockerfile简介Dockerfile是用来构建Docker镜像的构建文件,是由一系列

    2022年7月30日
    9
  • 如何学习verilog,如何快速入门?

    前言害怕真的有人不知道verilog是什么东西,于是就给把百度给搬来了!VerilogHDL是一种硬件描述语言,以文本形式来描述数字系统硬件的结构和行为的语言,用它可以表示逻辑电路图、逻辑表达式,还可以表示数字逻辑系统所完成的逻辑功能。VerilogHDL和VHDL是世界上最流行的两种硬件描述语言,都是在20世纪80年代中期开发出来的。前者由GatewayDesignAutomation公司(该公司于1989年被Cadence公司收购)开发。两种HDL均为IEEE标准。之前的文章《IC前端

    2022年4月7日
    64

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号