`

YARN/MRv2 MRAppMaster深入剖析—ContainerLauncher分析

 
阅读更多

ContainerLauncher负责与NodeManager通信,以启动一个container。在YARN中,运行Task所需的全部信息被封装到Container中,包括所需资源、依赖的外部文件、jar包、运行时环境变量、运行命令等。ContainerLauncher通过ContainerManager协议与NodeManager通信,该协议定义了三个RPC接口,具体如下:

StartContainerResponse startContainer(StartContainerRequest request) throws YarnRemoteException;//启动一个container

StopContainerResponse stopContainer(StopContainerRequest request) throws YarnRemoteException;//停止一个container

GetContainerStatusResponse getContainerStatus(GetContainerStatusRequest request) throws YarnRemoteException;//获取一个container运行情况

ContainerLauncher是一个接口,它定义了2种事件:

(1)CONTAINER_REMOTE_LAUNCH 启动一个container。当ContainerAllocator为某个任务申请到资源后,会将运行该任务相关的所有信息封装到container中,并要求对应的节点启动该container

(2)CONTAINER_REMOTE_CLEANUP 停止/杀死一个container。存在多种可能触发该事件的行为,常见的有,1)推测执行时一个任务运行完成,则需杀死另一个同输入数据的任务 2)用户发送一个杀死任务请求 3)任意一个任务运行结束时,YARN会触发一个杀死任务的命令,以便结束对应进程。尤其需要注意的是第三种情况。

ContainerLauncher接口由ContainerLauncherImpl类实现,它是一个服务,接收和处理来自事件调度器发送过来的CONTAINER_REMOTE_LAUNCH和CONTAINER_REMOTE_CLEANUP两种事件,它采用了队列+进程池的方式异步并行处理这两种事件。

对于CONTAINER_REMOTE_LAUNCH事件,它会调用Container.launch()函数与对应的NodeManager通信,以启动container,代码如下:

proxy = getCMProxy(containerID, containerMgrAddress, containerToken);//构造一个RPC client

ContainerLaunchContext containerLaunchContext = event.getContainer();

StartContainerRequest startRequest = Records.newRecord(StartContainerRequest.class);

startRequest.setContainerLaunchContext(containerLaunchContext);

//调用RPC函数,获取返回值

StartContainerResponse response = proxy.startContainer(startRequest);

对于CONTAINER_REMOTE_CLEANUP事件,它会调用Container. kill()函数与对应的NodeManager通信,以杀死一个container,代码如下:

proxy = getCMProxy(this.containerID, this.containerMgrAddress,this.containerToken);

StopContainerRequest stopRequest = Records.newRecord(StopContainerRequest.class);

stopRequest.setContainerId(this.containerID);
proxy.stopContainer(stopRequest);

总之,ContainerLauncherImpl是一个非常简单的服务,其最核心的代码组织方式是队列+进程池,以处理事件调度器发送过来的CONTAINER_REMOTE_LAUNCH和CONTAINER_REMOTE_CLEANUP两种事件。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics