From b17aa6b45b86819901ccf2b236fb6e7eef92a452 Mon Sep 17 00:00:00 2001 From: Alex Chi Date: Wed, 17 May 2023 09:27:20 -0400 Subject: [PATCH] use guard for stop tenant Signed-off-by: Alex Chi --- pageserver/src/tenant/mgr.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/pageserver/src/tenant/mgr.rs b/pageserver/src/tenant/mgr.rs index 8148888017..af83e03599 100644 --- a/pageserver/src/tenant/mgr.rs +++ b/pageserver/src/tenant/mgr.rs @@ -296,19 +296,23 @@ pub async fn create_tenant( let created_tenant = schedule_local_tenant_processing(conf, &tenant_directory, remote_storage, ctx)?; - // As we already removed the directory, the tenant should directly go into the broken state. - let destroy = || created_tenant.set_broken("failed to create".into()); + let tenant_cloned = Arc::clone(&created_tenant); + scopeguard::defer! { + if !succeed.get() { + // As we might have removed the directory, the tenant should directly go into the broken state. + tenant_cloned.set_broken("failed to create".into()); + } + } fail::fail_point!("tenant-create-fail", |_| { - destroy(); // add this to all fail paths anyhow::bail!("failpoint: tenant-create-fail"); }); let crated_tenant_id = created_tenant.tenant_id(); - if tenant_id != crated_tenant_id { - destroy(); // add this to all fail paths - anyhow::bail!("loaded created tenant has unexpected tenant id (expect {tenant_id} != actual {crated_tenant_id})"); - } + anyhow::ensure!( + tenant_id == crated_tenant_id, + "loaded created tenant has unexpected tenant id (expect {tenant_id} != actual {crated_tenant_id})", + ); vacant_entry.insert(Arc::clone(&created_tenant));