Hadoop源码解析之修改distributedshell使每个Container运行在不同节点上

25 篇文章 0 订阅
14 篇文章 1 订阅


1.提出问题

修改DistributedShell程序,使得每个Container运行在不同节点上(目前是随机的,可能运行在任意节点上)。

2.分析过程

         在YARN的处理流程中:

1.  AM通过RPC协议ApplicationMasterProtocol向RM申请Container。

2.  AM通过RPC协议ContainerManagementProtocol要求NM启动或者停止Container。

要想使每个Container运行在不同节点上,只需AM向每个节点都申请一个Container。

3.代码改动

通过第二步的分析得知,只需修改AM端,向每个节点都申请一个Container即可。

3.1 获取计算节点列表

定义nodeList用于保存计算节点列表,在ApplicationMaster的init()函数中添加初始化nodeList的代码。初始化完成后,nodeList中保存有计算节点的列表(不包括RM 节点)。

public  class  ApplicationMaster {

  //所有计算节点

  private  static List  nodeList =new  ArrayList();

 

  public  boolean init(String[] args)throws  ParseException, IOException {

//该函数的末尾添加如下代码,用于获取计算节点列表

…….

    try{

      YarnClient  yarnClient =YarnClient.createYarnClient();

      yarnClient.init(conf);

      yarnClient.start();

      List<NodeReport>clusterNodeReports;

      clusterNodeReports =yarnClient.getNodeReports(NodeState.RUNNING);

      for (NodeReport node :clusterNodeReports) {

        this.nodeList.add(node.getNodeId().getHost());

      }

    } catch (YarnException e) {

      e.printStackTrace();

    }

    return  true;

  }

}

 

3.2 向RM申请资源

申请资源的时候,会调用函数setupContainerAskForRM。

private ContainerRequest  setupContainerAskForRM() {

    // setup requirements for hosts

    // using * as any host will do for the distributed shellapp

    // set the priority for the request

    Priority pri= Records.newRecord(Priority.class);

    // TODO - what is the range for priority? how to decide?

    pri.setPriority(requestPriority);

 

    // Set up resource type requirements

    // For now, memory and CPU are supported so we set memoryandcpu requirements

    Resource capability= Records.newRecord(Resource.class);

    capability.setMemory(containerMemory);

    capability.setVirtualCores(containerVirtualCores);

 

    String[] nodes = null;

    if (!NodeList.isEmpty()) {

                   nodes = new String[1];

                   nodes[0] = (String)NodeList.get(0);

                   NodeList.remove(0);

         }

//默认的nodesnull

//考虑到本地性松弛,有可能节点1没有满足条件的Container可以分配,为了不让此Container分配到其余节点上,

//需要将本地性松弛参数关闭,即参数传入false

    ContainerRequest request=new ContainerRequest(capability,nodes, nullpri,false);

    LOG.info("Requestedcontainer ask: " +request.toString());

    return request;

  }

 

4.总结

解决该问题的关键点有以下几个地方:

1.      ApplicationMaster可以通过yarnClient从RM中获取计算节点列表。

2.      申请资源的时候,会调用函数setupContainerAskForRM。

3.      本地性松弛参数关闭。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值