飞道的博客

Hadoop3.2.1 【 HDFS 】源码分析 : RPC原理 [三] 概述&原理

517人阅读  评论(0)

1.概述

在这里RPC实现其实就是分三部分, 分别是 协议定义&实现 , Server端实现和Client实现. 三个部分. 下面会分别进行讲述

2.协议实现

2.1.定义协议

其实就是根据业务需要定义一个借口协议. 

协议详情可以参看:  Hadoop3.2.1 【 HDFS 】源码分析 : RPC实现 [一] proto接口协议

 

示例如下:


  
  1. /**
  2. * 协议接口
  3. */
  4. public interface ClicentNameNodeProtocol {
  5. //1. 定义协议的ID
  6. public static final long versionID = 1L;
  7. /**
  8. * 拿到元数据方法,协议通信前会访问元数据
  9. */
  10. public String getMetaData(String path);
  11. }

 

2.2.实现协议

实现协议这个也很简单, 就是根据接口创建一个实现类. 用的时候注册到Server服务中即可.

示例如下:


  
  1. /**
  2. * 实现协议结构
  3. */
  4. public class ClicentNameNodeImpl implements ClicentNameNodeProtocol {
  5. public String getMetaData(String path) {
  6. // 数据存放的路径,有多少块,块大小,校验和,存储在哪一台机器上
  7. return path + ":3 - {BLOCK_1,BLOCK_2,BLOCK_3....";
  8. }
  9. }

 

整理的调用逻辑图,参考如下. 后面来具体分析.

 

 

 

3. RPC Server处理流程

 

3.1.Server架构图

 

3.2.Server组件讲解

Listener: 
    Listener对象中存在一个Selector对象acceptSelector, 负责监听来自客户端的Socket连接请求。 当acceptSelector监听到连接请求后, Listener对象会初始化这个连接, 之后采用轮询的方式从readers线程池中选出一个Reader线程处理RPC请求的读取操作。

■ Reader: 
     用于读取RPC请求。 Reader线程类中存在一个Selector对象readSelector, 类似于Reactor模式中的readReactor, 这个对象用于监听网络中是否有可以读取的RPC请求。 当readSelector监听到有可读的RPC请求后, 会唤醒Reader线程读取这个请求, 并将请求封装在一个Call对象中, 然后将这个Call对象放入共享队列CallQueue中。

■ Handler: 
    用于处理RPC请求并发回响应。Handler对象会从CallQueue中不停地取出RPC请求, 然后执行RPC请求对应的本地函数, 最后封装响应并将响应发回客户端。 为了能够并发地处理RPC请求,Server中会存在多个Handler对象。

■ Responder: 
    用于向客户端发送RPC响应,  响应很大或者网络条件不佳等情况下, Handler线程很难将完整的响应发回客户端, 这就会造成Handler线程阻塞, 从而影响RPC请求的处理效率。 所以Handler在没能够将完整的RPC响应发回客户端时, 会在Responder内部的respondSelector上注册一个写响应事件, 这里的respondSelector与Reactor模式的respondSelector概念相同, 当respondSelector监听到网络情况具备写响应的条件时, 会通知Responder将剩余响应发回客户端。

 

 

3.2.Server组件代码讲解

Server服务是用RPC.Builder类中的build()方法进行构建的. 下面是构建Server的模拟代码.

 

构建server的模拟代码:


  
  1. public class Server {
  2. public static void main(String[] args) throws IOException {
  3. //1. 构建RPC框架
  4. RPC.Builder builder = new RPC.Builder( new Configuration());
  5. //2. 绑定地址
  6. builder.setBindAddress( "localhost");
  7. //3. 绑定端口
  8. builder.setPort( 7777);
  9. //4. 绑定协议
  10. builder.setProtocol(ClicentNameNodeProtocol.class);
  11. //5. 调用协议实现类
  12. builder.setInstance( new ClicentNameNodeImpl());
  13. //6. 创建服务
  14. RPC.Server server = builder.build();
  15. //7. 启动服务
  16. server.start();
  17. }
  18. }

其实就是通过RPC.Builder 构建一个Server对象. 

Builder构建对象中包含构建Server的各种属性. 如 IP, 端口, 协议, 实现类等等. 一个builer 只能绑定一个协议和实现类.

