feat(保留期清理): 补齐 7 处只增不删的数据;修结果对象孤儿
审核发现一批表/元数据只增不删,且对象存储存在活跃泄漏: - module-cleanup 漏收 task_result_item / task_result_payload 的 payload 指针, 每晚删完行就在 json-server 桶留下孤儿对象;指针收集触顶从"截断后照样删整组" 改为任务组二分拆分,拆到单任务仍触顶则整组不删并记 error(截断会让指针随行消失) - 结果文件对象此前从不回收(只删 DB 行、对象留在桶里),现改为事务提交后 按 file_result / task_file_job 的 result_file_url 逐个回收 - 新增 7 个保留期清理:device_log_file 元数据、price_track_loop_run、撞款扫描行、 密钥用量日统计、紫鸟记忆过期行、PUBLISH 任务、BRAND 任务。后两者刻意不复用 module-cleanup(BRAND 不写 biz_file_task;PUBLISH 单任务可达数万行), 改为调各自既有的业务删除入口,保证结果对象与子表一起回收 - 未读通知补保留期(180 天,已读仍 90 天);ZiniaoMemoryStoreService.deleteExpired 此前全库无调用方(实现了没接线),现已挂定时任务 全部沿用既有范式:@Scheduled + 分布式锁单实例 + 分批 + 单轮批次数上限 + 失败只记日志。
This commit is contained in:
+9
@@ -2,6 +2,7 @@ package com.nanri.aiimage.modules.usersecret.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.nanri.aiimage.modules.usersecret.model.entity.UserSecretUsageEntity;
|
||||
import org.apache.ibatis.annotations.Delete;
|
||||
import org.apache.ibatis.annotations.Insert;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
@@ -24,4 +25,12 @@ public interface UserSecretUsageMapper extends BaseMapper<UserSecretUsageEntity>
|
||||
@Param("moduleKey") String moduleKey,
|
||||
@Param("businessDate") LocalDate businessDate,
|
||||
@Param("count") int count);
|
||||
|
||||
/** 分批删除保留期外的用量日统计行(该表此前只增不删)。 */
|
||||
@Delete("""
|
||||
DELETE FROM biz_user_secret_usage_daily
|
||||
WHERE business_date < #{cutoff}
|
||||
LIMIT #{batchSize}
|
||||
""")
|
||||
int deleteBefore(@Param("cutoff") LocalDate cutoff, @Param("batchSize") int batchSize);
|
||||
}
|
||||
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
package com.nanri.aiimage.modules.usersecret.service;
|
||||
|
||||
import com.nanri.aiimage.common.service.DistributedJobLockService;
|
||||
import com.nanri.aiimage.modules.usersecret.mapper.UserSecretUsageMapper;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.LocalDate;
|
||||
|
||||
/**
|
||||
* 密钥用量日统计表(biz_user_secret_usage_daily)的保留期清理。
|
||||
*
|
||||
* <p>每次调用按「用户 × 模块 × 业务日」upsert 累加,只增不删;量级是
|
||||
* 用户数 × 模块数 × 天数,单行很小但长期累积无上限。
|
||||
*
|
||||
* <p>保留期取 400 天(而不是常见的一年):用量页要支持同比/跨年对比,
|
||||
* 恰好一年前的数据仍有价值,留一点余量避免跨年时把刚过期的上年数据删掉。
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class UserSecretUsageRetentionService {
|
||||
|
||||
/** 单轮最多删除的批次数,剩余留给下一轮。 */
|
||||
static final int MAX_BATCHES_PER_RUN = 20;
|
||||
|
||||
private final UserSecretUsageMapper usageMapper;
|
||||
private final DistributedJobLockService distributedJobLockService;
|
||||
|
||||
@Value("${aiimage.user-secret.usage-retention-days:400}")
|
||||
private int retentionDays = 400;
|
||||
|
||||
@Value("${aiimage.user-secret.usage-retention-batch-size:2000}")
|
||||
private int retentionBatchSize = 2000;
|
||||
|
||||
@Scheduled(cron = "${aiimage.user-secret.usage-retention-cron:0 10 4 * * *}")
|
||||
public void purgeExpiredUsage() {
|
||||
int days = Math.max(1, retentionDays);
|
||||
int batchSize = Math.max(100, retentionBatchSize);
|
||||
LocalDate cutoff = LocalDate.now().minusDays(days);
|
||||
|
||||
DistributedJobLockService.LockHandle lockHandle =
|
||||
distributedJobLockService.tryLock("user-secret:usage-retention", Duration.ofMinutes(15));
|
||||
if (lockHandle == null) {
|
||||
log.info("[user-secret] 用量统计保留清理跳过:另一实例持锁");
|
||||
return;
|
||||
}
|
||||
try (lockHandle) {
|
||||
int totalDeleted = 0;
|
||||
int batches = 0;
|
||||
while (batches < MAX_BATCHES_PER_RUN) {
|
||||
int deleted = usageMapper.deleteBefore(cutoff, batchSize);
|
||||
batches++;
|
||||
totalDeleted += deleted;
|
||||
if (deleted < batchSize) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
log.info("[user-secret] 用量统计保留清理完成 cutoff={} retentionDays={} deleted={} batches={}",
|
||||
cutoff, days, totalDeleted, batches);
|
||||
} catch (Exception ex) {
|
||||
log.warn("[user-secret] 用量统计保留清理失败 cutoff={} msg={}", cutoff, ex.getMessage(), ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user