Nettyコアソースコード解析(2)、Nettyサーバーのリクエスト受信処理のソースコード解析

シリーズ記事ディレクトリ

Nettyコアのソースコード解析(1)、Nettyのサーバーサイド起動処理のソースコード解析
Nettyのコアソースコード解析(2)、Nettyのサーバーサイドのリクエスト受信処理のソースコード解析
Nettyコアのソースコード解析(3) ビジネスリクエストの鍵実行 - ChannelPipeline、ChannelHandler、ChannelHandlerContext のソースコード解析
Netty コアのソースコード解析 (4) ハートビート検出のソースコード解析
Netty コアのソースコード解析 (5) コアコンポーネント EventLoop のソースコード解析

1. 接続要求受付処理のソースコード解析

Netty サーバーが起動すると、NioEventLoop の run メソッドが実行されます。これは無限ループであり、イベントが発生したかどうかを判断するために継続的にループします。

// io.netty.channel.nio.NioEventLoop#run
@Override
protected void run() {
    
    
    for (;;) {
    
    
        try {
    
    
            switch (selectStrategy.calculateStrategy(selectNowSupplier, hasTasks())) {
    
    
                case SelectStrategy.CONTINUE:
                    continue;
                case SelectStrategy.SELECT:
                    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 wake up 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 through
                default:
            }

            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 {
    
    
                	// 关键方法
                    processSelectedKeys();
                } finally {
    
    
                    // Ensure we always run tasks.
                    final long ioTime = System.nanoTime() - ioStartTime;
                    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);
        }
    }
}

1. イベントの価値

読み取り、書き込み、接続、および受け入れイベントはすべてSelectionKeyで定義され、計算にはオフセットが使用されます。

// 1
public static final int OP_READ = 1 << 0;
// 4
public static final int OP_WRITE = 1 << 2;
// 8
public static final int OP_CONNECT = 1 << 3;
// 16
public static final int OP_ACCEPT = 1 << 4;

2. processSelectedKeys イベントの取得

// io.netty.channel.nio.NioEventLoop#processSelectedKeys
private void processSelectedKeys() {
    
    
    if (selectedKeys != null) {
    
    
        processSelectedKeysOptimized();
    } else {
    
    
        processSelectedKeysPlain(selector.selectedKeys());
    }
}

// io.netty.channel.nio.NioEventLoop#processSelectedKey(java.nio.channels.SelectionKey, io.netty.channel.nio.AbstractNioChannel)
private void processSelectedKey(SelectionKey k, AbstractNioChannel ch) {
    
    
    final AbstractNioChannel.NioUnsafe unsafe = ch.unsafe();
    if (!k.isValid()) {
    
    
        final EventLoop eventLoop;
        try {
    
    
            eventLoop = ch.eventLoop();
        } catch (Throwable ignored) {
    
    
            // If the channel implementation throws an exception because there is no event loop, we ignore this
            // because we are only trying to determine if ch is registered to this event loop and thus has authority
            // to close ch.
            return;
        }
        // Only close ch if ch is still registered to this EventLoop. ch could have deregistered from the event loop
        // and thus the SelectionKey could be cancelled as part of the deregistration process, but the channel is
        // still healthy and should not be closed.
        // See https://github.com/netty/netty/issues/5125
        if (eventLoop != this || eventLoop == null) {
    
    
            return;
        }
        // close the channel if the key is not valid anymore
        unsafe.close(unsafe.voidPromise());
        return;
    }

    try {
    
    
        int readyOps = k.readyOps();
        // We first need to call finishConnect() before try to trigger a read(...) or write(...) as otherwise
        // the NIO JDK channel implementation may throw a NotYetConnectedException.
        if ((readyOps & SelectionKey.OP_CONNECT) != 0) {
    
    
            // remove OP_CONNECT as otherwise Selector.select(..) will always return without blocking
            // See https://github.com/netty/netty/issues/924
            int ops = k.interestOps();
            ops &= ~SelectionKey.OP_CONNECT;
            k.interestOps(ops);

            unsafe.finishConnect();
        }

        // Process OP_WRITE first as we may be able to write some queued buffers and so free memory.
        if ((readyOps & SelectionKey.OP_WRITE) != 0) {
    
    
            // Call forceFlush which will also take care of clear the OP_WRITE once there is nothing left to write
            ch.unsafe().forceFlush();
        }

        // Also check for readOps of 0 to workaround possible JDK bug which may otherwise lead
        // to a spin loop
        // 判断事件,selectKeys最终执行的方法
        if ((readyOps & (SelectionKey.OP_READ | SelectionKey.OP_ACCEPT)) != 0 || readyOps == 0) {
    
    
            unsafe.read();
        }
    } catch (CancelledKeyException ignored) {
    
    
        unsafe.close(unsafe.voidPromise());
    }
}

