diff --git a/backend-java/src/main/java/com/nanri/aiimage/config/NotificationProperties.java b/backend-java/src/main/java/com/nanri/aiimage/config/NotificationProperties.java index b72787be9..f2ec398bd 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/config/NotificationProperties.java +++ b/backend-java/src/main/java/com/nanri/aiimage/config/NotificationProperties.java @@ -79,14 +79,11 @@ public class NotificationProperties { */ private int maixiangSilentMinutes = 10; - /** 已读通知保留天数(超期自动清理),默认 90 天。 */ - private int readRetentionDays = 90; - /** - * 未读通知保留天数,默认 180 天(比已读长一倍)。 + * 通知保留天数:已读与未读同一条线,超期物理删除,默认 7 天。 * - *
未读通知此前永不清理:不看铃铛的用户会无限累积。保留期给得更宽, - * 是因为未读意味着"用户可能还没看到",但也不能永远留着。 + *
此前是「已读 90 / 未读 180」两档。一张按天扇出的告警表留半年只服务于一件没人做的事
+ * ——翻历史告警;2026-10-04 收成与设备日志一致的 7 天,删除动作见 NotificationRetentionService。
*/
- private int unreadRetentionDays = 180;
+ private int retentionDays = 7;
}
diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/notification/mapper/UserNotificationMapper.java b/backend-java/src/main/java/com/nanri/aiimage/modules/notification/mapper/UserNotificationMapper.java
index 545ff2a93..78ff14bfa 100644
--- a/backend-java/src/main/java/com/nanri/aiimage/modules/notification/mapper/UserNotificationMapper.java
+++ b/backend-java/src/main/java/com/nanri/aiimage/modules/notification/mapper/UserNotificationMapper.java
@@ -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 两条都按 {@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);
+
+ /** 分批删除保留期外的未读通知(与已读同一条时间线)。 */
+ @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);
}
diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/notification/service/NotificationRetentionService.java b/backend-java/src/main/java/com/nanri/aiimage/modules/notification/service/NotificationRetentionService.java
new file mode 100644
index 000000000..24d60f85f
--- /dev/null
+++ b/backend-java/src/main/java/com/nanri/aiimage/modules/notification/service/NotificationRetentionService.java
@@ -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 天)的行物理删除,
+ * 已读与未读同一条时间线。
+ *
+ * 此前这条清理寄生在 5 分钟一轮的 {@link NotificationScanScheduler} 里,有两个问题:
+ * 一是它跟着 {@code scan-enabled} 一起被关——生产为降噪关掉扫描,保留期就悄悄停摆、表无限增长;
+ * 二是一条无 LIMIT 的大 DELETE,首次收紧保留期时要一次删完 90/180 天攒下的存量,
+ * 而这张表每 60 秒就被铃铛轮询 {@code COUNT(*)} 一次,长事务会直接放大锁等待。
+ *
+ * 现在独立成凌晨任务并分批删除,与 {@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;
+ }
+}
diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/notification/service/NotificationScanScheduler.java b/backend-java/src/main/java/com/nanri/aiimage/modules/notification/service/NotificationScanScheduler.java
index a00be594f..c1fa8a3a2 100644
--- a/backend-java/src/main/java/com/nanri/aiimage/modules/notification/service/NotificationScanScheduler.java
+++ b/backend-java/src/main/java/com/nanri/aiimage/modules/notification/service/NotificationScanScheduler.java
@@ -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();
}
diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/notification/service/NotificationService.java b/backend-java/src/main/java/com/nanri/aiimage/modules/notification/service/NotificationService.java
index bf225beaa..6912291d6 100644
--- a/backend-java/src/main/java/com/nanri/aiimage/modules/notification/service/NotificationService.java
+++ b/backend-java/src/main/java/com/nanri/aiimage/modules/notification/service/NotificationService.java
@@ -251,28 +251,6 @@ public class NotificationService {
return updated;
}
- /** 清理指定时间之前已读的通知(保留期由调用方决定)。 */
- public int cleanupReadBefore(LocalDateTime cutoff) {
- int deleted = userNotificationMapper.delete(new LambdaQueryWrapper