fix(backend-java): 收口并发资源与恢复链路

This commit is contained in:
2026-09-05 14:33:07 +08:00
parent 5a00201931
commit 0bf39b04db
35 changed files with 917 additions and 154 deletions
@@ -12,9 +12,11 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.util.StreamUtils;
import org.springframework.web.client.RestClient;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.LinkedHashMap;
@@ -38,6 +40,7 @@ import java.util.regex.Pattern;
public class AppearancePatentLlmClient {
private static final MediaType APPLICATION_JSON_UTF8 = new MediaType("application", "json", StandardCharsets.UTF_8);
private static final int MAX_LLM_RESPONSE_BYTES = 16 * 1024 * 1024;
private static final String INFRINGEMENT = "侵权";
private static final String NO_INFRINGEMENT = "无侵权";
private static final String BRAND_QUERY_FAILED = "商标查询失败";
@@ -298,8 +301,7 @@ public class AppearancePatentLlmClient {
});
request.body(body);
String responseText = request.exchange((clientRequest, clientResponse) -> {
byte[] responseBytes = StreamUtils.copyToByteArray(clientResponse.getBody());
String responseBody = responseBytes.length == 0 ? "" : new String(responseBytes, StandardCharsets.UTF_8);
String responseBody = readResponseBodyBounded(clientResponse.getBody());
if (!clientResponse.getStatusCode().is2xxSuccessful()) {
throw new IllegalStateException("LLM http " + clientResponse.getStatusCode().value()
+ ": " + abbreviate(responseBody, 500));
@@ -467,6 +469,28 @@ public class AppearancePatentLlmClient {
}
}
private String readResponseBodyBounded(InputStream inputStream) throws IOException {
if (inputStream == null) {
return "";
}
try (InputStream input = inputStream; ByteArrayOutputStream output = new ByteArrayOutputStream(8192)) {
byte[] buffer = new byte[8192];
int read;
int total = 0;
while ((read = input.read(buffer)) != -1) {
if (read == 0) {
continue;
}
if ((long) total + read > MAX_LLM_RESPONSE_BYTES) {
throw new IOException("LLM response exceeds " + MAX_LLM_RESPONSE_BYTES + " bytes");
}
output.write(buffer, 0, read);
total += read;
}
return output.toString(StandardCharsets.UTF_8);
}
}
private JsonNode parseJsonOrThrow(String value) {
try {
return objectMapper.readTree(value);
@@ -165,6 +165,10 @@ public class AppearancePatentTaskService {
}
ParsedWorkbook parsed = parseWorkbook(input, source);
int maxParseRows = Math.max(1, properties.getMaxParseRows());
if ((long) allRows.size() + parsed.allRows().size() > maxParseRows) {
throw new BusinessException("解析总行数超过上限: " + maxParseRows);
}
totalRows += parsed.totalRows();
droppedRows += parsed.droppedRows();
allRows.addAll(parsed.allRows());
@@ -2295,6 +2299,7 @@ public class AppearancePatentTaskService {
int total = 0;
int dropped = 0;
int validRows = 0;
int maxParseRows = Math.max(1, properties.getMaxParseRows());
String currentBlockBaseId = "";
String currentGroupKey = "";
for (int i = 1; i <= sheet.getLastRowNum(); i++) {
@@ -2314,6 +2319,9 @@ public class AppearancePatentTaskService {
continue;
}
validRows++;
if (validRows > maxParseRows) {
throw new BusinessException("解析行数超过上限: " + maxParseRows);
}
AppearancePatentParsedRowVo vo = new AppearancePatentParsedRowVo();
vo.setSourceFileKey(source.getFileKey());
vo.setSourceFilename(firstNonBlank(source.getOriginalFilename(), input.getName()));
@@ -27,13 +27,19 @@ import java.util.Objects;
@Slf4j
public class AppearancePatentExcelParser {
private static final int DEFAULT_MAX_ROWS = 50_000;
public ParsedSheet parse(File input) {
return parse(input, DEFAULT_MAX_ROWS);
}
public ParsedSheet parse(File input, int maxRows) {
if (input == null) {
throw new IllegalArgumentException("input must not be null");
}
try (FileInputStream fis = new FileInputStream(input);
Workbook workbook = WorkbookFactory.create(fis)) {
return parseWorkbook(workbook);
return parseWorkbook(workbook, maxRows);
} catch (BusinessException ex) {
throw ex;
} catch (Exception ex) {
@@ -47,7 +53,7 @@ public class AppearancePatentExcelParser {
throw new IllegalArgumentException("input must not be null");
}
try (Workbook workbook = WorkbookFactory.create(input)) {
return parseWorkbook(workbook);
return parseWorkbook(workbook, DEFAULT_MAX_ROWS);
} catch (BusinessException ex) {
throw ex;
} catch (Exception ex) {
@@ -56,7 +62,8 @@ public class AppearancePatentExcelParser {
}
}
private ParsedSheet parseWorkbook(Workbook workbook) {
private ParsedSheet parseWorkbook(Workbook workbook, int maxRows) {
int safeMaxRows = Math.max(1, maxRows);
DataFormatter formatter = new DataFormatter();
Sheet sheet = workbook.getSheetAt(0);
Row header = sheet.getRow(0);
@@ -89,6 +96,9 @@ public class AppearancePatentExcelParser {
if (id.isBlank() && asin.isBlank() && country.isBlank()) {
continue;
}
if (rows.size() >= safeMaxRows) {
throw new BusinessException("解析行数超过上限: " + safeMaxRows);
}
rows.add(new AppearanceExcelRow(
i + 1,
id,