全站最硬核 百万字强肝RocketMq源码 火热更新中~(一百零一)经典面试题

源码如下:

org.apache.rocketmq.store.DefaultMessageStore.CleanCommitLogService

class CleanCommitLogService {

    private final static int MAX_MANUAL_DELETE_FILE_TIMES = 20;
    private final double diskSpaceWarningLevelRatio =
        Double.parseDouble(System.getProperty("rocketmq.broker.diskSpaceWarningLevelRatio", "0.90"));

    private final double diskSpaceCleanForciblyRatio =
        Double.parseDouble(System.getProperty("rocketmq.broker.diskSpaceCleanForciblyRatio", "0.85"));
    private long lastRedeleteTimestamp = 0;

    private volatile int manualDeleteFileSeveralTimes = 0;

    private volatile boolean cleanImmediately = false;

    public void excuteDeleteFilesManualy() {
        this.manualDeleteFileSeveralTimes = MAX_MANUAL_DELETE_FILE_TIMES;
        DefaultMessageStore.log.info("executeDeleteFilesManually was invoked");
    }

    public void run() {
        try {
            this.deleteExpiredFiles();

            this.redeleteHangedFile();
        } catch (Throwable e) {
            DefaultMessageStore.log.warn(this.getServiceName() + " service has exception. ", e);
        }
    }

    private void deleteExpiredFiles() {
        int deleteCount = 0;
        long fileReservedTime = DefaultMessageStore.this.getMessageStoreConfig().getFileReservedTime();
        int deletePhysicFilesInterval = DefaultMessageStore.this.getMessageStoreConfig().getDeleteCommitLogFilesInterval();
        int destroyMapedFileIntervalForcibly = DefaultMessageStore.this.getMessageStoreConfig().getDestroyMapedFileIntervalForcibly();

        boolean timeup = this.isTimeToDelete();
        boolean spacefull = this.isSpaceToDelete();
        boolean manualDelete = this.manualDeleteFileSeveralTimes > 0;

        if (timeup || spacefull || manualDelete) {

            if (manualDelete)
                this.manualDeleteFileSeveralTimes--;

            boolean cleanAtOnce = DefaultMessageStore.this.getMessageStoreConfig().isCleanFileForciblyEnable() && this.cleanImmediately;

            log.info("begin to delete before {} hours file. timeup: {} spacefull: {} manualDeleteFileSeveralTimes: {} cleanAtOnce: {}",
                fileReservedTime,
                timeup,
                spacefull,
                manualDeleteFileSeveralTimes,
                cleanAtOnce);

            fileReservedTime *= 60 * 60 * 1000;

            deleteCount = DefaultMessageStore.this.commitLog.deleteExpiredFile(fileReservedTime, deletePhysicFilesInterval,
                destroyMapedFileIntervalForcibly, cleanAtOnce);
            if (deleteCount > 0) {
            } else if (spacefull) {
                log.warn("disk space will be full soon, but delete file failed.");
            }
        }
    }

    private void redeleteHangedFile() {
        int interval = DefaultMessageStore.this.getMessageStoreConfig().getRedeleteHangedFileInterval();
        long currentTimestamp = System.currentTimeMillis();
        if ((currentTimestamp - this.lastRedeleteTimestamp) > interval) {
            this.lastRedeleteTimestamp = currentTimestamp;
            int destroyMapedFileIntervalForcibly =
                DefaultMessageStore.this.getMessageStoreConfig().getDestroyMapedFileIntervalForcibly();
            if (DefaultMessageStore.this.commitLog.retryDeleteFirstFile(destroyMapedFileIntervalForcibly)) {
            }
        }
    }

    public String getServiceName() {
        return CleanCommitLogService.class.getSimpleName();
    }

    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;
    }

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

        cleanImmediately = false;

        {
            String commitLogStorePath = DefaultMessageStore.this.getMessageStoreConfig().getStorePathCommitLog();
            String[] storePaths = commitLogStorePath.trim().split(MessageStoreConfig.MULTI_PATH_SPLITTER);
            Set<String> fullStorePath = new HashSet<>();
            double minPhysicRatio = 100;
            String minStorePath = null;
            for (String storePathPhysic : storePaths) {
                double physicRatio = UtilAll.getDiskPartitionSpaceUsedPercent(storePathPhysic);
                if (minPhysicRatio > physicRatio) {
                    minPhysicRatio =  physicRatio;
                    minStorePath = storePathPhysic;
                }
                if (physicRatio > diskSpaceCleanForciblyRatio) {
                    fullStorePath.add(storePathPhysic);
                }
            }
            DefaultMessageStore.this.commitLog.setFullStorePaths(fullStorePath);
            if (minPhysicRatio > diskSpaceWarningLevelRatio) {
                boolean diskok = DefaultMessageStore.this.runningFlags.getAndMakeDiskFull();
                if (diskok) {
                    DefaultMessageStore.log.error("physic disk maybe full soon " + minPhysicRatio +
                            ", so mark disk full, storePathPhysic=" + minStorePath);
                }

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

            if (minPhysicRatio < 0 || minPhysicRatio > ratio) {
                DefaultMessageStore.log.info("physic disk maybe full soon, so reclaim space, "
                        + minPhysicRatio + ", storePathPhysic=" + minStorePath);
                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;
    }

    public int getManualDeleteFileSeveralTimes() {
        return manualDeleteFileSeveralTimes;
    }

    public void setManualDeleteFileSeveralTimes(int manualDeleteFileSeveralTimes) {
        this.manualDeleteFileSeveralTimes = manualDeleteFileSeveralTimes;
    }

    public double calcStorePathPhysicRatio() {
        Set<String> fullStorePath = new HashSet<>();
        String storePath = getStorePathPhysic();
        String[] paths = storePath.trim().split(MessageStoreConfig.MULTI_PATH_SPLITTER);
        double minPhysicRatio = 100;
        for (String path : paths) {
            double physicRatio = UtilAll.isPathExists(path) ?
                    UtilAll.getDiskPartitionSpaceUsedPercent(path) : -1;
            minPhysicRatio = Math.min(minPhysicRatio, physicRatio);
            if (physicRatio > diskSpaceCleanForciblyRatio) {
                fullStorePath.add(path);
            }
        }
        DefaultMessageStore.this.commitLog.setFullStorePaths(fullStorePath);
        return minPhysicRatio;

    }

    public boolean isSpaceFull() {
        double physicRatio = calcStorePathPhysicRatio();
        double ratio = DefaultMessageStore.this.getMessageStoreConfig().getDiskMaxUsedSpaceRatio() / 100.0;
        if (physicRatio > ratio) {
            DefaultMessageStore.log.info("physic disk of commitLog used: " + physicRatio);
        }
        if (physicRatio > this.diskSpaceWarningLevelRatio) {
            boolean diskok = DefaultMessageStore.this.runningFlags.getAndMakeDiskFull();
            if (diskok) {
                DefaultMessageStore.log.error("physic disk of commitLog maybe full soon, used " + physicRatio + ", so mark disk full");
            }

            return true;
        } else {
            boolean diskok = DefaultMessageStore.this.runningFlags.getAndMakeDiskOK();

            if (!diskok) {
                DefaultMessageStore.log.info("physic disk space of commitLog OK " + physicRatio + ", so mark disk ok");
            }

            return false;
        }
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值