代码改变世界

Netty4源码分析--启动1

2016-10-24 23:58  yrpapa  阅读(247)  评论(0)    收藏  举报

Netty服务端启动过程可以概括为注册(register)-->绑定(bind)-->激活(active)三个过程

下面分析源码:

由AbstractBootstrap.bind(SocketAddress localAddress)开始

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 Channel channel = regFuture.channel();②
        if (regFuture.cause() != null) {
            return regFuture;
        }

        if (regFuture.isDone()) {③
            // At this point we know that the registration was complete and successful.
            ChannelPromise promise = channel.newPromise();
            doBind0(regFuture, channel, localAddress, promise);⑤
            return promise; 
        } else {④// Registration future is almost always fulfilled already, but just in case it's not.
            final PendingRegistrationPromise promise = new PendingRegistrationPromise(channel);
            regFuture.addListener(new ChannelFutureListener() {
                @Override
                public void operationComplete(ChannelFuture future) throws Exception {
                    Throwable cause = future.cause();
                    if (cause != null) {
                        // Registration on the EventLoop failed so fail the ChannelPromise directly to not cause an
                        // IllegalStateException once we try to access the EventLoop of the Channel.
                        promise.setFailure(cause);
                    } else {
                        // Registration was successful, so set the correct executor to use.
                        // See https://github.com/netty/netty/issues/2586
                        promise.registered();

                        doBind0(regFuture, channel, localAddress, promise);⑤
                    }
                }
            });
            return promise;
        }
    }

① initAndRegister顾名思义,就是完成初始化channel和注册channel的工作,代码比较多,分成两个部分分析,

第一部分:初始化channel

final ChannelFuture initAndRegister() {
        Channel channel = null;
        try {
            channel = channelFactory.newChannel(); // channelFactory=ReflectiveChannelFactory, open serversocket
            init(channel);//ServerBootStrap.init()
        } 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);
        }

        ChannelFuture regFuture = config().group().register(channel);
        if (regFuture.cause() != null) {
            if (channel.isRegistered()) {
                channel.close();
            } else {
                channel.unsafe().closeForcibly();
            }
        }

        // If we are here and the promise is not failed, it's one of the following cases:
        // 1) If we attempted registration from the event loop, the registration has been completed at this point.
        //    i.e. It's safe to attempt bind() or connect() now because the channel has been registered.
        // 2) If we attempted registration from the other thread, the registration request has been successfully
        //    added to the event loop's task queue for later execution.
        //    i.e. It's safe to attempt bind() or connect() now:
        //         because bind() or connect() will be executed *after* the scheduled registration task is executed
        //         because register(), bind(), and connect() are all bound to the same thread.

        return regFuture;
    }

// NioServerSocketChannel.java
private static ServerSocketChannel newSocket(SelectorProvider provider) {
        try {
            /**
             *  Use the {@link SelectorProvider} to open {@link SocketChannel} and so remove condition in
             *  {@link SelectorProvider#provider()} which is called by each ServerSocketChannel.open() otherwise.
             *
             *  See <a href="https://github.com/netty/netty/issues/2308">#2308</a>.
             */
            return provider.openServerSocketChannel();
        } catch (IOException e) {
            throw new ChannelException(
                    "Failed to open a server socket.", e);
        }
    }

public NioServerSocketChannel() {
        this(newSocket(DEFAULT_SELECTOR_PROVIDER));
    }

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

// AbstractNioMessageChannel.java
protected AbstractNioMessageChannel(Channel parent, SelectableChannel ch, int readInterestOp) {
        super(parent, ch, readInterestOp);
    }

// AbstractNioChannel.java
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);
        }
    }

// AbstractChannel.java
protected AbstractChannel(Channel parent) {
        this.parent = parent;
        id = newId();
        unsafe = newUnsafe(); // AbstractNioMessageChannel的内部类NioMessageUnsafe
        pipeline = newChannelPipeline();
    }

// ServerBootstrap.java
void init(Channel channel) throws Exception {
        final Map<ChannelOption<?>, Object> options = options0();
        synchronized (options) {
            channel.config().setOptions(options);
        }

        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());
            }
        }

        ChannelPipeline p = channel.pipeline();

        final EventLoopGroup currentChildGroup = childGroup;
        final ChannelHandler currentChildHandler = childHandler;
        final Entry<ChannelOption<?>, Object>[] currentChildOptions;
        final Entry<AttributeKey<?>, Object>[] currentChildAttrs;
        synchronized (childOptions) {
            currentChildOptions = childOptions.entrySet().toArray(newOptionArray(childOptions.size()));
        }
        synchronized (childAttrs) {
            currentChildAttrs = childAttrs.entrySet().toArray(newAttrArray(childAttrs.size()));
        }

        p.addLast(new ChannelInitializer<Channel>() {
            @Override
            public void initChannel(Channel ch) throws Exception {
                ChannelPipeline pipeline = ch.pipeline();
                ChannelHandler handler = config.handler();
                if (handler != null) {
                    pipeline.addLast(handler);//ChannelInitializer
                }
                pipeline.addLast(new ServerBootstrapAcceptor(
                        currentChildGroup, currentChildHandler, currentChildOptions, currentChildAttrs));
            }
        });
    }

初始化channel完成了几件事情,

 打开了一个ServerSocketChannel,并且设置为非阻塞

 channel维持了一个unsafe对象,unsafe用来操作io事件

 channel维持了一个pipeline,默认为DefaultChannelPipeline,用来管理handler链,DefaultChannelPipeline首先创建DefaultChannelPipeline内部类HeadContext(Inbound&Outbound)和TailContext(Inbound),使得目前的pipeline维持的handler链为head-tail

 给pipeline添加一个类型为ChannelInitializer的handler,ChannelInitializer是一个Inbound,在注册channel时候调用,作用为,将ServerBootstrapAcceptor添加到pipeline的handler链中,此时handler链为head--ChannelInitializer--tail

 

