feat: say why a restore failed, and add the user_schemas reset mode
- the agent reports the client's primary ERROR/FATAL line with a failed restore step; the hub keeps it as failure_reason (administrators only), logs it and audits restore.failed. psql runs with VERBOSITY=terse and DETAIL/CONTEXT/statement text is dropped so row data never travels - reset mode user_schemas empties every non-system schema in the restore transaction, so multi-schema databases restore without DROP DATABASE; proven against a real PostgreSQL server Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -359,6 +359,33 @@ impl Hub {
|
||||
.await?
|
||||
}
|
||||
|
||||
/// Like `post` for a restore report, with the reason for a failure in a header.
|
||||
pub(super) async fn post_with_reason(
|
||||
self: &Arc<Self>,
|
||||
path: String,
|
||||
body: serde_json::Value,
|
||||
restore: Uuid,
|
||||
reason: Option<String>,
|
||||
) -> Result<String> {
|
||||
let hub = Arc::clone(self);
|
||||
tokio::task::spawn_blocking(move || {
|
||||
let restore = restore.to_string();
|
||||
let mut headers = vec![
|
||||
("x-tessera-upload-token", hub.token.as_str()),
|
||||
("x-tessera-restore-token", restore.as_str()),
|
||||
];
|
||||
if let Some(reason) = reason.as_deref() {
|
||||
headers.push((tessera_core::restore::FAILURE_REASON_HEADER, reason));
|
||||
}
|
||||
http::post_json(
|
||||
&format!("{}/api/v1/wormhole/{path}", hub.url),
|
||||
&body,
|
||||
&headers,
|
||||
)
|
||||
})
|
||||
.await?
|
||||
}
|
||||
|
||||
async fn report(
|
||||
self: &Arc<Self>,
|
||||
lease: &CaptureLease,
|
||||
|
||||
@@ -117,8 +117,8 @@ async fn run(
|
||||
.await;
|
||||
let script = match downloaded {
|
||||
Ok(Ok(script)) => script,
|
||||
_ => {
|
||||
report(hub, lease, RestoreProgress::InvalidPayload).await?;
|
||||
other => {
|
||||
report_failed(hub, lease, RestoreProgress::InvalidPayload, why(other)).await?;
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
@@ -144,8 +144,8 @@ async fn run(
|
||||
.await;
|
||||
let identity = match verified {
|
||||
Ok(Ok(identity)) => identity,
|
||||
_ => {
|
||||
report(hub, lease, RestoreProgress::WrongTarget).await?;
|
||||
other => {
|
||||
report_failed(hub, lease, RestoreProgress::WrongTarget, why(other)).await?;
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
@@ -171,7 +171,7 @@ async fn run(
|
||||
})
|
||||
.await;
|
||||
if !matches!(safety, Ok(Ok(()))) {
|
||||
report(hub, lease, RestoreProgress::SafetyFailed).await?;
|
||||
report_failed(hub, lease, RestoreProgress::SafetyFailed, why(safety)).await?;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
@@ -182,20 +182,35 @@ async fn run(
|
||||
Some(lease.token),
|
||||
)
|
||||
.await?;
|
||||
let applied = matches!(
|
||||
tokio::time::timeout(timeout, target.apply(script)).await,
|
||||
Ok(Ok(()))
|
||||
);
|
||||
report(
|
||||
hub,
|
||||
lease,
|
||||
if applied {
|
||||
RestoreProgress::Applied
|
||||
} else {
|
||||
RestoreProgress::ApplyFailed
|
||||
},
|
||||
match tokio::time::timeout(timeout, target.apply(script)).await {
|
||||
Ok(Ok(())) => report(hub, lease, RestoreProgress::Applied).await,
|
||||
other => report_failed(hub, lease, RestoreProgress::ApplyFailed, why(other)).await,
|
||||
}
|
||||
}
|
||||
|
||||
/// Why a step did not succeed, in words: the error chain, or that it ran out of time.
|
||||
fn why<T>(result: Result<Result<T>, tokio::time::error::Elapsed>) -> String {
|
||||
match result {
|
||||
Ok(Ok(_)) => "unknown".into(),
|
||||
Ok(Err(error)) => format!("{error:#}"),
|
||||
Err(_) => "timed out".into(),
|
||||
}
|
||||
}
|
||||
|
||||
async fn report_failed(
|
||||
hub: &Arc<Hub>,
|
||||
lease: &RestoreLease,
|
||||
progress: RestoreProgress,
|
||||
reason: String,
|
||||
) -> Result<()> {
|
||||
hub.post_with_reason(
|
||||
format!("restore/{}/report", lease.job.id),
|
||||
serde_json::to_value(progress)?,
|
||||
lease.token,
|
||||
tessera_core::restore::clean_failure_reason(&reason),
|
||||
)
|
||||
.await
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn report(hub: &Arc<Hub>, lease: &RestoreLease, progress: RestoreProgress) -> Result<()> {
|
||||
|
||||
Reference in New Issue
Block a user