The following images are also recommended for Scalable translation by Doug Lea and are not very friendly to those who don’t know English. However, some of the translations are not as good as others. Scalable IO in Java, translated by Doug Lea

series

  • Java NIO three roles Channel, Buffer, Selector
  • Scalable IO in Java, translated by Doug Lea
  • Netty server create source code process analysis
  • What exactly is an EventLoopGroup?
  • To be continued..

Creation is not easy, if it is helpful to you, please pay attention to it. If you have any questions, you can exchange private letters. May you thrive in the Year of the Tiger.

Single-reactor single-thread model

The Reactor model, at its most basic, is that all I/O operations are done on the same NIO thread. The following is the thread model:



The Reactor model uses asynchronous non-blocking I/O. All I/O operations do not cause blocking. Theoretically, a county can handle all I/O operations independently, for example: The TCP connection request message is received from the Acceptor client. After the connection is established, the corresponding ByteBuffer is sent to the specified Handler through Dispatch for decoding the message. User thread messages are encoded and sent to the client through the NIO thread.

Application scenario: Applies to scenarios where service processing components in the processor chain can be quickly completed. Disadvantages: Does not apply to scenarios with heavy load and large concurrency. The reasons are as follows:

  • A NIO thread thread handling hundreds or thousands of connections at the same time is not performance sustainable
  • When the NIO thread becomes overloaded, the processing speed slows down, resulting in numerous client connection timeouts, message crushing, and processing timeouts, ultimately a performance bottleneck for the system
  • Reliability issues, if NIO threads unexpectedly die, can cause the entire system communication module to become unusable, unable to receive and process messages

Based on the above problems, a single Reactor multithreading model is developed.


Single-reactor multithreaded model



Features:

  • There is a special NIO thread, the Acceptor thread, which listens to the server and receives TCP connection requests from the client
  • Network I/O operations — read, write, and so on are handled by a NIO thread pool, which can be implemented using JDK thread pools and contains a task queue and N available threads that are responsible for reading, decoding, encoding, and sending responses to messages
  • A NIO thread can process N links at the same time, but only one NIO thread corresponds to each connection to prevent concurrent operation problems

This single Reactor model of processing threads in most scenarios are available to meet performance requirements, but in some special scenarios, a NIO thread is responsible for monitoring and dealing with all the client connection may performance problems, such as the server to the client for safety certification, the certification process and very loss performance, This will lead to the single point performance problem, so the multi-reactor multi-processing thread model is evolved.


Multireactor multithreaded model

This model is also known asReactor multithreading model.

The second model puts event processing into a thread pool for multi-threading. The third model divides the Reactor’s responsibilities. Instead of a single NIO thread, the Reactor receives client connections from an independent NIO thread pool, which is divided into two parts:

  • MainReactor: Monitors the ServerSocketChannel, processes the establishment of new connections to the client, and registers the established SocketChannel to the subReactor
  • SubReactor: Maintain its own Selector, SocketChannel based on the mainRactor established client to multiplex IO read and write events, read and write network data, and throw the business processing function to the worker thread pool to complete

Netty’s Reactor model

The client

EventLoopGroup group = new NioEventLoopGroup();
// Bootstrap is used here and ServerBootstrap is used on the server
Bootstrap b = new Bootstrap();
Copy the code
  • For Netty NIO clients, only one EventLoopGroup is created
  • An EventLoop can correspond to a Reactor, and an EventLoopGroup can be interpreted as a group of reactors
  • A Bootstrap startup can only initiate a connection to one remote address, so only one NIO Selector is used, that is, only one Reactor, even though we are declaring an EventLoopGroup, The EventLoopGroup also allocates only one EventLoop for PROCESSING IO events
  • If a business thread uses a Netty NIO client, consider it a single-reactor single-thread model
  • If there are multiple business threads using the Netty NIO client, the task can be “Single-reactor Multithreaded Model”
  • At the same time we can create multiple Netty NIO clients to implement the multi-reactor multi-thread model



The service side

Create two EventLoopGroup objects
EventLoopGroup bossGroup = new NioEventLoopGroup(1); // Create a boss thread group for the server to accept client connections
EventLoopGroup workerGroup = new NioEventLoopGroup(); // Create worker thread groups for SocketChannel data reads and writes
// Create ServerBootstrap object
ServerBootstrap b = new ServerBootstrap();
// Set the EventLoopGroup to use
b.group(bossGroup, workerGroup);
Copy the code
  • For the server, create two EventLoopGroups
    • BossGroup corresponds to the mainReactor, which is used by the server to accept the connection from the client. At the same time, the number of threads is passed as 1, indicating that only one Reactor is used. Yes, but it doesn’t make sense. A server can bind only one port, which means that only one Selector can be used to handle client connection events. Even if multiple threads are configured, only one is actually used
    • WorkerGroup corresponds to a subReactor, which is used to read and write data for SocketChannel
  • If the number of threads is not set for EventLoopGroup, the default value is number of CPU cores x 2.

Hi everyone, I am Yigui. Thank you for your thumbs up, collection and comments. I am constantly updating the article and I will see you next time.

You can also add my personal VX for communication: LHJ502819, to make efforts to impact the big factory together. In addition, there are a lot of learning and interview materials for you.