datax(9):Job和TaskGroup的通讯机制

datax(9):Job和TaskGroup的通讯机制先后看完了TaskGroupContainer和JobContainer,梳理下他们的关系与职责;一,各自职责JobContainer:Job执行器,负责Job全局拆分、调度、前置语句和后置语句等工作的工作单元。类似Yarn中的JobTrackerTaskGroupContainer:TaskGroup执行器,负责执行一组Task的工作单元,类似Yarn中的TaskTracker(Yarn中的JobTracker和Yarn中的TaskTracker通过RPC进行通讯);二.

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

先后看完了TaskGroupContainer 和 JobContainer,梳理下他们的关系与职责;


一、各自职责

  1. JobContainer: Job执行器,负责Job全局拆分、调度、前置语句和后置语句等工作的工作单元。类似Yarn中的JobTracker

  2. TaskGroupContainer: TaskGroup执行器,负责执行一组Task的工作单元,类似Yarn中的TaskTracker(Yarn中的JobTracker和Yarn中的TaskTracker通过RPC进行通讯);


二、相互关系

  1. TaskGroupContainer向JobContainer上报信息,称JobContainer是TaskGroupContainer的上级;
  2. Task向TaskGroupContainer上报信息,称TaskGroupContainer是Task的上级。

在这里插入图片描述


三、TaskGroupContainer向JobContianer汇报过程

汇报源码逻辑是在TaskGroupContainer#reportTaskGroupCommunication这个方法中,方法的两个形参分别为lastTaskGroupContainerCommunication为上次汇报的信息,每次做数据统计的时候需要将当前communication的数据和lastTaskGroupContainerCommunication进行合并;taskCount为该TaskGroup的所有的任务数。

  1. 收集当前TaskGroupContainer对应所有Task的的communication,然后将其合并成一个communication。
    具体合并代码为步骤1,主要逻辑是在Communication#mergeFrom,它的主要功能将两communication的变量合并。主要关注下两个communication的状态合并,可以看到只要该TaskGroup中有一个Task的状态是FAILED或者KILLED就会将整个TaskGroup的状态标记为FAILED,当且仅当所有的任务的状态是SUCCEEDED,该TaskGroup的状态才能标记为SUCCEEDED。

  2. 生成新的reportCommunication作为该TaskGroupContainer上报给JobContianer的communication,
    主要是生成一些技术统计,比方说当前已经导入的记录数和字节数等。

  3. 上报给JobContianer,主要代码见步骤2,将该TaskGroupContainer最新的communication更新到StandAloneJobContainerCommunicator 能够够的得到的地方,即全局变量LocalTGCommunicationManager#taskGroupCommunicationMap中。


  private Communication reportTaskGroupCommunication(Communication lastTGContainerComm,int taskCnt) { 
   
    Communication nowTGContainerComm = this.containerCommunicator.collect();
    nowTGContainerComm.setTimestamp(System.currentTimeMillis());
    Communication reportComm = CommunicationTool
        .getReportCommunication(nowTGContainerComm, lastTGContainerComm, taskCnt);
    this.containerCommunicator.report(reportComm);
    return reportComm;
  }

步骤1


//AbstractCollector#collectFromTask
public Communication collectFromTask() { 
   
    Communication communication = new Communication();
    communication.setState(State.SUCCEEDED);

    for (Communication taskCommunication :
            this.taskCommunicationMap.values()) { 
   
    communication.mergeFrom(taskCommunication);
    }
    return communication;
}
Communication#mergeStateFrom
public synchronized State mergeStateFrom(final Communication otherComm) { 
   
        State retState = this.getState();
        if (otherComm == null) { 
   
            return retState;
        }

        if (this.state == State.FAILED || otherComm.getState() == State.FAILED
                || this.state == State.KILLED || otherComm.getState() == State.KILLED) { 
   
            retState = State.FAILED;
        } else if (this.state.isRunning() || otherComm.state.isRunning()) { 
   
            retState = State.RUNNING;
        }

        this.setState(retState);
        return retState;
}

步骤2


// StandaloneTGContainerCommunicator#report
public void report(Communication communication) { 
   
    super.getReporter().reportTGCommunication(super.taskGroupId, communication);
}

// ProcessInnerReporter
public void reportTGCommunication(Integer taskGroupId, Communication communication) { 
   
    LocalTGCommunicationManager.updateTaskGroupCommunication(taskGroupId, communication);
}

