fix(品牌检测): 一次分片回传领一份 chunk 预算,用尽即转补测队列
上游 16890 卡死时每批各领一个 90 秒总超时,一个 chunk 十几批能叠到十几分钟, 桌面端是在同步等响应的路径上替它扛。现按 chunk 给 10 秒预算,预算内逐批只把 剩余额度交给远程;用尽后未查的品牌一律按查询失败入补测队列,既不按 accepted 放行也不写进无效品牌表。外观合批、品牌补测调度等后台路径仍按 90 秒走。
This commit is contained in:
+63
-4
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user