Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 43 additions & 23 deletions crates/engine/src/backup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use extenddb_core::types::{
};
use extenddb_storage::backup_catalog::{
BackupKey, BackupRow, BackupStatus, DeleteStart, TimeBounds, is_backup_not_found,
restore_target_in_use_message,
};
use extenddb_storage::error::StorageError;
use extenddb_storage::{
Expand Down Expand Up @@ -1845,7 +1846,7 @@ where

// 5. The target name, after the backup resolved and before the overrides
// are checked against it.
check_restore_target_name(storage, account_id, target_table_name).await?;
check_restore_target_name(storage, account_id, target_table_name, &backup_arn).await?;

// 6. Refusals every backend gets, whatever it stores.
source.ensure_restorable().map_err(storage_err_to_dynamo)?;
Expand Down Expand Up @@ -1883,7 +1884,7 @@ where
.await
.map_err(storage_err_to_dynamo)?
else {
return Err(restore_storage_err_to_dynamo(e));
return Err(restore_storage_err_to_dynamo(e, &backup_arn));
};
return restore_from_row(
storage_arc,
Expand All @@ -1899,7 +1900,7 @@ where
.await;
}
// 10.
Err(e) => return Err(restore_storage_err_to_dynamo(e)),
Err(e) => return Err(restore_storage_err_to_dynamo(e, &backup_arn)),
};

let response = restore_response(desc, &RestoreSource::from(&source), on_demand)?;
Expand Down Expand Up @@ -1937,7 +1938,13 @@ where
// backup into an existing table is TableAlreadyExistsException, a
// backup into its own CREATING target TableInUseException, not
// BackupInUseException) and before the manifest is read.
check_restore_target_name(&**storage, account_id, target_table_name).await?;
check_restore_target_name(
&**storage,
account_id,
target_table_name,
&arn.to_arn_string(),
)
.await?;
let store = check_catalog_row_for_restore(&**storage, runtime, arn, row).await?;
restore::request(
storage,
Expand Down Expand Up @@ -1998,19 +2005,19 @@ async fn check_catalog_row_for_restore<'r, S: BackupCatalog + ?Sized>(
runtime.require_healthy_store()
}

