YARN RPC应用实例(2)

来源:互联网 发布:安吉丽娜朱莉离婚 知乎 编辑:程序博客网 时间:2024/06/11 12:23

步骤3 为RPC函数的参数和返回值提供Protocol Buffers定义。YARN需要保证每个RPC函数的参数和返回值是采用Protocol Buffers定义的,因此ResourceTracker协议中RegisterNodeManagerRequest、RegisterNodeManagerResponse、NodeHeartbeatRequest和NodeHeartbeatResponse四个参数或者返回值需要使用Protocol Buffers定义,具体如下(见yarn_server_common_service_protos.proto文件):

 

  1. import "yarn_protos.proto";  
  2. import "yarn_server_common_protos.proto";  
  3.  
  4. message RegisterNodeManagerRequestProto {  
  5.   optional NodeIdProto node_id = 1;  
  6.   optional int32 http_port = 3;  
  7.   optional ResourceProto resource = 4;  
  8. }  
  9.  
  10. message RegisterNodeManagerResponseProto {  
  11.   optional MasterKeyProto container_token_master_key = 1;  
  12.   optional MasterKeyProto nm_token_master_key = 2;  
  13.   optional NodeActionProto nodeAction = 3;  
  14.   optional int64 rm_identifier = 4;  
  15.   optional string diagnostics_message = 5;  
  16. }  
  17. ... //其他几个参数和返回值的定义 

步骤4 为RPC函数的参数和返回值提供Java定义和封装。YARN采用了Protocol Buffers作为参数和返回值的序列化框架,且以原生态.proto文件的方式给出了定义,而具体的Java代码生成需在代码编写之后完成。基于以上考虑,为了更容易使用Protocol Buffers生成的(Java语言)参数和返回值定义,YARN RPC为每个RPC函数的参数和返回值提供Java定义和封装,以参数RegisterNodeManagerRequest为例进行说明。

Java接口定义如下(见Java包org.apache.hadoop.yarn.server.api.protocolrecords):
 

  1. public interface RegisterNodeManagerRequest {  
  2.   NodeId getNodeId();  
  3.   int getHttpPort();  
  4.   Resource getResource();  
  5.  
  6.   void setNodeId(NodeId nodeId);  
  7.   void setHttpPort(int port);  
  8.   void setResource(Resource resource);  

Java封装如下(见Java包org.apache.hadoop.yarn.server.api.protocolrecords.impl.pb):
 

  1. public class RegisterNodeManagerRequestPBImpl extends  
  2.   ProtoBase<RegisterNodeManagerRequestProto> implements RegisterNodeManagerRequest {  
  3.   RegisterNodeManagerRequestProto proto = RegisterNodeManagerRequestProto.getDefaultInstance();  
  4.   RegisterNodeManagerRequestProto.Builder builder = null;  
  5.   private NodeId nodeId = null;  
  6.   ...  
  7.   @Override  
  8.   public NodeId getNodeId() {  
  9.     RegisterNodeManagerRequestProtoOrBuilder p = viaProto ? proto : builder;  
  10.     if (this.nodeId != null) {  
  11.       return this.nodeId;  
  12.     }  
  13.     if (!p.hasNodeId()) {  
  14.       return null;  
  15.     }  
  16.     this.nodeId = convertFromProtoFormat(p.getNodeId());  
  17.     return this.nodeId;  
  18.   }  
  19.   @Override  
  20.   public void setNodeId(NodeId nodeId) {  
  21.     maybeInitBuilder();  
  22.     if (nodeId == null)  
  23.       builder.clearNodeId();  
  24.     this.nodeId = nodeId;  
  25.   }  
  26.   ...  

步骤5 为通信协议提供客户端和服务器端实现。客户端代码放在org.apache.hadoop.yarn.server.api.impl.pb.client包中,且类名为ResourceTrackerPBClientImpl,实现如下:
 

  1. public class ResourceTrackerPBClientImpl implements ResourceTracker, Closeable {  
  2.   private ResourceTrackerPB proxy;  
  3.   public ResourceTrackerPBClientImpl(long clientVersion, InetSocketAddress addr, Configuration conf) throws IOException {  
  4.     RPC.setProtocolEngine(conf, ResourceTrackerPB.class, ProtobufRpcEngine.class);  
  5.     proxy = (ResourceTrackerPB)RPC.getProxy(  
  6.         ResourceTrackerPB.class, clientVersion, addr, conf);  
  7.   }  
  8.   @Override  
  9.   public RegisterNodeManagerResponse registerNodeManager(  
  10.       RegisterNodeManagerRequest request) throws YarnException,  
  11.       IOException {  
  12.     RegisterNodeManagerRequestProto requestProto = ((RegisterNodeManagerRequestPBImpl)request).getProto();  
  13.     try {  
  14.       return new RegisterNodeManagerResponsePBImpl(proxy.registerNodeManager (null, requestProto));  
  15.     } catch (ServiceException e) {  
  16.       RPCUtil.unwrapAndThrowException(e);  
  17.       return null;  
  18.     }  
  19.   }  
  20.   ...  
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 三星0n7充电慢怎么办 在国外接受不到验证码怎么办 ip地址错误网络无法连通怎么办 注册微信收不到验证码怎么办 安卓手机收不到短信怎么办 手机被短信验证码轰炸怎么办 美国访学一年手机卡怎么办方便 小狗吃了葡萄皮怎么办 ios迅雷下载不了的资源怎么办 ipad软件商店内容少怎么办? 脸摔伤后留下黑印怎么办 不确定孩子是不是老公的怎么办 孩子接种证丢了怎么办 孩子的出生证丢了怎么办 私秘边上肿了怎么办 书法作品少写一个字怎么办 炉石传说ios闪退怎么办 ck手表表链大了怎么办 天梭手表卡扣坏了怎么办 机械表平时不戴怎么办 天梭机械表慢了怎么办 手表每天慢10秒怎么办 浪琴机械表不走了怎么办 大提单号被修改了怎么办 入户中山没有三年居住证明怎么办 加拼关单号舱单信息没有怎么办 外贸中交货期晚了怎么办 履约保函到期了怎么办 续贷高校未通过怎么办 安卓手机网速慢怎么办 探探性别错了怎么办 尿酸高导致脚肿怎么办 联通光猫复位后怎么办 头部和脸部出油怎么办 vcd解码板坏了怎么办 脚崴了里面筋疼怎么办 长寿龟身上烂了怎么办 纱裙没有腰身显胖怎么办 橘子平台下载速度慢怎么办 换手机号了支付宝账号怎么办 怀孕期间脚扭了怎么办