修复小壹生图网关超时后漏查原请求
This commit is contained in:
parent
cf86063860
commit
9512ed2ce4
@ -2008,7 +2008,7 @@ impl Backend {
|
||||
}
|
||||
Some(dispatch)
|
||||
} else { None };
|
||||
let original = read_json(&file)?;
|
||||
let mut original = read_json(&file)?;
|
||||
if original["id"] != candidate {
|
||||
return Err("原候选任务身份已变化".into());
|
||||
}
|
||||
@ -2017,6 +2017,10 @@ impl Backend {
|
||||
if original["slotBinding"] != record["binding"] {
|
||||
return Err("原候选任务与图位记录不一致".into());
|
||||
}
|
||||
if pending_production_image(record, &original)?.is_some() {
|
||||
self.image_job(None, &json!({"doc":source_doc,"taskId":candidate,"recoveryBudgetMs":9000}), true, events, ctx)?;
|
||||
original = read_json(&file)?;
|
||||
}
|
||||
return project_candidate(&source_dir, record, &original).map(|record| (record, false));
|
||||
}
|
||||
let record = reconcile_candidate(&source_dir, &original)?;
|
||||
@ -4163,7 +4167,15 @@ impl Backend {
|
||||
journal["notDispatched"] = json!(true);
|
||||
journal["message"] = json!(error);
|
||||
}
|
||||
Ok((candidate_receipt(&reconcile_candidate(dir, &journal)?),call.err()))
|
||||
let record = reconcile_candidate(dir, &journal)?;
|
||||
if !recover_inline && matches!(journal["phase"].as_str(), Some("dispatched" | "running" | "unknown"))
|
||||
&& let Ok(snapshot) = self.canvas_workspace_result(doc, dir)
|
||||
{
|
||||
// Publish the archived dispatch so the host queue can probe it before the agent yields.
|
||||
let _ = publish_prepared(events, snapshot);
|
||||
ctx.request_repaint_of(egui::ViewportId::ROOT);
|
||||
}
|
||||
Ok((candidate_receipt(&record),call.err()))
|
||||
}
|
||||
|
||||
pub(super) fn recover_deferred_candidate(&mut self, input: &Value, cancellation: &std::sync::atomic::AtomicBool) -> R<Value> {
|
||||
@ -4839,7 +4851,9 @@ mod tests {
|
||||
write_json(&file, &task).unwrap();
|
||||
assert_eq!(pending_production_images(&fixture.dir, &view).unwrap(), json!([]));
|
||||
task["phase"] = json!("unknown");
|
||||
task["message"] = json!("服务没有返回有效结果(HTTP 524)");
|
||||
write_json(&file, &task).unwrap();
|
||||
assert!(!file.with_file_name("recovery-http.json").exists());
|
||||
let originals = pending_production_images(&fixture.dir, &view).unwrap();
|
||||
assert_eq!(originals[0]["candidateId"], id);
|
||||
assert_eq!(originals[0]["outputId"], "main_images");
|
||||
@ -6941,9 +6955,10 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn production_original_reads_the_host_expiry_without_recovering_or_rewriting() {
|
||||
fn production_original_recovers_unconfirmed_tasks_and_reads_host_expiry() {
|
||||
let fixture = fixture();
|
||||
let mut backend = Backend::new(fixture.dir.join("profile")).unwrap();
|
||||
backend.auth = Value::Null;
|
||||
let doc = 1;
|
||||
backend.bind_new(doc);
|
||||
let dir = backend.project_dir(doc).unwrap();
|
||||
@ -6964,11 +6979,23 @@ mod tests {
|
||||
"source":{"candidateId":id,"recover":true}});
|
||||
let (events, receiver) = mpsc::channel();
|
||||
let ctx = egui::Context::default();
|
||||
super::super::images::settle_unresolved(&mut task, 100);
|
||||
write_json(&file, &task).unwrap();
|
||||
assert_eq!(backend.detail_archive(doc, &target, &events, &ctx).unwrap()["status"], "unknown");
|
||||
assert_eq!(read_json(&file).unwrap(), task);
|
||||
let mut legacy = task.clone();
|
||||
legacy["requestId"] = Value::Null;
|
||||
legacy["upstreamTaskId"] = Value::Null;
|
||||
legacy["message"] = json!("服务没有返回有效结果(HTTP 524)");
|
||||
write_json(&file, &legacy).unwrap();
|
||||
let (receipt, _) = backend.settle_image_candidate(&dir, doc, id, &record["binding"],
|
||||
Ok(json!({"phase":"unknown"})), &events, &ctx, false).unwrap();
|
||||
assert_eq!(receipt["status"], "unknown");
|
||||
let Event::Json(name, Ok(snapshot)) = receiver.try_recv().unwrap() else { panic!("pending image snapshot") };
|
||||
assert_eq!(name, "slot-progress");
|
||||
assert_eq!(snapshot["workflow"]["pendingImageRequests"][0]["candidateId"], id);
|
||||
let archived_record = read_candidate(&dir, id).unwrap();
|
||||
assert!(!file.with_file_name("recovery-http.json").exists());
|
||||
assert_eq!(backend.detail_archive(doc, &target, &events, &ctx).unwrap_err(), "请先在素材库登录");
|
||||
assert_eq!(read_json(&file).unwrap(), legacy);
|
||||
|
||||
super::super::images::settle_unresolved(&mut task, 100);
|
||||
super::super::images::settle_unresolved(&mut task, 280);
|
||||
write_json(&file, &task).unwrap();
|
||||
let receipt = backend.detail_archive(doc, &target, &events, &ctx).unwrap();
|
||||
@ -6976,7 +7003,7 @@ mod tests {
|
||||
assert_eq!(receipt["status"], "expired");
|
||||
assert_eq!(receipt["resultExpiry"], task["resultExpiry"]);
|
||||
assert!(pending_summary(&dir).unwrap().is_empty());
|
||||
assert_eq!(read_candidate(&dir, id).unwrap(), record);
|
||||
assert_eq!(read_candidate(&dir, id).unwrap(), archived_record);
|
||||
assert_eq!(read_json(&file).unwrap(), task);
|
||||
assert!(receiver.try_recv().is_err());
|
||||
}
|
||||
|
||||
@ -713,7 +713,7 @@ fn query_kind(path: &str, idempotency: Option<&str>) -> &'static str {
|
||||
/// structured lookup or task status only.
|
||||
fn observed_wait(kind: &str, code: u16, body: &Value) -> Option<RecoveryWait> {
|
||||
match (kind, code) {
|
||||
(_, 502..=504) => Some(RecoveryWait::Network),
|
||||
(_, 502..=504 | 520..=529) => Some(RecoveryWait::Network),
|
||||
("lookup", 404) if body["error"]["code"] == "request_not_found" => Some(RecoveryWait::RequestNotFound),
|
||||
("lookup", 200) => match body["status"].as_str() {
|
||||
Some("processing") => Some(RecoveryWait::Processing),
|
||||
@ -1293,6 +1293,7 @@ impl Backend {
|
||||
t["phase"] = json!("failed");
|
||||
return Err(error);
|
||||
}
|
||||
wait.set(observed_wait("dispatch", response.status().as_u16(), &Value::Null));
|
||||
let (code, v) = body(response, RESPONSE_BYTES)?;
|
||||
if !(200..300).contains(&code) {
|
||||
return Err(format!("图片服务暂时未能完成请求(HTTP {code}),原请求待核查;不会重发"));
|
||||
@ -2579,6 +2580,11 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn received_statuses_map_to_structured_waits_only() {
|
||||
for code in [502, 503, 504, 520, 521, 522, 523, 524, 525, 526, 527, 528, 529] {
|
||||
for kind in ["dispatch", "lookup", "result", "task-status", "task-result", "asset"] {
|
||||
assert_eq!(observed_wait(kind, code, &Value::Null), Some(RecoveryWait::Network));
|
||||
}
|
||||
}
|
||||
for (kind, code, body, expected) in [
|
||||
("lookup", 200, json!({"status":"processing"}), Some(RecoveryWait::Processing)),
|
||||
("lookup", 200, json!({"status":"unresolved"}), Some(RecoveryWait::Unresolved)),
|
||||
@ -2603,6 +2609,84 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gateway_timeout_without_a_request_id_recovers_using_the_original_key() {
|
||||
let request = "cc6a9ed9-863b-419f-bd1a-b126833227fb";
|
||||
let result_path = format!("/api/external/requests/{request}/result");
|
||||
let id = Uuid::new_v4().to_string();
|
||||
for (code, status, expected_wait) in [
|
||||
(200, "succeeded", None),
|
||||
(200, "processing", Some(RecoveryWait::Processing)),
|
||||
(404, "request_not_found", Some(RecoveryWait::RequestNotFound)),
|
||||
(524, "", Some(RecoveryWait::Network)),
|
||||
] {
|
||||
let mut task = json!({"id":id,"phase":"unknown","requestId":null,"upstreamTaskId":null,
|
||||
"message":"服务没有返回有效结果(HTTP 524)",
|
||||
"recoveryWait":observed_wait("dispatch", 524, &Value::Null).map(RecoveryWait::name)});
|
||||
assert_eq!(task["recoveryWait"], "network");
|
||||
let wait = std::cell::Cell::new(None);
|
||||
let mut calls = 0;
|
||||
let outcome = checked_original_response(&mut task, observing(&wait, |path, key| {
|
||||
calls += 1;
|
||||
if calls == 2 {
|
||||
assert_eq!(status, "succeeded");
|
||||
assert_eq!(path, result_path);
|
||||
assert_eq!(key, None);
|
||||
return Ok((200, json!({"data":[{"b64_json":"original-image"}]})));
|
||||
}
|
||||
assert_eq!(path, "/api/external/requests/by-idempotency-key");
|
||||
assert_eq!(key, Some(id.as_str()));
|
||||
let body = match code {
|
||||
200 => json!({"status":status,"request_id":request,"result_path":result_path,
|
||||
"result_available":status == "succeeded","result_expires_at":null}),
|
||||
404 => json!({"error":{"code":"request_not_found"}}),
|
||||
_ => Value::Null,
|
||||
};
|
||||
Ok((code, body))
|
||||
}), |_| Ok(()));
|
||||
assert_eq!(wait.get(), expected_wait);
|
||||
assert_eq!(calls, if status == "succeeded" { 2 } else { 1 });
|
||||
assert_eq!(task["id"], id);
|
||||
match status {
|
||||
"succeeded" => assert!(matches!(outcome, Ok(OriginalResponse::Completed(_)))),
|
||||
"processing" => {
|
||||
assert!(outcome.is_err());
|
||||
assert_eq!(task["requestId"], request);
|
||||
assert_eq!(task["phase"], "unknown");
|
||||
}
|
||||
"request_not_found" => {
|
||||
assert!(outcome.is_err());
|
||||
let since = task["requestNotFoundSince"].as_u64().unwrap();
|
||||
settle_request_not_found(&mut task, since + IMAGE_REQUEST_TIMEOUT.as_secs());
|
||||
assert_eq!(task["phase"], "expired");
|
||||
assert_eq!(task["resultExpiry"]["source"], "request_not_found");
|
||||
}
|
||||
_ => assert!(outcome.is_err()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn temporary_gateway_errors_do_not_extend_the_recovery_window() {
|
||||
for code in [502, 503, 504, 524] {
|
||||
let elapsed = std::cell::Cell::new(Duration::ZERO);
|
||||
let mut attempts = Vec::new();
|
||||
let receipt = retry_original_queries(
|
||||
|remaining| {
|
||||
attempts.push(remaining);
|
||||
let wait = observed_wait("lookup", code, &Value::Null).map(RecoveryWait::name);
|
||||
Ok(json!({"phase":"unknown","recoveryWait":wait}))
|
||||
},
|
||||
|delay| elapsed.set(elapsed.get() + delay),
|
||||
|| elapsed.get(),
|
||||
).unwrap();
|
||||
assert_eq!(receipt["recoveryWait"], "network");
|
||||
assert_eq!(elapsed.get(), IMAGE_REQUEST_TIMEOUT);
|
||||
assert_eq!(attempts[0], IMAGE_REQUEST_TIMEOUT);
|
||||
assert!(attempts.len() > 1 && attempts.windows(2).all(|pair| pair[1] < pair[0]));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unresolved_recovery_preserves_the_original_binding_until_a_terminal_lookup() {
|
||||
let id = "42e84e04-4738-48c0-b3c0-31ee097b7a35";
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user