Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the concept of Reactor network model in Java IO

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you what is the concept of the Reactor network model in Java IO, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

What is the Reactor model:

The reactor design pattern is an event handling pattern for handling service requests delivered concurrently to a service handler by one or more inputs. The service handler then demultiplexes the incoming requests and dispatches them synchronously to the associated request handlers.

The Reactor pattern, also known as the reactor design pattern, is an event design pattern that processes service requests and submits them concurrently to one or more service processors. When the request arrives, the request is distributed to the corresponding request processor by demultiplexing through the service processor. Reactor mode mainly consists of two core parts: Reactor and processor Handler. As shown in the following figure, both of them are responsible for the following tasks:

Reactor: responsible for listening and distributing events. Event types include connection events, read-write events

Handler: responsible for handling events, such as read-> business logic (decode + compute + encode)-> send

In most scenarios, there are several steps to process a network request:

① read: reads data from socket.

② decode: decoding. The data on the network is transmitted in the form of byte. In order to get the real request, it must be decoded.

③ compute: computing, that is, business processing.

④ encode: coding, the data on the network is transmitted in the form of byte, that is, socket only receives byte, so coding is necessary.

⑤ send: send reply data

For Reactor mode, whenever an Event is input to the Server side, the Service Handler will forward (dispatch) the corresponding Handler for processing. Three roles defined in the Reactor model:

Reactor: dispatcher, which is responsible for listening and assigning events, and dispatching events to the corresponding Handler. New events include connection establishment, read ready, write ready, and so on.

Acceptor: request connector to handle new client connections. When the Reactor receives the connection event from the client side, it forwards it to the Acceptor, and the Acceptor receives the connection to the Client, creates the corresponding Handler, and registers the Handler with Reactor.

Handler: request processor, responsible for handling events, binding itself to events, performing non-blocking read / write tasks, completing channel reading, and writing results to channel after processing business logic. Resource pools are available for management.

The model is roughly shown in the following figure:

For read / write requests, the Reactor model follows the following process:

(1) the application registers read / write ready events and associated event handlers

(2) the event splitter waits for the event to occur.

(3) when a read / write ready event occurs, the event splitter calls the event handler registered in the first step.

Second, the classification of Reactor model:

The Reactor in the Reactor model can be single or multiple, and Handler can also be single-threaded or multithreaded, so there are roughly three combined modes:

Single Reactor single thread model

Single Reactor multithreading model

Master-slave Reactor single thread model

Master-slave Reactor multithreading model

The third kind of master-slave Reactor single-thread model has no practical significance, so the other three models are introduced below.

1. Single Reactor single thread model: 1.1.The processing flow:

(1) the Reactor thread listens for events through select and distributes them through Dispatch after receiving them.

(2) if it is a connection establishment event, distributing the event to Acceptor,Acceptor will obtain the connection through the accept () method and create a Handler object to handle subsequent response events.

(3) if it is an IO read-write event, Reactor will leave the event to the currently connected Handler to handle.

(4) Handler will complete the complete business process of read-> Business processing-> send.

1.2. Advantages and disadvantages:

The advantage of single Reactor single thread model is that all the processing logic is implemented in one thread, and there is no problem of multithreading, process communication and competition. However, there are serious problems in the performance and reliability of the model.

① performance: only in the code to distinguish between components, the overall operation or single thread, can not make full use of CPU resources, and the Handler business processing part is not asynchronous, a Reactor is not only responsible for handling connection requests, but also responsible for handling read and write requests, generally speaking, processing connection requests is very fast, but dealing with read and write requests involves business logic processing, which is relatively slow. Therefore, when Reactor processes read and write requests, other requests can only wait, which can easily cause the performance bottleneck of the system.

② reliability: once the Reactor thread is accidentally interrupted or enters a dead loop, the communication module of the whole system will be unavailable, unable to receive and process external messages, resulting in node failure.

Therefore, the single-Reactor single-process model is not suitable for computing-intensive scenarios, but only for scenarios where business processing is very fast. The thread model of Redis is based on the single Reactor single thread model. Because Redis business processing is mainly done in memory, the speed of operation is very fast, and the performance bottleneck is not on CPU, so the processing of commands in Redis is a single process.

2. Single Reactor multithreading model:

In order to solve the performance problems of the single Reactor single thread model, the single Reactor multithread model is introduced, which uses multithreading (thread pool) in the event processor part.

2.1. Processing flow:

