perf(外观专利): 品牌校验整批共用一次查询与一个超时预算

checkTitleText → checkTitleTexts:一批 10 行原先各带一个 total-timeout-millis(90s)
预算串行,上游 16890 劣化时批间隔被拉到分钟级;改成跨行去重一次查询、整批共用
deadline、结论按行归因、返回与入参同序同长。真正的价值是把爆炸半径从"行"收回"批"。

改动比预估大了一圈:三个回退点(标题缺失 / 标题识别失败 / 外观识别失败)原先也在
逐行发品牌查询,而回退行查的是整条原始标题,splitTitleText 能切出十几个词,逐行发
反而最贵。现在回退行只造结论不写行,与正常行汇进同一条 (row, LlmResult) 队列,
出口收敛到 applyLlmRowsInOneBatch 一处;pending.isEmpty() 的早退分支同样要刷,
否则回退行连品牌结论都拿不到。单参 applyResult 重载已删(零调用方 + 编译验证)。

行数不变、LLM 请求次数不变、代理提取次数不变 ⇒ 零成本。语义变化只有当初拍过的那条:
同批同品牌共享一次结论。

护栏 AppearancePatentBrandBatchTest 3 例 + BrandCheckClientTitleBatchTest 4 例,
断言各守一个坑:times(1)+never(checkTitleText) 证明合批真发生、两行结论不同证明
批内归因停在行;变异自证 6/6。后端全量 3631 例 Failures 0 Errors 0。
This commit is contained in:
2026-10-04 18:38:27 +08:00
parent 5111858346
commit 3432232ee1
5 changed files with 467 additions and 31 deletions
@@ -23,6 +23,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
@@ -150,17 +151,21 @@ public class AppearancePatentLlmClient {
}
List<AppearancePatentResultRowDto> results = new ArrayList<>(rows.size());
List<Integer> pending = new ArrayList<>(rows.size());
List<AppearancePatentResultRowDto> llmRows = new ArrayList<>();
List<LlmResult> llmResults = new ArrayList<>();
for (AppearancePatentResultRowDto source : rows) {
AppearancePatentResultRowDto row = copy(source);
results.add(row);
String rawTitle = normalize(row.getTitle());
if (rawTitle.isBlank() || normalize(row.getUrl()).isBlank()) {
applyRowFallback(row, rawTitle, firstNonBlank(rawTitle, MISSING_ROW_DATA), "");
llmRows.add(row);
llmResults.add(fallbackResult(row, rawTitle, firstNonBlank(rawTitle, MISSING_ROW_DATA), ""));
} else {
pending.add(results.size() - 1);
}
}
if (pending.isEmpty()) {
applyLlmRowsInOneBatch(llmRows, llmResults);
return results;
}
Map<Integer, String> keywordsByKey = new LinkedHashMap<>();
@@ -176,8 +181,9 @@ public class AppearancePatentLlmClient {
}
// 标题识别失败时本行必走回退分支(外观结果会被丢弃),再发外观请求只会在劣化中的上游白等一轮重试
log.warn("[appearance-patent] 标题识别失败,跳过外观识别(本行同样走回退)asin={}", rows.get(index).getAsin());
applyRowFallback(results.get(index), normalize(rows.get(index).getTitle()),
firstNonBlank(titleErrors.get(index), "商标识别失败"), "");
llmRows.add(results.get(index));
llmResults.add(fallbackResult(results.get(index), normalize(rows.get(index).getTitle()),
firstNonBlank(titleErrors.get(index), "商标识别失败"), ""));
}
Map<Integer, AppearanceJudgement> judgements = new LinkedHashMap<>();
Map<Integer, String> appearanceErrors = new LinkedHashMap<>();
@@ -189,18 +195,33 @@ public class AppearancePatentLlmClient {
String rawTitle = normalize(source.getTitle());
AppearanceJudgement judgement = judgements.get(index);
if (judgement == null) {
applyRowFallback(results.get(index), rawTitle, firstNonBlank(rawTitle, MISSING_ROW_DATA),
firstNonBlank(appearanceErrors.get(index), "外观识别失败"));
llmRows.add(results.get(index));
llmResults.add(fallbackResult(results.get(index), rawTitle,
firstNonBlank(rawTitle, MISSING_ROW_DATA),
firstNonBlank(appearanceErrors.get(index), "外观识别失败")));
continue;
}
String keywords = keywordsByKey.get(index);
applyResult(results.get(index), new LlmResult(
llmRows.add(results.get(index));
llmResults.add(new LlmResult(
source.getGroupKey(), source.getId(), source.getAsin(), source.getCountry(),
keywords, judgement.status(), "", "", "", keywords, judgement.reason(), "", ""));
}
applyLlmRowsInOneBatch(llmRows, llmResults);
return results;
}
/**
* 整批一次品牌查询后逐行落结论:需要查的行共用一个耗时预算,
* 结论仍按行自己的词集合归因(见 brandChecksFor)。
*/
private void applyLlmRowsInOneBatch(List<AppearancePatentResultRowDto> llmRows, List<LlmResult> llmResults) {
List<BrandCheckClient.BrandCheckBatchResult> brandChecks = brandChecksFor(llmResults);
for (int k = 0; k < llmRows.size(); k++) {
applyResult(llmRows.get(k), llmResults.get(k), brandChecks.get(k));
}
}
private BatchOutcome<String> invokeTitleBatchSafely(List<AppearancePatentResultRowDto> rows,
List<Integer> chunk,
String apiKey) {
@@ -340,12 +361,13 @@ public class AppearancePatentLlmClient {
/**
* 行级回退分支:与原 LLM 工作流"默认值"语义对齐——appearance=外观识别异常、
* title 保留原始标题、title_reason/appearance_reason 填充对应错误信息。
* 只造结论不写行,交给 applyLlmRowsInOneBatch 与正常行合用同一次品牌查询。
*/
private AppearancePatentResultRowDto applyRowFallback(AppearancePatentResultRowDto row,
String title,
String titleReason,
String appearanceReason) {
LlmResult result = new LlmResult(
private LlmResult fallbackResult(AppearancePatentResultRowDto row,
String title,
String titleReason,
String appearanceReason) {
return new LlmResult(
row.getGroupKey(),
row.getId(),
row.getAsin(),
@@ -359,8 +381,6 @@ public class AppearancePatentLlmClient {
appearanceReason,
"",
"");
applyResult(row, result);
return row;
}
/** 一批行合并为一次文本请求;返回的行下标 → 关键词,仅包含通过字面校验的行。 */
@@ -865,8 +885,9 @@ public class AppearancePatentLlmClient {
List<AppearancePatentResultRowDto> merged = new ArrayList<>(rows.size());
boolean allowIndexFallback = results.size() == rows.size() && results.stream().noneMatch(this::hasIdentity);
List<LlmResult> resolved = new ArrayList<>(rows.size());
for (int i = 0; i < rows.size(); i++) {
AppearancePatentResultRowDto row = copy(rows.get(i));
AppearancePatentResultRowDto row = rows.get(i);
LlmResult result = resultByGroupKey.get(normalize(row.getGroupKey()));
if (result == null) {
result = resultByCompositeKey.get(rowKey(row.getId(), row.getAsin(), row.getCountry()));
@@ -883,7 +904,12 @@ public class AppearancePatentLlmClient {
if (result == null && allowIndexFallback && i < results.size()) {
result = results.get(i);
}
applyResult(row, result);
resolved.add(result);
}
List<BrandCheckClient.BrandCheckBatchResult> brandChecks = brandChecksFor(resolved);
for (int i = 0; i < rows.size(); i++) {
AppearancePatentResultRowDto row = copy(rows.get(i));
applyResult(row, resolved.get(i), brandChecks.get(i));
merged.add(row);
}
return merged;
@@ -906,14 +932,45 @@ public class AppearancePatentLlmClient {
|| !normalize(result.asin()).isBlank();
}
void applyResult(AppearancePatentResultRowDto row, LlmResult result) {
/**
* 一批行共用一次品牌查询:整批共用一个耗时预算、同一个词跨行只查一次,
* 返回值与入参同序,不需要查的行返回 null。
*
* 预算从「每行一个」收回「每批一个」的代价是上游劣化时同批一起判查询失败,
* 与货源侧 CollectDataBrandBatchFilter 已上线的口径一致;归因仍停在行,
* 别行的侵权词不会渗进干净行(BrandCheckClientTitleBatchTest 守这条)。
*/
private List<BrandCheckClient.BrandCheckBatchResult> brandChecksFor(List<LlmResult> results) {
List<Integer> pending = new ArrayList<>();
List<String> titles = new ArrayList<>();
for (int i = 0; i < results.size(); i++) {
LlmResult result = results.get(i);
// 与 applyResult 走品牌分支的条件严格一致:LLM 已判「无」的行结论不看品牌,
// 替它发一次上游查询就是白烧 16890 请求。
if (!isNoTitleRisk(result) && shouldCheckBrand(result)) {
pending.add(i);
titles.add(result.title());
}
}
BrandCheckClient.BrandCheckBatchResult[] byRowIndex =
new BrandCheckClient.BrandCheckBatchResult[results.size()];
if (!pending.isEmpty()) {
List<BrandCheckClient.BrandCheckBatchResult> batch = brandCheckClient.checkTitleTexts(titles, "Terms");
for (int k = 0; k < pending.size(); k++) {
byRowIndex[pending.get(k)] = batch.get(k);
}
}
return Arrays.asList(byRowIndex);
}
void applyResult(AppearancePatentResultRowDto row, LlmResult result,
BrandCheckClient.BrandCheckBatchResult brandCheck) {
if (row == null || result == null) {
return;
}
if (isNoTitleRisk(result)) {
row.setTitleRisk(NO_INFRINGEMENT);
} else if (shouldCheckBrand(result)) {
BrandCheckClient.BrandCheckBatchResult brandCheck = brandCheckClient.checkTitleText(result.title(), "Terms");
row.setTitleRisk(buildTitleRisk(result.title(), brandCheck));
} else {
row.setTitleRisk("");
@@ -16,8 +16,10 @@ import com.nanri.aiimage.config.HttpClientPool;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;
import java.util.concurrent.ExecutorService;
@@ -95,25 +97,73 @@ public class BrandCheckClient {
public BrandCheckBatchResult checkAll(List<String> brands, String strategy) {
List<String> distinctBrands = distinctNonBlank(brands);
// 整批共用一个耗时预算:上游 16890 卡死时,单品牌 10 次重试曾把一次分片回传拖到
// 103.5 秒(taskId 28599),客户端重试预算耗尽后中止了整个采集。预算用尽即停止重试。
long budgetMillis = properties.getTotalTimeoutMillis();
long deadlineNanos = budgetMillis > 0L ? System.nanoTime() + budgetMillis * 1_000_000L : Long.MAX_VALUE;
List<CompletableFuture<BrandCheckOutcome>> futures = new ArrayList<>(distinctBrands.size());
for (String brand : distinctBrands) {
futures.add(CompletableFuture.supplyAsync(
() -> checkOneBrand(brand, strategy, deadlineNanos), checkExecutor));
}
List<Object> failedData = new ArrayList<>();
List<Object> queryFailedData = new ArrayList<>();
for (CompletableFuture<BrandCheckOutcome> future : futures) {
BrandCheckOutcome outcome = awaitOutcome(future);
for (BrandCheckOutcome outcome : checkWords(distinctBrands, strategy).values()) {
failedData.addAll(outcome.failedData());
queryFailedData.addAll(outcome.queryFailedData());
}
return new BrandCheckBatchResult(distinctBrands, failedData, queryFailedData);
}
/**
* 一批标题一次查完:整批共用一个耗时预算、同一个词跨行只查一次,
* 但**结论按每行自己的词集合归因**(别行的侵权词/查询失败不得渗进本行)。
*
* 放在这里而不是让调用方各自拼:外观专利原先逐行 checkTitleText,一行一个 90 秒预算,
* 十行最坏九百秒;预算收回整批共用后爆炸半径从「行」回到「批」,
* 与货源侧 CollectDataBrandBatchFilter 已上线的口径一致。
* 返回值与入参同序同长,空标题行返回空词集合(调用方据此回退成标题原文)。
*/
public List<BrandCheckBatchResult> checkTitleTexts(List<String> titles, String strategy) {
if (titles == null || titles.isEmpty()) {
return List.of();
}
List<List<String>> wordsByTitle = new ArrayList<>(titles.size());
Set<String> distinctWords = new LinkedHashSet<>();
for (String title : titles) {
List<String> words = splitTitleText(title);
wordsByTitle.add(words);
distinctWords.addAll(words);
}
Map<String, BrandCheckOutcome> outcomes = checkWords(new ArrayList<>(distinctWords), strategy);
List<BrandCheckBatchResult> results = new ArrayList<>(titles.size());
for (List<String> words : wordsByTitle) {
List<Object> failedData = new ArrayList<>();
List<Object> queryFailedData = new ArrayList<>();
for (String word : words) {
BrandCheckOutcome outcome = outcomes.get(word);
if (outcome != null) {
failedData.addAll(outcome.failedData());
queryFailedData.addAll(outcome.queryFailedData());
}
}
results.add(new BrandCheckBatchResult(words, failedData, queryFailedData));
}
return results;
}
/**
* 并发查一批**已去重**的词,整批共用一个耗时预算。
* 键顺序即入参顺序,checkAll 的并集顺序因此与改动前一致。
*/
private Map<String, BrandCheckOutcome> checkWords(List<String> brands, String strategy) {
// 整批共用一个耗时预算:上游 16890 卡死时,单品牌 10 次重试曾把一次分片回传拖到
// 103.5 秒(taskId 28599),客户端重试预算耗尽后中止了整个采集。预算用尽即停止重试。
long budgetMillis = properties.getTotalTimeoutMillis();
long deadlineNanos = budgetMillis > 0L ? System.nanoTime() + budgetMillis * 1_000_000L : Long.MAX_VALUE;
Map<String, BrandCheckOutcome> outcomes = new LinkedHashMap<>();
List<CompletableFuture<BrandCheckOutcome>> futures = new ArrayList<>(brands.size());
for (String brand : brands) {
futures.add(CompletableFuture.supplyAsync(
() -> checkOneBrand(brand, strategy, deadlineNanos), checkExecutor));
}
for (int index = 0; index < brands.size(); index++) {
outcomes.put(brands.get(index), awaitOutcome(futures.get(index)));
}
return outcomes;
}
private BrandCheckOutcome checkOneBrand(String brand, String strategy, long deadlineNanos) {
int attempts = Math.max(1, properties.getRetryTimes());
BrandCheckResponse response = null;