1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 package org.apache.hadoop.yarn.server.api.impl.pb.client;
20 
21 import java.io.Closeable;
22 import java.io.IOException;
23 import java.net.InetSocketAddress;
24 
25 import org.apache.hadoop.conf.Configuration;
26 import org.apache.hadoop.ipc.ProtobufRpcEngine;
27 import org.apache.hadoop.ipc.RPC;
28 import org.apache.hadoop.yarn.exceptions.YarnException;
29 import org.apache.hadoop.yarn.ipc.RPCUtil;
30 import org.apache.hadoop.yarn.proto.YarnServerCommonServiceProtos.NodeHeartbeatRequestProto;
31 import org.apache.hadoop.yarn.proto.YarnServerCommonServiceProtos.RegisterNodeManagerRequestProto;
32 import org.apache.hadoop.yarn.server.api.ResourceTracker;
33 import org.apache.hadoop.yarn.server.api.ResourceTrackerPB;
34 import org.apache.hadoop.yarn.server.api.protocolrecords.NodeHeartbeatRequest;
35 import org.apache.hadoop.yarn.server.api.protocolrecords.NodeHeartbeatResponse;
36 import org.apache.hadoop.yarn.server.api.protocolrecords.RegisterNodeManagerRequest;
37 import org.apache.hadoop.yarn.server.api.protocolrecords.RegisterNodeManagerResponse;
38 import org.apache.hadoop.yarn.server.api.protocolrecords.impl.pb.NodeHeartbeatRequestPBImpl;
39 import org.apache.hadoop.yarn.server.api.protocolrecords.impl.pb.NodeHeartbeatResponsePBImpl;
40 import org.apache.hadoop.yarn.server.api.protocolrecords.impl.pb.RegisterNodeManagerRequestPBImpl;
41 import org.apache.hadoop.yarn.server.api.protocolrecords.impl.pb.RegisterNodeManagerResponsePBImpl;
42 
43 import com.google.protobuf.ServiceException;
44 
45 public class ResourceTrackerPBClientImpl implements ResourceTracker, Closeable {
46 
47 private ResourceTrackerPB proxy;
48 
ResourceTrackerPBClientImpl(long clientVersion, InetSocketAddress addr, Configuration conf)49   public ResourceTrackerPBClientImpl(long clientVersion, InetSocketAddress addr, Configuration conf) throws IOException {
50     RPC.setProtocolEngine(conf, ResourceTrackerPB.class, ProtobufRpcEngine.class);
51     proxy = (ResourceTrackerPB)RPC.getProxy(
52         ResourceTrackerPB.class, clientVersion, addr, conf);
53   }
54 
55   @Override
close()56   public void close() {
57     if(this.proxy != null) {
58       RPC.stopProxy(this.proxy);
59     }
60   }
61 
62   @Override
registerNodeManager( RegisterNodeManagerRequest request)63   public RegisterNodeManagerResponse registerNodeManager(
64       RegisterNodeManagerRequest request) throws YarnException,
65       IOException {
66     RegisterNodeManagerRequestProto requestProto = ((RegisterNodeManagerRequestPBImpl)request).getProto();
67     try {
68       return new RegisterNodeManagerResponsePBImpl(proxy.registerNodeManager(null, requestProto));
69     } catch (ServiceException e) {
70       RPCUtil.unwrapAndThrowException(e);
71       return null;
72     }
73   }
74 
75   @Override
nodeHeartbeat(NodeHeartbeatRequest request)76   public NodeHeartbeatResponse nodeHeartbeat(NodeHeartbeatRequest request)
77       throws YarnException, IOException {
78     NodeHeartbeatRequestProto requestProto = ((NodeHeartbeatRequestPBImpl)request).getProto();
79     try {
80       return new NodeHeartbeatResponsePBImpl(proxy.nodeHeartbeat(null, requestProto));
81     } catch (ServiceException e) {
82       RPCUtil.unwrapAndThrowException(e);
83       return null;
84     }
85   }
86 
87 }
88