当前位置:网站首页>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
边栏推荐
- 手写ABA遇到的坑
- sizeof关键字
- 新手程序员该不该背代码?
- GD32F4XX串口接收中断和闲时中断配置
- 数据处理技巧(7):MATLAB 读取数字字符串混杂的文本文件txt中的数据
- Management background --5, sub classification
- HDR image reconstruction from a single exposure using deep CNN reading notes
- 柔性数组到底如何使用呢?
- Research and investment strategy report of China's VOCs catalyst industry (2022 Edition)
- ResNet-RS:谷歌领衔调优ResNet,性能全面超越EfficientNet系列 | 2021 arxiv
猜你喜欢
MySQL----初识MySQL
Chapter 3: detailed explanation of class loading process (class life cycle)
The SQL response is slow. What are your troubleshooting ideas?
signed、unsigned关键字
pytorch_ Yolox pruning [with code]
Self made j-flash burning tool -- QT calls jlinkarm DLL mode
0 basic learning C language - interrupt
[leetcode] 19. Delete the penultimate node of the linked list
硬件开发笔记(十): 硬件开发基本流程,制作一个USB转RS232的模块(九):创建CH340G/MAX232封装库sop-16并关联原理图元器件
[leetcode daily clock in] 1020 Number of enclaves
随机推荐
【雅思口语】安娜口语学习记录part1
机试刷题1
Chapter 4: talk about class loader again
MySQL数据库基本操作-DML
在IPv6中 链路本地地址的优势
Applet system update prompt, and force the applet to restart and use the new version
Management background --1 Create classification
volatile关键字
硬件開發筆記(十): 硬件開發基本流程,制作一個USB轉RS232的模塊(九):創建CH340G/MAX232封裝庫sop-16並關聯原理圖元器件
0 basic learning C language - digital tube
anaconda安装第三方包
Installation and use of labelimg
Senior soft test (Information System Project Manager) high frequency test site: project quality management
云原生技术--- 容器知识点
做国外LEAD2022年下半年几点建议
剑指offer刷题记录1
void关键字
中国VOCs催化剂行业研究与投资战略报告(2022版)
[leetcode] 19. Delete the penultimate node of the linked list
Unity3d Learning Notes 6 - GPU instantiation (1)