前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >RocketMQ CommitLog 文件规则

RocketMQ CommitLog 文件规则

作者头像
java404
发布2018-12-28 15:34:29
2.5K0
发布2018-12-28 15:34:29
举报
文章被收录于专栏:java 成神之路java 成神之路

1、CommitLog 文件生成规则

偏移量:每个 CommitLog 文件的大小为 1G,一般情况下第一个 CommitLog 的起始偏移量为 0,第二个 CommitLog 的起始偏移量为 1073741824 (1G = 1073741824byte)。

2、怎么知道消息存储在哪个 CommitLog 文件上?

假设 1073742827 为物理偏移量(物理偏移量也即全局偏移量),则其对应的相对偏移量为 1003(1003 = 1073742827 - 1073741824),并且该偏移量位于第二个 CommitLog。

index 和 ComsumerQueue 中都有消息对应的物理偏移量,通过物理偏移量就可以计算出该消息位于哪个 CommitLog 文件上。

3、CommitLog 文件命名规则

代码语言:javascript
复制
public MappedFile getLastMappedFile(final long startOffset, boolean needCreate) {
    long createOffset = -1;
    MappedFile mappedFileLast = getLastMappedFile();
    // 1、如果 mappedFileLast 为空或者已满,则计算新文件的物理偏移量
    if (mappedFileLast == null) {
        createOffset = startOffset - (startOffset % this.mappedFileSize);
    }

    if (mappedFileLast != null && mappedFileLast.isFull()) {
        createOffset = mappedFileLast.getFileFromOffset() + this.mappedFileSize;
    }

    if (createOffset != -1 && needCreate) {
        // 2、通过物理偏移量获得文件名
        String nextFilePath = this.storePath + File.separator + UtilAll.offset2FileName(createOffset);
        String nextNextFilePath = this.storePath + File.separator
            + UtilAll.offset2FileName(createOffset + this.mappedFileSize);
        MappedFile mappedFile = null;

        if (this.allocateMappedFileService != null) {
            mappedFile = this.allocateMappedFileService.putRequestAndReturnMappedFile(nextFilePath,
                nextNextFilePath, this.mappedFileSize);
        } else {
            try {
                mappedFile = new MappedFile(nextFilePath, this.mappedFileSize);
            } catch (IOException e) {
                log.error("create mappedFile exception", e);
            }
        }
        // 省略代码
        ......
}

新创建的 mappedFile 物理偏移量计算

  • 1、如果 mappedFileLast 为空,那么肯定是第一次启动 MQ,那么物理偏移量为0。
  • 2、如果 mappedFileLast 已满,则获取上一个 mappedFile 的起始物理偏移量 + 文件大小。

文件名通过调用UtilAll.offset2FileName(createOffset) 进行获取。

获取文件名
代码语言:javascript
复制
public static String offset2FileName(final long offset) {
    final NumberFormat nf = NumberFormat.getInstance();
    nf.setMinimumIntegerDigits(20);
    nf.setMaximumFractionDigits(0);
    nf.setGroupingUsed(false);
    return nf.format(offset);
}

可以看出就是根据上一步传入 createOffset 转化成20位的数字字符串。不够20位前面补零。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.12.26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1、CommitLog 文件生成规则
  • 2、怎么知道消息存储在哪个 CommitLog 文件上?
  • 3、CommitLog 文件命名规则
    • 获取文件名
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档