From 411fcead9d7f38c8b2bef1dfb24c109b8d9c245c Mon Sep 17 00:00:00 2001 From: Xuanwo Date: Sat, 29 Jul 2023 12:41:32 +0800 Subject: [PATCH 1/3] feat: Add drop catalog support for metasrv Signed-off-by: Xuanwo --- src/meta/api/src/schema_api.rs | 4 ++ src/meta/api/src/schema_api_impl.rs | 76 +++++++++++++++++++++++ src/meta/api/src/schema_api_test_suite.rs | 18 +++++- src/meta/app/src/schema/catalog.rs | 3 + 4 files changed, 99 insertions(+), 2 deletions(-) diff --git a/src/meta/api/src/schema_api.rs b/src/meta/api/src/schema_api.rs index 5f0df0799ddc5..967d44b600218 100644 --- a/src/meta/api/src/schema_api.rs +++ b/src/meta/api/src/schema_api.rs @@ -31,6 +31,8 @@ use common_meta_app::schema::CreateVirtualColumnReply; use common_meta_app::schema::CreateVirtualColumnReq; use common_meta_app::schema::DatabaseInfo; use common_meta_app::schema::DeleteTableLockRevReq; +use common_meta_app::schema::DropCatalogReply; +use common_meta_app::schema::DropCatalogReq; use common_meta_app::schema::DropDatabaseReply; use common_meta_app::schema::DropDatabaseReq; use common_meta_app::schema::DropIndexReply; @@ -238,6 +240,8 @@ pub trait SchemaApi: Send + Sync { async fn get_catalog(&self, req: GetCatalogReq) -> Result, KVAppError>; + async fn drop_catalog(&self, req: DropCatalogReq) -> Result; + async fn list_catalogs(&self, req: ListCatalogReq) -> Result>, KVAppError>; diff --git a/src/meta/api/src/schema_api_impl.rs b/src/meta/api/src/schema_api_impl.rs index c2946818e2b8b..4040138c7ce16 100644 --- a/src/meta/api/src/schema_api_impl.rs +++ b/src/meta/api/src/schema_api_impl.rs @@ -79,6 +79,8 @@ use common_meta_app::schema::DatabaseType; use common_meta_app::schema::DbIdList; use common_meta_app::schema::DbIdListKey; use common_meta_app::schema::DeleteTableLockRevReq; +use common_meta_app::schema::DropCatalogReply; +use common_meta_app::schema::DropCatalogReq; use common_meta_app::schema::DropDatabaseReply; use common_meta_app::schema::DropDatabaseReq; use common_meta_app::schema::DropIndexReply; @@ -3241,6 +3243,80 @@ impl> SchemaApi for KV { Ok(Arc::new(catalog)) } + #[tracing::instrument(level = "debug", ret, err, skip_all)] + async fn drop_catalog(&self, req: DropCatalogReq) -> Result { + debug!(req = debug(&req), "SchemaApi: {}", func_name!()); + + let name_key = &req.name_ident; + + let ctx = &func_name!(); + + let mut trials = txn_trials(None, ctx); + + loop { + trials.next().unwrap()?; + + let res = + get_catalog_or_err(self, name_key, format!("drop_catalog: {}", &name_key)).await; + + let (_, catalog_id, catalog_meta_seq, _) = match res { + Ok(x) => x, + Err(e) => { + if let KVAppError::AppError(AppError::UnknownCatalog(_)) = e { + if req.if_exists { + return Ok(DropCatalogReply {}); + } + } + + return Err(e); + } + }; + + // Create catalog by inserting these record: + // (tenant, catalog_name) -> catalog_id + // (catalog_id) -> catalog_meta + // (catalog_id) -> (tenant, catalog_name) + let id_key = CatalogId { catalog_id }; + let id_to_name_key = CatalogIdToName { catalog_id }; + + debug!( + catalog_id, + name_key = debug(&name_key), + "catalog id to delete" + ); + + { + let condition = vec![txn_cond_seq(&id_key, Eq, catalog_meta_seq)]; + let if_then = vec![ + txn_op_del(name_key), // (tenant, catalog_name) -> catalog_id + txn_op_del(&id_key), // (catalog_id) -> catalog_meta + txn_op_del(&id_to_name_key), /* __fd_catalog_id_to_name/ -> (tenant,catalog_name) */ + ]; + + let txn_req = TxnRequest { + condition, + if_then, + else_then: vec![], + }; + + let (succ, _) = send_txn(self, txn_req).await?; + + debug!( + name = debug(&name_key), + id = debug(&id_key), + succ = display(succ), + "drop_catalog" + ); + + if succ { + break; + } + } + } + + Ok(DropCatalogReply {}) + } + #[tracing::instrument(level = "debug", ret, err, skip_all)] async fn list_catalogs( &self, diff --git a/src/meta/api/src/schema_api_test_suite.rs b/src/meta/api/src/schema_api_test_suite.rs index b30edf80aa225..97491baea00fd 100644 --- a/src/meta/api/src/schema_api_test_suite.rs +++ b/src/meta/api/src/schema_api_test_suite.rs @@ -45,6 +45,7 @@ use common_meta_app::schema::DatabaseType; use common_meta_app::schema::DbIdList; use common_meta_app::schema::DbIdListKey; use common_meta_app::schema::DeleteTableLockRevReq; +use common_meta_app::schema::DropCatalogReq; use common_meta_app::schema::DropDatabaseReq; use common_meta_app::schema::DropIndexReq; use common_meta_app::schema::DropTableByIdReq; @@ -296,7 +297,7 @@ impl SchemaApiTestSuite { suite .virtual_column_create_list_drop(&b.build().await) .await?; - suite.catalog_create_get_list(&b.build().await).await?; + suite.catalog_create_get_list_drop(&b.build().await).await?; Ok(()) } @@ -1320,7 +1321,7 @@ impl SchemaApiTestSuite { } #[tracing::instrument(level = "debug", skip_all)] - async fn catalog_create_get_list(&self, mt: &MT) -> anyhow::Result<()> { + async fn catalog_create_get_list_drop(&self, mt: &MT) -> anyhow::Result<()> { let tenant = "tenant1"; let catalog_name = "catalog1"; @@ -1357,6 +1358,19 @@ impl SchemaApiTestSuite { assert_eq!(got[0].name_ident.tenant, "tenant1"); assert_eq!(got[0].name_ident.catalog_name, "catalog1"); + let _ = mt + .drop_catalog(DropCatalogReq { + if_exists: false, + name_ident: CatalogNameIdent { + tenant: tenant.to_string(), + catalog_name: catalog_name.to_string(), + }, + }) + .await?; + + let got = mt.list_catalogs(ListCatalogReq::new("tenant1")).await?; + assert_eq!(got.len(), 0); + Ok(()) } diff --git a/src/meta/app/src/schema/catalog.rs b/src/meta/app/src/schema/catalog.rs index 3f4f8285c437f..c921a941ee017 100644 --- a/src/meta/app/src/schema/catalog.rs +++ b/src/meta/app/src/schema/catalog.rs @@ -146,6 +146,9 @@ impl Display for DropCatalogReq { } } +#[derive(serde::Serialize, serde::Deserialize, Clone, Debug, PartialEq, Eq)] +pub struct DropCatalogReply {} + #[derive(serde::Serialize, serde::Deserialize, Clone, Debug, PartialEq, Eq)] pub struct GetCatalogReq { pub inner: CatalogNameIdent, From c573b7b622d3b914bbf45988f942164ad0435e9b Mon Sep 17 00:00:00 2001 From: Xuanwo Date: Sat, 29 Jul 2023 12:43:07 +0800 Subject: [PATCH 2/3] Fix typo Signed-off-by: Xuanwo --- src/meta/api/src/schema_api_impl.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/meta/api/src/schema_api_impl.rs b/src/meta/api/src/schema_api_impl.rs index 4040138c7ce16..664067ad26d60 100644 --- a/src/meta/api/src/schema_api_impl.rs +++ b/src/meta/api/src/schema_api_impl.rs @@ -3272,7 +3272,7 @@ impl> SchemaApi for KV { } }; - // Create catalog by inserting these record: + // Delete catalog by deleting these record: // (tenant, catalog_name) -> catalog_id // (catalog_id) -> catalog_meta // (catalog_id) -> (tenant, catalog_name) @@ -3282,7 +3282,7 @@ impl> SchemaApi for KV { debug!( catalog_id, name_key = debug(&name_key), - "catalog id to delete" + "catalog keys to delete" ); { From 4b7a442ba1d08852f40fed63c566ef0935994801 Mon Sep 17 00:00:00 2001 From: Xuanwo Date: Sat, 29 Jul 2023 21:49:07 +0800 Subject: [PATCH 3/3] Fix build Signed-off-by: Xuanwo --- src/meta/api/src/schema_api_impl.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/meta/api/src/schema_api_impl.rs b/src/meta/api/src/schema_api_impl.rs index 45eac5810b79e..7b4dd3c5da4f7 100644 --- a/src/meta/api/src/schema_api_impl.rs +++ b/src/meta/api/src/schema_api_impl.rs @@ -3311,7 +3311,7 @@ impl> SchemaApi for KV { #[logcall::logcall("debug")] #[minitrace::trace] async fn drop_catalog(&self, req: DropCatalogReq) -> Result { - debug!(req = debug(&req), "SchemaApi: {}", func_name!()); + debug!(req = as_debug!(&req); "SchemaApi: {}", func_name!()); let name_key = &req.name_ident; @@ -3346,8 +3346,8 @@ impl> SchemaApi for KV { let id_to_name_key = CatalogIdToName { catalog_id }; debug!( - catalog_id, - name_key = debug(&name_key), + catalog_id = catalog_id, + name_key = as_debug!(&name_key); "catalog keys to delete" ); @@ -3368,9 +3368,9 @@ impl> SchemaApi for KV { let (succ, _) = send_txn(self, txn_req).await?; debug!( - name = debug(&name_key), - id = debug(&id_key), - succ = display(succ), + name = as_debug!(&name_key), + id = as_debug!(&id_key), + succ = succ; "drop_catalog" );