Grpc 線程架構
Execute_thrd: 公有線程,啟動時負責啟動監(jiān)聽,IO,其他任務;
Timer_thrd:負責執(zhí)行指定的定時任務(非周期性任務);
Consumer_thrd:從Queue中取Event消費。
網(wǎng)絡模型
Thread: 用戶Consumer線程;
Completion_queue: Grpc Queue,用來實現(xiàn)異步的事件隊列;
Pollset: 每個queue有個對應的pollset,用來從底層收集IO事件;
Polling island : 是pollset的一個邏輯單位,可以包含多個pollset;
Epoll set: 底層os對應的epool set,OS內核負責收活動FD,底層epool的封裝(windows giops封裝)
Transport Explainer
在Grpc上的一個batch,由幾個基本的stream ops組成。Batch是調度的基本單位。
Stream ops如下:
send_initial_metadata
Client: initate an RPC
Server: supply response headers
recv_initial_metadata
Client: get response headers
Server: accept an RPC
send_message (zero or more) : send a data buffer
recv_message (zero or more) : receive a data buffer
send_trailing_metadata
Client: half-close indicating that no more messages will be coming
Server: full-close providing final status for the RPC
recv_trailing_metadata: get final status for the RPC
Server extra: This op shouldn't actually be considered complete until the server has also sent trailing metadata to provide the other side with final status
cancel_stream: Attempt to cancel an RPC
collect_stats: Get stats
The fundamental responsibility of the transport is to transform between this internal format and an actual wire format, so the processing of these operations is largely transport-specific.
Transport負責內部格式和網(wǎng)絡格式之間轉換。
One or more of these ops are grouped into a batch. Applications can start all of a call's ops in a single batch, or they can split them up into multiple batches. Results of each batch are returned asynchronously via a completion queue.
每個call的ops可以放到一個batch中,如果一部實現(xiàn),會放到多個batch中,每一個batch會返回completion queue一個Event。
Internally, we use callbacks to indicate completion. The surface layer creates a callback when starting a new batch and sends it down the filter stack along with the batch. The transport must invoke this callback when the batch is complete, and then the surface layer returns an event to the application via the completion queue. Each batch can have up to 3 callbacks:
recv_initial_metadata_ready (called by the transport when the recv_initial_metadata op is complete)
recv_message_ready (called by the transport when the recv_message op is complete)
on_complete (called by the transport when the entire batch is complete)
上面三個回調函數(shù),肯定會被執(zhí)行
一個batch可以分成這三個階段recv_initial_metadata,recv_message_ready,on_complete。
一個call的時序圖:
Client send_initial_metadata: Initiate an RPC with a path (method) and authority
Server recv_initial_metadata: accept an RPC
Client send_message: Supply the input proto for the RPC
Server recv_message: Get the input proto from the RPC
Client send_trailing_metadata: This is a half-close indicating that the client will not be sending any more messages
Server recv_trailing_metadata: The server sees this from the client and knows that it will not get any more messages. This won't complete yet though, as described above.
Server send_initial_metadata, send_message, send_trailing_metadata: A batch can contain multiple ops, and this batch provides the RPC response headers, response content, and status. Note that sending the trailing metadata will also complete the server's receive of trailing metadata.
Client recv_initial_metadata: The number of ops in one side of the batch has no relation with the number of ops on the other side of the batch. In this case, the client is just collecting the response headers.
Client recv_message, recv_trailing_metadata: Get the data response and status
模式處理
服務器端異步模式框架,添加自定義Event Queue增加并發(fā)性能