该文所涉及的 RocketMQ 源码版本为 4.9.3。

RocketMQ ConsumeQueue 详解

RocketMQ 基于主题订阅模式实现消息消费,消费者关注每一个主题下的所有消息,但是同一主题下的消息是不连续地存储在 CommitLog 文件中的,如果消费者直接从消息存储文件中遍历查找主题下的消息,效率会特别低。所以为了在查找消息的时候效率更高一些,设计了 ConsumeQueue 文件,可以看作 CommitLog 消费的目录文件.

ConsumeQueue 的第一级目录为消息主题名称,第二级目录为主题的队列 id

为了加速 ConsumeQueue 消息的查询速度并节省磁盘空间,不会存储消息的全量信息,只会 存储一些 关键信息,如 8 字节的 CommmitLog 偏移量、4 字节的文件大小、8 字节的 tag 哈希码

1、根据消息存储时间查找物理偏移量:

org.apache.rocketmq.store.ConsumeQueue#getOffsetInQueueByTime

第一步:根据时间戳定位物理文件

  1. public MappedFile getMappedFileByTime(final long timestamp) {
  2. Object[] mfs = this.copyMappedFiles(0);
  3. if (null == mfs)
  4. return null;
  5. for (int i = 0; i < mfs.length; i++) {
  6. MappedFile mappedFile = (MappedFile) mfs[i];
  7. if (mappedFile.getLastModifiedTimestamp() >= timestamp) {
  8. return mappedFile;
  9. }
  10. }
  11. return (MappedFile) mfs[mfs.length - 1];
  12. }

从第一个文件 开始,找到第一个更新时间大于该时间戳的文件

第二步:利用二分查找法来加速检索

计算最低查找偏移量,如果消息队列偏移量大于文件的偏移量,则最低偏移量等于消息队列偏移量减去文件的偏移量,反之为 0

int low = minLogicOffset > mappedFile.getFileFromOffset() ? (int) (minLogicOffset - mappedFile.getFileFromOffset()) : 0;

计算中间偏移量,其中CQ_STORE_UNIT_SIZE = 8 字节的 CommmitLog 偏移量 + 4 字节的文件大小+8 字节的 tag 哈希码

midOffset = (low + high) / (2 * *CQ_STORE_UNIT_SIZE*) * *CQ_STORE_UNIT_SIZE*;

如果得到的物理偏移量小于当前最小物理偏移量,则待查找消息的物理偏移量大于 midOffset,将 low 设置为 midOffset,继续查询

  1. byteBuffer.position(midOffset);
  2. long phyOffset = byteBuffer.getLong();
  3. int size = byteBuffer.getInt();
  4. if (phyOffset < minPhysicOffset) {
  5. low = midOffset +CQ_STORE_UNIT_SIZE;
  6. leftOffset = midOffset;
  7. continue;
  8. }

如果得到的物理偏移量大于最小物理偏移量,说明该消息为有效信息,则根据消息物理偏移量和消息长度获取消息存储的时间戳

  1. long storeTime = this.defaultMessageStore.getCommitLog().pickupStoreTimestamp(phyOffset, size);

如果存储时间小于 0,则为无效消息,返回 0;

如果存储时间戳等于待查找时间戳,说明查找到了目标消息,设置 targetOffset,跳出循环;

如果存储时间戳大于待查找时间戳,说明待查找消息的物理偏移量小于 midOffset,设置 high 为 midOffset,设置 rightIndexValue 等于 storeTime,设置 rightOffset 为 midOffset;

如果存储时间戳小于待查找时间戳,说明待查找消息的物理偏移量大于 midOffset,设置 low 为 midOffset,设置 leftIndexValue 等于 storeTime,设置 leftOffset 为 midOffset

  1. if (storeTime < 0) {
  2. return 0;
  3. } else if (storeTime == timestamp) {
  4. targetOffset = midOffset;
  5. break;
  6. } else if (storeTime > timestamp) {
  7. high = midOffset -CQ_STORE_UNIT_SIZE;
  8. rightOffset = midOffset;
  9. rightIndexValue = storeTime;
  10. } else {
  11. low = midOffset +CQ_STORE_UNIT_SIZE;
  12. leftOffset = midOffset;
  13. leftIndexValue = storeTime;
  14. }

如果 targetOffset 不等于-1,表示找到了存储时间戳等于待查找时间戳的消息;

如果 leftIndexValue 等于-1,返回大于并且最接近待查找消息的时间戳的偏移量

