feat(通知): 保留期收成 7 天一条线,独立成凌晨分批清理任务

- readRetentionDays=90 / unreadRetentionDays=180 两档合并为 retentionDays=7,
  两个旧 env 键随字段一起删(全仓已无引用)。按天扇出的告警表留半年只服务于一件没人做的事。
- 删除动作从 5 分钟一轮的 NotificationScanScheduler 里摘出来:原先它跟着 scan-enabled 一起被关,
  生产为降噪停扫描就等于悄悄停掉保留期;同时去掉 NotificationService 上两条无 LIMIT 的清理方法。
- 新建 NotificationRetentionService(04:05 + notification:retention job 锁 + 每轮 20 批 × 2000 行),
  与 DeviceLogRetentionService 同一范式。分批是因为这张表每 60 秒被铃铛 COUNT(*) 一次,
  首轮要把按 90/180 攒下的存量删掉,单条大 DELETE 会直接放大锁等待;宁可连续几天收敛。
- 不补索引:两条删除都走建表即有的 idx_created_at,保留窗口外的行几乎全部命中删除条件。
- NotificationRetentionServiceTest 8 例(变异自证 7/7 转红):其中一条锁注解 SQL 的谓词与 LIMIT——
  mock 掉 mapper 的那几条看不见「已读写成未读条件」,而线上表现是某一侧永远删不掉。
