ApplicationMaster是如何启动container并通信

本文解释了ApplicationMaster启动container并与其通信的机制.

hadoop 2.0的yarn 实现了MapReduce计算框架, 其中container进程运行着mapreduce的task进程, 即YarnChild. 相对于以前hadoop版本的Child进程.

 

YarnChild主流程(main方法)

    启动YarnChild的命令行会传递一些参数给YarnChild进程, 包括host, port, TaskAttemptID和jvmIdInt. 这里的host和port为该container需要汇报的AM(ApplicationMaster)地址.

    进程启动后, 首先获取命令行参数, 然后初始化metrics和安全相关的对象(可选).

    创建TaskUmbilicalProtocol对象(使用hadoop-common的RPC), 使用TaskUmbilicalProtocol协议向AM通信,  其实TaskUmbilicalProtocol存在于以前的hadoop版本中, 是task向tasktracker通信的协议. TaskUmbilicalProtocol接口提供的方法如下, 主要包括状态更新, 获取map结束事件等.

                   clip_image002

    然后通过一系列的初始化后运行用户代码, 即map或reduce的run方法. 这和以前版本的代码类似, 不再赘述.

    这里具体介绍一下YarnChild启动的参数host和port的来龙去脉, 是谁生成的, 又是怎么传到container的.

    在MapReduceChildJVM类中, 通过getVMCommand方法创建了container的启动命令, 在这里AM使用把自己的host和port放到了启动命令行的参数中(即container直接向AM汇报), 而这个方法只被TaskAttemptImpl类的createContainerLaunchContext调用, 也就是说, 是在创建ContainerLaunchContext的时候赋值的. 这与官网(http://hadoop.apache.org/docs/r0.23.3/hadoop-yarn/hadoop-yarn-site/WritingYarnApplications.html)中描述的类似:

转载请注明出处(http://www.cnblogs.com/shenh062326/archive/2012/11/24/2785694.html)

After a container has been allocated to the ApplicationMaster, it needs to follow a similar process that the Client followed in setting up the ContainerLaunchContext for the eventual task that is going to be running on the allocated Container. Once the ContainerLaunchContext is defined, the ApplicationMaster can then communicate with the ContainerManager to start its allocated container.

也就是说创建ContainerLaunchContext后交给NodeManager启动相应的container (YarnChild). YarnChild再向AM汇报.

下图展示了三者的关系:

    image

注: ContainerManager协议是ApplicationMaster向NodeManager通信的协议

posted @ 2012-11-24 13:53  南国故人(Wall)  阅读(3278)  评论(0编辑  收藏  举报