YARN RPC应用实例(2)

步骤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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值