Interviewer: Netty's threading model is not as simple as master-slave multiple Reactors

In my opinion, the core of Netty mainly includes the following three parts: insert image description hereThe main responsibilities of each core module are as follows:

  • Memory management mainly improves efficient memory management, including memory allocation and memory recycling.

  • The Netcom channel replicates network communication, such as implementing the encapsulation of underlying JAVA APIs such as NIO and OIO, and simplifies the network programming model.

  • threading model

    Provides an efficient thread collaboration model.

You may wish to recall that in the past interview process, the interviewer usually asked: What is Netty's threading model?

The master-slave multi-Reactor model , I believe everyone can blurt it out, and then what? There's no after that?

What kind of problems does the thread model mainly solve in network communication? How is it solved in Netty, and why is Netty's threading model so efficient? Please allow me to speak slowly.

> Reminder: In order to ensure the rigor of the opinions of the article, the research area is locked in: Netty NIO related.

1. Master-slave multi-Reactor model

The master-slave multi-Reactor model is a very classic thread programming model in the industry. Its schematic diagram is as follows: insert image description hereLet us first briefly introduce several important roles involved in the above figure:

  • Acceptor

    The request receiver, in practice, has a responsibilities similar to that of the server, and is not really responsible for the establishment of the connection request, but only delegates its request to the Main Reactor thread pool for implementation, which plays a forwarding role.

  • Main Reactor The main Reactor thread group is mainly responsible for connecting events and forwarding IO read and write requests to the SubReactor thread pool . Of course, in some scenarios that require permission control on the client, the responsibility of permission verification can be placed in the Main Reactor thread pool, that is, the Main Reactor can also register the read and write events of the channel , and read the data packets related to the client permission verification. , execute permission verification, and then register 2 channels to the IO thread after the permission verification is passed.

  • Sub Reactor Main Reactor usually listens for client connections and forwards the read and write of the channel to a thread (load balancing) in the Sub Reactor thread pool, which is responsible for data read and write. The read (OP_READ) and write events (OP_WRITE) of the channel are usually registered in NIO.

In order to have a deeper understanding of the master-slave Reactor model, let's take a look at the key actions that network communication generally includes: insert image description hereThe usual steps of a network interaction are as follows:

  • The server starts and listens on a specific port, such as port 80 for a web application.
  • The client initiates a three-way handshake of TCP and establishes a connection with the server. Taking NIO as an example, a NioSocketChannel object will be created after the connection is successfully established.
  • The server reads data from the network card through NioSocketChannel .
  • The server decodes each request from the binary stream according to the communication protocol .
  • According to the request, perform the corresponding business operation , for example, the Dubbo server accepts a query for user information whose user ID is 1.
  • Returning the business execution result to the client usually involves protocol encoding, compression , and so on.

Problems that need to be solved in the thread model : how to use multi-threaded programming to improve performance in connection monitoring, network reading and writing, encoding, decoding, and business execution .

How does the master-slave multi-Reactor model solve the above problems?

  1. The connection establishment (OP_ACCEPT) is in charge of the Main Reactor thread pool. After the NioSocketChannel is created, it is forwarded to the SubReactor.

  2. The SubReactor thread pool is mainly responsible for reading and writing the network (reading the byte stream from the network, sending the byte stream to the network), that is, registering OP_READ, OP_WRITE, and a SubReactor thread will be bound to the same channel .

  3. Encoding, decoding, and business execution, the specific situation is analyzed in detail

    Usually encoding and decoding are executed in the IO thread, and the execution of business logic usually uses an additional thread pool , but it is not absolute. A good framework usually uses parameters for customized selection, such as ping and pong. Heartbeat packets are executed directly in the IO thread, without being forwarded to the business thread pool to avoid thread switching overhead.

> Tips: In network programming, the thread used for network reading and writing is usually called IO thread.

2. Netty's threading model

Netty's threading model is based on the master-slave multi-Reactor model. insert image description hereThe connection event (OP_ACCEPT) of the network in Netty is implemented by the Main Reactor thread group, that is, the Boss Group, usually only one thread needs to be set .

The read and write operations of the network are implemented by the Work Group (Sub Reactor) thread group. The number of threads is 2 * CPU Core by default. One Channel is bound to one of the Work threads, and multiple Channels can be bound to one Work thread .

Encoding, decoding and other operations in Netty will be encapsulated into an event handler (ChannelHandler). Are these Handlers executed in the IO thread pool?

By default, ChannelHandler is executed in the IO thread, so how to change the default behavior? The key code is as follows: insert image description here Key point : When adding the event handler to the event chain, you can specify which thread pool to execute in, and if you do not specify it, it will execute in the IO thread .

Interviewer: Usually, a thread pool is specially created for business operations. After the business processing is completed, how to write the response result to the network card through the IO thread? insert image description hereThe business thread calls the write method of the Channel object and does not immediately write to the network, but only puts the data into a queue to be written (buffer area), and then the IO thread will obtain it from the buffer area to be written after each event selection is performed. The writing task actually writes the data to the network. Before the data reaches the network card, it will go through a series of Channel Handlers (Netty event propagation mechanism) and finally write to the network card.

Finally, let's introduce the general workflow of IO threads in Netty. insert image description hereThe key points of IO thread processing:

  • Each IO thread executes the above operations serially, that is, all channels registered in a Selector (event selector), **only one channel's events are processed at the same time. **This is also the fundamental reason why NIO does not have an advantage when dealing with large file transfers.
  • After the IO thread has processed all ready events, it will also obtain tasks from the task queue (Task Queue). For example, the business thread mentioned above needs to write the return result to the network after executing the business . All network read and write in Netty The operation can only be really run in the IO thread , so the business thread needs to encapsulate the response result with writing into a Task and put it into the IO thread task queue.

3. Summary

Back to the topic, if we encounter the interviewer asking "What is Netty's threading model?" during the interview process, we should be able to deal with it calmly.

I think it can be expanded from the following aspects.

  1. Netty's threading model is based on a master-slave multi-Reactor model. Usually a thread is responsible for handling OP_ACCEPT events, and IO threads with twice the number of CPU cores handle read and write events.
  2. The IO operations of a channel are bound to an IO thread, and an IO thread can register multiple channels.
  3. A network communication usually includes network data reading and writing, encoding, decoding, and business processing. By default, operations such as encoding and decoding will run in the IO thread, but other thread pools can also be specified.
  4. Usually business processing will open the business thread pool separately, but it can be further refined. For example, the heartbeat packet can be processed directly in the IO thread, but needs to be forwarded to the business thread pool to avoid thread switching.
  5. Events for all channels in an IO thread are processed serially .

Well, this article is introduced here. Your likes and forwards are the biggest encouragement for me to continue to output high-quality articles.

Welcome to the public account "Middleware Interest Circle" to explore the source code together, exchange high concurrency and architecture experience, and reply to PDF to get a lot of learning materials.

{{o.name}}
{{m.name}}

Guess you like

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