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:
+165
@@ -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<LocalDateTime> readCutoff = ArgumentCaptor.forClass(LocalDateTime.class);
|
||||
ArgumentCaptor<LocalDateTime> 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<LocalDateTime> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
-8
@@ -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 则两端通知串台。
|
||||
|
||||
Reference in New Issue
Block a user