In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "what is the process of io request processing". Interested friends may wish to take a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the io request processing process"?
1. Eventloop main cycle
As mentioned in the previous article, after netty starts, there will be many eventloop threads working in a loop all the time (server common features), such as select or task. Let's review the implementation of NioEventLoop again.
Let's first take a look at NioEventLoop's class diagram:
It looks very complicated, regardless of it. Its core method is naturally run ()
/ / io.netty.channel.nio.NioEventLoop#run @ Overrideprotected void run () {/ / an endless loop detection task, which is for (;;) {try {switch (selectStrategy.calculateStrategy (selectNowSupplier, hasTasks () {case SelectStrategy.CONTINUE:continue / / execute the task when there is a task, otherwise block and wait for network events, or be awakened by case SelectStrategy.SELECT:// select.select () with a timeout limit select (wakenUp.getAndSet (false)); / / 'wakenUp.compareAndSet (false, true)' is always evaluated// before calling 'selector.wakeup ()' to reduce the wake-up// overhead. (Selector.wakeup () is an expensive operation.) / However, there is a race condition in this approach.// The race condition is triggered when 'wakenUp' is set to// true too early.////' wakenUp' is set to true too early if:// 1) Selector is waken up between 'wakenUp.set (false)' and// 'selector.select (...)'. (BAD) / / 2) Selector is waken up between 'selector.select (...)' And//'if (wakenUp.get ()) {...}'. (OK) / In the first case, 'wakenUp' is set to true and the// following' selector.select (...)' Will wake up immediately.// Until 'wakenUp' is set to false again in the next round,//' wakenUp.compareAndSet (false, true) 'will fail, and therefore// any attempt to wake up the Selector will fail, too, causing// the following' selector.select (...)' Call to block// unnecessarily.//// To fix this problem, we wakeup the selector again if wakenUp// is true immediately after selector.select (...). / / It is inefficient in that it wakes up the selector for both// the first case (BAD-wake-up required) and the second case// (OK-no wake-up required). If (wakenUp.get ()) {selector.wakeup () } / / fall throughdefault:} cancelledKeys = 0; needsToSelectAgain = false;// ioRatio is the percentage of io operations. Compared with running tasks, the default is 50:50final int ioRatio = this.ioRatio;if (ioRatio = = 100) {try {/ / step1. Run the io operation processSelectedKeys ();} finally {/ / Ensure we always run tasks.// step2. Run the task task runAllTasks ();}} else {final long ioStartTime = System.nanoTime (); try {processSelectedKeys ();} finally {/ / Ensure we always run tasks.final long ioTime = System.nanoTime ()-ioStartTime;// maximum runAllTasks (ioTime * (100-ioRatio) / ioRatio) } catch (Throwable t) {handleLoopException (t);} / / Always handle shutdown even if the loop processing threw an exception.try {if (isShuttingDown ()) {closeAll (); if (confirmShutdown ()) {return } catch (Throwable t) {handleLoopException (t);} / / select, according to the event loop private void select (boolean oldWakenUp) throws IOException {Selector selector = this.selector;try {int selectCnt = 0 int selectCnt = System.nanoTime () / / with timeout limit, the default maximum timeout is 1s, but when there is a delayed task processing, it is used as the standard 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.