feat: make async task creation durably idempotent

This commit is contained in:
xpp committed 2026-10-21 18:41:21 +08:00
1 parent 5533a483d9
commit 76265845dc
8 files changed
+232 -9

No files matched your search

@@ -113,7 +113,13 @@ public class AsyncTaskService {
@Transactional
public Map<String, Object> create(String authorization, String taskType, Map<String, Object> input, ApiRequestMetadata metadata) {
return createForKey(requireKey(authorization), taskType, input, metadata);
return create(authorization, taskType, input, null, metadata);
}
@Transactional
public Map<String, Object> create(String authorization, String taskType, Map<String, Object> input,
String idempotencyKey, ApiRequestMetadata metadata) {
return createForKey(requireKey(authorization), taskType, input, idempotencyKey, metadata);
}
@Transactional
@@ -123,8 +129,36 @@ public class AsyncTaskService {
@Transactional
public Map<String, Object> createForKey(Map<String, Object> key, String taskType, Map<String, Object> input, ApiRequestMetadata metadata) {
return createForKey(key, taskType, input, null, metadata);
}
@Transactional
public Map<String, Object> createForKey(Map<String, Object> key, String taskType, Map<String, Object> input,
String idempotencyKey, ApiRequestMetadata metadata) {
// Explicit transaction also protects callers that do not go through the Spring proxy.
return inTransaction(() -> createInTransaction(key, taskType, input, idempotencyKey, metadata));
}
private Map<String,Object> createInTransaction(Map<String,Object> key, String taskType, Map<String,Object> input,
String idempotencyKey, ApiRequestMetadata metadata) {
UUID apiKeyId = UUID.fromString(String.valueOf(key.get("id")));
gatewayGuard.checkRate(apiKeyId, false);
if (input == null) throw new ApiException("INVALID_REQUEST", "请提供任务请求");
TaskCreationIdentity identity = TaskCreationIdentity.of(objectMapper, idempotencyKey, taskType, input);
UUID userId = catalogService.keyOwnerId(apiKeyId);
if (identity.keyHash() != null) {
// Serialize the same user/key before checking capacity, budget or wallet balances.
jdbc.queryForList("SELECT pg_advisory_xact_lock(hashtextextended(?,0))",
"moyi:task-idempotency:" + userId + ":" + identity.keyHash());
var existing = jdbc.queryForList("SELECT id,request_hash FROM model_tasks WHERE user_id=? AND idempotency_hash=?", userId, identity.keyHash());
if (!existing.isEmpty()) {
if (!identity.requestHash().equals(existing.getFirst().get("request_hash"))) {
throw new ApiException("IDEMPOTENCY_CONFLICT", "此 Idempotency-Key 已用于不同的任务请求", 409);
}
// Authenticated same-user reads remain valid even if the original route was retired.
return taskView(String.valueOf(existing.getFirst().get("id")));
}
}
String requestedModelId = String.valueOf(input.getOrDefault("model", ""));
Map<String, Object> model = GatewayModelIds.resolve(catalogService, key, requestedModelId);
if (model == null) throw new ApiException("MODEL_NOT_FOUND", "模型不存在或已下线", 404);
@@ -137,7 +171,6 @@ public class AsyncTaskService {
if (prompt.isBlank() && (mediaInput == null || String.valueOf(mediaInput).isBlank())) {
throw new ApiException("PROMPT_REQUIRED", "生成提示词或媒体输入不能为空");
}
UUID userId = catalogService.keyOwnerId(apiKeyId);
requestRisk.enforce(new ApiRequestContext(userId, apiKeyId, ApiRequestProtocol.ASYNC_TASK,
requestedModelId, modelId, false, estimateInputUnits(input), 0, metadata, System.nanoTime()));
// Serialize the queue-capacity check with task insertion for this
@@ -161,8 +194,8 @@ public class AsyncTaskService {
OffsetDateTime now = now();
try {
jdbc.update(
"INSERT INTO model_tasks (id, user_id, api_key_id, model_id, task_type, provider_task_id, status, progress, prompt, input, reserved_amount, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, 'queued', 0, ?, CAST(? AS jsonb), ?, ?, ?)",
id, userId, apiKeyId, modelId, taskType, providerTaskId, prompt, writeJson(input == null ? Map.of() : input), reservedAmount, now, now
"INSERT INTO model_tasks (id, user_id, api_key_id, model_id, task_type, provider_task_id, status, progress, prompt, input, reserved_amount, created_at, updated_at, idempotency_hash, request_hash) VALUES (?, ?, ?, ?, ?, ?, 'queued', 0, ?, CAST(? AS jsonb), ?, ?, ?, ?, ?)",
id, userId, apiKeyId, modelId, taskType, providerTaskId, prompt, writeJson(input), reservedAmount, now, now, identity.keyHash(), identity.requestHash()
);
} catch (RuntimeException exception) {
releaseWallet(userId, reservedAmount);
@@ -27,11 +27,12 @@ public class TaskController {
public ResponseEntity<Map<String, Object>> create3d(
@org.springframework.web.bind.annotation.PathVariable(required = false) String type,
@RequestHeader(value = "Authorization", required = false) String authorization,
@RequestHeader(value = "Idempotency-Key", required = false) String idempotencyKey,
@RequestBody Map<String, Object> input,
HttpServletRequest request
) {
String taskType = type == null ? String.valueOf(input == null ? "3d" : input.getOrDefault("taskType", "3d")) : type;
return ResponseEntity.status(HttpStatus.ACCEPTED).body(Map.of("task", tasks.create(authorization, taskType, input,
return ResponseEntity.status(HttpStatus.ACCEPTED).body(Map.of("task", tasks.create(authorization, taskType, input, idempotencyKey,
ApiRequestMetadata.fromHttp(request.getRemoteAddr()))));
}
@@ -39,10 +40,11 @@ public class TaskController {
@PostMapping("/video/tasks")
public ResponseEntity<Map<String, Object>> createVideo(
@RequestHeader(value = "Authorization", required = false) String authorization,
@RequestHeader(value = "Idempotency-Key", required = false) String idempotencyKey,
@RequestBody Map<String, Object> input,
HttpServletRequest request
) {
return ResponseEntity.status(HttpStatus.ACCEPTED).body(Map.of("task", tasks.create(authorization, "video", input,
return ResponseEntity.status(HttpStatus.ACCEPTED).body(Map.of("task", tasks.create(authorization, "video", input, idempotencyKey,
ApiRequestMetadata.fromHttp(request.getRemoteAddr()))));
}
@@ -0,0 +1,58 @@
package com.moyi.controlplane.task;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.DecimalNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.moyi.controlplane.common.ApiException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HexFormat;
import java.util.Map;
import java.util.TreeSet;
/** Stable request identity only; never stores or exposes the caller's raw idempotency key. */
record TaskCreationIdentity(String keyHash, String requestHash) {
static TaskCreationIdentity of(ObjectMapper mapper, String key, String type, Map<String,Object> input) {
if (key == null) return new TaskCreationIdentity(null, null);
if (!key.matches("[!-~]{1,128}")) {
throw new ApiException("INVALID_IDEMPOTENCY_KEY", "Idempotency-Key 须为 1–128 个可见 ASCII 字符");
}
try {
ObjectNode request = mapper.createObjectNode();
request.put("taskType", type);
request.set("input", canonical(mapper, mapper.valueToTree(input)));
return new TaskCreationIdentity(hash(key), hash(mapper.writeValueAsString(request)));
} catch (JsonProcessingException | IllegalArgumentException exception) {
throw new ApiException("INVALID_REQUEST", "任务请求无法编码");
}
}
private static JsonNode canonical(ObjectMapper mapper, JsonNode node) {
if (node.isObject()) {
ObjectNode result = mapper.createObjectNode();
var fields = new TreeSet<String>();
node.fieldNames().forEachRemaining(fields::add);
for (String field : fields) result.set(field, canonical(mapper, node.get(field)));
return result;
}
if (node.isArray()) {
ArrayNode result = mapper.createArrayNode();
node.forEach(value -> result.add(canonical(mapper, value)));
return result;
}
if (node.isNumber()) return DecimalNode.valueOf(node.decimalValue().stripTrailingZeros());
return node;
}
private static String hash(String value) {
try {
return HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(value.getBytes(StandardCharsets.UTF_8)));
} catch (NoSuchAlgorithmException exception) {
throw new IllegalStateException("SHA-256 is required by the Java runtime", exception);
}
}
}