与IPC相关的代码在org.apache.hadoop.ipc包下。共七个文件,其中4个辅助类:

RemoteException

Status

VersionedProtocol

ConnectionHeader

主要实现类3个:

Client

Server

RPC

客户端Client:

如上图:

与IPC连接相关的

  • Client.Connection
  • Client.ConnectionId
  • ConnectionHeader

与远程调用Call相关的

  • Client.Call
  • Client.ParallelCall
  • Client.ParallelResults

服务器端Server:

与IPC连接相关的

  • Server.Connection
  • ConnectionHeader

与远程调用Call相关的

  • Server.Call
  • Server.Responder
  • Server.Listener
  • Server.Handler

 

RPC

RPC是在Server及Client的基础上实现了Hadoop IPC。

与客户端相关的功能:

  • RPC.ClientCache
  • RPC.Invoker(继承java.lang.reflect.InvocationHandler)
  • RPC.Invocation

与服务端相关的功能:

  • RPC.Server

 

Connection

客户端与服务器端对连接的抽象不一样,所以有Server.Connection和Client.Connection。Hadoop远程调用采用TCP协议通信。

1)客户端Client.ConnectionId

连接复用:当多个IPC客户端的ConnectionId相同时,他们共享一个IPC连接。连接复用可以减少Hadoop Server、Client的资源占用,同时节省IPC连接时间。

2)ConnectionHeader

Server与Client间TCP连接建立后交换的第一条信息,包含ConnectionId.ticket(UserGroupInformation)用户信息和IPC接口信息,检验是否实现了IPC接口,以及该用户是否有权使用接口。

Call

建立连接后,即可以进行远程过程调用服务,即对IPC接口方法的调用,源码抽象为Call。

远程调用Client.Call对象和Server.Call对象,是一个IPC调用产生的,存在于IPC客户端(存根)和IPC服务端(骨架)中的实体。

Client.Call对象通过IPC连接到服务器后,自然会构成相应的Server.Call对象。

 

Client.Call何时产生以及如何产生?

如上图所示流程:

  1. 用户发起远程接口调用
  2. 动态代理,RPC.Invoker调用句柄捕获远程调用
  3. 根据invoke的输入参数method、args生成RPC.Invocation对象
  4. 并调用Client.call,call会将上一步的Invocation对象序列化并通过IPC连接发送到服务器。Client.call会等待服务端返回的结果。
  5. 服务器端Listener监听Client发来的连接请求和数据请求,并调用Server端的连接对象。
  6. 连接对象接收远程调用请求帧,反序列化,并将请求放于阻塞队列中,由Handler处理。
  7. Handler调用对应的IPC接口实现类,完成过程调用,将结果序列化。
  8. 如果此时连接的应答队列为空,返回给客户端。
  9. 否则,客户端比较忙,应答队列不为空,Handler将结果放入响应队列,由Responser通过IPC发送给客户端。

Client.Connection对象,需要通过setupIOstreams方法和服务器建立连接,该方法首先通过Java套接字与server建立Socket连接,如果失败,则进行一定次数的重试,如下代码,是setupIOstreams调用的setupConnection:

private synchronized void setupConnection() throws IOException {
short ioFailures = 0;
short timeoutFailures = 0;
while (true) {
try {
this.socket = socketFactory.createSocket();
this.socket.setTcpNoDelay(tcpNoDelay);
//禁用tcp的Nagle算法,关闭socket底层缓冲
// 配置项 ${ipc.client.tcpnodelay} /*
* Bind the socket to the host specified in the principal name of the
* client, to ensure Server matching address of the client connection
* to host name in principal passed.
*/
if (UserGroupInformation.isSecurityEnabled()) {
KerberosInfo krbInfo =
remoteId.getProtocol().getAnnotation(KerberosInfo.class);
if (krbInfo != null && krbInfo.clientPrincipal() != null) {
String host =
SecurityUtil.getHostFromPrincipal(remoteId.getTicket().getUserName()); // If host name is a valid local address then bind socket to it
InetAddress localAddr = NetUtils.getLocalInetAddress(host);
if (localAddr != null) {
this.socket.bind(new InetSocketAddress(localAddr, 0));
}
}
} // connection time out is 20s
NetUtils.connect(this.socket, server, 20000);
if (rpcTimeout > 0) {
pingInterval = rpcTimeout; // rpcTimeout overwrites pingInterval
} this.socket.setSoTimeout(pingInterval);
return;
} catch (SocketTimeoutException toe) {
/* Check for an address change and update the local reference.
* Reset the failure counter if the address was changed
*/
if (updateAddress()) {
timeoutFailures = ioFailures = 0;
}
/* The max number of retries is 45,
* which amounts to 20s*45 = 15 minutes retries.
*/
handleConnectionFailure(timeoutFailures++, 45, toe);
} catch (IOException ie) {
if (updateAddress()) {
timeoutFailures = ioFailures = 0;
}
handleConnectionFailure(ioFailures++, maxRetries, ie);
}
}
}

 

IPC连接

  • 连接建立
  • 连接上的数据读写
  • 连接维护
  • 连接关闭

在接下来的几篇内分别介绍以上内容。

