spark 源碼分析之八--Spark RPC剖析之TransportContext和TransportClientFactory剖析

spark 源碼分析之八--Spark RPC剖析之TransportContext和TransportClientFactory剖析服務器

TransportContext

首先官方文檔對TransportContext的說明以下:網絡

Contains the context to create a TransportServer, TransportClientFactory, and to setup Netty Channel pipelines with a TransportChannelHandler. There are two communication protocols that the TransportClient provides, control-plane RPCs and data-plane "chunk fetching". The handling of the RPCs is performed outside of the scope of the TransportContext (i.e., by a user-provided handler), and it is responsible for setting up streams which can be streamed through the data plane in chunks using zero-copy IO. The TransportServer and TransportClientFactory both create a TransportChannelHandler for each channel. As each TransportChannelHandler contains a TransportClient, this enables server processes to send messages back to the client on an existing channel.dom

首先這個上下文對象是一個建立TransportServer, TransportClientFactory,使用TransportChannelHandler創建netty channel pipeline的上下文,這也是它的三個主要功能。socket

TransportClient 提供了兩種通訊協議:控制層面的RPC以及數據層面的 "chunk抓取"。ide

用戶經過構造方法傳入的 rpcHandler 負責處理RPC 請求。而且 rpcHandler 負責設置流,這些流可使用零拷貝IO以數據塊的形式流式傳輸。oop

TransportServer 和 TransportClientFactory 都爲每個channel建立一個 TransportChannelHandler對象。每個TransportChannelHandler 包含一個 TransportClient,這使服務器進程可以在現有通道上將消息發送回客戶端。源碼分析

成員變量:fetch

1. logger: 負責打印日誌的對象ui

2. conf:TransportConf對象this

3. rpcHandler:RPCHandler的實例

4. closeIdleConnections:空閒時是否關閉鏈接

5. ENCODER: 網絡層數據的加密,MessageEncoder實例

6. DECODER:網絡層數據的解密,MessageDecoder實例

三類方法:

1. 建立TransportClientFactory,兩個方法以下:

 

2. 建立TransportServer,四個方法以下:

 

3. 創建netty channel pipeline,涉及方法以及調用關係以下:

 

注意:TransportClient就是在 創建netty channel pipeline時候被調用的。整個rpc模塊,只有這個方法能夠實例化TransportClient對象。

TransportClientFactory

TransportClientFactory 

使用 TransportClientFactory 的 createClient 方法建立 TransportClient。這個factory維護到其餘主機的鏈接池,並應爲同一遠程主機返回相同的TransportClient。全部TransportClients共享一個工做線程池,TransportClients將盡量重用。

在完成新TransportClient的建立以前,將運行全部給定的TransportClientBootstraps。

其內部維護了一個鏈接池,以下:

TransportClientFactory 類圖以下:

TransportClientFactory成員變量以下:

1. logger 日誌類

2. context 是 TransportContext 實例

3. conf 是 TransportConf 實例

4. clientBootstraps是一個 List<TransportClientBootstrap>實例

5. connectionPool 是一個 ConcurrentHashMap<SocketAddress, ClientPool>實例,維護了 SocketAddress和ClientPool的映射關係,即鏈接到某臺機器某個端口的信息被封裝到

6. rand是一個Random 隨機器,主要用於在ClientPool中選擇TransportClient 實例

7. numConnectionsPerPeer 表示到一個rpcAddress 的鏈接數

8. socketChannelClass 是一個 Channel 的Class 對象

9. workerGroup 是一個EventLoopGroup 主要是爲了註冊channel 對象

10. pooledAllocator是一個 PooledByteBufAllocator 對象,負責分配buffer 的 11.metrics是一個 NettyMemoryMetrics對象,主要負責從 PooledByteBufAllocator 中收集內存使用metric 信息

其成員方法比較簡單,簡言之就是幾個建立TransportClient的幾個方法。

建立受管理的TransportClient,所謂的受管理,其實指的是建立的對象被放入到了connectionPool中:

建立不受管理的TransportClient,新對象建立後不須要放入connectionPool中:

上面的兩個方法都調用了核心方法 createClient 方法,其源碼以下:

 

其中Bootstrap類目的是爲了讓client 更加容易地建立channel。Bootstrap能夠認爲就是builder模式中的builder。

將複雜的channel初始化過程隱藏在Bootstrap類內部。

至於TransportClient是在初始化channel過程當中被初始化的,因爲本篇文章長度限制,咱們下節剖析。

相關文章
相關標籤/搜索