(十四)消息发送完以后内存处理
发布日期:2021-11-18 17:47:35 浏览次数:10 分类:技术文章

本文共 3381 字,大约阅读时间需要 11 分钟。

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录


前言

在消息处理后,会有内存回收的过程this.accumulator.deallocate(batch);

private void completeBatch(RecordBatch batch, Errors error, long baseOffset, long timestamp, long correlationId, long now) {
//如果响应里面带有异常 并且 这个请求是可以重试的 if (error != Errors.NONE && canRetry(batch, error)) {
// retry log.warn("Got error produce response with correlation id {} on topic-partition {}, retrying ({} attempts left). Error: {}", correlationId, batch.topicPartition, this.retries - batch.attempts - 1, error); //重新把发送失败等着批次 加入到队列里面。 this.accumulator.reenqueue(batch, now); this.sensors.recordRetries(batch.topicPartition.topic(), batch.recordCount); } else {
//这儿过来的数据:带有异常,但是不可以重试(1:压根就不让重试2:重试次数超了) //其余的都走这个分支。 RuntimeException exception; //如果响应里面带有 没有权限的异常 if (error == Errors.TOPIC_AUTHORIZATION_FAILED) //自己封装一个异常信息(自定义了异常) exception = new TopicAuthorizationException(batch.topicPartition.topic()); else // exception = error.exception(); // tell the user the result of their request //TODO 核心代码 把异常的信息也给带过去了 //我们刚刚看的就是这儿的代码 //里面调用了用户传进来的回调函数 //回调函数调用了以后 //说明我们的一个完整的消息的发送流程就结束了。 batch.done(baseOffset, timestamp, exception); //看起来这个代码就是要回收资源的。 this.accumulator.deallocate(batch); if (error != Errors.NONE) this.sensors.recordErrors(batch.topicPartition.topic(), batch.recordCount); } if (error.exception() instanceof InvalidMetadataException) {
if (error.exception() instanceof UnknownTopicOrPartitionException) log.warn("Received unknown topic or partition error in produce request on partition {}. The " + "topic/partition may not exist or the user may not have Describe access to it", batch.topicPartition); metadata.requestUpdate(); } // Unmute the completed partition. if (guaranteeMessageOrder) this.accumulator.unmutePartition(batch.topicPartition); }

调用deallocate()

public void deallocate(RecordBatch batch) {
//从某个数据结构里面移除 已经成功处理的批次 incomplete.remove(batch); //释放内存 free.deallocate(batch.records.buffer(), batch.records.initialCapacity()); }
public void deallocate(ByteBuffer buffer, int size) {
lock.lock(); try {
//如果你还回来的内存的大小 就等于一个批次的大小, //我们的参数设置的内存是16K,你计算出来一个批次的大小也是16,申请的内存也是16k if (size == this.poolableSize && size == buffer.capacity()) {
//内存里面的东西清空 buffer.clear(); //把内存放入到内存池 this.free.add(buffer); } else {
//但是如果 我们释放的内存的大小 //不是一个批次的大小,那就把归为可用内存 //等着垃圾回收即可 this.availableMemory += size; } Condition moreMem = this.waiters.peekFirst(); if (moreMem != null) //释放了内存(或者是还了内存以后) //都会唤醒等待内存的线程。 //接下来是不是还是要唤醒正在等待分配内存的线程。 moreMem.signal(); } finally {
lock.unlock(); } }

转载地址:https://blog.csdn.net/weixin_37850264/article/details/112385425 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:(十五)处理有异常消息
下一篇:(十三)处理暂存状态的响应消息

发表评论

最新留言

逛到本站,mark一下
[***.202.152.39]2024年04月07日 23时23分26秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

(精华)2021年3月31日 redis数据库 redis数据库的配置 2019-04-26
(精华)2021年3月31日 redis数据库 服务端和客户端的命令 2019-04-26
(更新时间)2021年4月9日 Django框架 Cookie的使用 2019-04-26
(更新时间)2021年4月10日 Django框架 Session和Redis的使用 2021-06-29
(更新时间)2021年4月10日 Django框架 类视图 2021-06-29
(更新时间)2021年6月7日 网络安全 windows系统批处理文件编写 2021-06-29
(更新时间)2021年6月13日 网络安全 用户与组管理 2021-06-29
(更新时间)2021年6月13日 网络安全 服务器远程管理 2021-06-29
(更新时间)2021年4月17日 Django框架 itsdangerous加解密库的使用 2021-06-29
(更新时间)2021年6月13日 网络安全 破解windows系统密码 2021-06-29
(更新时间)2021年6月22日 网络安全 端口和端口入侵服务详解 2021-06-29
(更新时间)2021年6月13日 网络安全 NTFS安全权限 2021-06-29
(更新时间)2021年6月14日 网络安全 文件共享服务器 2019-04-26
(更新时间)2021年6月14日 网络安全 DHCP部署与安全 2019-04-26
(更新时间)2021年6月14日 网络安全 DNS部署与安全 2019-04-26
(更新时间)2021年4月22日 网络安全 IIS之web服务器 2019-04-26
(更新时间)2021年4月22日 网络安全 IIS之FTP服务器 2019-04-26
(更新时间)2021年4月22日 网络安全 域的使用 2019-04-26
(更新时间)2021年4月24日 网络安全 PKI 2019-04-26
(更新时间)2021年4月25日 网络安全 渗透测试 2019-04-26