Skip to content

Commit 88a884e

Browse files
authored
refactor: remove count-table for tenant. It is not used at all (#15134)
1 parent b721dd0 commit 88a884e

File tree

14 files changed

+13
-427
lines changed

14 files changed

+13
-427
lines changed

src/meta/api/src/schema_api.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
use std::sync::Arc;
1616

1717
use databend_common_meta_app::schema::CatalogInfo;
18-
use databend_common_meta_app::schema::CountTablesReply;
19-
use databend_common_meta_app::schema::CountTablesReq;
2018
use databend_common_meta_app::schema::CreateCatalogReply;
2119
use databend_common_meta_app::schema::CreateCatalogReq;
2220
use databend_common_meta_app::schema::CreateDatabaseReply;
@@ -272,8 +270,6 @@ pub trait SchemaApi: Send + Sync {
272270
req: GcDroppedTableReq,
273271
) -> Result<GcDroppedTableResp, KVAppError>;
274272

275-
async fn count_tables(&self, req: CountTablesReq) -> Result<CountTablesReply, KVAppError>;
276-
277273
async fn list_lock_revisions(
278274
&self,
279275
req: ListLockRevReq,

src/meta/api/src/schema_api_impl.rs

Lines changed: 0 additions & 193 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,6 @@ use databend_common_meta_app::schema::CatalogIdToName;
6565
use databend_common_meta_app::schema::CatalogInfo;
6666
use databend_common_meta_app::schema::CatalogMeta;
6767
use databend_common_meta_app::schema::CatalogNameIdent;
68-
use databend_common_meta_app::schema::CountTablesKey;
69-
use databend_common_meta_app::schema::CountTablesReply;
70-
use databend_common_meta_app::schema::CountTablesReq;
7168
use databend_common_meta_app::schema::CreateCatalogReply;
7269
use databend_common_meta_app::schema::CreateCatalogReq;
7370
use databend_common_meta_app::schema::CreateDatabaseReply;
@@ -204,7 +201,6 @@ use databend_common_meta_types::TxnGetRequest;
204201
use databend_common_meta_types::TxnGetResponse;
205202
use databend_common_meta_types::TxnOp;
206203
use databend_common_meta_types::TxnRequest;
207-
use databend_common_meta_types::UpsertKV;
208204
use futures::TryStreamExt;
209205
use log::debug;
210206
use log::error;
@@ -1521,7 +1517,6 @@ impl<KV: kvapi::KVApi<Error = MetaError> + ?Sized> SchemaApi for KV {
15211517

15221518
debug!(req :? =(&req); "SchemaApi: {}", func_name!());
15231519

1524-
let tenant = req.name_ident.tenant();
15251520
let tenant_dbname_tbname = &req.name_ident;
15261521
let tenant_dbname = req.name_ident.db_name_ident();
15271522

@@ -1554,15 +1549,11 @@ impl<KV: kvapi::KVApi<Error = MetaError> + ?Sized> SchemaApi for KV {
15541549
table_name: req.name_ident.table_name.clone(),
15551550
};
15561551

1557-
// fixed
1558-
let key_table_count = CountTablesKey::new(tenant.name());
1559-
15601552
// The keys of values to re-fetch for every retry in this txn.
15611553
let keys = vec![
15621554
key_dbid.to_string_key(),
15631555
key_dbid_tbname.to_string_key(),
15641556
key_table_id_list.to_string_key(),
1565-
key_table_count.to_string_key(),
15661557
];
15671558

15681559
// Initialize required key-values
@@ -1574,21 +1565,6 @@ impl<KV: kvapi::KVApi<Error = MetaError> + ?Sized> SchemaApi for KV {
15741565
.collect::<Vec<_>>()
15751566
};
15761567

1577-
// Initialize table count if needed
1578-
assert_eq!(data[3].key, key_table_count.to_string_key());
1579-
if data[3].value.is_none() {
1580-
init_table_count(self, &key_table_count).await?;
1581-
1582-
// Re-fetch
1583-
data = {
1584-
let values = self.mget_kv(&keys).await?;
1585-
keys.iter()
1586-
.zip(values.into_iter())
1587-
.map(|(k, v)| TxnGetResponse::new(k, v.map(pb::SeqV::from)))
1588-
.collect::<Vec<_>>()
1589-
};
1590-
}
1591-
15921568
let mut trials = txn_backoff(None, func_name!());
15931569
loop {
15941570
trials.next().unwrap()?.await;
@@ -1630,7 +1606,6 @@ impl<KV: kvapi::KVApi<Error = MetaError> + ?Sized> SchemaApi for KV {
16301606

16311607
let mut condition = vec![];
16321608
let mut if_then = vec![];
1633-
let mut tb_count = None;
16341609

16351610
let opt = {
16361611
let d = data.remove(0);
@@ -1660,7 +1635,6 @@ impl<KV: kvapi::KVApi<Error = MetaError> + ?Sized> SchemaApi for KV {
16601635
db_id.data,
16611636
false,
16621637
false,
1663-
&mut tb_count,
16641638
&mut condition,
16651639
&mut if_then,
16661640
)
@@ -1680,14 +1654,6 @@ impl<KV: kvapi::KVApi<Error = MetaError> + ?Sized> SchemaApi for KV {
16801654
v.unwrap_or_default()
16811655
};
16821656

1683-
let mut tb_count = {
1684-
let d = data.remove(0);
1685-
let (k, v) = deserialize_id_get_response::<CountTablesKey>(d)?;
1686-
assert_eq!(key_table_count, k);
1687-
1688-
v.unwrap_or_default()
1689-
};
1690-
16911657
// Table id is unique and does not need to re-generate in every loop.
16921658
if key_table_id.is_none() {
16931659
let id = fetch_id(self, IdGenerator::table_id()).await?;
@@ -1733,15 +1699,6 @@ impl<KV: kvapi::KVApi<Error = MetaError> + ?Sized> SchemaApi for KV {
17331699
txn_op_put(&key_table_id_to_name, serialize_struct(&key_dbid_tbname)?), /* __fd_table_id_to_name/db_id/table_name -> DBIdTableName */
17341700
]);
17351701

1736-
// tb_id_seq is 0 means that is a create operation, in this case need to update table count
1737-
if tb_id_seq == 0 {
1738-
tb_count.data.0 += 1;
1739-
// update table count atomically
1740-
condition.push(txn_cond_seq(&key_table_count, Eq, tb_count.seq));
1741-
// _fd_table_count/tenant -> tb_count
1742-
if_then.push(txn_op_put(&key_table_count, serialize_u64(tb_count.data)?));
1743-
}
1744-
17451702
let txn_req = TxnRequest {
17461703
condition,
17471704
if_then,
@@ -1811,9 +1768,6 @@ impl<KV: kvapi::KVApi<Error = MetaError> + ?Sized> SchemaApi for KV {
18111768

18121769
let tenant_dbname_tbname = &req.name_ident;
18131770
let tenant_dbname = req.name_ident.db_name_ident();
1814-
let mut tbcount_found = false;
1815-
let mut tb_count = 0;
1816-
let mut tb_count_seq;
18171771

18181772
let mut trials = txn_backoff(None, func_name!());
18191773
loop {
@@ -1883,22 +1837,6 @@ impl<KV: kvapi::KVApi<Error = MetaError> + ?Sized> SchemaApi for KV {
18831837
let tbid = TableId { table_id };
18841838
let (tb_meta_seq, tb_meta): (_, Option<TableMeta>) = get_pb_value(self, &tbid).await?;
18851839

1886-
// get current table count from _fd_table_count/tenant
1887-
let tb_count_key = CountTablesKey {
1888-
tenant: tenant_dbname.tenant.name().to_string(),
1889-
};
1890-
(tb_count_seq, tb_count) = {
1891-
let (seq, count) = get_u64_value(self, &tb_count_key).await?;
1892-
if seq > 0 {
1893-
(seq, count)
1894-
} else if !tbcount_found {
1895-
// only count_tables for the first time.
1896-
tbcount_found = true;
1897-
(0, count_tables(self, &tb_count_key).await?)
1898-
} else {
1899-
(0, tb_count)
1900-
}
1901-
};
19021840
// add drop_on time on table meta
19031841
// (db_id, table_name) -> table_id
19041842

@@ -1928,8 +1866,6 @@ impl<KV: kvapi::KVApi<Error = MetaError> + ?Sized> SchemaApi for KV {
19281866
txn_cond_seq(&dbid_tbname, Eq, tb_id_seq),
19291867
// table is not changed
19301868
txn_cond_seq(&tbid, Eq, tb_meta_seq),
1931-
// update table count atomically
1932-
txn_cond_seq(&tb_count_key, Eq, tb_count_seq),
19331869
],
19341870
if_then: vec![
19351871
// Changing a table in a db has to update the seq of db_meta,
@@ -1938,7 +1874,6 @@ impl<KV: kvapi::KVApi<Error = MetaError> + ?Sized> SchemaApi for KV {
19381874
txn_op_put(&dbid_tbname, serialize_u64(table_id)?), /* (tenant, db_id, tb_name) -> tb_id */
19391875
// txn_op_put(&dbid_tbname_idlist, serialize_struct(&tb_id_list)?)?, // _fd_table_id_list/db_id/table_name -> tb_id_list
19401876
txn_op_put(&tbid, serialize_struct(&tb_meta)?), /* (tenant, db_id, tb_id) -> tb_meta */
1941-
txn_op_put(&tb_count_key, serialize_u64(tb_count + 1)?), /* _fd_table_count/tenant -> tb_count */
19421877
],
19431878
else_then: vec![],
19441879
};
@@ -2564,7 +2499,6 @@ impl<KV: kvapi::KVApi<Error = MetaError> + ?Sized> SchemaApi for KV {
25642499
let table_id = req.tb_id;
25652500
debug!(req :? =(&table_id); "SchemaApi: {}", func_name!());
25662501

2567-
let mut tb_count = None;
25682502
let tenant = &req.tenant;
25692503

25702504
let mut trials = txn_backoff(None, func_name!());
@@ -2582,7 +2516,6 @@ impl<KV: kvapi::KVApi<Error = MetaError> + ?Sized> SchemaApi for KV {
25822516
req.db_id,
25832517
req.if_exists,
25842518
true,
2585-
&mut tb_count,
25862519
&mut condition,
25872520
&mut if_then,
25882521
)
@@ -3584,65 +3517,6 @@ impl<KV: kvapi::KVApi<Error = MetaError> + ?Sized> SchemaApi for KV {
35843517
Ok(GcDroppedTableResp {})
35853518
}
35863519

3587-
/// Get the count of tables for one tenant.
3588-
///
3589-
/// Accept tenant name and returns the count of tables for the tenant.
3590-
///
3591-
/// It get the count from kv space first,
3592-
/// if not found, it will compute the count by listing all databases and table ids.
3593-
#[logcall::logcall("debug")]
3594-
#[minitrace::trace]
3595-
async fn count_tables(&self, req: CountTablesReq) -> Result<CountTablesReply, KVAppError> {
3596-
debug!(req :? =(&req); "SchemaApi: {}", func_name!());
3597-
3598-
let key = CountTablesKey {
3599-
tenant: req.tenant.to_string_key(),
3600-
};
3601-
3602-
let count = loop {
3603-
let (seq, cnt) = {
3604-
// get the count from kv space first
3605-
let (seq, c) = get_u64_value(self, &key).await?;
3606-
if seq > 0 {
3607-
// if seq > 0, we can get the count directly
3608-
break c;
3609-
}
3610-
3611-
// if not, we should compute the count from by listing all databases and table ids
3612-
3613-
// this line of codes will only be executed once,
3614-
// because if `send_txn` failed, it means another txn will put the count value into the kv space,
3615-
// and then the next loop will get the count value through `get_u64_value`.
3616-
(0, count_tables(self, &key).await?)
3617-
};
3618-
3619-
let key = CountTablesKey {
3620-
tenant: req.tenant.clone(),
3621-
};
3622-
3623-
let txn_req = TxnRequest {
3624-
// table count should not be changed.
3625-
condition: vec![txn_cond_seq(&key, Eq, seq)],
3626-
if_then: vec![txn_op_put(&key, serialize_u64(cnt)?)],
3627-
else_then: vec![],
3628-
};
3629-
3630-
let (succ, _) = send_txn(self, txn_req).await?;
3631-
// if txn succeeds, count can be returned safely
3632-
if succ {
3633-
break cnt;
3634-
}
3635-
};
3636-
3637-
debug!(
3638-
tenant = &req.tenant,
3639-
count = count;
3640-
"count tables for a tenant"
3641-
);
3642-
3643-
Ok(CountTablesReply { count })
3644-
}
3645-
36463520
#[minitrace::trace]
36473521
async fn list_lock_revisions(
36483522
&self,
@@ -4275,7 +4149,6 @@ async fn construct_drop_table_txn_operations(
42754149
db_id: u64,
42764150
if_exists: bool,
42774151
if_delete: bool,
4278-
tb_count_opt: &mut Option<u64>,
42794152
condition: &mut Vec<TxnCondition>,
42804153
if_then: &mut Vec<TxnOp>,
42814154
) -> Result<(Option<(Vec<ShareSpec>, Vec<ShareTableInfoMap>)>, u64), KVAppError> {
@@ -4322,24 +4195,6 @@ async fn construct_drop_table_txn_operations(
43224195
};
43234196
}
43244197

4325-
// get current table count from _fd_table_count/<tenant>
4326-
let tb_count_key = CountTablesKey {
4327-
tenant: tenant.name().to_string(),
4328-
};
4329-
let (tb_count_seq, tb_count) = {
4330-
let (seq, count) = get_u64_value(kv_api, &tb_count_key).await?;
4331-
if seq > 0 {
4332-
(seq, count)
4333-
} else if tb_count_opt.is_none() {
4334-
// only count_tables for the first time.
4335-
let count = count_tables(kv_api, &tb_count_key).await?;
4336-
*tb_count_opt = Some(count);
4337-
(0, count)
4338-
} else {
4339-
(0, tb_count_opt.unwrap())
4340-
}
4341-
};
4342-
43434198
let (db_meta_seq, db_meta) = get_db_by_id_or_err(kv_api, db_id, "drop_table_by_id").await?;
43444199

43454200
// cannot operate on shared database
@@ -4385,11 +4240,6 @@ async fn construct_drop_table_txn_operations(
43854240
condition.push(txn_cond_seq(&dbid_tbname, Eq, tb_id_seq));
43864241
// (db_id, tb_name) -> tb_id
43874242
if_then.push(txn_op_del(&dbid_tbname));
4388-
4389-
// update table count atomically
4390-
condition.push(txn_cond_seq(&tb_count_key, Eq, tb_count_seq));
4391-
// _fd_table_count/tenant -> tb_count
4392-
if_then.push(txn_op_put(&tb_count_key, serialize_u64(tb_count - 1)?));
43934243
}
43944244

43954245
// remove table from share
@@ -4771,49 +4621,6 @@ fn table_has_to_not_exist(
47714621
}
47724622
}
47734623

4774-
/// Initialize count of tables for one tenant.
4775-
async fn init_table_count(
4776-
kv_api: &(impl kvapi::KVApi<Error = MetaError> + ?Sized),
4777-
key: &CountTablesKey,
4778-
) -> Result<(), KVAppError> {
4779-
let n = count_tables(kv_api, key).await?;
4780-
4781-
kv_api
4782-
.upsert_kv(UpsertKV::insert(key.to_string_key(), &serialize_u64(n)?))
4783-
.await?;
4784-
4785-
Ok(())
4786-
}
4787-
4788-
/// Get the count of tables for one tenant by listing databases and table ids.
4789-
///
4790-
/// It returns (seq, `u64` value).
4791-
/// If the count value is not in the kv space, (0, `u64` value) is returned.
4792-
async fn count_tables(
4793-
kv_api: &(impl kvapi::KVApi<Error = MetaError> + ?Sized),
4794-
key: &CountTablesKey,
4795-
) -> Result<u64, KVAppError> {
4796-
// For backward compatibility:
4797-
// If the table count of a tenant is not found in kv space,,
4798-
// we should compute the count by listing all tables of the tenant.
4799-
let databases = kv_api
4800-
.list_databases(ListDatabaseReq {
4801-
tenant: Tenant::new_or_err(&key.tenant, func_name!())?,
4802-
filter: None,
4803-
})
4804-
.await?;
4805-
let mut count = 0;
4806-
for db in databases.into_iter() {
4807-
let dbid_tbname = DBIdTableName {
4808-
db_id: db.ident.db_id,
4809-
table_name: "".to_string(),
4810-
};
4811-
let (_, ids) = list_u64_value(kv_api, &dbid_tbname).await?;
4812-
count += ids.len() as u64;
4813-
}
4814-
Ok(count)
4815-
}
4816-
48174624
async fn get_share_table_info_map(
48184625
kv_api: &(impl kvapi::KVApi<Error = MetaError> + ?Sized),
48194626
table_meta: &TableMeta,

0 commit comments

Comments
 (0)