(转)Java版的各类Thrift server实现的比较

本文是我对这篇文章的翻译:Thrift Java Servers Compared,为了便于阅读,我将原文附于此处,翻译穿插在其中。此外,为了防止原连接在将来某一天失效后,文中的图片再也看不到的问题,我将原文中的图片也保存到了本站的服务器上,我不知道github或原做者是否容许这样作,但我翻译本文仅在于传播知识的目的,在此向原做者和github表示深深的感谢:感谢大家分享了这样好的文章。
       
       
       
Thrift Java Servers Compared
This article talks only about Java servers. See this page if you are interested in C++ servers.
本文仅讨论Java版的Thrift server.若是你对C++版的感兴趣,请参考 这个 页面。

Thrift is a cross-language serialization/RPC framework with three major components, protocol, transport, and server. Protocol defines how messages are serialized. Transport defines how messages are communicated between client and server. Server receives serialized messages from the transport, deserializes them according to the protocol and invokes user-defined message handlers, and serializes the responses from the handlers and writes them back to the transport. The modular architecture of Thrift allows it to offer various choices of servers. Here are the list of server available for Java:
Thrift 是一个跨语言的序列化/RPC框架,它含有三个主要的组件:protocol,transport和server,其中,protocol定义了消息是怎样序列化的,transport定义了消息是怎样在客户端和服务器端之间通讯的,server用于从transport接收序列化的消息,根据protocol反序列化之,调用用户定义的消息处理器,并序列化消息处理器的响应,而后再将它们写回transport。Thrift模块化的结构使得它能提供各类server实现。下面列出了Java中可用的server实现:
· TSimpleServer
· TNonblockingServer
· THsHaServer
· TThreadedSelectorServer
· TThreadPoolServer
Having choices is great, but which server is right for you? In this article, I'll describe the differences among all those servers and show benchmark results to illustrate performance characteristics (the details of the benchmark is explained in Appendix B). Let's start with the simplest one: TSimpleServer.
有多个选择很好,可是哪一个适合你呢?在本文中,我将描述这些server之间的区别,并展现测试结果,以说明它们的性能特色(测试的细节在附录B中)。下面,咱们就从最简单的开始:TSimpleServer。
文章来源:http://www.codelast.com/html

TSimpleServer
TSimpleServer accepts a connection, processes requests from the connection until the client closes the connection, and goes back to accept a new connection. Since it is all done in a single thread with blocking I/O, it can only serve one client connection, and all the other clients will have to wait until they get accepted. TSimpleServer is mainly used for testing purpose. Don't use it in production!
TSimplerServer接受一个链接,处理链接请求,直到客户端关闭了链接,它才回去接受一个新的链接。正由于它只在一个单独的线程中以阻塞I/O的方式完成这些工做,因此它只能服务一个客户端链接,其余全部客户端在被服务器端接受以前都只能等待。TSimpleServer主要用于测试目的,不要在生产环境中使用它!
文章来源:http://www.codelast.com/java

 

TNonblockingServer vs. THsHaServer
TNonblockingServer solves the problem with TSimpleServer of one client blocking all the other clients by using non-blocking I/O. It usesjava.nio.channels.Selector, which allows you to get blocked on multiple connections instead of a single connection by calling select(). The select() call returns when one ore more connections are ready to be accepted/read/written. TNonblockingServer handles those connections either by accepting it, reading data from it, or writing data to it, and calls select() again to wait for the next available connections. This way, multiple clients can be served without one client starving others.
TNonblockingServer使用非阻塞的I/O解决了TSimpleServer一个客户端阻塞其余全部客户端的问题。它使用了java.nio.channels.Selector,经过调用select(),它使得你阻塞在多个链接上,而不是阻塞在单一的链接上。当一或多个链接准备好被接受/读/写时,select()调用便会返回。TNonblockingServer处理这些链接的时候,要么接受它,要么从它那读数据,要么把数据写到它那里,而后再次调用select()来等待下一个可用的链接。通用这种方式,server可同时服务多个客户端,而不会出现一个客户端把其余客户端所有“饿死”的状况。git

 