当Builder中的各种属性填充完, 满足构建Server的条件之后, 就会构建Server对象. 并且调用Server的start方法,启动Server.


  
  1. public static class Builder {
  2. //设置协议
  3. private Class<?> protocol = null;
  4. //设置协议的实例
  5. private Object instance = null;
  6. //设置绑定地址
  7. private String bindAddress = "0.0.0.0";
  8. //设置端口
  9. private int port = 0;
  10. //这是处理任务的hadnler数量
  11. private int numHandlers = 1;
  12. //设置读取任务的县城数量
  13. private int numReaders = - 1;
  14. private int queueSizePerHandler = - 1;
  15. private boolean verbose = false;
  16. private final Configuration conf;
  17. private SecretManager<? extends TokenIdentifier> secretManager = null;
  18. private String portRangeConfig = null;
  19. private AlignmentContext alignmentContext = null;
  20. public Builder(Configuration conf) {
  21. this.conf = conf;
  22. }
  23. }

根据上面的代码,其实最核心的是创建Server服务

RPC.Server server = builder.build(); 

接下来,我们看看, 上面这行代码,干了什么. 通过怎样的方式构建了一个Server服务.

 


  
  1. /**
  2. * Build the RPC Server.
  3. * @throws IOException on error
  4. * @throws HadoopIllegalArgumentException when mandatory fields are not set
  5. */
  6. public Server build() throws IOException, HadoopIllegalArgumentException {
  7. if ( this.conf == null) {
  8. throw new HadoopIllegalArgumentException( "conf is not set");
  9. }
  10. if ( this.protocol == null) {
  11. throw new HadoopIllegalArgumentException( "protocol is not set");
  12. }
  13. if ( this.instance == null) {
  14. throw new HadoopIllegalArgumentException( "instance is not set");
  15. }
  16. //调用getProtocolEngine()获取当前RPC类配置的RpcEngine对象
  17. //在 NameNodeRpcServer的构造方法中已经将
  18. // 当前RPC类的RpcEngine对象设置为 ProtobufRpcEngine了。
  19. // 获取了ProtobufRpcEngine对象之后,build()方法会在
  20. // ProtobufRpcEngine对象上调用getServer()方法获取一个RPC Server对象的引用。
  21. return getProtocolEngine( this.protocol, this.conf).getServer(
  22. this.protocol, this.instance, this.bindAddress, this.port,
  23. this.numHandlers, this.numReaders, this.queueSizePerHandler,
  24. this.verbose, this.conf, this.secretManager, this.portRangeConfig,
  25. this.alignmentContext);
  26. }

看到这, 其实最主要的是getProtocolEngine方法,获取RpcEngine.  这个方法加了synchronized关键字, 所以是个同步方法.

 


  
  1. // return the RpcEngine configured to handle a protocol
  2. static synchronized RpcEngine getProtocolEngine( Class<?> protocol,
  3. Configuration conf) {
  4. //从缓存中获取RpcEngine
  5. RpcEngine engine = PROTOCOL_ENGINES. get( protocol);
  6. if ( engine == null) {
  7. //获取RpcEngine实现
  8. Class<?> impl = conf.getClass( ENGINE_PROP+ "."+ protocol.getName(),
  9. WritableRpcEngine.class);
  10. engine = (RpcEngine)ReflectionUtils.newInstance(impl, conf);
  11. PROTOCOL_ENGINES.put(protocol, engine);
  12. }
  13. return engine;
  14. }

 

RpcEngine有两种ProtobufRpcEngine和WritableRpcEngine, 默认是WritableRpcEngine(已过时). 本文代码会走默认的WritableRpcEngine.

后面会有单独的文章来解释具体的实现.

 

WritableRpcEngine获取Server.


  
  1. /* Construct a server for a protocol implementation instance listening on a
  2. * port and address. */
  3. @Override
  4. public RPC. Server getServer(Class<?> protocolClass,
  5. Object protocolImpl, String bindAddress, int port,
  6. int numHandlers, int numReaders, int queueSizePerHandler,
  7. boolean verbose, Configuration conf,
  8. SecretManager<? extends TokenIdentifier> secretManager,
  9. String portRangeConfig, AlignmentContext alignmentContext)
  10. throws IOException {
  11. return new Server(protocolClass, protocolImpl, conf, bindAddress, port,
  12. numHandlers, numReaders, queueSizePerHandler, verbose, secretManager,
  13. portRangeConfig, alignmentContext);
  14. }

ProtobufRpcEngine获取Server.


  
  1. @Override
  2. public RPC. Server getServer(Class<?> protocol, Object protocolImpl,
  3. String bindAddress, int port, int numHandlers, int numReaders,
  4. int queueSizePerHandler, boolean verbose, Configuration conf,
  5. SecretManager<? extends TokenIdentifier> secretManager,
  6. String portRangeConfig, AlignmentContext alignmentContext)
  7. throws IOException {
  8. return new Server(protocol, protocolImpl, conf, bindAddress, port,
  9. numHandlers, numReaders, queueSizePerHandler, verbose, secretManager,
  10. portRangeConfig, alignmentContext);
  11. }

 