如果 rightIndexValue 等于-1,返回小于并且最接近待查找消息的时间戳的偏移量

  1. if (targetOffset != -1) {
  2. offset = targetOffset;
  3. } else {
  4. if (leftIndexValue == -1) {
  5. offset = rightOffset;
  6. } else if (rightIndexValue == -1) {
  7. offset = leftOffset;
  8. } else {
  9. offset = Math.abs(timestamp - leftIndexValue) > Math.abs(timestamp - rightIndexValue) ? rightOffset : leftOffset;
  10. }
  11. }

2、根据当前偏移量获取下一个文件的偏移量

org.apache.rocketmq.store.ConsumeQueue#rollNextFile

  1. public long rollNextFile(final long index) {
  2. int mappedFileSize = this.mappedFileSize;
  3. int totalUnitsInFile = mappedFileSize /CQ_STORE_UNIT_SIZE;
  4. return index + totalUnitsInFile - index % totalUnitsInFile;
  5. }

3、ConsumeQueue 添加消息

org.apache.rocketmq.store.ConsumeQueue#putMessagePositionInfo

将消息偏移量、消息长度、tag 哈希码写入 ByteBuffer,将内容追加到 ConsumeQueue 的内存映射文件中。

  1. private boolean putMessagePositionInfo(final long offset, final int size, final long tagsCode,
  2. final long cqOffset) {
  3. if (offset + size <= this.maxPhysicOffset) {
  4. log.warn("Maybe try to build consume queue repeatedly maxPhysicOffset={} phyOffset={}", maxPhysicOffset, offset);
  5. return true;
  6. }
  7. this.byteBufferIndex.flip();
  8. this.byteBufferIndex.limit(CQ_STORE_UNIT_SIZE);
  9. this.byteBufferIndex.putLong(offset);
  10. this.byteBufferIndex.putInt(size);
  11. this.byteBufferIndex.putLong(tagsCode);
  12. final long expectLogicOffset = cqOffset *CQ_STORE_UNIT_SIZE;
  13. MappedFile mappedFile = this.mappedFileQueue.getLastMappedFile(expectLogicOffset);
  14. if (mappedFile != null) {
  15. if (mappedFile.isFirstCreateInQueue() && cqOffset != 0 && mappedFile.getWrotePosition() == 0) {
  16. this.minLogicOffset = expectLogicOffset;
  17. this.mappedFileQueue.setFlushedWhere(expectLogicOffset);
  18. this.mappedFileQueue.setCommittedWhere(expectLogicOffset);
  19. this.fillPreBlank(mappedFile, expectLogicOffset);
  20. log.info("fill pre blank space " + mappedFile.getFileName() + " " + expectLogicOffset + " "
  21. + mappedFile.getWrotePosition());
  22. }
  23. if (cqOffset != 0) {
  24. long currentLogicOffset = mappedFile.getWrotePosition() + mappedFile.getFileFromOffset();
  25. if (expectLogicOffset < currentLogicOffset) {
  26. log.warn("Build consume queue repeatedly, expectLogicOffset: {} currentLogicOffset: {} Topic: {} QID: {} Diff: {}", expectLogicOffset, currentLogicOffset, this.topic, this.queueId, expectLogicOffset - currentLogicOffset);
  27. return true;
  28. }
  29. if (expectLogicOffset != currentLogicOffset) {
  30. LOG_ERROR.warn("[BUG]logic queue order maybe wrong, expectLogicOffset: {} currentLogicOffset: {} Topic: {} QID: {} Diff: {}",
  31. expectLogicOffset,
  32. currentLogicOffset,
  33. this.topic,
  34. this.queueId,
  35. expectLogicOffset - currentLogicOffset
  36. );
  37. }
  38. }
  39. this.maxPhysicOffset = offset + size;
  40. return mappedFile.appendMessage(this.byteBufferIndex.array());
  41. }
  42. return false;
  43. }

4、ConsumeQueue 文件删除

org.apache.rocketmq.store.ConsumeQueue#destroy

重置 ConsumeQueue 的 maxPhysicOffset 与 minLogicOffset,调用 MappedFileQueue 的 destroy()方法将 ConsumeQueue 目录下的文件全部删除

  1. public void destroy() {
  2. this.maxPhysicOffset = -1;
  3. this.minLogicOffset = 0;
  4. this.mappedFileQueue.destroy();
  5. if (isExtReadEnable()) {
  6. this.consumeQueueExt.destroy();
  7. }
  8. }