ROcketMQ源码分析(十一)之CommitLog

本文主要分析RocketMQ的CommitLog,它是保存消息元数据的地方,记录每条消息的消费状态。内容涉及CommitLog的存储目录、构造方法、消息追加以及如何获取偏移量。通过自旋锁保证并发安全性。
摘要由CSDN通过智能技术生成

版本

  1. 基于rocketmq-all-4.3.1版本

简介

  1. CommitLog是保存消息元数据的地方,所有Topic的消息到达Broker后都会保存到CommitLog文件。CommitLog 里面记录了每条消息的消费情况,是否被消费,由谁消费,该消息是否持久化等信息

  2. CommitLog文件的存储目录默认为**${ROCKET_HOME}/store/commitlog**,默认大小是1G

  3. CommitLog核心属性

    public class CommitLog {
         
        // Message's MAGIC CODE daa320a7
        public final static int MESSAGE_MAGIC_CODE = -626843481;
        private static final InternalLogger log = InternalLoggerFactory.getLogger(LoggerName.STORE_LOGGER_NAME);
        // End of file empty MAGIC CODE cbd43194
        private final static int BLANK_MAGIC_CODE = -875286124;
        /**
         * 内存映射文件队列,CommitLog和ConsumeQueue都有一个对应的MappedFileQueue
         */
        private final MappedFileQueue mappedFileQueue;
        private final DefaultMessageStore defaultMessageStore;
        /**
         * 同步刷盘
         * flushCommitLogService = new GroupCommitService();
         * 异步刷盘
         * flushCommitLogService = new FlushRealTimeService();
         */
        private final FlushCommitLogService flushCommitLogService;
    
        //If TransientStorePool enabled, we must flush message to FileChannel at fixed periods
        private final FlushCommitLogService commitLogService;
    
        private final AppendMessageCallback appendMessageCallback;
        private final ThreadLocal<MessageExtBatchEncoder> batchEncoderThreadLocal;
        private HashMap<String/* topic-queueid */, Long/* offset */> topicQueueTable = new HashMap<String, Long>(1024);
        private volatile long confirmOffset = -1L;
    
        private volatile long beginTimeInLock = 0;
        private final PutMessageLock putMessageLock;
    		...省略...
    }  
    

构造方法

  1. CommitLog构造方法:可以看出CommitLogDefaultMessageStore绑定,CommitLog是对外的统一抽象,实际文件的操作委托给mappedFile

    public CommitLog(final DefaultMessageStore defaultMessageStore) {
         
        //创建MappedFileQueue
        this.mappedFileQueue = new MappedFileQueue(defaultMessageStore.getMessageStoreConfig().getStorePathCommitLog(),
            defaultMessageStore.getMessageStoreConfig().getMapedFileSizeCommitLog(), defaultMessageStore.getAllocateMappedFileService());
        this.defaultMessageStore = defaultMessageStore;
        // 默认是异步刷盘FlushDiskType.ASYNC_FLUSH
        if (FlushDiskType.SYNC_FLUSH == defaultMessageStore.getMessageStoreConfig().getFlushDiskType()) {
         
            this.flushCommitLogService = new 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值