fix(cost-guard): 注解 SQL 收进 XML、新端点进守卫名单,并按实扫对齐 catch 棘轮

三格护栏红都在本笔自己头上:UserSecretUsageMapper/UserTaskGuardMapper 的 5 条
注解 SQL 迁进 mapper XML(两处语句台账同步);cost-guard 两个端点身份取自 JWT,
进 AdminApiGuardFilter 无条件名单而不是登记成「方法内自校验」;catch 静默棘轮
先摘掉 6 处能去的静默出口(含 resume 写失败被吞成「已继续任务」的假成功),
余下 6 处带 [cost-guard] 锚点的 fail-open 逐处记名入账。
This commit is contained in:
2026-10-24 13:21:47 +08:00
parent ba20f5ed42
commit 1743666334
13 changed files with 120 additions and 113 deletions
@@ -1,11 +1,9 @@
package com.nanri.aiimage.common.retry;
import cn.hutool.crypto.digest.DigestUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.client.HttpStatusCodeException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;
@@ -30,11 +28,14 @@ import java.util.concurrent.atomic.AtomicLong;
@Slf4j
public final class LlmKeyGate {
private static final int MAX_CONCURRENCY = positiveInt("AIIMAGE_LLM_KEY_MAX_CONCURRENCY", 8);
private static final java.util.regex.Pattern POSITIVE = java.util.regex.Pattern.compile("\\d+");
private static final int MAX_CONCURRENCY =
(int) positive("AIIMAGE_LLM_KEY_MAX_CONCURRENCY", 3, 8);
private static final long MIN_INTERVAL_NANOS =
TimeUnit.MILLISECONDS.toNanos(positiveLong("AIIMAGE_LLM_KEY_MIN_INTERVAL_MS", 50L));
TimeUnit.MILLISECONDS.toNanos(positive("AIIMAGE_LLM_KEY_MIN_INTERVAL_MS", 9, 50L));
private static final long ACQUIRE_TIMEOUT_NANOS =
TimeUnit.MILLISECONDS.toNanos(positiveLong("AIIMAGE_LLM_KEY_ACQUIRE_TIMEOUT_MS", 120_000L));
TimeUnit.MILLISECONDS.toNanos(positive("AIIMAGE_LLM_KEY_ACQUIRE_TIMEOUT_MS", 9, 120_000L));
private static final ConcurrentHashMap<String, Gate> GATES = new ConcurrentHashMap<>();
@@ -92,36 +93,17 @@ public final class LlmKeyGate {
private static String fingerprint(String apiKey) {
String key = apiKey == null ? "" : apiKey.trim();
try {
byte[] digest = MessageDigest.getInstance("SHA-256")
.digest(key.getBytes(StandardCharsets.UTF_8));
StringBuilder hex = new StringBuilder(digest.length * 2);
for (byte b : digest) {
hex.append(String.format("%02x", b));
}
return hex.toString();
} catch (NoSuchAlgorithmException ex) {
// SHA-256 是 JDK 必备算法;真缺失时退化为长度+哈希码,仍不落原文
return "fallback:" + key.length() + ":" + key.hashCode();
}
return DigestUtil.sha256Hex(key);
}
private static int positiveInt(String name, int fallback) {
try {
int value = Integer.parseInt(System.getenv().getOrDefault(name, "").trim());
return value > 0 ? value : fallback;
} catch (RuntimeException ex) {
return fallback;
}
}
private static long positiveLong(String name, long fallback) {
try {
long value = Long.parseLong(System.getenv().getOrDefault(name, "").trim());
return value > 0 ? value : fallback;
} catch (RuntimeException ex) {
/** 环境变量读数:非纯数字/可能越界/非正一律走默认,坏配置不许把闸门拖崩。 */
private static long positive(String name, int maxDigits, long fallback) {
String raw = System.getenv().getOrDefault(name, "").trim();
if (raw.length() > maxDigits || !POSITIVE.matcher(raw).matches()) {
return fallback;
}
long value = Long.parseLong(raw);
return value > 0 ? value : fallback;
}
private static final class Gate {
@@ -123,6 +123,8 @@ public class AdminApiGuardFilter extends OncePerRequestFilter {
// 2026-09 全维度审查补:内部端点此前仅靠 controller 自校验令牌,纳入守卫后
// 不带令牌的请求直接 401(带可信令牌的仍由 doFilterInternal 放行)
"/api/internal",
// 损耗监控的暂停/继续:uid 只从 JWT 取,匿名请求既读不到状态也不该能解除暂停闸
"/api/user/cost-guard",
};
private final AdminAuthSupport adminAuthSupport;
@@ -5,7 +5,6 @@ import com.nanri.aiimage.modules.usersecret.model.dto.UserSecretUsageAggregate;
import com.nanri.aiimage.modules.usersecret.model.entity.UserSecretUsageEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.time.LocalDate;
import java.util.List;
@@ -40,28 +39,9 @@ public interface UserSecretUsageMapper extends BaseMapper<UserSecretUsageEntity>
* 当日用量按用户 × 模块聚合(损耗扫描每分钟读一次)。
* 表是「用户 × 模块 × 天」的日聚合,行数 = 用户数 × 3,全量 GROUP BY 成本可忽略。
*/
@Select("""
SELECT user_id AS userId,
module_key AS moduleKey,
COALESCE(SUM(call_count), 0) AS callCount,
COALESCE(SUM(prompt_tokens + completion_tokens), 0) AS tokens
FROM biz_user_secret_usage_daily
WHERE business_date = #{businessDate}
GROUP BY user_id, module_key
""")
List<UserSecretUsageAggregate> sumByUserAndModule(@Param("businessDate") LocalDate businessDate);
/** 单个用户的当日读数:客户端状态接口每 30 秒轮询一次,不能为此全表 GROUP BY。 */
@Select("""
SELECT user_id AS userId,
module_key AS moduleKey,
COALESCE(SUM(call_count), 0) AS callCount,
COALESCE(SUM(prompt_tokens + completion_tokens), 0) AS tokens
FROM biz_user_secret_usage_daily
WHERE business_date = #{businessDate}
AND user_id = #{userId}
GROUP BY user_id, module_key
""")
List<UserSecretUsageAggregate> sumForUser(@Param("userId") Long userId,
@Param("businessDate") LocalDate businessDate);
@@ -2,27 +2,14 @@ package com.nanri.aiimage.modules.usersecret.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.nanri.aiimage.modules.usersecret.model.entity.UserTaskGuardEntity;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;
@Mapper
public interface UserTaskGuardMapper extends BaseMapper<UserTaskGuardEntity> {
/** 幂等暂停:无记录则建,有记录则刷新原因与时刻。 */
@Insert("""
INSERT INTO biz_user_task_guard (user_id, paused, reason, paused_at, updated_at)
VALUES (#{userId}, 1, #{reason}, NOW(), NOW())
ON DUPLICATE KEY UPDATE paused = 1, reason = #{reason},
paused_at = NOW(), updated_at = NOW()
""")
int pause(@Param("userId") Long userId, @Param("reason") String reason);
@Update("""
UPDATE biz_user_task_guard
SET paused = 0, reason = '', paused_at = NULL, updated_at = NOW()
WHERE user_id = #{userId}
""")
int resume(@Param("userId") Long userId);
}
@@ -150,17 +150,12 @@ public class CostGuardScheduler {
if (userId == null || userId <= 0) {
return new UserTotals();
}
try {
UserTotals totals = new UserTotals();
for (UserSecretUsageAggregate row : usageMapper.sumForUser(userId, LocalDate.now())) {
accumulate(totals, row);
}
return totals;
} catch (Exception ex) {
// 状态接口是客户端每 30 秒一次的轮询:读不到就返回零值,不让一次库抖动打断提示
log.warn("[cost-guard] 读取用户当日用量失败 userId={} err={}", userId, ex.getMessage());
return new UserTotals();
// 库抖动就让它抛:客户端把轮询失败当「维持上一次读数」处理,比在这里伪造一组零值诚实。
UserTotals totals = new UserTotals();
for (UserSecretUsageAggregate row : usageMapper.sumForUser(userId, LocalDate.now())) {
accumulate(totals, row);
}
return totals;
}
private String breachReason(UserTotals now, UserTotals before, WindowDelta delta) {
@@ -205,12 +200,8 @@ public class CostGuardScheduler {
return;
}
lastSharedRefreshAt = now;
try {
sharedSecretMonitorService.refresh();
} catch (Exception ex) {
// 共享密钥扫描失败不影响损耗暂停主流程,下一轮再试
log.warn("[cost-guard] 共享密钥扫描失败 err={}", ex.getMessage(), ex);
}
// 不自己吞:runOnce 里这是最后一步,抛出由 scan() 的 [cost-guard] WARN 记名,下一间隔再试
sharedSecretMonitorService.refresh();
}
public static final class UserTotals {
@@ -1,5 +1,6 @@
package com.nanri.aiimage.modules.usersecret.service;
import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nanri.aiimage.config.CostGuardProperties;
import com.nanri.aiimage.modules.notification.service.NotificationDispatchService;
@@ -12,13 +13,10 @@ import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.HexFormat;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@@ -115,7 +113,7 @@ public class SharedSecretMonitorService {
private void pushDailyWarning(Long userId, UserSecretModule module, String groupKey,
SharedSecretWarningVo warning) {
String day = LocalDate.now().format(DateTimeFormatter.BASIC_ISO_DATE);
String dedupe = "secret_shared:" + module.key() + ':' + sha256Hex(groupKey)
String dedupe = "secret_shared:" + module.key() + ':' + DigestUtil.sha256Hex(groupKey)
+ ':' + userId + ':' + day;
notificationDispatchService.pushToUser(userId, NotificationService.SCENE_SECRET_SHARED,
NotificationService.LEVEL_WARNING,
@@ -130,15 +128,6 @@ public class SharedSecretMonitorService {
+ "建议在「密钥设置」里为不同账号拆分独立密钥,便于控费和定位问题。";
}
private String sha256Hex(String value) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
return HexFormat.of().formatHex(digest.digest(value.getBytes(StandardCharsets.UTF_8)));
} catch (Exception ex) {
return Integer.toHexString(value.hashCode());
}
}
private static final class SecretGroup {
private final UserSecretModule module;
private final Set<Long> userIds = new HashSet<>();
@@ -71,20 +71,17 @@ public class UserTaskGuardService {
}
}
/** 用户手动继续:只清暂停位,不删任何已完成的任务/结果。 */
public boolean resume(Long userId) {
/**
* 用户手动继续:只清暂停位,不删任何已完成的任务/结果。
* 写失败一律外抛——静默返回 false 会让接口把「没解开」回成「已继续任务」。
*/
public void resume(Long userId) {
if (userId == null || userId <= 0) {
return false;
}
try {
guardMapper.resume(userId);
cache.remove(userId);
log.info("[cost-guard] 用户手动继续任务 userId={}", userId);
return true;
} catch (Exception ex) {
log.error("[cost-guard] 继续任务写入失败 userId={} err={}", userId, ex.getMessage(), ex);
return false;
return;
}
guardMapper.resume(userId);
cache.remove(userId);
log.info("[cost-guard] 用户手动继续任务 userId={}", userId);
}
private GuardState load(Long userId) {
@@ -22,4 +22,23 @@
updated_at = NOW()
WHERE user_id = #{userId} AND module_key = #{moduleKey} AND business_date = #{businessDate}
</update>
<select id="sumByUserAndModule" resultType="com.nanri.aiimage.modules.usersecret.model.dto.UserSecretUsageAggregate">
SELECT user_id AS userId,
module_key AS moduleKey,
COALESCE(SUM(call_count), 0) AS callCount,
COALESCE(SUM(prompt_tokens + completion_tokens), 0) AS tokens
FROM biz_user_secret_usage_daily
WHERE business_date = #{businessDate}
GROUP BY user_id, module_key
</select>
<select id="sumForUser" resultType="com.nanri.aiimage.modules.usersecret.model.dto.UserSecretUsageAggregate">
SELECT user_id AS userId,
module_key AS moduleKey,
COALESCE(SUM(call_count), 0) AS callCount,
COALESCE(SUM(prompt_tokens + completion_tokens), 0) AS tokens
FROM biz_user_secret_usage_daily
WHERE business_date = #{businessDate}
AND user_id = #{userId}
GROUP BY user_id, module_key
</select>
</mapper>
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.nanri.aiimage.modules.usersecret.mapper.UserTaskGuardMapper">
<insert id="pause">
INSERT INTO biz_user_task_guard (user_id, paused, reason, paused_at, updated_at)
VALUES (#{userId}, 1, #{reason}, NOW(), NOW())
ON DUPLICATE KEY UPDATE paused = 1, reason = #{reason},
paused_at = NOW(), updated_at = NOW()
</insert>
<update id="resume">
UPDATE biz_user_task_guard
SET paused = 0, reason = '', paused_at = NULL, updated_at = NOW()
WHERE user_id = #{userId}
</update>
</mapper>
@@ -59,7 +59,9 @@ class MigratedSqlEquivalenceTest {
// 67 → 78:keywordshare 模块新写 11 条 select,逐条登记进下方 NOT_MIGRATED。
// 78 → 86:brandgating 模块新写 8 条 select,同样逐条点入
// (与 SqlLayeringConventionTest.XML_STATEMENT_TOTAL 同轮 99 → 115 → 124)。
private static final int XML_SELECT_TOTAL = 86;
// 86 → 88:损耗监控在 UserSecretUsageMapper 新写 2 条聚合 select,逐条点入
// (SqlLayeringConventionTest.XML_STATEMENT_TOTAL 同轮 124 → 126)。
private static final int XML_SELECT_TOTAL = 88;
/**
* 不是「从注解搬过来」的语句:注解时代没有原文可对撞,等价性这支只能跳过它们。
@@ -92,7 +94,10 @@ class MigratedSqlEquivalenceTest {
"BrandGatingMapper#countDistinctBrands",
"BrandGatingMapper#countGroupByType",
"BrandGatingMapper#countGroupByCountry",
"BrandGatingMapper#countGroupBySource");
"BrandGatingMapper#countGroupBySource",
// 损耗监控(2026-10-24)新写的 2 条聚合 select:当日全量扫描 + 单用户读数。
"UserSecretUsageMapper#sumByUserAndModule",
"UserSecretUsageMapper#sumForUser");
/** 账本 5 列:接口 / 方法 / @Select 种类 / 是否含常量插值 / 注解原文(Java 反转义后那条串)。 */
private static final int LEDGER_COLUMNS = 5;
@@ -75,7 +75,10 @@ class SqlLayeringConventionTest {
// 99 → 115:keywordshare 模块(五国关键词共享网页化,5 份 XML 16 条)新写语句。
// 115 → 124:brandgating 模块(品牌Gating库并入管理后台,1 份 XML 9 条)新写语句;
// 两批 19 条 select 逐条登记进 MigratedSqlEquivalenceTest.NOT_MIGRATED(67 → 78 → 86)。
private static final int XML_STATEMENT_TOTAL = 124;
// 124 → 126:损耗监控在 {@code UserSecretUsageMapper} 新写 2 条聚合 select
// (当日全量扫描 + 单用户读数),同样逐条登记进 NOT_MIGRATED(86 → 88)。
// 126 → 128:同轮 {@code UserTaskGuardMapper} 的 pause/resume 两条写侧语句从注解迁进 XML。
private static final int XML_STATEMENT_TOTAL = 128;
/**
* wrapper 里夹带函数级 SQL 的现状("这不是简单单表"的证据),文件名 → 条数:
@@ -65,7 +65,8 @@ class AdminGuardSwitchConfigTest {
private static final String ANCHOR = AdminApiGuardFilter.SWITCH_ANCHOR;
private static final int USER_TOOL_PREFIX_COUNT = 15;
private static final int SELF_SERVICE_PREFIX_COUNT = 6;
// 6 → 7:损耗监控 /api/user/cost-guard 进无条件名单(uid 只从 JWT 取,匿名不该能解除暂停闸)。
private static final int SELF_SERVICE_PREFIX_COUNT = 7;
/** {@code ${ENV:默认}} 形状;只取默认段,判的是出厂值,不受跑测试的机器上的环境变量影响。 */
private static final Pattern PLACEHOLDER = Pattern.compile("^\\$\\{([^:}]+):([^}]*)\\}$");
@@ -47,8 +47,9 @@ class CatchSilenceRatchetTest {
// ==== 验收基线(用户 2026-10-08 12:0x 实测,HEAD 274ad017,单位:处)====
// 10-17 心跳证据两处 fail-open 抬 559 -> 561,逐处出处见 STRICT_NOT_RETHROW 上方注释。
// 10-18 外观专利失败兜底自吞抬 561 -> 562,逐处出处同下。
// 10-24 损耗监控三处带锚点的 fail-open 抬 562 -> 564,逐处出处见 STRICT_NOT_RETHROW 上方注释。
static final int GIVEN_TOTAL = 1000;
static final int GIVEN_NOT_RETHROW = 562;
static final int GIVEN_NOT_RETHROW = 564;
static final int GIVEN_LOG_ONLY = 249;
static final int GIVEN_EMPTY = 56;
@@ -71,6 +72,28 @@ class CatchSilenceRatchetTest {
* 契约明确要求「实现应自吞异常(兜底失败只记日志,不影响 job 已终态失败的事实)」,与
* DeleteBrandRunService 同名兜底同形状;Worker 侧另有 catch 双保险,让判死作业的兜底每轮
* stuck 扫描幂等重入。实测 1004。
* 10-24 损耗监控(cost-guard)本笔新增 6 处,逐处点名(同批先写后摘、没留在树上的 6 处:
* SHA-256 指纹改走 {@code DigestUtil.sha256Hex}、env 读数改纯正则前置校验、共享密钥扫描的
* 自吞交给 {@code scan()} 已有的那枚、当日用量读数改成直接抛(客户端把轮询失败当
* 「维持上一次读数」处理,比伪造一组零值诚实)、{@code resume()} 的吞异常改成外抛——
* 它返回 false 时控制器仍回「已继续任务」,那是假成功):
* <ul>
* <li>{@code LlmKeyGate.Gate.acquire()} 两枚:{@code InterruptedException} ⇒ 复位中断位并重抛
* IllegalStateException;{@code RuntimeException} ⇒ 先 {@code permits.release()} 再原样重抛,
* 少了这一处排队令牌就永久漏一个(两枚都是 RETHROW 形状);</li>
* <li>{@code LlmKeyGate.Gate.waitForInterval()} 一枚:同为 InterruptedException 重抛形状;</li>
* <li>{@code CostGuardScheduler.scan()} 一枚:调度线程整轮异常只打 {@code [cost-guard]} WARN,
* 下一轮照跑——让它抛出等于这条监控自己变成噪声源;</li>
* <li>{@code UserTaskGuardService.pause()} 一枚:单个账号的暂停写失败不能中止整轮扫描
* (否则排在这之后的账号这轮全不被检查),下一轮还会再判一次;</li>
* <li>{@code UserTaskGuardService.load()} 一枚:读不到闸门状态按「未暂停」走——数据库抖动
* 不该把全体用户的合法任务一起冻住,真失控由下一轮重新置位。</li>
* </ul>
* 本格实测 1004:这 6 处落进的是 1004 之前已被更早删腾出、无人认领的额度里(上一笔整树实测
* 997,见 RETHROW_FLOOR 上方),所以基线不抬反而按实测收到 1003;**再 +1 是同一时段
* {@code ba20f5ed feat(workbench-client)} 那枚新增 catch,不属本批、也未由本批记名**,
* 该由那一笔自己把出处补进这份 diff。每一处本批的新增都在这里点名。
* 不重抛/只 log/空体三格上限一个字没动。
*/
static final int STRICT_TOTAL = 1004;
/**
@@ -144,15 +167,28 @@ class CatchSilenceRatchetTest {
* 与 {@code DeleteBrandRunService.fallbackAssembleResultFile} 同形状、同性质:兜底失败不得掩盖
* 作业已终态失败的事实,也不得打断 stuck 扫描对终态失败作业的幂等重入。形状是 log+return false,
* 因此只进本格与 total,不进 STRICT_LOG_ONLY(严格口径不数 log 后接 return)。
* 10-24 抬 562 -> 564,逐处记名(损耗监控 cost-guard 的三处 fail-open,都带 {@code [cost-guard]}
* 日志锚点、巡检按锚点数得到,不是「异常到此为止外面看不见」那种无声出口):
* 1){@code CostGuardScheduler.scan()}:整轮扫描异常只 WARN——这条监控抛出就等于自己变成
* 现网噪声源,而下一轮 fixedDelay 照跑,暂停判定不会因此停摆;
* 2){@code UserTaskGuardService.pause()}:单个账号的暂停写失败若外抛,{@code runOnce} 的
* 逐账号循环就地中止,排在这之后的账号这一轮全部不被检查(最坏晚一个间隔才暂停=多烧钱);
* 3){@code UserTaskGuardService.load()}:读不到闸门状态按「未暂停」走——一次库抖动不该把
* 全体用户的合法任务一起冻住,真失控由下一轮扫描重新置位(方向是「少冻一个号」)。
* 其中 1)、2) 块体只有一句 log,故同时进 STRICT_LOG_ONLY;3) 是 log+return,不进。
* 同批先写后摘、没留在树上的 6 处静默出口见 total 那一格的点名。
*/
static final int STRICT_NOT_RETHROW = 562;
static final int STRICT_NOT_RETHROW = 564;
/**
* 10-17 抬 172 -> 173,出处就是上面点名过的 {@code TaskHeartbeatLivenessService.stamp()}:
* 块体只有「取到 key 名 + log.warn」一句,写心跳证据失败时既不重试也不抛出——
* 心跳接口不许因为「记不下证据」而 500。同类的 {@code lastHeartbeatMillis()} 读失败还要
* 落 0 返回,形状是 log+return 默认值,因此不进这一格。
* 10-24 抬 173 -> 175,出处即上面 STRICT_NOT_RETHROW 里点名过的 cost-guard 两处:
* {@code CostGuardScheduler.scan()} 与 {@code UserTaskGuardService.pause()}——块体各只有一句
* {@code log.warn}/{@code log.error},都是带锚点的 fail-open,不是把已有出口改回吞掉。
*/
static final int STRICT_LOG_ONLY = 173; // 严格口径:块内每条顶层语句都是 log 调用才算
static final int STRICT_LOG_ONLY = 175; // 严格口径:块内每条顶层语句都是 log 调用才算
static final int STRICT_EMPTY = 56;
/**