// LocalTGCommunicationManager#updateTaskGroupCommunication
public static void updateTaskGroupCommunication(final int taskGroupId,
                                                    final Communication communication) { 
   
        Validate.isTrue(taskGroupCommunicationMap.containsKey(
                taskGroupId), String.format("taskGroupCommunicationMap中没有注册taskGroupId[%d]的Communication," +
                "无法更新该taskGroup的信息", taskGroupId));
        taskGroupCommunicationMap.put(taskGroupId, communication);
}


四、TaskGroupContainer向JobContianer汇报时机

TaskGroupContainer向JobContainer汇报该TaskGroupContainer的执行情况的时机均在TaskGroupContainer#start中。

1、当前TaskGroup中有状态为FAILED或者KILLED的Task

如果一个Task只能执行一次(默认是1次,没有做重试)且该Task被标记为FAILED或者KILLED,马上将failedOrKilled这个变量标记为true并执行汇报逻辑。这种情况下除了汇报之后,还会抛出一个运行时异常,结束执行当前TaskGroupContainer的线程(TaskGroupContianer是在线程池中执行的)。


if (failedOrKilled) { 
   
    lastTaskGroupContainerCommunication = reportTaskGroupCommunication(
            lastTaskGroupContainerCommunication, taskCountInThisTaskGroup);

    throw DataXException.asDataXException(
        FrameworkErrorCode.PLUGIN_RUNTIME_ERROR, lastTaskGroupContainerCommunication.getThrowable());
}

2、上次失败的Task仍未结束

如果一个Task标记为FAILED或者KILLED,但是有重试逻辑就不会执行上面第1步的逻辑,而是会调用当前的Task对应TaskExecutor#shutdown,关闭当前的TaskExecutor。在调用TaskExecutor#shutdown一段时间发发现给TaskExecutor还没有关闭,触发下面逻辑,进行汇报的同时抛出异常。

if(now - failedTime > taskMaxWaitInMsec){ 
   
    markCommunicationFailed(taskId);
    reportTaskGroupCommunication(lastTaskGroupContainerCommunication, taskCountInThisTaskGroup);
    throw DataXException.asDataXException(CommonErrorCode.WAIT_TIME_EXCEED, "task failover等待超时");
}

3、TaskGroupContainer任务列表为空,所有任务都是成功执行, 搜集状态为SUCCEEDED

这个没什么好说的,该TaskGroup中所有的任务执行成功,该Job执行成功。

4、如果当前时间已经超出汇报时间的interval,那么我们需要马上汇报

可以理解为心跳了

5、TaskGroupContainer所在的线程正常结束时汇报一次

这个真没什么好说的了


五、JobContainer收到汇报之后的处理

JobContainer的处理逻辑是在dataX所在JVM的主线程中,具体是在AbstractScheduler#schedule中。

  1. 每隔一段时间,合并所有TaskGoupContianer汇报的信息,具体合并的逻辑和TaskGoupContianer合并Task的汇报信息差不多;

  2. 正常结束就正常退出;

  3. 处理isJobKilling,StandAloneScheduler并没有提供kill接口,咱不管;

  4. 重点关注下FAILED的逻辑,直接关闭当前Scheduler的线程池并在主线程中抛出异常,整个dataX进程退出。

// AbstractScheduler#schedule
public void schedule(List<Configuration> configurations) { 
   
        ...
        ...
        Communication lastJobContainerCommunication = new Communication();

        long lastReportTimeStamp = System.currentTimeMillis();
        try { 
   
            while (true) { 
   
                Communication nowJobContainerCommunication = this.containerCommunicator.collect();
                nowJobContainerCommunication.setTimestamp(System.currentTimeMillis());
                LOG.debug(nowJobContainerCommunication.toString());

                //汇报周期
                long now = System.currentTimeMillis();
                if (now - lastReportTimeStamp > jobReportIntervalInMillSec) { 
   
                    Communication reportCommunication = CommunicationTool
                            .getReportCommunication(nowJobContainerCommunication, lastJobContainerCommunication, totalTasks);

                    this.containerCommunicator.report(reportCommunication);
                    lastReportTimeStamp = now;
                    lastJobContainerCommunication = nowJobContainerCommunication;
                }

                errorLimit.checkRecordLimit(nowJobContainerCommunication);

                if (nowJobContainerCommunication.getState() == State.SUCCEEDED) { 
   
                    LOG.info("Scheduler accomplished all tasks.");
                    break;
                }

                if (isJobKilling(this.getJobId())) { 
   
                    dealKillingStat(this.containerCommunicator, totalTasks);
                } else if (nowJobContainerCommunication.getState() == State.FAILED) { 
   
                    dealFailedStat(this.containerCommunicator, nowJobContainerCommunication.getThrowable());
                }

                Thread.sleep(jobSleepIntervalInMillSec);
            }
        } catch (InterruptedException e) { 
   
            // 以 failed 状态退出
            LOG.error("捕获到InterruptedException异常!", e);

            throw DataXException.asDataXException(
                    FrameworkErrorCode.RUNTIME_ERROR, e);
        }

    }
    
    // ProcessInnerScheduler#dealFailedStat
    public void dealFailedStat(AbstractContainerCommunicator frameworkCollector, Throwable throwable) { 
   
        this.taskGroupContainerExecutorService.shutdownNow();
        throw DataXException.asDataXException(
                FrameworkErrorCode.PLUGIN_RUNTIME_ERROR, throwable);
    }