There is a catch, however. Messages are processed by the same thread that calls select(). Let's say there are 10 clients, and each message takes 100 ms to process. What would be the latency and throughput? While a message is being processed, 9 clients are waiting to be selected, so it takes 1 second for the clients to get the response back from the server, and throughput will be 10 requests / second. Wouldn't it be great if multiple messages can be processed simultaneously?
然而,还有个棘手的问题:全部消息是被调用select()方法的同一个线程处理的。假设有10个客户端,处理每条消息所需时间为100毫秒,那么,latency和吞吐量分别是多少?当一条消息被处理的时候,其余9个客户端就等着被select,因此客户端须要等待1秒钟才能从服务器端获得回应,吞吐量就是10个请求/秒。若是能够同时处理多条消息的话,会很不错吧?github

This is where THsHaServer (Half-Sync/Half-Async server) comes into picture. It uses a single thread for network I/O, and a separate pool of worker threads to handle message processing. This way messages will get processed immediately if there is an idle worker threads, and multiple messages can be processed concurrently. Using the example above, now the latency is 100 ms and throughput will be 100 requests / sec.
所以,THsHaServer(半同步/半异步的server)就应运而生了。它使用一个单独的线程来处理网络I/O,一个独立的worker线程池来处理消息。这样,只要有空闲的worker线程,消息就会被当即处理,所以多条消息能被并行处理。用上面的例子来讲,如今的latency就是100毫秒,而吞吐量就是100个请求/秒。apache

To demonstrate this, I ran a benchmark with 10 clients and a modified message handler that simply sleeps for 100 ms before returning. I used THsHaServer with 10 worker threads. The handler looks something like this:
为了演示,我作了一个测试,有10客户端和一个修改过的消息处理器——它的功能仅仅是在返回以前简单地sleep 100毫秒。我使用的是有10个worker线程的THsHaServer。消息处理器的代码看上去就像下面这样:api

1安全

2服务器

3网络

4并发

5

6

7

8

public ResponseCode sleep() throws TException

{  

try {

Thread.sleep(100);

} catch (Exception ex) {

}

return ResponseCode.Success;

}

thrift-java-servers-compared

thrift-java-servers-compared

The results are as expected. THsHaServer is able to process all the requests concurrently, while TNonblockingServer processes requests one at a time.

结果正如咱们想像的那样,THsHaServer可以并行处理全部请求,而TNonblockingServer只能一次处理一个请求。
文章来源:http://www.codelast.com/

THsHaServer vs. TThreadedSelectorServer

Thrift 0.8 introduced yet another server, TThreadedSelectorServer. The main difference between TThreadedSelectorServer and THsHaServer is that TThreadedSelectorServer allows you to have multiple threads for network I/O. It maintains 2 thread pools, one for handling network I/O, and one for handling request processing. TThreadedSelectorServer performs better than THsHaServer when the network io is the bottleneck. To show the difference, I ran a benchmark with a handler that returns immediately without doing anything, and measured the average latency and throughput with varying number of clients. I used 32 worker threads for THsHaServer, and 16 worker threads/16 selector threads for TThreadedSelectorServer.
Thrift 0.8引入了另外一种server实现,即TThreadedSelectorServer。它与THsHaServer的主要区别在于,TThreadedSelectorServer容许你用多个线程来处理网络I/O。它维护了两个线程池,一个用来处理网络I/O,另外一个用来进行请求的处理。当网络I/O是瓶颈的时候,TThreadedSelectorServer比THsHaServer的表现要好。为了展示它们的区别,我进行了一个测试,令其消息处理器在不作任何工做的状况下当即返回,以衡量在不一样客户端数量的状况下的平均latency和吞吐量。对THsHaServer,我使用32个worker线程;对TThreadedSelectorServer,我使用16个worker线程和16个selector线程。

thrift-java-servers-compared

thrift-java-servers-compared

The result shows that TThreadedSelectorServer has much higher throughput than THsHaServer while maintaining lower latency.
结果显示,TThreadedSelectorServer比THsHaServer的吞吐量高得多,而且维持在一个更低的latency上。
文章来源:http://www.codelast.com/