Hadoop IPC的代码结构分析的更多相关文章

  1. org.apache.hadoop.ipc.RemoteException(java.io.IOException)

    昨晚突然之间mr跑步起来了 jps查看 进程都在的,但是在reduce任务跑了85%的时候会抛异常 异常情况如下: 2016-09-21 21:32:28,538 INFO [org.apache.h ...

  2. 一张图解释Hadoop IPC

    基于hadoop2.6.2.... 一张图Server启动,Client访问..... RPC是IPC的一种,IPC还有另外一种LPC,相关请看参考中的3 使用hadoop ipc步骤: 1.定义RP ...

  3. 运行基准测试hadoop集群中的问题:org.apache.hadoop.ipc.RemoteException: java.io.IOException: File /benchmarks/TestDFSIO/io_data/test_

    在master(即:host2)中执行 hadoop jar hadoop-test-1.1.2.jar DFSCIOTest -write -nrFiles 12 -fileSize 10240 - ...

  4. hive运行query语句时提示错误:org.apache.hadoop.ipc.RemoteException: java.io.IOException: java.io.IOException:

    hive> select product_id, track_time from trackinfo limit 5; Total MapReduce jobs = 1 Launching Jo ...

  5. Caused by: org.apache.hadoop.ipc.RemoteException(org.apache.hadoop.security.AccessControlException):

    用windows连接hadoop集群执行mapreduce任务的时候出现以下错误: org.apache.hadoop.security.AccessControlException:Permissi ...

  6. Hive JDBC:java.lang.RuntimeException: org.apache.hadoop.ipc.RemoteException(org.apache.hadoop.security.authorize.AuthorizationException): User: root is not allowed to impersonate anonymous

    今天使用JDBC来操作Hive时,首先启动了hive远程服务模式:hiveserver2 &(表示后台运行),然后到eclipse中运行程序时出现错误: java.sql.SQLExcepti ...

  7. 一脸懵逼加从入门到绝望学习hadoop之 org.apache.hadoop.ipc.RemoteException(org.apache.hadoop.security.AccessControlException): Permission denied: user=Administrator, access=WRITE, inode="/":root:supergroup:drwxr-xr报错

    1:初学hadoop遇到各种错误,这里贴一下,方便以后脑补吧,报错如下: 主要是在window环境下面搞hadoop,而hadoop部署在linux操作系统上面:出现这个错误是权限的问题,操作hado ...

  8. HBase中此类异常解决记录org.apache.hadoop.ipc.RemoteException(java.io.IOException):

    ERROR: Can't get master address from ZooKeeper; znode data == null   一定注意这只是问题的第一层表象,真的问题是: File /hb ...

  9. Hadoop学习笔记之一:Hadoop IPC

    因为某些原因需要把前一段时间对Hadoop(版本基于0.20.2)的学习积累搬到这里,成为一个系列.写得会很简单,只为必要时给自己提醒. IPC框架 所有Hadoop协议接口的实现都依赖Hadoop ...

随机推荐

  1. NYOJ题目436sum of all integer numbers

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAr0AAAHKCAIAAACBiWRrAAAgAElEQVR4nO3dP1LjSts34G8T5CyEFB

  2. Spring学习笔记—最小化Spring XML配置

    自动装配(autowiring)有助于减少甚至消除配置<property>元素和<constructor-arg>元素,让Spring自动识别如何装配Bean的依赖关系. 自动 ...

  3. DedeCMS Error: (PHP 5.3 and above) Please set request_order

    部分使用PHP 5.3的主机可能会有下面的提示: (PHP 5.3 and above) Please set 'request_order' ini value to include C,G and ...

  4. 使用Autofac在ASP.NET Web API上实现依赖注入

    在ASP.NET Web API里使用Autofac 1.通过NuGet安装Autofac.WebApi(当时安装的是Autofac 3.1.0) PM > Install-Package Au ...

  5. HTML5学习之跨文档传输消息(七)

    新标准中提供了文档之间直接的消息传输API.而且不限制跨域消息传递! 发送消息使用的是Window对象的postMessage(data,targetURL)方法就可以了,但给哪个window对象发送 ...

  6. sqlplus 中spool命令的简单用法

    spool基本格式: spool 路径+文件名 select col1||','||col2||','||col3||','||col4||'..' from tablename; spool off ...

  7. Oracle性能优化之SQL语句

    1.SQL语句执行过程 1.1 SQL语句的执行步骤 1)语法分析,分析语句的语法是否符合规范,衡量语句中各表达式的意义. 2)语义分析,检查语句中涉及的所有数据库对象是否存在,且用户有相应的权限. ...

  8. MySQL5.7更改密码时出现ERROR 1054 (42S22): Unknown column 'password' in 'field list'

    转自:http://blog.csdn.net/u010603691/article/details/50379282 新安装的MySQL5.7,登录时提示密码错误,安装的时候并没有更改密码,后来通过 ...

  9. hpunix下11gRac的安装

    一.检查环境 1.操作系统版本# uname -a 2.补丁包三大补丁包#swlist -l bundle|grep QPKAPPS#swlist -l bundle|grep QPKBASE#swl ...

  10. Educational Codeforces Round 3 E. Minimum spanning tree for each edge LCA/(树链剖分+数据结构) + MST

    E. Minimum spanning tree for each edge   Connected undirected weighted graph without self-loops and ...