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

chore: fix drop_table_by_id fail when TableIdToName cannot found #14002

Merged
merged 3 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 5 additions & 1 deletion src/binaries/metabench/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,11 @@ async fn benchmark_table(client: &Arc<ClientHandle>, prefix: u64, client_num: u6
let res = client
.drop_table_by_id(DropTableByIdReq {
if_exists: false,
tenant: tenant(),
name_ident: TableNameIdent {
tenant: tenant(),
db_name: db_name(),
table_name: table_name(),
},
tb_id: t.ident.table_id,
})
.await;
Expand Down
39 changes: 32 additions & 7 deletions src/meta/api/src/schema_api_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ use common_meta_app::app_error::UndropTableAlreadyExists;
use common_meta_app::app_error::UndropTableHasNoHistory;
use common_meta_app::app_error::UndropTableWithNoDropTime;
use common_meta_app::app_error::UnknownCatalog;
use common_meta_app::app_error::UnknownDatabase;
use common_meta_app::app_error::UnknownIndex;
use common_meta_app::app_error::UnknownStreamId;
use common_meta_app::app_error::UnknownTable;
Expand Down Expand Up @@ -188,6 +189,7 @@ use log::as_debug;
use log::as_display;
use log::debug;
use log::error;
use log::warn;
use minitrace::func_name;
use ConditionResult::Eq;

Expand Down Expand Up @@ -2323,7 +2325,7 @@ impl<KV: kvapi::KVApi<Error = MetaError> + ?Sized> SchemaApi for KV {
let mut tb_count = 0;
let mut tb_count_seq;
let tbid = TableId { table_id };
let tenant = &req.tenant;
let tenant = &req.name_ident.tenant;
let mut retry = 0;

while retry < TXN_MAX_RETRY_TIMES {
Expand All @@ -2342,12 +2344,35 @@ impl<KV: kvapi::KVApi<Error = MetaError> + ?Sized> SchemaApi for KV {
let (_, table_name_opt): (_, Option<DBIdTableName>) =
get_pb_value(self, &table_id_to_name).await?;

let dbid_tbname = table_name_opt.ok_or_else(|| {
KVAppError::AppError(AppError::UnknownTableId(UnknownTableId::new(
table_id,
"drop_table_by_id failed to find db_id",
)))
})?;
let dbid_tbname = if let Some(db_id_table_name) = table_name_opt {
db_id_table_name
} else {
let name_key = DatabaseNameIdent {
tenant: tenant.clone(),
db_name: req.name_ident.db_name.clone(),
};
warn!(
"drop_table_by_id cannot find {:?}, use {:?} instead",
table_id_to_name, name_key
);
match get_u64_value(self, &name_key).await {
Ok((_db_id_seq, db_id)) => DBIdTableName {
db_id,
table_name: req.name_ident.table_name.clone(),
},
Err(_) => {
return Err(KVAppError::AppError(AppError::UnknownDatabase(
UnknownDatabase::new(
&req.name_ident.db_name,
format!(
"drop_table_by_id cannot find db {:?}",
req.name_ident.db_name
),
),
)));
}
}
};

let db_id = dbid_tbname.db_id;
let tbname = dbid_tbname.table_name.clone();
Expand Down
130 changes: 117 additions & 13 deletions src/meta/api/src/schema_api_test_suite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,9 @@ impl SchemaApiTestSuite {
.await?;
suite.catalog_create_get_list_drop(&b.build().await).await?;
suite.table_least_visible_time(&b.build().await).await?;
suite
.drop_table_without_tableid_to_name(&b.build().await)
.await?;

Ok(())
}
Expand Down Expand Up @@ -1402,6 +1405,75 @@ impl SchemaApiTestSuite {
Ok(())
}

#[minitrace::trace]
async fn drop_table_without_tableid_to_name<
MT: SchemaApi + kvapi::AsKVApi<Error = MetaError>,
>(
&self,
mt: &MT,
) -> anyhow::Result<()> {
let tenant = "tenant1";
let db = "db";
let table_name = "tbl";

let create_db_req = CreateDatabaseReq {
if_not_exists: false,
name_ident: DatabaseNameIdent {
tenant: tenant.to_string(),
db_name: db.to_string(),
},
meta: DatabaseMeta {
engine: "".to_string(),
..DatabaseMeta::default()
},
};

let _res = mt.create_database(create_db_req.clone()).await?;

let schema = || {
Arc::new(TableSchema::new(vec![TableField::new(
"number",
TableDataType::Number(NumberDataType::UInt64),
)]))
};

let table_meta = |created_on| TableMeta {
schema: schema(),
engine: "JSON".to_string(),
options: BTreeMap::new(),
updated_on: created_on,
created_on,
..TableMeta::default()
};
let created_on = Utc::now();

let req = CreateTableReq {
if_not_exists: false,
name_ident: TableNameIdent {
tenant: tenant.to_string(),
db_name: db.to_string(),
table_name: table_name.to_string(),
},

table_meta: table_meta(created_on),
};
let resp = mt.create_table(req.clone()).await?;
let table_id = resp.table_id;

let table_id_to_name = TableIdToName { table_id };
// delete TableIdToName before drop table
delete_test_data(mt.as_kv_api(), &table_id_to_name).await?;

mt.drop_table_by_id(DropTableByIdReq {
if_exists: false,
name_ident: req.name_ident.clone(),
tb_id: table_id,
})
.await?;

Ok(())
}

#[minitrace::trace]
async fn table_least_visible_time<MT: SchemaApi>(&self, mt: &MT) -> anyhow::Result<()> {
let tenant = "tenant1";
Expand Down Expand Up @@ -1740,7 +1812,11 @@ impl SchemaApiTestSuite {
{
let plan = DropTableByIdReq {
if_exists: false,
tenant: tenant.to_string(),
name_ident: TableNameIdent {
tenant: tenant.to_string(),
db_name: db_name.to_string(),
table_name: tbl_name.to_string(),
},
tb_id,
};
mt.drop_table_by_id(plan.clone()).await?;
Expand All @@ -1767,7 +1843,11 @@ impl SchemaApiTestSuite {
{
let plan = DropTableByIdReq {
if_exists: false,
tenant: tenant.to_string(),
name_ident: TableNameIdent {
tenant: tenant.to_string(),
db_name: db_name.to_string(),
table_name: tbl_name.to_string(),
},
tb_id,
};
let res = mt.drop_table_by_id(plan).await;
Expand All @@ -1784,7 +1864,11 @@ impl SchemaApiTestSuite {
{
let plan = DropTableByIdReq {
if_exists: true,
tenant: tenant.to_string(),
name_ident: TableNameIdent {
tenant: tenant.to_string(),
db_name: db_name.to_string(),
table_name: tbl_name.to_string(),
},
tb_id,
};
mt.drop_table_by_id(plan.clone()).await?;
Expand Down Expand Up @@ -3651,7 +3735,7 @@ impl SchemaApiTestSuite {

mt.drop_table_by_id(DropTableByIdReq {
if_exists: false,
tenant: tenant.to_string(),
name_ident: req.name_ident.clone(),
tb_id: resp.table_id,
})
.await?;
Expand All @@ -3673,7 +3757,7 @@ impl SchemaApiTestSuite {
let resp = mt.create_table(req.clone()).await?;
mt.drop_table_by_id(DropTableByIdReq {
if_exists: false,
tenant: tenant.to_string(),
name_ident: req.name_ident.clone(),
tb_id: resp.table_id,
})
.await?;
Expand Down Expand Up @@ -3749,7 +3833,7 @@ impl SchemaApiTestSuite {
drop_ids_2.push(DroppedId::Table(db_id, resp.table_id, "tb1".to_string()));
mt.drop_table_by_id(DropTableByIdReq {
if_exists: false,
tenant: tenant.to_string(),
name_ident: req.name_ident.clone(),
tb_id: resp.table_id,
})
.await?;
Expand All @@ -3772,7 +3856,7 @@ impl SchemaApiTestSuite {
drop_ids_2.push(DroppedId::Table(db_id, resp.table_id, "tb2".to_string()));
mt.drop_table_by_id(DropTableByIdReq {
if_exists: false,
tenant: tenant.to_string(),
name_ident: req.name_ident.clone(),
tb_id: resp.table_id,
})
.await?;
Expand Down Expand Up @@ -3947,7 +4031,7 @@ impl SchemaApiTestSuite {

mt.drop_table_by_id(DropTableByIdReq {
if_exists: false,
tenant: tenant.to_string(),
name_ident: req.name_ident.clone(),
tb_id: resp.table_id,
})
.await?;
Expand Down Expand Up @@ -4202,7 +4286,11 @@ impl SchemaApiTestSuite {
let old_db = mt.get_database(Self::req_get_db(tenant, db_name)).await?;
mt.drop_table_by_id(DropTableByIdReq {
if_exists: false,
tenant: tenant.to_string(),
name_ident: TableNameIdent {
tenant: tenant.to_string(),
db_name: db_name.to_string(),
table_name: tbl_name.to_string(),
},
tb_id,
})
.await?;
Expand Down Expand Up @@ -4257,7 +4345,11 @@ impl SchemaApiTestSuite {
let old_db = mt.get_database(Self::req_get_db(tenant, db_name)).await?;
mt.drop_table_by_id(DropTableByIdReq {
if_exists: false,
tenant: tenant.to_string(),
name_ident: TableNameIdent {
tenant: tenant.to_string(),
db_name: db_name.to_string(),
table_name: tbl_name.to_string(),
},
tb_id,
})
.await?;
Expand Down Expand Up @@ -4315,7 +4407,11 @@ impl SchemaApiTestSuite {
let tb_info = mt.get_table((tenant, db_name, tbl_name).into()).await?;
mt.drop_table_by_id(DropTableByIdReq {
if_exists: false,
tenant: tenant.to_string(),
name_ident: TableNameIdent {
tenant: tenant.to_string(),
db_name: db_name.to_string(),
table_name: tbl_name.to_string(),
},
tb_id: tb_info.ident.table_id,
})
.await?;
Expand Down Expand Up @@ -4417,7 +4513,11 @@ impl SchemaApiTestSuite {
// then drop table2
let drop_plan = DropTableByIdReq {
if_exists: false,
tenant: tenant.to_string(),
name_ident: TableNameIdent {
tenant: tenant.to_string(),
db_name: db_name.to_string(),
table_name: new_tbl_name.to_string(),
},
tb_id: new_tb_info.ident.table_id,
};

Expand Down Expand Up @@ -6338,7 +6438,11 @@ where MT: SchemaApi + kvapi::AsKVApi<Error = MetaError>
async fn drop_table_by_id(&mut self) -> anyhow::Result<()> {
let req = DropTableByIdReq {
if_exists: false,
tenant: self.tenant(),
name_ident: TableNameIdent {
tenant: self.tenant(),
db_name: self.db_name(),
table_name: self.tbl_name(),
},
tb_id: self.table_id,
};
self.mt.drop_table_by_id(req.clone()).await?;
Expand Down
6 changes: 5 additions & 1 deletion src/meta/api/src/share_api_test_suite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1704,7 +1704,11 @@ impl ShareApiTestSuite {

let plan = DropTableByIdReq {
if_exists: false,
tenant: tenant.to_string(),
name_ident: TableNameIdent {
tenant: tenant.to_string(),
db_name: db_name.to_string(),
table_name: tbl_name.to_string(),
},
tb_id: table_id,
};
let _res = mt.drop_table_by_id(plan).await;
Expand Down
4 changes: 2 additions & 2 deletions src/meta/app/src/schema/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,9 +488,9 @@ pub struct CreateTableReply {
pub struct DropTableByIdReq {
pub if_exists: bool,

pub tenant: String,

pub tb_id: MetaId,

pub name_ident: TableNameIdent,
}

impl DropTableByIdReq {
Expand Down
6 changes: 5 additions & 1 deletion src/query/ee/src/stream/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,11 @@ impl StreamHandler for RealStreamHandler {
catalog
.drop_table_by_id(DropTableByIdReq {
if_exists: plan.if_exists,
tenant,
name_ident: TableNameIdent {
tenant: tenant.clone(),
db_name: plan.database.clone(),
table_name: stream_name.clone(),
},
tb_id: table.get_id(),
lichuang marked this conversation as resolved.
Show resolved Hide resolved
})
.await
Expand Down
7 changes: 6 additions & 1 deletion src/query/service/src/interpreters/interpreter_table_drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use common_exception::Result;
use common_management::RoleApi;
use common_meta_app::principal::GrantObjectByID;
use common_meta_app::schema::DropTableByIdReq;
use common_meta_app::schema::TableNameIdent;
use common_sql::plans::DropTablePlan;
use common_storages_fuse::FuseTable;
use common_storages_share::save_share_spec;
Expand Down Expand Up @@ -107,7 +108,11 @@ impl Interpreter for DropTableInterpreter {
let resp = catalog
.drop_table_by_id(DropTableByIdReq {
if_exists: self.plan.if_exists,
tenant: self.plan.tenant.clone(),
name_ident: TableNameIdent {
tenant,
db_name: db_name.to_string(),
table_name: tbl_name.to_string(),
},
tb_id: tbl.get_table_info().ident.table_id,
})
.await?;
Expand Down
6 changes: 5 additions & 1 deletion src/query/service/src/interpreters/interpreter_view_alter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ impl Interpreter for AlterViewInterpreter {
catalog
.drop_table_by_id(DropTableByIdReq {
if_exists: true,
tenant: self.plan.tenant.clone(),
name_ident: TableNameIdent {
tenant: self.plan.tenant.clone(),
db_name: self.plan.database.clone(),
table_name: self.plan.view_name.clone(),
},
tb_id: tbl.get_id(),
})
.await?;
Expand Down
Loading
Loading