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;
@@ -0,0 +1,173 @@
package com.nanri.aiimage.modules.appearancepatent.client;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nanri.aiimage.config.AppearancePatentProperties;
import com.nanri.aiimage.config.BrandCheckProperties;
import com.nanri.aiimage.modules.appearancepatent.model.dto.AppearancePatentResultRowDto;
import com.nanri.aiimage.modules.brand.client.BrandCheckClient;
import com.nanri.aiimage.modules.usersecret.service.UserSecretUsageService;
import com.sun.net.httpserver.HttpServer;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* 外观专利合并一行的品牌校验必须走批接口。
*
* 原先 applyResult 里逐行 checkTitleText:一行一个 90 秒耗时预算,十行最坏九百秒,
* 上游 16890 劣化时批间隔被拉到分钟级(同项目货源侧已改成整批共用预算)。
*
* 这两条断言各自守一个坑:
* - times(1) + never():合批真的发生了,而不是把逐行调用换了个名字;
* - 两行结论不同:批内归因停在行,别行的侵权词没有渗进干净行。
*/
class AppearancePatentBrandBatchTest {
private BrandCheckClient brandCheckClient = mock(BrandCheckClient.class);
private final AppearancePatentLlmClient client = new AppearancePatentLlmClient(
new AppearancePatentProperties(),
new ObjectMapper(),
null,
brandCheckClient,
mock(UserSecretUsageService.class)
);
@Test
void mergedBatchAsksBrandCheckOnceAndAttributesPerRow() throws Exception {
// 桩按「本行的词」返回:含阿凡达的行 failedData 非空,另一行为空。
when(brandCheckClient.checkTitleTexts(anyList(), eq("Terms"))).thenAnswer(invocation -> {
List<String> titles = invocation.getArgument(0);
List<BrandCheckClient.BrandCheckBatchResult> results = new ArrayList<>();
for (String title : titles) {
List<Object> failed = title.contains("阿凡达") ? List.of("阿凡达") : List.of();
results.add(new BrandCheckClient.BrandCheckBatchResult(List.of(title), failed, List.of()));
}
return results;
});
List<AppearancePatentResultRowDto> rows = List.of(row("1"), row("2"));
String dataText = """
{"data":[
{"row_id":"1","title":"阿凡达","appearance":"无侵权","result":"无侵权"},
{"row_id":"2","title":"小米","appearance":"无侵权","result":"无侵权"}
]}
""";
List<AppearancePatentResultRowDto> merged = client.mergeRowsFromDataText(rows, dataText);
verify(brandCheckClient, times(1)).checkTitleTexts(anyList(), eq("Terms"));
verify(brandCheckClient, never()).checkTitleText(anyString(), anyString());
assertThat(merged).hasSize(2);
assertThat(merged.get(0).getTitleRisk()).isEqualTo("侵权");
assertThat(merged.get(1).getTitleRisk()).as("干净行不得继承别行的侵权词").isEqualTo("无侵权");
}
@Test
void batchWithNothingToCheckSendsNoBrandQueryAtAll() throws Exception {
List<AppearancePatentResultRowDto> rows = List.of(row("1"));
String dataText = """
{"data":[{"row_id":"1","title":"无","appearance":"无侵权","result":"无侵权"}]}
""";
client.mergeRowsFromDataText(rows, dataText);
verify(brandCheckClient, never()).checkTitleTexts(anyList(), anyString());
verify(brandCheckClient, never()).checkTitleText(anyString(), anyString());
}
/**
* 回退行(商标识别失败 / 外观识别失败)也必须走批接口。
*
* 这条比正常行更贵:回退时查的是**整条原始标题**而不是模型抽出的关键词,
* splitTitleText 会把十几个词逐个排队,还各带一个耗时预算;
* 而回退行偏偏集中在上游劣化的那一轮——正是预算最紧的时候。
*/
@Test
void fallbackRowsAlsoAskBrandCheckOnceForTheWholeBatch() throws Exception {
when(brandCheckClient.checkTitleTexts(anyList(), eq("Terms"))).thenAnswer(invocation -> {
List<String> titles = invocation.getArgument(0);
List<BrandCheckClient.BrandCheckBatchResult> results = new ArrayList<>();
for (String title : titles) {
List<Object> failed = title.contains("阿凡达") ? List.of("阿凡达") : List.of();
results.add(new BrandCheckClient.BrandCheckBatchResult(List.of(title), failed, List.of()));
}
return results;
});
HttpServer llmDown = startUnusableLlm();
try {
AppearancePatentProperties properties = new AppearancePatentProperties();
properties.setLlmHost("http://127.0.0.1:" + llmDown.getAddress().getPort());
properties.setLlmRetryTimes(1);
properties.setLlmReadTimeoutMillis(5_000);
AppearancePatentLlmClient inspectClient = new AppearancePatentLlmClient(
properties, new ObjectMapper(), null, brandCheckClient, mock(UserSecretUsageService.class));
List<AppearancePatentResultRowDto> rows = inspectClient.inspectRows(
List.of(
source("1", "阿凡达 玩具 手办 正版"),
source("2", "小米 充电宝 20000mAh 快充")
),
null,
"test-key");
@SuppressWarnings("unchecked")
ArgumentCaptor<List<String>> titlesCaptor = ArgumentCaptor.forClass(List.class);
verify(brandCheckClient, times(1)).checkTitleTexts(titlesCaptor.capture(), eq("Terms"));
verify(brandCheckClient, never()).checkTitleText(anyString(), anyString());
assertThat(titlesCaptor.getValue())
.containsExactly("阿凡达 玩具 手办 正版", "小米 充电宝 20000mAh 快充");
assertThat(rows).hasSize(2);
for (AppearancePatentResultRowDto row : rows) {
assertThat(row.getAppearanceRisk()).isEqualTo("外观识别异常");
}
assertThat(rows.get(0).getTitleRisk()).isEqualTo("侵权");
assertThat(rows.get(1).getTitleRisk()).as("回退行的归因同样停在行").isEqualTo("无侵权");
} finally {
llmDown.stop(0);
}
}
/** LLM 一律回 502:让每一行都落到回退分支,只观察品牌校验的调用形状。 */
private HttpServer startUnusableLlm() throws IOException {
HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
server.createContext("/v1/chat/completions", exchange -> {
byte[] bytes = "{\"error\":{\"message\":\"bad gateway\"}}".getBytes(StandardCharsets.UTF_8);
exchange.sendResponseHeaders(502, bytes.length);
try (OutputStream os = exchange.getResponseBody()) {
os.write(bytes);
}
});
server.start();
return server;
}
private AppearancePatentResultRowDto source(String id, String title) {
AppearancePatentResultRowDto row = row(id);
row.setTitle(title);
row.setUrl("https://img.example.com/" + id + ".jpg");
return row;
}
private AppearancePatentResultRowDto row(String id) {
AppearancePatentResultRowDto row = new AppearancePatentResultRowDto();
row.setId(id);
row.setAsin("B000000" + id);
row.setCountry("US");
return row;
}
}
@@ -10,7 +10,9 @@ import org.junit.jupiter.api.Test;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
@@ -87,8 +89,10 @@ class AppearancePatentLlmClientTest {
@Test
void callBrandCheckOnlyWhenTitleAndAppearanceArePresent() throws Exception {
BrandCheckClient brandCheckClient = mock(BrandCheckClient.class);
when(brandCheckClient.checkTitleText("阿凡达", "Terms"))
.thenReturn(new BrandCheckClient.BrandCheckBatchResult(List.of("阿凡达"), List.of(), List.of()));
// 一行也走批接口:逐行调用等于把 90 秒耗时预算按行复制
when(brandCheckClient.checkTitleTexts(anyList(), eq("Terms")))
.thenReturn(List.of(new BrandCheckClient.BrandCheckBatchResult(
List.of("阿凡达"), List.of(), List.of())));
AppearancePatentLlmClient llmClient = new AppearancePatentLlmClient(
new AppearancePatentProperties(),
new ObjectMapper(),
@@ -106,7 +110,8 @@ class AppearancePatentLlmClientTest {
assertThat(merged).hasSize(1);
assertThat(merged.get(0).getTitleRisk()).isEqualTo("无侵权");
verify(brandCheckClient).checkTitleText("阿凡达", "Terms");
verify(brandCheckClient).checkTitleTexts(List.of("阿凡达"), "Terms");
verify(brandCheckClient, never()).checkTitleText(anyString(), anyString());
}
@Test
@@ -0,0 +1,151 @@
package com.nanri.aiimage.modules.brand.client;
import com.nanri.aiimage.config.BrandCheckProperties;
import com.sun.net.httpserver.HttpServer;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import static org.assertj.core.api.Assertions.assertThat;
/**
* 一批标题的品牌校验(外观专利热路径:原先每行各调一次 checkTitleText,
* 一行一个 90 秒预算,上游 16890 劣化时十行就是九百秒)。
*
* 合批之后必须钉住三件事,缺一条就是静默错判:
* ① 结论按**本行自己的词**归因,别行的侵权词不得渗进本行;
* ② 同一个词在一批里只查一次(省的是上游请求数,不是行数);
* ③ 空标题行的词集合仍为空——buildTitleRisk 靠 brands().isEmpty() 回退成标题本身,
* 如果被整批去重后的词表污染,判定会从「标题原文」变成「无侵权」。
*/
class BrandCheckClientTitleBatchTest {
@Test
void verdictIsAttributedToEachTitleOwnWords() throws Exception {
Map<String, String> hits = new ConcurrentHashMap<>();
hits.put("阿凡达", "{\"faild_data\":[\"阿凡达\"],\"query_faild_data\":[]}");
HttpServer server = startBrandCheckServer(brandResponses(hits));
try {
List<BrandCheckClient.BrandCheckBatchResult> results =
newClient(server, 3).checkTitleTexts(List.of("阿凡达,玩具", "小米,充电宝"), "Terms");
assertThat(results).hasSize(2);
assertThat(results.get(0).hasFailedData()).as("第 1 行含侵权词").isTrue();
assertThat(results.get(1).hasFailedData()).as("第 2 行不得继承第 1 行的侵权结论").isFalse();
assertThat(results.get(1).hasQueryFailedData()).isFalse();
assertThat(results.get(1).brands()).containsExactly("小米", "充电宝");
} finally {
server.stop(0);
}
}
@Test
void sameWordAcrossTitlesIsQueriedOnce() throws Exception {
// 计数器必须按品牌各自一个:共用一个计数器会让第二个词一进门就落在 count>1,
// 被判成"重试过",量出来的请求数就不是去重结果了。
Map<String, AtomicInteger> counters = new ConcurrentHashMap<>();
HttpServer server = startBrandCheckServer(exchange -> {
String brand = readBrand(exchange);
int count = counters.computeIfAbsent(brand, key -> new AtomicInteger()).incrementAndGet();
respond(exchange, count == 1 ? "{\"faild_data\":[],\"query_faild_data\":[]}"
: "{\"faild_data\":[],\"query_faild_data\":[\"" + brand + "\"]}");
});
try {
List<BrandCheckClient.BrandCheckBatchResult> results =
newClient(server, 3).checkTitleTexts(List.of("阿凡达,玩具", "阿凡达,手机", "阿凡达,周边"), "Terms");
assertThat(counters.get("阿凡达").get()).as("跨行同一个词只查一次").isEqualTo(1);
assertThat(counters).hasSize(4);
assertThat(counters.keySet()).containsExactlyInAnyOrder("阿凡达", "玩具", "手机", "周边");
assertThat(results).hasSize(3);
assertThat(results).allSatisfy(result -> {
assertThat(result.hasQueryFailedData()).as("每个词首轮即成功").isFalse();
assertThat(result.hasFailedData()).isFalse();
});
} finally {
server.stop(0);
}
}
@Test
void blankTitleKeepsEmptyWordSetEvenWhenOtherRowsHaveWords() throws Exception {
HttpServer server = startBrandCheckServer(brandResponses(
Map.of("阿凡达", "{\"faild_data\":[\"阿凡达\"],\"query_faild_data\":[]}")));
try {
List<BrandCheckClient.BrandCheckBatchResult> results =
newClient(server, 3).checkTitleTexts(List.of("", "阿凡达"), "Terms");
assertThat(results.get(0).brands()).as("空标题行的词集合必须为空,否则判定从标题原文变成无侵权").isEmpty();
assertThat(results.get(0).hasFailedData()).isFalse();
assertThat(results.get(1).brands()).containsExactly("阿凡达");
assertThat(results.get(1).hasFailedData()).isTrue();
} finally {
server.stop(0);
}
}
@Test
void queryFailureOnOneRowDoesNotMarkOtherRowsFailed() throws Exception {
// 上游对「任天堂」持续返回查询失败:只有含该词的行判 BRAND_QUERY_FAILED,
// 同批里查成功的行仍判无侵权——归因粒度必须停在行。
Map<String, String> hits = new ConcurrentHashMap<>();
hits.put("任天堂", "{\"faild_data\":[],\"query_faild_data\":[\"任天堂\"]}");
HttpServer server = startBrandCheckServer(brandResponses(hits));
try {
List<BrandCheckClient.BrandCheckBatchResult> results =
newClient(server, 2).checkTitleTexts(List.of("任天堂,主机", "小米,充电宝"), "Terms");
assertThat(results.get(0).hasQueryFailedData()).isTrue();
assertThat(results.get(1).hasQueryFailedData()).as("别行的查询失败不得扩散").isFalse();
} finally {
server.stop(0);
}
}
private com.sun.net.httpserver.HttpHandler brandResponses(Map<String, String> canned) {
return exchange -> {
String brand = readBrand(exchange);
respond(exchange, canned.getOrDefault(brand, "{\"faild_data\":[],\"query_faild_data\":[]}"));
};
}
private String readBrand(com.sun.net.httpserver.HttpExchange exchange) throws IOException {
String body = new String(exchange.getRequestBody().readAllBytes(), StandardCharsets.UTF_8);
int start = body.indexOf("\"brand\"");
int from = body.indexOf('"', start + 7);
int to = body.indexOf('"', from + 1);
return body.substring(from + 1, to);
}
private BrandCheckClient newClient(HttpServer server, int retryTimes) {
BrandCheckProperties props = new BrandCheckProperties();
props.setBaseUrl("http://127.0.0.1:" + server.getAddress().getPort());
props.setPath("/brand_check");
props.setRetryTimes(retryTimes);
props.setRetryIntervalMillis(1);
return new BrandCheckClient(props, null);
}
private HttpServer startBrandCheckServer(com.sun.net.httpserver.HttpHandler handler) throws IOException {
HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
server.createContext("/brand_check", handler);
server.start();
return server;
}
private void respond(com.sun.net.httpserver.HttpExchange exchange, String body) throws IOException {
byte[] bytes = body.getBytes(StandardCharsets.UTF_8);
exchange.getResponseHeaders().set("Content-Type", "application/json; charset=utf-8");
exchange.sendResponseHeaders(200, bytes.length);
try (OutputStream os = exchange.getResponseBody()) {
os.write(bytes);
}
}
}