首页  

Kafka网络模型     所属分类 kafka 浏览量 1094
基于NIO的Reactor多线程模型

An NIO socket server. 
The threading model is 1 Acceptor thread that handles new connections
Acceptor has N Processor threads that each have their own selector and read requests from sockets
M Handler threads that handle requests and produce responses back to the processor threads for writing.

一个Acceptor线程负责处理新连接请求
N个Processor线程,每个都有自己的Selector,负责从socket中读取请求和将返回结果写回
M个Handler线程,负责处理请求,并且将结果返回给Processor

kafka 1.1.0  broker  jstack 信息
kafka-socket-acceptor-ListenerName(PLAINTEXT)-PLAINTEXT-9092
kafka-network-thread-0-ListenerName(PLAINTEXT)-PLAINTEXT-$id
kafka-request-handler-$id



# The number of threads that the server uses for receiving requests from the network and sending responses to the network
num.network.threads=3

# The number of threads that the server uses for processing requests, which may include disk I/O
num.io.threads=8

# The number of queued request allowed before blocking the network threads
#queued.max.requests

# The send buffer (SO_SNDBUF) used by the socket server
socket.send.buffer.bytes=102400

# The receive buffer (SO_RCVBUF) used by the socket server
socket.receive.buffer.bytes=102400

# The maximum size of a request that the socket server will accept (protection against OOM)
socket.request.max.bytes=104857600


Acceptor 
1个接收线程,负责监听新的连接请求,注册OP_ACCEPT 事件,将新的连接按照 "round robin" 方式交给对应的 Processor 线程处理

Processor 
N 个处理器线程,每个 Processor 都有自己的 selector,向 Acceptor 分配的 SocketChannel 注册相应的 OP_READ 事件
num.networker.threads


KafkaRequestHandler 
M个请求处理线程,包含在线程池 KafkaRequestHandlerPool内部
从RequestChannel的全局请求队列 requestQueue中获取请求数据并交给KafkaApis处理
num.io.threads

RequestChannel 
Kafka服务端的请求通道,包含一个全局的请求队列 requestQueue和多个与Processor处理器相对应的响应队列responseQueue
提供给Processor与请求处理线程KafkaRequestHandler和KafkaApis交换数据 

NetworkClient
Java NIO 封装
KafkaProducer的send方法主要调用NetworkClient完成消息发送

SocketServer 
启动一个Acceptor线程和多个Processor线程
典型的Reactor多线程模式,将接收客户端请求和处理请求分离

KafkaServer 
代表一个Kafka Broker的实例  实例启动的入口 startup


KafkaApis 
业务逻辑处理Api,负责处理不同类型的请求;比如  发送消息 获取消息偏移量offset  和 处理心跳请求 等

上一篇     下一篇
kafka消费机制要点

kafka消费端核心参数

kafka消费分区重平衡机制

java nio 编程模型简介

kafka Coordinator 简介

云原生时代的Java