Hadoop IPC分析 (基于v0.1.0)
IPC - Inter-Process Communication
定义了Invocation类,该类封装了调用的方法和参数信息。
其余代码整体上主要分两部分,一部分为client call server,一部分为server启动监听服务。后面按实际用法进行分析。
Usage 1: 调用远程服务
XXXProtocol xxx = (XXXProtocol) RPC.getProxy(XXXProtocol.class,
new InetSocketAddress(port), conf);
public static Object getProxy(Class protocol, InetSocketAddress addr,
Configuration conf) {
return Proxy.newProxyInstance(protocol.getClassLoader(),
new Class[] { protocol },
new Invoker(addr, conf));
}
定义了Invoker类,该类实现了InvocationHandler接口,为具体调用类。
封装了地址信息和客户端信息,有configuration的get/set保证Client的单例
InetSocketAddress address; // IP|hostname + port
Client CLIENT; // new Client(ObjectWritable.class, conf); 并set回conf保证唯一的CLIENT实例
以下是InvocationHandler的具体实现方法
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// 此处调用Client的call方法,其会建立远程connection,发起call,不断监听以获取返回值
ObjectWritable value = (ObjectWritable) CLIENT.call(
new Invocation(method, args), address);
return value.get();
}
Usage 2: 启动服务,监听端口
Server server = RPC.getServer(...);
server.start();
public static Server getServer(final Object instance, final int port,
Configuration conf);
public static Server getServer(final Object instance, final int port,
final int numHandlers, final boolean verbose, Configuration conf);
以上两个方法返回Server的实例,new Server(...)
Server类继承自org.apache.hadoop.ipc.Server,实现了Writable call(Writable param)方法。该方法反射调用了Server的具体方法
Method method = implementation.getMethod(call.getMethodName(),
call.getParameterClasses());
Object value = method.invoke(instance, call.getParameters());
return new ObjectWritable(method.getReturnType(), value);
/** Expert: Make multiple, parallel calls to a set of servers. */
public static Object[] call(Method method, Object[][] params,
InetSocketAddress[] addrs, Configuration conf) {
......
}