Understand Thrift from the shallower to the deeper (3) - Analysis of several working modes of the Thrift server side

Reprint address: https://blog.csdn.net/houjixin/article/details/42779915

For related sample codes, see: http://download.csdn.net/detail/hjx_1000/8374829

5. Analysis and summary of several working modes of the Thrift server

Thrift provides a variety of working modes for the server side. This article will involve the following 5 working modes: TSimpleServer, TNonblockingServer, THsHaServer, TThreadPoolServer, TThreadedSelectorServer. The detailed working principles of these 5 working modes are as follows:

1. TSimpleServer mode

The working mode of TSimpleServer has only one worker thread, which loops to monitor the arrival of new requests and completes the processing of requests. It is only used for simple demonstrations. Its working method is shown in Figure 5.1:


Figure 5.1 Working mode of TSimpleServer

The working mode of TSimpleServer adopts the simplest blocking IO. The implementation method is concise and easy to understand. However, it can only receive and process one socket connection at a time, which is relatively inefficient. It is mainly used to demonstrate the working process of Thrift, and is rarely used in the actual development process. use it.

2. TNonblockingServer mode

TNonblockingServer working mode, this mode is also single-threaded work, but this mode adopts NIO method, all sockets are registered in the selector, and all sockets are monitored through the selector loop in one thread, and each time the selector ends, all sockets are processed. For the socket in the ready state, the data read operation is performed for the socket with incoming data, the data transmission is performed for the socket with data transmission, and a new business socket is generated for the listening socket and registered in the selector, as shown in Figure 5.2 below. Show:


Figure 5.2, TNonblockingServer working mode

The business processing after reading the data in Figure 5.2 above is to call a specific function to complete the processing according to the read call request, and subsequent operations can only be performed after the function processing is completed;

Advantages of TNonblockingServer mode:

Compared with TSimpleServer, the efficiency improvement is mainly reflected in IO multiplexing. TNonblockingServer adopts non-blocking IO and monitors the status changes of multiple sockets at the same time;

Disadvantages of TNonblockingServer mode:

The TNonblockingServer mode still uses a single-threaded sequence to complete business processing. When business processing is complex and time-consuming, for example, some interface functions need to read the database and execute for a long time. At this time, the efficiency of this mode is not high, because there are many The call request tasks are still executed sequentially one after the other.

3. THsHaServer mode (semi-synchronous and semi-asynchronous)

The THsHaServer class is a subclass of the TNonblockingServer class. In the TNonblockingServer mode in Section 5.2, a thread is used to monitor and process all sockets, resulting in low efficiency. The introduction of the THsHaServer mode partially solves these problems. In the THsHaServer mode, a thread pool is introduced for business processing, as shown in Figure 5.3 below;


Figure 5.3 THsHaServer mode

Advantages of THsHaServer:

Compared with the TNonblockingServer mode, after THsHaServer completes the data reading, the business processing process is completed by a thread pool, and the main thread directly returns to the next loop operation, which greatly improves the efficiency;

Disadvantages of THsHaServer:

As can be seen from Figure 5.3, the main thread needs to complete the work of monitoring all sockets and reading and writing data. When the number of concurrent requests is large and the amount of data sent is large, new connection requests on the monitoring socket cannot be accepted in time.

4. TThreadPoolServer mode

The TThreadPoolServer mode works in a blocking socket mode. The main thread is responsible for blocking monitoring of whether a new socket arrives in the "listening socket", and business processing is handled by a thread pool, as shown in Figure 5.4 below:


Figure 5.4 Working process of thread pool mode

TThreadPoolServer模式优点:

线程池模式中,数据读取和业务处理都交由线程池完成,主线程只负责监听新连接,因此在并发量较大时新连接也能够被及时接受。线程池模式比较适合服务器端能预知最多有多少个客户端并发的情况,这时每个请求都能被业务线程池及时处理,性能也非常高。

TThreadPoolServer模式缺点:

线程池模式的处理能力受限于线程池的工作能力,当并发请求数大于线程池中的线程数时,新请求也只能排队等待。

5.      TThreadedSelectorServer

TThreadedSelectorServer模式是目前Thrift提供的最高级的模式,它内部有如果几个部分构成:

(1)  一个AcceptThread线程对象,专门用于处理监听socket上的新连接;

(2)  若干个SelectorThread对象专门用于处理业务socket的网络I/O操作,所有网络数据的读写均是有这些线程来完成;

(3)  一个负载均衡器SelectorThreadLoadBalancer对象,主要用于AcceptThread线程接收到一个新socket连接请求时,决定将这个新连接请求分配给哪个SelectorThread线程。

(4)  一个ExecutorService类型的工作线程池,在SelectorThread线程中,监听到有业务socket中有调用请求过来,则将请求读取之后,交个ExecutorService线程池中的线程完成此次调用的具体执行;


图5.5 TThreadedSelectorServer模式的工作过程

如上图5.5所示,TThreadedSelectorServer模式中有一个专门的线程AcceptThread用于处理新连接请求,因此能够及时响应大量并发连接请求;另外它将网络I/O操作分散到多个SelectorThread线程中来完成,因此能够快速对网络I/O进行读写操作,能够很好地应对网络I/O较多的情况;TThreadedSelectorServer对于大部分应用场景性能都不会差,因此,如果实在不知道选择哪种工作模式,使用TThreadedSelectorServer就可以。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324691823&siteId=291194637