"Turn" netty that thing. (iv) Netty and reactor mode

Source: Internet
Author: User

"Original" https://github.com/code4craft/netty-learning/blob/master/posts/ch4-reactor.md

One: Netty, NIO, multithreading?

After a long time finally updated! Before has been slow to move also because accumulation is not enough, the back is more difficult to start. During the Chinese New Year @ Li Linfeng HW released a Netty5.0 architecture analysis and source interpretation http://vdisk.weibo.com/s/C9LV9iVqH13rW/1391437855, read is also a lot of harvest. In the previous article we analyzed the structure of the Netty, this time we will analyze the most intricate part of the-netty in the multithreading and the application of NIO.

Before we can clarify the relationship between NiO and Netty, we must first look at the reactor model. Netty is a typical use of multi-threaded reactor mode, understand this part, in the macroscopic understanding of Netty NiO and multi-threaded parts will not have any difficulty.

This article is still for Netty 3.7, but because also saw a little Netty 5 of the source code, so there will be a little introduction.

Two: Reactor, reactor or nuclear power plant? 1, the Origin of reactor

Reactor is a design pattern that is widely used in server-side development. Reactor Chinese mostly translated as "reactor", when I first approached the concept, it felt very strong, is it the principle of "nuclear reaction" similar? Later only to know that in fact there is no relationship, from reactor brothers "Proactor" (multi-translation for the front) can be seen, the two words of Chinese translation is not too good, not enough image. In fact, the reactor pattern has an alias of "Dispatcher" or "Notifier", and I think both are more indicative of its nature.

So what exactly is the reactor model? This starts with an event-driven approach to development. As we know, one of the main rules of the application server is that the CPU is processed much faster than the IO speed, and if the CPU blocks for IO operations (such as reading a piece of data from a socket), it is obviously not worth it. The better way is divided into multiple processes or threads to deal with, but this will bring some process switching overhead, imagine a process a data read 500ms, during the process to switch to it 3 times, but the CPU is nothing to dry, so switch away, is not a cost-effective?

The pioneers then found the event-driven, or called a callback, to do the job. In this way, the application business registers a callback (event handler) with an intermediary and, when IO is ready, generates an event for the middleman and notifies the handler to handle it. This callback is also reflected in the "Hollywood Principles" (Hollywood Principle)-"Don t call us, we'll call you", also useful in our familiar IOC. It seems that software development is really interoperability!

OK, let's look at reactor mode now. In the previous event-driven example, there is a question: how do we know IO is ready for this event, who will act as the middleman? The answer to the reactor pattern is that it is done by a separate process (thread) that is constantly waiting and looping, it accepts all handler registrations, and is responsible for operating the OS query Io is ready, calling the specified handler for processing after it is ready, and the role's name is called reactor.

2. Reactor and NIO

