当前位置:网站首页>Jafka来源分析——Processor
Jafka来源分析——Processor
2022-07-06 15:10:00 【全栈程序员站长】
大家好,又见面了,我是全栈君。
Jafka Acceptor接受client而建立后的连接请求,Acceptor会将Socket连接交给Processor进行处理。Processor通过下面的处理步骤进行client请求的处理:
1. 读取client请求。
2. 依据client请求类型的不同,调用对应的处理函数进行处理。
Processor读取client请求是一个比較有意思的事情,须要考虑两个方面的事情:第一,请求规则(Processor须要依照一定的规则进行请求的解析)。第二,怎样确定一次请求的读取已经结束(由于是非堵塞连接,很有可能第一次读操作读取了请求的一部分数据,第二次到第N次读取才干把整个client请求读取完整)。以下我们具体解析一下client请求的格式。
client请求首先包括一个int,该int指明本次client请求的大小(size)。随后,请求包括一个两个byte(short)的请求类型(请求类型包括:CreaterRequest、DeleterRequest、FetchRequest、MultiFetchRequest、MultiProducerRequest、OffsetRequest和ProducerRequest。然后每种请求类型有固定的格式。下图具体说明了ProducerRequest的格式:
知道了上面的格式之后,问题二(怎样确定一次请求已经读取完毕)就非常easy攻克了。
首先为“请求长度”分配一个4byte的ByteBuffer,直到该Buffer读满,否则说明长度一直没有读取完毕。“请求长度”读取完毕后,为请求分配一个“请求长度”大小的ByteBuffer,直到该Buffer读满则说明一次请求读取完毕。读取完毕后,依据“请求类型”调用对应的处理函数(Handler)进行处理。在jafka中,上述的两个Buffer在类BoundedByteBufferReceive中进行声明和管理。Processor接收到Acceptor分配的socket连接后。会为socke连接建立一个BoundedByteBufferReceive并将其与socket连接进行绑定。每当该socket连接“可读”时。将BoundedByteBufferReceive拿出来从上次读取的基础上继续读取。直到一次请求彻底读取完毕,详细过程如以下代码(Processor.read)所看到的:
private void read(SelectionKey key) throws IOException {
SocketChannel socketChannel = channelFor(key);
Receive request = null;
request = new BoundedByteBufferReceive(maxRequestSize);
key.attach(request);
} else {
request = (Receive) key.attachment();
}
int read = request.readFrom(socketChannel);
stats.recordBytesRead(read);
if (read < 0) {
close(key);
} else if (request.complete()) {
Send maybeResponse = handle(key, request);
key.attach(null);
// if there is a response, send it, otherwise do nothing
if (maybeResponse != null) {
key.attach(maybeResponse);
key.interestOps(SelectionKey.OP_WRITE);
}
} else {
// more reading to be done
key.interestOps(SelectionKey.OP_READ);
getSelector().wakeup();
if (logger.isTraceEnabled()) {
logger.trace("reading request not been done. " + request);
}
}
}
BoundedByteBufferReceive.readFrom的实现详细例如以下:主要是申请两个Buffer并不断的读取数据。
public int readFrom(ReadableByteChannel channel) throws IOException {
expectIncomplete();
int read = 0;
if (sizeBuffer.remaining() > 0) {
read += Utils.read(channel, sizeBuffer);
}
if (contentBuffer == null && !sizeBuffer.hasRemaining()) {
sizeBuffer.rewind();
int size = sizeBuffer.getInt();
if (size <= 0) {
throw new InvalidRequestException(...);
}
if (size > maxRequestSize) {
final String msg = "Request of length %d is not valid, it is larger than the maximum size of %d bytes.";
throw new InvalidRequestException(format(msg, size, maxRequestSize));
}
contentBuffer = byteBufferAllocate(size);
}
//
if (contentBuffer != null) {
read = Utils.read(channel, contentBuffer);
//
if (!contentBuffer.hasRemaining()) {
contentBuffer.rewind();
setCompleted();
}
}
return read;
}
读取完毕后,Processor会解析“请求类型”,依据请求类型的不同调用不同的Handler处理对应于该请求。
版权声明:本文博主原创文章,博客,未经同意不得转载。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/116982.html原文链接:https://javaforall.cn
边栏推荐
- Export MySQL table data in pure mode
- 柔性数组到底如何使用呢?
- Mise en place d'un environnement de développement OP - tee basé sur qemuv8
- [leetcode daily clock in] 1020 Number of enclaves
- Seata aggregates at, TCC, Saga and XA transaction modes to create a one-stop distributed transaction solution
- MySQL教程的天花板,收藏好,慢慢看
- 二叉(搜索)树的最近公共祖先 ●●
- 2022年6月国产数据库大事记-墨天轮
- extern关键字
- 网络基础入门理解
猜你喜欢
[Digital IC hand tearing code] Verilog burr free clock switching circuit | topic | principle | design | simulation
Leetcode exercise - Sword finger offer 26 Substructure of tree
NPDP certification | how do product managers communicate across functions / teams?
[linear algebra] determinant of order 1.3 n
Assembly and interface technology experiment 5-8259 interrupt experiment
Learn the principle of database kernel from Oracle log parsing
leetcode:面试题 17.24. 子矩阵最大累加和(待研究)
2500 common Chinese characters + 130 common Chinese and English characters
Seata聚合 AT、TCC、SAGA 、 XA事务模式打造一站式的分布式事务解决方案
Sword finger offer question brushing record 1
随机推荐
Classification, function and usage of MySQL constraints
关于声子和热输运计算中BORN电荷和non-analytic修正的问题
[Digital IC hand tearing code] Verilog burr free clock switching circuit | topic | principle | design | simulation
uniapp设置背景图效果demo(整理)
Crawler obtains real estate data
Research and investment strategy report of China's VOCs catalyst industry (2022 Edition)
CCNA Cisco network EIGRP protocol
case 关键字后面的值有什么要求吗?
中国1,4-环己烷二甲醇(CHDM)行业调研与投资决策报告(2022版)
Aardio - 利用customPlus库+plus构造一个多按钮组件
Hardware development notes (10): basic process of hardware development, making a USB to RS232 module (9): create ch340g/max232 package library sop-16 and associate principle primitive devices
General implementation and encapsulation of go diversified timing tasks
sizeof关键字
做国外LEAD2022年下半年几点建议
空结构体多大?
中国VOCs催化剂行业研究与投资战略报告(2022版)
2022年6月国产数据库大事记-墨天轮
【雅思口语】安娜口语学习记录part1
【踩坑合辑】Attempting to deserialize object on CUDA device+buff/cache占用过高+pad_sequence
0 basic learning C language - digital tube