Hadoop3.2.1 【 HDFS 】源碼分析 : RPC原理 [四] RpcEngine父類

 

1.概述

RpcEngine是ProtobufRpcEngine和WritableRpcEngine[已過時]的父類. 在hadoop的RPC通訊的體系中扮演着很重要的角色.

爲什麼這麼說呢? 因爲正常的RPC請求,分爲三個部分: 通訊協議, Client端 和Server端.   通訊協議是Client和Server端溝通的橋樑.

在Client是通過RpcEngine.getProxy 方法獲取Server的代理對象. 在Server端是通過getServer方法獲取Server端的實例.所以RpcEngine是Hadoop RPC 通訊體系中非常重要的父類定義.

 

getServer : Server端 獲取RPC.Server的實例. 

getProxy :  Client 端  獲取RPC.Server的實例. 

getProtocolMetaInfoProxy : 根據給定的connection id 獲取ProtocolMetaInfoPB 代理對象.

 

2. getServer 解析

RpcEngine 只有一個getServer 方法 獲取RPC.Server的實例. 

  /**
   *
   * 該方法用於產生一個RPC Server對象,服務器會啓動這個Server對象 監聽從客戶端發來的請求。
   * 成功從網絡接收請求數據後,
   * Server對象會調用 Rpclnvoker(在RpcEngine的實現類中定義)對象處理這個請求。
   *
   * Construct a server for a protocol implementation instance.
   * 
   * @param protocol the class of protocol to use
   * @param instance the instance of protocol whose methods will be called
   * @param conf the configuration to use
   * @param bindAddress the address to bind on to listen for connection
   * @param port the port to listen for connections on
   * @param numHandlers the number of method handler threads to run
   * @param numReaders the number of reader threads to run
   * @param queueSizePerHandler the size of the queue per hander thread
   * @param verbose whether each call should be logged
   * @param secretManager The secret manager to use to validate incoming requests.
   * @param portRangeConfig A config parameter that can be used to restrict
   *        the range of ports used when port is 0 (an ephemeral port)
   * @param alignmentContext provides server state info on client responses
   * @return The Server instance
   * @throws IOException on any error
   */
  RPC.Server getServer(Class<?> protocol, Object instance, String bindAddress,
                       int port, int numHandlers, int numReaders,
                       int queueSizePerHandler, boolean verbose,
                       Configuration conf, 
                       SecretManager<? extends TokenIdentifier> secretManager,
                       String portRangeConfig,
                       AlignmentContext alignmentContext) throws IOException;

 


 

 

3. getProxy解析

RpcEngine 有兩個getProxy 方法 獲取ProtocolProxy的實例. 

 /**
   * 客戶端會調用RpcEngine.getProxy()方法獲取一個本地接口的代理對 象,
   * 然後在這個代理對象上調用本地接口的方法。
   *
   * getProxy()方法的實現採用了 Java動態代理機制,
   * 客戶端調用程序在代理對象上的調用會由一個
   * RpcInvocationHandler(java.lang.reflect.InvocationHandler的子類,
   * 在RpcEngine的 實現類中定義)對象處理,
   * 這個RpcInvocationHandler會將請求序列化
   * (使用 RpcEngine實現類定義的序列化方式)
   * 並調用Client.call()方法將請求發送到遠程 服務器。
   * 當遠程服務器發回響應信息後,RpcInvocationHandler會將響應信息反 序列化並返回給調用程序,
   * 這一切通過Java動態代理機制對於調用程序是完全透 明的,就像本地調用一樣。
   *
   * @param protocol
   * @param clientVersion
   * @param addr
   * @param ticket
   * @param conf
   * @param factory
   * @param rpcTimeout
   * @param connectionRetryPolicy
   * @param <T>
   * @return
   * @throws IOException
   */
  <T> ProtocolProxy<T> getProxy(Class<T> protocol,
                  long clientVersion, InetSocketAddress addr,
                  UserGroupInformation ticket, Configuration conf,
                  SocketFactory factory, int rpcTimeout,
                  RetryPolicy connectionRetryPolicy) throws IOException;

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

 

3.  getProtocolMetaInfoProxy

根據給定的connection id 獲取ProtocolMetaInfoPB 代理對象.

/**
   * Returns a proxy for ProtocolMetaInfoPB, which uses the given connection
   * id.
   * @param connId, ConnectionId to be used for the proxy.
   * @param conf, Configuration.
   * @param factory, Socket factory.
   * @return Proxy object.
   * @throws IOException
   */
  ProtocolProxy<ProtocolMetaInfoPB> getProtocolMetaInfoProxy(
      ConnectionId connId, Configuration conf, SocketFactory factory)
      throws IOException;

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章