前面的六篇博客,我们已经讲了服务端的整个启动的流程,并且基于源码的做了一些解释,这篇博客我们主要讲下pipeline
的生命周期的函数的调用过程。为此我们书写了以下的代码,首先看下我们服务端的启动代码,具体的代码如下:
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
public class NettyServer {
public static void main(String[] args) throws InterruptedException {
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 128)
.childOption(ChannelOption.SO_KEEPALIVE, true)
.handler(new TestServerInitializer())
.childHandler(new ChannelInitializer<NioSocketChannel>() {
@Override
protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
nioSocketChannel.pipeline().addLast(new StringDecoder(), new NettyServerHendler());
}
});
System.out.println(".........server init..........");
ChannelFuture future = serverBootstrap.bind(9090).sync();
System.out.println(".........server start..........");
future.channel().closeFuture().sync();
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
我们继续查看TestServerInitializer
类的代码,具体的代码如下:
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.ServerSocketChannel;
public class TestServerInitializer extends ChannelInitializer<ServerSocketChannel> {
@Override
protected void initChannel(ServerSocketChannel ch) throws Exception {
System.out.println(this.getClass() + " TestServerInitializer.initChannel");
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new FirstServerHandler());
pipeline.addLast(new TwoServerHandler());
}
@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
System.out.println(this.getClass() + " TestServerInitializer.handlerAdded");
}
@Override
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
System.out.println(this.getClass() + " TestServerInitializer.handlerRemoved");
}
@Override
public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
System.out.println(this.getClass() + " TestServerInitializer.channelUnregistered");
ctx.fireChannelUnregistered();
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println(this.getClass() + " TestServerInitializer.channelActive");
ctx.fireChannelActive();
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
System.out.println(this.getClass() + " TestServerInitializer.channelInactive");
ctx.fireChannelInactive();
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println(this.getClass() + " TestServerInitializer.channelRead");
ctx.fireChannelRead(msg);
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
System.out.println(this.getClass() + " TestServerInitializer.channelReadComplete");
ctx.fireChannelReadComplete();
}
@Override
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
System.out.println(this.getClass() + " TestServerInitializer.channelWritabilityChanged");
ctx.fireChannelWritabilityChanged();
}
}
上面的代码,我通过一个特殊的类ChannelInitializer
添加了两个pipeline
,分别是FirstServerHandler
和TwoServerHandler
,我们再来查看一下这两个类的代码,具体的代码如下:
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
public class FirstServerHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
System.out.println(this.getClass()+" FirstServerHandler.channelWritabilityChanged");
ctx.fireChannelWritabilityChanged();
}
@Override
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
System.out.println(this.getClass() + " FirstServerHandler.channelRegistered");
ctx.fireChannelRegistered();
}
@Override
public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
System.out.println(this.getClass() + " FirstServerHandler.channelUnregistered");
ctx.fireChannelUnregistered();
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println(this.getClass() + " FirstServerHandler.channelActive");
ctx.fireChannelActive();
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
System.out.println(this.getClass() + " FirstServerHandler.channelInactive");
ctx.fireChannelInactive();
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println(this.getClass() + " FirstServerHandler.channelRead");
ctx.fireChannelRead(msg);
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
System.out.println(this.getClass() + " FirstServerHandler.channelReadComplete");
ctx.fireChannelReadComplete();
}
@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
System.out.println(this.getClass() + " FirstServerHandler.handlerAdded");
}
@Override
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
System.out.println(this.getClass() + " FirstServerHandler.handlerRemoved");
}
}
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
public class TwoServerHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
System.out.println(this.getClass() + " TwoServerHandler.channelRegistered");
ctx.fireChannelRegistered();
}
@Override
public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
System.out.println(this.getClass() + " TwoServerHandler.channelUnregistered");
ctx.fireChannelUnregistered();
}
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println(this.getClass() + " TwoServerHandler.channelActive");
ctx.fireChannelActive();
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
System.out.println(this.getClass() + " TwoServerHandler.channelInactive");
ctx.fireChannelInactive();
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println(this.getClass() + " TwoServerHandler.channelRead");
ctx.fireChannelRead(msg);
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
System.out.println(this.getClass() + " TwoServerHandler.channelReadComplete");
ctx.fireChannelReadComplete();
}
@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
System.out.println(this.getClass() + " TwoServerHandler.handlerAdded");
}
@Override
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
System.out.println(this.getClass() + " TwoServerHandler.handlerRemoved");
}
@Override
public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {
System.out.println(this.getClass()+" TwoServerHandler.channelWritabilityChanged");
ctx.fireChannelWritabilityChanged();
}
}
由于前面的源码的阅读我们知道现在的pipeline
如下图所示:
同时我也为了对应的HeadContext
和ServerBootstrapAcceptor
和TailContext
添加了对应的打印的方法,然后运行的我们的NettyServer
类,运行的结果如下所示:
我们发现没有调用到HeadContext
和TailContext
的handlerAdded
的方法,这是因为在创建pipeline
的时候,是直接将这两个类给创建出来,然后赋值的。然后我们会发现先调用的是继承ChannelInitializer
类的子类的handlerAdded
方法,然后调用的HeadContext
的channelRegistered
方法,然后调用的是继承ChannelInitializer
类的子类的initChannel
方法,也是这个时候的我们自定义的pipeline
添加到pipeline
队列中去,最后分别执行要添加到pipeline
的handlerAdded
的方法,这个时候继承ChannelInitializer
类的子类的使命已经完成了,就会对应的handlerRemoved
方法将这个特殊的pipeline
移除,这个时候又会执行一遍HeadContext
的channelRegistered
方法,然后分别执行剩下的pipeline
的channelRegistered
方法,执行完所有pipeline
的channelRegistered
的方法后,表示服务端已经启动完成了。这个时候会依次调用我们pipeline
的channelActive
的方法。这个时候大家肯定有个疑问,为什么我们的ServerBootstrapAcceptor
中的方法没有调用呢?打开发现添加了一个@skin
的注解,这个注解就是跳过的意思。我们可以尝试一下,我们在FirstServerHandler
的channelActive
上加个@skin
注解试试,注意这儿的注解是不可被外部访问,我改了源码将它权限改成了public,然后查看运行结果如下:
这个时候你会发现FirstServerHandler
的channelActive
就会直接跳过,直接不调用了。还有一些生命周期的方法,我们没有讲到,后面等我们读到对应的代码后再具体介绍。接下来我会将Netty
是如何来规避Nio
空轮训的Bug
。Selector
执行select
方法的时候可能会发生空轮训的Bug,在Netty
中是如何规避的,废话不多说,直接上代码。
public final class NioEventLoop extends SingleThreadEventLoop {
//事件循环
@Override
protected void run() {
for (;;) {
try {
try {
//hasTasks() 若taskQueue or tailTasks任务队列中有任务 返回true 没有则返回false
//有任务返回selectNow的返回值 没任务返回-1
switch (selectStrategy.calculateStrategy(selectNowSupplier, hasTasks())) {
case SelectStrategy.CONTINUE:
continue;
case SelectStrategy.BUSY_WAIT:
// fall-through to SELECT since the busy-wait is not supported with NIO
case SelectStrategy.SELECT:
//首先轮询注册到reactor线程对应的selector上的所有的channel的IO事件
//wakenUp 表示是否应该唤醒正在阻塞的select操作,netty在每次进行新的loop之前,都会将wakeUp 被设置成false,标志新的一轮loop的开始
select(wakenUp.getAndSet(false));
if (wakenUp.get()) {
selector.wakeup();
}
// fall through
default:
}
} catch (IOException e) {
// If we receive an IOException here its because the Selector is messed up. Let's rebuild
// the selector and retry. https://github.com/netty/netty/issues/8566
rebuildSelector0();
handleLoopException(e);
continue;
}
cancelledKeys = 0;
needsToSelectAgain = false;
final int ioRatio = this.ioRatio;
if (ioRatio == 100) {
try {
processSelectedKeys();
} finally {
// Ensure we always run tasks
runAllTasks();
}
} else {
final long ioStartTime = System.nanoTime();
try {
//2.处理产生网络IO事件的channel
processSelectedKeys();
} finally {
// Ensure we always run tasks.
final long ioTime = System.nanoTime() - ioStartTime;
//3.处理任务队列
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);
}
}
}
}
上面的代码就是这个Reactor
线程的核心,也是服务端的核心代码,由于我们要看Netty
中是如何规避Nio
空轮训的Bug
,所以肯定要发生读写,这个时候我们就需要查看上面switch
中代码,而switch
中代码的触发需要任务队列中没有任务,这个时候就会调用select(wakenUp.getAndSet(false));
方法中的代码,我们继续跟进去查看对应的代码,具体的代码如下:
public final class NioEventLoop extends SingleThreadEventLoop {
private void select(boolean oldWakenUp) throws IOException {
Selector selector = this.selector;
try {
int selectCnt = 0;
long currentTimeNanos = System.nanoTime();
//当scheduledTaskQueue为空时 selectDeadLineNanos=当前时间加一秒
long selectDeadLineNanos = currentTimeNanos + delayNanos(currentTimeNanos);
for (;;) {
//1.定时任务截止事时间快到了,中断本次轮询
long timeoutMillis = (selectDeadLineNanos - currentTimeNanos + 500000L) / 1000000L;
//当前的定时任务队列中有任务的截止事件快到了(<=0.5ms),就跳出循环。
if (timeoutMillis <= 0) {
//如果到目前还没有进行过select操作 调用selectNow()
if (selectCnt == 0) {
selector.selectNow();
selectCnt = 1;
}
break;
}
// 2.轮询过程中发现有任务加入,中断本次轮询 netty为了保证任务队列能够及时执行,在进行阻塞select操作之前会判断任务队列是否为空,如果不为空,就执行一次非阻塞select操作,跳出循环
//hasTasks() && wakenUp.compareAndSet(false, true) 如果队列中有任务 则设置wakenUp为true 并返回true
if (hasTasks() && wakenUp.compareAndSet(false, true)) {
selector.selectNow();
selectCnt = 1;
break;
}
//阻塞式select操作
//执行到这一步,说明netty任务队列里面队列为空,并且所有定时任务延迟时间还未到(大于0.5ms),
//于是,在这里进行一次阻塞select操作,截止到第一个定时任务的截止时间
int selectedKeys = selector.select(timeoutMillis);
selectCnt ++;
if (selectedKeys != 0 || oldWakenUp || wakenUp.get() || hasTasks() || hasScheduledTasks()) {
// - 轮询到io事件
// - oldWakenUp 参数为true
// - 用户主动唤醒
// - 任务队列里面有任务
// - 第一个定时任务即将要被执行
break;
}
if (Thread.interrupted()) {
if (logger.isDebugEnabled()) {
logger.debug("Selector.select() returned prematurely because " +
"Thread.currentThread().interrupt() was called. Use " +
"NioEventLoop.shutdownGracefully() to shutdown the NioEventLoop.");
}
selectCnt = 1;
break;
}
long time = System.nanoTime();
//现在的时间-select阻塞的时间=>运行之前的时间
if (time - TimeUnit.MILLISECONDS.toNanos(timeoutMillis) >= currentTimeNanos) {
// timeoutMillis在没有选择任何内容的情况下运行。
selectCnt = 1;
} else if (SELECTOR_AUTO_REBUILD_THRESHOLD > 0 &&
selectCnt >= SELECTOR_AUTO_REBUILD_THRESHOLD) {
//如果selectCnt>=512就重新创建新的selector并替换
//创建新的selector
selector = selectRebuildSelector(selectCnt);
selectCnt = 1;
break;
}
currentTimeNanos = time;
}
if (selectCnt > MIN_PREMATURE_SELECTOR_RETURNS) {
if (logger.isDebugEnabled()) {
logger.debug("Selector.select() returned prematurely {} times in a row for Selector {}.",
selectCnt - 1, selector);
}
}
} catch (CancelledKeyException e) {
if (logger.isDebugEnabled()) {
logger.debug(CancelledKeyException.class.getSimpleName() + " raised by a Selector {} - JDK bug?",
selector, e);
}
}
}
}
我们先打开delayNanos(currentTimeNanos);
对应的代码查看一下,具体的代码如下:
public abstract class SingleThreadEventExecutor extends AbstractScheduledEventExecutor implements OrderedEventExecutor {
//当前的时间加上1秒
private static final long SCHEDULE_PURGE_INTERVAL = TimeUnit.SECONDS.toNanos(1);
protected long delayNanos(long currentTimeNanos) {
//获取定时任务的队列
ScheduledFutureTask<?> scheduledTask = peekScheduledTask();
//如果为空的话
if (scheduledTask == null) {
return SCHEDULE_PURGE_INTERVAL;
}
return scheduledTask.delayNanos(currentTimeNanos);
}
}
final class ScheduledFutureTask<V> extends PromiseTask<V> implements ScheduledFuture<V>, PriorityQueueNode {
//距离当前任务执行还有多长时间
public long delayNanos(long currentTimeNanos) {
//多长时间执行-(传进来的当前时间-当前任务的创建时间)
return Math.max(0, deadlineNanos() - (currentTimeNanos - START_TIME));
}
}
上面的代码的大概的意思就是,如果定时任务队列中有任务就直接返回距离当前的任务执行还剩多长的时间,如果定时队列中没有任务就直接返回当前的时间加上1秒。我们再回到原来的代码
public final class NioEventLoop extends SingleThreadEventLoop {
private void select(boolean oldWakenUp) throws IOException {
Selector selector = this.selector;
try {
int selectCnt = 0;
long currentTimeNanos = System.nanoTime();
//当scheduledTaskQueue为空时 selectDeadLineNanos=当前时间加一秒
long selectDeadLineNanos = currentTimeNanos + delayNanos(currentTimeNanos);
for (;;) {
//1.定时任务截止事时间快到了,中断本次轮询
long timeoutMillis = (selectDeadLineNanos - currentTimeNanos + 500000L) / 1000000L;
//当前的定时任务队列中有任务的截止时间快到了(<=0.5ms),就跳出循环。
if (timeoutMillis <= 0) {
//如果到目前还没有进行过select操作 调用selectNow()
if (selectCnt == 0) {
selector.selectNow();
selectCnt = 1;
}
break;
}
//中间省略一部分代码
currentTimeNanos = time;
}
if (selectCnt > MIN_PREMATURE_SELECTOR_RETURNS) {
if (logger.isDebugEnabled()) {
logger.debug("Selector.select() returned prematurely {} times in a row for Selector {}.",
selectCnt - 1, selector);
}
}
} catch (CancelledKeyException e) {
if (logger.isDebugEnabled()) {
logger.debug(CancelledKeyException.class.getSimpleName() + " raised by a Selector {} - JDK bug?",
selector, e);
}
}
}
}
上面的代码就是结束这个死循环(轮训看看有没有自己感兴趣的事情)的第一个情况:定时任务截止的时间快到了,中断本次轮询,当前的定时任务队列中有任务的截止时间快到了(<=0.5ms),就跳出循环。这个时候会执行一遍selectNow()
,看看有没有发生自己感兴趣的事件。
我们再来看看第二种结束死循环的方法,具体的代码如下:
public final class NioEventLoop extends SingleThreadEventLoop {
private void select(boolean oldWakenUp) throws IOException {
Selector selector = this.selector;
try {
int selectCnt = 0;
long currentTimeNanos = System.nanoTime();
//当scheduledTaskQueue为空时 selectDeadLineNanos=当前时间加一秒
long selectDeadLineNanos = currentTimeNanos + delayNanos(currentTimeNanos);
for (;;) {
//省略一部分代码
// 2.轮询过程中发现有任务加入,中断本次轮询 netty为了保证任务队列能够及时执行,在进行阻塞select操作之前会判断任务队列是否为空,如果不为空,就执行一次非阻塞select操作,跳出循环
//hasTasks() && wakenUp.compareAndSet(false, true) 如果队列中有任务 则设置wakenUp为true 并返回true
if (hasTasks() && wakenUp.compareAndSet(false, true)) {
selector.selectNow();
selectCnt = 1;
break;
}
//省略一部分代码
currentTimeNanos = time;
}
if (selectCnt > MIN_PREMATURE_SELECTOR_RETURNS) {
if (logger.isDebugEnabled()) {
logger.debug("Selector.select() returned prematurely {} times in a row for Selector {}.",
selectCnt - 1, selector);
}
}
} catch (CancelledKeyException e) {
if (logger.isDebugEnabled()) {
logger.debug(CancelledKeyException.class.getSimpleName() + " raised by a Selector {} - JDK bug?",
selector, e);
}
}
}
}
上面的代码就是结束这个死循环(轮训看看有没有自己感兴趣的事情)的第二个情况:在轮询过程中发现有任务加入,中断本次轮询 。netty
为了保证任务队列能够及时执行,在进行阻塞select
操作之前会判断任务队列是否为空,如果不为空,就执行一次非阻塞select
操作,跳出循环。这个时候会执行一遍selectNow()
,看看有没有发生自己感兴趣的事件。
下面我来介绍第三种情况,具体的代码如下:
public final class NioEventLoop extends SingleThreadEventLoop {
private void select(boolean oldWakenUp) throws IOException {
Selector selector = this.selector;
try {
int selectCnt = 0;
long currentTimeNanos = System.nanoTime();
//当scheduledTaskQueue为空时 selectDeadLineNanos=当前时间加一秒
long selectDeadLineNanos = currentTimeNanos + delayNanos(currentTimeNanos);
for (;;) {
//省略一部分代码
//阻塞式select操作
//执行到这一步,说明netty任务队列里面队列为空,并且所有定时任务延迟时间还未到(大于0.5ms),
//于是,在这里进行一次阻塞select操作,截止到第一个定时任务的截止时间
int selectedKeys = selector.select(timeoutMillis);
if (selectedKeys != 0 || oldWakenUp || wakenUp.get() || hasTasks() || hasScheduledTasks()) {
// - 轮询到io事件
// - oldWakenUp 参数为true
// - 用户主动唤醒
// - 任务队列里面有任务
// - 第一个定时任务即将要被执行
break;
}
//省略一部分代码
currentTimeNanos = time;
}
if (selectCnt > MIN_PREMATURE_SELECTOR_RETURNS) {
if (logger.isDebugEnabled()) {
logger.debug("Selector.select() returned prematurely {} times in a row for Selector {}.",
selectCnt - 1, selector);
}
}
} catch (CancelledKeyException e) {
if (logger.isDebugEnabled()) {
logger.debug(CancelledKeyException.class.getSimpleName() + " raised by a Selector {} - JDK bug?",
selector, e);
}
}
}
}
上面的代码就是结束这个死循环(轮训看看有没有自己感兴趣的事情)的第三个情况:(1)轮询到IO
事件。(2)oldWakeUp
参数为true
。(3)用户主动唤醒。(4)任务队列里面有任务。(5)第一个定时任务即将要被执行。这个时候都会跳出当前循环。
下面我们再来看看第四种情况,具体的代码如下:
public final class NioEventLoop extends SingleThreadEventLoop {
private void select(boolean oldWakenUp) throws IOException {
Selector selector = this.selector;
try {
int selectCnt = 0;
long currentTimeNanos = System.nanoTime();
//当scheduledTaskQueue为空时 selectDeadLineNanos=当前时间加一秒
long selectDeadLineNanos = currentTimeNanos + delayNanos(currentTimeNanos);
for (;;) {
//省略一步分代码
//线程被interrupted
if (Thread.interrupted()) {
if (logger.isDebugEnabled()) {
logger.debug("Selector.select() returned prematurely because " +
"Thread.currentThread().interrupt() was called. Use " +
"NioEventLoop.shutdownGracefully() to shutdown the NioEventLoop.");
}
selectCnt = 1;
break;
}
currentTimeNanos = time;
}
if (selectCnt > MIN_PREMATURE_SELECTOR_RETURNS) {
if (logger.isDebugEnabled()) {
logger.debug("Selector.select() returned prematurely {} times in a row for Selector {}.",
selectCnt - 1, selector);
}
}
} catch (CancelledKeyException e) {
if (logger.isDebugEnabled()) {
logger.debug(CancelledKeyException.class.getSimpleName() + " raised by a Selector {} - JDK bug?",
selector, e);
}
}
}
}
上面的代码就是结束这个死循环(轮训看看有没有自己感兴趣的事情)的第四个情况:当前的线程被interrupted
,这个时候也会结束这次轮询。
下面我们再来查看一下第五种情况,具体的代码如下:
public final class NioEventLoop extends SingleThreadEventLoop {
private void select(boolean oldWakenUp) throws IOException {
Selector selector = this.selector;
try {
int selectCnt = 0;
long currentTimeNanos = System.nanoTime();
//当scheduledTaskQueue为空时 selectDeadLineNanos=当前时间加一秒
long selectDeadLineNanos = currentTimeNanos + delayNanos(currentTimeNanos);
for (;;) {
//省略一部分代码
//阻塞式select操作
//执行到这一步,说明netty任务队列里面队列为空,并且所有定时任务延迟时间还未到(大于0.5ms),
//于是,在这里进行一次阻塞select操作,截止到第一个定时任务的截止时间
int selectedKeys = selector.select(timeoutMillis);
selectCnt ++;
long time = System.nanoTime();
//现在的时间-select阻塞的时间=>运行之前的时间
if (time - TimeUnit.MILLISECONDS.toNanos(timeoutMillis) >= currentTimeNanos) {
// timeoutMillis在没有选择任何内容的情况下运行。
selectCnt = 1;
} else if (SELECTOR_AUTO_REBUILD_THRESHOLD > 0 &&
selectCnt >= SELECTOR_AUTO_REBUILD_THRESHOLD) { //发生空轮询
//如果selectCnt>=512就重新创建新的selector并替换
//创建新的selector
selector = selectRebuildSelector(selectCnt);
selectCnt = 1;
break;
}
currentTimeNanos = time;
}
if (selectCnt > MIN_PREMATURE_SELECTOR_RETURNS) {
if (logger.isDebugEnabled()) {
logger.debug("Selector.select() returned prematurely {} times in a row for Selector {}.",
selectCnt - 1, selector);
}
}
} catch (CancelledKeyException e) {
if (logger.isDebugEnabled()) {
logger.debug(CancelledKeyException.class.getSimpleName() + " raised by a Selector {} - JDK bug?",
selector, e);
}
}
}
}
上面的代码就是结束这个死循环(轮训看看有没有自己感兴趣的事情)的第五个情况:发生了空轮训的次数大于512次,当现在时间-select阻塞的时间小于等于运行之前的时间,表示发生了一次空轮训。当发生的空轮训的次数大于512次,Netty
就进行对应的处理。我们来看看Netty
是如何处理的。我们继续跟进对应的selectRebuildSelector(selectCnt);
方法,具体的代码如下:
public final class NioEventLoop extends SingleThreadEventLoop {
private Selector selectRebuildSelector(int selectCnt) throws IOException {
logger.warn(
"Selector.select() returned prematurely {} times in a row; rebuilding Selector {}.",
selectCnt, selector);
rebuildSelector();
Selector selector = this.selector;
//再次选择以填充selectedKeys。
selector.selectNow();
return selector;
}
public void rebuildSelector() {
if (!inEventLoop()) {
execute(new Runnable() {
@Override
public void run() {
rebuildSelector0();
}
});
return;
}
rebuildSelector0();
}
private void rebuildSelector0() {
final Selector oldSelector = selector;
final SelectorTuple newSelectorTuple;
if (oldSelector == null) {
return;
}
try {
//重新打开并创建selector
newSelectorTuple = openSelector();
} catch (Exception e) {
logger.warn("Failed to create a new Selector.", e);
return;
}
// 将所有通道注册到新的选择器。
/**
* 拿到有效的key
* 取消该key在旧的selector上的事件注册
* 将该key对应的channel注册到新的selector上
* 重新绑定channel和新的key的关系
* 如果附加属性是AbstractNioChannel则修改selectionKey属性为最新key
*/
int nChannels = 0;
for (SelectionKey key: oldSelector.keys()) {
Object a = key.attachment();
try {
if (!key.isValid() || key.channel().keyFor(newSelectorTuple.unwrappedSelector) != null) {
continue;
}
int interestOps = key.interestOps();
key.cancel();
//注册到新的到selector上去
SelectionKey newKey = key.channel().register(newSelectorTuple.unwrappedSelector, interestOps, a);
if (a instanceof AbstractNioChannel) {
// Update SelectionKey
((AbstractNioChannel) a).selectionKey = newKey;
}
nChannels ++;
} catch (Exception e) {
logger.warn("Failed to re-register a Channel to the new Selector.", e);
if (a instanceof AbstractNioChannel) {
AbstractNioChannel ch = (AbstractNioChannel) a;
ch.unsafe().close(ch.unsafe().voidPromise());
} else {
@SuppressWarnings("unchecked")
NioTask<SelectableChannel> task = (NioTask<SelectableChannel>) a;
invokeChannelUnregistered(task, key, e);
}
}
}
//赋值最新selector
selector = newSelectorTuple.selector;
unwrappedSelector = newSelectorTuple.unwrappedSelector;
try {
// 是时候关闭旧的选择器了,因为其他所有内容都已注册到新选择器
oldSelector.close();
} catch (Throwable t) {
if (logger.isWarnEnabled()) {
logger.warn("Failed to close the old Selector.", t);
}
}
if (logger.isInfoEnabled()) {
logger.info("Migrated " + nChannels + " channel(s) to the new Selector.");
}
}
}
我们发现最终调用的rebuildSelector0();
方法,我们发现先创建了一个selector
对象,然后将老的selector
的对象上的信息赋值给新的的selector
对象上,最后将老的selector
对象关闭。至此Netty
成功了规避了空轮询的Bug
了。
最终我们来总结一下服务端主要的干的事,具体如下图: