1 Netty Introduce

Native NIO The problem is :

1. NIO Class library and API multifarious , Use of trouble : Need to master Selector、ServerSocketChannel、SocketChannel、ByteBuffer etc. .

2. Other additional skills are needed : Be familiar with Java Multithreaded programming , because NIO Programming involves Reactor Pattern , You have to be very familiar with multithreading and network programming , To write high quality NIO Program .

3. The development workload and difficulty are very large : For example, the client is facing disconnection and reconnection 、 Network flash off 、 Half a pack of reading and writing 、 Failed cache 、 Network congestion and abnormal flow processing and so on .

4. JDK NIO Of Bug: notorious Epoll Bug, It leads to Selector Empty polling , Eventually lead to CPU 100%. until JDK 1.7 Version this problem still exists , Not fundamentally solved

stay NIO Pass through Selector Whether there is currently IO event , according to JDK NIO api describe ,Selector Of select Methods will always block , until IO Event reached or timed out , But in Linux Sometimes there are problems on the platform , In some cases select Method will return directly , Even if there is no timeout and there is no IO Event arrival , That's the famous one epoll bug, This is a serious problem bug, It causes the thread to fall into a dead loop , Will make CPU Up to 100%, Greatly affect the reliability of the system , up to now ,JDK They haven't completely solved this problem .

2 summary

Netty By JBOSS One of the Java Open source framework .Netty Provide asynchronous 、 Based on event driven network application framework , For rapid development of high performance 、 A highly reliable network IO Program . Netty It's based on NIO Network programming framework of , Use Netty Can help you quickly 、 Simply develop a A network application , It is equivalent to simplification and process NIO Development process of . As the most popular NIO frame ,Netty In the field of Internet 、 Big data distributed computing 、 Game industry 、 Communication industry has been widely used , Well-known Elasticsearch 、Dubbo Inside the frame, we use Netty.

image.png


You can see from the picture that Netty The power of : Zero copy 、 Extensible event model ; Support TCP、UDP、HTTP、WebSocket Such agreement ; Provide secure transmission 、 Compress 、 Big file transfer 、 Codec support and so on .

It has the following advantages :

1. Elegant design , Provide blocking and non blocking Socket; Provide flexible and extensible event model ; Provides a highly customizable thread model .

2. Higher performance and throughput , Use zero copy technology to minimize unnecessary memory replication , Reduce the consumption of resources .

3. Provide secure transmission features .

4. Support a variety of mainstream protocols ; Preset a variety of encoding and decoding functions , Support users to develop private protocols .


3  Threading model

3.1 Basic introduction to thread model :

Different threading patterns , It has a great impact on the performance of the program , I'm learning Netty Before thread mode , First of all, let's talk about Each thread mode , Finally, take a look at Netty What are the advantages of the threading model . The existing thread models are :

        1 The traditional block I/O Service model

        2Reactor Model

        according to Reactor The number of threads processing the resource pool is different , Yes 3 A typical implementation

             1 single Reactor Single thread

             2 single Reactor Multithreading

             3 Master-slave Reactor Multithreading

3.2 The traditional block I/O Service model :

Use blocking IO Mode to get the input data , Each connection needs an independent thread to complete the data input , Business processing and data return work .

image.png

Existing problems :

1. When the concurrency is large , It creates a lot of threads , It takes up a lot of system resources

2. After the connection is created , If the current thread has no data to read temporarily , The thread will block in read operation , This causes a waste of thread resources


3.3 Reactor Model

Reactor Pattern , The pattern of passing one or more inputs to the service processor at the same time , The server-side program processes multiple incoming requests , And synchronously dispatch them to the corresponding processing thread , therefore Reactor The pattern is also called Dispatcher Pattern . Reactor Mode use IO Reuse listening Events , After receiving the incident , Distribute to a thread ( process ), This is the key to high concurrency of network servers .

   1. single Reactor Single thread

  image.png

                Selector It is possible for an application to listen for multiple connection requests through a blocking object

                Reactor Object passing Selector Monitoring client request events , Pass after receiving the event Dispatch To distribute is to establish a connection request event , By Acceptor adopt Accept Processing connection requests , And then create a Handler Object processing subsequent business processing after the connection is completed

                Handler Will complete Read→ Business processing →Send The complete business process of

advantage :

advantage : Simple model , No multithreading 、 Process of communication 、 Competition issues , All in one thread

shortcoming :

1. Performance issues : Only one thread , Unable to fully play multi-core CPU Performance of .Handler When processing business on a connection , The entire process cannot handle other connection events , Easy to cause performance bottlenecks

2. Reliability issues : Thread terminated unexpectedly or entered a dead loop , It will make the whole system communication module unavailable , Cannot receive and process external messages , Cause node failure

   

     2. single Reactor Multithreading

image.png


                    Reactor Object passing selector Monitoring client request events , After receiving the incident , adopt dispatch distributed

                     If a connection request is established , Then right Acceptor adopt accept Processing connection requests

                     If it's not a connection request , By reactor The distribution call connection corresponds to handler To deal with it

                    handler Respond to events only , Do not do specific business processing , adopt read After reading data , It will be distributed to the following worker A thread in the thread pool handles business

                    worker The thread pool allocates independent threads to complete the real business , And return the result to handler

                    handler After receiving the response , adopt send Return the result to client

                     advantage :

                     Can make full use of multi-core cpu Handling capacity of

                     shortcoming :

                     Multithreaded data sharing and access are complex , reactor Handle all event monitoring and response , Running on a single thread , stay

                     High concurrency scenarios are prone to performance bottlenecks


