fix: classify upstream failures and prevent empty billed responses

This commit is contained in:
wlx committed 2026-09-28 04:55:00 +08:00
1 parent b455d8d79f
commit 9acc69d4c7
28 files changed
+1449 -131

No files matched your search

@@ -20,7 +20,10 @@ final class ChatStreamCollector {
if(!(raw instanceof Map<?,?> choice)) continue;
if(choice.get("finish_reason")!=null) finish=choice.get("finish_reason");
if(choice.get("delta") instanceof Map<?,?> delta) {
append(content,delta.get("content")); append(reasoning,delta.get("reasoning_content"));
appendContent(delta.get("content"));
appendReasoning(delta.get("reasoning_content"));
appendReasoning(delta.get("reasoning"));
appendReasoning(delta.get("thinking"));
if(delta.get("tool_calls") instanceof List<?> calls) for(Object item:calls) {
if(!(item instanceof Map<?,?> call) || !(call.get("index") instanceof Number index)) continue;
int i=index.intValue();
@@ -35,14 +38,55 @@ final class ChatStreamCollector {
}
}
}
if(firstToken==null && ((delta.get("content") instanceof String s && !s.isEmpty()) || (delta.get("reasoning_content") instanceof String r && !r.isEmpty()) || delta.get("tool_calls")!=null)) firstToken=(int)(System.currentTimeMillis()-started);
if(firstToken==null && (hasText(delta.get("content")) || hasText(delta.get("reasoning_content"))
|| hasText(delta.get("reasoning")) || hasText(delta.get("thinking"))
|| (delta.get("tool_calls") instanceof List<?> calls && !calls.isEmpty())))
firstToken=(int)(System.currentTimeMillis()-started);
}
}
}
private void append(StringBuilder target,Object value) { if(value instanceof String s && target.length()<8000) target.append(s,0,Math.min(s.length(),8000-target.length())); }
private void appendContent(Object value) {
if(value instanceof String) { append(content,value); return; }
if(value instanceof List<?> parts) for(Object part:parts) {
if(part instanceof Map<?,?> map) {
Object type=map.get("type");
if(isReasoningType(type)) {
appendReasoning(firstString(map,"text","thinking","content","reasoning"));
} else {
append(content,firstString(map,"text","content"));
}
} else append(content,part);
}
}
private void appendReasoning(Object value) { append(reasoning,value); }
private void append(StringBuilder target,Object value) {
if(value instanceof String s && target.length()<8000) target.append(s,0,Math.min(s.length(),8000-target.length()));
}
private boolean hasText(Object value) {
if(value instanceof String text) return !text.isBlank();
if(value instanceof List<?> parts) for(Object part:parts) {
if(part instanceof Map<?,?> map) {
if(isReasoningType(map.get("type"))) {
if(hasText(firstString(map,"text","thinking","content","reasoning"))) return true;
} else if(hasText(firstString(map,"text","content"))) return true;
} else if(hasText(part)) return true;
}
return false;
}
private boolean isReasoningType(Object type) {
if(type==null) return false;
String normalized=String.valueOf(type).toLowerCase(Locale.ROOT);
return normalized.equals("thinking") || normalized.equals("reasoning") || normalized.equals("reasoning_content");
}
private String firstString(Map<?,?> map,String... names) {
for(String name:names) if(map.get(name) instanceof String text) return text;
return "";
}
Integer firstTokenMs() { return firstToken; }
Map<String,Object> result() {
if(finish==null) throw new ApiException("UPSTREAM_STREAM_INCOMPLETE","上游未完成输出,本次未扣费",502);
if(content.toString().isBlank() && reasoning.toString().isBlank() && !hasToolOutput())
throw new ApiException("UPSTREAM_RESPONSE_INVALID","上游未返回完整的聊天结果,本次未扣费",502);
var message=new LinkedHashMap<String,Object>(); message.put("role","assistant"); message.put("content",content.toString());
if(!reasoning.isEmpty()) message.put("reasoning_content",reasoning.toString());
if(!tools.isEmpty()) message.put("tool_calls",List.copyOf(tools.values()));
@@ -50,4 +94,5 @@ final class ChatStreamCollector {
result.put("usage",usage); result.put("choices",List.of(Map.of("index",0,"message",message,"finish_reason",finish)));
return result;
}
private boolean hasToolOutput() { return tools.values().stream().anyMatch(tool -> !tool.isEmpty()); }
}
@@ -162,9 +162,44 @@ public class GatewayController {
private int elapsed(long started) { return (int)Math.min(Integer.MAX_VALUE,System.currentTimeMillis()-started); }
private void requireCompletion(Map<String,Object> body) {
if(body.containsKey("error") || !(body.get("choices") instanceof List<?> choices) || choices.isEmpty()
|| !(choices.getFirst() instanceof Map<?,?> choice) || choice.get("finish_reason")==null)
|| !(choices.getFirst() instanceof Map<?,?> choice) || choice.get("finish_reason")==null || !hasAssistantOutput(choice.get("message")))
throw new ApiException("UPSTREAM_RESPONSE_INVALID","上游未返回完整的聊天结果,本次未扣费",502);
}
/**
* Reasoning models may finish with an empty final content field while
* returning reasoning_content. That is a valid, billable upstream result
* and the UI renders the reasoning separately. A response with neither
* content, reasoning, nor tool calls is a genuine empty response and must
* not be settled as a successful paid call.
*/
private boolean hasAssistantOutput(Object rawMessage) {
if (!(rawMessage instanceof Map<?,?> message)) return false;
if (hasAssistantValue(message.get("content"))) return true;
for (String field : List.of("reasoning_content", "reasoning", "thinking")) {
if (hasText(message.get(field))) return true;
}
if (message.get("tool_calls") instanceof List<?> calls) {
return calls.stream().anyMatch(call -> call instanceof Map<?,?> item && !item.isEmpty());
}
return false;
}
private boolean hasAssistantValue(Object value) {
if (hasText(value)) return true;
if (!(value instanceof List<?> parts)) return false;
return parts.stream().anyMatch(part -> {
if (!(part instanceof Map<?,?> map)) return hasText(part);
for (String field : List.of("text", "content", "thinking", "reasoning", "reasoning_content")) {
if (hasText(map.get(field))) return true;
}
return false;
});
}
private boolean hasText(Object value) {
return value instanceof String text && !text.isBlank();
}
private Map<String,Object> mockResponse(String modelId,Map<String,Object> input) {
String prompt = extractPrompt(input.get("messages"));
String content = "MOYI Java Mock 已收到:" + (prompt.isBlank() ? "(空消息)" : prompt);
@@ -1,6 +1,8 @@
package com.moyi.controlplane.gateway;
import java.util.List;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;
@@ -21,6 +23,15 @@ public class GatewayGuard {
private static final DefaultRedisScript<Long> RATE = script("redis/rate-limit.lua");
private static final DefaultRedisScript<Long> ACQUIRE = script("redis/acquire-slot.lua");
private static final DefaultRedisScript<Long> ACQUIRE_PAIR = script("redis/acquire-pair.lua");
private static final DefaultRedisScript<List> CAPACITY = new DefaultRedisScript<>("""
local time = redis.call('TIME')
local now = tonumber(time[1]) * 1000 + math.floor(tonumber(time[2]) / 1000)
local counts = {}
for i, key in ipairs(KEYS) do
counts[i] = redis.call('ZCOUNT', key, '(' .. now, '+inf')
end
return counts
""", List.class);
private static final DefaultRedisScript<Long> RELEASE = new DefaultRedisScript<>("return redis.call('ZREM', KEYS[1], ARGV[1])", Long.class);
private final StringRedisTemplate redis;
private final int requestsPerMinute;
@@ -118,29 +129,64 @@ public class GatewayGuard {
public long queueWaitMs() { return queueWaitMs; }
public int defaultModelConcurrency() { return defaultModelConcurrency; }
public int defaultKeyConcurrency() { return defaultKeyConcurrency; }
public record Capacity(long running, long queued) {}
public record CapacitySnapshot(boolean available, Map<String, Capacity> groups) {}
/** Reads live leases rather than historical usage rows. Expired workers do not count as running. */
public CapacitySnapshot capacitySnapshot(List<String> modelIds) {
if (modelIds.isEmpty()) return new CapacitySnapshot(true, Map.of());
List<String> buckets = new ArrayList<>();
for (String id : modelIds) {
buckets.add(redisPrefix + "gateway:slots:model:" + id);
buckets.add(redisPrefix + "gateway:waiting:model:" + id);
}
try {
List<?> counts = redis.execute(CAPACITY, buckets);
if (counts == null || counts.size() != buckets.size()) return new CapacitySnapshot(false, Map.of());
Map<String, Capacity> groups = new LinkedHashMap<>();
for (int index = 0; index < modelIds.size(); index++) {
groups.put(modelIds.get(index), new Capacity(((Number) counts.get(index * 2)).longValue(), ((Number) counts.get(index * 2 + 1)).longValue()));
}
return new CapacitySnapshot(true, groups);
} catch (DataAccessException exception) {
log.warn("Gateway capacity snapshot unavailable ({})", exception.getClass().getSimpleName());
return new CapacitySnapshot(false, Map.of());
}
}
/** Atomically reserves the model and API-Key slots, so queued requests do not hold one scarce slot while waiting for the other. */
public Admission acquire(Map<String, Object> model, UUID keyId, long requestedWaitMs) {
String modelBucket = redisPrefix + "gateway:slots:model:" + model.get("id");
String keyBucket = redisPrefix + "gateway:slots:key:" + keyId;
String waitingBucket = redisPrefix + "gateway:waiting:model:" + model.get("id");
String token = UUID.randomUUID().toString();
long waitMs = Math.max(0, Math.min(queueWaitMs, requestedWaitMs > 0 ? requestedWaitMs : queueWaitMs));
long deadline = System.nanoTime() + java.util.concurrent.TimeUnit.MILLISECONDS.toNanos(waitMs);
long lastBusyCode = 0;
while (true) {
long acquired = executePair(modelBucket, keyBucket, Integer.toString(modelConcurrency(model)), Integer.toString(defaultKeyConcurrency), Long.toString(SLOT_TTL_MS), token);
if (acquired == 1) return new Admission(modelBucket, keyBucket, token);
lastBusyCode = acquired;
if (waitMs == 0 || System.nanoTime() >= deadline) {
boolean keyBusy = lastBusyCode == 2;
throw new GatewayLimitException(keyBusy ? "KEY_BUSY" : "MODEL_BUSY", keyBusy ? "当前 API Key 并发已满,已排队等待但仍未获得资源" : "当前模型分组并发已满,已排队等待但仍未获得资源", 429, Math.max(1, (int) Math.ceil(queuePollMs / 1000.0)));
}
try {
long remaining = java.util.concurrent.TimeUnit.NANOSECONDS.toMillis(deadline - System.nanoTime());
Thread.sleep(Math.max(1, Math.min(queuePollMs, remaining)));
} catch (InterruptedException exception) {
Thread.currentThread().interrupt();
throw new GatewayLimitException("MODEL_BUSY", "排队被中断,请稍后重试", 429, 1);
boolean admitted = false;
try {
while (true) {
long remaining = Math.max(0, java.util.concurrent.TimeUnit.NANOSECONDS.toMillis(deadline - System.nanoTime()));
long acquired = executePair(modelBucket, keyBucket, waitingBucket, Integer.toString(modelConcurrency(model)), Integer.toString(defaultKeyConcurrency), Long.toString(SLOT_TTL_MS), token, Long.toString(remaining), Long.toString(Math.max(SLOT_TTL_MS, waitMs + 1000)));
if (acquired == 1) {
admitted = true; // The acquisition script also removed the waiting entry.
return new Admission(modelBucket, keyBucket, token);
}
if (waitMs == 0 || System.nanoTime() >= deadline) {
boolean keyBusy = acquired == 2;
throw new GatewayLimitException(keyBusy ? "KEY_BUSY" : "MODEL_BUSY", keyBusy ? "当前 API Key 并发已满,已排队等待但仍未获得资源" : "当前模型分组并发已满,已排队等待但仍未获得资源", 429, Math.max(1, (int) Math.ceil(queuePollMs / 1000.0)));
}
try {
Thread.sleep(Math.max(1, Math.min(queuePollMs, remaining)));
} catch (InterruptedException exception) {
Thread.currentThread().interrupt();
throw new GatewayLimitException("MODEL_BUSY", "排队被中断,请稍后重试", 429, 1);
}
}
} finally {
if (!admitted && waitMs > 0) releaseQuietly(waitingBucket, token);
}
}
@@ -174,9 +220,9 @@ public class GatewayGuard {
throw new GatewayLimitException("GATEWAY_GUARD_UNAVAILABLE", "网关流量控制暂不可用,请稍后重试", 503, 3);
}
private long executePair(String modelKey, String keyKey, String modelLimit, String keyLimit, String ttl, String token) {
private long executePair(String modelKey, String keyKey, String waitingKey, String modelLimit, String keyLimit, String ttl, String token, String waitMs, String waitingTtl) {
try {
Long value = redis.execute(ACQUIRE_PAIR, List.of(modelKey, keyKey), modelLimit, keyLimit, ttl, token);
Long value = redis.execute(ACQUIRE_PAIR, List.of(modelKey, keyKey, waitingKey), modelLimit, keyLimit, ttl, token, waitMs, waitingTtl);
if (value != null) return value;
} catch (DataAccessException exception) {
log.warn("Gateway pair admission Redis operation failed ({})", exception.getClass().getSimpleName());
@@ -184,6 +230,14 @@ public class GatewayGuard {
throw new GatewayLimitException("GATEWAY_GUARD_UNAVAILABLE", "网关流量控制暂不可用,请稍后重试", 503, 3);
}
private void releaseQuietly(String bucket, String token) {
try {
execute(RELEASE, bucket, token);
} catch (GatewayLimitException exception) {
log.warn("Gateway concurrency entry release failed; waiting for lease expiry");
}
}
public final class Admission implements AutoCloseable {
private final Slot modelSlot;
private final Slot keySlot;
@@ -212,12 +266,8 @@ public class GatewayGuard {
@Override
public void close() {
if (!closed.compareAndSet(false, true)) return;
try {
execute(RELEASE, key, token);
} catch (GatewayLimitException exception) {
// A failed release expires automatically and must not replace the upstream response.
log.warn("Gateway concurrency slot release failed; waiting for lease expiry");
}
// A failed release expires automatically and must not replace the upstream response.
releaseQuietly(key, token);
}
}
}
@@ -5,9 +5,11 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.moyi.controlplane.common.ApiException;
import com.moyi.controlplane.security.SecretCipher;
import java.net.URI;
import java.net.SocketTimeoutException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpTimeoutException;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
@@ -67,8 +69,13 @@ public class OpenAiCompatibleProvider {
try(var response=open(providerId,upstreamModel,input,false)) {
byte[] bytes=response.body.readNBytes(4*1024*1024+1);
if(bytes.length>4*1024*1024) throw new ApiException("UPSTREAM_RESPONSE_TOO_LARGE","上游响应超过限制",502);
return objectMapper.readValue(bytes, new com.fasterxml.jackson.core.type.TypeReference<>() {});
Map<String,Object> body=objectMapper.readValue(bytes, new com.fasterxml.jackson.core.type.TypeReference<>() {});
if(body.containsKey("error")) throw classifyUpstreamError(200,writeJson(body));
return body;
} catch(ApiException e) { throw e; }
catch(HttpTimeoutException | SocketTimeoutException e) {
throw new ApiException("UPSTREAM_PROVIDER_TIMEOUT","上游模型供应商响应超时",504);
}
catch(Exception e) { throw classifyUpstreamError(502, ""); }
}
@@ -109,6 +116,8 @@ public class OpenAiCompatibleProvider {
return new StreamResponse(response.body(),Math.max(1,timeoutSeconds*1000L-TimeUnit.NANOSECONDS.toMillis(System.nanoTime()-started)));
} catch (ApiException exception) {
throw exception;
} catch (HttpTimeoutException | SocketTimeoutException exception) {
throw new ApiException("UPSTREAM_PROVIDER_TIMEOUT", "上游模型供应商响应超时", 504);
} catch (InterruptedException exception) {
Thread.currentThread().interrupt();
throw new ApiException("UPSTREAM_PROVIDER_TIMEOUT", "上游模型供应商请求被中断", 502);
@@ -131,23 +140,28 @@ public class OpenAiCompatibleProvider {
StringBuilder event=new StringBuilder(), line=new StringBuilder();
long total=0;
int c;
// Bound both individual frames and the whole response before allocating unbounded strings.
while((c=reader.read())!=-1) {
try {
// Bound both individual frames and the whole response before allocating unbounded strings.
while((c=reader.read())!=-1) {
if(expired.get()) throw new ApiException("UPSTREAM_PROVIDER_TIMEOUT","上游响应超时,本次未扣费",504);
if(++total>4*1024*1024 || line.length()>262144 || event.length()>262144) throw new ApiException("UPSTREAM_RESPONSE_TOO_LARGE","上游响应超过限制",502);
if(c=='\n') {
String text=line.toString(); line.setLength(0);
if(text.endsWith("\r")) text=text.substring(0,text.length()-1);
if(text.isEmpty()) {
if(!event.isEmpty()) {
String data=event.substring(0,event.length()-1); event.setLength(0);
if("[DONE]".equals(data)) return;
Map<String,Object> chunk=objectMapper.readValue(data,new com.fasterxml.jackson.core.type.TypeReference<>(){});
if(chunk.containsKey("error")) throw classifyUpstreamError(502, writeJson(chunk));
consumer.accept(chunk);
}
} else if(text.startsWith("data:")) event.append(text.substring(5).stripLeading()).append('\n');
} else line.append((char)c);
}
} catch(IOException exception) {
if(expired.get()) throw new ApiException("UPSTREAM_PROVIDER_TIMEOUT","上游响应超时,本次未扣费",504);
if(++total>4*1024*1024 || line.length()>262144 || event.length()>262144) throw new ApiException("UPSTREAM_RESPONSE_TOO_LARGE","上游响应超过限制",502);
if(c=='\n') {
String text=line.toString(); line.setLength(0);
if(text.endsWith("\r")) text=text.substring(0,text.length()-1);
if(text.isEmpty()) {
if(!event.isEmpty()) {
String data=event.substring(0,event.length()-1); event.setLength(0);
if("[DONE]".equals(data)) return;
Map<String,Object> chunk=objectMapper.readValue(data,new com.fasterxml.jackson.core.type.TypeReference<>(){});
if(chunk.containsKey("error")) throw classifyUpstreamError(502, writeJson(chunk));
consumer.accept(chunk);
}
} else if(text.startsWith("data:")) event.append(text.substring(5).stripLeading()).append('\n');
} else line.append((char)c);
throw exception;
}
throw new ApiException(expired.get()?"UPSTREAM_PROVIDER_TIMEOUT":"UPSTREAM_STREAM_INCOMPLETE","上游流式响应中断,本次未扣费",502);
}
@@ -169,6 +183,9 @@ public class OpenAiCompatibleProvider {
*/
static ApiException classifyUpstreamError(int status, String body) {
String details = body == null ? "" : body.toLowerCase(java.util.Locale.ROOT);
if (status == 408 || status == 504 || details.matches(".*(request|gateway|upstream).{0,12}(timed out|timeout|超时).*")) {
return new ApiException("UPSTREAM_PROVIDER_TIMEOUT", "上游模型供应商响应超时", 504);
}
if (status == 401 || status == 403 || details.matches(".*(invalid.{0,8}(api[ _-]?key|token)|unauthori[sz]ed|forbidden|无效.*(key|凭据)|无权限|认证失败).*")) {
return new ApiException("UPSTREAM_AUTH_FAILED", "上游渠道凭据无效或无权限", 502);
}
@@ -178,6 +195,9 @@ public class OpenAiCompatibleProvider {
if (status == 429 || details.matches(".*(rate.?limit|too many requests|concurr|并发|频率过高|请求过于频繁|限流).*")) {
return new ApiException("UPSTREAM_RATE_LIMITED", "上游渠道限流或并发已满,请稍后重试", 429);
}
if (status == 404 || details.matches(".*(no available channel|model.{0,16}(not found|does not exist|unavailable)|not_found|unsupported model|没有可用渠道|模型不存在|模型不可用).*")) {
return new ApiException("UPSTREAM_MODEL_UNAVAILABLE", "上游渠道没有可用的模型路由", 502);
}
return new ApiException("UPSTREAM_PROVIDER_ERROR", "上游模型供应商请求失败", 502);
}
@@ -73,12 +73,21 @@ public class AsyncProviderClient {
if ("POST".equals(method)) builder.POST(HttpRequest.BodyPublishers.ofString(write(body)));
else builder.GET();
HttpResponse<String> response = client.send(builder.build(), HttpResponse.BodyHandlers.ofString());
if (response.statusCode() < 200 || response.statusCode() >= 300) throw new ApiException("UPSTREAM_PROVIDER_ERROR", message + "(HTTP " + response.statusCode() + ")", 502);
if (response.statusCode() < 200 || response.statusCode() >= 300) {
String code = switch (response.statusCode()) {
case 401, 403 -> "UPSTREAM_AUTH_FAILED";
case 402 -> "UPSTREAM_CREDIT_INSUFFICIENT";
case 429 -> "UPSTREAM_RATE_LIMITED";
default -> "UPSTREAM_PROVIDER_ERROR";
};
throw new ApiException(code, message + "(HTTP " + response.statusCode() + ")", 502);
}
Map<String,Object> parsed = mapper.readValue(response.body(), new TypeReference<>() {});
Object code = parsed.get("code");
if (parsed.containsKey("error") || code instanceof Number number && number.intValue() != 0) {
String detail = parsed.get("message") == null ? message : String.valueOf(parsed.get("message"));
throw new ApiException("UPSTREAM_PROVIDER_ERROR", detail, 502);
// Upstream messages can echo credentials or account details.
// Only locally defined diagnostics may reach task history/UI.
throw new ApiException("UPSTREAM_PROVIDER_ERROR", message, 502);
}
return parsed;
} catch (ApiException exception) { throw exception; }
@@ -35,6 +35,7 @@ public class AsyncTaskService {
private final int maxQueuedPerModel;
private final int defaultModelConcurrency;
private final int defaultKeyConcurrency;
private final long providerTimeoutMs;
private final GatewayGuard gatewayGuard;
private final UsageAuditService usage;
private final AsyncProviderClient providerClient;
@@ -43,7 +44,7 @@ public class AsyncTaskService {
/** 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, gatewayGuard, usage, null, null);
this(jdbc, objectMapper, catalogService, maxQueuedPerModel, 4, 4, 3_600_000, gatewayGuard, usage, null, null);
}
@org.springframework.beans.factory.annotation.Autowired
@@ -54,18 +55,21 @@ public class AsyncTaskService {
@Value("${moyi.tasks.max-queued-per-model:100}") int maxQueuedPerModel,
@Value("${moyi.gateway.default-model-concurrency:4}") int defaultModelConcurrency,
@Value("${moyi.gateway.default-key-concurrency:4}") int defaultKeyConcurrency,
@Value("${moyi.tasks.provider-timeout-ms:3600000}") long providerTimeoutMs,
GatewayGuard gatewayGuard,
UsageAuditService usage,
AsyncProviderClient providerClient,
PlatformTransactionManager transactionManager
) {
if (maxQueuedPerModel < 1 || defaultModelConcurrency < 1 || defaultKeyConcurrency < 1) throw new IllegalArgumentException("Task queue and concurrency limits must be positive");
if (providerTimeoutMs < 1_000) throw new IllegalArgumentException("Provider task timeout must be at least one second");
this.jdbc = jdbc;
this.objectMapper = objectMapper;
this.catalogService = catalogService;
this.maxQueuedPerModel = maxQueuedPerModel;
this.defaultModelConcurrency = defaultModelConcurrency;
this.defaultKeyConcurrency = defaultKeyConcurrency;
this.providerTimeoutMs = providerTimeoutMs;
this.gatewayGuard = gatewayGuard;
this.usage = usage;
this.providerClient = providerClient;
@@ -155,7 +159,7 @@ public class AsyncTaskService {
"{\"format\":\"mp4\",\"url\":\"https://mock.moyi.ai/tasks/result.mp4\"}", now(), now()
);
taskIds.forEach(taskId -> usage.updateTask(taskId, "succeeded", Map.of("format", "result", "url", "https://mock.moyi.ai/tasks/" + taskId + "/result"), null, null));
List<Map<String,Object>> pending = jdbc.queryForList("SELECT t.id, t.provider_task_id, m.provider_id FROM model_tasks t JOIN model_catalog m ON m.id=t.model_id WHERE t.status IN ('queued','running') AND t.provider_task_id <> 'mock' ORDER BY t.created_at LIMIT 50");
List<Map<String,Object>> pending = jdbc.queryForList("SELECT t.id, t.provider_task_id, t.provider_deadline_at, t.created_at, m.provider_id FROM model_tasks t JOIN model_catalog m ON m.id=t.model_id WHERE t.status IN ('queued','running') AND t.provider_task_id <> 'mock' AND (t.updated_at < now() - INTERVAL '5 seconds' OR t.provider_deadline_at <= now()) ORDER BY t.updated_at LIMIT 50");
for (Map<String,Object> row : pending) pollProviderTask(row);
dispatchQueuedTasks();
}
@@ -185,9 +189,12 @@ public class AsyncTaskService {
String.valueOf(task.get("task_type")),
String.valueOf(task.get("prompt"))
);
jdbc.update("UPDATE model_tasks SET provider_task_id=?, updated_at=? WHERE id=? AND status='running'", providerTaskId(upstream), now(), taskId);
OffsetDateTime submittedAt = now();
jdbc.update("UPDATE model_tasks SET provider_task_id=?, provider_deadline_at=?, updated_at=? WHERE id=? AND status='running'", providerTaskId(upstream), submittedAt.plus(java.time.Duration.ofMillis(providerTimeoutMs)), submittedAt, taskId);
} catch (RuntimeException exception) {
failTask(taskId, "UPSTREAM_TASK_SUBMIT_FAILED", exception.getMessage() == null ? "上游异步任务创建失败" : exception.getMessage());
String code = exception instanceof ApiException error ? error.getCode() : "UPSTREAM_TASK_SUBMIT_FAILED";
String message = exception instanceof ApiException error ? error.getMessage() : "上游异步任务创建失败";
failTask(taskId, code, message);
}
}
}
@@ -236,12 +243,24 @@ public class AsyncTaskService {
}
private void failTask(String taskId, String code, String message) {
int changed = jdbc.update(
"UPDATE model_tasks SET status='failed', error=CAST(? AS jsonb), updated_at=?, completed_at=? WHERE id=? AND status IN ('queued','running')",
writeJson(Map.of("code", code, "message", message)), now(), now(), taskId);
if (changed == 0) return;
releaseTask(taskId);
usage.updateTask(taskId, "failed", null, code, message);
finishTask(taskId, "failed", 0, null, code, message);
}
private void finishTask(String taskId, String status, int progress, Map<String,Object> result, String code, String message) {
// The terminal state and wallet change must commit together. Otherwise a
// crash after marking failed/succeeded leaves a hold that polling never
// revisits. The conditional update also prevents duplicate settlement.
inTransaction(() -> {
int changed = jdbc.update(
"UPDATE model_tasks SET status=?, progress=?, result=CAST(? AS jsonb), error=CAST(? AS jsonb), updated_at=?, completed_at=? WHERE id=? AND status IN ('queued','running')",
status, progress, result == null ? null : writeJson(result),
code == null ? null : writeJson(Map.of("code", code, "message", message)), now(), now(), taskId);
if (changed == 0) return null;
if ("succeeded".equals(status)) settleTaskInTransaction(taskId);
else releaseTaskInTransaction(taskId);
usage.updateTask(taskId, status, result, code, message);
return null;
});
}
private Map<String, Object> requireKey(String authorization) {
@@ -286,27 +305,59 @@ public class AsyncTaskService {
private void pollProviderTask(Map<String,Object> row) {
String taskId = String.valueOf(row.get("id"));
try {
Map<String,Object> response = providerClient.poll(String.valueOf(row.get("provider_id")), String.valueOf(row.get("provider_task_id")));
String status = normalizedStatus(response);
int progress = progress(response, status);
Map<String,Object> result = result(response);
if ("succeeded".equals(status) || "failed".equals(status) || "cancelled".equals(status)) {
jdbc.update("UPDATE model_tasks SET status=?, progress=?, result=CAST(? AS jsonb), error=CAST(? AS jsonb), updated_at=?, completed_at=? WHERE id=? AND status IN ('queued','running')", status, progress, result == null ? null : writeJson(result), "failed".equals(status) ? writeJson(Map.of("message", "上游异步任务失败")) : null, now(), now(), taskId);
if ("succeeded".equals(status)) settleTask(taskId);
else releaseTask(taskId);
usage.updateTask(taskId, status, result, "failed".equals(status) ? "UPSTREAM_TASK_FAILED" : null, "failed".equals(status) ? "上游异步任务失败" : null);
} else {
jdbc.update("UPDATE model_tasks SET status=?, progress=?, updated_at=? WHERE id=? AND status IN ('queued','running')", status, progress, now(), taskId);
}
} catch (Exception exception) {
jdbc.update("UPDATE model_tasks SET status='failed', error=CAST(? AS jsonb), updated_at=?, completed_at=? WHERE id=? AND status IN ('queued','running')", writeJson(Map.of("message", "上游异步任务查询失败")), now(), now(), taskId);
// A polling failure is terminal for this task. Release the
// reserved wallet amount as part of the same failure path so a
// transient/provider error cannot permanently lock user credit.
releaseTask(taskId);
usage.updateTask(taskId, "failed", null, "UPSTREAM_TASK_POLL_FAILED", "上游异步任务查询失败");
OffsetDateTime deadline = taskTimestamp(row.get("provider_deadline_at"));
if (deadline == null) {
// Defensive fallback for pre-migration tasks. The migration also
// persists deadlines so restarting cannot reset their timeout.
OffsetDateTime createdAt = taskTimestamp(row.get("created_at"));
if (createdAt == null) throw new IllegalStateException("Persisted task timestamp is missing");
deadline = createdAt.plus(java.time.Duration.ofMillis(providerTimeoutMs));
}
if (!now().isBefore(deadline)) {
failTask(taskId, "UPSTREAM_TASK_TIMEOUT", "异步任务查询已超时,执行结果未知;平台冻结额度已释放。上游可能仍在执行,请先核实后再提交");
return;
}
Map<String,Object> response;
try {
response = providerClient.poll(String.valueOf(row.get("provider_id")), String.valueOf(row.get("provider_task_id")));
} catch (RuntimeException exception) {
// Even a permanent authentication/configuration error only proves
// that polling failed, not that an already accepted job stopped.
// Keep its hold and retry until the persisted deadline.
String message = exception instanceof ApiException error && "UPSTREAM_AUTH_FAILED".equals(error.getCode())
? "上游查询鉴权异常,执行结果尚未确认;正在重试"
: "暂时无法查询上游任务,执行结果尚未确认;正在重试";
updatePollingState(taskId, null, "UPSTREAM_TASK_POLL_RETRY", message);
return;
}
String status = normalizedStatus(response);
int progress = progress(response, status);
Map<String,Object> result = result(response);
if ("succeeded".equals(status) || "failed".equals(status) || "cancelled".equals(status)) {
// Persistence failures must roll back and remain pollable, rather
// than being mistaken for upstream failures and refunded.
finishTask(taskId, status, progress, result,
"failed".equals(status) ? "UPSTREAM_TASK_FAILED" : null,
"failed".equals(status) ? "上游异步任务失败" : null);
} else {
updatePollingState(taskId, progress, null, null);
}
}
private void updatePollingState(String taskId, Integer progress, String code, String message) {
inTransaction(() -> {
int changed = jdbc.update("UPDATE model_tasks SET status='running', progress=COALESCE(?,progress), error=CAST(? AS jsonb), updated_at=? WHERE id=? AND status IN ('queued','running')",
progress, code == null ? null : writeJson(Map.of("code", code, "message", message, "retryable", true)), now(), taskId);
if (changed > 0) usage.updateTask(taskId, "running", null, code, message);
return null;
});
}
private OffsetDateTime taskTimestamp(Object value) {
if (value == null) return null;
if (value instanceof OffsetDateTime timestamp) return timestamp;
if (value instanceof java.sql.Timestamp timestamp) return timestamp.toInstant().atOffset(ZoneOffset.UTC);
throw new IllegalStateException("Persisted task timestamp is missing");
}
private String normalizedStatus(Map<String,Object> response) {
@@ -381,14 +432,6 @@ public class AsyncTaskService {
if (amount.signum() > 0) jdbc.update("UPDATE user_wallets SET reserved_balance=GREATEST(0,reserved_balance-?),updated_at=now() WHERE user_id=?", amount, userId);
}
private void settleTask(String taskId) {
if (transactions == null) {
settleTaskInTransaction(taskId);
return;
}
transactions.executeWithoutResult(status -> settleTaskInTransaction(taskId));
}
private void settleTaskInTransaction(String taskId) {
Map<String,Object> task = jdbc.queryForMap("SELECT user_id,reserved_amount,cost_amount FROM model_tasks WHERE id=? FOR UPDATE", taskId);
BigDecimal amount = (BigDecimal) task.get("reserved_amount");
@@ -403,20 +446,13 @@ public class AsyncTaskService {
jdbc.update("UPDATE usage_events SET cost_amount=? WHERE provider_task_id=? AND event_kind='task'", amount, taskId);
}
private void releaseTask(String taskId) {
if (transactions == null) {
releaseTaskInTransaction(taskId);
return;
}
transactions.executeWithoutResult(status -> releaseTaskInTransaction(taskId));
}
private void releaseTaskInTransaction(String taskId) {
Map<String,Object> task = jdbc.queryForMap("SELECT user_id,reserved_amount,cost_amount FROM model_tasks WHERE id=? FOR UPDATE", taskId);
BigDecimal amount = (BigDecimal) task.get("reserved_amount");
if (amount != null && amount.signum() > 0 && task.get("cost_amount") == null) {
releaseWallet((UUID) task.get("user_id"), amount);
jdbc.update("UPDATE model_tasks SET cost_amount=0 WHERE id=?", taskId);
jdbc.update("UPDATE usage_events SET cost_amount=0 WHERE provider_task_id=? AND event_kind='task'", taskId);
}
}
@@ -7,6 +7,7 @@ import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.moyi.controlplane.auth.AuthService;
import com.moyi.controlplane.common.ApiException;
import com.moyi.controlplane.gateway.GatewayGuard;
import java.nio.charset.StandardCharsets;
import java.sql.ResultSet;
import java.sql.SQLException;
@@ -21,16 +22,31 @@ import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
@Service
public class UsageAuditService {
private static final int MAX_PAYLOAD_CHARS = 32_000;
private final JdbcTemplate jdbc;
private final ObjectMapper objectMapper;
private final GatewayGuard gatewayGuard;
private final int asyncQueueLimit;
private final long asyncWorkerIntervalMs;
public UsageAuditService(JdbcTemplate jdbc, ObjectMapper objectMapper) {
this(jdbc, objectMapper, null, 100, 1000);
}
@Autowired
public UsageAuditService(JdbcTemplate jdbc, ObjectMapper objectMapper, GatewayGuard gatewayGuard,
@Value("${moyi.tasks.max-queued-per-model:100}") int asyncQueueLimit,
@Value("${moyi.tasks.worker-interval-ms:1000}") long asyncWorkerIntervalMs) {
this.jdbc = jdbc;
this.objectMapper = objectMapper;
this.gatewayGuard = gatewayGuard;
this.asyncQueueLimit = asyncQueueLimit;
this.asyncWorkerIntervalMs = asyncWorkerIntervalMs;
}
public void startChat(UUID id, UUID userId, UUID keyId, Map<String,Object> model, Map<String,Object> input) {
@@ -105,7 +121,6 @@ public class UsageAuditService {
Map<String, Object> result = new LinkedHashMap<>();
result.put("callsToday", count("SELECT COUNT(*) FROM usage_events WHERE deleted_at IS NULL AND event_kind = 'call' AND created_at >= CURRENT_DATE"));
result.put("callsThisMonth", count("SELECT COUNT(*) FROM usage_events WHERE deleted_at IS NULL AND event_kind = 'call' AND created_at >= date_trunc('month', now())"));
result.put("waiting", count("SELECT COUNT(*) FROM usage_events WHERE deleted_at IS NULL AND status IN ('queued','running')"));
result.put("costToday", decimal("SELECT COALESCE(SUM(cost_amount),0) FROM usage_events WHERE deleted_at IS NULL AND status = 'succeeded' AND created_at >= CURRENT_DATE"));
result.put("costThisMonth", decimal("SELECT COALESCE(SUM(cost_amount),0) FROM usage_events WHERE deleted_at IS NULL AND status = 'succeeded' AND created_at >= date_trunc('month', now())"));
result.put("series", jdbc.query(
@@ -114,26 +129,70 @@ public class UsageAuditService {
"LEFT JOIN usage_events ue ON ue.deleted_at IS NULL AND ue.created_at >= day AND ue.created_at < day + INTERVAL '1 day' " +
"GROUP BY day ORDER BY day",
(rs, row) -> Map.of("date", rs.getString("day"), "count", rs.getLong("count"))));
result.put("capacityGroups", jdbc.query(
"SELECT m.id, m.public_name, COALESCE(m.max_concurrency, 0) AS max_concurrency, " +
"COUNT(*) FILTER (WHERE ue.status = 'running') AS running, " +
"COUNT(*) FILTER (WHERE ue.status IN ('queued','running')) AS queued " +
"FROM model_catalog m LEFT JOIN usage_events ue ON ue.model_id = m.id " +
"AND ue.deleted_at IS NULL AND ue.created_at >= now() - INTERVAL '10 minutes' " +
"WHERE m.deleted_at IS NULL AND m.status = 'active' " +
"GROUP BY m.id, m.public_name, m.max_concurrency ORDER BY m.public_name LIMIT 100",
int defaultModelLimit = gatewayGuard == null ? 4 : gatewayGuard.defaultModelConcurrency();
List<Map<String, Object>> groups = jdbc.query("""
WITH pending AS (
SELECT model_id,
COUNT(*) FILTER (WHERE status='running') AS running,
COUNT(*) FILTER (WHERE status='queued') AS queued
FROM model_tasks WHERE status IN ('queued','running') GROUP BY model_id
)
SELECT m.id, m.public_name, m.source_label, p.name AS provider_name, m.call_mode,
COALESCE(NULLIF(m.max_concurrency, 0), ?) AS max_concurrency,
(m.status='active' AND m.deleted_at IS NULL AND p.status='active' AND p.deleted_at IS NULL
AND d.status='active' AND d.deleted_at IS NULL) AS route_enabled,
COALESCE(pending.running, 0) AS running, COALESCE(pending.queued, 0) AS queued
FROM model_catalog m JOIN model_providers p ON p.id=m.provider_id
JOIN model_definitions d ON d.id=m.logical_model_id
LEFT JOIN pending ON pending.model_id=m.id
ORDER BY m.public_name, m.source_label, m.id
""",
(rs, row) -> {
Map<String, Object> group = new LinkedHashMap<>();
group.put("id", rs.getString("id"));
group.put("name", rs.getString("public_name"));
group.put("sourceLabel", rs.getString("source_label"));
group.put("providerName", rs.getString("provider_name"));
group.put("mode", rs.getString("call_mode"));
group.put("routeEnabled", rs.getBoolean("route_enabled"));
group.put("limit", rs.getInt("max_concurrency"));
group.put("running", rs.getLong("running"));
group.put("queued", rs.getLong("queued"));
return group;
}));
}, defaultModelLimit);
List<String> synchronousIds = groups.stream().filter(group -> !"async".equals(group.get("mode"))).map(group -> group.get("id").toString()).toList();
GatewayGuard.CapacitySnapshot snapshot = gatewayGuard == null
? new GatewayGuard.CapacitySnapshot(false, Map.of()) : gatewayGuard.capacitySnapshot(synchronousIds);
for (Map<String, Object> group : groups) {
if ("async".equals(group.get("mode"))) continue;
GatewayGuard.Capacity current = snapshot.groups().get(group.get("id").toString());
group.put("running", current == null ? null : current.running());
group.put("queued", current == null ? null : current.queued());
}
// Keep disabled groups visible while their queued or running work still exists.
groups = groups.stream().filter(group -> Boolean.TRUE.equals(group.get("routeEnabled"))
|| positive(group.get("running")) || positive(group.get("queued"))).toList();
result.put("capacityGroups", groups);
result.put("running", totalCapacity(groups, "running"));
result.put("waiting", totalCapacity(groups, "queued"));
result.put("capacityConfig", Map.of(
"queueWaitMs", gatewayGuard == null ? 30000 : gatewayGuard.queueWaitMs(),
"defaultModelConcurrency", defaultModelLimit,
"defaultKeyConcurrency", gatewayGuard == null ? 4 : gatewayGuard.defaultKeyConcurrency(),
"asyncQueueLimit", asyncQueueLimit,
"asyncWorkerIntervalMs", asyncWorkerIntervalMs,
"synchronousSnapshotAvailable", snapshot.available()
));
return result;
}
private boolean positive(Object value) { return value instanceof Number number && number.longValue() > 0; }
private Long totalCapacity(List<Map<String, Object>> groups, String field) {
if (groups.stream().anyMatch(group -> group.get(field) == null)) return null;
return groups.stream().mapToLong(group -> ((Number) group.get(field)).longValue()).sum();
}
private long count(String sql) {
Number value = jdbc.queryForObject(sql, Number.class);
return value == null ? 0L : value.longValue();