This commit is contained in:
2026-10-04 01:20:11 +08:00
parent ea1b1909b2
commit 03f087f3cd
8 changed files with 277 additions and 58 deletions
@@ -79,14 +79,11 @@ public class NotificationProperties {
*/
private int maixiangSilentMinutes = 10;
/** 已读通知保留天数(超期自动清理),默认 90 天。 */
private int readRetentionDays = 90;
/**
* 未读通知保留天数,默认 180 天(比已读长一倍)。
* 通知保留天数:已读与未读同一条线,超期物理删除,默认 7 天。
*
* <p>未读通知此前永不清理:不看铃铛的用户会无限累积。保留期给得更宽,
* 是因为未读意味着"用户可能还没看到",但也不能永远留着。
* <p>此前是「已读 90 / 未读 180」两档。一张按天扇出的告警表留半年只服务于一件没人做的事
* ——翻历史告警;2026-10-04 收成与设备日志一致的 7 天,删除动作见 NotificationRetentionService。
*/
private int unreadRetentionDays = 180;
private int retentionDays = 7;
}
@@ -2,8 +2,33 @@ package com.nanri.aiimage.modules.notification.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.nanri.aiimage.modules.notification.model.entity.UserNotificationEntity;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import java.time.LocalDateTime;
@Mapper
public interface UserNotificationMapper extends BaseMapper<UserNotificationEntity> {
/**
* 分批删除保留期外的<b>已读</b>通知。
*
* <p>两条都按 {@code created_at < cutoff} 走建表即有的 {@code idx_created_at}:保留窗口外的行
* 几乎全部命中删除条件,索引 range 扫前 N 行就能凑满一批,因此不必再为 read_at 补组合索引。
*/
@Delete("""
DELETE FROM biz_user_notification
WHERE read_at IS NOT NULL AND created_at < #{cutoff}
LIMIT #{batchSize}
""")
int deleteReadBeforeBatch(@Param("cutoff") LocalDateTime cutoff, @Param("batchSize") int batchSize);
/** 分批删除保留期外的<b>未读</b>通知(与已读同一条时间线)。 */
@Delete("""
DELETE FROM biz_user_notification
WHERE read_at IS NULL AND created_at < #{cutoff}
LIMIT #{batchSize}
""")
int deleteUnreadBeforeBatch(@Param("cutoff") LocalDateTime cutoff, @Param("batchSize") int batchSize);
}
@@ -0,0 +1,79 @@
package com.nanri.aiimage.modules.notification.service;
import com.nanri.aiimage.common.service.DistributedJobLockService;
import com.nanri.aiimage.config.NotificationProperties;
import com.nanri.aiimage.modules.notification.mapper.UserNotificationMapper;
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.LocalDateTime;
/**
* 站内通知的保留期清理:超过 {@code aiimage.notification.retention-days}(默认 7 天)的行物理删除,
* 已读与未读同一条时间线。
*
* <p>此前这条清理寄生在 5 分钟一轮的 {@link NotificationScanScheduler} 里,有两个问题:
* 一是它跟着 {@code scan-enabled} 一起被关——生产为降噪关掉扫描,保留期就悄悄停摆、表无限增长;
* 二是一条无 LIMIT 的大 DELETE,首次收紧保留期时要一次删完 90/180 天攒下的存量,
* 而这张表每 60 秒就被铃铛轮询 {@code COUNT(*)} 一次,长事务会直接放大锁等待。
*
* <p>现在独立成凌晨任务并分批删除,与 {@code DeviceLogRetentionService} 等保留清理任务同一范式:
* 双节点靠 job 锁单实例执行,单轮限制批次数以免长时间占锁,剩余量留给下一轮。
*/
@Service
@RequiredArgsConstructor
@Slf4j
public class NotificationRetentionService {
/** 单轮最多删除的批次数(已读、未读各自计数),剩余留给下一轮。 */
static final int MAX_BATCHES_PER_RUN = 20;
private static final String LOCK_NAME = "notification:retention";
private final UserNotificationMapper userNotificationMapper;
private final NotificationProperties properties;
private final DistributedJobLockService distributedJobLockService;
@Value("${aiimage.notification.retention-batch-size:2000}")
private int retentionBatchSize = 2000;
@Scheduled(cron = "${aiimage.notification.retention-cron:0 5 4 * * *}")
public void purgeExpired() {
int days = Math.max(1, properties.getRetentionDays());
int batchSize = Math.max(100, retentionBatchSize);
LocalDateTime cutoff = LocalDateTime.now().minusDays(days);
DistributedJobLockService.LockHandle lockHandle =
distributedJobLockService.tryLock(LOCK_NAME, Duration.ofMinutes(15));
if (lockHandle == null) {
log.info("[notification] 保留期清理跳过:另一实例持锁");
return;
}
try (lockHandle) {
int readDeleted = deleteInBatches(true, cutoff, batchSize);
int unreadDeleted = deleteInBatches(false, cutoff, batchSize);
log.info("[notification] 保留期清理完成 cutoff={} retentionDays={} 已读删除={} 未读删除={}",
cutoff, days, readDeleted, unreadDeleted);
} catch (Exception ex) {
log.warn("[notification] 保留期清理失败 cutoff={} msg={}", cutoff, ex.getMessage(), ex);
}
}
private int deleteInBatches(boolean read, LocalDateTime cutoff, int batchSize) {
int totalDeleted = 0;
for (int batch = 0; batch < MAX_BATCHES_PER_RUN; batch++) {
int deleted = read
? userNotificationMapper.deleteReadBeforeBatch(cutoff, batchSize)
: userNotificationMapper.deleteUnreadBeforeBatch(cutoff, batchSize);
totalDeleted += deleted;
if (deleted < batchSize) {
break;
}
}
return totalDeleted;
}
}
@@ -17,7 +17,6 @@ import org.springframework.stereotype.Service;
import org.springframework.web.client.RestClient;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
@@ -72,9 +71,6 @@ public class NotificationScanScheduler {
private final ProxyBalancePort proxyBalancePort;
private final MaixiangAnomalyScanner maixiangAnomalyScanner;
/** 上次清理日期(每天最多清理一次;双节点由分布式锁保证只有一个实例执行)。 */
private volatile LocalDate lastCleanupDate;
@Scheduled(fixedDelayString = "${aiimage.notification.scan-interval-ms:300000}",
initialDelayString = "${aiimage.notification.scan-initial-delay-ms:120000}")
public void scan() {
@@ -96,7 +92,6 @@ public class NotificationScanScheduler {
}
// 麦象异常扫描(任务停滞/失败/队列积压);开关与凭据在扫描器内部判定
maixiangAnomalyScanner.scan();
cleanupExpiredIfNeeded();
} catch (Exception ex) {
log.warn("[notification-scan] 扫描异常终止 err={}", ex.getMessage(), ex);
}
@@ -271,19 +266,6 @@ public class NotificationScanScheduler {
"service_down:" + serviceKey + ":" + dayOf(LocalDateTime.now()), null);
}
/** 通知保留期清理:每天最多一次;已读与未读各按自己的保留天数。 */
private void cleanupExpiredIfNeeded() {
LocalDate today = LocalDate.now();
if (today.equals(lastCleanupDate)) {
return;
}
lastCleanupDate = today;
LocalDateTime readCutoff = LocalDateTime.now().minusDays(Math.max(1, properties.getReadRetentionDays()));
notificationService.cleanupReadBefore(readCutoff);
LocalDateTime unreadCutoff = LocalDateTime.now().minusDays(Math.max(1, properties.getUnreadRetentionDays()));
notificationService.cleanupUnreadBefore(unreadCutoff);
}
private String dedupeKeyOf(BucketKey key) {
return "task_failed:" + key.userId() + ":" + key.moduleType() + ":" + key.day();
}
@@ -251,28 +251,6 @@ public class NotificationService {
return updated;
}
/** 清理指定时间之前已读的通知(保留期由调用方决定)。 */
public int cleanupReadBefore(LocalDateTime cutoff) {
int deleted = userNotificationMapper.delete(new LambdaQueryWrapper<UserNotificationEntity>()
.isNotNull(UserNotificationEntity::getReadAt)
.lt(UserNotificationEntity::getCreatedAt, cutoff));
if (deleted > 0) {
log.info("[notification] 清理历史已读通知 cutoff={} 删除={} 条", cutoff, deleted);
}
return deleted;
}
/** 清理指定时间之前仍未读的通知(保留期由调用方决定,比已读给得更宽)。 */
public int cleanupUnreadBefore(LocalDateTime cutoff) {
int deleted = userNotificationMapper.delete(new LambdaQueryWrapper<UserNotificationEntity>()
.isNull(UserNotificationEntity::getReadAt)
.lt(UserNotificationEntity::getCreatedAt, cutoff));
if (deleted > 0) {
log.info("[notification] 清理历史未读通知 cutoff={} 删除={} 条", cutoff, deleted);
}
return deleted;
}
private LambdaQueryWrapper<UserNotificationEntity> baseWrapper(Long userId, String audience, boolean onlyUnread,
NotificationPageQuery query) {
LambdaQueryWrapper<UserNotificationEntity> wrapper = new LambdaQueryWrapper<UserNotificationEntity>()