Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add drop catalog support for metasrv #12248

Merged
merged 4 commits into from
Jul 29, 2023
Merged
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
4 changes: 4 additions & 0 deletions src/meta/api/src/schema_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -238,6 +240,8 @@ pub trait SchemaApi: Send + Sync {

async fn get_catalog(&self, req: GetCatalogReq) -> Result<Arc<CatalogInfo>, KVAppError>;

async fn drop_catalog(&self, req: DropCatalogReq) -> Result<DropCatalogReply, KVAppError>;

async fn list_catalogs(&self, req: ListCatalogReq)
-> Result<Vec<Arc<CatalogInfo>>, KVAppError>;

Expand Down
77 changes: 77 additions & 0 deletions src/meta/api/src/schema_api_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -3306,6 +3308,81 @@ impl<KV: kvapi::KVApi<Error = MetaError>> SchemaApi for KV {
Ok(Arc::new(catalog))
}

#[logcall::logcall("debug")]
#[minitrace::trace]
async fn drop_catalog(&self, req: DropCatalogReq) -> Result<DropCatalogReply, KVAppError> {
debug!(req = as_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);
}
};

// Delete catalog by deleting 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 = catalog_id,
name_key = as_debug!(&name_key);
"catalog keys 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/<catalog_id> -> (tenant,catalog_name) */
];

let txn_req = TxnRequest {
condition,
if_then,
else_then: vec![],
};

let (succ, _) = send_txn(self, txn_req).await?;

debug!(
name = as_debug!(&name_key),
id = as_debug!(&id_key),
succ = succ;
"drop_catalog"
);

if succ {
break;
}
}
}

Ok(DropCatalogReply {})
}

#[logcall::logcall("debug")]
#[minitrace::trace]
async fn list_catalogs(
Expand Down
18 changes: 16 additions & 2 deletions src/meta/api/src/schema_api_test_suite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(())
}
Expand Down Expand Up @@ -1320,7 +1321,7 @@ impl SchemaApiTestSuite {
}

#[minitrace::trace]
async fn catalog_create_get_list<MT: SchemaApi>(&self, mt: &MT) -> anyhow::Result<()> {
async fn catalog_create_get_list_drop<MT: SchemaApi>(&self, mt: &MT) -> anyhow::Result<()> {
let tenant = "tenant1";
let catalog_name = "catalog1";

Expand Down Expand Up @@ -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(())
}

Expand Down
3 changes: 3 additions & 0 deletions src/meta/app/src/schema/catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down