3. Master-slave Reactor Multithreading

image.png


image.png

   

Reactor The main thread MainReactor Object passing select Listen for client connection Events , After receiving the incident , adopt Acceptor Handle client connection Events

When Acceptor After handling the client connection event ( Set up with the client Socket Connect ),MainReactor Assign connections to SubReactor.( namely :MainReactor Only responsible for listening to client connection requests , After establishing a connection with the client, give the connection to SubReactor Monitor the back IO event .)

SubReactor Join the connection to your own connection queue to listen , And create Handler Deal with all kinds of events

When something new happens on the connection ,SubReactor Will call the corresponding Handler Handle

Handler adopt read Read request data from connection , Distribute request data to Worker Thread pool for business processing

Worker The thread pool allocates independent threads to complete real business processing , And return the processing result to Handler.Handler adopt send Send a response to the client

One MainReactor Can correspond to multiple SubReactor, That is, a MainReactor Threads can correspond to multiple SubReactor Threads

advantage :

1. MainReactor Threads and SubReactor The data interaction of thread is simple, and the responsibility is clear ,MainReactor The thread just needs to receive new connections ,SubReactor The thread completes the subsequent business processing

2. MainReactor Threads and SubReactor The data interaction of thread is simple , MainReactor The thread just needs to pass the new connection to SubReactor Threads ,SubReactor Threads do not need to return data

3. Multiple SubReactor Threads can handle higher concurrent requests

shortcoming :

The drawback of this model is the high programming complexity . But because of its obvious advantages , Widely used in many projects , Include Nginx、Memcached、Netty etc. . This pattern is also called server's 1+M+N Thread mode , That is, the server developed with this pattern contains a ( Or more ,1 It just means relatively less ) Connection setup thread +M individual IO Threads +N A business processing thread . This is a mature server programming pattern in the industry .


3.4 Netty Threading model

Netty The design of is mainly based on the master-slave Reactor Multithreading mode , Some improvements have been made .

1. Simple version Netty Model

image.png


BossGroup Thread maintenance Selector,ServerSocketChannel Sign up for this Selector On , Focus only on connection establishment request events ( Lord Reactor)

When receiving the connection establishment request event from the client , adopt ServerSocketChannel.accept Method to get the corresponding SocketChannel, And encapsulate it into NioSocketChannel Sign up to WorkerGroup In thread

Selector, Every Selector Running in a thread ( from Reactor) When WorkerGroup In thread Selector Listen to what you're interested in IO After the event , Just call Handler To deal with

2. premium Netty Model

image.png

     There are two sets of thread pools :BossGroup and WorkerGroup,BossGroup The thread in is responsible for establishing a connection with the client ,WorkerGroup Threads in are dedicated to handling read and write on the connection

    BossGroup and WorkerGroup There are multiple threads that execute event processing continuously , Each thread contains a Selector, Used to listen for Channel

     Every BossGroup The thread loop in performs the following three steps

             Rotation registered on it ServerSocketChannel Of accept event (OP_ACCEPT event )

             Handle accept event , Establish a connection with the client , Generate a NioSocketChannel, And register it in WorkerGroup On a thread in Selector On

             Then go to this loop to process the next event in the task queue

             Every WorkerGroup The thread loop in performs the following three steps

                     Rotation registered on it NioSocketChannel Of read/write event (OP_READ/OP_WRITE things Pieces of )

                     In the corresponding NioSocketChannel Upper processing read/write event

                     Then go to this loop to process the next event in the task queue

3. A detailed version Netty Model


image.png

Netty Abstract out two groups of thread pools :BossGroup and WorkerGroup, It can also be called BossNioEventLoopGroup and WorkerNioEventLoopGroup. Every thread pool has NioEventLoop Threads .BossGroup The thread in is responsible for establishing a connection with the client ,WorkerGroup Threads in are dedicated to handling read and write on the connection .BossGroup and WorkerGroup The types of NioEventLoopGroup

NioEventLoopGroup It's equivalent to an event loop group , There are multiple event loops in this group , Every event cycle is

It's a NioEventLoop

NioEventLoop Represents a thread that performs event processing in a continuous loop , Every NioEventLoop All contain a Selector, Used to listen for Socket network connections (Channel)

NioEventLoopGroup It can have multiple threads , It can contain more than one NioEventLoop

Every BossNioEventLoop The loop performs the following three steps

           select: Rotation registered on it ServerSocketChannel Of accept event (OP_ACCEPT event )

            processSelectedKeys: Handle accept event , Establish a connection with the client , Generate a NioSocketChannel, And register it with a WorkerNioEventLoop Upper Selector On

            runAllTasks: And then go to this loop to process other tasks in the task queue

Every WorkerNioEventLoop The loop performs the following three steps

            select: Rotation registered on it NioSocketChannel Of read/write event (OP_READ/OP_WRITE event )

            processSelectedKeys: In the corresponding NioSocketChannel Upper processing read/write event

            runAllTasks: And then go to this loop to process other tasks in the task queue

In the above two processSelectedKeys In the step , Will use Pipeline( The Conduit ),Pipeline Is cited in Channel, That is, through Pipeline You can get the corresponding Channel,Pipeline A lot of processors are maintained in ( Blocking processor 、 Filter processor 、 Custom processors, etc ).

Finished writing , This chapter is also something I like very much ... Welcome to study and discuss ... ha-ha , As a programmer , Input is the greatest sense of accomplishment .