Kafka系列文章
基于Kafka2.1解读Producer原理
基于Kafka2.1解读Consumer原理
Kafka服务端NIO操作原理解析(一)
文章目录
前言
废话不多说,继续上一篇文章,我们继续基于Kafka3.7解读服务端的nio原理

一、基本认知
可以看到Acceptor和Processor都是线程,所以实际Kafka服务端在启动(执行startup)方法之后,会基于一个Acceptor多个Processor的模式启动,并把这多个线程执行起来。
二、Acceptor的主体流程

对于Acceptor来说,主要就是通过selector监听accept事件,然后选择一个processor来进行后续操作
2.1 run方法源码
/**
* Accept loop that checks for new connection attempts
*/
override def run(): Unit = {
serverChannel.register(nioSelector, SelectionKey.OP_ACCEPT)
try {
while (shouldRun.get()) {
try {
acceptNewConnections()
closeThrottledConnections()
}
catch {
// We catch all the throwables to prevent the acceptor thread from exiting on exceptions due
// to a select operation on a specific channel or a bad request. We don't want
// the broker to stop responding to requests from other clients in these scenarios.
case e: ControlThrowable => throw e
case e: Throwable => error("Error occurred", e)
}
}
} finally {
debug("Closing server socket, selector, and any throttled sockets.")
CoreUtils.swallow(serverChannel.close(), this, Level.ERROR)
CoreUtils.swallow(nioSelector.close

最低0.47元/天 解锁文章
977

被折叠的 条评论
为什么被折叠?



