From b4e3025324fb4694dd785ed6422047bb693881fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E7=82=8E=E6=B3=BC?= Date: Wed, 25 Sep 2024 14:11:17 +0800 Subject: [PATCH] chore: simplify SchemaApi::truncate_table() (#16506) * refactor: SchemaApi::list_tables() should specify db-id instead of db-name - Change list_tables() to accept db_id instead of db_name for better precision - Modify return type from TableInfo to (table_name, table_id, SeqV). Because some info in TableInfo can not be provided by SchemaApi * refactor: when SchemaApi::truncate_table(), no need to assert table seq truncate_table() first lists all copied files belonging to a table, then deletes them in small chunks. The delete operations assert that the seq of each file does not change. With this approach, there is no need to assert that the seq of the containing table does not change. * chore: simplify SchemaApi::truncate_table() * chore: fixup lint --- src/meta/api/src/schema_api.rs | 9 +- src/meta/api/src/schema_api_impl.rs | 172 ++++++------------ src/meta/api/src/schema_api_test_suite.rs | 69 +++---- src/meta/api/src/util.rs | 111 ----------- src/meta/app/src/schema/table.rs | 17 +- .../src/databases/default/default_database.rs | 28 ++- 6 files changed, 127 insertions(+), 279 deletions(-) diff --git a/src/meta/api/src/schema_api.rs b/src/meta/api/src/schema_api.rs index 6d371ecfcacd..679f2aedd7e8 100644 --- a/src/meta/api/src/schema_api.rs +++ b/src/meta/api/src/schema_api.rs @@ -211,9 +211,16 @@ pub trait SchemaApi: Send + Sync { async fn get_tables_history( &self, req: ListTableReq, + db_name: &str, ) -> Result>, KVAppError>; - async fn list_tables(&self, req: ListTableReq) -> Result>, KVAppError>; + /// List all tables in the database. + /// + /// Returns a list of `(table_name, table_id, table_meta)` tuples. + async fn list_tables( + &self, + req: ListTableReq, + ) -> Result)>, KVAppError>; /// Return TableMeta by table_id. /// diff --git a/src/meta/api/src/schema_api_impl.rs b/src/meta/api/src/schema_api_impl.rs index 74ac7c7a5f15..9e84ab0aa1fd 100644 --- a/src/meta/api/src/schema_api_impl.rs +++ b/src/meta/api/src/schema_api_impl.rs @@ -133,7 +133,6 @@ use databend_common_meta_app::schema::RenameTableReq; use databend_common_meta_app::schema::SetTableColumnMaskPolicyAction; use databend_common_meta_app::schema::SetTableColumnMaskPolicyReply; use databend_common_meta_app::schema::SetTableColumnMaskPolicyReq; -use databend_common_meta_app::schema::TableCopiedFileInfo; use databend_common_meta_app::schema::TableCopiedFileNameIdent; use databend_common_meta_app::schema::TableId; use databend_common_meta_app::schema::TableIdHistoryIdent; @@ -202,6 +201,7 @@ use crate::kv_app_error::KVAppError; use crate::kv_pb_api::KVPbApi; use crate::kv_pb_crud_api::KVPbCrudApi; use crate::list_keys; +use crate::list_u64_value; use crate::meta_txn_error::MetaTxnError; use crate::name_id_value_api::NameIdValueApi; use crate::name_value_api::NameValueApi; @@ -217,8 +217,6 @@ use crate::txn_op_put; use crate::util::db_id_has_to_exist; use crate::util::deserialize_id_get_response; use crate::util::deserialize_struct_get_response; -use crate::util::get_table_by_id_or_err; -use crate::util::list_tables_from_unshare_db; use crate::util::mget_pb_values; use crate::util::txn_delete_exact; use crate::util::txn_op_put_pb; @@ -1569,29 +1567,13 @@ impl + ?Sized> SchemaApi for KV { async fn get_tables_history( &self, req: ListTableReq, + db_name: &str, ) -> Result>, KVAppError> { debug!(req :? =(&req); "SchemaApi: {}", func_name!()); - let tenant_dbname = &req.inner; - - // Get db by name to ensure presence - let res = get_db_or_err( - self, - tenant_dbname, - format!("get_tables_history: {}", tenant_dbname.display()), - ) - .await; - - let (seq_db_id, _db_meta) = match res { - Ok(x) => x, - Err(e) => { - return Err(e); - } - }; - // List tables by tenant, db_id, table_name. let table_id_history_ident = TableIdHistoryIdent { - database_id: *seq_db_id.data, + database_id: req.database_id.db_id, table_name: "dummy".to_string(), }; @@ -1605,7 +1587,7 @@ impl + ?Sized> SchemaApi for KV { .iter() .map(|table_id_list_key| { TableIdHistoryIdent { - database_id: *seq_db_id.data, + database_id: req.database_id.db_id, table_name: table_id_list_key.table_name.clone(), } .to_string_key() @@ -1640,11 +1622,7 @@ impl + ?Sized> SchemaApi for KV { .map(|(table_id, seqv)| { Arc::new(TableInfo { ident: TableIdent::new(table_id.table_id, seqv.seq()), - desc: format!( - "'{}'.'{}'", - tenant_dbname.database_name(), - table_id_list_key.table_name, - ), + desc: format!("'{}'.'{}'", db_name, table_id_list_key.table_name,), name: table_id_list_key.table_name.to_string(), meta: seqv.data, db_type: DatabaseType::NormalDB, @@ -1661,29 +1639,38 @@ impl + ?Sized> SchemaApi for KV { #[logcall::logcall] #[fastrace::trace] - async fn list_tables(&self, req: ListTableReq) -> Result>, KVAppError> { + async fn list_tables( + &self, + req: ListTableReq, + ) -> Result)>, KVAppError> { debug!(req :? =(&req); "SchemaApi: {}", func_name!()); - let tenant_dbname = &req.inner; + let dbid_tbname = DBIdTableName { + db_id: req.database_id.db_id, + // Use empty name to scan all tables + table_name: "".to_string(), + }; - // Get db by name to ensure presence - let res = get_db_or_err( - self, - tenant_dbname, - format!("list_tables: {}", tenant_dbname.display()), - ) - .await; + let (names, ids) = list_u64_value(self, &dbid_tbname).await?; - let (seq_db_id, _db_meta) = match res { - Ok(x) => x, - Err(e) => { - return Err(e); - } - }; + let ids = ids + .into_iter() + .map(|id| TableId { table_id: id }) + .collect::>(); - let tb_infos = list_tables_from_unshare_db(self, *seq_db_id.data, tenant_dbname).await?; + let mut seq_metas = vec![]; + for chunk in ids.chunks(DEFAULT_MGET_SIZE) { + let got = self.get_pb_values_vec(chunk.to_vec()).await?; + seq_metas.extend(got); + } - Ok(tb_infos) + let res = names + .into_iter() + .zip(ids) + .zip(seq_metas) + .filter_map(|((n, id), seq_meta)| seq_meta.map(|x| (n.table_name, id, x))) + .collect::>(); + Ok(res) } #[logcall::logcall] @@ -2105,8 +2092,6 @@ impl + ?Sized> SchemaApi for KV { debug!(req :? =(&req); "SchemaApi: {}", func_name!()); - let ctx = func_name!(); - let table_id = TableId { table_id: req.table_id, }; @@ -2118,20 +2103,26 @@ impl + ?Sized> SchemaApi for KV { // If table seq is not changed before and after listing, we can be sure the list of copied // files is consistent to this version of the table. - let (mut seq_1, _tb_meta) = get_table_by_id_or_err(self, &table_id, ctx).await?; + let mut seq_1 = self.get_seq(&table_id).await?; let mut trials = txn_backoff(None, func_name!()); let copied_files = loop { trials.next().unwrap()?.await; - let copied_files = list_table_copied_files(self, table_id.table_id).await?; + let copied_file_ident = TableCopiedFileNameIdent { + table_id: table_id.table_id, + file: "dummy".to_string(), + }; + let dir_name = DirName::new(copied_file_ident); + let copied_files = self.list_pb_vec(&dir_name).await?; - let (seq_2, _tb_meta) = get_table_by_id_or_err(self, &table_id, ctx).await?; + let seq_2 = self.get_seq(&table_id).await?; if seq_1 == seq_2 { debug!( - "list all copied file of table {}: {:?}", - table_id.table_id, copied_files + "list all copied file of table {}: {} files", + table_id.table_id, + copied_files.len() ); break copied_files; } else { @@ -2141,64 +2132,22 @@ impl + ?Sized> SchemaApi for KV { // 2. Remove the copied files only when the seq of a copied file has not changed. // - // During running this step with several small transaction, other transactions may be - // modifying the table. - // - // - We assert the table seq is not changed in each transaction. // - We do not assert the seq of each copied file in each transaction, since we only delete // non-changed ones. for chunk in copied_files.chunks(chunk_size as usize) { - let str_keys: Vec<_> = chunk.iter().map(|f| f.to_string_key()).collect(); - - // Load the `seq` of every copied file - let seqs = { - let seq_infos: Vec<(u64, Option)> = - mget_pb_values(self, &str_keys).await?; - - seq_infos.into_iter().map(|(seq, _)| seq) + let txn = TxnRequest { + condition: vec![], + if_then: chunk + .iter() + .map(|(name, seq_file)| { + TxnOp::delete_exact(name.to_string_key(), Some(seq_file.seq())) + }) + .collect(), + else_then: vec![], }; - let mut if_then = vec![]; - for (copied_seq, copied_str_key) in seqs.zip(str_keys) { - if copied_seq == 0 { - continue; - } - - if_then.push(TxnOp::delete_exact(copied_str_key, Some(copied_seq))); - } - - let mut trials = txn_backoff(None, func_name!()); - loop { - trials.next().unwrap()?.await; - - let (tb_meta_seq, tb_meta) = get_table_by_id_or_err(self, &table_id, ctx).await?; - - let mut if_then = if_then.clone(); - - // Update to increase table meta seq, so that to assert no other process modify the table - if_then.push(txn_op_put(&table_id, serialize_struct(&tb_meta)?)); - - let txn_req = TxnRequest { - condition: vec![txn_cond_seq(&table_id, Eq, tb_meta_seq)], - if_then, - else_then: vec![], - }; - - debug!("submit chunk delete copied files: {:?}", txn_req); - - let (succ, _responses) = send_txn(self, txn_req).await?; - debug!( - id :? =(&table_id), - succ = succ, - ctx = ctx; - "" - ); - - if succ { - break; - } - } + let (_succ, _responses) = send_txn(self, txn).await?; } Ok(TruncateTableReply {}) @@ -3472,23 +3421,6 @@ async fn remove_copied_files_for_dropped_table( unreachable!() } -/// List the copied file identities belonging to a table. -async fn list_table_copied_files( - kv_api: &(impl kvapi::KVApi + ?Sized), - table_id: u64, -) -> Result, MetaError> { - let copied_file_ident = TableCopiedFileNameIdent { - table_id, - file: "dummy".to_string(), - }; - - let dir_name = DirName::new(copied_file_ident); - - let copied_files = list_keys(kv_api, &dir_name).await?; - - Ok(copied_files) -} - /// Get the retention boundary time before which the data can be permanently removed. fn get_retention_boundary(now: DateTime) -> DateTime { now - Duration::from_secs(DEFAULT_DATA_RETENTION_SECONDS as u64) diff --git a/src/meta/api/src/schema_api_test_suite.rs b/src/meta/api/src/schema_api_test_suite.rs index d19c1974e031..cc87b3e3a707 100644 --- a/src/meta/api/src/schema_api_test_suite.rs +++ b/src/meta/api/src/schema_api_test_suite.rs @@ -3988,6 +3988,8 @@ impl SchemaApiTestSuite { )])) }; + let db_id; + info!("--- prepare db"); { let plan = CreateDatabaseReq { @@ -4003,6 +4005,7 @@ impl SchemaApiTestSuite { info!("create database res: {:?}", res); assert_eq!(1, *res.db_id, "first database id is 1"); + db_id = res.db_id; } let created_on = Utc::now(); @@ -4030,7 +4033,7 @@ impl SchemaApiTestSuite { assert!(table_id >= 1, "table id >= 1"); let res = mt - .get_tables_history(ListTableReq::new(&tenant, db_name)) + .get_tables_history(ListTableReq::new(&tenant, db_id), db_name) .await?; assert_eq!(res.len(), 1); @@ -4048,7 +4051,7 @@ impl SchemaApiTestSuite { upsert_test_data(mt.as_kv_api(), &tbid, data).await?; // assert not return out of retention time data let res = mt - .get_tables_history(ListTableReq::new(&tenant, db_name)) + .get_tables_history(ListTableReq::new(&tenant, db_id), db_name) .await?; assert_eq!(res.len(), 0); @@ -4672,6 +4675,8 @@ impl SchemaApiTestSuite { ..TableMeta::default() }; + let db_id; + info!("--- prepare db"); { let plan = CreateDatabaseReq { @@ -4687,6 +4692,8 @@ impl SchemaApiTestSuite { info!("create database res: {:?}", res); assert_eq!(1, *res.db_id, "first database id is 1"); + + db_id = res.db_id; } let created_on = Utc::now(); @@ -4707,7 +4714,7 @@ impl SchemaApiTestSuite { assert!(res.table_id >= 1, "table id >= 1"); let res = mt - .get_tables_history(ListTableReq::new(&tenant, db_name)) + .get_tables_history(ListTableReq::new(&tenant, db_id), db_name) .await?; calc_and_compare_drop_on_table_result(res, vec![DroponInfo { @@ -4743,7 +4750,7 @@ impl SchemaApiTestSuite { assert!(old_db.meta.seq < cur_db.meta.seq); let res = mt - .get_tables_history(ListTableReq::new(&tenant, db_name)) + .get_tables_history(ListTableReq::new(&tenant, db_id), db_name) .await?; calc_and_compare_drop_on_table_result(res, vec![DroponInfo { name: tbl_name.to_string(), @@ -4765,7 +4772,7 @@ impl SchemaApiTestSuite { assert!(old_db.meta.seq < cur_db.meta.seq); let res = mt - .get_tables_history(ListTableReq::new(&tenant, db_name)) + .get_tables_history(ListTableReq::new(&tenant, db_id), db_name) .await?; calc_and_compare_drop_on_table_result(res, vec![DroponInfo { name: tbl_name.to_string(), @@ -4796,7 +4803,7 @@ impl SchemaApiTestSuite { assert!(old_db.meta.seq < cur_db.meta.seq); let res = mt - .get_tables_history(ListTableReq::new(&tenant, db_name)) + .get_tables_history(ListTableReq::new(&tenant, db_id), db_name) .await?; calc_and_compare_drop_on_table_result(res, vec![DroponInfo { name: tbl_name.to_string(), @@ -4823,7 +4830,7 @@ impl SchemaApiTestSuite { assert!(res.table_id >= 1, "table id >= 1"); let res = mt - .get_tables_history(ListTableReq::new(&tenant, db_name)) + .get_tables_history(ListTableReq::new(&tenant, db_id), db_name) .await?; calc_and_compare_drop_on_table_result(res, vec![DroponInfo { @@ -4855,7 +4862,7 @@ impl SchemaApiTestSuite { assert!(old_db.meta.seq < cur_db.meta.seq); let res = mt - .get_tables_history(ListTableReq::new(&tenant, db_name)) + .get_tables_history(ListTableReq::new(&tenant, db_id), db_name) .await?; calc_and_compare_drop_on_table_result(res, vec![DroponInfo { name: tbl_name.to_string(), @@ -4876,7 +4883,7 @@ impl SchemaApiTestSuite { assert!(old_db.meta.seq < cur_db.meta.seq); let res = mt - .get_tables_history(ListTableReq::new(&tenant, db_name)) + .get_tables_history(ListTableReq::new(&tenant, db_id), db_name) .await?; calc_and_compare_drop_on_table_result(res, vec![DroponInfo { @@ -4913,7 +4920,7 @@ impl SchemaApiTestSuite { let old_db = mt.get_database(Self::req_get_db(&tenant, db_name)).await?; let _res = mt.create_table(req.clone()).await?; let res = mt - .get_tables_history(ListTableReq::new(&tenant, db_name)) + .get_tables_history(ListTableReq::new(&tenant, db_id), db_name) .await?; let cur_db = mt.get_database(Self::req_get_db(&tenant, db_name)).await?; assert!(old_db.meta.seq < cur_db.meta.seq); @@ -4960,7 +4967,7 @@ impl SchemaApiTestSuite { assert!(old_db.meta.seq < cur_db.meta.seq); let res = mt - .get_tables_history(ListTableReq::new(&tenant, db_name)) + .get_tables_history(ListTableReq::new(&tenant, db_id), db_name) .await?; calc_and_compare_drop_on_table_result(res, vec![ DroponInfo { @@ -4997,7 +5004,7 @@ impl SchemaApiTestSuite { assert!(old_db.meta.seq < cur_db.meta.seq); let res = mt - .get_tables_history(ListTableReq::new(&tenant, db_name)) + .get_tables_history(ListTableReq::new(&tenant, db_id), db_name) .await?; calc_and_compare_drop_on_table_result(res, vec![ DroponInfo { @@ -5920,22 +5927,14 @@ impl SchemaApiTestSuite { let tenant = Tenant::new_or_err(tenant_name, func_name!())?; let db_name = "db1"; - info!("--- list table on unknown db"); - { - let res = mt.list_tables(ListTableReq::new(&tenant, db_name)).await; - debug!("list table on unknown db res: {:?}", res); - assert!(res.is_err()); - - let code = ErrorCode::from(res.unwrap_err()).code(); - assert_eq!(ErrorCode::UNKNOWN_DATABASE, code); - } - info!("--- prepare db"); { let res = self.create_database(mt, &tenant, db_name, "eng1").await?; assert_eq!(1, *res.db_id, "first database id is 1"); } + let db_id = DatabaseId::new(1u64); + info!("--- create 2 tables: tb1 tb2"); { // Table schema with metadata(due to serde issue). @@ -5984,10 +5983,10 @@ impl SchemaApiTestSuite { info!("--- get_tables"); { - let res = mt.list_tables(ListTableReq::new(&tenant, db_name)).await?; + let res = mt.list_tables(ListTableReq::new(&tenant, db_id)).await?; assert_eq!(tb_ids.len(), res.len()); - assert_eq!(tb_ids[0], res[0].ident.table_id); - assert_eq!(tb_ids[1], res[1].ident.table_id); + assert_eq!(tb_ids[0], res[0].1.table_id); + assert_eq!(tb_ids[1], res[1].1.table_id); } } @@ -6035,7 +6034,7 @@ impl SchemaApiTestSuite { info!("--- get_tables"); { let res = mt - .list_tables(ListTableReq::new(&util.tenant(), util.db_name())) + .list_tables(ListTableReq::new(&util.tenant(), util.db_id())) .await?; assert_eq!(n, res.len()); } @@ -7039,6 +7038,7 @@ impl SchemaApiTestSuite { let db_name = "db1"; let mut tb_ids = vec![]; + let db_id; { let req = CreateDatabaseReq { @@ -7052,6 +7052,7 @@ impl SchemaApiTestSuite { let res = node_a.create_database(req).await; info!("create database res: {:?}", res); assert!(res.is_ok()); + db_id = res.unwrap().db_id; let tables = vec!["tb1", "tb2"]; let schema = Arc::new(TableSchema::new(vec![TableField::new( @@ -7090,16 +7091,14 @@ impl SchemaApiTestSuite { info!("--- list tables from node_b"); { - let res = node_b - .list_tables(ListTableReq::new(&tenant, db_name)) - .await; + let res = node_b.list_tables(ListTableReq::new(&tenant, db_id)).await; debug!("get table list: {:?}", res); let res = res?; assert_eq!(2, res.len(), "table list len is 2"); - assert_eq!(tb_ids[0], res[0].ident.table_id, "tb1 id"); - assert_eq!("tb1", res[0].name, "tb1.name is tb1"); - assert_eq!(tb_ids[1], res[1].ident.table_id, "tb2 id"); - assert_eq!("tb2", res[1].name, "tb2.name is tb2"); + assert_eq!(tb_ids[0], res[0].1.table_id, "tb1 id"); + assert_eq!("tb1", res[0].0, "tb1.name is tb1"); + assert_eq!(tb_ids[1], res[1].1.table_id, "tb2 id"); + assert_eq!("tb2", res[1].0, "tb2.name is tb2"); } Ok(()) @@ -7621,6 +7620,10 @@ where MT: SchemaApi + kvapi::AsKVApi self.db_name.clone() } + fn db_id(&self) -> DatabaseId { + DatabaseId::new(self.db_id) + } + fn tbl_name(&self) -> String { self.table_name.clone() } diff --git a/src/meta/api/src/util.rs b/src/meta/api/src/util.rs index ade4dfcb0832..90debd670d70 100644 --- a/src/meta/api/src/util.rs +++ b/src/meta/api/src/util.rs @@ -14,7 +14,6 @@ use std::any::type_name; use std::fmt::Display; -use std::sync::Arc; use std::time::Duration; use databend_common_base::display::display_slice::DisplaySliceExt; @@ -22,15 +21,8 @@ use databend_common_meta_app::app_error::AppError; use databend_common_meta_app::app_error::UnknownDatabase; use databend_common_meta_app::app_error::UnknownDatabaseId; use databend_common_meta_app::app_error::UnknownTable; -use databend_common_meta_app::app_error::UnknownTableId; use databend_common_meta_app::primitive::Id; use databend_common_meta_app::schema::database_name_ident::DatabaseNameIdent; -use databend_common_meta_app::schema::DBIdTableName; -use databend_common_meta_app::schema::DatabaseType; -use databend_common_meta_app::schema::TableId; -use databend_common_meta_app::schema::TableIdent; -use databend_common_meta_app::schema::TableInfo; -use databend_common_meta_app::schema::TableMeta; use databend_common_meta_app::schema::TableNameIdent; use databend_common_meta_kvapi::kvapi; use databend_common_meta_kvapi::kvapi::DirName; @@ -427,106 +419,3 @@ pub fn assert_table_exist( format!("{}: {}", ctx, name_ident), ))? } - -/// Return OK if a `table_id->*` exists by checking the seq. -/// -/// Otherwise returns [`AppError::UnknownTableId`] error -pub fn assert_table_id_exist( - seq: u64, - table_id: &TableId, - ctx: impl Display, -) -> Result<(), AppError> { - if seq > 0 { - return Ok(()); - } - - debug!(seq = seq, table_id :? =(table_id); "does not exist"); - - Err(UnknownTableId::new( - table_id.table_id, - format!("{}: {}", ctx, table_id), - ))? -} - -/// Get `table_meta_seq` and [`TableMeta`] by [`TableId`], -/// or return [`AppError::UnknownTableId`] error wrapped in a [`KVAppError`] if not found. -pub async fn get_table_by_id_or_err( - kv_api: &(impl kvapi::KVApi + ?Sized), - table_id: &TableId, - ctx: impl Display + Copy, -) -> Result<(u64, TableMeta), KVAppError> { - let (seq, table_meta): (_, Option) = get_pb_value(kv_api, table_id).await?; - assert_table_id_exist(seq, table_id, ctx)?; - - let table_meta = table_meta.unwrap(); - - debug!( - ident :% =(table_id), - table_meta :? =(&table_meta); - "{}", - ctx - ); - - Ok((seq, table_meta)) -} - -pub async fn get_tableinfos_by_ids( - kv_api: &(impl kvapi::KVApi + ?Sized), - name_ids: Vec<(DBIdTableName, u64)>, - tenant_dbname: &DatabaseNameIdent, - db_type: DatabaseType, -) -> Result>, KVAppError> { - let mut res = Vec::with_capacity(name_ids.len()); - let chunk_size = DEFAULT_MGET_SIZE; - - for chunk in name_ids.chunks(chunk_size) { - let id_idents = chunk.iter().map(|(_, id)| TableId { table_id: *id }); - let seq_metas = kv_api.get_pb_values_vec(id_idents).await?; - - for ((name_ident, id), seq_meta) in chunk.iter().zip(seq_metas) { - let table_name = &name_ident.table_name; - let Some(seq_meta) = seq_meta else { - continue; - }; - - let tb_info = TableInfo { - ident: TableIdent { - table_id: *id, - seq: seq_meta.seq, - }, - desc: format!("'{}'.'{}'", tenant_dbname.database_name(), table_name), - meta: seq_meta.data, - name: table_name.clone(), - db_type: db_type.clone(), - catalog_info: Default::default(), - }; - res.push(Arc::new(tb_info)); - } - } - - Ok(res) -} - -pub async fn list_tables_from_unshare_db( - kv_api: &(impl kvapi::KVApi + ?Sized), - db_id: u64, - tenant_dbname: &DatabaseNameIdent, -) -> Result>, KVAppError> { - // List tables by tenant, db_id, table_name. - - let dbid_tbname = DBIdTableName { - db_id, - // Use empty name to scan all tables - table_name: "".to_string(), - }; - - let (dbid_tbnames, ids) = list_u64_value(kv_api, &dbid_tbname).await?; - - get_tableinfos_by_ids( - kv_api, - dbid_tbnames.into_iter().zip(ids).collect(), - tenant_dbname, - DatabaseType::NormalDB, - ) - .await -} diff --git a/src/meta/app/src/schema/table.rs b/src/meta/app/src/schema/table.rs index 9030b7e5ddd7..9a0384a9f5ec 100644 --- a/src/meta/app/src/schema/table.rs +++ b/src/meta/app/src/schema/table.rs @@ -36,6 +36,7 @@ use maplit::hashmap; use super::CatalogInfo; use super::CreateOption; +use super::DatabaseId; use crate::schema::database_name_ident::DatabaseNameIdent; use crate::storage::StorageParams; use crate::tenant::Tenant; @@ -891,21 +892,15 @@ impl GetTableReq { #[derive(Clone, Debug, PartialEq, Eq)] pub struct ListTableReq { - pub inner: DatabaseNameIdent, -} - -impl Deref for ListTableReq { - type Target = DatabaseNameIdent; - - fn deref(&self) -> &Self::Target { - &self.inner - } + pub tenant: Tenant, + pub database_id: DatabaseId, } impl ListTableReq { - pub fn new(tenant: &Tenant, db_name: impl ToString) -> ListTableReq { + pub fn new(tenant: &Tenant, database_id: DatabaseId) -> ListTableReq { ListTableReq { - inner: DatabaseNameIdent::new(tenant, db_name), + tenant: tenant.clone(), + database_id, } } } diff --git a/src/query/service/src/databases/default/default_database.rs b/src/query/service/src/databases/default/default_database.rs index 3fa8a35fe404..86c55a877990 100644 --- a/src/query/service/src/databases/default/default_database.rs +++ b/src/query/service/src/databases/default/default_database.rs @@ -71,12 +71,31 @@ impl DefaultDatabase { } async fn list_table_infos(&self) -> Result>> { - let table_infos = self + let db_id = self.db_info.database_id; + + let name_id_metas = self .ctx .meta - .list_tables(ListTableReq::new(self.get_tenant(), self.get_db_name())) + .list_tables(ListTableReq::new(self.get_tenant(), db_id)) .await?; + let table_infos = name_id_metas + .iter() + .map(|(name, id, meta)| { + Arc::new(TableInfo { + ident: TableIdent { + table_id: id.table_id, + seq: meta.seq(), + }, + desc: format!("'{}'.'{}'", self.get_db_name(), name), + name: name.to_string(), + meta: meta.data.clone(), + db_type: DatabaseType::NormalDB, + catalog_info: Default::default(), + }) + }) + .collect::>(); + if self.ctx.disable_table_info_refresh { Ok(table_infos) } else { @@ -194,7 +213,10 @@ impl Database for DefaultDatabase { let mut dropped = self .ctx .meta - .get_tables_history(ListTableReq::new(self.get_tenant(), self.get_db_name())) + .get_tables_history( + ListTableReq::new(self.get_tenant(), self.db_info.database_id), + self.get_db_name(), + ) .await? .into_iter() .filter(|i| i.meta.drop_on.is_some())