feat(phase-5): AC-19/AC-21 读路径时间窗与 keyset、心跳瘦读、菜单缓存、去 RAND、MDC 与补检限流

AC-19(P1-B1/B2/B3/B4)
- 任务中心(P1-B1):TaskCenterQueryService.tasks() 不带 startTime 时补 90 天默认窗口
  (startOfDefaultWindow),不再把 null 透传给 12 模块 UNION 扫全历史;selectTaskPage 的
  `LIMIT ? OFFSET ?` 改成 (createdAt,id) 游标,深翻页的 skip 挪进新语句
  selectTaskPageAnchor——它只投影 (id,createdAt) 两列。宽/窄两份 UNION 共用同一对筛选片段
  (taskFilterT/taskFilterB),锚点与页查询筛的行集在结构上不可能漂;页码语义、total、
  响应结构与前端全部不变。
- 关键词抽词(P1-B2):selectRandomUnused 不再 ORDER BY RAND()(全表 filesort)。
  服务层先 countHit 数命中集、再把窗口起点掷在 [0, total-limit],按 k.id 有序取一段后
  在内存洗牌。分布口径变了(连续 id 的一段 vs 逐行独立随机),已如实登记。
- 心跳(P1-B3):selectFileTask 只捞 id/moduleType/status/userId/updatedAt 五列;
  需要整实体的模块由新 SPI 默认方法 heartbeatNeedsFullTaskEntity()(默认 true=保守)
  声明后补一次主键读,collect-data/similar-asin/appearance-patent 三个只用
  task.getId() 的显式豁免。半行进 saveTaskCache 会写出坏缓存,比多读一次列难查得多。
  markInterrupted 改走整实体读(要 errorMessage 并同步模块缓存/快照)。
- 权限菜单(P1-B4):loadMenus 加 (companyId,menuType) 短 TTL(30s) 快照缓存,
  五处目录写路径 create/update/delete/reorder/copyTreeFromTemplate 全部写时失效;
  只缓存目录本体、不缓存用户授权行——撤权必须下一请求就生效。

AC-21(审计 §5/PERF §6)
- RequestTraceFilter 把已解析却只进日志参数的 requestId 写入 MDC(缺失写 "-"),
  finally 里 MDC.clear()(tomcat 线程复用,不清就把上一条的 id 带给下一条日志);
  logback-spring.xml 的两条 pattern 默认值 + application.yml 显式设的 file/console
  两条同批改带 [%X{requestId}]——本文件那两条会盖掉默认值,漏一处线上照样看不见。
- AppearancePatentDeliverySupport.requestRecheckForMissingRows 的补检轮 WARN 按任务限流:
  5 分钟窗口内首条 WARN(带 suppressedInLastWindow 汇总),其余降 DEBUG。刻意不吃
  recheckRounds/countRecheckRound 那对轮次计数——那是 scheduleFallbackRecheck 的补检预算,
  从日志路径再计一次会让"限流"吃掉业务预算、任务更早被判补检耗尽。
  "补检排不上队"那条不受限流(要人处置的信号不许压)。

契约激活与判据变更
- PendingPhase5ReadPathTest 5 例、PendingPhase5LogVolumeAndMdcTest 3 例全部去 @Disabled 转绿;
  后者预置的 logback 路径层级写错(mainSourcesRoot() 已是 src/main/java),激活时修正,
  并补一条 application.yml 的 pattern 判据(只增不减)。
- 新增 AppearancePatentRecheckWarnThrottleTest 3 例:钉实跑读数(6 轮只留 1 条 WARN + 5 条
  DEBUG、按任务隔离、排不上队仍出声),形状尺子之外的行为证据。
- TaskCenterQueryMapperSqlTest +2 例(锚点窄投影、游标收窄两分支)、
  TaskCenterQueryServiceTest +3 例(默认窗口、第一页不付锚点钱、页码越界返回空页)。
