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();
@@ -50,6 +50,7 @@ moyi:
tasks:
max-queued-per-model: ${TASK_MAX_QUEUED_PER_MODEL:100}
worker-interval-ms: ${MOYI_TASK_WORKER_INTERVAL_MS:1000}
provider-timeout-ms: ${MOYI_TASK_PROVIDER_TIMEOUT_MS:3600000}
web:
cors-origins: ${CONTROL_PLANE_CORS_ORIGINS:http://localhost:5173,http://127.0.0.1:5173}
auth:
@@ -0,0 +1,10 @@
-- Persist the polling deadline: process restarts must not reset the timeout.
ALTER TABLE model_tasks ADD COLUMN provider_deadline_at timestamptz;
-- Existing accepted tasks also need a finite deadline. Unsubmitted queued
-- tasks receive theirs only after the upstream accepts their submission.
UPDATE model_tasks
SET provider_deadline_at = created_at + INTERVAL '1 hour'
WHERE status IN ('queued', 'running')
AND provider_task_id IS NOT NULL
AND provider_task_id <> 'mock';
@@ -2,8 +2,18 @@ local time = redis.call('TIME')
local now = tonumber(time[1]) * 1000 + math.floor(tonumber(time[2]) / 1000)
redis.call('ZREMRANGEBYSCORE', KEYS[1], '-inf', now)
redis.call('ZREMRANGEBYSCORE', KEYS[2], '-inf', now)
if redis.call('ZCARD', KEYS[1]) >= tonumber(ARGV[1]) then return 0 end
if redis.call('ZCARD', KEYS[2]) >= tonumber(ARGV[2]) then return 2 end
redis.call('ZREMRANGEBYSCORE', KEYS[3], '-inf', now)
local busy = nil
if redis.call('ZCARD', KEYS[1]) >= tonumber(ARGV[1]) then busy = 0
elseif redis.call('ZCARD', KEYS[2]) >= tonumber(ARGV[2]) then busy = 2 end
if busy ~= nil then
if tonumber(ARGV[5]) > 0 then
redis.call('ZADD', KEYS[3], now + tonumber(ARGV[5]), ARGV[4])
redis.call('PEXPIRE', KEYS[3], ARGV[6])
end
return busy
end
redis.call('ZREM', KEYS[3], ARGV[4])
redis.call('ZADD', KEYS[1], now + tonumber(ARGV[3]), ARGV[4])
redis.call('ZADD', KEYS[2], now + tonumber(ARGV[3]), ARGV[4])
redis.call('PEXPIRE', KEYS[1], ARGV[3])
@@ -68,7 +68,8 @@ class ChatStreamingTest {
// Leave the body open: the client hard deadline must terminate the read.
}); server.start();
long started=System.nanoTime();
assertThrows(Exception.class,()->{ try(var stream=provider.stream("provider-deepseek","alias",request)) { stream.events(chunk->{}); } });
var error=assertThrows(ApiException.class,()->{ try(var stream=provider.stream("provider-deepseek","alias",request)) { stream.events(chunk->{}); } });
assertEquals("UPSTREAM_PROVIDER_TIMEOUT",error.getCode());
assertTrue(TimeUnit.NANOSECONDS.toMillis(System.nanoTime()-started)<4000);
}
@Test void upstreamHttpFailureNeverBecomesSuccessfulStream() {
@@ -84,6 +85,31 @@ class ChatStreamingTest {
assertThrows(ApiException.class,()->ChatMeter.tokens(Map.of("usage",Map.of("prompt_tokens",1.5,"completion_tokens",2))));
assertThrows(ApiException.class,()->ChatMeter.tokens(Map.of("usage",Map.of("prompt_tokens",1,"completion_tokens",2,"prompt_cache_hit_tokens",3))));
}
@Test void reasoningAliasAndContentPartsAreCollectedSeparately() {
var collector=new ChatStreamCollector("public-route",System.currentTimeMillis());
collector.accept(Map.of("choices",List.of(Map.of("delta",Map.of(
"content",List.of(Map.of("type","text","text","OK")),
"reasoning", "thinking"
)))));
collector.accept(Map.of("choices",List.of(Map.of("delta",Map.of(),"finish_reason","stop"))));
var message=(Map<?,?>)((Map<?,?>)((List<?>)collector.result().get("choices")).getFirst()).get("message");
assertEquals("OK",message.get("content"));
assertEquals("thinking",message.get("reasoning_content"));
}
@Test void finishedWhitespaceOnlyStreamIsRejectedBeforeSettlement() {
var collector=new ChatStreamCollector("public-route",System.currentTimeMillis());
collector.accept(Map.of("choices",List.of(Map.of("delta",Map.of("content"," \n")))));
collector.accept(Map.of("choices",List.of(Map.of("delta",Map.of(),"finish_reason","stop"))));
var error=assertThrows(ApiException.class,collector::result);
assertEquals("UPSTREAM_RESPONSE_INVALID",error.getCode());
}
@Test void completedStreamWithoutContentOrReasoningIsNotBillable() {
var collector=new ChatStreamCollector("public-route",System.currentTimeMillis());
collector.accept(Map.of("choices",List.of(Map.of("index",0,"delta",Map.of(),"finish_reason","stop"))));
assertEquals("UPSTREAM_RESPONSE_INVALID",assertThrows(ApiException.class,collector::result).getCode());
}
@Test void tokenBoundsAndUnsupportedMultimodalAreRejectedBeforeUpstream() {
assertThrows(ApiException.class,()->ChatMeter.request(Map.of("messages",List.of(Map.of("role","user","content",List.of(Map.of("type","image_url"))))),json));
assertThrows(ApiException.class,()->ChatMeter.request(Map.of("messages",List.of(Map.of("role","user","content","test")),"max_tokens",0),json));
@@ -20,6 +20,32 @@ import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
class GatewayDeploymentTest {
@Test
void capacitySnapshotReadsBothLiveAndWaitingBucketsAndDoesNotInventZeroOnFailure() {
StringRedisTemplate redis = mock(StringRedisTemplate.class);
doReturn(List.of(2L, 3L, 0L, 1L)).when(redis).execute(any(RedisScript.class), anyList());
GatewayGuard guard = new GatewayGuard(redis, 60, 300, 4, "moyi:test:");
var snapshot = guard.capacitySnapshot(List.of("model-a", "model-b"));
assertTrue(snapshot.available());
assertEquals(new GatewayGuard.Capacity(2, 3), snapshot.groups().get("model-a"));
assertEquals(new GatewayGuard.Capacity(0, 1), snapshot.groups().get("model-b"));
verify(redis).execute(any(RedisScript.class), eq(List.of(
"moyi:test:gateway:slots:model:model-a", "moyi:test:gateway:waiting:model:model-a",
"moyi:test:gateway:slots:model:model-b", "moyi:test:gateway:waiting:model:model-b")));
doThrow(new org.springframework.dao.DataAccessResourceFailureException("offline")).when(redis).execute(any(RedisScript.class), anyList());
assertFalse(guard.capacitySnapshot(List.of("model-a")).available());
assertTrue(guard.capacitySnapshot(List.of("model-a")).groups().isEmpty());
}
@Test
void queueTimeoutCleansItsWaitingEntry() {
StringRedisTemplate redis = mock(StringRedisTemplate.class);
doReturn(0L).when(redis).execute(any(RedisScript.class), anyList(), any(Object[].class));
GatewayGuard guard = new GatewayGuard(redis, 60, 300, 1, 1, 15, 10, "moyi:test:");
assertThrows(GatewayLimitException.class, () -> guard.acquire(Map.of("id", "model-a"), UUID.randomUUID(), 15));
verify(redis).execute(any(RedisScript.class), eq(List.of("moyi:test:gateway:waiting:model:model-a")), any(Object[].class));
}
@Test
void redisRateKeysAreEnvironmentScoped() {
StringRedisTemplate redis = mock(StringRedisTemplate.class);
@@ -108,6 +134,65 @@ class GatewayDeploymentTest {
assertEquals(200, controller.chatCompletions("Bearer test-key", Map.of("model", "mock","messages",List.of(Map.of("role","user","content","test"))),null,new MockHttpServletResponse()).getStatusCode().value());
}
@Test
void syncResponsePreservesReasoningAndFinishReasonWhenContentIsEmpty() {
CatalogService catalog = mock(CatalogService.class);
GatewayGuard guard = mock(GatewayGuard.class);
OpenAiCompatibleProvider provider = mock(OpenAiCompatibleProvider.class);
ChatBillingService billing = mock(ChatBillingService.class);
String userId = UUID.randomUUID().toString();
Map<String, Object> key = Map.of("id", UUID.randomUUID().toString(), "userId", userId);
Map<String, Object> model = Map.of("id", "reasoning-route", "providerId", "provider-relay", "upstreamModel", "reasoning-model", "mode", "sync");
when(catalog.authenticateKey("test-key")).thenReturn(key);
when(catalog.activeModel("reasoning-route")).thenReturn(model);
when(catalog.keyCanInvoke(key, "reasoning-route")).thenReturn(true);
when(provider.isConfigured("provider-relay")).thenReturn(true);
Map<String, Object> upstreamResponse = new java.util.LinkedHashMap<>(Map.of(
"id", "upstream-response",
"choices", List.of(Map.of("index", 0, "message", Map.of("role", "assistant", "content", "", "reasoning_content", "private reasoning"), "finish_reason", "length")),
"usage", Map.of("prompt_tokens", 2, "completion_tokens", 16, "total_tokens", 18)
));
when(provider.chat(eq("provider-relay"), eq("reasoning-model"), anyMap())).thenReturn(upstreamResponse);
when(billing.reserve(anyMap(), anyMap(), any(), isNull())).thenReturn(new ChatBillingService.Ticket(UUID.randomUUID(), UUID.fromString(userId), ChatMeter.rates(null, true)));
when(billing.settle(any(), anyMap(), anyInt(), isNull())).thenReturn(java.math.BigDecimal.ZERO);
GatewayController controller = new GatewayController(catalog, provider, guard, billing, new ObjectMapper());
var response = controller.chatCompletions("Bearer test-key", Map.of("model", "reasoning-route", "messages", List.of(Map.of("role", "user", "content", "hello"))), null, new MockHttpServletResponse());
assertEquals(200, response.getStatusCode().value());
Map<?, ?> choice = (Map<?, ?>)((List<?>)response.getBody().get("choices")).getFirst();
Map<?, ?> message = (Map<?, ?>)choice.get("message");
assertEquals("", message.get("content"));
assertEquals("private reasoning", message.get("reasoning_content"));
assertEquals("length", choice.get("finish_reason"));
}
@Test
void trulyEmptySyncResponseIsRejectedBeforeSettlement() {
CatalogService catalog = mock(CatalogService.class);
GatewayGuard guard = mock(GatewayGuard.class);
OpenAiCompatibleProvider provider = mock(OpenAiCompatibleProvider.class);
ChatBillingService billing = mock(ChatBillingService.class);
String userId = UUID.randomUUID().toString();
Map<String, Object> key = Map.of("id", UUID.randomUUID().toString(), "userId", userId);
Map<String, Object> model = Map.of("id", "empty-route", "providerId", "provider-relay", "upstreamModel", "empty-model", "mode", "sync");
when(catalog.authenticateKey("test-key")).thenReturn(key);
when(catalog.activeModel("empty-route")).thenReturn(model);
when(catalog.keyCanInvoke(key, "empty-route")).thenReturn(true);
when(provider.isConfigured("provider-relay")).thenReturn(true);
Map<String, Object> upstreamResponse = new java.util.LinkedHashMap<>(Map.of(
"id", "upstream-empty",
"choices", List.of(Map.of("index", 0, "message", Map.of("role", "assistant", "content", ""), "finish_reason", "stop"))
));
when(provider.chat(eq("provider-relay"), eq("empty-model"), anyMap())).thenReturn(upstreamResponse);
when(billing.reserve(anyMap(), anyMap(), any(), isNull())).thenReturn(new ChatBillingService.Ticket(UUID.randomUUID(), UUID.fromString(userId), ChatMeter.rates(null, true)));
GatewayController controller = new GatewayController(catalog, provider, guard, billing, new ObjectMapper());
ApiException error = assertThrows(ApiException.class, () -> controller.chatCompletions("Bearer test-key", Map.of("model", "empty-route", "messages", List.of(Map.of("role", "user", "content", "hello"))), null, new MockHttpServletResponse()));
assertEquals("UPSTREAM_RESPONSE_INVALID", error.getCode());
verify(billing, never()).settle(any(), anyMap(), anyInt(), any());
verify(billing).fail(any(), eq("UPSTREAM_RESPONSE_INVALID"), anyString(), anyInt());
}
@Test
void unimplementedMediaProviderDoesNotCreateFakeTask() {
JdbcTemplate jdbc = mock(JdbcTemplate.class);
@@ -26,4 +26,25 @@ class ProviderErrorClassificationTest {
assertEquals("UPSTREAM_RATE_LIMITED", error.getCode());
assertEquals(429, error.getStatus());
}
@Test
void classifiesMissingUpstreamRouteWithoutEchoingProviderDetails() {
var error = OpenAiCompatibleProvider.classifyUpstreamError(502, "No available channel for model gemini-3-flash");
assertEquals("UPSTREAM_MODEL_UNAVAILABLE", error.getCode());
assertEquals(502, error.getStatus());
assertEquals("上游渠道没有可用的模型路由", error.getMessage());
}
@Test
void classifiesErrorEnvelopeReturnedWithHttp200() {
var error = OpenAiCompatibleProvider.classifyUpstreamError(200, "{\"error\":{\"code\":\"invalid_api_key\",\"message\":\"unauthorized\"}}");
assertEquals("UPSTREAM_AUTH_FAILED", error.getCode());
}
@Test
void classifiesTimeoutResponsesWithoutExposingProviderBody() {
var error = OpenAiCompatibleProvider.classifyUpstreamError(504, "gateway timeout at upstream");
assertEquals("UPSTREAM_PROVIDER_TIMEOUT", error.getCode());
assertEquals(504, error.getStatus());
}
}
@@ -0,0 +1,77 @@
package com.moyi.controlplane.task;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.moyi.controlplane.common.ApiException;
import com.moyi.controlplane.security.SecretCipher;
import com.sun.net.httpserver.HttpServer;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate;
class AsyncProviderClientTest {
@Test
void upstreamErrorDoesNotExposeProviderMessage() throws Exception {
HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
server.createContext("/", exchange -> {
byte[] body = "{\"code\":1001,\"message\":\"secret account and API key details\"}".getBytes(StandardCharsets.UTF_8);
exchange.getResponseHeaders().set("Content-Type", "application/json");
exchange.sendResponseHeaders(200, body.length);
try (var output = exchange.getResponseBody()) { output.write(body); }
});
server.start();
try {
String baseUrl = "http://127.0.0.1:" + server.getAddress().getPort() + "/tripo";
AsyncProviderClient client = clientFor(baseUrl);
ApiException error = assertThrows(ApiException.class, () -> client.submit("provider-vast", "v3.1-20260211", "3d", "chair"));
assertEquals("UPSTREAM_PROVIDER_ERROR", error.getCode());
assertEquals("上游 3D 任务创建失败", error.getMessage());
} finally {
server.stop(0);
}
}
@ParameterizedTest
@CsvSource({"401,UPSTREAM_AUTH_FAILED", "403,UPSTREAM_AUTH_FAILED", "402,UPSTREAM_CREDIT_INSUFFICIENT", "429,UPSTREAM_RATE_LIMITED", "503,UPSTREAM_PROVIDER_ERROR"})
void httpErrorsExposeOnlySafeCodes(int status, String code) throws Exception {
HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
server.createContext("/", exchange -> {
byte[] body = "private account details".getBytes(StandardCharsets.UTF_8);
exchange.sendResponseHeaders(status, body.length);
try (var output = exchange.getResponseBody()) { output.write(body); }
});
server.start();
try {
AsyncProviderClient client = clientFor("http://127.0.0.1:" + server.getAddress().getPort() + "/tripo");
ApiException submit = assertThrows(ApiException.class, () -> client.submit("provider-vast", "v3.1-20260211", "3d", "chair"));
assertEquals(code, submit.getCode());
assertEquals("上游 3D 任务创建失败(HTTP " + status + ")", submit.getMessage());
ApiException poll = assertThrows(ApiException.class, () -> client.poll("provider-vast", "task-test"));
assertEquals(code, poll.getCode());
assertEquals("上游异步任务查询失败(HTTP " + status + ")", poll.getMessage());
} finally {
server.stop(0);
}
}
private AsyncProviderClient clientFor(String baseUrl) {
JdbcTemplate jdbc = mock(JdbcTemplate.class);
SecretCipher cipher = new SecretCipher("test-encryption");
when(jdbc.queryForMap(any(String.class), eq("provider-vast"))).thenReturn(Map.of(
"code", "vast-tripo", "base_url", baseUrl, "credential_ciphertext", cipher.encrypt("test-key")
));
return new AsyncProviderClient(jdbc, cipher, new ObjectMapper(), mock(Environment.class));
}
}
@@ -0,0 +1,186 @@
package com.moyi.controlplane.task;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.moyi.controlplane.catalog.CatalogService;
import com.moyi.controlplane.common.ApiException;
import com.moyi.controlplane.gateway.GatewayGuard;
import com.moyi.controlplane.usage.UsageAuditService;
import java.math.BigDecimal;
import java.time.Duration;
import java.time.OffsetDateTime;
import java.util.Map;
import java.util.UUID;
import org.flywaydb.core.Flyway;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.test.util.ReflectionTestUtils;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
@EnabledIfEnvironmentVariable(named="MOYI_AUTH_TEST_JDBC_URL", matches="jdbc:postgresql://127\\.0\\.0\\.1:[0-9]+/.+")
class AsyncTaskPostgresTest {
private final ObjectMapper json = new ObjectMapper().findAndRegisterModules();
private final UUID userId = UUID.randomUUID(), keyId = UUID.randomUUID();
private final String taskId = "async-transaction-test";
private String schema;
private DriverManagerDataSource base;
private JdbcTemplate jdbc;
private UsageAuditService usage;
private AsyncProviderClient provider;
private AsyncTaskService tasks;
@BeforeEach void setup() {
String url = System.getenv("MOYI_AUTH_TEST_JDBC_URL");
schema = "async_task_test_" + UUID.randomUUID().toString().replace("-", "");
base = new DriverManagerDataSource(url, "moyi_role_test", "");
var source = new DriverManagerDataSource(url + "?currentSchema=" + schema, "moyi_role_test", "");
Flyway.configure().dataSource(base).schemas(schema).defaultSchema(schema).target("25").load().migrate();
jdbc = new JdbcTemplate(source);
usage = spy(new UsageAuditService(jdbc, json));
provider = mock(AsyncProviderClient.class);
tasks = new AsyncTaskService(jdbc, json, mock(CatalogService.class), 100, 4, 4, 120_000,
mock(GatewayGuard.class), usage, provider, new DataSourceTransactionManager(source));
jdbc.update("INSERT INTO users(id,account,account_kind,password_hash,display_name) VALUES (?,'async-test','username','not-a-password','Test')", userId);
jdbc.update("INSERT INTO platform_api_keys(id,user_id,name,key_prefix,key_hash) VALUES (?,?,'Test','test-prefix',?)", keyId, userId, "b".repeat(64));
jdbc.update("INSERT INTO user_wallets(user_id,balance,reserved_balance) VALUES (?,10,1.44)", userId);
jdbc.update("INSERT INTO model_tasks(id,user_id,api_key_id,model_id,task_type,status,prompt,reserved_amount,provider_task_id) VALUES (?,?,?,'model-mock-3d','3d','running','chair',1.44,'upstream-task')", taskId,userId,keyId);
usage.recordTask(userId,keyId,"model-mock-3d","provider-vast",taskId,"running",Map.of("prompt","chair"),null,null,null);
Flyway.configure().dataSource(base).schemas(schema).defaultSchema(schema).load().migrate();
}
@AfterEach void cleanup() {
if (base != null && schema != null) new JdbcTemplate(base).execute("DROP SCHEMA IF EXISTS " + schema + " CASCADE");
}
private void fail() {
ReflectionTestUtils.invokeMethod(tasks,"failTask",taskId,"PROVIDER_NOT_CONFIGURED","异步渠道尚未配置 API Key");
}
private void poll() {
Map<String,Object> row = jdbc.queryForMap("SELECT id,provider_task_id,provider_deadline_at,created_at FROM model_tasks WHERE id=?", taskId);
row.put("provider_id", "provider-vast");
ReflectionTestUtils.invokeMethod(tasks,"pollProviderTask",row);
}
private BigDecimal wallet(String column) {
return jdbc.queryForObject("SELECT " + column + " FROM user_wallets WHERE user_id=?",BigDecimal.class,userId);
}
private String status() {
return jdbc.queryForObject("SELECT status FROM model_tasks WHERE id=?",String.class,taskId);
}
private BigDecimal cost() {
return jdbc.queryForObject("SELECT cost_amount FROM model_tasks WHERE id=?",BigDecimal.class,taskId);
}
@ParameterizedTest
@ValueSource(strings={"PROVIDER_NOT_CONFIGURED", "UPSTREAM_AUTH_FAILED"})
void submissionFailureReleasesExactlyOnceWithoutDebitingBalance(String code) {
jdbc.update("UPDATE model_tasks SET status='queued',provider_task_id=NULL WHERE id=?",taskId);
when(provider.submit(anyString(),anyString(),anyString(),anyString())).thenThrow(new ApiException(code,"上游任务未接受",503));
tasks.completeMockTasks();
tasks.completeMockTasks();
assertEquals("failed",status());
assertEquals(new BigDecimal("10.00000000"),wallet("balance"));
assertEquals(0,wallet("reserved_balance").signum());
assertEquals(0,cost().signum());
assertEquals(0,jdbc.queryForObject("SELECT cost_amount FROM usage_events WHERE provider_task_id=?",BigDecimal.class,taskId).signum());
assertEquals(0,jdbc.queryForObject("SELECT count(*) FROM wallet_entries WHERE user_id=?",Integer.class,userId));
verify(provider,times(1)).submit(anyString(),anyString(),anyString(),anyString());
}
@Test void refundAndTaskStateRollBackTogetherWhenAuditFails() {
doThrow(new IllegalStateException("simulated database write failure")).when(usage).updateTask(eq(taskId),eq("failed"),isNull(),anyString(),anyString());
assertThrows(IllegalStateException.class,this::fail);
assertEquals("running",status());
assertEquals(new BigDecimal("1.44000000"),wallet("reserved_balance"));
assertNull(cost());
doCallRealMethod().when(usage).updateTask(eq(taskId),eq("failed"),isNull(),anyString(),anyString());
fail(); fail();
assertEquals("failed",status());
assertEquals(0,wallet("reserved_balance").signum());
assertEquals(new BigDecimal("10.00000000"),wallet("balance"));
}
@Test void successfulSettlementRollsBackForRetryAndCannotBeRefundedByLateFailure() {
when(provider.poll("provider-vast","upstream-task")).thenReturn(Map.of("data",Map.of("status","success","progress",100,"output",Map.of("model_url","https://example.test/model.glb"))));
doThrow(new IllegalStateException("simulated database write failure")).when(usage).updateTask(eq(taskId),eq("succeeded"),anyMap(),isNull(),isNull());
assertThrows(IllegalStateException.class,this::poll);
assertEquals("running",status());
assertEquals(new BigDecimal("10.00000000"),wallet("balance"));
assertEquals(new BigDecimal("1.44000000"),wallet("reserved_balance"));
assertNull(cost());
assertEquals(0,jdbc.queryForObject("SELECT count(*) FROM wallet_entries WHERE user_id=?",Integer.class,userId));
doCallRealMethod().when(usage).updateTask(eq(taskId),eq("succeeded"),anyMap(),isNull(),isNull());
poll(); poll(); fail();
assertEquals("succeeded",status());
assertEquals(new BigDecimal("8.56000000"),wallet("balance"));
assertEquals(0,wallet("reserved_balance").signum());
assertEquals(new BigDecimal("1.44000000"),cost());
assertEquals(1,jdbc.queryForObject("SELECT count(*) FROM wallet_entries WHERE user_id=?",Integer.class,userId));
}
@ParameterizedTest
@ValueSource(strings={"UPSTREAM_PROVIDER_ERROR", "UPSTREAM_AUTH_FAILED", "UPSTREAM_RATE_LIMITED"})
void pollingErrorsKeepTheHoldAndClearOnRecovery(String code) {
when(provider.poll("provider-vast","upstream-task"))
.thenThrow(new ApiException(code,"private upstream details",502))
.thenReturn(Map.of("data",Map.of("status","running","progress",75)));
poll();
assertEquals("running",status());
assertEquals(new BigDecimal("1.44000000"),wallet("reserved_balance"));
assertEquals(new BigDecimal("10.00000000"),wallet("balance"));
assertNull(cost());
String error = jdbc.queryForObject("SELECT error::text FROM model_tasks WHERE id=?",String.class,taskId);
assertTrue(error.contains("UPSTREAM_TASK_POLL_RETRY"));
assertFalse(error.contains("private upstream details"));
poll();
assertEquals("running",status());
assertEquals(75,jdbc.queryForObject("SELECT progress FROM model_tasks WHERE id=?",Integer.class,taskId));
assertNull(jdbc.queryForObject("SELECT error::text FROM model_tasks WHERE id=?",String.class,taskId));
assertNull(jdbc.queryForObject("SELECT error_code FROM usage_events WHERE provider_task_id=?",String.class,taskId));
}
@Test void deadlineReleasesHoldWithoutClaimingTheUpstreamFailed() {
jdbc.update("UPDATE model_tasks SET provider_deadline_at=now()-interval '1 second' WHERE id=?",taskId);
poll(); poll();
assertEquals("failed",status());
assertEquals(new BigDecimal("10.00000000"),wallet("balance"));
assertEquals(0,wallet("reserved_balance").signum());
assertEquals(0,cost().signum());
String error = jdbc.queryForObject("SELECT error::text FROM model_tasks WHERE id=?",String.class,taskId);
assertTrue(error.contains("UPSTREAM_TASK_TIMEOUT"));
assertTrue(error.contains("执行结果未知"));
verifyNoInteractions(provider);
}
@Test void confirmedUpstreamFailureReleasesHoldImmediately() {
when(provider.poll("provider-vast","upstream-task")).thenReturn(Map.of("data",Map.of("status","failed")));
poll();
assertEquals("failed",status());
assertEquals(0,wallet("reserved_balance").signum());
assertEquals(new BigDecimal("10.00000000"),wallet("balance"));
assertEquals(0,cost().signum());
}
@Test void migrationBackfillsOldAcceptedTasksAndNewSubmissionsUseConfiguredDeadline() {
OffsetDateTime createdAt = jdbc.queryForObject("SELECT created_at FROM model_tasks WHERE id=?",OffsetDateTime.class,taskId);
OffsetDateTime migratedDeadline = jdbc.queryForObject("SELECT provider_deadline_at FROM model_tasks WHERE id=?",OffsetDateTime.class,taskId);
assertEquals(Duration.ofHours(1),Duration.between(createdAt,migratedDeadline));
jdbc.update("UPDATE model_tasks SET status='queued',provider_task_id=NULL,provider_deadline_at=NULL WHERE id=?",taskId);
when(provider.submit(anyString(),anyString(),anyString(),anyString())).thenReturn(Map.of("data",Map.of("task_id","upstream-task")));
tasks.completeMockTasks();
OffsetDateTime submittedAt = jdbc.queryForObject("SELECT updated_at FROM model_tasks WHERE id=?",OffsetDateTime.class,taskId);
OffsetDateTime deadline = jdbc.queryForObject("SELECT provider_deadline_at FROM model_tasks WHERE id=?",OffsetDateTime.class,taskId);
assertEquals(Duration.ofMinutes(2),Duration.between(submittedAt,deadline));
tasks.completeMockTasks();
verify(provider,times(1)).submit(anyString(),anyString(),anyString(),anyString());
verify(provider,never()).poll(anyString(),anyString());
assertEquals("running",status());
}
}
@@ -0,0 +1,83 @@
package com.moyi.controlplane.usage;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.moyi.controlplane.auth.AuthService;
import com.moyi.controlplane.gateway.GatewayGuard;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.flywaydb.core.Flyway;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.Mockito.*;
@EnabledIfEnvironmentVariable(named="MOYI_AUTH_TEST_JDBC_URL", matches="jdbc:postgresql://127\\.0\\.0\\.1:[0-9]+/.+")
class UsageCapacityPostgresTest {
private JdbcTemplate jdbc;
private JdbcTemplate base;
private String schema;
@BeforeEach void setup() {
String url = System.getenv("MOYI_AUTH_TEST_JDBC_URL");
schema = "capacity_test_" + UUID.randomUUID().toString().replace("-", "");
var source = new DriverManagerDataSource(url, "moyi_role_test", "");
base = new JdbcTemplate(source);
Flyway.configure().dataSource(source).schemas(schema).defaultSchema(schema).load().migrate();
jdbc = new JdbcTemplate(new DriverManagerDataSource(url + "?currentSchema=" + schema, "moyi_role_test", ""));
jdbc.update("UPDATE model_providers SET status='disabled'");
jdbc.update("INSERT INTO model_providers(id,code,name,protocol,status) VALUES ('capacity-provider','capacity-provider','Capacity provider','http','active')");
jdbc.update("INSERT INTO model_definitions(id,public_name,model_type,call_mode) VALUES ('capacity-model','Capacity model','text','sync')");
jdbc.update("INSERT INTO model_catalog(id,provider_id,logical_model_id,public_name,source_label,upstream_model,model_type,call_mode) SELECT 'capacity-route-'||n,'capacity-provider','capacity-model','Capacity model','Group '||n,'test','text','sync' FROM generate_series(1,105) n");
jdbc.update("UPDATE model_catalog SET call_mode='async',model_type='3d' WHERE id='capacity-route-105'");
UUID user = UUID.randomUUID(), key = UUID.randomUUID();
jdbc.update("INSERT INTO users(id,account,account_kind,password_hash,display_name) VALUES (?,'capacity-user','username','not-loginable','Capacity user')", user);
jdbc.update("INSERT INTO platform_api_keys(id,user_id,name,key_prefix,key_hash) VALUES (?,?,'Capacity test','local-test',?)", key, user, "c".repeat(64));
for (String state : List.of("running", "queued", "succeeded")) {
jdbc.update("INSERT INTO model_tasks(id,user_id,api_key_id,model_id,task_type,status,created_at) VALUES (?,?,?,'capacity-route-105','3d',?,now()-interval '1 day')", "task-" + state, user, key, state);
}
}
@AfterEach void cleanup() {
if (base != null && schema != null) base.execute("DROP SCHEMA " + schema + " CASCADE");
}
@Test void liveCapacitySeparatesQueuedWorkAndKeepsEveryGroupAndLongRunningTask() {
GatewayGuard guard = mock(GatewayGuard.class);
when(guard.defaultModelConcurrency()).thenReturn(7);
when(guard.defaultKeyConcurrency()).thenReturn(3);
when(guard.queueWaitMs()).thenReturn(4500L);
when(guard.capacitySnapshot(anyList())).thenAnswer(invocation -> {
List<String> ids = invocation.getArgument(0);
Map<String, GatewayGuard.Capacity> states = new LinkedHashMap<>();
ids.forEach(id -> states.put(id, new GatewayGuard.Capacity(id.equals("capacity-route-1") ? 2 : 0, id.equals("capacity-route-1") ? 3 : 0)));
return new GatewayGuard.CapacitySnapshot(true, states);
});
var service = new UsageAuditService(jdbc, new ObjectMapper(), guard, 77, 2500);
Map<String, Object> summary = service.adminSummary("test", mock(AuthService.class));
List<Map<String, Object>> groups = (List<Map<String, Object>>) summary.get("capacityGroups");
assertEquals(105, groups.size(), "Capacity listing must not truncate at 100 groups");
Map<String, Object> async = groups.stream().filter(group -> group.get("id").equals("capacity-route-105")).findFirst().orElseThrow();
assertEquals(1L, async.get("running"), "A task older than ten minutes must remain in live capacity");
assertEquals(1L, async.get("queued"), "Running tasks must not also count as queued");
assertEquals(7, async.get("limit"), "Null route limits use the real configured model default");
assertEquals(3L, summary.get("running"));
assertEquals(4L, summary.get("waiting"));
Map<?, ?> config = (Map<?, ?>) summary.get("capacityConfig");
assertEquals(4500L, config.get("queueWaitMs"));
assertEquals(3, config.get("defaultKeyConcurrency"));
assertEquals(77, config.get("asyncQueueLimit"));
assertEquals(2500L, config.get("asyncWorkerIntervalMs"));
when(guard.capacitySnapshot(anyList())).thenReturn(new GatewayGuard.CapacitySnapshot(false, Map.of()));
var unavailable = service.adminSummary("test", mock(AuthService.class));
assertNull(unavailable.get("running"), "Unavailable Redis must not be reported as zero load");
assertNull(unavailable.get("waiting"));
}
}