Serve thrift

Learn how to serve thrift.

thrift is a RPC framework used widely in various environments, which was developed by Facebook and adopted by Apache later. In order to interact with thrift servers and solves issues on thread-safety, usabilities and concurrencies, brpc directly supports the thrift protocol that is used by thrift in NonBlocking mode.

Example: example/thrift_extension_c++.

Advantages compared to the official solution:

  • Thread safety. No need to set up separate clients for each thread.
  • Supports synchronous, asynchronous, batch synchronous, batch asynchronous, and other access methods. Combination channels such as ParallelChannel are also supported.
  • Support various connection types(short, connection pool). Support timeout, backup request, cancellation, tracing, built-in services, and other benefits offered by brpc.
  • Better performance.

Compile

brpc depends on the thrift library and reuses some code generated by thrift tools. Please read official documents to find out how to write thrift files, generate code, compilations etc.

brpc does not enable thrift support or depend on the thrift lib by default. If the support is needed, compile brpc with extra –with-thrift or -DWITH_THRIFT=ON

Install thrift under Linux Read Official wiki to install depended libs and tools, then download thrift source code from official site, uncompress and compile。

  1. wget http://www.apache.org/dist/thrift/0.11.0/thrift-0.11.0.tar.gz
  2. tar -xf thrift-0.11.0.tar.gz
  3. cd thrift-0.11.0/
  4. ./configure --prefix=/usr --with-ruby=no --with-python=no --with-java=no --with-go=no --with-perl=no --with-php=no --with-csharp=no --with-erlang=no --with-lua=no --with-nodejs=no
  5. make CPPFLAGS=-DFORCE_BOOST_SMART_PTR -j 4 -s
  6. sudo make install

Config brpc with thrift support, then make. The compiled libbrpc.a includes extended code for thrift support and can be linked normally as in other brpc projects.

  1. # Ubuntu
  2. sh config_brpc.sh --headers=/usr/include --libs=/usr/lib --with-thrift
  3. # Fedora/CentOS
  4. sh config_brpc.sh --headers=/usr/include --libs=/usr/lib64 --with-thrift
  5. # Or use cmake
  6. mkdir build && cd build && cmake ../ -DWITH_THRIFT=ON

Read Getting Started for more compilation options.

Client accesses thrift server

Steps:

  • Create a Channel setting protocol to brpc::PROTOCOL_THRIFT
  • Create brpc::ThriftStub
  • Use native request and response to start RPC directly.

Example code:

  1. #include <brpc/channel.h>
  2. #include <brpc/thrift_message.h> // Defines ThriftStub
  3. ...
  4. DEFINE_string(server, "0.0.0.0:8019", "IP Address of thrift server");
  5. DEFINE_string(load_balancer, "", "The algorithm for load balancing");
  6. ...
  7. brpc::ChannelOptions options;
  8. options.protocol = brpc::PROTOCOL_THRIFT;
  9. brpc::Channel thrift_channel;
  10. if (thrift_channel.Init(Flags_server.c_str(), FLAGS_load_balancer.c_str(), &options) != 0) {
  11. LOG(ERROR) << "Fail to initialize thrift channel";
  12. return -1;
  13. }
  14. brpc::ThriftStub stub(&thrift_channel);
  15. ...
  16. // example::[EchoRequest/EchoResponse] are types generated by thrift
  17. example::EchoRequest req;
  18. example::EchoResponse res;
  19. req.data = "hello";
  20. stub.CallMethod("Echo", &cntl, &req, &res, NULL);
  21. if (cntl.Failed()) {
  22. LOG(ERROR) << "Fail to send thrift request, " << cntl.ErrorText();
  23. return -1;
  24. }

Server processes thrift requests

Inherit brpc::ThriftService to implement the processing code, which may call the native handler generated by thrift to re-use existing entry directly, or read the request and set the response directly just as in other protobuf services.

  1. class EchoServiceImpl : public brpc::ThriftService {
  2. public:
  3. void ProcessThriftFramedRequest(brpc::Controller* cntl,
  4. brpc::ThriftFramedMessage* req,
  5. brpc::ThriftFramedMessage* res,
  6. google::protobuf::Closure* done) override {
  7. // Dispatch calls to different methods
  8. if (cntl->thrift_method_name() == "Echo") {
  9. return Echo(cntl, req->Cast<example::EchoRequest>(),
  10. res->Cast<example::EchoResponse>(), done);
  11. } else {
  12. cntl->SetFailed(brpc::ENOMETHOD, "Fail to find method=%s",
  13. cntl->thrift_method_name().c_str());
  14. done->Run();
  15. }
  16. }
  17. void Echo(brpc::Controller* cntl,
  18. const example::EchoRequest* req,
  19. example::EchoResponse* res,
  20. google::protobuf::Closure* done) {
  21. // This object helps you to call done->Run() in RAII style. If you need
  22. // to process the request asynchronously, pass done_guard.release().
  23. brpc::ClosureGuard done_guard(done);
  24. res->data = req->data + " (processed)";
  25. }
  26. };

Set the implemented service to ServerOptions.thrift_service and start the service.

  1. brpc::Server server;
  2. brpc::ServerOptions options;
  3. options.thrift_service = new EchoServiceImpl;
  4. options.idle_timeout_sec = FLAGS_idle_timeout_s;
  5. options.max_concurrency = FLAGS_max_concurrency;
  6. // Start the server.
  7. if (server.Start(FLAGS_port, &options) != 0) {
  8. LOG(ERROR) << "Fail to start EchoServer";
  9. return -1;
  10. }

Performance test for native thrift compare with brpc thrift implementaion

Test Env: 48 core 2.30GHz

server side return string “hello” sent from client

FrameworkThreads NumQPSAvg lantecyCPU
native thrift606.9w0.9ms2.8%
brpc thrift6030w0.2ms18%

server side return string “hello” * 1000

FrameworkThreads NumQPSAvg lantecyCPU
native thrift605.2w1.1ms4.5%
brpc thrift6019.5w0.3ms22%

server side do some complicated math and return string “hello” * 1000

FrameworkThreads NumQPSAvg lantecyCPU
native thrift601.7w3.5ms76%
brpc thrift602.1w2.9ms93%

Last modified February 13, 2023: add the new committer Zhaogeng Li (#133) (ade03ff)