fix(任务进度): 剔除非归属任务时点名 taskId——那 512 条「请求 1 个 命中 0 个」以前读不出是哪一单
现网形状(2026-11-05 主机A 只读取证,43 分钟日志窗):`userId=1228` 打了 **512 条** 「进度查询剔除非归属任务 … 请求 1 个 命中 0 个」,同一账号每 5 秒轮询一支取不到结果的任务, 界面一直转圈。旧文案只有两个数,于是三种完全不同的事实共用同一行日志: · 拿别人的 taskId 在轮询(越权探测形状,该报警); · 网页端换号后仍用旧缓存(该清缓存); · 单已删而客户端按旧 id 空转(B21「在跑的任务列表没掉」那一族)。 读不出是哪一种,就没人能修——所以本笔只补「点名被剔的 taskId」这一件事, 判据与语义一字未动:剔除仍是安全边界,归属仍只由那一条带 `user_id` 的 SQL 决定。 刻意**不加**第二次归因查询、也**不把**归属条件挪进 Java: ① 挪条件=踩铁律 20「先取页再在 Java 里 filter」那一族(取页与过滤的先后一旦脱钩, 饿死某一分页的缺陷会在这里重生);② 为一条日志在轮询热路径多打一次往返不划算; ③ 本仓 catch 总量棘轮 `CatchSilenceRatchetTest`(STRICT_TOTAL=1069)实测把 「多包一层兜底 try」直接判红——第一版就是因此回炉的。 拿到 id 之后「这单存不存在、现在归谁」是一条主键查询就能定死的事,留给取证侧。 点名有上界(ATTRIBUTION_PREVIEW_LIMIT=12):一次批量轮询不许刷出一行超长日志。 三格新判据:id 必须出现在行里、干净轮询恰好一次查询且零日志、预览被截断在 12 个; 另把 `ProgressQueryOwnershipOutletTest` 的取证口径写回「一次 selectList」并注释清楚 归因只是日志、不是第二道过滤。
This commit is contained in:
+28
-3
@@ -7,7 +7,9 @@ import lombok.RequiredArgsConstructor;
|
|||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 进度查询的归属过滤(2026-09 全维度审查修复)。
|
* 进度查询的归属过滤(2026-09 全维度审查修复)。
|
||||||
@@ -28,6 +30,9 @@ public class TaskProgressOwnershipSupport {
|
|||||||
|
|
||||||
private final FileTaskMapper fileTaskMapper;
|
private final FileTaskMapper fileTaskMapper;
|
||||||
|
|
||||||
|
/** 归因文案里最多点名几个被剔除的 taskId(再多就只报数量,防止一次批量轮询刷出一行长日志)。 */
|
||||||
|
static final int ATTRIBUTION_PREVIEW_LIMIT = 12;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 过滤出属于 userId 的任务 id。
|
* 过滤出属于 userId 的任务 id。
|
||||||
*
|
*
|
||||||
@@ -57,10 +62,30 @@ public class TaskProgressOwnershipSupport {
|
|||||||
.map(FileTaskEntity::getId)
|
.map(FileTaskEntity::getId)
|
||||||
.toList();
|
.toList();
|
||||||
if (owned.size() != normalized.size()) {
|
if (owned.size() != normalized.size()) {
|
||||||
// 非归属任务出现在请求里:可能是越权尝试,也可能是前端历史遗留缓存了别人的 taskId
|
warnDropped(userId, normalized, owned);
|
||||||
log.warn("[progress-ownership] 进度查询剔除非归属任务 userId={} 请求 {} 个 命中 {} 个",
|
|
||||||
userId, normalized.size(), owned.size());
|
|
||||||
}
|
}
|
||||||
return owned;
|
return owned;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 剔除必须能被归因(2026-11-05 现网:主机A 43 分钟的日志窗里 {@code userId=1228} 打了 512 条
|
||||||
|
* 「请求 1 个 命中 0 个」,界面一直转圈,而我们连「它要的是哪个 taskId」都读不出来)。
|
||||||
|
*
|
||||||
|
* <p>三种完全不同的事实此前共用同一行日志:拿别人的 taskId 在轮询(越权探测)、网页端换号后
|
||||||
|
* 仍用旧缓存、单已删而客户端按旧 id 空转(B21「在跑的任务列表没掉」那一族)。读不出是哪一种,
|
||||||
|
* 就没人能修。</p>
|
||||||
|
*
|
||||||
|
* <p>这里<b>只点名 id</b>:不多打一次归因查询,也不把归属条件从 SQL 挪进 Java——归属必须由那一条
|
||||||
|
* 带 {@code user_id} 的查询说了算(铁律 20「先取页再过滤」之戒同形,且热路径不该为一条日志多一次
|
||||||
|
* 往返)。拿到 id 之后,「这单存不存在、现在归谁」是一条主键查询就能定死的事,留给取证侧。</p>
|
||||||
|
*/
|
||||||
|
private void warnDropped(Long userId, List<Long> requested, List<Long> owned) {
|
||||||
|
List<Long> missing = new ArrayList<>(requested);
|
||||||
|
missing.removeAll(owned);
|
||||||
|
String preview = missing.stream().limit(ATTRIBUTION_PREVIEW_LIMIT)
|
||||||
|
.map(String::valueOf)
|
||||||
|
.collect(Collectors.joining(","));
|
||||||
|
log.warn("[progress-ownership] 进度查询剔除非归属任务 userId={} 请求 {} 个 命中 {} 个 剔除 taskId={}",
|
||||||
|
userId, requested.size(), owned.size(), preview);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+2
@@ -231,6 +231,8 @@ class ProgressQueryOwnershipOutletTest {
|
|||||||
// 所以必须把 wrapper 抓出来,钉住 user_id 这一列和会话推出来的那个值。
|
// 所以必须把 wrapper 抓出来,钉住 user_id 这一列和会话推出来的那个值。
|
||||||
ArgumentCaptor<LambdaQueryWrapper<FileTaskEntity>> captor =
|
ArgumentCaptor<LambdaQueryWrapper<FileTaskEntity>> captor =
|
||||||
ArgumentCaptor.forClass(LambdaQueryWrapper.class);
|
ArgumentCaptor.forClass(LambdaQueryWrapper.class);
|
||||||
|
// 剔除路径只多打一行**日志**(把被剔的 taskId 点名,见 TaskProgressOwnershipDropAttributionTest),
|
||||||
|
// 不多打一次查询:归属条件必须一直只由这唯一一支带 user_id 的查询决定。
|
||||||
verify(mapper).selectList(captor.capture());
|
verify(mapper).selectList(captor.capture());
|
||||||
captor.getValue().getSqlSegment(); // MP 惯例:先求值一次才铺出参数
|
captor.getValue().getSqlSegment(); // MP 惯例:先求值一次才铺出参数
|
||||||
assertThat(captor.getValue().getSqlSegment()).contains("user_id");
|
assertThat(captor.getValue().getSqlSegment()).contains("user_id");
|
||||||
|
|||||||
+123
@@ -0,0 +1,123 @@
|
|||||||
|
package com.nanri.aiimage.modules.task.service;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.times;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import ch.qos.logback.classic.Logger;
|
||||||
|
import ch.qos.logback.classic.spi.ILoggingEvent;
|
||||||
|
import ch.qos.logback.core.read.ListAppender;
|
||||||
|
import com.baomidou.mybatisplus.core.MybatisConfiguration;
|
||||||
|
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
||||||
|
import com.nanri.aiimage.modules.task.mapper.FileTaskMapper;
|
||||||
|
import com.nanri.aiimage.modules.task.model.entity.FileTaskEntity;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.LongStream;
|
||||||
|
import org.apache.ibatis.builder.MapperBuilderAssistant;
|
||||||
|
import org.junit.jupiter.api.AfterEach;
|
||||||
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 「进度查询剔除非归属任务」必须能被归因(2026-11-05 现网驱动)。
|
||||||
|
*
|
||||||
|
* <p>现网形状:主机A 一个 25,000 行的日志窗(43 分钟)里 {@code userId=1228} 打了 512 条
|
||||||
|
* 「请求 1 个 命中 0 个」——同一账号每 5 秒轮询一支取不到结果的任务,界面一直转圈。旧文案只有
|
||||||
|
* 两个数,于是三种完全不同的事实共用同一行日志:拿别人的 taskId(越权探测)、网页端换号后仍用
|
||||||
|
* 旧缓存、单已删而客户端按旧 id 空转(B21 那一族)。读不出是哪一种就没人能修,所以本类钉的是
|
||||||
|
* 「读得出来」这件事,而不是任何判定语义。</p>
|
||||||
|
*
|
||||||
|
* <p>三条判据缺一不可:
|
||||||
|
* <ol>
|
||||||
|
* <li>剔除语义不变(这是安全边界,不许为了归因放行别人的单);</li>
|
||||||
|
* <li>热路径不多一次查询——归属永远只由那一条带 {@code user_id} 的 SQL 决定
|
||||||
|
* (铁律 20「先取页再在 Java 里 filter」之戒同形);</li>
|
||||||
|
* <li>点名有上界,一次批量轮询不许刷出一行超长日志。</li>
|
||||||
|
* </ol>
|
||||||
|
*/
|
||||||
|
class TaskProgressOwnershipDropAttributionTest {
|
||||||
|
|
||||||
|
private static final Long SESSION_USER = 1228L;
|
||||||
|
|
||||||
|
private FileTaskMapper fileTaskMapper;
|
||||||
|
private TaskProgressOwnershipSupport support;
|
||||||
|
private ListAppender<ILoggingEvent> appender;
|
||||||
|
|
||||||
|
@BeforeAll
|
||||||
|
static void initMybatisPlusTableInfo() {
|
||||||
|
TableInfoHelper.initTableInfo(new MapperBuilderAssistant(new MybatisConfiguration(), ""),
|
||||||
|
FileTaskEntity.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
fileTaskMapper = mock(FileTaskMapper.class);
|
||||||
|
support = new TaskProgressOwnershipSupport(fileTaskMapper);
|
||||||
|
Logger logger = (Logger) LoggerFactory.getLogger(TaskProgressOwnershipSupport.class);
|
||||||
|
appender = new ListAppender<>();
|
||||||
|
appender.start();
|
||||||
|
logger.addAppender(appender);
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterEach
|
||||||
|
void tearDown() {
|
||||||
|
((Logger) LoggerFactory.getLogger(TaskProgressOwnershipSupport.class)).detachAppender(appender);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static FileTaskEntity row(long id) {
|
||||||
|
FileTaskEntity entity = new FileTaskEntity();
|
||||||
|
entity.setId(id);
|
||||||
|
entity.setUserId(SESSION_USER);
|
||||||
|
return entity;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String logged() {
|
||||||
|
return appender.list.stream().map(ILoggingEvent::getFormattedMessage)
|
||||||
|
.reduce("", (a, b) -> a + "\n" + b);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void droppedTaskIdsAreNamedSoTheShapeIsReadable() {
|
||||||
|
when(fileTaskMapper.selectList(any())).thenReturn(List.of(row(101L)));
|
||||||
|
|
||||||
|
List<Long> kept = support.filterOwnedTaskIds(List.of(101L, 202L, 303L), SESSION_USER);
|
||||||
|
|
||||||
|
assertThat(kept).containsExactly(101L); // 剔除本身是安全边界,语义一字不动
|
||||||
|
String line = logged();
|
||||||
|
assertThat(line).contains("userId=" + SESSION_USER)
|
||||||
|
.contains("请求 3 个 命中 1 个")
|
||||||
|
.contains("202").contains("303");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void cleanPollLogsNothingAndPaysExactlyOneQuery() {
|
||||||
|
when(fileTaskMapper.selectList(any()))
|
||||||
|
.thenReturn(List.of(row(101L), row(102L)));
|
||||||
|
|
||||||
|
List<Long> kept = support.filterOwnedTaskIds(List.of(101L, 102L), SESSION_USER);
|
||||||
|
|
||||||
|
assertThat(kept).containsExactly(101L, 102L);
|
||||||
|
verify(fileTaskMapper, times(1)).selectList(any());
|
||||||
|
assertThat(appender.list).isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void attributionPreviewIsCapped() {
|
||||||
|
when(fileTaskMapper.selectList(any())).thenReturn(List.of());
|
||||||
|
List<Long> requested = LongStream.rangeClosed(1, 40).boxed().toList();
|
||||||
|
|
||||||
|
List<Long> kept = support.filterOwnedTaskIds(requested, SESSION_USER);
|
||||||
|
|
||||||
|
assertThat(kept).isEmpty();
|
||||||
|
String line = logged();
|
||||||
|
assertThat(line).contains("1,2,3");
|
||||||
|
assertThat(line).doesNotContain(",40"); // 只点名前 ATTRIBUTION_PREVIEW_LIMIT 个
|
||||||
|
long named = line.substring(line.indexOf("剔除 taskId=")).split(",").length;
|
||||||
|
assertThat(named).isEqualTo(TaskProgressOwnershipSupport.ATTRIBUTION_PREVIEW_LIMIT);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user