クライアント接続がある場合、readyOps の値が 16 であり、これに対応する ACCEPT イベントも 16 であり、この時点で unsafe.read() メソッドが実行されることがわかります。
ここに画像の説明を挿入
NioMessageUnsafe は AbstractNioMessageChannel の内部クラスです。

// io.netty.channel.nio.AbstractNioMessageChannel.NioMessageUnsafe
private final class NioMessageUnsafe extends AbstractNioUnsafe {
    
    

    private final List<Object> readBuf = new ArrayList<Object>();

    @Override
    public void read() {
    
    
    	// 检查该eventLoop是否是当前线程
        assert eventLoop().inEventLoop();
        final ChannelConfig config = config(); // 拿到config
        final ChannelPipeline pipeline = pipeline(); // 拿到pipeline
        final RecvByteBufAllocator.Handle allocHandle = unsafe().recvBufAllocHandle();
        allocHandle.reset(config);

        boolean closed = false;
        Throwable exception = null;
        try {
    
    
            try {
    
    
                do {
    
    
                	// readBuf是一个list,此处循环执行该方法
                	// doReadMessages是读取boss线程中的NioServerSocketChannel接收到的请求,并把这些请求放进readBuf
                    int localRead = doReadMessages(readBuf);
                    if (localRead == 0) {
    
    
                        break;
                    }
                    if (localRead < 0) {
    
    
                        closed = true;
                        break;
                    }

                    allocHandle.incMessagesRead(localRead);
                } while (allocHandle.continueReading());
            } catch (Throwable t) {
    
    
                exception = t;
            }

            int size = readBuf.size();
            for (int i = 0; i < size; i ++) {
    
    
                readPending = false;
                // 循环调用handler中的ChannelRead方法
                pipeline.fireChannelRead(readBuf.get(i));
            }
            readBuf.clear();
            allocHandle.readComplete();
            pipeline.fireChannelReadComplete();

            if (exception != null) {
    
    
                closed = closeOnReadError(exception);

                pipeline.fireExceptionCaught(exception);
            }

            if (closed) {
    
    
                inputShutdown = true;
                if (isOpen()) {
    
    
                    close(voidPromise());
                }
            }
        } finally {
    
    
            // Check if there is a readPending which was not processed yet.
            // This could be for two reasons:
            // * The user called Channel.read() or ChannelHandlerContext.read() in channelRead(...) method
            // * The user called Channel.read() or ChannelHandlerContext.read() in channelReadComplete(...) method
            //
            // See https://github.com/netty/netty/issues/2254
            if (!readPending && !config.isAutoRead()) {
    
    
                removeReadOp();
            }
        }
    }
}

(1) doReadMessagesメソッド

// io.netty.channel.socket.nio.NioServerSocketChannel#doReadMessages
@Override
protected int doReadMessages(List<Object> buf) throws Exception {
    
    
	// 实际上调用NIO的的accept方法,获取SocketChannel
    SocketChannel ch = SocketUtils.accept(javaChannel());

    try {
    
    
        if (ch != null) {
    
    
        	// 将NIO的SocketChannel包装成NioSocketChannel
            buf.add(new NioSocketChannel(this, ch));
            return 1;
        }
    } catch (Throwable t) {
    
    
        logger.warn("Failed to create a new channel from an accepted socket.", t);

        try {
    
    
            ch.close();
        } catch (Throwable t2) {
    
    
            logger.warn("Failed to close a socket.", t2);
        }
    }

    return 0;
}

