fix(品牌检测): 一次分片回传领一份 chunk 预算,用尽即转补测队列

上游 16890 卡死时每批各领一个 90 秒总超时,一个 chunk 十几批能叠到十几分钟,
桌面端是在同步等响应的路径上替它扛。现按 chunk 给 10 秒预算,预算内逐批只把
剩余额度交给远程;用尽后未查的品牌一律按查询失败入补测队列,既不按 accepted
放行也不写进无效品牌表。外观合批、品牌补测调度等后台路径仍按 90 秒走。
This commit is contained in:
2026-10-07 13:34:18 +08:00
parent 80b53695dd
commit 54411c6446
6 changed files with 299 additions and 42 deletions
@@ -96,10 +96,22 @@ public class BrandCheckClient {
}
public BrandCheckBatchResult checkAll(List<String> brands, String strategy) {
return checkAll(brands, strategy, properties.getTotalTimeoutMillis());
}
/**
* 调用方自带整批耗时预算(毫秒,{@code <= 0} 表示不限制):预算只掐「重试」,
* 每个品牌的首轮请求始终执行,所以上游只是慢而不是坏时不会因此丢结论。
*
* <p>分片回传用它把一次 POST 的等待上界收到几秒:{@code total-timeout-millis} 那 90 秒
* 是给后台批量任务(外观合批、品牌补测调度)的预算,同步等响应的桌面客户端不该替它扛
* ——上游卡死时按批各领一个 90 秒,一个 chunk 十几批能叠到十几分钟。
*/
public BrandCheckBatchResult checkAll(List<String> brands, String strategy, long budgetMillis) {
List<String> distinctBrands = distinctNonBlank(brands);
List<Object> failedData = new ArrayList<>();
List<Object> queryFailedData = new ArrayList<>();
for (BrandCheckOutcome outcome : checkWords(distinctBrands, strategy).values()) {
for (BrandCheckOutcome outcome : checkWords(distinctBrands, strategy, deadlineNanos(budgetMillis)).values()) {
failedData.addAll(outcome.failedData());
queryFailedData.addAll(outcome.queryFailedData());
}
@@ -126,7 +138,8 @@ public class BrandCheckClient {
wordsByTitle.add(words);
distinctWords.addAll(words);
}
Map<String, BrandCheckOutcome> outcomes = checkWords(new ArrayList<>(distinctWords), strategy);
Map<String, BrandCheckOutcome> outcomes = checkWords(
new ArrayList<>(distinctWords), strategy, deadlineNanos(properties.getTotalTimeoutMillis()));
List<BrandCheckBatchResult> results = new ArrayList<>(titles.size());
for (List<String> words : wordsByTitle) {
List<Object> failedData = new ArrayList<>();
@@ -144,14 +157,12 @@ public class BrandCheckClient {
}
/**
* 并发查一批**已去重**的词,整批共用一个耗时预算。
* 并发查一批**已去重**的词,整批共用调用方给定的截止时刻。
* 键顺序即入参顺序,checkAll 的并集顺序因此与改动前一致。
*/
private Map<String, BrandCheckOutcome> checkWords(List<String> brands, String strategy) {
private Map<String, BrandCheckOutcome> checkWords(List<String> brands, String strategy, long deadlineNanos) {
// 整批共用一个耗时预算:上游 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) {
@@ -164,6 +175,11 @@ public class BrandCheckClient {
return outcomes;
}
/** 预算毫秒数换算成截止时刻;{@code <= 0} 表示不限制(首轮与全部重试都不掐)。 */
private static long deadlineNanos(long budgetMillis) {
return budgetMillis > 0L ? System.nanoTime() + budgetMillis * 1_000_000L : Long.MAX_VALUE;
}
private BrandCheckOutcome checkOneBrand(String brand, String strategy, long deadlineNanos) {
int attempts = Math.max(1, properties.getRetryTimes());
BrandCheckResponse response = null;
@@ -26,6 +26,11 @@ import java.util.regex.Pattern;
* 品牌判定结果在过滤器实例内短期缓存(同一任务多个 chunk 提交复用同一
* 实例):已判定的品牌再次出现时不再发起远程调用,避免同品牌重复请求。
* 远程抛错不写缓存(可恢复后重查);缓存有界,超限淘汰最旧条目。
*
* 一次调用(= 一个 chunk)共用一个总耗时预算:预算用尽后不再为剩余批次发起远程
* 检查,未判定的品牌整批归 queryFailed 转入补测队列。预算按 chunk 而不是按批,
* 是因为「每批各领一份 aiimage.brand-check.total-timeout-millis」会随批数线性叠加,
* 而这批请求正卡在客户端同步等待的分片回传里。设为 0 退回旧口径(每批各领一份)。
*/
@Slf4j
@Component
@@ -37,9 +42,13 @@ public class CollectDataBrandBatchFilter {
private static final String VERDICT_QUERY_FAILED = "QUERY_FAILED";
private static final String VERDICT_OK = "OK";
/** 未显式配置时一次 chunk 的品牌检查预算(毫秒)。 */
static final long DEFAULT_CHUNK_BUDGET_MILLIS = 10_000L;
private final BrandCheckClient brandCheckClient;
private final int batchSize;
private final int cacheCapacity;
private final long chunkBudgetMillis;
/** 品牌(小写标准化)→ 判定结果;access-order LRU,超限淘汰最旧。 */
private final Map<String, String> verdictCache;
@@ -49,13 +58,19 @@ public class CollectDataBrandBatchFilter {
this(brandCheckClient, batchSize, 512);
}
public CollectDataBrandBatchFilter(BrandCheckClient brandCheckClient, int batchSize, int cacheCapacity) {
this(brandCheckClient, batchSize, cacheCapacity, DEFAULT_CHUNK_BUDGET_MILLIS);
}
@Autowired
public CollectDataBrandBatchFilter(BrandCheckClient brandCheckClient,
@Value("${aiimage.collect-data.brand-check-batch-size:10}") int batchSize,
@Value("${aiimage.collect-data.brand-check-cache-capacity:512}") int cacheCapacity) {
@Value("${aiimage.collect-data.brand-check-cache-capacity:512}") int cacheCapacity,
@Value("${aiimage.collect-data.brand-check-chunk-budget-millis:10000}") long chunkBudgetMillis) {
this.brandCheckClient = brandCheckClient;
this.batchSize = Math.max(1, batchSize);
this.cacheCapacity = Math.max(1, cacheCapacity);
this.chunkBudgetMillis = Math.max(0L, chunkBudgetMillis);
this.verdictCache = new LinkedHashMap<>(Math.max(16, this.cacheCapacity / 2), 0.75f, true);
}
@@ -69,6 +84,9 @@ public class CollectDataBrandBatchFilter {
if (rows == null || rows.isEmpty()) {
return new BrandBatchOutcome(rejected, queryFailed, accepted);
}
long deadlineNanos = chunkBudgetMillis > 0L
? System.nanoTime() + chunkBudgetMillis * 1_000_000L : Long.MAX_VALUE;
int budgetCutRows = 0;
for (int start = 0; start < rows.size(); start += batchSize) {
int end = Math.min(start + batchSize, rows.size());
List<CollectDataResultRowVo> batch = rows.subList(start, end);
@@ -96,9 +114,17 @@ public class CollectDataBrandBatchFilter {
}
continue;
}
if (deadlineNanos != Long.MAX_VALUE && System.nanoTime() >= deadlineNanos) {
// 预算用尽:不再为后面的批次捶上游。已缓存的品牌仍按缓存结论归类(不花代价),
// 没结论的行归 queryFailed 入补测队列——既不按 accepted 放行(可能正是侵权品牌),
// 也不按 rejected 拉黑(没查过就不该写进无效品牌表,否则永久误杀)。
classifyUnchecked(batch, cachedVerdicts, rejected, queryFailed, accepted);
budgetCutRows += batch.size();
continue;
}
Map<String, String> batchVerdicts = new LinkedHashMap<>(cachedVerdicts);
try {
batchVerdicts.putAll(checkAndCache(uncachedBrands));
batchVerdicts.putAll(checkAndCache(uncachedBrands, remainingBudgetMillis(deadlineNanos)));
} catch (RuntimeException ex) {
log.warn("[collect-data] brand check batch failed, degrade batch to queryFailed err={}", ex.getMessage());
for (CollectDataResultRowVo row : batch) {
@@ -110,12 +136,24 @@ public class CollectDataBrandBatchFilter {
}
classify(batch, batchVerdicts, rejected, queryFailed, accepted);
}
if (budgetCutRows > 0) {
log.info("[collect-data] 品牌检查预算用尽,剩余批次转入补测队列 预算={}ms 延后行={} 总行={}",
chunkBudgetMillis, budgetCutRows, rows.size());
}
return new BrandBatchOutcome(rejected, queryFailed, accepted);
}
/** 本批次还能用的毫秒数;0 表示不限制(交给 checkAll 按配置预算走)。 */
private static long remainingBudgetMillis(long deadlineNanos) {
if (deadlineNanos == Long.MAX_VALUE) {
return 0L;
}
return Math.max(1L, (deadlineNanos - System.nanoTime()) / 1_000_000L);
}
/** 远程检查未缓存品牌并写入缓存;返回新查品牌(小写标准化)→ 判定映射。 */
private Map<String, String> checkAndCache(List<String> uncachedBrands) {
BrandCheckClient.BrandCheckBatchResult check = brandCheckClient.checkAll(uncachedBrands, "Terms");
private Map<String, String> checkAndCache(List<String> uncachedBrands, long budgetMillis) {
BrandCheckClient.BrandCheckBatchResult check = brandCheckClient.checkAll(uncachedBrands, "Terms", budgetMillis);
Set<String> failedBrands = normalizeObjectSet(check == null ? null : check.faildData());
Set<String> queryFailedBrands = normalizeObjectSet(check == null ? null : check.queryFaildData());
Map<String, String> verdicts = new LinkedHashMap<>();
@@ -170,6 +208,27 @@ public class CollectDataBrandBatchFilter {
}
}
/**
* 预算用尽后归一仍未检查的批次:有缓存结论的按结论分类,没结论的品牌按
* QUERY_FAILED 处理(空品牌行仍按 rejected,与已检查批次同口径)。
*/
private void classifyUnchecked(List<CollectDataResultRowVo> batch, Map<String, String> cachedVerdicts,
List<CollectDataResultRowVo> rejected,
List<CollectDataResultRowVo> queryFailed,
List<CollectDataResultRowVo> accepted) {
Map<String, String> verdicts = new LinkedHashMap<>(cachedVerdicts);
for (CollectDataResultRowVo row : batch) {
if (row == null) {
continue;
}
String brand = normalizeBrand(row.getBrand());
if (!brand.isBlank()) {
verdicts.putIfAbsent(brand, VERDICT_QUERY_FAILED);
}
}
classify(batch, verdicts, rejected, queryFailed, accepted);
}
private void classify(List<CollectDataResultRowVo> batch, Map<String, String> verdicts,
List<CollectDataResultRowVo> rejected,
List<CollectDataResultRowVo> queryFailed,