From 03f087f3cd19258c535c59668ec781a040a50032 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E8=87=AA=E8=BE=BE?= <980324341@qq.com> Date: Sun, 4 Oct 2026 01:20:11 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E9=80=9A=E7=9F=A5):=20=E4=BF=9D=E7=95=99?= =?UTF-8?q?=E6=9C=9F=E6=94=B6=E6=88=90=207=20=E5=A4=A9=E4=B8=80=E6=9D=A1?= =?UTF-8?q?=E7=BA=BF=EF=BC=8C=E7=8B=AC=E7=AB=8B=E6=88=90=E5=87=8C=E6=99=A8?= =?UTF-8?q?=E5=88=86=E6=89=B9=E6=B8=85=E7=90=86=E4=BB=BB=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 的那几条看不见「已读写成未读条件」,而线上表现是某一侧永远删不掉。 --- .../config/NotificationProperties.java | 11 +- .../mapper/UserNotificationMapper.java | 25 +++ .../service/NotificationRetentionService.java | 79 +++++++++ .../service/NotificationScanScheduler.java | 18 -- .../service/NotificationService.java | 22 --- .../src/main/resources/application.yml | 7 +- .../NotificationRetentionServiceTest.java | 165 ++++++++++++++++++ .../service/NotificationServiceTest.java | 8 - 8 files changed, 277 insertions(+), 58 deletions(-) create mode 100644 backend-java/src/main/java/com/nanri/aiimage/modules/notification/service/NotificationRetentionService.java create mode 100644 backend-java/src/test/java/com/nanri/aiimage/modules/notification/service/NotificationRetentionServiceTest.java 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