doReadMessages が実際に NIO の SocketChannel の accept メソッドを呼び出して、NIO の SocketChannel を取得していることがわかりました。
次に、Netty の NioSocketChannel を使用して NIO の SocketChannel をラップし、最後にそれを buf コンテナーに追加します。

(2) パイプラインの fireChannelRead メソッド

doReadMessages メソッドが NioSocketChannel を取得した後、fireChannelRead はこれらの NioSocketChannel をパラメータとして渡します。

// io.netty.channel.DefaultChannelPipeline#fireChannelRead
@Override
public final ChannelPipeline fireChannelRead(Object msg) {
    
    
    AbstractChannelHandlerContext.invokeChannelRead(head, msg);
    return this;
}
// io.netty.channel.AbstractChannelHandlerContext#invokeChannelRead(io.netty.channel.AbstractChannelHandlerContext, java.lang.Object)
static void invokeChannelRead(final AbstractChannelHandlerContext next, Object msg) {
    
    
    final Object m = next.pipeline.touch(ObjectUtil.checkNotNull(msg, "msg"), next);
    EventExecutor executor = next.executor();
    if (executor.inEventLoop()) {
    
    
        next.invokeChannelRead(m);
    } else {
    
    
        executor.execute(new Runnable() {
    
    
            @Override
            public void run() {
    
    
                next.invokeChannelRead(m);
            }
        });
    }
}

// io.netty.channel.AbstractChannelHandlerContext#invokeChannelRead(java.lang.Object)
private void invokeChannelRead(Object msg) {
    
    
    if (invokeHandler()) {
    
    
        try {
    
    
            ((ChannelInboundHandler) handler()).channelRead(this, msg);
        } catch (Throwable t) {
    
    
            notifyHandlerException(t);
        }
    } else {
    
    
        fireChannelRead(msg);
    }
}

channelHandler の channelRead メソッドが呼び出されることがわかりました。

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    
    
    ctx.fireChannelRead(msg);
}
@Override
public ChannelHandlerContext fireChannelRead(final Object msg) {
    
    
    invokeChannelRead(findContextInbound(), msg);
    return this;
}
static void invokeChannelRead(final AbstractChannelHandlerContext next, Object msg) {
    
    
    final Object m = next.pipeline.touch(ObjectUtil.checkNotNull(msg, "msg"), next);
    EventExecutor executor = next.executor();
    if (executor.inEventLoop()) {
    
    
        next.invokeChannelRead(m);
    } else {
    
    
        executor.execute(new Runnable() {
    
    
            @Override
            public void run() {
    
    
                next.invokeChannelRead(m);
            }
        });
    }
}

channelRead メソッドが呼び出されると、ctx.fireChannelRead(msg); が呼び出されてリクエストがパイプライン内の次のハンドラーに渡され、パイプライン内でハンドラーがチェーンに格納されることがわかりました。

ServerBootstrapAcceptor はパイプラインの最後のハンドラーであり、その channelRead メソッドは ServerBootStrap 親クラスに実装されています。引き続き、ServerBootstrapAcceptor の channelRead メソッドを見てみましょう。

注意!此处的Handler,并不是child的Handler!而是bossGroup中定义的Handler!
woekerGroup定义的Handler在children中!

(3) ServerBootstrapAcceptorのchannelReadメソッド

// io.netty.bootstrap.ServerBootstrap.ServerBootstrapAcceptor#channelRead
@Override
@SuppressWarnings("unchecked")
public void channelRead(ChannelHandlerContext ctx, Object msg) {
    
    
    final Channel child = (Channel) msg;

    child.pipeline().addLast(childHandler);

    setChannelOptions(child, childOptions, logger);

    for (Entry<AttributeKey<?>, Object> e: childAttrs) {
    
    
        child.attr((AttributeKey<Object>) e.getKey()).set(e.getValue());
    }

    try {
    
     // 将客户端连接注册到worker线程池
    	// 此处的childGroup,就是我们的workergroup,child就是NioSocketChannel
        childGroup.register(child).addListener(new ChannelFutureListener() {
    
    
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
    
    
                if (!future.isSuccess()) {
    
    
                    forceClose(child, future.cause());
                }
            }
        });
    } catch (Throwable t) {
    
    
        forceClose(child, t);
    }
}