TThreadedSelectorServer vs. TThreadPoolServer

Finally, there is TThreadPoolServer. TThreadPoolServer is different from the other 3 servers in that:
最后,还剩下 TThreadPoolServer。TThreadPoolServer与其余三种server不一样的是:
·         There is a dedicated thread for accepting connections.
·         有一个专用的线程用来接受链接。
·         Once a connection is accepted, it gets scheduled to be processed by a worker thread in ThreadPoolExecutor.
·         一旦接受了一个链接,它就会被放入ThreadPoolExecutor中的一个worker线程里处理。
·         The worker thread is tied to the specific client connection until it's closed. Once the connection is closed, the worker thread goes back to the thread pool.
·         worker线程被绑定到特定的客户端链接上,直到它关闭。一旦链接关闭,该worker线程就又回到了线程池中。
·         You can configure both minimum and maximum number of threads in the thread pool. Default values are 5 and Integer.MAX_VALUE, respectively.
·         你能够配置线程池的最小、最大线程数,默认值分别是5(最小)和Integer.MAX_VALUE(最大)。

This means that if there are 10000 concurrent client connections, you need to run 10000 threads. As such, it is not as resource friendly as other servers. Also, if the number of clients exceeds the maximum number of threads in the thread pool, requests will be blocked until a worker thread becomes available.
这意味着,若是有1万个并发的客户端链接,你就须要运行1万个线程。因此它对系统资源的消耗不像其余类型的server同样那么“友好”。此外,若是客户端数量超过了线程池中的最大线程数,在有一个worker线程可用以前,请求将被一直阻塞在那里。

Having said that, TThreadPoolServer performs very well; on the box I'm using it's able to support 10000 concurrent clients without any problem. If you know the number of clients that will be connecting to your server in advance and you don't mind running a lot of threads, TThreadPoolServer might be a good choice for you.
咱们已经说过,TThreadPoolServer的表现很是优异。在我正在使用的计算机上,它能够支持1万个并发链接而没有任何问题。若是你提早知道了将要链接到你服务器上的客户端数量,而且你不介意运行大量线程的话,TThreadPoolServer对你多是个很好的选择。

thrift-java-servers-compared

thrift-java-servers-compared

文章来源:http://www.codelast.com/

Conclusion
结论

I hope this article helps you decide which Thrift server is right for you. I think TThreadedSelectorServer would be a safe choice for most of the use cases. You might also want to consider TThreadPoolServer if you can afford to run lots of concurrent threads. Feel free to send me email atmapkeeper-users@googlegroups.com or post your comments here if you have any questions/comments.
但愿本文能帮你作出决定:哪种Thrift server适合你。我认为TThreadedSelectorServer对大多数案例来讲都是个安全之选。若是你的系统资源容许运行大量并发线程的话,你可能会想考虑使用TThreadPoolServer。(后面的就不翻译了)

Appendix A: Hardware Configuration

 

1

2

3

4

Processors:     2 x Xeon E5620 2.40GHz (HT enabled, 8 cores, 16 threads)

Memory:         8GB

Network:        1Gb/s <full-duplex>

OS:             RHEL Server 5.4 Linux 2.6.18-164.2.1.el5 x86_64

 

Appendix B: Benchmark Details
It's pretty straightforward to run the benchmark yourself. First clone the MapKeeper repository and compile the stub java server:

 

1

2

3

git clone git://github.com/m1ch1/mapkeeper.git

cd mapkeeper/stubjava

make

Then, start the server you like to benchmark:

1

2

3

4

make run mode=threadpool    # run TThreadPoolServer

make run mode=nonblocking   # run TNonblockingServer

make run mode=hsha          # run THsHaServer

make run mode=selector      # run TThreadedSelectorServer

Then, clone YCSB repository and compile:

1

2

3

git clone git://github.com/brianfrankcooper/YCSB.git

cd YCSB

mvn clean package

Once the compilation finishes, you can run YCSB against the stub server:

1

./bin/ycsb load mapkeeper -P ./workloads/workloada

For more detailed information about how to use YCSB, check out their wiki page.

相关文章
相关标签/搜索