版本
- 基于
rocketmq-all-4.3.1
版本
简介
-
CommitLog是保存消息元数据的地方,所有Topic的消息到达Broker后都会保存到CommitLog文件。CommitLog 里面记录了每条消息的消费情况,是否被消费,由谁消费,该消息是否持久化等信息
-
CommitLog文件的存储目录默认为**${ROCKET_HOME}/store/commitlog**,默认大小是1G
-
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; ...省略... }
构造方法
-
CommitLog构造方法:可以看出CommitLog与DefaultMessageStore绑定,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