sqtsqt 发表于 2016-12-7 06:00:11

hadoop中RPC小析

1.什么是RPC

   RPC(Remote Procedure Call Protocol)——远程过程调用协议,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议。这句话概括的,虽然正确,不过却略显得冗余,如果要用一句话来概括RPC是什么的话,那RPC就是在一台机子上调用另外一台机子上的方法,得到想要的结果的过程。

2.RPC的作用

   在hadoop中,消息的传递分为两类:控制消息和数据消息。前者包括DataNode定期向NameNode发送心跳,得到要执行的命令。这个过程就是用RPC实现的,即,在DataNode上调用NameNode上的方法,得到想要的结果。

3.RPC的数据
   那么这么看来,RPC也不过就是两台电脑间的信息通信。如果要调用另一台电脑上的方法,同时要能得到记过的话,那么至少必须具备以下条件:1.通信,2.需要调用的方法信息(包括类名,方法名,方法参数),3.调用的结果。

   那么,这么看来,首先要实现的,就是能够将传入信息(比如方法参数类)、传出信息(结果参数)能够实现在网络上的传输。hadoop中对此定义为:基本数据类型和String能直接传输,而类的话,就要实现Wirtable接口才行。

public class MyWritable implements Writable {      
// Some data         
private int counter;      
private long timestamp;      

public void write(DataOutput out) throws IOException {      
out.writeInt(counter);      
out.writeLong(timestamp);      
}      

public void readFields(DataInput in) throws IOException {      
counter = in.readInt();      
timestamp = in.readLong();      
}      

public static MyWritable read(DataInput in) throws IOException {      
MyWritable w = new MyWritable();      
w.readFields(in);      
return w;      
}      
}

(代码来自《hadoop源码分析》)
4.RPC的过程
   那么,传输数据的过程呢?RPC是通信,而通讯的模式就是服务器/客户机,在hadoop中,NameNode充当服务器,而DataNode这充当客户机。在通讯时,首先要实现通讯协议的制定,hadoop中是使用的org.apache.hadoop.ipc.VersionedProtocol这个接口。所有要在服务器上实现的协议都要继承这个接口(RPC中的协议是以方法为基础的,可以看做是传输的是方法信息)。而在服务器上需要实现这个接口,而客户机只要保留对应接口信息就可以了。
接口定义如下:其中,getString即为要在服务器上调用的方法

public interface Datanode2NamenodeProtocil extends VersionedProtocol{
public static final long versionID = 19L;
public String getString();
}


然后,服务器端要继承并实现这个接口,并且创建一个server类,(hadoop中为org.apache.hadoop.ipc.Server),等待并接受要处理信息。
而客户机端就简单了,只要具有协议接口(如:Datanode2NamenodeProtocil ),而不用指定其实现,在hadoop中是使用org.apache.hadoop.ipc.RPC类的waitForProxy方法,得到对应的代理类,而调用时,就是能够通过得打的代理类,将信息发送的服务器上,然后将接受到的结果在进行序列化,就Over了。
5.RPC的底层
   上一部分提到了,RPC客户端是实现的动态代理,那么底层呢?先看源码。
waitForProxy方法核心代码如下

while (true) {
try {
return getProxy(protocol, clientVersion, addr, conf);   //这个
} catch(ConnectException se) {// namenode has not been started
LOG.info("Server at " + addr + " not available yet, Zzzzz...");
ioe = se;
} catch(SocketTimeoutException te) {// namenode is busy
LOG.info("Problem connecting to server: " + addr);
ioe = te;
}


继续F3,得到的getProxy核心代码如下

VersionedProtocol proxy =
(VersionedProtocol) Proxy.newProxyInstance(
protocol.getClassLoader(), new Class[] { protocol },
new Invoker(protocol, addr, ticket, conf, factory));

可见,此处使用的是动态代理,而代理处理类为Invoker,继续F3,查看Invoker。
Invoker中的Invoke方法如下:

public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
final boolean logDebug = LOG.isDebugEnabled();
long startTime = 0;
if (logDebug) {
startTime = System.currentTimeMillis();
}
ObjectWritable value = (ObjectWritable)
client.call(new Invocation(method, args), remoteId);
if (logDebug) {
long callTime = System.currentTimeMillis() - startTime;
LOG.debug("Call: " + method.getName() + " " + callTime);
}
return value.get();
}

其核心也就是,ObjectWritable value = (ObjectWritable) client.call(new Invocation(method, args), remoteId);这一句,就是得到一个实现Writeable接口的、通过client(负责通讯、连接服务器)得到的类,然后,将这个结果返回。
继续看,Invoke类中的部分源码如下:

public Invocation(Method method, Object[] parameters) {
this.methodName = method.getName();
this.parameterClasses = method.getParameterTypes();
this.parameters = parameters;
}

可见,其中包括方法变量,参数变量。
而它的写入/写出方法:

public void readFields(DataInput in) throws IOException {
methodName = UTF8.readString(in);
parameters = new Object;
parameterClasses = new Class;
ObjectWritable objectWritable = new ObjectWritable();
for (int i = 0; i < parameters.length; i++) {
parameters = ObjectWritable.readObject(in, objectWritable, this.conf);
parameterClasses = objectWritable.getDeclaredClass();
}
}
public void write(DataOutput out) throws IOException {
UTF8.writeString(out, methodName);
out.writeInt(parameterClasses.length);
for (int i = 0; i < parameterClasses.length; i++) {
ObjectWritable.writeObject(out, parameters, parameterClasses,
conf);
}
}

写入/写出的信息都是关于对应方法信息、参数信息。由此可见,RPC底层通讯时,传递的信息如上所述:为方法/参数信息。
页: [1]
查看完整版本: hadoop中RPC小析