Netty源码解析一服务端启动

Netty的版本是4.1.22

我们从ServerBootstrap的bind()方法开始跟踪Netty服务端的启动过程。大致分为以下四步:

  1. 创建服务器Channel
  2. 初始化服务器channel
  3. 注册selector
  4. 端口绑定

一:创建服务器Channel

AbstractBootStrap

    public ChannelFuture bind(String inetHost, int inetPort) {
        return bind(SocketUtils.socketAddress(inetHost, inetPort));
    }

    public ChannelFuture bind(SocketAddress localAddress) {
        validate();
        if (localAddress == null) {
            throw new NullPointerException("localAddress");
        }
        return doBind(localAddress);
    }

    private ChannelFuture doBind(final SocketAddress localAddress) {
        final ChannelFuture regFuture = initAndRegister(); // 初始化并注册
        ......

    final ChannelFuture initAndRegister() {
        Channel channel = null;
        try {
            channel = channelFactory.newChannel();
        ......

跟踪来到AbstractBootStrap的initAndRegister(),它调用channelFactory的newChannel来创建channel,那么channelFactory指向谁?什么时候赋的值?一般在服务端会有这样的设置bootstrap.channel(NioServerSocketChannel.class),指定服务端要创建的channel类型为NioServerSocketChannel,跟踪该方法会发现

AbstractBootStrap:
    public B channel(Class<? extends C> channelClass) {
        if (channelClass == null) {
            throw new NullPointerException("channelClass");
        }
        return channelFactory(new ReflectiveChannelFactory<C>(channelClass));
    }

继续往下跟踪你会发现这么一句this.channelFactory = channelFactory;
即channelFactory 指向ReflectiveChannelFactory。
接着上面来看看newChannel()的实现

    @Override
    public T newChannel() {
        try {
            return clazz.getConstructor().newInstance();
        } catch (Throwable t) {
            throw new ChannelException("Unable to create Channel from class " + clazz, t);
        }
    }

利用反射得到Channel实例。
clazz即bootstrap.channel(NioServerSocketChannel.class)里的channel方法的参数(这里即是NioServerSocketChannel.class)。
接下来进入到NioServerSocketChannel来看看它的初始化做了什么

	private static final SelectorProvider DEFAULT_SELECTOR_PROVIDER 
		= SelectorProvider.provider();
		
	public NioServerSocketChannel() {
        this(newSocket(DEFAULT_SELECTOR_PROVIDER));
    }

    public NioServerSocketChannel(ServerSocketChannel channel) {
        super(null, channel, SelectionKey.OP_ACCEPT);
        config = new NioServerSocketChannelConfig(this, javaChannel().socket());
    }

先来看看newSocket()方法

    private static ServerSocketChannel newSocket(SelectorProvider provider) {
        try {
            return provider.openServerSocketChannel();
        } catch (IOException e) {
            throw new ChannelException(
                    "Failed to open a server socket.", e);
        }
    }

利用SelectorProvider来创建SocketChannel,实际上是通过JDK来创建底层jdk channel。
继续上面的代码,还创建了NioServerSocketChannelConfig实例,它是tcp的参数配置类。
通过super(null, channel, SelectionKey.OP_ACCEPT)往上。注意这里SelectionKey.OP_ACCEPT,将会在之后NioServerSocketChannel注册到selector时监听该事件

AbstractNioMessageChannel:
    protected AbstractNioMessageChannel(Channel parent, SelectableChannel ch, int readInterestOp) {
        super(parent, ch, readInterestOp);
    }

继续往上

AbstractNioChannel:
    protected AbstractNioChannel(Channel parent, SelectableChannel ch, int readInterestOp) {
        super(parent);
        this.ch = ch;
        this.readInterestOp = readInterestOp;
        try {
            ch.configureBlocking(false);
        } catch (IOException e) {
            try {
                ch.close();
            } catch (IOException e2) {
                if (logger.isWarnEnabled()) {
                    logger.warn(
                            "Failed to close a partially initialized socket.", e2);
                }
            }

            throw new ChannelException("Failed to enter non-blocking mode.", e);
        }
    }

一行熟悉的代码出现ch.configureBlocking(false);,ch指向newSocket()方法中创建的ServerSocketChannel,这里给它设置非阻塞模式。
SelectionKey.OP_ACCEPT被赋值给了readInterestOp字段,之后会用到。
继续向上来到AbstractChannel

    protected AbstractChannel(Channel parent) {
        this.parent = parent;
        id = newId(); // channel唯一标识。这里有个疑问它有啥其它重要作用吗?
        // 这里的Unsafe并非是jdk中底层Unsafe类
        //用来负责底层的connect、 register、read和write等操作。
        unsafe = newUnsafe();
        // 创建该channel的pipeline
        pipeline = newChannelPipeline();
    }

    protected DefaultChannelPipeline newChannelPipeline() {
        return new DefaultChannelPipeline(this);
    }

总结一下重要的方法:
channelFactory:
newChannel(),通过反射创建Channel,类型是你在bootstrap.channel()中指定的,这里服务端是NioServerSocketChannel。

NioServerSocketChannel:
newSocket(),创建java.nio.channels.ServerSocketChannel。
NioServerSocketChannelConfig:tcp的参数配置类

AbstractNioChannel
configureBlocking(false) 设置非阻塞模式

AbstractChannel
创建id, unsafe, pipeline

二:初始化服务端Channel

先回到AbstractBootStrap里的initAndRegister()

    final ChannelFuture initAndRegister() {
        Channel channel = null;
        try {
            channel = channelFactory.newChannel();
            init(channel);

初始化逻辑入口就在init()方法,由于是服务端我们跟踪来到ServerBootStrap

    @Override
    void init(Channel channel) throws Exception {
    // 对应客户端代码中的bootStrap.option(),如服务端指的就是为NioServerSocketChannel配置的option
        final Map<ChannelOption<?>, Object> options = options0();
        synchronized (options) {
            setChannelOptions(channel, options, logger);
        }

// 对应客户端代码中的bootStrap.attr(),如服务端指的就是为NioServerSocketChannel要存储数据attr
        final Map<AttributeKey<?>, Object> attrs = attrs0();
        synchronized (attrs) {
            for (Entry<AttributeKey<?>, Object> e: attrs.entrySet()) {
                @SuppressWarnings("unchecked")
                AttributeKey<Object> key = (AttributeKey<Object>) e.getKey();
                channel.attr(key).set(e.getValue());
            }
        }
// 服务端的pipline
        ChannelPipeline p = channel.pipeline();
// 新连接channel的EventLoopGroup,bootStrap.group(bossGroup, workerGroup)
// 新连接channel的group就是workerGroup
        final EventLoopGroup currentChildGroup = childGroup;
// 新连接channel的handler,对应bootstrap.childHandler()
        final ChannelHandler currentChildHandler = childHandler;
// 新连接channel的option,对应bootstrap.childOption()
        final Entry<ChannelOption<?>, Object>[] currentChildOptions;
// 新连接channel要存储的数据,对应bootstrap.childAttr()
        final Entry<AttributeKey<?>, Object>[] currentChildAttrs;
        synchronized (childOptions) {
            currentChildOptions = childOptions.entrySet().toArray(newOptionArray(childOptions.size()));
        }
        synchronized (childAttrs) {
            currentChildAttrs = childAttrs.entrySet().toArray(newAttrArray(childAttrs.size()));
        }
        ......
    }

设置channel的options和attrs。这里的option与attrs指的是你在代码里的。(Attribute<?> 用来在channel上记录数据。)

bootstrap.option(ChannelOption.SO_BACKLOG, 128)
	.childrenOption(ChannelOption.SO_KEEPALIVE, true)
	.attr(..)
	.childAttr()

之后给服务端的piplene添加了一个handler

// 向pipline中添加一个handler
        p.addLast(new ChannelInitializer<Channel>() {
            @Override
            public void initChannel(final Channel ch) throws Exception {
                final ChannelPipeline pipeline = ch.pipeline();
                // 指的是你的代码中bootstrap.handler()所配的handler,
                // 将其添加进pipline
                ChannelHandler handler = config.handler();
                if (handler != null) {
                    pipeline.addLast(handler);
                }
	// 新连接channel在创建后,需要将配置的属性,数据,handler设置给新的channel
                ch.eventLoop().execute(new Runnable() {
                    @Override
                    public void run() {
                        pipeline.addLast(new ServerBootstrapAcceptor(
                                ch, currentChildGroup, currentChildHandler,
                                 currentChildOptions, currentChildAttrs));
                    }
                });
            }
        });

关于Pipeline,EventLoopGroup,EventLoop,以及ServerBootstrapAcceptor之后文章分析。
总结一下
关于初始化服务端channel,就是配置option,attrs,handler给channel

三:注册

再次回到AbstractBootStrap里的initAndRegister()

    final ChannelFuture initAndRegister() {
        Channel channel = null;
        try {
            channel = channelFactory.newChannel();
            init(channel);
        } catch (Throwable t) {
            if (channel != null) {
                // channel can be null if newChannel crashed (eg SocketException("too many open files"))
                channel.unsafe().closeForcibly();
                // as the Channel is not registered yet we need to force the usage of the GlobalEventExecutor
                return new DefaultChannelPromise(channel, GlobalEventExecutor.INSTANCE).setFailure(t);
            }
            // as the Channel is not registered yet we need to force the usage of the GlobalEventExecutor
            return new DefaultChannelPromise(new FailedChannel(), GlobalEventExecutor.INSTANCE).setFailure(t);
        }

        ChannelFuture regFuture = config().group().register(channel);
        .....

注册逻辑代码在这一句

 ChannelFuture regFuture = config().group().register(channel);

config()实现在ServerBootstrap中

ServerBootstrap:

	private final ServerBootstrapConfig config = new ServerBootstrapConfig(this);
   
    @Override
    public final ServerBootstrapConfig config() {
        return config;
    }

在创建ServerBootstrapConfig时将ServerBootstrap传入,其会被赋值给AbstractBootstrapConfig的bootstrap字段。
继续定位group()

AbstractBopotstrapConfig:
    public final EventLoopGroup group() {
        return bootstrap.group();
    }

bootstrap字段指向ServerBootstrap,其group()方法实现在AbstractBootStrap

AbstractBootStrap:
    public final EventLoopGroup group() {
        return group;
    }

	volatile EventLoopGroup group;

那么这个group字段什么时候被赋的值?
你的服务端代码中会有这样一句:bootstrap.group(bossGroup, workerGroup)
跟踪该代码

    public ServerBootstrap group(EventLoopGroup parentGroup, EventLoopGroup childGroup) {
    // 这里就将bossGroup赋给了AbstractBootStrap的group字段
        super.group(parentGroup); 
......
	// 将workerGroup赋给this.childGroup字段
        this.childGroup = childGroup;
        return this;
    }

代码逻辑绕到了eventLoopGroup里,为什么要绕这一圈,因为注册首先是要从eventLoopGroup所管理的多个eventLoop中选择一个,channel就在该eventLoop的线程里执行。

MultithreadEventLoopGroup:
    @Override
    public ChannelFuture register(Channel channel) {
        return next().register(channel);
    }
    
    @Override
    public EventLoop next() {
        return (EventLoop) super.next();
    }
    
MultithreadEventExecutorGroup:
    @Override
    public EventExecutor next() {
        return chooser.next();
    }
    
这里以PowerOfTwoEventExecutorChooser为例:
    @Override
    public EventExecutor next() {
        return executors[idx.getAndIncrement() & executors.length - 1];
    }

这里executors数组指的是什么?它指的是EventLoop数组,EventLoopGroup管理多个EventLoop,next()调用chooser策略找到下一个EventLoop,调用其register()进行注册逻辑。该数组创建逻辑在MultithreadEventExecutorGroup构造函数中,数组中元素为NioEventLoop。
定位到NioEventLoop,注意方法重载,接收Channel参数的register方法在其父类SingleThreadEventLoop中

SingleThreadEventLoop:
    @Override
    public ChannelFuture register(Channel channel) {
        return register(new DefaultChannelPromise(channel, this));
    }

    @Override
    public ChannelFuture register(final ChannelPromise promise) {
        ObjectUtil.checkNotNull(promise, "promise");
        promise.channel().unsafe().register(this, promise);
        return promise;
    }

重点在这一句promise.channel().unsafe().register(this, promise);
promise.channel()指的是之前创建的NioServerSocketChannel,其在初始化过程中会创建一个NioMessageUnsafe。代码逻辑在AbstractChannel构造函数中,调用newUnsafe()方法,其实现在AbstractNioMessageChannel中

    @Override
    protected AbstractNioUnsafe newUnsafe() {
        return new NioMessageUnsafe();
    }

NioMessageUnsafe是AbstractNioMessageChannel的内部类,往上在AbstractChannel的内部类AbstractUnsafe中找到register实现

AbstractUnsafe:
	public final void register(EventLoop eventLoop, final ChannelPromise promise) {
            ......
            AbstractChannel.this.eventLoop = eventLoop;
            if (eventLoop.inEventLoop()) {
                register0(promise);
            } else {
            ......

先将EventLoop事件循环器绑定到该NioServerSocketChannel上,然后调用 register0()

AbstractUnsafe:
        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) {
                        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);
            }
        }

先调用AbstractNioChannel的doRegister():重点在selectionKey = javaChannel().register(eventLoop().unwrappedSelector(), 0, this);
就是利用JDK来完成实际注册过程,返回selectionKey 。
再调用invokeHandlerAddedIfNeeded()和invokeHandlerAddedIfNeeded()。这两个方法干了什么?举个例子

bootstrap.handler(new SimpleServerHandler())

class SimpleServerHandler extends ChannelInboundHandlerAdapter {
        @Override
        public void channelActive(ChannelHandlerContext ctx) throws Exception {
            System.out.println("channelActive");
        }

        @Override
        public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
            System.out.println("channelRegistered");
        }

        @Override
        public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
            System.out.println("handlerAdded");
        }
    }

它们分别调用了上述例子中的handlerAdded与channelRegistered。
回到register0继续往下有这样一段代码

                if (isActive()) {
                    if (firstRegistration) {
                        pipeline.fireChannelActive();

fireChannelActive显然是调用例子中的channelActive方法的,但是这里它不会被执行,因为isActice返回false
定位到NioServerSocketChannel

    @Override
    public boolean isActive() {
        return javaChannel().socket().isBound();
    }

定位到jdk的ServerSocket中

    /**
     * Returns the binding state of the ServerSocket.
     *
     * @return true if the ServerSocket successfully bound to an address
     * @since 1.4
     */
    public boolean isBound() {
        // Before 1.3 ServerSockets were always bound during creation
        return bound || oldImpl;
    }

因为此时channel还没有绑定地址,所以返回false。
那么例子中的channelActive方法什么时候被调用呢?端口绑定成功后。

四:端口绑定

注册完成后,代码回到一开始的AbstractBootstrap.doBind方法,具体绑定逻辑入口在doBind0(regFuture, channel, localAddress, promise);

    private static void doBind0(
            final ChannelFuture regFuture, final Channel channel,
            final SocketAddress localAddress, final ChannelPromise promise) {

        // This method is invoked before channelRegistered() is triggered.  Give user handlers a chance to set up
        // the pipeline in its channelRegistered() implementation.
        channel.eventLoop().execute(new Runnable() {
            @Override
            public void run() {
                if (regFuture.isSuccess()) {
                    channel.bind(localAddress, promise).addListener(ChannelFutureListener.CLOSE_ON_FAILURE);
                } else {
                    promise.setFailure(regFuture.cause());
                }
            }
        });
    }

在上述的注册过程中EventLoopGroup选择了一个EventLoop,将其与channel绑定,这里的逻辑就是将绑定操作封装成一个Runnable,交给eventLoop,异步化执行。
往下跟踪bind方法发现最终

AbstractChannel:
    @Override
    public ChannelFuture bind(SocketAddress localAddress) {
        return pipeline.bind(localAddress);
    }

    @Override
    public final ChannelFuture bind(SocketAddress localAddress) {
        return tail.bind(localAddress);
    }

关于pipeline之后文章分析
继续往下跟踪

HeadContext : 它是DefaultChannelPipeline的内部类
     public void bind(
             ChannelHandlerContext ctx, SocketAddress localAddress, 
             ChannelPromise promise)throws Exception {
         unsafe.bind(localAddress, promise);
     }

这里的unsafe指的是前面说过的NioMessageUnsafe。

AbstractUnsafe: AbstractChannel的内部类
        public final void bind(final SocketAddress localAddress, final ChannelPromise promise) {
			......
            boolean wasActive = isActive();
            try {
                doBind(localAddress);
            } catch (Throwable t) {
                safeSetFailure(promise, t);
                closeIfClosed();
                return;
            }

            if (!wasActive && isActive()) {
                invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        pipeline.fireChannelActive();
                    }
                });
            }

            safeSetSuccess(promise);
        }

NioServerSocketChannel:
    @Override
    protected void doBind(SocketAddress localAddress) throws Exception {
        if (PlatformDependent.javaVersion() >= 7) {
            javaChannel().bind(localAddress, config.getBacklog());
        } else {
            javaChannel().socket().bind(localAddress, config.getBacklog());
        }
    }

就是利用JDK来完成绑定端口任务。若绑定端口成功则(!wasActive && isActive()为true,pipeline.fireChannelActive()包装成Runnable交给eventLoop执行,例子中channelActive方法被调用。

到这里已经绑定好了,不过还没有给服务端channel注册OP_ACCEPT事件。

接着往下来看看在调用pipeline.fireChannelActive()的过程
之前的创建服务端channel,在AbstractChannel构造器中会为当前channel创建一个pipelinepipeline = newChannelPipeline();,创建的是DefaultChannelPipeline,其构造函数里head = new HeadContext(this);
当你跳转到DefaultChannelPipeline的

    public final ChannelPipeline fireChannelActive() {
        AbstractChannelHandlerContext.invokeChannelActive(head);
        return this;
    }

最后代码定位到HeadContext

HeadContext : 它是DefaultChannelPipeline的内部类
        public void channelActive(ChannelHandlerContext ctx) throws Exception {
            ctx.fireChannelActive(); // 例子中的channelActive方法将被调用

            readIfIsAutoRead();
        }

        private void readIfIsAutoRead() {
            if (channel.config().isAutoRead()) { //为true
                channel.read();
            }
        }

DefaultChannelCongfig:
	private volatile int autoRead = 1;
	public boolean isAutoRead() {
    	return autoRead == 1;
	}

AbstractChannel:
    public Channel read() {
        pipeline.read();
        return this;
    }

最后定位到AbstractNioUnsafe,它时AbstractNioChannel的内部类

AbstractNioUnsafe:
    protected void doBeginRead() throws Exception {
        // Channel.read() or ChannelHandlerContext.read() was called
        final SelectionKey selectionKey = this.selectionKey;
        if (!selectionKey.isValid()) {
            return;
        }

        readPending = true;
	 //返回为0,因为注册的即为0
        final int interestOps = selectionKey.interestOps();
        if ((interestOps & readInterestOp) == 0) {
        	调用JDK的方法。将选择键的interest集合设为OP_ACCEPT
            selectionKey.interestOps(interestOps | readInterestOp);
        }
    }

this.selectionKey什么时候被赋的值?doRegister()被调用,即在上面注册过程中的register0方法,它的实际注册操作会调用doRegister。
在上面的创建服务器channel过程中,AbstractNioChannel的初始化时其this.readInterestOp会被赋予SelectionKey.OP_ACCEPT。

总结

总结netty启动一个服务所经过的流程
1.创建server对应的channel,创建各大组件,包括ChannelConfig,ChannelId,ChannelPipeline,ChannelHandler,Unsafe等
2.初始化server对应的channel,设置一些attr,option,以及设置子channel的attr,option
3.先将channel注册到某个EventLoop上,再调用JDK完成实际注册,过程中会触发handlerAdd与channelRegistered事件。
4.调用到jdk底层做端口绑定,并触发active事件,最后注册OP_ACCEPT事件。

猜你喜欢

转载自blog.csdn.net/sinat_34976604/article/details/84946115