这两个类型的RpcEngine都会调用父类的初始化. 

比如初始化  listener , handlers, responder,connectionManager 等等..


  
  1. protected Server(String bindAddress, int port,
  2. Class<? extends Writable> rpcRequestClass, int handlerCount,
  3. int numReaders, int queueSizePerHandler, Configuration conf,
  4. String serverName, SecretManager<? extends TokenIdentifier> secretManager,
  5. String portRangeConfig)
  6. throws IOException {
  7. this.bindAddress = bindAddress;
  8. this.conf = conf;
  9. this.portRangeConfig = portRangeConfig;
  10. this.port = port;
  11. this.rpcRequestClass = rpcRequestClass;
  12. this.handlerCount = handlerCount;
  13. this.socketSendBufferSize = 0;
  14. this.serverName = serverName;
  15. this.auxiliaryListenerMap = null;
  16. this.maxDataLength = conf.getInt(CommonConfigurationKeys.IPC_MAXIMUM_DATA_LENGTH,
  17. CommonConfigurationKeys.IPC_MAXIMUM_DATA_LENGTH_DEFAULT);
  18. if (queueSizePerHandler != -1) {
  19. this.maxQueueSize = handlerCount * queueSizePerHandler;
  20. } else {
  21. this.maxQueueSize = handlerCount * conf.getInt(
  22. CommonConfigurationKeys.IPC_SERVER_HANDLER_QUEUE_SIZE_KEY,
  23. CommonConfigurationKeys.IPC_SERVER_HANDLER_QUEUE_SIZE_DEFAULT);
  24. }
  25. this.maxRespSize = conf.getInt(
  26. CommonConfigurationKeys.IPC_SERVER_RPC_MAX_RESPONSE_SIZE_KEY,
  27. CommonConfigurationKeys.IPC_SERVER_RPC_MAX_RESPONSE_SIZE_DEFAULT);
  28. if (numReaders != -1) {
  29. this.readThreads = numReaders;
  30. } else {
  31. this.readThreads = conf.getInt(
  32. CommonConfigurationKeys.IPC_SERVER_RPC_READ_THREADS_KEY,
  33. CommonConfigurationKeys.IPC_SERVER_RPC_READ_THREADS_DEFAULT);
  34. }
  35. this.readerPendingConnectionQueue = conf.getInt(
  36. CommonConfigurationKeys.IPC_SERVER_RPC_READ_CONNECTION_QUEUE_SIZE_KEY,
  37. CommonConfigurationKeys.IPC_SERVER_RPC_READ_CONNECTION_QUEUE_SIZE_DEFAULT);
  38. // Setup appropriate callqueue
  39. final String prefix = getQueueClassPrefix();
  40. this.callQueue = new CallQueueManager<Call>(getQueueClass(prefix, conf),
  41. getSchedulerClass(prefix, conf),
  42. getClientBackoffEnable(prefix, conf), maxQueueSize, prefix, conf);
  43. this.secretManager = (SecretManager<TokenIdentifier>) secretManager;
  44. this.authorize =
  45. conf.getBoolean(CommonConfigurationKeys.HADOOP_SECURITY_AUTHORIZATION,
  46. false);
  47. // configure supported authentications
  48. this.enabledAuthMethods = getAuthMethods(secretManager, conf);
  49. this.negotiateResponse = buildNegotiateResponse(enabledAuthMethods);
  50. // Start the listener here and let it bind to the port
  51. listener = new Listener(port);
  52. // set the server port to the default listener port.
  53. this.port = listener.getAddress().getPort();
  54. connectionManager = new ConnectionManager();
  55. this.rpcMetrics = RpcMetrics.create( this, conf);
  56. this.rpcDetailedMetrics = RpcDetailedMetrics.create( this.port);
  57. this.tcpNoDelay = conf.getBoolean(
  58. CommonConfigurationKeysPublic.IPC_SERVER_TCPNODELAY_KEY,
  59. CommonConfigurationKeysPublic.IPC_SERVER_TCPNODELAY_DEFAULT);
  60. this.setLogSlowRPC(conf.getBoolean(
  61. CommonConfigurationKeysPublic.IPC_SERVER_LOG_SLOW_RPC,
  62. CommonConfigurationKeysPublic.IPC_SERVER_LOG_SLOW_RPC_DEFAULT));
  63. // Create the responder here
  64. responder = new Responder();
  65. if (secretManager != null || UserGroupInformation.isSecurityEnabled()) {
  66. SaslRpcServer.init(conf);
  67. saslPropsResolver = SaslPropertiesResolver.getInstance(conf);
  68. }
  69. this.exceptionsHandler.addTerseLoggingExceptions(StandbyException. class);
  70. }