/// Step 5 of a restore, the target name under the account, read through
/// `describe_table` after the backup resolved and before the permit: a
/// CREATING table with a
/// `RestoreSummary` is a restore target, `TableInUseException` naming its
/// source backup (the text the backend's own registration answers with,
/// [`StorageError::TableBeingRestored`]); any other table of that name is
/// Step 5 of a restore of `backup_arn`, the target name under the account,
/// read through `describe_table` after the backup resolved and before the
/// permit: a CREATING table with a `RestoreSummary` is a restore target,
/// answered as the backend's registration answers it
/// ([`restore_storage_err_to_dynamo`]); any other table of that name is
/// `TableAlreadyExistsException`; no table passes. Unfenced: the backend
/// checks the name again under its registration, so two restores into one
/// free name still have one winner.
async fn check_restore_target_name<S: TableEngine + ?Sized>(
storage: &S,
account_id: &str,
target_table_name: &str,
backup_arn: &str,
) -> Result<(), DynamoDbError> {
let probe = storage
.describe_table(
Expand All @@ -2027,13 +2034,16 @@ async fn check_restore_target_name<S: TableEngine + ?Sized>(
.as_ref()
.filter(|_| desc.table_status == TableStatus::Creating)
.and_then(|s| s.source_backup_arn.clone());
Err(restore_storage_err_to_dynamo(match source {
Some(backup_arn) => StorageError::TableBeingRestored {
table_name: target_table_name.to_owned(),
backup_arn,
Err(restore_storage_err_to_dynamo(
match source {
Some(backup_arn) => StorageError::TableBeingRestored {
table_name: target_table_name.to_owned(),
backup_arn,
},
None => StorageError::TableAlreadyExists(target_table_name.to_owned()),
},
None => StorageError::TableAlreadyExists(target_table_name.to_owned()),
}))
backup_arn,
))
}
Err(StorageError::TableNotFound(_)) => Ok(()),
Err(e) => Err(storage_err_to_dynamo(e)),
Expand Down Expand Up @@ -2113,20 +2123,30 @@ fn restore_response(
Ok(out)
}

/// [`storage_err_to_dynamo`] for RestoreTableFromBackup, where a taken target
/// name is `TableAlreadyExistsException` (the operation's own error in the
/// service model, and what the service returns), and a name taken by a
/// `CREATING` restore target is `TableInUseException` with the service's text
/// (`Table: <name> is already being restored from backup: <arn>`, measured on
/// DynamoDB), which the storage error's `Display` carries.
/// [`storage_err_to_dynamo`] for a RestoreTableFromBackup of `backup_arn`,
/// where a taken target name is `TableAlreadyExistsException` (the
/// operation's own error in the service model, and what the service
/// returns), and a name taken by a `CREATING` restore target is
/// `TableInUseException` with the service's text (both measured on
/// DynamoDB): `Table: <name> is already being restored from backup: <arn>`,
/// the storage error's `Display`, when that target is restored from
/// `backup_arn`, else [`restore_target_in_use_message`]. `backup_arn` is the
/// ARN the request resolved to, the spelling a target's `RestoreSummary`
/// holds, so the two compare as strings.
/// A backup with a restore from it still `CREATING` is `BackupInUseException`
/// (the generic mapping; measured on DynamoDB). Only this operation maps the
/// first two so; CreateTable keeps `ResourceInUseException`.
fn restore_storage_err_to_dynamo(e: StorageError) -> DynamoDbError {
fn restore_storage_err_to_dynamo(e: StorageError, backup_arn: &str) -> DynamoDbError {
match e {
StorageError::TableAlreadyExists(name) => DynamoDbError::TableAlreadyExistsException(
error_message(ErrorMessageKey::TableAlreadyExists, &[&name]),
),
StorageError::TableBeingRestored {
table_name,
backup_arn: restoring,
} if restoring != backup_arn => {
DynamoDbError::TableInUseException(restore_target_in_use_message(&table_name))
}
e @ StorageError::TableBeingRestored { .. } => {
DynamoDbError::TableInUseException(e.to_string())
}
Expand Down
3 changes: 2 additions & 1 deletion crates/engine/src/backup/restore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ where
table_name: target_table_name.to_owned(),
backup_arn: in_flight.backup_arn,
},
&arn_text,
));
}
tracing::info!(
Expand Down Expand Up @@ -485,7 +486,7 @@ pub(crate) async fn start<S>(
Ok(registered) => registered,
Err(e) => {
drop(permit);
let error = restore_storage_err_to_dynamo(e);
let error = restore_storage_err_to_dynamo(e, &arn_text);
if let Some(registration) = registration {
registration.finish(Err(error.clone()));
}
Expand Down
131 changes: 87 additions & 44 deletions crates/engine/src/backup/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6205,31 +6205,50 @@ async fn a_taken_name_is_table_already_exists_on_restore_only() {

/// Measured on DynamoDB: a target name taken by a
/// `CREATING` restore is `TableInUseException` `Table: <name> is already
/// being restored from backup: <arn>`; a backup with a restore from it still
/// being restored from backup: <arn>` for that restore's backup and
/// `Attempt to change a resource which is still in use: Table: <name> is
/// being used.` for another; a backup with a restore from it still
/// `CREATING` is `BackupInUseException`. The backends raise the storage
/// errors; this is their mapping.
/// errors; this is their mapping, for the backup the request resolved to.
#[tokio::test]
async fn a_name_being_restored_is_table_in_use_and_a_backup_being_restored_is_backup_in_use() {
let fake = Fake::with_source(source(Some(definition("PROVISIONED"))));
*fake.restore_error.lock().unwrap() = Some(StorageError::TableBeingRestored {
table_name: "MusicRestored".to_owned(),
backup_arn: arn(ACCOUNT),
});
let err = restore(&fake, restore_body(json!({}))).await.unwrap_err();
match &err {
DynamoDbError::TableInUseException(m) => assert_eq!(
m,
&format!(
"Table: MusicRestored is already being restored from backup: {}",
arn(ACCOUNT)
)
),
other => panic!("expected TableInUseException, got {other:?}"),
let ours = arn(ACCOUNT);
let earlier = ours.replace(REGION, "eu-west-1");
let b2 = arn_n(2);
let same =
|arn: &str| format!("Table: MusicRestored is already being restored from backup: {arn}");
let another = restore_target_in_use_message("MusicRestored");
// The backup's stored ARN, the request's `BackupArn`, the backup of the
// restore in progress, the text: the same backup as issued, trimmed,
// in another region and stored under an earlier `server.region`, then
// another backup.
let rows = [
(&ours, ours.clone(), &ours, same(&ours)),
(&ours, format!(" {ours}\n"), &ours, same(&ours)),
(&ours, earlier.clone(), &ours, same(&ours)),
(&earlier, earlier.clone(), &earlier, same(&earlier)),
(&ours, ours.clone(), &b2, another),
];
for (stored, requested, restoring, text) in &rows {
let mut src = source(Some(definition("PROVISIONED")));
src.backup_arn.clone_from(stored);
let fake = Fake::with_source(src);
*fake.restore_error.lock().unwrap() = Some(StorageError::TableBeingRestored {
table_name: "MusicRestored".to_owned(),
backup_arn: restoring.to_string(),
});
let err = restore(&fake, restore_body(json!({ "BackupArn": requested })))
.await
.unwrap_err();
assert!(
matches!(&err, DynamoDbError::TableInUseException(m) if m == text),
"{requested:?} into a target of {restoring}: {err:?}"
);
assert_eq!(
err.full_error_type(),
"com.amazonaws.dynamodb.v20120810#TableInUseException"
);
}
assert_eq!(
err.full_error_type(),
"com.amazonaws.dynamodb.v20120810#TableInUseException"
);

let fake = Fake::with_source(source(Some(definition("PROVISIONED"))));
*fake.restore_error.lock().unwrap() = Some(StorageError::BackupInUse(format!(
Expand Down Expand Up @@ -8254,7 +8273,8 @@ async fn restore_takes_the_permit_last() {
/// overrides and the permit, the order the service uses: with every permit
/// held and the backup restorable, a restore into a name a table has is
/// TableAlreadyExists, a retry into a CREATING restore target is TableInUse
/// naming the target's source backup in the backend's own text, a CREATING
/// naming the target's source backup in the backend's own text (the
/// service's name-in-use text for a target of another backup), a CREATING
/// table with no RestoreSummary (an ordinary create) and an ACTIVE former
/// target (its summary no longer in progress) are TableAlreadyExists; the
/// backup was read once and the backend not asked. With a missing backup,
Expand Down Expand Up @@ -8300,42 +8320,48 @@ async fn restore_checks_the_target_name_after_the_backup_and_before_the_permit()
(fake, err)
}
let restorable = || Fake::with_source(source(Some(definition("PROVISIONED"))));
// Each target with its TableInUse text, `None` for TableAlreadyExists.
let targets = [
table(TableStatus::Active, None),
table(TableStatus::Creating, None),
table(TableStatus::Creating, Some(&arn(ACCOUNT))),
table(TableStatus::Active, Some(&arn(ACCOUNT))),
(table(TableStatus::Active, None), None),
(table(TableStatus::Creating, None), None),
(
table(TableStatus::Creating, Some(&arn(ACCOUNT))),
Some(format!(
"Table: MusicRestored is already being restored from backup: {}",
arn(ACCOUNT)
)),
),
(table(TableStatus::Active, Some(&arn(ACCOUNT))), None),
(
table(TableStatus::Creating, Some(&arn_n(2))),
Some(restore_target_in_use_message("MusicRestored")),
),
];

// The backup restorable: the name's refusal, after one read of the backup.
let in_use_text = format!(
"Table: MusicRestored is already being restored from backup: {}",
arn(ACCOUNT)
);
for (i, target) in targets.iter().enumerate() {
for (i, (target, in_use)) in targets.iter().enumerate() {
let (fake, err) = check(restorable(), target.clone()).await;
assert_eq!(fake.source_reads(), 1, "{i}: the backup was read first");
assert_eq!(
fake.table_reads.lock().unwrap().as_slice(),
["MusicRestored"],
"{i}: the name was read once, under the request's spelling"
);
if i == 2 {
assert!(
matches!(err, DynamoDbError::TableInUseException(ref m) if *m == in_use_text),
"a CREATING restore target: {err:?}"
);
} else {
assert!(
match in_use {
Some(text) => assert!(
matches!(err, DynamoDbError::TableInUseException(ref m) if m == text),
"{i}: a CREATING restore target: {err:?}"
),
None => assert!(
matches!(err, DynamoDbError::TableAlreadyExistsException(ref m) if m.contains("MusicRestored")),
"{i}: {err:?}"
);
),
}
}

// The backup missing: BackupNotFound whatever the target's state, and
// the name is never read.
for (i, target) in targets.iter().enumerate() {
for (i, (target, _)) in targets.iter().enumerate() {
let (fake, err) = check(Arc::new(Fake::default()), target.clone()).await;
assert!(
matches!(err, DynamoDbError::BackupNotFoundException(ref m)
Expand Down Expand Up @@ -15127,7 +15153,7 @@ async fn the_registrations_refusals_keep_their_classes_and_return_the_slot() {
);
let runtime = job_runtime(&mem);
type Expected = fn(&DynamoDbError) -> bool;
let cases: [(StorageError, Expected); 4] = [
let cases: [(StorageError, Expected); 5] = [
(
StorageError::TableAlreadyExists("MusicRestored".to_owned()),
|e| matches!(e, DynamoDbError::TableAlreadyExistsException(_)),
Expand All @@ -15137,7 +15163,22 @@ async fn the_registrations_refusals_keep_their_classes_and_return_the_slot() {
table_name: "MusicRestored".to_owned(),
backup_arn: arn(ACCOUNT),
},
|e| matches!(e, DynamoDbError::TableInUseException(_)),
|e| {
matches!(e, DynamoDbError::TableInUseException(m) if *m == format!(
"Table: MusicRestored is already being restored from backup: {}",
arn(ACCOUNT)
))
},
),
(
StorageError::TableBeingRestored {
table_name: "MusicRestored".to_owned(),
backup_arn: arn_n(2),
},
|e| {
matches!(e, DynamoDbError::TableInUseException(m)
if *m == restore_target_in_use_message("MusicRestored"))
},
),
(
extenddb_storage::backup_catalog::backup_not_found(&arn(ACCOUNT)),
Expand Down Expand Up @@ -15247,7 +15288,8 @@ async fn a_backup_being_restored_is_in_use_until_its_target_is_active() {
.await
.unwrap_err();
assert!(
matches!(err, DynamoDbError::TableInUseException(_)),
matches!(err, DynamoDbError::TableInUseException(ref m)
if *m == restore_target_in_use_message("MusicRestored")),
"another backup into the name being restored into: {err:?}"
);
gate.add_permits(1);
Expand Down Expand Up @@ -20007,7 +20049,8 @@ async fn a_retry_waits_on_its_registration_and_another_backup_is_refused() {
.await
.unwrap_err();
assert!(
matches!(other, DynamoDbError::TableInUseException(_)),
matches!(other, DynamoDbError::TableInUseException(ref m)
if *m == restore_target_in_use_message("MusicRestored")),
"{other:?}"
);
assert_eq!(fake.restore_claims.lock().unwrap().len(), 1);
Expand Down
16 changes: 12 additions & 4 deletions crates/storage/src/backup_catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1015,13 +1015,21 @@ pub fn backup_in_use(table: &str, backup_arn: &str) -> StorageError {
StorageError::BackupInUse(backup_in_use_message(backup_arn))
}

/// The text the service answers for a change to a CREATING restore target:
/// DeleteTable of it (measured on DynamoDB on 2026-11-21) and a restore of
/// another backup into its name (measured on 2026-11-22).
#[must_use]
pub fn restore_target_in_use_message(table_name: &str) -> String {
format!(
"Attempt to change a resource which is still in use: Table: {table_name} is being used."
)
}

/// DeleteTable on a CREATING restore target, as every backend answers it
/// (`ResourceInUseException`, the text measured on DynamoDB on 2026-11-21).
/// (`ResourceInUseException`, [`restore_target_in_use_message`]).
#[must_use]
pub fn restore_target_in_use(table_name: &str) -> StorageError {
StorageError::IndexesInUse(format!(
"Attempt to change a resource which is still in use: Table: {table_name} is being used."
))
StorageError::IndexesInUse(restore_target_in_use_message(table_name))
}

/// The one signal of a missing backup every backend emits inside a
Expand Down
Loading
Loading