美文网首页
rocketmq_过期文件清理

rocketmq_过期文件清理

作者: kele2018 | 来源:发表于2022-05-16 10:56 被阅读0次
Q、broker清理过期文件的机制是什么?

A:broker在启动的时候会开启一个定时任务,默认延迟60s执行,间隔10s执行一次;

      // org.apache.rocketmq.store.DefaultMessageStore 1346
        this.scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                DefaultMessageStore.this.cleanFilesPeriodically();
            }
        }, 1000 * 60, this.messageStoreConfig.getCleanResourceInterval(), TimeUnit.MILLISECONDS);
Q、broker会清理哪些过期的文件?

A:commitLog和consumeQueue

// org.apache.rocketmq.store.DefaultMessageStore 1389
private void cleanFilesPeriodically() {
        // 删除commitlog
        this.cleanCommitLogService.run();
        // 删除consumeQueue
        this.cleanConsumeQueueService.run();
    }

Q、broker是如何判定一个文件已经过期?

A:当前时间-文件最后一次更新时间>3天

 // org.apache.rocketmq.store.MappedFileQueue 354
 long liveMaxTimestamp = mappedFile.getLastModifiedTimestamp() + expiredTime;
 if (System.currentTimeMillis() >= liveMaxTimestamp || cleanImmediately) {
       if (mappedFile.destroy(intervalForcibly)) {
       }
}
Q、哪些情况会触发broker删除过期文件?

A:(1) 到了删除的时间点(可配置),默认为凌晨4点;

// org.apache.rocketmq.store.DefaultMessageStore.CleanCommitLogService 1674
private boolean isTimeToDelete() {
   String when = DefaultMessageStore.this.getMessageStoreConfig().getDeleteWhen();
      if (UtilAll.isItTimeToDo(when)) {
              DefaultMessageStore.log.info("it's time to reclaim disk space, " + when);
               return true;
      }
       return false;
}

(2) 磁盘空间不足(这个方法的逻辑很清晰,不再多啰嗦);

private boolean isSpaceToDelete() {
            double ratio = DefaultMessageStore.this.getMessageStoreConfig().getDiskMaxUsedSpaceRatio() / 100.0;

            cleanImmediately = false;

            {
                String storePathPhysic = DefaultMessageStore.this.getMessageStoreConfig().getStorePathCommitLog();
                // 当前磁盘分区(比如c盘、d盘)的空间使用率
                double physicRatio = UtilAll.getDiskPartitionSpaceUsedPercent(storePathPhysic);
                if (physicRatio > diskSpaceWarningLevelRatio) {  // 大于90%
                    // 如果磁盘分区使用率超过该阔值,将设置磁盘不可写,此时会拒绝新消息的写入。
                    boolean diskok = DefaultMessageStore.this.runningFlags.getAndMakeDiskFull();
                    if (diskok) {
                        DefaultMessageStore.log.error("physic disk maybe full soon " + physicRatio + ", so mark disk full");
                    }

                    cleanImmediately = true;
                } else if (physicRatio > diskSpaceCleanForciblyRatio) { // 大于85%
                    cleanImmediately = true;
                } else {
                    boolean diskok = DefaultMessageStore.this.runningFlags.getAndMakeDiskOK();
                    if (!diskok) {
                        DefaultMessageStore.log.info("physic disk space OK " + physicRatio + ", so mark disk ok");
                    }
                }

                if (physicRatio < 0 || physicRatio > ratio) {  // 如果ratio配置太大,不会删除过期文件,但是runningFlags改了,消息便不能发送成功
                    DefaultMessageStore.log.info("physic disk maybe full soon, so reclaim space, " + physicRatio);
                    return true;
                }
            }

            {
                String storePathLogics = StorePathConfigHelper
                    .getStorePathConsumeQueue(DefaultMessageStore.this.getMessageStoreConfig().getStorePathRootDir());
                double logicsRatio = UtilAll.getDiskPartitionSpaceUsedPercent(storePathLogics);
                if (logicsRatio > diskSpaceWarningLevelRatio) {
                    boolean diskok = DefaultMessageStore.this.runningFlags.getAndMakeDiskFull();
                    if (diskok) {
                        DefaultMessageStore.log.error("logics disk maybe full soon " + logicsRatio + ", so mark disk full");
                    }

                    cleanImmediately = true;
                } else if (logicsRatio > diskSpaceCleanForciblyRatio) {
                    cleanImmediately = true;
                } else {
                    boolean diskok = DefaultMessageStore.this.runningFlags.getAndMakeDiskOK();
                    if (!diskok) {
                        DefaultMessageStore.log.info("logics disk space OK " + logicsRatio + ", so mark disk ok");
                    }
                }

                if (logicsRatio < 0 || logicsRatio > ratio) {  //
                    DefaultMessageStore.log.info("logics disk maybe full soon, so reclaim space, " + logicsRatio);
                    return true;
                }
            }

            return false;
        }

(3)预留,手工触发

boolean manualDelete = this.manualDeleteFileSeveralTimes > 0;

相关文章

  • rocketmq_过期文件清理

    Q、broker清理过期文件的机制是什么? A:broker在启动的时候会开启一个定时任务,默认延迟60s执行,间...

  • MySQL Binlog

    过期时间 通过设置日志的过期时间,系统会自动清理过期的日志文件,默认0永不过期。 刷新日志 每次数据库服务启动时,...

  • 2021-10-11

    《金钱智慧》第二天落地实修 断舍离 1.清理手机相册和视频 2.清理QQ聊天记录和邮件 3.清理电脑桌面和过期文件...

  • shell定时清理过期文件/日志

    前言 我从事于一家游戏公司,公司有一台日志服务器,用于临时存储各游戏日志进行以便数据清洗操作。日积月累日志文件耗费...

  • 逆水行舟

    2019030 坐标:郑州 昨天晚上还打开的文件,今天早起系统就提示文件已过期或被清理,无法打开了。郁闷的不行,只...

  • CentOS定时备份mysql数据库和清理过期备份文件

      本篇主要用于介绍如何在linux服务器下编写备份mysql数据库文件和清理过期备份文件的脚本,以及设置定时任务...

  • 抓大放小

    很多时候,我的时间都是浪费在无所谓的小事上,比如说整理很多不用的纸张,清理过期不用的电脑垃圾文件,清理手机上存的的...

  • mysql清理过期binlog堵塞数据库

    背景 线上环境 疑问 疑问1分析(binlog在什么时候会进行过期清理清理) 疑问2分析(为什么binlog过期期...

  • 先生教会我共情

    最近微信有些卡,导致新收的图片都点不开。于是我试着清理微信收藏里的一些过期或无效文件。 不...

  • 过期文件

    过去的事情舍不得去忘记就会,产生无法再次努力前进的阻力。是的,我们要学会忘记一些过期的文件,让大脑给一些有深刻含义...

网友评论

      本文标题:rocketmq_过期文件清理

      本文链接:https://www.haomeiwen.com/subject/pojburtx.html