Five, thrift analysis and summary of several working modes of server terminal
Thrift provides a variety of operating modes for the server side, this article will cover the following 5 working modes: Tsimpleserver, Tnonblockingserver, Thshaserver, Tthreadpoolserver, Tthreadedselectorserver, the working mode in this 5 works in detail as follows:
1. Tsimpleserver mode
Tsimpleserver's working mode has only one worker thread, which loops through the arrival of the new request and completes the processing of the request, which is used only in a simple demo, and it works in 5.1:
Figure 5.1 Tsimpleserver mode of operation
Tsimpleserver's working mode uses the simplest blocking IO, which is simple and easy to understand, but can only receive and process a socket connection at a time, is inefficient, and is mainly used to demonstrate the work process of thrift, which is seldom used in the actual development process.
2. Tnonblockingserver mode
Tnonblockingserver work mode, this mode is also single-threaded, but the mode uses NIO, all sockets are registered in the selector, in a thread through the Seletor loop monitoring all sockets, At the end of each selector, all sockets in the ready state are processed, data is read for the socket with data arrival, and data is sent to the socket with data sent. For a listening socket, a new business socket is generated and registered to selector, as shown in 5.2:
Figure 5.2, Tnonblockingserver working mode
After reading the data in 5.2, the business process is to call the specific function to complete processing according to the call request, and only complete the function processing to carry out the subsequent operation;
Tnonblockingserver Mode Advantages:
Compared to the Tsimpleserver efficiency improvement is mainly embodied in the IO multiplexing, Tnonblockingserver uses non-blocking IO, simultaneously monitors the state change of multiple sockets;
Tnonblockingserver Mode Disadvantages:
Tnonblockingserver mode in the business process or the use of single-threaded sequence to complete, when the business process is more complex, time-consuming, such as some interface functions need to read the database execution time is longer, this mode is not efficient, because the multiple call request task is still sequential one after another execution.
3. Thshaserver mode (semi-synchronous semi-asynchronous)
The Thshaserver class is a subclass of the Tnonblockingserver class, In the Tnonblockingserver mode in section 5.2, the use of a thread to complete the monitoring of all sockets and business processing, resulting in a low efficiency, the introduction of Thshaserver mode is partly to solve these problems. In Thshaserver mode, a line pool is introduced to specialize in business processing, as shown in 5.3;
Figure 5.3 Thshaserver Mode
Advantages of Thshaserver:
Compared with the tnonblockingserver mode, thshaserver after the data read, the business process to a thread pool to complete, the main thread directly back to the next cycle operation, greatly improved efficiency;
Disadvantages of Thshaserver:
As can be seen from Figure 5.3, the main thread needs to complete the monitoring of all sockets and data read and write work, when the number of concurrent requests, and the amount of data sent, the listener socket on the new connection request can not be timely accepted.
4. Tthreadpoolserver mode
Tthreadpoolserver mode uses blocking socket mode, the main thread is responsible for blocking the interception of "listening socket" in the presence of a new socket, business processing by a thread pool to handle, as shown in 5.4:
Figure 5.4 Thread pool mode working process
Tthreadpoolserver Mode Advantages:
In thread pool mode, both the data read and the business processing are left to the thread pool, and the main thread is only responsible for listening for new connections, so a newer connection can be accepted in a large amount of concurrency. Thread pool mode is suitable for the server side can predict the maximum number of client concurrency, when each request can be the business thread pool timely processing, performance is very high.
Tthreadpoolserver Mode Disadvantages:
The processing power of the thread pool pattern is limited by the ability of the wire pool to work, and when the number of concurrent requests is greater than the number of threads in the thread pool, new requests can only be queued.
5. Tthreadedselectorserver
The Tthreadedselectorserver mode is the most advanced model currently provided by thrift, and it has internal if several parts constitute:
(1) A Acceptthread thread object specifically designed to handle new connections on the listener socket;
(2) Several Selectorthread objects are designed to handle the network I/O operations of a business socket, and all network data reads and writes are performed by these threads;
(3) A Load Balancer Selectorthreadloadbalancer object that is used primarily to determine which selectorthread thread the new connection request is assigned to when the Acceptthread thread receives a new socket connection request.
(4) A executorservice type of work thread pool, in the Selectorthread thread, the supervisor hears that there is a call request in the business socket, then reads the request. A thread in the Executorservice thread pool completes the execution of this call;
Figure 5.5 The working process of Tthreadedselectorserver mode
As shown in 5.5, the Tthreadedselectorserver mode has a dedicated thread acceptthread to handle new connection requests, so it can respond to a large number of concurrent connection requests in a timely manner, and it i/the network O operations are dispersed across multiple selectorthread threads, so it is possible to read and write network I/O quickly, and can cope well with network I/O; tthreadedselectorserver performance is not bad for most scenarios, so If you really do not know which mode of work to choose, use Tthreadedselectorserver.
Learn Thrift (iii)