rocketMQ学习笔记——消息读写与存储结构

本文介绍了RocketMQ中commitLog和consumeQueue的存储结构及工作原理。commitLog以1G文件存储消息,consumeQueue按topic分类,每个文件大小为600w字节。消息消费时,从consumeQueue获取offset,再从commitLog读取消息。ReputMessageService负责异步构建consumeQueue,长轮询模式下在构建时发出通知。consumeQueue通过offsetTable管理消费状态,首次消费时获取队列最小偏移量。RocketMQ利用PageCache和顺序读取优化consumeQueue的读性能,减少随机读取对性能的影响。
摘要由CSDN通过智能技术生成

在这里插入图片描述

在rocketMQ中, 消息存储在硬盘中, commitLog与consumeQueue在结构概念上是一个队列, 但是在具体实现上是用多文件存储结构

commitLog的文件默认大小为1G, consumeQueue的文件默认大小为600w字节, 即可以存放30w个条目

    public int getMappedFileSizeConsumeQueue() {
   
		// mappedFileSizeConsumeQueue = 300000 * ConsumeQueue.CQ_STORE_UNIT_SIZE
        // ConsumeQueue.CQ_STORE_UNIT_SIZE = 20
        int factor = (int) Math.ceil(this.mappedFileSizeConsumeQueue / (ConsumeQueue.CQ_STORE_UNIT_SIZE * 1.0));
        return (int) (factor * ConsumeQueue.CQ_STORE_UNIT_SIZE);
    }

commitLog

首先看看commitLog的文件存储结构

在CommitLog类中有一个load方法, 在该方法中加载了mappedFileQueue, 即rocketMQ的逻辑队列

    public boolean load() {
   
        boolean result = this.mappedFileQueue.load();
        log.info("load commit log " + (result ? "OK" : "Failed"));
        return result;
    }

在mappedFileQueue的load方法中:

  1. 读取根目录文件夹
  2. 遍历文件排序
  3. 用MappedFile类读取文件夹下的文件并且创建fileChannel
    private String storePathCommitLog = System.getProperty("user.home") + File.separator + "store"
        + File.separator + "commitlog";
    // CommitLog file size,default is 1G
    private int mappedFileSizeCommitLog = 1024 * 1024 * 1024;

mappedFile对象就是commitLog对硬盘的操作对象

在消费流程中, 消费中订阅topic从topic下的消费队列中获取offset, 然后根据offset从commitLog中获取消息

    public SelectMappedBufferResult getData(final long offset, final boolean returnFirstOnNotFound) {
   
        // 这里的大小是默认配置的1g
        int mappedFileSize = this.defaultMessageStore.getMessageStoreConfig().getMappedFileSizeCommitLog(
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值