feat: modularize gateway requests and recover settlements
This commit is contained in:
1 parent
7785cda6a6
commit
5533a483d9
40 files changed
+1598
-232
No files matched your search
+10
-52
@@ -5,6 +5,7 @@ import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import com.moyi.controlplane.security.SecretCipher;
|
||||
import com.moyi.controlplane.provider.credentials.UpstreamCredentialResolver;
|
||||
import java.net.URI;
|
||||
import java.net.InetAddress;
|
||||
import java.net.http.HttpClient;
|
||||
@@ -450,17 +451,15 @@ public class AsyncProviderClient {
|
||||
String ciphertext = row.get("credential_ciphertext") == null ? "" : String.valueOf(row.get("credential_ciphertext"));
|
||||
String key = ciphertext.isBlank() ? "" : cipher.decrypt(ciphertext);
|
||||
String code = String.valueOf(row.get("code"));
|
||||
List<String> keys = new ArrayList<>();
|
||||
if (!key.isBlank()) keys.add(key);
|
||||
addEnvironmentKeys(keys, code);
|
||||
addEnvironmentKeys(keys, id);
|
||||
var resolver = new UpstreamCredentialResolver(environment);
|
||||
List<String> keys = new ArrayList<>(resolver.providerKeys(key, code, id));
|
||||
// Tripo's official API key may be supplied through the server secret
|
||||
// environment so it never has to be committed or exposed in SQL.
|
||||
if (key.isBlank() && isTripo(code, String.valueOf(row.get("base_url")))) {
|
||||
addEnvironmentKeys(keys, "TRIPO_API_KEY");
|
||||
addEnvironmentKeys(keys, "MOYI_TRIPO_API_KEY");
|
||||
keys.addAll(resolver.environmentKeys("TRIPO_API_KEY"));
|
||||
keys.addAll(resolver.environmentKeys("MOYI_TRIPO_API_KEY"));
|
||||
}
|
||||
if (keys.isEmpty() && routeKeys(code, id, routeId, upstreamModel).isEmpty()) throw new ApiException("PROVIDER_NOT_CONFIGURED", "异步渠道尚未配置 API Key", 503);
|
||||
if (keys.isEmpty() && resolver.scopedKeys(code, routeId, upstreamModel).isEmpty()) throw new ApiException("PROVIDER_NOT_CONFIGURED", "异步渠道尚未配置 API Key", 503);
|
||||
return new Provider(code, String.valueOf(row.get("protocol")), String.valueOf(row.get("base_url")), keys.stream().distinct().toList(), keyCursors.computeIfAbsent(id, ignored -> new AtomicInteger()));
|
||||
} catch (EmptyResultDataAccessException exception) { throw new ApiException("PROVIDER_NOT_CONFIGURED", "异步渠道不存在或已停用", 503); }
|
||||
}
|
||||
@@ -477,32 +476,6 @@ public class AsyncProviderClient {
|
||||
private String trim(String value) { return value == null ? "" : value.replaceAll("/+$", ""); }
|
||||
private String encode(String value) { return java.net.URLEncoder.encode(value, java.nio.charset.StandardCharsets.UTF_8); }
|
||||
private String write(Object value) throws JsonProcessingException { return mapper.writeValueAsString(value == null ? Map.of() : value); }
|
||||
private void addEnvironmentKeys(List<String> keys, String alias) {
|
||||
String name = alias.endsWith("_API_KEY") ? alias : "MOYI_UPSTREAM_API_KEYS_" + alias.toUpperCase(java.util.Locale.ROOT).replace('-', '_');
|
||||
String configured = environment.getProperty(name);
|
||||
if (configured != null && !configured.isBlank()) for (String value : configured.split("\\|")) if (!value.isBlank()) keys.add(value.trim());
|
||||
}
|
||||
|
||||
private List<String> routeKeys(String providerCode, String providerId, String routeId, String upstreamModel) {
|
||||
if (routeId != null && !routeId.isBlank()) {
|
||||
String configured = environment.getProperty("MOYI_UPSTREAM_API_KEYS_ROUTE_" + normalize(routeId));
|
||||
if (configured != null && !configured.isBlank()) return parseKeys(configured);
|
||||
}
|
||||
if (upstreamModel != null && !upstreamModel.isBlank()) {
|
||||
String scopedName = "MOYI_UPSTREAM_API_KEYS_MODEL_" + normalize(providerCode) + "_" + normalize(upstreamModel);
|
||||
String configured = environment.getProperty(scopedName);
|
||||
if (configured != null && !configured.isBlank()) return parseKeys(configured);
|
||||
}
|
||||
return List.of();
|
||||
}
|
||||
|
||||
private List<String> parseKeys(String configured) {
|
||||
List<String> keys = new ArrayList<>();
|
||||
for (String key : configured.split("\\|")) if (!key.isBlank()) keys.add(key.trim());
|
||||
return keys.stream().distinct().toList();
|
||||
}
|
||||
|
||||
private String normalize(String value) { return String.valueOf(value == null ? "" : value).toUpperCase(java.util.Locale.ROOT).replaceAll("[^A-Z0-9]", "_"); }
|
||||
private Map<String,Object> withKeyFingerprint(Map<String,Object> response, String apiKey) {
|
||||
Map<String,Object> copy = new LinkedHashMap<>();
|
||||
if (response != null) copy.putAll(response);
|
||||
@@ -528,15 +501,15 @@ public class AsyncProviderClient {
|
||||
return normalizedCode.contains("tripo") || normalizedUrl.contains("tripo3d");
|
||||
}
|
||||
private record Provider(String code, String protocol, String baseUrl, List<String> apiKeys, AtomicInteger cursor) {
|
||||
@Override public String toString() { return "Provider[code=" + code + ", credentialCount=" + apiKeys.size() + "]"; }
|
||||
private String nextApiKey() {
|
||||
if (apiKeys.isEmpty()) throw new ApiException("UPSTREAM_KEY_NOT_AVAILABLE", "异步渠道没有可用的上游凭据", 503);
|
||||
return apiKeys.get(Math.floorMod(cursor.getAndIncrement(), apiKeys.size()));
|
||||
return UpstreamCredentialResolver.nextKey(apiKeys, cursor);
|
||||
}
|
||||
private String nextApiKey(String routeId, String upstreamModel, Environment environment, ConcurrentMap<String, AtomicInteger> cursors) {
|
||||
List<String> routeKeys = routeKeys(routeId, upstreamModel, environment);
|
||||
if (routeKeys.isEmpty()) return nextApiKey();
|
||||
AtomicInteger routeCursor = cursors.computeIfAbsent("route:" + (routeId == null ? upstreamModel : routeId), ignored -> new AtomicInteger());
|
||||
return routeKeys.get(Math.floorMod(routeCursor.getAndIncrement(), routeKeys.size()));
|
||||
return UpstreamCredentialResolver.nextKey(routeKeys, routeCursor);
|
||||
}
|
||||
private String keyForFingerprint(String routeId, String upstreamModel, Environment environment,
|
||||
ConcurrentMap<String, AtomicInteger> cursors, String fingerprint) {
|
||||
@@ -548,22 +521,7 @@ public class AsyncProviderClient {
|
||||
throw new ApiException("UPSTREAM_KEY_NOT_AVAILABLE", "创建任务时使用的上游凭据已从服务器配置中移除,无法安全轮询", 503);
|
||||
}
|
||||
private List<String> routeKeys(String routeId, String upstreamModel, Environment environment) {
|
||||
if (environment == null) return List.of();
|
||||
if (routeId != null && !routeId.isBlank()) {
|
||||
String value = environment.getProperty("MOYI_UPSTREAM_API_KEYS_ROUTE_" + normalize(routeId));
|
||||
if (value != null && !value.isBlank()) return parseKeys(value);
|
||||
}
|
||||
if (upstreamModel != null && !upstreamModel.isBlank()) {
|
||||
String value = environment.getProperty("MOYI_UPSTREAM_API_KEYS_MODEL_" + normalize(code()) + "_" + normalize(upstreamModel));
|
||||
if (value != null && !value.isBlank()) return parseKeys(value);
|
||||
}
|
||||
return List.of();
|
||||
return new UpstreamCredentialResolver(environment).scopedKeys(code, routeId, upstreamModel);
|
||||
}
|
||||
private static List<String> parseKeys(String value) {
|
||||
List<String> keys = new ArrayList<>();
|
||||
for (String key : value.split("\\|")) if (!key.isBlank()) keys.add(key.trim());
|
||||
return keys.stream().distinct().toList();
|
||||
}
|
||||
private static String normalize(String value) { return String.valueOf(value == null ? "" : value).toUpperCase(java.util.Locale.ROOT).replaceAll("[^A-Z0-9]", "_"); }
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,11 @@ import com.moyi.controlplane.catalog.CatalogService;
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import com.moyi.controlplane.gateway.GatewayGuard;
|
||||
import com.moyi.controlplane.gateway.GatewayModelIds;
|
||||
import com.moyi.controlplane.gateway.request.ApiRequestContext;
|
||||
import com.moyi.controlplane.gateway.request.ApiRequestMetadata;
|
||||
import com.moyi.controlplane.gateway.request.ApiRequestProtocol;
|
||||
import com.moyi.controlplane.gateway.request.ApiRequestRiskEngine;
|
||||
import com.moyi.controlplane.gateway.request.ApiRequestRiskPolicy;
|
||||
import com.moyi.controlplane.usage.UsageAuditService;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
@@ -43,11 +48,13 @@ public class AsyncTaskService {
|
||||
private final AsyncProviderClient providerClient;
|
||||
private final TransactionTemplate transactions;
|
||||
private final com.moyi.controlplane.gateway.EnterprisePolicies policies;
|
||||
private final ApiRequestRiskPolicy requestRisk;
|
||||
|
||||
/** Compatibility constructor for isolated gateway tests that only exercise mock tasks. */
|
||||
public AsyncTaskService(JdbcTemplate jdbc, ObjectMapper objectMapper, CatalogService catalogService,
|
||||
int maxQueuedPerModel, GatewayGuard gatewayGuard, UsageAuditService usage) {
|
||||
this(jdbc, objectMapper, catalogService, maxQueuedPerModel, 4, 4, 3_600_000, true, gatewayGuard, usage, null, null);
|
||||
this(jdbc, objectMapper, catalogService, maxQueuedPerModel, 4, 4, 3_600_000, true,
|
||||
gatewayGuard, usage, null, null, new ApiRequestRiskPolicy(ApiRequestRiskEngine.allowAll()));
|
||||
}
|
||||
|
||||
/** Compatibility constructor for isolated tests using explicit limits. */
|
||||
@@ -58,7 +65,8 @@ public class AsyncTaskService {
|
||||
AsyncProviderClient providerClient, PlatformTransactionManager transactionManager
|
||||
) {
|
||||
this(jdbc, objectMapper, catalogService, maxQueuedPerModel, defaultModelConcurrency,
|
||||
defaultKeyConcurrency, providerTimeoutMs, true, gatewayGuard, usage, providerClient, transactionManager);
|
||||
defaultKeyConcurrency, providerTimeoutMs, true, gatewayGuard, usage, providerClient,
|
||||
transactionManager, new ApiRequestRiskPolicy(ApiRequestRiskEngine.allowAll()));
|
||||
}
|
||||
|
||||
@org.springframework.beans.factory.annotation.Autowired
|
||||
@@ -74,7 +82,8 @@ public class AsyncTaskService {
|
||||
GatewayGuard gatewayGuard,
|
||||
UsageAuditService usage,
|
||||
AsyncProviderClient providerClient,
|
||||
PlatformTransactionManager transactionManager
|
||||
PlatformTransactionManager transactionManager,
|
||||
ApiRequestRiskPolicy requestRisk
|
||||
) {
|
||||
// A model concurrency of 0 means no MOYI-side model-group cap. The
|
||||
// upstream provider still enforces its own quota, while API-Key
|
||||
@@ -94,16 +103,28 @@ public class AsyncTaskService {
|
||||
this.providerClient = providerClient;
|
||||
this.transactions = transactionManager == null ? null : new TransactionTemplate(transactionManager);
|
||||
this.policies = new com.moyi.controlplane.gateway.EnterprisePolicies(jdbc,objectMapper);
|
||||
this.requestRisk = java.util.Objects.requireNonNull(requestRisk, "requestRisk");
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Map<String, Object> create(String authorization, String taskType, Map<String, Object> input) {
|
||||
return createForKey(requireKey(authorization), taskType, input);
|
||||
return create(authorization, taskType, input, ApiRequestMetadata.internal());
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Map<String, Object> create(String authorization, String taskType, Map<String, Object> input, ApiRequestMetadata metadata) {
|
||||
return createForKey(requireKey(authorization), taskType, input, metadata);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Map<String, Object> createForKey(Map<String, Object> key, String taskType, Map<String, Object> input) {
|
||||
gatewayGuard.checkRate(UUID.fromString(String.valueOf(key.get("id"))), false);
|
||||
return createForKey(key, taskType, input, ApiRequestMetadata.internal());
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Map<String, Object> createForKey(Map<String, Object> key, String taskType, Map<String, Object> input, ApiRequestMetadata metadata) {
|
||||
UUID apiKeyId = UUID.fromString(String.valueOf(key.get("id")));
|
||||
gatewayGuard.checkRate(apiKeyId, false);
|
||||
String requestedModelId = String.valueOf(input.getOrDefault("model", ""));
|
||||
Map<String, Object> model = GatewayModelIds.resolve(catalogService, key, requestedModelId);
|
||||
if (model == null) throw new ApiException("MODEL_NOT_FOUND", "模型不存在或已下线", 404);
|
||||
@@ -111,19 +132,20 @@ public class AsyncTaskService {
|
||||
if (!taskType.equals(model.get("type")) || !"async".equals(model.get("mode"))) throw new ApiException("NOT_ASYNC_MEDIA_MODEL", "该模型不是对应的异步媒体模型", 400);
|
||||
if (Boolean.FALSE.equals(model.get("executionReady"))) throw new ApiException("ASYNC_PROVIDER_NOT_IMPLEMENTED", "该模型的专用执行接口尚未接入", 503);
|
||||
if (!"provider-mock".equals(model.get("providerId")) && providerClient == null) throw new ApiException("ASYNC_PROVIDER_NOT_IMPLEMENTED", "异步执行器未配置", 501);
|
||||
// Serialize the queue-capacity check with task insertion for this
|
||||
// model. Without the transaction advisory lock concurrent creates
|
||||
// could all observe the same count and exceed maxQueuedPerModel.
|
||||
jdbc.queryForList("SELECT pg_advisory_xact_lock(hashtextextended('moyi:async-queue:' || ?, 0))", modelId);
|
||||
Integer queued = jdbc.queryForObject("SELECT COUNT(*) FROM model_tasks WHERE model_id = ? AND status IN ('queued', 'running')", Integer.class, modelId);
|
||||
if (queued != null && queued >= maxQueuedPerModel) throw new ApiException("TASK_QUEUE_FULL", "该模型当前排队已满,请稍后重试", 429);
|
||||
String prompt = String.valueOf(input.getOrDefault("prompt", "")).trim();
|
||||
Object mediaInput = input.get("media_url");
|
||||
if (prompt.isBlank() && (mediaInput == null || String.valueOf(mediaInput).isBlank())) {
|
||||
throw new ApiException("PROMPT_REQUIRED", "生成提示词或媒体输入不能为空");
|
||||
}
|
||||
UUID apiKeyId = UUID.fromString(String.valueOf(key.get("id")));
|
||||
UUID userId = catalogService.keyOwnerId(apiKeyId);
|
||||
requestRisk.enforce(new ApiRequestContext(userId, apiKeyId, ApiRequestProtocol.ASYNC_TASK,
|
||||
requestedModelId, modelId, false, estimateInputUnits(input), 0, metadata, System.nanoTime()));
|
||||
// Serialize the queue-capacity check with task insertion for this
|
||||
// model. Without the transaction advisory lock, concurrent creates
|
||||
// could all observe the same count and exceed maxQueuedPerModel.
|
||||
jdbc.queryForList("SELECT pg_advisory_xact_lock(hashtextextended('moyi:async-queue:' || ?, 0))", modelId);
|
||||
Integer queued = jdbc.queryForObject("SELECT COUNT(*) FROM model_tasks WHERE model_id = ? AND status IN ('queued', 'running')", Integer.class, modelId);
|
||||
if (queued != null && queued >= maxQueuedPerModel) throw new ApiException("TASK_QUEUE_FULL", "该模型当前排队已满,请稍后重试", 429);
|
||||
@SuppressWarnings("unchecked") Map<String,Object> frozenPricing=(Map<String,Object>) model.get("pricing");
|
||||
if(AsyncPriceSchedule.tokens(frozenPricing) && !"image".equals(taskType)) throw new ApiException("ASYNC_BILLING_UNSUPPORTED","该媒体类型的 Token 用量结算尚未接入",503);
|
||||
BigDecimal reservedAmount = "provider-mock".equals(model.get("providerId")) ? BigDecimal.ZERO : AsyncPriceSchedule.reserve(frozenPricing,input);
|
||||
@@ -146,7 +168,7 @@ public class AsyncTaskService {
|
||||
releaseWallet(userId, reservedAmount);
|
||||
throw exception;
|
||||
}
|
||||
usage.recordTask(userId, apiKeyId, modelId, String.valueOf(model.get("providerId")), id, "queued", input, null, null, null);
|
||||
usage.recordTask(userId, apiKeyId, modelId, String.valueOf(model.get("providerId")), id, "queued", input, null, null, null, metadata);
|
||||
if (!"provider-mock".equals(model.get("providerId"))) jdbc.update("UPDATE model_tasks SET pricing_snapshot=CAST(? AS jsonb),price_version=(SELECT max(id) FROM model_price_history WHERE model_id=?) WHERE id=?",writeJson(frozenPricing),modelId,id);
|
||||
catalogService.touchKey(key);
|
||||
return taskView(id);
|
||||
@@ -513,6 +535,11 @@ public class AsyncTaskService {
|
||||
|
||||
private String writeJson(Object value) { try { return objectMapper.writeValueAsString(value); } catch (JsonProcessingException exception) { return "{}"; } }
|
||||
|
||||
private long estimateInputUnits(Map<String,Object> input) {
|
||||
try { return objectMapper.writeValueAsBytes(input == null ? Map.of() : input).length; }
|
||||
catch (JsonProcessingException exception) { throw new ApiException("INVALID_REQUEST", "任务请求无法编码"); }
|
||||
}
|
||||
|
||||
private Map<String,Object> usageOnly(Map<String,Object> result) {
|
||||
var selected=new LinkedHashMap<String,Object>();
|
||||
if(result!=null) for(String field:List.of("usage","usageMetadata")) if(result.get(field) instanceof Map<?,?> value) selected.put(field,value);
|
||||
|
||||
@@ -11,6 +11,8 @@ import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestHeader;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import com.moyi.controlplane.gateway.request.ApiRequestMetadata;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/v1")
|
||||
@@ -25,19 +27,23 @@ public class TaskController {
|
||||
public ResponseEntity<Map<String, Object>> create3d(
|
||||
@org.springframework.web.bind.annotation.PathVariable(required = false) String type,
|
||||
@RequestHeader(value = "Authorization", required = false) String authorization,
|
||||
@RequestBody Map<String, Object> input
|
||||
@RequestBody Map<String, Object> input,
|
||||
HttpServletRequest request
|
||||
) {
|
||||
String taskType = type == null ? String.valueOf(input == null ? "3d" : input.getOrDefault("taskType", "3d")) : type;
|
||||
return ResponseEntity.status(HttpStatus.ACCEPTED).body(Map.of("task", tasks.create(authorization, taskType, input)));
|
||||
return ResponseEntity.status(HttpStatus.ACCEPTED).body(Map.of("task", tasks.create(authorization, taskType, input,
|
||||
ApiRequestMetadata.fromHttp(request.getRemoteAddr()))));
|
||||
}
|
||||
|
||||
|
||||
@PostMapping("/video/tasks")
|
||||
public ResponseEntity<Map<String, Object>> createVideo(
|
||||
@RequestHeader(value = "Authorization", required = false) String authorization,
|
||||
@RequestBody Map<String, Object> input
|
||||
@RequestBody Map<String, Object> input,
|
||||
HttpServletRequest request
|
||||
) {
|
||||
return ResponseEntity.status(HttpStatus.ACCEPTED).body(Map.of("task", tasks.create(authorization, "video", input)));
|
||||
return ResponseEntity.status(HttpStatus.ACCEPTED).body(Map.of("task", tasks.create(authorization, "video", input,
|
||||
ApiRequestMetadata.fromHttp(request.getRemoteAddr()))));
|
||||
}
|
||||
|
||||
@GetMapping("/tasks/{id}")
|
||||
|
||||
Reference in new issue
Block a user