最後にworkGroup(childGroup)の register メソッドを呼び出して、NioSocketChannelをworkerGroup(childGroup)に登録します。

注意!workGroup是我们命名的,Netty称为childGroup

3.workerGroupの登録処理

上記の分析によると、bossGroup は NioSocketChannel を取得した後、workerGroup (childGroup) の register メソッドを呼び出して登録します。

// io.netty.channel.MultithreadEventLoopGroup#register(io.netty.channel.Channel)
@Override
public ChannelFuture register(Channel channel) {
    
    
    return next().register(channel);
}

次のメソッドは、workerGroup の次のスレッドを取得するためにポーリングし (workerGroup は通常、複数のスレッドを作成します。デフォルトは CPU コアの数 * 2 です)、 register メソッドを呼び出して NioSocketChannel に渡します。

// io.netty.util.concurrent.DefaultEventExecutorChooserFactory.GenericEventExecutorChooser#next
@Override
public EventExecutor next() {
    
    
	// 取下一个执行器
    return executors[Math.abs(idx.getAndIncrement() % executors.length)];
}

引き続き register メソッドを見てみましょう。

// io.netty.channel.SingleThreadEventLoop#register(io.netty.channel.Channel)
@Override
public ChannelFuture register(Channel channel) {
    
    
	// 对Channel进一步包装
    return register(new DefaultChannelPromise(channel, this));
}
// io.netty.channel.SingleThreadEventLoop#register(io.netty.channel.ChannelPromise)
@Override
public ChannelFuture register(final ChannelPromise promise) {
    
    
    ObjectUtil.checkNotNull(promise, "promise");
    // 真正的register的方法
    promise.channel().unsafe().register(this, promise);
    return promise;
}
// io.netty.channel.AbstractChannel.AbstractUnsafe#register
@Override
public final void register(EventLoop eventLoop, final ChannelPromise promise) {
    
    
    if (eventLoop == null) {
    
    
        throw new NullPointerException("eventLoop");
    }
    if (isRegistered()) {
    
    
        promise.setFailure(new IllegalStateException("registered to an event loop already"));
        return;
    }
    if (!isCompatible(eventLoop)) {
    
    
        promise.setFailure(
                new IllegalStateException("incompatible event loop type: " + eventLoop.getClass().getName()));
        return;
    }

    AbstractChannel.this.eventLoop = eventLoop;

    if (eventLoop.inEventLoop()) {
    
    
        register0(promise);
    } else {
    
    
        try {
    
    
            eventLoop.execute(new Runnable() {
    
    
                @Override
                public void run() {
    
    
                	// 进入到这里
                    register0(promise);
                }
            });
        } catch (Throwable t) {
    
    
            logger.warn(
                    "Force-closing a channel whose registration task was not accepted by an event loop: {}",
                    AbstractChannel.this, t);
            closeForcibly();
            closeFuture.setClosed();
            safeSetFailure(promise, t);
        }
    }
}

register0メソッドでは、doRegisterメソッドの登録が実行され、Channelに存在する可能性のあるタスクがあるかどうかを判断して実行されます。

