In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the "distributed Netty source EventLoopGroup analysis" related knowledge, the editor through the actual case to show you the operation process, the method of operation is simple and fast, practical, I hope that this "distributed Netty source EventLoopGroup analysis" article can help you solve the problem.
EventLoopGroup introduction
As mentioned in the previous article, EventLoopGroup is mainly responsible for two things, which I repeat here:
It mainly includes two functions, registering Channel and performing some Runnable tasks.
Function 1: let's first take a look at the registration Channel
Register Channel with Selector, and Selector dispatches events related to Channel, such as read, write, Accept, and so on.
The design of EventLoopGroup is that it contains multiple EventLoop (each EventLoop usually contains a thread). In the process of performing the above registration, you need to select one of the EventLoop to perform the above registration behavior. Here, there is a problem of choosing a policy. The choice policy interface is EventExecutorChooser, and you can also customize an implementation.
As can be seen from the above, most of the work done by EventLoopGroup is some overall work, such as initializing the above-mentioned multiple EventLoop, EventExecutorChooser, etc., and the specific registration of Channel is to be realized by its internal EventLoop.
Function 2: perform some Runnable tasks
EventLoopGroup inherits EventExecutorGroup,EventExecutorGroup is also a collection of EventExecutor, EventExecutorGroup is also in charge of the initialization of EventExecutor, EventExecutorGroup for the execution of Runnable tasks is to choose an internal EventExecutor to do specific implementation work.
Many tasks in netty are executed asynchronously. Once the current thread wants to perform related operations on an EventLoop, such as registering Channel to an EventLoop, if the current thread and the internal thread of the EventLoop you want to operate are not the same, then the current thread will only submit a registration task to the EventLoop and return a ChannelFuture to the outside.
Summary: EventLoopGroup contains the above two functions, it is more of a collection, but the specific functional implementation is to select an internal item element to perform related tasks. The internal item elements here usually implement both EventLoop and EventExecutor, such as NioEventLoop, etc.
Let's take a look at the overall class diagram of EventLoopGroup.
You can see from the figure that there are 2 branches:
1 MultithreadEventLoopGroup: used to encapsulate the initialization logic of multi-threads, specify the number of threads, etc., that is, initialize the corresponding number of EventLoop, each EventLoop is assigned to a thread
For the newChild method in the figure above, NioEventLoopGroup uses NioEventLoop as its implementation, and EpollEventLoopGroup uses EpollEventLoop as its implementation.
Such as the implementation of NioEventLoopGroup:
Protected EventLoop newChild (Executor executor, Object... Args) throws Exception {return new NioEventLoop (this, executor, (SelectorProvider) args [0], (SelectStrategyFactory) args [1]) .newSelectStrategy (), (RejectedExecutionHandler) args [2]);}
2 the EventLoop interface implements the EventLoopGroup interface, mainly because the functional interface in EventLoopGroup still depends on the internal EventLoop to complete the specific operation.
EventLoop introduction
The main job of EventLoop is to register Channel and is responsible for monitoring and managing events such as reading and writing of Channel, which involves different monitoring methods. There are three ways to monitor events under linux.
Select 、 poll 、 epoll
Currently, the Selector API of java is implemented as follows:
PollSelectorImpl: poll mode is implemented
EPollSelectorImpl: epoll mode is implemented
Netty uses the following:
NioEventLoop: uses the jdk Selector interface (using PollSelectorImpl's poll method) to implement event detection for Channel
EpollEventLoop: instead of using the interface of jdk Selector to implement EPollSelectorImpl, Netty implements its own epoll way to achieve event detection of Channel, so there is no Selector of jdk in EpollEventLoop.
NioEventLoop introduction
For the functions of NioEventLoopGroup, NioEventLoop should do the actual implementation. NioEventLoop should not only realize the registration function, but also run the Runnable task.
For registering Channel:NioEventLoop, register Channel to PollSelectorImpl inside NioEventLoop to listen for read and write events of that Channel
For running Runnable tasks: SingleThreadEventExecutor, the parent class of NioEventLoop's parent class, implements the running Runnable task. In SingleThreadEventExecutor, there is a task queue and an assigned thread.
Private final Queue taskQueue;private volatile Thread thread
In this thread, NioEventLoop not only executes the IO events brought by Selector, but also constantly fetches tasks from the above taskQueue to execute these non-IO events. Let's take a closer look at this process.
Protected void run () {for (;;) {try {switch (selectStrategy.calculateStrategy (selectNowSupplier, hasTasks () {case SelectStrategy.CONTINUE: continue; case SelectStrategy.SELECT: select (wakenUp.getAndSet (false)) If (wakenUp.get ()) {selector.wakeup ();} default: / / fallthrough} cancelledKeys = 0; needsToSelectAgain = false; final int ioRatio = this.ioRatio If (ioRatio = = 100) {processSelectedKeys (); runAllTasks ();} else {final long ioStartTime = System.nanoTime (); processSelectedKeys (); final long ioTime = System.nanoTime ()-ioStartTime; runAllTasks (ioTime * (100-ioRatio) / ioRatio) } if (isShuttingDown ()) {closeAll (); if (confirmShutdown ()) {break;} catch (Throwable t) {...}
Let's talk about this process in detail:
1 calculate whether the select process is currently required
If there is no Runnable task currently, execute select (this select process will be described in more detail later).
If there is a Runnable task at present, you have to execute the processing flow. By the way, execute selector.selectNow () at this time. If there is an event, you will make a profit, and you will not go through this processing process for nothing.
2 execute IO tasks and non-IO tasks according to the time percentage setting of IO tasks, that is, Runnable tasks mentioned above
If ioRatio=100, all IO tasks are executed each time, and all non-IO tasks are executed by default ioRatio=50, that is, half the time is spent on IO tasks and the other half of the time is spent on non-IO tasks. How do you control the time taken by non-IO tasks?
Here, for every 64 non-IO tasks executed (here it may be that each non-IO task is relatively short, reducing the consumption of judgment) to determine whether the occupancy time exceeds the above time limit.
Next, let's take a detailed look at the above select process.
Selector selector = this.selector;try {int selectCnt = 0; long currentTimeNanos = System.nanoTime (); long selectDeadLineNanos = currentTimeNanos + delayNanos (currentTimeNanos); for (;;) {long timeoutMillis = (selectDeadLineNanos-currentTimeNanos + 500000L) / 1000000L; if (timeoutMillis
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.