第二部分:注册channel

// MultithreadEventLoopGroup.java
public ChannelFuture register(Channel channel) {
        return next().register(channel);
    }

public EventLoop next() {
        return (EventLoop) super.next();
    }

@Override
    public EventExecutor next() {// MultithreadEventExecutorGroup.java
        return chooser.next(); // chooser = PowerOfTowEventExecutorChooser || GenericEventExecutorChooser, power of two选择一个child
    }

// SingleThreadEventLoop.java
public ChannelFuture register(Channel channel) {
        return register(new DefaultChannelPromise(channel, this));
    }

public ChannelFuture register(final ChannelPromise promise) {
        ObjectUtil.checkNotNull(promise, "promise");
        promise.channel().unsafe().register(this, promise); // unsafe = AbstractNioMessageChannel.NioMessageUnsafe
        return promise;
    }

// AbstractChannel.java
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()) {// 保证在当前线程,否则提交一个task到eventloop所在的线程的queue
                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);
                }
            }
        }

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;

                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);
            }
        }

protected void doRegister() throws Exception {// AbstractNioChannel.java
        boolean selected = false;
        for (;;) {
            try {
                selectionKey = javaChannel().register(eventLoop().selector, 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;
                }
            }
        }
    }

注册channel完成了几项工作(以下工作都在boss线程中进行)

启动boss线程

在ServerSocketChannel上注册了selector,感兴趣的事件为0,也就是并没有感兴趣的io事件

safeSetSuccess(promise)--调用listener //TODO

 

pipeline.fireChannelRegistered()

// DefaultChannelPipeline.java
public final ChannelPipeline fireChannelRegistered() {
        AbstractChannelHandlerContext.invokeChannelRegistered(head);
        return this;
    }

 public final ChannelPipeline fireChannelUnregistered() {
        AbstractChannelHandlerContext.invokeChannelUnregistered(head);
        return this;
    }

// AbstractChannelHandlerContext.java
static void invokeChannelRegistered(final AbstractChannelHandlerContext next) {
        EventExecutor executor = next.executor();
        if (executor.inEventLoop()) {
// 此时handler链为head--ChannelInitializer--tail,调用ChannelInitializer.channelRegistered(channel)
            next.invokeChannelRegistered();
        } else {
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    next.invokeChannelRegistered();
                }
            });
        }
    }
private void invokeChannelRegistered() {
        if (invokeHandler()) {
            try {
                ((ChannelInboundHandler) handler()).channelRegistered(this);
            } catch (Throwable t) {
                notifyHandlerException(t);
            }
        } else {
            fireChannelRegistered();
        }
    }
// ChannelInitializer.java
public final void channelRegistered(ChannelHandlerContext ctx) throws Exception {
        initChannel((C) ctx.channel()); // initChannel为ServerBootstrap.init(channel)中创建的匿名类ChannelInitializer
        ctx.pipeline().remove(this); // 将ChannelInitializer从handler链中去掉,此时handler链为head--ServerBootstrapAcceptor--tail
        ctx.pipeline().fireChannelRegistered(); // pipeline().fireChannelRegistered(),通过pipeline调用为从头开始调用(inbound从head开始,outbound从tail开始)
    }

// 匿名类ChannelInitializer
p.addLast(new ChannelInitializer<Channel>() {
            @Override
            public void initChannel(Channel ch) throws Exception {
                ChannelPipeline pipeline = ch.pipeline();
                ChannelHandler handler = config.handler();
                if (handler != null) {
                    pipeline.addLast(handler);//ChannelInitializer
                }
                pipeline.addLast(new ServerBootstrapAcceptor(
                        currentChildGroup, currentChildHandler, currentChildOptions, currentChildAttrs));
            }
        });

// head.fireChannelRegistered
public final ChannelPipeline fireChannelRegistered() {
        AbstractChannelHandlerContext.invokeChannelRegistered(head);
        return this;
    }

public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
            if (firstRegistration) {
                firstRegistration = false;
                // We are now registered to the EventLoop. It's time to call the callbacks for the ChannelHandlers,
                // that were added before the registration was done.
                callHandlerAddedForAllHandlers();
            }

            ctx.fireChannelRegistered(); // 用ctx发起的调用,为调用下一个handler,此时为调用ServerBootstrapAcceptor.fireChannelRegistered
        }

// AbstractChannelHandlerContext.java
public ChannelHandlerContext fireChannelRegistered() {
        invokeChannelRegistered(findContextInbound());
        return this;
    }


// ServerBootstrapAcceptor.fireChannelRegistered
// 调用父类ChannelInboundHandlerAdapter.channelRegistered
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
        ctx.fireChannelRegistered();// 调用下一个handler,也就是tail
    }


// tail.fireChannelRegistered什么也没做

目前channel并没有激活,所以pipeline.fireChannelActive()并不会触发

至此注册工作完成了,那么bind工作是何时开始的呢???

    在标注1,final ChannelFuture regFuture = initAndRegister();①,调用后获得一个Future对象,可以获得线程的执行状态,在标注3,if (regFuture.isDone()) {③

如果至此initAndRegister执行完了,那就直接执行标注5,doBind0(regFuture, channel, localAddress, promise);⑤,否则,就将doBind0作为一个task放入bossGroup的eventloop的任务队列。在initAndRegister执行后,就会执行下一个task,也就是doBind0。

 

下一篇继续分析bind