The NIO in Java can be well combined with the reactor pattern. About the reactor model in NIO, I think there's no more information than Doug Lea's God (don't know Doug Lea?). Look at the JDK bundle and the author of the contract, which is more concise and comprehensive in the scalable IO in Java. The core of reactor in NiO is Selector that I've written a simple reactor example where I'm pasting a core reactor loop (which is called the Loop structure EventLoop ), and the remaining code is in the LEARNING-SRC directory.

    PublicvoidRun(){Try{While(!Thread.Interrupted()){Selector.Select();SetSelected=Selector.Selectedkeys();Iteratorit = selected. Iterator (); while  (it. Hasnext ()) dispatch ((selectionkey   (it. Nextselected. (); } } catch  (ioexception ex {/* ... */ }< /span>                 
3. Other concepts related to reactor

The Proactor model mentioned earlier, what is this? In short, reactor mode, the operating system is only responsible for notifying the IO is ready, the specific IO operation (such as read and write) is still to be blocked in the business process to do, while the Proactor mode is further, by the operating system to perform IO operations (such as reading, Will read the data directly into memory buffer, and handler is only responsible for handling its own logic, really do the IO and program processing asynchronous execution. So we generally say that reactor is synchronous io,proactor is asynchronous IO.

For blocking and non-blocking, asynchronous and non-asynchronous, and Unix-based mechanisms, you can look at this article IO-synchronous, asynchronous, blocking, non-blocking (over-the-back), and Tao Hui (author of Deep Understanding Nginx), "High-performance network Programming" series.

Three: From reactor to understand the Netty1, multi-threaded reactor

Speaking of a bunch of reactor, we went back to Netty. In "Scalable IO in Java", the reactor mode is described in a multi-threaded way. In this mode, Mainreactor has only one, is responsible for responding to the client's connection request, and establishes the connection, it uses one NiO selector;subreactor can have one or more, each subreactor will execute in a separate thread, and maintain an independent NIO Selector.

This benefit is obvious, because Subreactor also performs some more time-consuming IO operations, such as reading and writing messages, using multiple threads to execute, which is more conducive to CPU computing power, reduce IO latency.

2. Reactor and NiO in Netty

Well, understand the multi-threaded reactor mode, let's take a look at Netty bar (the following sections are mainly for the Nio,oio part more simple, do not repeat the introduction). The role of Mainreactor in Netty is called "Boss", and the role of subreactor corresponds to "Worker". Boss is responsible for assigning the request, the worker is responsible for execution, it seems very appropriate! In the case of the server side of TCP, the two corresponding implementation classes are the same as NioServerBoss NioWorker (there is no difference between the server and the client's worker, since the connection is made to be transmitted by peers).

Netty 3.7 reactor in the EventLoop AbstractNioSelector.run() , it implements the Runnable interface. This class is the core of the Netty NiO section. Its logic is very complex and includes some processing of JDK bugs (for example rebuildSelector ), and it doesn't need to go into detail when it starts reading. I've streamlined most of the code, keeping the trunk as follows:

AbstractClassAbstractnioselectorImplementsNioselector{NIO SelectorProtectedVolatileSelectorSelector;Internal task queuePrivateFinalQueue<Runnable>Taskqueue=NewConcurrentlinkedqueue<Runnable> ();Selector cyclePublicvoidRun(){For(;;){Try{Working with internal task queuesProcesstaskqueue();Handling Selector Event Correspondence logicProcess(Selector);}Catch(ThrowableT){Try{Thread.Sleep(1000);}Catch(InterruptedexceptionE){Ignore.}}}}PrivatevoidProcesstaskqueue(){For(;;){final runnable task = taskQueue< Span class= "O". poll (); if  (task == null ) {break} task. (); } } protected abstract void Span class= "NF" >process (selector selector) throws ioexception;        

Where process is the main logic for handling events, for example in AbstractNioWorker , the processing logic is as follows:

    ProtectedvoidProcess(SelectorSelector)ThrowsIOException{Set<Selectionkey>Selectedkeys=Selector.Selectedkeys();If(Selectedkeys.IsEmpty()){Return;}For(Iterator<Selectionkey>I=Selectedkeys.Iterator();I.Hasnext();){SelectionkeyK=I.Next();I.Remove();Try{IntReadyops=K.Readyops();If((Readyops&Selectionkey.Op_read)!=0||Readyops==0){If(!Read(K)){Connection already closed-no need to handle write.Continue;}}If((Readyops&Selectionkey.op_write)! = 0) { Writefromselectorloop(k); } } catch (cancelledkeyexception e) { close(k); } if (cleanupcancelledkeys()) { break; //Break the loop to avoid concurrentmodificationexception }        }}  

Is this the Selector classic usage mentioned in the second part?

After Netty 4.0, the author felt that NioSelector the term, as well as NioBoss the distinction and the practice is a NioWorker little more cumbersome, simply to merge these into NioEventLoop , since then the two roles do not differentiate. I think the new version will be a bit more elegant.

3, multi-threading in Netty

Let's look at the multithreaded part of Netty. Once the corresponding boss or worker is started, it is assigned to a thread to be executed. The corresponding concept is the BossPool and WorkerPool . For each NioServerSocketChannel , the boss's reactor has one thread, while the number of worker threads is determined by the worker thread pool size, but the default maximum does not exceed the CPU core number, of course, this parameter can be NioServerSocketChannelFactory set by the constructor parameters.

    Nioserversocketchannelfactory(            bossexecutorworkerexecutorworkercountthis(  Bossexecutor1workerexecutorworkercount}        

In the end, we are concerned with a question, ChannlePipeline which thread did our previous Channlehandler execute on? The answer is executed in the worker thread and blocks the worker's eventloop. For example, in NioWorker , after reading the message, the event is triggered MessageReceived , which causes the handler in the pipeline to be executed.

    Read(k(0firemessagereceived(channelbuffertrue}  

As you can see, for a business that handles events for a long time, it is not appropriate to put them directly into Channelhandler. So how do you deal with it? We will introduce them in the handler section.

Resources:

    • Scalable IO in Java http://gee.cs.oswego.edu/dl/cpjslides/nio.pdf
    • Netty5.0 structure Analysis and source code interpretation http://vdisk.weibo.com/s/C9LV9iVqH13rW/1391437855
    • Reactor pattern Http://en.wikipedia.org/wiki/Reactor_pattern
    • Reactor-an Object Behavioral Pattern for demultiplexing and dispatching Handles for synchronous eventshttp://www.cs.wust L.edu/~schmidt/pdf/reactor-siemens.pdf
    • High Performance network programming 6--reactor reactor and timer management http://blog.csdn.net/russell_tao/article/details/17452997
    • IO-synchronous, asynchronous, blocking, non-blocking (http://blog.csdn.net/historyasamirror/article/details/5778378)

Map from: http://www.worldindustrialreporter.com/france-gives-green-light-to-tokamak-fusion-reactor/

"Turn" netty that thing. (iv) Netty and reactor mode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.