Detailed explanation of Reactor and Proactor threading models

1. Reactor mode

Reactor模式Also called Dispatcher模式, based onEvent-driven processing mode, The basic design idea isI/O multiplexing combined with thread pool. The picture below isEvent-driven processingSchematic diagram, you can see the general process isThe server program monitors and processes the incoming events of multiple requests, and dispatches the events to the processing thread corresponding to the request to complete the processing

Insert picture description here

There are 2 key components in the Reactor pattern:

  1. Reactor
    Runs in a separate thread, responsible for monitoring and distributing events, and dispatching events to the appropriate Handler to respond to IO requests
  2. Handler
    Bind itself to the event, and actually execute the event to be completed by the I/O. Reactor responds to I/O events by dispatching appropriate Handlers to perform non-blocking operations

According to the number of Reactor and the number of threads processing events, the Reactor mode has 3 implementations :

  1. Single Reactor single thread
  2. Single Reactor multi-threaded
  3. Master-slave Reactor multithreading

1.1 Single Reactor single thread

The schematic diagram of the implementation is as follows, and the message processing flow can be summarized as the following steps:

  1. Reactor monitors all events on the connection through select, and distributes them through dispatch after receiving the event
  2. If the event is a request to establish a connection, the Acceptor will accept the connection and create a Handler for it to handle subsequent events
  3. If it is not a connection establishment event, Reactor will distribute it to the corresponding Handler to respond
  4. Handler actually handles the event and completes the business processing flow of read->handle->send

Insert picture description here

单Reactor单线程模型Only the code components are distinguished. The overall operation is still single-threaded. The advantages and disadvantages are as follows:

  • 优点
    The model is simple, all processing is completed in one thread, there is no overhead of multi-threaded context switching, no process communication and lock competition issues
  • 缺点
  1. There is only one thread, which cannot fully utilize the performance of the multi-core CPU, causing waste
  2. When the Handler processes the business on a connection, the entire process cannot handle other connection events, which easily leads to performance bottlenecks
  3. Once the Reactor thread is interrupted unexpectedly or runs away, the communication module of the entire system may be unavailable, unable to receive and process external messages, and cause node failure
  • 适用场景
    The number of clients is limited and business processing is very fast. For example, Redis uses a single Reactor single thread model

1.2 Single Reactor multithreaded

The schematic diagram of the implementation is shown below, and the message processing flow can be divided into the following steps:

  1. Reactor monitors all events on the connection through select, and distributes them through dispatch after receiving the event
  2. If it is an event requesting to establish a connection, the Acceptor will process the connection request through accept, and then create a Handler to handle the connection and complete subsequent read and write events
  3. If it is not a connection establishment event, Reactor will distribute it to the corresponding Handler to respond. In this implementation, Handler is only responsible for responding to events. After read reads the data, it will be distributed to the Worker thread pool for handle business processing
  4. The Worker thread pool schedules threads to complete the actual business processing and returns the response results to the main thread Handler. The Handler returns the results to the requester through send to complete the request response process

Insert picture description here
单 Reactor 多线程模型Relatively 单Reactor单线程模型speaking,The handle business logic is handled by the thread pool, Its advantages and disadvantages are as follows:

  • 优点
    It can make full use of the processing power of multi-core CPU to make Reactor focus more on event distribution and improve the throughput of the entire application
  • 缺点
  1. Data sharing and access in a multi-threaded environment are more complicated. After the sub-thread completes the business processing, the result is passed to the main thread Handler for sending. It is necessary to consider the mutual exclusion and protection mechanism of shared data
  2. Reactor's main thread runs in a single thread, and is responsible for monitoring and responding to all events. It will become a performance bottleneck in high concurrency scenarios.

1.3 Master-slave Reactor multithreading

单 Reactor 多线程模型Medium Reactor runs in a single thread, and it is easy to become a performance bottleneck in high concurrency scenarios. A solution to this shortcoming isLet Reactor run in multiple threads, So it came into being 主从 Reactor 多线程模型. Compared with the second model, it divides Reactor into two parts:

  1. MainReactor is only used to handle the operation of network IO connection establishment, and register the established connection to SubReactor.
  2. SubReactor is responsible for processing the event of the connection registered on it, completing business processing,Usually the number of SubReactor can be equal to the number of CPU

