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() - .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() - .isNull(UserNotificationEntity::getReadAt) - .lt(UserNotificationEntity::getCreatedAt, cutoff)); - if (deleted > 0) { - log.info("[notification] 清理历史未读通知 cutoff={} 删除={} 条", cutoff, deleted); - } - return deleted; - } - private LambdaQueryWrapper baseWrapper(Long userId, String audience, boolean onlyUnread, NotificationPageQuery query) { LambdaQueryWrapper wrapper = new LambdaQueryWrapper() diff --git a/backend-java/src/main/resources/application.yml b/backend-java/src/main/resources/application.yml index e0bf3f82d..c43d80218 100644 --- a/backend-java/src/main/resources/application.yml +++ b/backend-java/src/main/resources/application.yml @@ -467,9 +467,10 @@ aiimage: maixiang-queue-processing-threshold: ${AIIMAGE_NOTIFICATION_MAIXIANG_QUEUE_PROCESSING_THRESHOLD:100} # 采集端 worker 静默阈值(分钟):超时无轮询 = 采集机掉线。唯一直接反映整机停摆的信号。 maixiang-silent-minutes: ${AIIMAGE_NOTIFICATION_MAIXIANG_SILENT_MINUTES:10} - read-retention-days: ${AIIMAGE_NOTIFICATION_READ_RETENTION_DAYS:90} - # 未读通知也设保留期(此前永不清理,不看铃铛的用户会无限累积);给得比已读宽一倍 - unread-retention-days: ${AIIMAGE_NOTIFICATION_UNREAD_RETENTION_DAYS:180} + # 通知最多留 7 天(已读/未读同一条线),超期由 NotificationRetentionService 凌晨分批物理删除 + retention-days: ${AIIMAGE_NOTIFICATION_RETENTION_DAYS:7} + retention-cron: ${AIIMAGE_NOTIFICATION_RETENTION_CRON:0 5 4 * * *} + retention-batch-size: ${AIIMAGE_NOTIFICATION_RETENTION_BATCH_SIZE:2000} security: shop-credential-key: ${AIIMAGE_SHOP_CREDENTIAL_KEY:change-me-shop-credential-key} # 轮换过渡期用:逗号分隔的旧密钥,解密按顺序回试,加密只用上面那个;迁完就清空 diff --git a/backend-java/src/test/java/com/nanri/aiimage/modules/notification/service/NotificationRetentionServiceTest.java b/backend-java/src/test/java/com/nanri/aiimage/modules/notification/service/NotificationRetentionServiceTest.java new file mode 100644 index 000000000..02471f235 --- /dev/null +++ b/backend-java/src/test/java/com/nanri/aiimage/modules/notification/service/NotificationRetentionServiceTest.java @@ -0,0 +1,165 @@ +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 org.apache.ibatis.annotations.Delete; +import org.junit.jupiter.api.Test; +import org.mockito.ArgumentCaptor; + +import java.time.Duration; +import java.time.LocalDateTime; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +/** + * 站内通知保留期清理(biz_user_notification 最多留 7 天,超期物理删除)。 + */ +class NotificationRetentionServiceTest { + + private final UserNotificationMapper mapper = mock(UserNotificationMapper.class); + private final DistributedJobLockService jobLockService = mock(DistributedJobLockService.class); + private final NotificationProperties properties = new NotificationProperties(); + + private NotificationRetentionService service(int retentionDays) { + properties.setRetentionDays(retentionDays); + return new NotificationRetentionService(mapper, properties, jobLockService); + } + + private void lockAvailable() { + when(jobLockService.tryLock(anyString(), any())) + .thenReturn(mock(DistributedJobLockService.LockHandle.class)); + } + + @Test + void deletesInBatchesUntilBatchNotFull() { + lockAvailable(); + when(mapper.deleteReadBeforeBatch(any(), anyInt())).thenReturn(2000, 2000, 120); + when(mapper.deleteUnreadBeforeBatch(any(), anyInt())).thenReturn(500); + + service(7).purgeExpired(); + + verify(mapper, times(3)).deleteReadBeforeBatch(any(LocalDateTime.class), eq(2000)); + verify(mapper, times(1)).deleteUnreadBeforeBatch(any(LocalDateTime.class), eq(2000)); + } + + @Test + void stopsAtBatchLimitPerRun() { + lockAvailable(); + when(mapper.deleteReadBeforeBatch(any(), anyInt())).thenReturn(2000); + when(mapper.deleteUnreadBeforeBatch(any(), anyInt())).thenReturn(2000); + + service(7).purgeExpired(); + + // 断言写成字面量而非引用常量:引用常量会让「改常量」这种变异同时改掉期望值,恒绿。 + verify(mapper, times(20)) + .deleteReadBeforeBatch(any(LocalDateTime.class), anyInt()); + verify(mapper, times(20)) + .deleteUnreadBeforeBatch(any(LocalDateTime.class), anyInt()); + } + + @Test + void skipsWhenAnotherInstanceHoldsLock() { + when(jobLockService.tryLock(anyString(), any())).thenReturn(null); + + service(7).purgeExpired(); + + verify(mapper, never()).deleteReadBeforeBatch(any(), anyInt()); + verify(mapper, never()).deleteUnreadBeforeBatch(any(), anyInt()); + } + + @Test + void deleteFailureIsLoggedAndDoesNotThrow() { + lockAvailable(); + when(mapper.deleteReadBeforeBatch(any(), anyInt())).thenThrow(new IllegalStateException("db down")); + + service(7).purgeExpired(); + + verify(mapper).deleteReadBeforeBatch(any(LocalDateTime.class), anyInt()); + } + + /** 契约:已读与未读共用一条保留线(此前是「已读 90 / 未读 180」两档,现收成 7 天)。 */ + @Test + void readAndUnreadShareOneCutoffAtRetentionBoundary() { + lockAvailable(); + when(mapper.deleteReadBeforeBatch(any(), anyInt())).thenReturn(0); + when(mapper.deleteUnreadBeforeBatch(any(), anyInt())).thenReturn(0); + + service(7).purgeExpired(); + + ArgumentCaptor readCutoff = ArgumentCaptor.forClass(LocalDateTime.class); + ArgumentCaptor unreadCutoff = ArgumentCaptor.forClass(LocalDateTime.class); + verify(mapper).deleteReadBeforeBatch(readCutoff.capture(), anyInt()); + verify(mapper).deleteUnreadBeforeBatch(unreadCutoff.capture(), anyInt()); + assertThat(Duration.between(readCutoff.getValue(), unreadCutoff.getValue()).abs().toMillis()).isZero(); + assertThat(Duration.between(readCutoff.getValue(), LocalDateTime.now()).toDays()).isEqualTo(7); + } + + @Test + void retentionDaysDefaultsToSeven() { + lockAvailable(); + when(mapper.deleteReadBeforeBatch(any(), anyInt())).thenReturn(0); + when(mapper.deleteUnreadBeforeBatch(any(), anyInt())).thenReturn(0); + + new NotificationRetentionService(mapper, new NotificationProperties(), jobLockService).purgeExpired(); + + ArgumentCaptor cutoff = ArgumentCaptor.forClass(LocalDateTime.class); + verify(mapper).deleteReadBeforeBatch(cutoff.capture(), anyInt()); + assertThat(Duration.between(cutoff.getValue(), LocalDateTime.now()).toDays()).isEqualTo(7); + } + + /** 契约:保留清理不再寄生在 scan-enabled 之下——为降噪关掉扫描,不能连带把保留期停摆。 */ + @Test + void retentionRunsEvenWhenScanIsDisabled() { + lockAvailable(); + when(mapper.deleteReadBeforeBatch(any(), anyInt())).thenReturn(0); + when(mapper.deleteUnreadBeforeBatch(any(), anyInt())).thenReturn(0); + properties.setRetentionDays(7); + properties.setScanEnabled(false); + + new NotificationRetentionService(mapper, properties, jobLockService).purgeExpired(); + + verify(mapper).deleteReadBeforeBatch(any(LocalDateTime.class), anyInt()); + verify(mapper).deleteUnreadBeforeBatch(any(LocalDateTime.class), anyInt()); + } + + /** + * 契约:两条分批删除各自锁死自己的谓词。上面的用例把 mapper 整层 mock 掉了, + * 「已读写成未读条件」在单测里完全不可见,但线上表现为某一侧永远删不掉(表继续无限增长)。 + */ + @Test + void batchDeleteSqlKeepsItsOwnPredicateAndLimit() { + String readSql = deleteSqlOf("deleteReadBeforeBatch"); + String unreadSql = deleteSqlOf("deleteUnreadBeforeBatch"); + for (String sql : List.of(readSql, unreadSql)) { + assertThat(sql).contains("DELETE FROM biz_user_notification"); + assertThat(sql).contains("created_at < #{cutoff}"); + assertThat(sql).contains("LIMIT #{batchSize}"); + } + assertThat(readSql).contains("read_at IS NOT NULL").doesNotContain("read_at IS NULL"); + // 未读侧必须只写 IS NULL:IS NOT NULL 不含 IS NULL,两者可互相排他 + assertThat(unreadSql).contains("read_at IS NULL").doesNotContain("IS NOT NULL"); + } + + private static String deleteSqlOf(String methodName) { + try { + Delete delete = UserNotificationMapper.class + .getMethod(methodName, LocalDateTime.class, int.class) + .getAnnotation(Delete.class); + assertThat(delete).as("%s 上的 @Delete 注解", methodName).isNotNull(); + return String.join(" ", delete.value()); + } catch (NoSuchMethodException ex) { + throw new AssertionError(ex); + } + } +} diff --git a/backend-java/src/test/java/com/nanri/aiimage/modules/notification/service/NotificationServiceTest.java b/backend-java/src/test/java/com/nanri/aiimage/modules/notification/service/NotificationServiceTest.java index 5303fe119..2272a5da3 100644 --- a/backend-java/src/test/java/com/nanri/aiimage/modules/notification/service/NotificationServiceTest.java +++ b/backend-java/src/test/java/com/nanri/aiimage/modules/notification/service/NotificationServiceTest.java @@ -196,14 +196,6 @@ class NotificationServiceTest { assertThat(service.markAllRead(7L, NotificationService.AUDIENCE_ADMIN)).isEqualTo(4); } - @Test - void cleanupDeletesOnlyReadRowsBeforeCutoff() { - when(mapper.delete(any())).thenReturn(2); - int deleted = service.cleanupReadBefore(LocalDateTime.now().minusDays(90)); - assertThat(deleted).isEqualTo(2); - verify(mapper).delete(any()); - } - /** * 可见范围回归:读取侧必须按「接收者 user_id + 端 audience」双条件裁剪。 * 一旦有人漏掉 user_id,管理员/用户就会看到别人的通知;漏掉 audience 则两端通知串台。