fix(上架品牌): 清洗挪到 biz_publish_item 唯一写库出口,回传重插不再灌回脏值

上一笔 15e04511 只堵了服务端解析 Excel 那一个入口。现网取证(10-09 01:3x,主机A 业务库)显示
脏值有两个来源:7 个脏值任务里 5 个的行创建时间等于任务完成时刻,即任务完成时
PublishTaskService.replaceRows 用客户端回传的分片整表删后重插写回的路径。也就是说每完成一个
上架任务,"Brand:" 就会从回传值里再落库一次,存量清洗会被自己顶回去。

改为:新增 PublishBrandLabel 作为标签正则的唯一持有者,insertRows 写实体前清洗(replaceRows
与解析路径都汇到这里),PublishWorkbookService 删掉本地那份正则改为委派。

配套 PublishBrandLabelTest 5 例:标签形状与反例("Brands: NIKE"、"Super Brand: Nike"、
"Brand: NIKE: X")、publish 模块内 entity.setBrand 出口必须走清洗、全模块只允许一份标签正则。
四条变异实跑全杀(去锚 / strip 原值返回 / 出口不清洗 / 解析层另抄正则);publish 相关
11 个测试类 101 例在独立 worktree 跑绿。
This commit is contained in:
2026-10-09 02:04:16 +08:00
parent 981221c884
commit 233182b0c5
4 changed files with 192 additions and 18 deletions
@@ -0,0 +1,156 @@
package com.nanri.aiimage.modules.publish.service;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
/**
* 上架品牌值的清洗出口。
*
* <p>2026-10-08 的事故链是:Excel 里带 "Brand:" 标签前缀 → WIPO 把它当 Solr 字段限定符
* ({@code undefined field Brand},单日 19,247 次 500)→ 上游错误信封不是合法 Base64、被客户端
* 当瞬时限流重跑满 30 轮 → 品牌全局锁被一个任务持到 1h45m → 同日 16 单里 11 单排队等满 1800s 判死。
*
* <p>第一版只堵了"服务端解析 Excel"那一个入口,漏了更常用的一个:任务完成时
* {@code PublishTaskService.replaceRows} 会用客户端回传的分片整表删后重插,
* 脏值照样落库(现网 7 个脏值来源任务里 5 个的行创建时间 == 任务完成时刻,即由这条路径写入)。
* 所以护栏从"注释里提醒调用方"改成"落在唯一写库出口 + 可实跑的用例"。
*/
class PublishBrandLabelTest {
private static final String OUTLET_CLASS = "PublishBrandLabel.java";
/** 标签正则的特征片段:出现第二处就等于开了第二条会漂移的副本。 */
private static final String REGEX_SIGNATURE = "marque|marka";
@Test
void stripsLabelPrefixButKeepsLookalikes() {
assertThat(PublishBrandLabel.strip("Brand: NIKE")).isEqualTo("NIKE");
assertThat(PublishBrandLabel.strip(" brand : NIKE ")).isEqualTo("NIKE");
assertThat(PublishBrandLabel.strip("品牌:耐克")).isEqualTo("耐克");
assertThat(PublishBrandLabel.strip("MARCA: X")).isEqualTo("X");
// "Brands:" 不是标签;值中间的冒号属于品牌自身内容,只剥行首一次
assertThat(PublishBrandLabel.strip("Brands: NIKE")).isEqualTo("Brands: NIKE");
assertThat(PublishBrandLabel.strip("Brand: NIKE: X")).isEqualTo("NIKE: X");
assertThat(PublishBrandLabel.strip("NIKE Brand")).isEqualTo("NIKE Brand");
// 标签只有出现在行首才是"字段名";句中出现的要原样留着,否则把 ^ 锚摘掉(变成"逢 Brand: 就裁")这条就会红
assertThat(PublishBrandLabel.strip("Super Brand: Nike"))
.isEqualTo("Super Brand: Nike");
assertThat(PublishBrandLabel.strip("BrandNIKE")).isEqualTo("BrandNIKE");
assertThat(PublishBrandLabel.strip("")).isEmpty();
assertThat(PublishBrandLabel.strip(null)).isNull();
}
@Test
void pureLabelOnlyValuesCollapseToEmpty() {
// 现网 1,533 个脏值清洗后 0 个变空串(实测),但 "Brand:" 这种纯标签值确实会洗成空。
// 钉住这一条是因为 PublishTaskService.isBlankRow 要求八个字段同时为空才算空行,
// 单靠 brand 变空不会让一行消失 —— 改了这条判据就会静默丢行。
assertThat(PublishBrandLabel.strip("Brand:")).isEmpty();
assertThat(PublishBrandLabel.strip("Brand: ")).isEmpty();
}
@Test
void everyPublishItemBrandWriteGoesThroughTheOutlet() throws IOException {
List<String> offenders = new ArrayList<>();
List<String> written = new ArrayList<>();
for (Path file : publishSources()) {
if (file.getFileName().toString().equals(OUTLET_CLASS)) {
continue;
}
for (String raw : read(file).split("\r?\n", -1)) {
String line = raw.trim();
if (line.startsWith("//") || line.startsWith("*") || line.startsWith("/*")) {
continue;
}
// 只有写进实体的那一行算出口;DTO 之间的搬运(copyRow/toRowDto)不是
if (line.matches("^entity\\.setBrand\\(.*\\);")) {
written.add(file.getFileName() + ": " + line);
if (!line.contains("PublishBrandLabel.strip(")) {
offenders.add(file.getFileName() + ": " + line);
}
}
}
}
// 取样集非空自检:扫描 0 命中时下面这条恒绿,等于没护栏
assertThat(written).as("publish 模块里写 PublishItemEntity.brand 的行应被扫到").isNotEmpty();
assertThat(offenders)
.as("绕过 PublishBrandLabel.strip 的 brand 写入点(客户端回传 replaceRows 会整表重插,"
+ "少堵一处就等于每完成一个任务把脏值写回去一次)")
.isEmpty();
}
@Test
void labelRegexLivesInExactlyOneMainSource() throws IOException {
List<String> carriers = new ArrayList<>();
for (Path file : mainSources()) {
if (read(file).contains(REGEX_SIGNATURE)) {
carriers.add(file.getFileName().toString());
}
}
assertThat(carriers)
.as("品牌标签正则的副本清单 —— 每多一处,下次加语种就要多改一处")
.containsExactly(OUTLET_CLASS);
}
@Test
void parseSideAlsoDelegatesInsteadOfReimplementing() throws IOException {
String text = read(sourceOf("PublishWorkbookService.java"));
assertThat(text).contains("row.setBrand(PublishBrandLabel.strip(values.get(3)));");
assertThat(text).doesNotContain("stripBrandLabel");
}
private static List<Path> publishSources() throws IOException {
return mainSources().stream()
.filter(p -> p.toString().replace('\\', '/').contains("/modules/publish/"))
.collect(Collectors.toList());
}
private static List<Path> mainSources() throws IOException {
Path root = moduleRoot().resolve("src/main/java");
if (!Files.isDirectory(root)) {
fail("找不到 " + root + " —— 用例依赖以 backend-java 为工作目录运行,别让守卫静默通过");
}
try (Stream<Path> walk = Files.walk(root)) {
return walk.filter(Files::isRegularFile)
.filter(p -> p.getFileName().toString().endsWith(".java"))
.collect(Collectors.toList());
}
}
private static Path sourceOf(String fileName) throws IOException {
return mainSources().stream()
.filter(p -> p.getFileName().toString().equals(fileName))
.findFirst()
.orElseThrow(() -> new IllegalStateException("找不到 " + fileName));
}
/** surefire 的工作目录是模块目录;从仓库根或 IDE 跑时退到 backend-java/。 */
private static Path moduleRoot() {
Path here = Path.of("").toAbsolutePath();
if (Files.isDirectory(here.resolve("src/main/java"))) {
return here;
}
Path nested = here.resolve("backend-java");
if (Files.isDirectory(nested.resolve("src/main/java"))) {
return nested;
}
throw new IllegalStateException("无法定位 backend-java 模块目录(cwd=" + here + ")");
}
private static String read(Path file) {
try {
return Files.readString(file, StandardCharsets.UTF_8);
} catch (IOException e) {
throw new IllegalStateException("读取失败: " + file, e);
}
}
}