注:

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

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

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

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

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • directshow摄像头录像_open camera 使用方法

    directshow摄像头录像_open camera 使用方法Win1064+VS2012工程下载:http://download.csdn.net/detail/yulinxx/9263639建一个基于Dialog的MFC程序,而局如下:一个PIC控件,用于显示摄像头捕捉画面,几个按钮创建一个C++类,类名为:CCamera在CCamera.h中,需要包含#include#include”qedit.h”

    2022年10月12日
    1
  • 5g切片隔离原理_5G切片编排器

    5g切片隔离原理_5G切片编排器5G网络切片安全隔离机制与应用*毛玉欣1,陈林2,游世林1,闫新成1,吴强1【摘要】介绍了满足多样化垂直行业应用的5G网络服务化架构和网络切片实现。针对5G网络架构重构、网络部署形态的变化,研究提出了网络切片端到端安全隔离的实现方法,包括切片在接入网络、承载网络和核心网络中的隔离实现。结合典型行业应用的要求,给出了定制化切片的隔离实现案例。【关键词】垂直行业;服务化架构;网络切片;切片隔离引用格式:毛玉欣,陈林,游世林,等.5G网络切片安全隔离机制与应用[J].移动通信,2019,4

    2022年9月28日
    0
  • 限制普通域帐户将其他计算机加入域的方法!

    限制普通域帐户将其他计算机加入域的方法!

    2021年8月1日
    62
  • 【linux命令】 tree命令

    【linux命令】 tree命令文章目录Tree命令安装方法一,yum安装方法二,源码安装Tree命令安装方法一,yum安装命令:yuminstalltree方法二,源码安装1.下载安装包,地址:http://mama.indstate.edu/users/ice/tree/2.解压安装1)Linux环境(CentOS6.5)下安装a.解压tree-1.7.0.tgz文件,命令:tar-zxvftree-1.7.0.tgzb.进入解压目录中,命令:cdtree-1.7.0      c.安装文件,命令:

    2022年7月24日
    6
  • 基于yolov4的目标检测_yolov3目标检测

    基于yolov4的目标检测_yolov3目标检测1项目的克隆和必要的环境依赖1.1项目的克隆YOLOv5的代码是开源的,因此我们可以从github上克隆其源码。不得不说GitHub的确是全球最大的男性交友网站,里面的人个个都是人才,yolov5发布才一年左右的时间,YOLOv5就已经更新了5个分支了,分别是yolov5.1-yolov5.5分支。该项目就是利用的yolov5.5分支来作为讲解。首先打开yolov5的github的https://github.com/ultralytics/yolov5/tree…

    2022年10月16日
    0
  • 22、Windows10下局域网的两台电脑间传输文件

    22、Windows10下局域网的两台电脑间传输文件一、说明局域网内两台电脑传输文件使用windows自带的文件共享机制即可,不需要找专门的文件传输软件,下面来介绍使用方法。二、步骤1、控制面板2、网络和Internet3、网络和共享中心4、记住网络类型并点击更改高级共享设置5、根据对应的网络类型,在其下选择启用网络发现6、设置所有网络如下7、随便找一个文件夹设置为共享,这里在桌面新建一个名为“共享”的文件夹作文测试8、右击->属性->共享9、在其它电脑上访问自己的的文件夹,假设共享文件夹所在主机IP

    2022年5月4日
    339

发表回复

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

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