主从 Reactor 多线程模型The message processing process can be divided into the following steps:

  1. Reactor main thread MainReactor through select Monitor connection establishment events, After receiving the event, receive it through Acceptor and handle the connection establishment event
  2. After Acceptor establishes the connection, MainReactor assigns the connection to the Reactor sub-thread SubReactor for processing. SubReactor will add the connection to the connection queue for monitoring, and create a Handler to handle the read and write events on the connection
  3. When a read-write event occurs, SubReactor calls the corresponding Handler to respond, and the Handler reads the data and distributes it to the Worker thread pool for handle business processing
  4. The Worker thread pool schedules threads to complete the actual business processing and returns the response result to the Handler of SubReactor. The Handler returns the result to the requester through send to complete the request response process

Insert picture description here
This Reactor implementation model is very widely used, and the more famousIncluding Nginx master-slave Reactor multi-process model, Memcached master-slave multi-threading, Netty master-slave multi-threading model supportThe main advantages are as follows:

  1. The data interaction between MainReactor and SubReactor is simple, MainReactor only needs to hand over the new connection to SubReactor, SubReactor does not need to return data
  2. MainReactor and SubReactor have clear master-slave responsibilities, MainReactor is only responsible for receiving new connections, and SubReactor is responsible for completing subsequent business processing

2. Proactor mode

In Reactor mode,Reactor waits for an event to occur in the user process through select polling, And then distribute this event to the Handler, which will do the actual read and write operations.In this process, the read and write operations are still synchronous. If the I/O operation is changed to asynchronous, it will be completed by the operating system, and the performance can be further improved. This is the asynchronous network model Proactor

Proactor模式It is also based on the event-driven model, but Proactor is not concerned 读取就绪事件, but concerned 读取完成事件, because asynchronous IO is that the operating system reads and writes data to the specified buffer, and the application can directly access it from the buffer. The following is a schematic diagram, and its various modules are as follows:

  1. Procator Initiator
    Responsible for creating Procator and Handler, and registering Procator and Handler to the kernel
  2. Asynchronous Operation Processor
    Responsible for processing registration requests and performing IO operations. Procator will be notified when the IO operation is completed
  3. Procator
    Procator waits for the completion of the IO event in the kernel process, According to the event type to call back the corresponding Handler for business processing. Handler is responsible for completing the actual business processing, and can also register a new Handler to the kernel

Insert picture description here
Taking the read operation as an example, Proactor模式the message processing flow is as follows:

  1. The application initializes Proactor and the corresponding Handler, and then registers it to the kernel
  2. The requester sends data, the operating system calls the kernel thread to complete the read operation, and puts the read content into the specified buffer
  3. Proactor waits for the read operation completion event in the kernel process. After the read completion event is captured, it calls back the corresponding Handler
  4. Handler reads data directly from the buffer for processing, no actual reading operation is needed anymore

In summary, we can understand the difference between Proactor and Reactor:

  1. Reactor polls and monitors events by the application itself, while Proactor monitors events by the kernel process, which has lower overhead
  2. Reactor read and write IO operations are handled by the application itself, Proactor is the kernel asynchronous IO to complete the read and write operations, and then notify

In theory, Proactor is more efficient than Reactor, but Proactor has the following disadvantages:

  1. 内存使用
    The buffer must continue to occupy memory during the time period of read and write operations, and each concurrent operation requires an independent buffer. Compared with the Reactor mode, which does not require a cache before reading and writing is ready, Proactor proposes more for memory Requirements
  2. 操作系统支持
    Asynchronous I/O relies on operating system support. In Windows, real asynchronous I/O is realized through IOCP, while in Linux system, asynchronous I/O is not yet perfect.

Guess you like

Origin blog.csdn.net/weixin_45505313/article/details/106813785