(1) the Reactor thread listens for events through select and distributes them through Dispatch after receiving them.

(2) if it is a connection establishment event, distributing the event to Acceptor,Acceptor will obtain the connection through the accept () method and create a Handler object to handle subsequent response events.

(3) if it is an IO read-write event, Reactor will hand the event over to the Handler corresponding to the current connection.

(4) unlike a single Reactor single thread, Handler no longer does specific business processing, but is only responsible for receiving and responding to events. After receiving data through read, the data is sent to the subsequent Worker thread pool for business processing.

(5) Worker thread pool redistributes threads for business processing, and sends the response result to Handler for processing.

(6) after receiving the response result, Handler returns the response result to Client through send.

2.2. Advantages and disadvantages:

Compared with the first model, after processing the business logic, that is, after obtaining the IO read and write events, it is handed over to the thread pool to handle, and the Handler receives the response and returns the response result to the client through send. This can reduce the performance overhead of Reactor, thus focus more on event distribution, improve the throughput of the entire application, and Handler uses multi-threaded mode, which can make full use of the performance of CPU. But the problem with this model is:

(1) Handler uses multi-thread mode, which naturally brings the cost of multi-thread competition for resources, and involves the mutual exclusion and protection mechanism of shared data, so the implementation is more complex.

(2) A single Reactor is responsible for monitoring, distributing and responding to all events. For high concurrency scenarios, it is easy to cause performance bottlenecks.

3. Master-slave Reactor multithreading model:

The single Reactor multithreading model solves the performance problem of Handler single threading, but Reactor is still single threaded, and there will still be performance bottlenecks for high concurrency scenarios, so it is necessary to adjust Reactor to multithreading mode, which is the master-slave Reactor multithreading model to be introduced next. The master-slave Reactor multithreading model divides Reactor into two parts:

(1) MainReactor: it is only responsible for handling connection establishment events, listens to server socket through select, and registers the established socketChannel with subReactor, usually with one thread.

(2) SubReactor: responsible for reading and writing events, maintaining its own selector, multiplexing IO read and write events based on MainReactor registered SocketChannel, reading and writing network data, and handing business processing to worker thread pool. The number of SubReactor is generally the same as that of CPU.

3.1. Processing flow:

(1) the MainReactor object in the main thread listens for events through select, receives the events and distributes them through Dispatch. If the event type is a connection establishment event, it is distributed to Acceptor for connection establishment.

Connection establishment:

① randomly selects a Reactor thread from the main thread pool as the Acceptor thread to bind the listening port and receive client connections.

After receiving the client connection request, the ② Acceptor thread creates a new SocketChannel and registers it with other Reactor threads in the main thread pool, which is responsible for access authentication, IP blacklist filtering, handshake and other operations.

After the ③ step ② is completed, the link of the business layer is formally established, the SocketChannel is removed from the multiplexer of the Reactor thread of the main thread pool, re-registered with the thread of the SubReactor thread pool, and a Handler is created to handle various connection events.

(2) if the connection establishment event is not received, it is distributed to SubReactor,SubReactor to call the Handler corresponding to the current connection for processing.

(3) after reading the data through read, Handler distributes the data to the Worker thread pool for business processing, while the Worker thread pool allocates threads for business processing, and sends the response result to Handler upon completion.

(4) after receiving the response result, Handler returns the response result to Client through send.

3.2. Advantages and disadvantages:

The advantage of the master-slave Reactor multithread model is that the main thread and the child thread have a clear division of labor, the main thread is only responsible for receiving the new connection, and the child thread is responsible for completing the subsequent business processing, and the interaction between the master thread and the child thread is also very simple. After the child thread receives the connection from the main thread, it can only deal with the business without paying attention to the main thread, and can directly send the processing result to the client in the child thread.

The Reactor model is suitable for high concurrency scenarios, and the Netty network communication framework also uses this implementation.

4. Advantages and disadvantages of Reactor:

(1) Fast response and need not be blocked by a single synchronization time, although Reactor itself is still synchronous.

(2) the complex multithreading and synchronization problems can be avoided to the greatest extent, and the switching overhead of multithreading / process can be avoided.

(3) scalability, you can easily increase the number of Reactor instances to make full use of CPU resources.

(4) reusability, the Reactor model itself has nothing to do with the specific event processing logic, and has high reusability.

The above is all the content of the article "what is the concept of Reactor network model in Java IO". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report