Netty--主從Reactor多線程模式的源碼實現
總覽

EventLoopGroup到底是什么?
EventLoopGroup是一個存儲EventLoop的容器,同時他應該具備線程池的功能。
由於EventLoopGroup間接繼承ScheduledExecutorService接口,因此其實現類應該具備線程池的功能。
看一下NioEventLoopGroup的核心屬性
// 默認的線程池大小
private static final int DEFAULT_EVENT_LOOP_THREADS;
static {
DEFAULT_EVENT_LOOP_THREADS = Math.max(1, SystemPropertyUtil.getInt(
"io.netty.eventLoopThreads", NettyRuntime.availableProcessors() * 2)); // CPU核數 x 2
}
// 存儲EventLoop的數組
private final EventExecutor[] children;
構造方法
// 如果傳入的nThread為空,那么使用默認的線程池大小(CPU核數 x 2)
protected MultithreadEventLoopGroup(int nThreads, ThreadFactory threadFactory, Object... args) {
super(nThreads == 0 ? DEFAULT_EVENT_LOOP_THREADS : nThreads, threadFactory, args);
}
// 最終的構建方法
protected MultithreadEventExecutorGroup(int nThreads, Executor executor,
EventExecutorChooserFactory chooserFactory, Object... args) {
// ......省略
children = new EventExecutor[nThreads]; // 初始化EventExecutor數組的大小
for (int i = 0; i < nThreads; i ++) {
boolean success = false;
try {
children[i] = newChild(executor, args); // 初始化EventLoop
success = true;
} catch (Exception e) {
throw new IllegalStateException("failed to create a child event loop", e);
} finally {
// ......省略
}
}
// ......省略
}
當創建NioEventLoopGroup實例后,就已經初始化好其EventExecutor數組的大小以及其存儲的EventLoop。
EventLoop又是什么?
EventLoop是EventExecutor的子接口。
看一下NioEventLoop的核心屬性
private Selector selector;
private volatile Thread thread;
private final Executor executor;
private final Queue<Runnable> taskQueue;
static final int DEFAULT_MAX_PENDING_EXECUTOR_TASKS = Math.max(16,
SystemPropertyUtil.getInt("io.netty.eventexecutor.maxPendingTasks", Integer.MAX_VALUE));
每一個NioEventLoop都有一個Selector、Thread、Executor、Queue屬性,當創建EventLoop實例后其thread屬性仍為NULL,還沒有創建線程。
Channel
看一下Channel的核心屬性
private volatile EventLoop eventLoop; // 指向EventLoop的引用
每一個Channel都有一個指向EventLoop的引用,也就是說每一個Channel都會與一個EventLoop進行綁定,其eventLoop()方法返回此Channel綁定的EventLoop。
Netty啟動服務器后的核心流程
// 創建bossGroup和workerGroup
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
// 綁定端口,啟動服務
serverBootstrap.bind(8888).sync();
AbstarctBootstrap的doBind()方法
private ChannelFuture doBind(final SocketAddress localAddress) {
final ChannelFuture regFuture = initAndRegister(); // 初始化Channel並將Channel注冊到bossGroup中的EventLoop中的Selector
final Channel channel = regFuture.channel();
if (regFuture.cause() != null) {
return regFuture;
}
if (regFuture.isDone()) {
ChannelPromise promise = channel.newPromise();
doBind0(regFuture, channel, localAddress, promise);
return promise;
} else {
final PendingRegistrationPromise promise = new PendingRegistrationPromise(channel);
regFuture.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception { // 當initAndRegister()方法完成后,由IO線程自動調用
Throwable cause = future.cause();
if (cause != null) {
promise.setFailure(cause);
} else {
promise.registered();
doBind0(regFuture, channel, localAddress, promise); // 進行綁定操作
}
}
});
return promise;
}
}
final ChannelFuture initAndRegister() {
Channel channel = null;
try {
channel = channelFactory.newChannel(); // 創建NioServerSocketChannel
init(channel); // 初始化Channel
} catch (Throwable t) {
if (channel != null) {
channel.unsafe().closeForcibly();
return new DefaultChannelPromise(channel, GlobalEventExecutor.INSTANCE).setFailure(t);
}
return new DefaultChannelPromise(new FailedChannel(), GlobalEventExecutor.INSTANCE).setFailure(t);
}
// config()方法返回AbstractBootStrap
// AbstractBootStrap存儲bossGroup,ServerBootStrap存儲workerGroup
ChannelFuture regFuture = config().group().register(channel);
if (regFuture.cause() != null) {
if (channel.isRegistered()) {
channel.close();
} else {
channel.unsafe().closeForcibly();
}
}
return regFuture;
}
EventLoopGroup的register()方法會通過選擇器從EventLoopGroup中取出一個EventLoop,然后調用EventLoop的register()方法
// EventLoopGroup的register()方法
public ChannelFuture register(Channel channel) {
return next().register(channel); // 調用EventLoop的register()方法
}
/**
* 通過選擇器從EventLoopGroup中取出一個EventLoop
*/
public EventExecutor next() {
return chooser.next();
}
SingleThreadEventLoop的register()方法
public ChannelFuture register(final ChannelPromise promise) {
ObjectUtil.checkNotNull(promise, "promise");
promise.channel().unsafe().register(this, promise); // 調用Channel的register()方法,傳遞當前對象,即當前EventLoop實例
return promise;
}
AbstarctChannel的register()方法
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; // 初始化Channel的EventLoop屬性
// 第一次調用時,EventLoop中的thread為空,因此inEventLoop()返回false
if (eventLoop.inEventLoop()) {
register0(promise);
} else {
try {
eventLoop.execute(new Runnable() { // 調用EventLoop的execute()方法,並將注冊操作作為一個任務,放入到Runnable實例中
@Override
public void run() {
// register0()方法最終將會調用doRegister()方法,將Channel注冊到EventLoop中的Selector
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);
}
}
}
SingleThreadEventExecutor的execute()方法
public void execute(Runnable task) {
if (task == null) {
throw new NullPointerException("task");
}
boolean inEventLoop = inEventLoop();
addTask(task); // 將任務放入到當前EventLoop的任務隊列中
if (!inEventLoop) {
startThread(); // 開啟一個線程,最終調用doStartThread()方法
if (isShutdown()) {
boolean reject = false;
try {
if (removeTask(task)) {
reject = true;
}
} catch (UnsupportedOperationException e) {
}
if (reject) {
reject();
}
}
}
if (!addTaskWakesUp && wakesUpForTask(task)) {
wakeup(inEventLoop);
}
}
SingleThreadEventExecutor的doStartThread()方法
private void doStartThread() {
assert thread == null; // 當前EventLoop中的thread為空,輸出結果肯定為true
executor.execute(new Runnable() { // 向線程池中提交一個任務,到這里才創建線程同時異步執行
@Override
public void run() {
thread = Thread.currentThread(); // 初始化EventLoop中的thread屬性,即線程池中執行該任務的線程
if (interrupted) {
thread.interrupt();
}
boolean success = false;
updateLastExecutionTime();
try {
SingleThreadEventExecutor.this.run(); // 核心方法(當前對象為NioEventLoop)
success = true;
} catch (Throwable t) {
logger.warn("Unexpected exception from an event executor: ", t);
} finally {
// ......省略
}
}
});
}
NioEventLoop的run()方法
protected void run() {
for (;;) { // 死循環
try {
try {
switch (selectStrategy.calculateStrategy(selectNowSupplier, hasTasks())) {
case SelectStrategy.CONTINUE:
continue;
case SelectStrategy.BUSY_WAIT:
case SelectStrategy.SELECT:
select(wakenUp.getAndSet(false));
if (wakenUp.get()) {
selector.wakeup();
}
default:
}
} catch (IOException e) {
rebuildSelector0();
handleLoopException(e);
continue;
}
cancelledKeys = 0;
needsToSelectAgain = false;
final int ioRatio = this.ioRatio;
if (ioRatio == 100) {
try {
processSelectedKeys(); // 處理Channel的就緒事件,最終調用processSelectedKeysOptimized()方法
} finally {
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);
}
try {
if (isShuttingDown()) {
closeAll();
if (confirmShutdown()) {
return;
}
}
} catch (Throwable t) {
handleLoopException(t);
}
}
}
private void processSelectedKeysOptimized() {
for (int i = 0; i < selectedKeys.size; ++i) { // 遍歷就緒的Channel對應的SelectionKey集合,如果Channel沒有事件就緒則集合為空
final SelectionKey k = selectedKeys.keys[i];
selectedKeys.keys[i] = null;
final Object a = k.attachment();
if (a instanceof AbstractNioChannel) {
processSelectedKey(k, (AbstractNioChannel) a);
} else {
@SuppressWarnings("unchecked")
NioTask<SelectableChannel> task = (NioTask<SelectableChannel>) a;
processSelectedKey(k, task);
}
if (needsToSelectAgain) {
selectedKeys.reset(i + 1);
selectAgain();
i = -1;
}
}
}
NioEventLoop的processSelectedKey()方法
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) {
return;
}
if (eventLoop != this || eventLoop == null) {
return;
}
unsafe.close(unsafe.voidPromise());
return;
}
try {
int readyOps = k.readyOps(); // 就緒的事件
if ((readyOps & SelectionKey.OP_CONNECT) != 0) { // 連接就緒
int ops = k.interestOps();
ops &= ~SelectionKey.OP_CONNECT;
k.interestOps(ops);
unsafe.finishConnect(); // 處理連接就緒
}
if ((readyOps & SelectionKey.OP_WRITE) != 0) { // 寫就緒
ch.unsafe().forceFlush(); // 處理寫就緒
}
if ((readyOps & (SelectionKey.OP_READ | SelectionKey.OP_ACCEPT)) != 0 || readyOps == 0) { // 接收和讀就緒
unsafe.read(); // 處理接收和讀就緒
}
} catch (CancelledKeyException ignored) {
unsafe.close(unsafe.voidPromise());
}
}
當EventLoop中的線程處理完Channel的就緒事件后將會執行保存在任務隊列中的所有任務,此時注冊任務將被執行
private void register0(ChannelPromise promise) {
try {
if (!promise.setUncancellable() || !ensureOpen(promise)) {
return;
}
boolean firstRegistration = neverRegistered;
doRegister(); // 執行注冊操作
neverRegistered = false;
registered = true;
pipeline.invokeHandlerAddedIfNeeded();
safeSetSuccess(promise); // ChannelFuture設置成成功狀態,同時isDone()方法將返回true
pipeline.fireChannelRegistered();
if (isActive()) {
if (firstRegistration) {
pipeline.fireChannelActive();
} else if (config().isAutoRead()) {
beginRead();
}
}
} catch (Throwable t) {
closeForcibly();
closeFuture.setClosed();
safeSetFailure(promise, t); // ChannelFuture設置成失敗狀態,同時isDone()方法將返回true
}
}
AbstractNioChannel的doRegister()方法
protected void doRegister() throws Exception {
boolean selected = false;
for (;;) {
try {
// 將Channel注冊到EventLoop中的Selector
selectionKey = javaChannel().register(eventLoop().unwrappedSelector(), 0, this); // 將Channel注冊到Selector當中,當Channel執行完相應操作后,再向Selector傳遞需要監聽此Channel的事件類型
return;
} catch (CancelledKeyException e) {
if (!selected) {
eventLoop().selectNow();
selected = true;
} else {
throw e;
}
}
}
}
回到一開始的AbstarctBootstrap的doBind()方法
private ChannelFuture doBind(final SocketAddress localAddress) {
final ChannelFuture regFuture = initAndRegister();
final Channel channel = regFuture.channel();
if (regFuture.cause() != null) {
return regFuture;
}
if (regFuture.isDone()) { // 由於注冊任務是異步執行的,此時任務還未被執行,因此isDone()方法將返回false
ChannelPromise promise = channel.newPromise();
doBind0(regFuture, channel, localAddress, promise);
return promise;
} else {
final PendingRegistrationPromise promise = new PendingRegistrationPromise(channel);
regFuture.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception { // 當注冊任務被執行完畢后,由IO線程自動
Throwable cause = future.cause();
if (cause != null) {
promise.setFailure(cause);
} else {
promise.registered();
doBind0(regFuture, channel, localAddress, promise); // 進行綁定操作
}
}
});
return promise;
}
}
private static void doBind0(
final ChannelFuture regFuture, final Channel channel,
final SocketAddress localAddress, final ChannelPromise promise) {
channel.eventLoop().execute(new Runnable() { // 同樣的,獲取Channel綁定的EventLoop,調用EventLoop的execute()方法,並將綁定操作作為一個任務,放入到Runnable實例中
@Override
public void run() {
if (regFuture.isSuccess()) {
channel.bind(localAddress, promise).addListener(ChannelFutureListener.CLOSE_ON_FAILURE);
} else {
promise.setFailure(regFuture.cause());
}
}
});
}
public void execute(Runnable task) {
if (task == null) {
throw new NullPointerException("task");
}
boolean inEventLoop = inEventLoop(); // 由於當前方法是被處理注冊任務的那個線程調用的,即EventLoop中的那個線程,因此inEventLoop()方法返回true
addTask(task); // 將任務放入到隊列中,等待被執行
if (!inEventLoop) { // 不會執行,到目前為止仍然只開啟了一個線程
startThread();
if (isShutdown()) {
boolean reject = false;
try {
if (removeTask(task)) {
reject = true;
}
} catch (UnsupportedOperationException e) {
}
if (reject) {
reject();
}
}
}
if (!addTaskWakesUp && wakesUpForTask(task)) {
wakeup(inEventLoop);
}
}
這就是Netty啟動服務端后的核心流程
1.創建ServerSocketChannel並進行初始化。
2.調用bossGroup的register()方法,方法內部通過選擇器從bossGroup中取出一個EventLoop,然后調用EventLoop的register()方法。
3.最終調用Channel的register()方法,方法中初始化Channel的eventLoop屬性,然后將Channel注冊到bossGroup中的EventLoop中的Selector作為一個任務,放入到Runnable實例中,然后調用EventLoop的execute(Runnable)方法。
4.execute()方法將任務放入到任務隊列當中,然后向線程池中提交一個任務,此時才創建一個線程,同時初始化EventLoop中的thread屬性。
5.任務中初始化EventLoop的thread屬性,然后調用NioEventLoop的run()方法,死循環去處理Channel的就緒事件以及執行保存在任務隊列中的所有任務。
6.當注冊任務被執行完畢后,該線程會回調ChannelFutureListener中的operationComplete()方法,將綁定操作作為一個任務,然后調用EventLoop的execute(Runnable)方法。
7.重復第4步驟,將任務放入到任務隊列中,由於當前線程就是EventLoop中的thread,因此inEventLoop()方法返回true,不會向線程池中提交任務,任務等待被EventLoop中的線程執行。
Netty處理接收和讀就緒事件的核心流程
BossGroup中的EventLoop中的Thread正在死循環的處理Channel的就緒事件以及執行保存在任務隊列中的所有任務
// 創建一個連接
SocketChannel socketChannel = SocketChannel.open();
socketChannel.connect(new InetSocketAddress(InetAddress.getLocalHost(), 8888));
BossGroup中的EventLoop中的Selector監聽到ServerSocketChannel有接收就緒事件
// NioEventLoop的processSelectedKey()方法
if ((readyOps & (SelectionKey.OP_READ | SelectionKey.OP_ACCEPT)) != 0 || readyOps == 0) {
unsafe.read();
}
public void read() {
assert eventLoop().inEventLoop(); // 肯定是true
final ChannelConfig config = config();
final ChannelPipeline pipeline = pipeline();
final RecvByteBufAllocator.Handle allocHandle = unsafe().recvBufAllocHandle();
allocHandle.reset(config);
boolean closed = false;
Throwable exception = null;
try {
try {
do {
int localRead = doReadMessages(readBuf); // 讀取Channel中的數據
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;
pipeline.fireChannelRead(readBuf.get(i)); // 最終會調用ServerBootStrap的register()方法
}
readBuf.clear();
allocHandle.readComplete();
pipeline.fireChannelReadComplete();
if (exception != null) {
closed = closeOnReadError(exception);
pipeline.fireExceptionCaught(exception);
}
if (closed) {
inputShutdown = true;
if (isOpen()) {
close(voidPromise());
}
}
} finally {
if (!readPending && !config.isAutoRead()) {
removeReadOp();
}
}
}
NioServerSocketChannel的doReadMessage()方法
protected int doReadMessages(List<Object> buf) throws Exception {
SocketChannel ch = SocketUtils.accept(javaChannel()); // 接收連接,調用ServerSocketChannel的accept()方法
try {
if (ch != null) {
buf.add(new NioSocketChannel(this, ch)); // 將接收到連接放入到buffer中
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;
}
ServerBootstrap的register()方法
public void channelRead(ChannelHandlerContext ctx, Object msg) {
final Channel child = (Channel) msg;
child.pipeline().addLast(childHandler);
setChannelOptions(child, childOptions, logger);
setAttributes(child, childAttrs);
try {
// 調用workerGroup的register()方法,方法內部通過選擇器從workerGroup中取出一個EventLoop,然后調用EventLoop的register()方法,最終調用AbstractChannel的register()方法
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);
}
}
AbstractChannel的register()方法
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; // 初始化Channel的eventLoop屬性
// 如果取出的是新的EventLoop,那么其thread屬性為空,當前線程總是bossGroup中的EventLoop中的thread,因此inEventLoop()返回false。
// 如果取出的是舊的EventLoop,那么其thread屬性不為空,當前線程總是bossGroup中的EventLoop中的thread,因此inEventLoop()返回false。
if (eventLoop.inEventLoop()) { // 總是返回false,當前線程總是bossGroup中的EventLoop中的thread,肯定與workerGroup中的任意一個EventLoop中的thread都不相等。
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);
}
}
}
public void execute(Runnable task) {
if (task == null) {
throw new NullPointerException("task");
}
boolean inEventLoop = inEventLoop();
addTask(task); // 將任務放入到隊列當中
if (!inEventLoop) {
startThread(); // 不管是新的還是舊的EventLoop都會調用該方法
if (isShutdown()) {
boolean reject = false;
try {
if (removeTask(task)) {
reject = true;
}
} catch (UnsupportedOperationException e) {
}
if (reject) {
reject();
}
}
}
if (!addTaskWakesUp && wakesUpForTask(task)) {
wakeup(inEventLoop);
}
}
private void startThread() {
if (state == ST_NOT_STARTED) { // 如果取出的是舊的EventLoop,那么其thread屬性本身就不為空,因此其state屬性就不等於ST_NOT_STARTED,因此不會開啟新的線程,注冊任務等待被EventLoop中的線程執行
if (STATE_UPDATER.compareAndSet(this, ST_NOT_STARTED, ST_STARTED)) {
boolean success = false;
try {
doStartThread(); // 取出的是新的EventLoop,其thread屬性為null,同時其state等於ST_NOT_STARTED,因此需要開啟線程,向線程池中提交一個任務,死循環去處理Channel的就緒事件以及執行保存在任務隊列中的所有任務
success = true;
} finally {
if (!success) {
STATE_UPDATER.compareAndSet(this, ST_STARTED, ST_NOT_STARTED);
}
}
}
}
}
這就是Netty處理接收和讀就緒事件的核心流程
1.客戶端建立SocketChannel並進行連接。
2.bossGroup中的EventLoop中的Selector監聽到ServerSocketChannel有接收就緒事件。
3.接收連接,最終調用workerGroup的register()方法,方法內部通過選擇器從workGroup中取出一個EventLoop,然后調用EventLoop的register()方法。
4.最終調用Channel的register()方法,方法中初始化Channel的eventLoop屬性,然后將Channel注冊到bossGroup中的EventLoop中的Selector作為一個任務,放入到Runnable實例中,然后調用EventLoop的execute(Runnable)方法。
5.execute()方法將任務放入到任務隊列中,如果取出的是新的EventLoop,那么其thread屬性為空,此時將會開啟線程,向線程池中提交一個任務,死循環去處理Channel的就緒事件以及執行保存在任務隊列中的所有任務,如果取出的是舊的EventLoop,那么其thread屬性不為空,任務等待被EventLoop中的線程執行。
總結
1.當創建NioEventLoopGroup實例后,就已經初始化好其EventExecutor數組的大小以及其存儲的EventLoop。
2.每一個NioEventLoop都有一個Selector、Thread、Executor、Queue屬性,當創建NioEventLoop實例后,其thread屬性仍為空。
3.每一個Channel都會與一個EventLoop進行綁定,其eventLoop()方法返回其綁定的EventLoop,同時該Channel會注冊到其綁定的EventLoop的Selector中。
4.EventLoopGroup的register()方法會通過選擇器從EventLoopGroup中取出一個EventLoop,然后調用EventLoop的register()方法。
5.EventLoop的execute()方法會將任務放入到任務隊列當中,如果inEventLoop()方法返回false同時其thread屬性為空,則創建一個線程,向線程池中提交一個任務(任務中初始化EventLoop中的thread屬性,然后死循環去處理Channel的就緒事件以及執行保存在任務隊列中的所有任務)
