From 233182b0c563368657fe78224099d70e6dad8331 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E8=87=AA=E8=BE=BE?= <980324341@qq.com> Date: Fri, 9 Oct 2026 02:04:16 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E4=B8=8A=E6=9E=B6=E5=93=81=E7=89=8C):=20?= =?UTF-8?q?=E6=B8=85=E6=B4=97=E6=8C=AA=E5=88=B0=20biz=5Fpublish=5Fitem=20?= =?UTF-8?q?=E5=94=AF=E4=B8=80=E5=86=99=E5=BA=93=E5=87=BA=E5=8F=A3=EF=BC=8C?= =?UTF-8?q?=E5=9B=9E=E4=BC=A0=E9=87=8D=E6=8F=92=E4=B8=8D=E5=86=8D=E7=81=8C?= =?UTF-8?q?=E5=9B=9E=E8=84=8F=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 上一笔 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 跑绿。 --- .../publish/service/PublishBrandLabel.java | 34 ++++ .../publish/service/PublishTaskService.java | 2 +- .../service/PublishWorkbookService.java | 18 +- .../service/PublishBrandLabelTest.java | 156 ++++++++++++++++++ 4 files changed, 192 insertions(+), 18 deletions(-) create mode 100644 backend-java/src/main/java/com/nanri/aiimage/modules/publish/service/PublishBrandLabel.java create mode 100644 backend-java/src/test/java/com/nanri/aiimage/modules/publish/service/PublishBrandLabelTest.java diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/publish/service/PublishBrandLabel.java b/backend-java/src/main/java/com/nanri/aiimage/modules/publish/service/PublishBrandLabel.java new file mode 100644 index 000000000..1bbe135cd --- /dev/null +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/publish/service/PublishBrandLabel.java @@ -0,0 +1,34 @@ +package com.nanri.aiimage.modules.publish.service; + +import java.util.regex.Pattern; + +/** + * 上架链路上品牌值的唯一清洗出口:剥掉 {@code Brand: } 这类"字段标签 + 冒号"前缀。 + * + *

亚马逊详情页把卖家名渲染成 "Brand: XXX",用户从页面复制进 Excel 就会带上前缀。 + * 该值进 WIPO 品牌库查询时会被当 Solr 字段限定符({@code undefined field Brand} → 上游 500), + * 10-08 单日被顶着重跑 30 轮、连带把品牌全局锁持到 1h45m,压死 11 个别的用户的任务。 + * + *

清洗必须落在这里而不是某个调用点:{@code biz_publish_item.brand} 有两个写入来源 + * (服务端 Excel 解析、客户端结果分片回传 {@code replaceRows}),历史上只堵了前者, + * 后者在任务完成时整表删后重插,把脏值又写回去。 + */ +public final class PublishBrandLabel { + + /** + * 只剥行首的一个标签,且要求冒号紧跟其后:{@code Brands: NIKE} 与 {@code Brand: NIKE: X} + * 都是真实品牌写法,不能顺手裁掉。 + */ + private static final Pattern LABEL_PREFIX = Pattern.compile( + "(?i)^\\s*(?:brand|品牌|marke|marca|marque|marka)\\s*[::]\\s*"); + + private PublishBrandLabel() { + } + + public static String strip(String value) { + if (value == null || value.isEmpty()) { + return value; + } + return LABEL_PREFIX.matcher(value).replaceFirst("").trim(); + } +} diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/publish/service/PublishTaskService.java b/backend-java/src/main/java/com/nanri/aiimage/modules/publish/service/PublishTaskService.java index 88ecb7ec0..d989a4f50 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/publish/service/PublishTaskService.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/publish/service/PublishTaskService.java @@ -1933,7 +1933,7 @@ public class PublishTaskService { entity.setSourceId(row.getSourceId()); entity.setAsin(row.getAsin()); entity.setCountry(row.getCountry()); - entity.setBrand(row.getBrand()); + entity.setBrand(PublishBrandLabel.strip(row.getBrand())); entity.setPriceValue(row.getPrice()); entity.setStatusValue(row.getStatus()); entity.setSyncStatus(row.getSyncStatus()); diff --git a/backend-java/src/main/java/com/nanri/aiimage/modules/publish/service/PublishWorkbookService.java b/backend-java/src/main/java/com/nanri/aiimage/modules/publish/service/PublishWorkbookService.java index b171fbe3e..d27dc9b37 100644 --- a/backend-java/src/main/java/com/nanri/aiimage/modules/publish/service/PublishWorkbookService.java +++ b/backend-java/src/main/java/com/nanri/aiimage/modules/publish/service/PublishWorkbookService.java @@ -170,7 +170,7 @@ public class PublishWorkbookService { row.setSourceId(values.get(0)); row.setAsin(values.get(1)); row.setCountry(values.get(2)); - row.setBrand(stripBrandLabel(values.get(3))); + row.setBrand(PublishBrandLabel.strip(values.get(3))); row.setPrice(values.get(4)); return row; } @@ -293,22 +293,6 @@ public class PublishWorkbookService { return value.isBlank() ? "publish" : value; } - /** - * 剥掉亚马逊 byline 残留的「Brand:」标签前缀。 - * - * 这类值送进 WIPO 品牌查询会被当成 Solr 字段限定符,返回 - * isError / status 500 / undefined field Brand(2026-10-08 主机 A 实测 19,247 次, - * 不带前缀的查询 0 次)。只剥开头一次:值中间的冒号属品牌自身内容, - * "Brands:" 也不是标签。 - */ - private String stripBrandLabel(String value) { - if (value == null || value.isEmpty()) { - return value; - } - return value.replaceFirst( - "(?i)^\\s*(?:brand|品牌|marke|marca|marque|marka)\\s*[::]\\s*", "").trim(); - } - private String normalize(String value) { if (value == null) { return ""; diff --git a/backend-java/src/test/java/com/nanri/aiimage/modules/publish/service/PublishBrandLabelTest.java b/backend-java/src/test/java/com/nanri/aiimage/modules/publish/service/PublishBrandLabelTest.java new file mode 100644 index 000000000..1a3667982 --- /dev/null +++ b/backend-java/src/test/java/com/nanri/aiimage/modules/publish/service/PublishBrandLabelTest.java @@ -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; + +/** + * 上架品牌值的清洗出口。 + * + *

2026-10-08 的事故链是:Excel 里带 "Brand:" 标签前缀 → WIPO 把它当 Solr 字段限定符 + * ({@code undefined field Brand},单日 19,247 次 500)→ 上游错误信封不是合法 Base64、被客户端 + * 当瞬时限流重跑满 30 轮 → 品牌全局锁被一个任务持到 1h45m → 同日 16 单里 11 单排队等满 1800s 判死。 + * + *

第一版只堵了"服务端解析 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 offenders = new ArrayList<>(); + List 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 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 publishSources() throws IOException { + return mainSources().stream() + .filter(p -> p.toString().replace('\\', '/').contains("/modules/publish/")) + .collect(Collectors.toList()); + } + + private static List mainSources() throws IOException { + Path root = moduleRoot().resolve("src/main/java"); + if (!Files.isDirectory(root)) { + fail("找不到 " + root + " —— 用例依赖以 backend-java 为工作目录运行,别让守卫静默通过"); + } + try (Stream 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); + } + } +}