- 三处既有判据按新契约反转,一支没注释掉:selectTaskPage 的 `LIMIT ? OFFSET ?`→游标、
  分页参数绑定数 2→1/16→18/8→7;tasks() 三处 isNull()→notNull()(默认窗口生效本身成为断言)。
- 棘轮登记:SQL 分层语句总数 143→144(新语句点名),MigratedSqlEquivalenceTest
  XML_SELECT_TOTAL 99→100、LEDGER_ROWS 85→84、LEDGER_SELECTS 60→59——
  selectTaskPage 那一行整条摘出基准账本并点名进 NOT_MIGRATED,因为基准是"搬迁前的注解原文",
  改写它等于让等价性测试替新 SQL 背书。
- application.yml 走 hash-object+update-index 只提交本轮 pattern 那一格(4/2),
  另一路会话在飞的 client-task-pull 注释格原样留在工作树未提交。

读数:后端 mvn -o test 5,021 例/0 Failures/0 Errors/19 skipped(6:21 min,
较 Phase 4 的 5,013 净增 8 例=激活 8 例预置后新增 8 例判据);fat jar 实起,落盘行带出
`[main] INFO  [] c.nanri.aiimage.AiImageApplication - Starting...` 证明 MDC 位真在渲染;
本机无 MySQL ⇒ Hikari 反复 Starting、Tomcat 未监听,请求级 requestId 关联与现网四样本顺延 Phase 6。
This commit is contained in:
2026-10-29 16:32:46 +08:00
parent ef0bc51fa2
commit 217f5eec62
23 changed files with 630 additions and 62 deletions
@@ -80,8 +80,10 @@ logging:
root: INFO
com.nanri.aiimage: INFO
pattern:
file: "%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n"
console: "%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n"
# AC-21:与 logback-spring.xml 的默认值同口径(本文件显式设了这两条,默认值会被盖掉),
# %X{requestId} 由 RequestTraceFilter 入 MDC。两处必须一起改,漏一处线上就看不见请求链。
file: "%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level [%X{requestId}] %logger{36} - %msg%n"
console: "%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level [%X{requestId}] %logger{36} - %msg%n"
mybatis-plus:
mapper-locations: classpath*:mapper/**/*.xml
@@ -4,8 +4,11 @@
<springProperty scope="context" name="LOG_NAME" source="logging.file.name" defaultValue="aiimage-backend.log"/>
<springProperty scope="context" name="ROOT_LOG_LEVEL" source="logging.level.root" defaultValue="INFO"/>
<springProperty scope="context" name="APP_LOG_LEVEL" source="logging.level.com.nanri.aiimage" defaultValue="INFO"/>
<springProperty scope="context" name="FILE_PATTERN" source="logging.pattern.file" defaultValue="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n"/>
<springProperty scope="context" name="CONSOLE_PATTERN" source="logging.pattern.console" defaultValue="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n"/>
<!-- AC-21(审计 §5.2):%X{requestId} 由 RequestTraceFilter 入 MDC。放在 %-5level 之后、
%logger 之前,既保住原有列宽,又能用一行 requestId 把整条请求链的日志串起来。
⚠ 这里是"配置没给时的默认值",application.yml 显式设了 logging.pattern.* 会盖掉它 ⇒ 两边同改。 -->
<springProperty scope="context" name="FILE_PATTERN" source="logging.pattern.file" defaultValue="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level [%X{requestId}] %logger{36} - %msg%n"/>
<springProperty scope="context" name="CONSOLE_PATTERN" source="logging.pattern.console" defaultValue="%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level [%X{requestId}] %logger{36} - %msg%n"/>
<property name="APP_LOG_FILE" value="${LOG_PATH}/${LOG_NAME}"/>
<property name="APP_LOG_ARCHIVE" value="${LOG_PATH}/${LOG_NAME}.%d{yyyy-MM-dd}.%i"/>
@@ -45,13 +45,17 @@
LIMIT #{f.offset}, #{f.limit}
</select>
<!-- AC-19(审计 P1-B2):随机抽词不再 ORDER BY RAND()——那一句给命中集每一行打一个随机数再
filesort 全表,词库越大越慢。改成「按主键升序取一段窗口」:窗口起点由服务层先 count 命中集、
再掷在 [0, total-limit] 上,LIMIT 之前不必排序。服务层拿到窗口后就地洗牌,
页面看到的仍是散着的词。 -->
<select id="selectRandomUnused" resultType="com.nanri.aiimage.modules.keywordshare.model.vo.KeywordShareRowVo">
SELECT k.id, k.country, k.keyword, k.chinese, k.category, k.sub_category, k.category_l3, k.category_l4,
(u.country IS NOT NULL) AS used, u.used_time, u.user_name AS used_user
FROM <include refid="usageJoin"/>
WHERE <include refid="filterConds"/>
ORDER BY RAND()
LIMIT #{f.limit}
ORDER BY k.id
LIMIT #{f.limit} OFFSET #{f.offset}
</select>
<select id="countDupKeywords" resultType="long">
@@ -54,8 +54,28 @@
<select id="selectTaskCountries" resultType="com.nanri.aiimage.modules.taskcenter.model.dto.TaskCenterCountryRow">
SELECT t.id AS taskId, COALESCE(NULLIF(NULLIF(JSON_UNQUOTE(JSON_EXTRACT(t.request_json, '$.filters.countryCode')), ''), 'null'),NULLIF(NULLIF(JSON_UNQUOTE(JSON_EXTRACT(t.request_json, '$.countryCode')), ''), 'null'),NULLIF(NULLIF(JSON_UNQUOTE(JSON_EXTRACT(t.request_json, '$.country')), ''), 'null'),NULLIF(NULLIF(JSON_UNQUOTE(JSON_EXTRACT(t.request_json, '$.publish_country')), ''), 'null'),NULLIF(NULLIF(JSON_UNQUOTE(JSON_EXTRACT(t.request_json, '$.countryCodes')), ''), 'null'),NULLIF(NULLIF(JSON_UNQUOTE(JSON_EXTRACT(t.request_json, '$.country_codes')), ''), 'null'),NULLIF(NULLIF(JSON_UNQUOTE(JSON_EXTRACT(t.request_json, '$.filters.countryCodes')), ''), 'null')) AS country FROM biz_file_task t WHERE t.id IN <foreach collection='taskIds' item='tid' open='(' separator=',' close=')'>#{tid}</foreach>
</select>
<!-- AC-19(审计 P1-B1):页查询不再吃 OFFSET。深翻页的 skip 挪进 selectTaskPageAnchor,
它只投影 (id,createdAt) 两列;页查询本身按 (createdAt,id) 游标取 limit 行,
MySQL 8.0.22+ 能把游标条件压进 UNION 分支、用上 created_at 索引,OFFSET 压不进去,
只能把整个 UNION 物化再跳过。
下面的筛选片段是宽行 UNION 与窄锚点的唯一公共出处:两份投影的筛选必须逐字相同,
否则锚点算的是另一批行,第 N 页会安静地给错。共用片段让「漂」在结构上不可能发生。 -->
<sql id="taskFilterT">t.module_type IN ('PUBLISH','DELETE_BRAND','PRODUCT_RISK_RESOLVE','PRICE_TRACK','SHOP_MATCH','PATROL_DELETE','QUERY_ASIN','WITHDRAW','APPEARANCE_PATENT','SIMILAR_ASIN','COLLECT_DATA','SHOP_DATA_CRAWL')<if test='userIds != null'> AND t.user_id IN <foreach collection='userIds' item='uid' open='(' separator=',' close=')'>#{uid}</foreach></if><if test='moduleType != null'> AND UPPER(t.module_type) = #{moduleType}</if><if test='statuses != null'> AND CASE WHEN UPPER(t.status) = 'CANCELED' THEN 'CANCELLED' ELSE UPPER(t.status) END IN <foreach collection='statuses' item='st' open='(' separator=',' close=')'>#{st}</foreach></if><if test='startTime != null'> AND t.created_at &gt;= #{startTime}</if><if test='endTime != null'> AND t.created_at &lt;= #{endTime}</if><if test='shop != null'> AND (EXISTS (SELECT 1 FROM biz_publish_file p WHERE p.task_id = t.id AND LOWER(TRIM(p.shop_name)) = LOWER(TRIM(#{shop}))) OR (t.module_type IN ('PATROL_DELETE','PRICE_TRACK','PRODUCT_RISK_RESOLVE','QUERY_ASIN','SHOP_DATA_CRAWL','SHOP_MATCH') AND EXISTS (SELECT 1 FROM biz_file_result f WHERE f.task_id = t.id AND LOWER(TRIM(f.source_filename)) = LOWER(TRIM(#{shop})))))</if></sql>
<sql id="taskFilterB">1 = 1<if test='userIds != null'> AND b.user_id IN <foreach collection='userIds' item='uid' open='(' separator=',' close=')'>#{uid}</foreach></if><if test='moduleType != null'> AND 'BRAND' = #{moduleType}</if><if test='statuses != null'> AND CASE WHEN UPPER(b.status) = 'CANCELED' THEN 'CANCELLED' ELSE UPPER(b.status) END IN <foreach collection='statuses' item='st' open='(' separator=',' close=')'>#{st}</foreach></if><if test='startTime != null'> AND b.created_at &gt;= #{startTime}</if><if test='endTime != null'> AND b.created_at &lt;= #{endTime}</if><if test='shop != null'> AND 1 = 0</if></sql>
<sql id="taskUnionRows">
(SELECT t.id AS id, t.task_no AS taskNo, UPPER(t.module_type) AS moduleType, CASE WHEN UPPER(t.status) = 'CANCELED' THEN 'CANCELLED' ELSE UPPER(t.status) END AS status, t.user_id AS userId, t.device_id AS deviceId, t.created_at AS createdAt, t.finished_at AS finishedAt, t.updated_at AS updatedAt, t.source_file_count AS totalCount, t.success_file_count AS successCount, t.failed_file_count AS failedCount, 0 AS brand FROM biz_file_task t WHERE <include refid="taskFilterT"/> UNION ALL SELECT b.id, CONCAT('brand-', b.id), 'BRAND', CASE WHEN UPPER(b.status) = 'CANCELED' THEN 'CANCELLED' ELSE UPPER(b.status) END, b.user_id, b.device_id, b.created_at, CASE WHEN b.status IN ('cancelled','failed','success') THEN b.updated_at END, b.updated_at, b.progress_total, b.progress_current, 0, 1 FROM brand_crawl_tasks b WHERE <include refid="taskFilterB"/>)
</sql>
<sql id="taskUnionKeys">
(SELECT t.id AS id, t.created_at AS createdAt FROM biz_file_task t WHERE <include refid="taskFilterT"/> UNION ALL SELECT b.id, b.created_at FROM brand_crawl_tasks b WHERE <include refid="taskFilterB"/>)
</sql>
<select id="selectTaskPageAnchor" resultType="com.nanri.aiimage.modules.taskcenter.model.dto.TaskCenterTaskRow">
SELECT m.createdAt, m.id FROM <include refid="taskUnionKeys"/> m
ORDER BY m.createdAt DESC, m.id DESC LIMIT 1 OFFSET #{offset}
</select>
<select id="selectTaskPage" resultType="com.nanri.aiimage.modules.taskcenter.model.dto.TaskCenterTaskRow">
SELECT m.* FROM (SELECT t.id AS id, t.task_no AS taskNo, UPPER(t.module_type) AS moduleType, CASE WHEN UPPER(t.status) = 'CANCELED' THEN 'CANCELLED' ELSE UPPER(t.status) END AS status, t.user_id AS userId, t.device_id AS deviceId, t.created_at AS createdAt, t.finished_at AS finishedAt, t.updated_at AS updatedAt, t.source_file_count AS totalCount, t.success_file_count AS successCount, t.failed_file_count AS failedCount, 0 AS brand FROM biz_file_task t WHERE t.module_type IN ('PUBLISH','DELETE_BRAND','PRODUCT_RISK_RESOLVE','PRICE_TRACK','SHOP_MATCH','PATROL_DELETE','QUERY_ASIN','WITHDRAW','APPEARANCE_PATENT','SIMILAR_ASIN','COLLECT_DATA','SHOP_DATA_CRAWL')<if test='userIds != null'> AND t.user_id IN <foreach collection='userIds' item='uid' open='(' separator=',' close=')'>#{uid}</foreach></if><if test='moduleType != null'> AND UPPER(t.module_type) = #{moduleType}</if><if test='statuses != null'> AND CASE WHEN UPPER(t.status) = 'CANCELED' THEN 'CANCELLED' ELSE UPPER(t.status) END IN <foreach collection='statuses' item='st' open='(' separator=',' close=')'>#{st}</foreach></if><if test='startTime != null'> AND t.created_at &gt;= #{startTime}</if><if test='endTime != null'> AND t.created_at &lt;= #{endTime}</if><if test='shop != null'> AND (EXISTS (SELECT 1 FROM biz_publish_file p WHERE p.task_id = t.id AND LOWER(TRIM(p.shop_name)) = LOWER(TRIM(#{shop}))) OR (t.module_type IN ('PATROL_DELETE','PRICE_TRACK','PRODUCT_RISK_RESOLVE','QUERY_ASIN','SHOP_DATA_CRAWL','SHOP_MATCH') AND EXISTS (SELECT 1 FROM biz_file_result f WHERE f.task_id = t.id AND LOWER(TRIM(f.source_filename)) = LOWER(TRIM(#{shop})))))</if> UNION ALL SELECT b.id, CONCAT('brand-', b.id), 'BRAND', CASE WHEN UPPER(b.status) = 'CANCELED' THEN 'CANCELLED' ELSE UPPER(b.status) END, b.user_id, b.device_id, b.created_at, CASE WHEN b.status IN ('cancelled','failed','success') THEN b.updated_at END, b.updated_at, b.progress_total, b.progress_current, 0, 1 FROM brand_crawl_tasks b WHERE 1 = 1<if test='userIds != null'> AND b.user_id IN <foreach collection='userIds' item='uid' open='(' separator=',' close=')'>#{uid}</foreach></if><if test='moduleType != null'> AND 'BRAND' = #{moduleType}</if><if test='statuses != null'> AND CASE WHEN UPPER(b.status) = 'CANCELED' THEN 'CANCELLED' ELSE UPPER(b.status) END IN <foreach collection='statuses' item='st' open='(' separator=',' close=')'>#{st}</foreach></if><if test='startTime != null'> AND b.created_at &gt;= #{startTime}</if><if test='endTime != null'> AND b.created_at &lt;= #{endTime}</if><if test='shop != null'> AND 1 = 0</if>) m ORDER BY m.createdAt DESC, m.id DESC LIMIT #{limit} OFFSET #{offset}
SELECT m.* FROM <include refid="taskUnionRows"/> m
<if test='afterCreatedAt != null'> WHERE (m.createdAt &lt; #{afterCreatedAt} OR (m.createdAt = #{afterCreatedAt} AND m.id &lt; #{afterId}))</if>
ORDER BY m.createdAt DESC, m.id DESC LIMIT #{limit}
</select>
<select id="selectUserIdsByUsername" resultType="long">
SELECT id FROM users WHERE username LIKE CONCAT('%', #{keyword}, '%') LIMIT 500