聊聊PowerJob的CleanService

本文主要研究一下PowerJob的CleanService

CleanService

@Slf4j
@Service
public class CleanService {

    private final DFsService dFsService;

    private final InstanceInfoRepository instanceInfoRepository;

    private final WorkflowInstanceInfoRepository workflowInstanceInfoRepository;

    private final WorkflowNodeInfoRepository workflowNodeInfoRepository;

    private final LockService lockService;

    private final int instanceInfoRetentionDay;

    private final int localContainerRetentionDay;

    private final int remoteContainerRetentionDay;

    private static final int TEMPORARY_RETENTION_DAY = 3;

    /**
     * 每天凌晨3点定时清理
     */
    private static final String CLEAN_TIME_EXPRESSION = "0 0 3 * * ?";

    private static final String HISTORY_DELETE_LOCK = "history_delete_lock";

    public CleanService(DFsService dFsService, InstanceInfoRepository instanceInfoRepository, WorkflowInstanceInfoRepository workflowInstanceInfoRepository,
                        WorkflowNodeInfoRepository workflowNodeInfoRepository, LockService lockService,
                        @Value("${oms.instanceinfo.retention}") int instanceInfoRetentionDay,
                        @Value("${oms.container.retention.local}") int localContainerRetentionDay,
                        @Value("${oms.container.retention.remote}") int remoteContainerRetentionDay) {
        this.dFsService = dFsService;
        this.instanceInfoRepository = instanceInfoRepository;
        this.workflowInstanceInfoRepository = workflowInstanceInfoRepository;
        this.workflowNodeInfoRepository = workflowNodeInfoRepository;
        this.lockService = lockService;
        this.instanceInfoRetentionDay = instanceInfoRetentionDay;
        this.localContainerRetentionDay = localContainerRetentionDay;
        this.remoteContainerRetentionDay = remoteContainerRetentionDay;
    }

    //......
}    

CleanService提供了timingClean、cleanLocal方法

timingClean

    @Async(PJThreadPool.TIMING_POOL)
    @Scheduled(cron = CLEAN_TIME_EXPRESSION)
    public void timingClean() {

        // 释放本地缓存
        WorkerClusterManagerService.cleanUp();

        // 释放磁盘空间
        cleanLocal(OmsFileUtils.genLogDirPath(), instanceInfoRetentionDay);
        cleanLocal(OmsFileUtils.genContainerJarPath(), localContainerRetentionDay);
        cleanLocal(OmsFileUtils.genTemporaryPath(), TEMPORARY_RETENTION_DAY);

        // 删除数据库历史的数据
        cleanByOneServer();
    }

timingClean先执行WorkerClusterManagerService.cleanUp()释放本地缓存,之后通过cleanLocal释放本地磁盘空间,最后执行cleanByOneServer删除历史数据

cleanLocal

    @VisibleForTesting
    public void cleanLocal(String path, int day) {
        if (day < 0) {
            log.info("[CleanService] won't clean up {} because of offset day <= 0.", path);
            return;
        }

        Stopwatch stopwatch = Stopwatch.createStarted();
        File dir = new File(path);
        if (!dir.exists()) {
            return;
        }
        File[] logFiles = dir.listFiles();
        if (logFiles == null || logFiles.length == 0) {
            return;
        }

        // 计算最大偏移量
        long maxOffset = day * 24 * 60 * 60 * 1000L;

        for (File f : logFiles) {
            long offset = System.currentTimeMillis() - f.lastModified();
            if (offset >= maxOffset) {
                if (!f.delete()) {
                    log.warn("[CleanService] delete file({}) failed.", f.getName());
                }else {
                    log.info("[CleanService] delete file({}) successfully.", f.getName());
                }
            }
        }
        log.info("[CleanService] clean {} successfully, using {}.", path, stopwatch.stop());
    }

cleanLocal会删除指定目录中lastModified距离当前时间大于等于1天的文件

cleanByOneServer

    private void cleanByOneServer() {
        // 只要第一个server抢到锁其他server就会返回,所以锁10分钟应该足够了
        boolean lock = lockService.tryLock(HISTORY_DELETE_LOCK, 10 * 60 * 1000L);
        if (!lock) {
            log.info("[CleanService] clean job is already running, just return.");
            return;
        }
        try {
            // 删除数据库运行记录
            cleanInstanceLog();
            cleanWorkflowInstanceLog();
            // 删除无用节点
            cleanWorkflowNodeInfo();
            // 删除 GridFS 过期文件
            cleanRemote(Constants.LOG_BUCKET, instanceInfoRetentionDay);
            cleanRemote(Constants.CONTAINER_BUCKET, remoteContainerRetentionDay);
        } finally {
            lockService.unlock(HISTORY_DELETE_LOCK);
        }
    }

cleanByOneServer先加锁,然后执行cleanInstanceLog、cleanWorkflowInstanceLog、cleanWorkflowNodeInfo等

小结

PowerJob的CleanService提供了timingClean、cleanLocal方法,其中timingClean先执行WorkerClusterManagerService.cleanUp()释放本地缓存,之后通过cleanLocal释放本地磁盘空间,最后执行cleanByOneServer删除历史数据;cleanLocal会删除指定目录中lastModified距离当前时间大于等于1天的文件。

相关推荐

  1. 聊聊PowerJobCleanService

    2024-02-13 23:14:03       29 阅读
  2. 聊聊PowerJobStoreStrategy

    2024-02-13 23:14:03       34 阅读
  3. 聊聊PowerJobAbstractScriptProcessor

    2024-02-13 23:14:03       39 阅读
  4. 聊聊PowerJobAbstractSqlProcessor

    2024-02-13 23:14:03       38 阅读
  5. 聊聊PowerJobDispatchStrategy

    2024-02-13 23:14:03       34 阅读
  6. 聊聊PowerJobTimingStrategyHandler

    2024-02-13 23:14:03       30 阅读
  7. 聊聊PowerJobUseCacheLock

    2024-02-13 23:14:03       44 阅读
  8. 聊聊PowerJobQueryConvertUtils

    2024-02-13 23:14:03       33 阅读
  9. 聊聊PowerJobAlarmable

    2024-02-13 23:14:03       24 阅读
  10. 聊聊PowerJobAliOssService

    2024-02-13 23:14:03       31 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-13 23:14:03       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-13 23:14:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-13 23:14:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-13 23:14:03       18 阅读

热门阅读

  1. 蓝桥杯每日一题之内存问题

    2024-02-13 23:14:03       39 阅读
  2. C#中使用 => 运算符的 switch 表达式

    2024-02-13 23:14:03       32 阅读
  3. Python scapy 构建多层嵌套数据包

    2024-02-13 23:14:03       26 阅读
  4. 业务流程

    2024-02-13 23:14:03       33 阅读
  5. 安装PostgreSQL和PostGIS

    2024-02-13 23:14:03       24 阅读
  6. 所有设计模式大全及学习链接

    2024-02-13 23:14:03       40 阅读
  7. 一篇文章学透所有Python知识

    2024-02-13 23:14:03       32 阅读