// io.netty.channel.AbstractChannel.AbstractUnsafe#register0
private void register0(ChannelPromise promise) {
    
    
    try {
    
    
        // check if the channel is still open as it could be closed in the mean time when the register
        // call was outside of the eventLoop
        if (!promise.setUncancellable() || !ensureOpen(promise)) {
    
    
            return;
        }
        boolean firstRegistration = neverRegistered;
        // 执行注册
        doRegister();
        neverRegistered = false;
        registered = true;

        // Ensure we call handlerAdded(...) before we actually notify the promise. This is needed as the
        // user may already fire events through the pipeline in the ChannelFutureListener.
        pipeline.invokeHandlerAddedIfNeeded();

        safeSetSuccess(promise);
        pipeline.fireChannelRegistered();
        // Only fire a channelActive if the channel has never been registered. This prevents firing
        // multiple channel actives if the channel is deregistered and re-registered.
        if (isActive()) {
    
    
            if (firstRegistration) {
    
    
            	// 第一次注册执行ChannelActive方法
                pipeline.fireChannelActive();
            } else if (config().isAutoRead()) {
    
    
                // This channel was registered before and autoRead() is set. This means we need to begin read
                // again so that we process inbound data.
                //
                // See https://github.com/netty/netty/issues/4805
                // 开始读取
                beginRead();
            }
        }
    } catch (Throwable t) {
    
    
        // Close the channel directly to avoid FD leak.
        closeForcibly();
        closeFuture.setClosed();
        safeSetFailure(promise, t);
    }
}
// io.netty.channel.nio.AbstractNioChannel#doRegister
@Override
protected void doRegister() throws Exception {
    
    
    boolean selected = false;
    for (;;) {
    
    
        try {
    
    
            selectionKey = javaChannel().register(eventLoop().unwrappedSelector(), 0, this);
            return;
        } catch (CancelledKeyException e) {
    
    
            if (!selected) {
    
    
                // Force the Selector to select now as the "canceled" SelectionKey may still be
                // cached and not removed because no Select.select(..) operation was called yet.
                eventLoop().selectNow();
                selected = true;
            } else {
    
    
                // We forced a select operation on the selector before but the SelectionKey is still cached
                // for whatever reason. JDK bug ?
                throw e;
            }
        }
    }
}

4. beginRead メソッド

// io.netty.channel.AbstractChannel.AbstractUnsafe#beginRead
@Override
public final void beginRead() {
    
    
    assertEventLoop();

    if (!isActive()) {
    
    
        return;
    }

    try {
    
    
        doBeginRead();
    } catch (final Exception e) {
    
    
        invokeLater(new Runnable() {
    
    
            @Override
            public void run() {
    
    
                pipeline.fireExceptionCaught(e);
            }
        });
        close(voidPromise());
    }
}

// io.netty.channel.nio.AbstractNioChannel#doBeginRead
@Override
protected void doBeginRead() throws Exception {
    
    
    // Channel.read() or ChannelHandlerContext.read() was called
    final SelectionKey selectionKey = this.selectionKey;
    if (!selectionKey.isValid()) {
    
    
        return;
    }

    readPending = true;

    final int interestOps = selectionKey.interestOps();
    if ((interestOps & readInterestOp) == 0) {
    
    
        selectionKey.interestOps(interestOps | readInterestOp);
    }
}

この時点で、クライアント側が送信した接続リクエストをサーバー側が受信し、workerGroupに登録し、workerGroupがデータの受信を開始します。
ここに画像の説明を挿入

5. Netty が要求を受け入れるプロセスのソース コードの概要

全体的なプロセス: 接続を受け入れる ----> 新しい NioSocketChannel を作成する --------> ワーカー EventLoop に登録する ----> selecot Read イベントを登録する。

1) サーバーは Accept イベントをポーリングし、イベントを取得した後、ServerSocket の内部クラスである unsafe の read メソッドを呼び出します。
2) doReadMessages は、JDK の Nio Channel クライアントをラップする NioSocketChannel オブジェクトの作成に使用されます。このメソッドは、ServerSocketChanel の作成と同様に、関連するパイプライン、安全でない構成を作成します。
3) 次に、pipeline.fireChannelRead メソッドを実行し、ServerBootstrapAcceptor の channelRead メソッド内で、workerGroup (childGroup) の register メソッドを呼び出して登録します。
4) 登録が成功したら、Read イベントのリッスンを開始します。

おすすめ

転載: blog.csdn.net/A_art_xiang/article/details/130320465