Yanyg - Software Engineer

RPC Howto

目录

1 硬件适配层

管理网卡,分流(Flow Bifurcation or Flow Director):

  • 传统网卡
  • RDMA网卡
  • 智能网卡

2 网络层

构建Connection语义。

  • 内核Socket, epoll;
  • 用户态TCP/IP, mTCP;
  • RDMA网络层;
  • Multipath管理;// 建立,拥塞,负载均衡;
  • 鉴权认证;
  • ShareLink

3 传输层

Crc, Compress, Encryption;

4 协议层

Serialize, Deserialize, Packets.

5 接口层

Channel, Client used to submit a RPC to RPC core.

6 Example

6.1 Client

Protobuf service client.

class RpcChannel : public google::protobuf::RpcChannel
{
  void CallMethod(...) override
  {
    // do some task, then dispatch to RpcClientCore.
  }
  // Non-virtual new method (fast service)
  void CallMethod(uint16_t serviceId, const FastRequest &req, FastResponse *resp,
                  Closure *done);
};

void Write(RpcController *ctrl, const ReadRequestPB &req, ReadResponsePB *resp)
{
  // protobuf framework
  RpcChannel channel; // channel can be cached to reduce cpu.
  mRpcClient->OpenChannel(addr, &channel);
  IoService::Stub stub(&channel);
  SyncClosure sync;
  stub.Write(ctrl, req, resp, done, &sync);
  sync.Wait();
  // parse response and/or update ctrl.
}

void Write(RpcController *ctrl, const FastRequest &req, FastResponse *resp)
{
  // protobuf framework
  RpcChannel channel; // channel can be cached to reduce cpu.
  mRpcClient->OpenChannel(addr, &channel);
  IoService::Stub stub(&channel);
  SyncClosure sync;
  stub.Write(ctrl, req, resp, done, &sync);
  sync.Wait();
  // parse response and/or update ctrl.
}

6.2 Server

Protobuf service server.

void Start()
{
  RpcServer server;
  server.RegisterService(&mIoService);
  server.Start(); // start receive packets from network.
}

void Write(RpcController *ctrl,
           const ReadRequestPB &req, ReadResponsePB *resp,
           Closure *done)
{
  // Write Task:
  // parse and check request
  // then do Write task, then fill response.
  int result = doWriteTask(ctrl, req, resp);
  resp->set_result(result);
  done->Run();
}

7 References

GRPC
https://grpc.io/
  • A high performance, open source universal RPC framework
  • From google
BRPC
https://github.com/apache/incubator-brpc
  • An industrial-grade RPC framework used throughout Baidu, with 1,000,000+ instances(not counting clients) and thousands kinds of services. "brpc" means "better RPC".
  • From baidu
Hadoop RPC
https://github.com/apache/hadoop/tree/trunk/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/ipc
Seastar
https://github.com/scylladb/seastar
  • SeaStar is an event-driven framework allowing you to write non-blocking, asynchronous code in a relatively straightforward manner (once understood). It is based on futures.
(no term)
Fstack
QUIC
https://en.wikipedia.org/wiki/QUIC
(no term)
Flow Director/Flow Bifurcation
RPC 实战与核心原理
https://time.geekbang.org/column/intro/100046201?tab=catalog