看到这里就不细说了,我后面会单独拎出章节来写这两个引擎.

 

最后调用start 方法, 开启server服务. 


  
  1. /** Starts the service. Must be called before any calls will be handled. */
  2. public synchronized void start() {
  3. responder.start();
  4. listener.start();
  5. if (auxiliaryListenerMap != null && auxiliaryListenerMap.size() > 0) {
  6. for (Listener newListener : auxiliaryListenerMap.values()) {
  7. newListener.start();
  8. }
  9. }
  10. handlers = new Handler[handlerCount];
  11. for ( int i = 0; i < handlerCount; i++) {
  12. handlers[i] = new Handler(i);
  13. handlers[i].start();
  14. }
  15. }


 

2. Client 实现

 

 

先看一下client调用server端的代码样例. 其实就是通过RPC.getProxy 方法获取server端的代理对象, 然后再通过代理对象调用具体的方法,代理对象根据方法,请求server端, 获取数据.  最终将数据返回给客户端.


  
  1. /**
  2. * 访问RPC服务
  3. */
  4. public class Client {
  5. public static void main(String[] args) throws IOException {
  6. //1. 拿到RPC协议
  7. ClicentNameNodeProtocol proxy = RPC.getProxy(ClicentNameNodeProtocol.class, 1L,
  8. new InetSocketAddress( "localhost", 7777), new Configuration());
  9. //2. 发送请求
  10. String metaData = proxy.getMetaData( "/meta");
  11. //3. 打印元数据
  12. System. out.println(metaData);
  13. }
  14. }

 

先看一下如何获取代理对象.


  
  1. /**
  2. * Get a protocol proxy that contains a proxy connection to a remote server
  3. * and a set of methods that are supported by the server
  4. *
  5. * @param protocol protocol
  6. * @param clientVersion client's version
  7. * @param addr server address
  8. * @param ticket security ticket
  9. * @param conf configuration
  10. * @param factory socket factory
  11. * @param rpcTimeout max time for each rpc; 0 means no timeout
  12. * @param connectionRetryPolicy retry policy
  13. * @param fallbackToSimpleAuth set to true or false during calls to indicate if
  14. * a secure client falls back to simple auth
  15. * @return the proxy
  16. * @throws IOException if any error occurs
  17. */
  18. public static <T> ProtocolProxy<T> getProtocolProxy(Class<T> protocol,
  19. long clientVersion,
  20. InetSocketAddress addr,
  21. UserGroupInformation ticket,
  22. Configuration conf,
  23. SocketFactory factory,
  24. int rpcTimeout,
  25. RetryPolicy connectionRetryPolicy,
  26. AtomicBoolean fallbackToSimpleAuth)
  27. throws IOException {
  28. if (UserGroupInformation.isSecurityEnabled()) {
  29. SaslRpcServer.init(conf);
  30. }
  31. return getProtocolEngine(protocol, conf).getProxy(protocol, clientVersion,
  32. addr, ticket, conf, factory, rpcTimeout, connectionRetryPolicy,
  33. fallbackToSimpleAuth, null);
  34. }

 

根据上面的方法, 先通过getProtocolEngine(protocol, conf) 这个方法, 获取到RpcEngine, 然后调用getProxy 获取对应的对象.

这个是getProxy 的接口定义, 根据RpcEngine 的不同, 实现方式也不同. 后面会有文章做讲述.


  
  1. /** Construct a client-side proxy object. */
  2. <T> ProtocolProxy<T> getProxy(Class<T> protocol,
  3. long clientVersion, InetSocketAddress addr,
  4. UserGroupInformation ticket, Configuration conf,
  5. SocketFactory factory, int rpcTimeout,
  6. RetryPolicy connectionRetryPolicy,
  7. AtomicBoolean fallbackToSimpleAuth,
  8. AlignmentContext alignmentContext) throws IOException;

 

拿到代理对象之后, 就可以像本地一样调用里面的方法了.
 

 

 

 

 

 

 

 

 

参考:

Hadoop 2.X HDFS源码剖析 -- 徐鹏
 

 

 

 

 

 

 

 


转载:https://blog.csdn.net/zhanglong_4444/article/details/105607870
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场