Skip to content

Commit cab23ee

Browse files
authored
refactor: add Tenant to ShareConsumer (#15142)
1 parent 4e894ae commit cab23ee

File tree

5 files changed

+33
-31
lines changed

5 files changed

+33
-31
lines changed

src/meta/api/src/share_api_impl.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ impl<KV: kvapi::KVApi<Error = MetaError>> ShareApi for KV {
327327
for account in req.accounts.iter() {
328328
if !share_meta.has_account(account) {
329329
add_share_account_keys.push(ShareConsumer {
330-
tenant: account.clone(),
330+
tenant: Tenant::new_or_err(account, "add_share_tenants")?,
331331
share_id,
332332
});
333333
}
@@ -358,7 +358,7 @@ impl<KV: kvapi::KVApi<Error = MetaError>> ShareApi for KV {
358358
condition.push(txn_cond_seq(share_account_key, Eq, 0));
359359

360360
let share_account_meta = ShareAccountMeta::new(
361-
share_account_key.tenant.clone(),
361+
share_account_key.tenant.name().to_string(),
362362
share_id,
363363
req.share_on,
364364
);
@@ -368,7 +368,7 @@ impl<KV: kvapi::KVApi<Error = MetaError>> ShareApi for KV {
368368
serialize_struct(&share_account_meta)?,
369369
)); /* (account, share_id) -> share_account_meta */
370370

371-
share_meta.add_account(share_account_key.tenant.clone());
371+
share_meta.add_account(share_account_key.tenant.name().to_string());
372372
}
373373
if_then.push(txn_op_put(&id_key, serialize_struct(&share_meta)?)); /* (share_id) -> share_meta */
374374

@@ -438,7 +438,7 @@ impl<KV: kvapi::KVApi<Error = MetaError>> ShareApi for KV {
438438
}
439439
if share_meta.has_account(account) {
440440
let share_account_key = ShareConsumer {
441-
tenant: account.clone(),
441+
tenant: Tenant::new_or_err(account, "remove_share_tenants")?,
442442
share_id,
443443
};
444444

@@ -485,7 +485,7 @@ impl<KV: kvapi::KVApi<Error = MetaError>> ShareApi for KV {
485485

486486
if_then.push(txn_op_del(&share_account_key_and_seq.0)); // del (account, share_id)
487487

488-
share_meta.del_account(&share_account_key_and_seq.0.tenant);
488+
share_meta.del_account(share_account_key_and_seq.0.tenant.name());
489489
}
490490
if_then.push(txn_op_put(&id_key, serialize_struct(&share_meta)?)); /* (share_id) -> share_meta */
491491

@@ -1358,7 +1358,7 @@ async fn get_outbound_share_tenants_by_name(
13581358
let mut accounts = vec![];
13591359
for account in share_meta.get_accounts() {
13601360
let share_account_key = ShareConsumer {
1361-
tenant: account.clone(),
1361+
tenant: Tenant::new_or_err(&account, "get_outbound_share_tenants_by_name")?,
13621362
share_id,
13631363
};
13641364

@@ -1652,7 +1652,7 @@ async fn drop_accounts_granted_from_share(
16521652
// get all accounts seq from share_meta
16531653
for account in share_meta.get_accounts() {
16541654
let share_account_key = ShareConsumer {
1655-
tenant: account.clone(),
1655+
tenant: Tenant::new_or_err(&account, "drop_accounts_granted_from_share")?,
16561656
share_id,
16571657
};
16581658
let ret = get_share_account_meta_or_err(

src/meta/api/src/share_api_test_suite.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ async fn is_all_share_data_removed(
8282

8383
for account in share_meta.get_accounts() {
8484
let share_account_key = ShareConsumer {
85-
tenant: account.clone(),
85+
tenant: Tenant::new_or_err(account, "is_all_share_data_removed")?,
8686
share_id,
8787
};
8888
let res = get_share_account_meta_or_err(kv_api, &share_account_key, "").await;
@@ -581,7 +581,7 @@ impl ShareApiTestSuite {
581581

582582
// get and check share account meta
583583
let share_account_name = ShareConsumer {
584-
tenant: account.to_string(),
584+
tenant: Tenant::new_or_err(account, "share_add_remove_account")?,
585585
share_id,
586586
};
587587
let (_share_account_meta_seq, share_account_meta) =
@@ -707,7 +707,7 @@ impl ShareApiTestSuite {
707707

708708
// check share account meta has been removed
709709
let share_account_name = ShareConsumer {
710-
tenant: account2.to_string(),
710+
tenant: Tenant::new_or_err(account2, "share_add_remove_account")?,
711711
share_id,
712712
};
713713
let res = get_share_account_meta_or_err(mt.as_kv_api(), &share_account_name, "").await;
@@ -730,7 +730,7 @@ impl ShareApiTestSuite {
730730

731731
// check share account meta has been removed
732732
let share_account_name = ShareConsumer {
733-
tenant: account.to_string(),
733+
tenant: Tenant::new_or_err(account, "share_add_remove_account")?,
734734
share_id,
735735
};
736736
let res = get_share_account_meta_or_err(mt.as_kv_api(), &share_account_name, "").await;

src/meta/api/src/util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@ fn share_account_meta_has_to_exist(
638638

639639
Err(KVAppError::AppError(AppError::UnknownShareAccounts(
640640
UnknownShareAccounts::new(
641-
&[name_key.tenant.clone()],
641+
&[name_key.tenant.name().to_string()],
642642
name_key.share_id,
643643
format!("{}: {}", msg, name_key),
644644
),

src/meta/app/src/share/share.rs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ use crate::schema::TableInfo;
3131
use crate::schema::TableMeta;
3232
use crate::share::ShareEndpointIdent;
3333
use crate::tenant::Tenant;
34+
use crate::tenant::ToTenant;
3435

3536
// serde is required by `DatabaseType`
3637
/// A share that is created by `tenant` and is named with `share_name`.
@@ -48,15 +49,24 @@ impl Display for ShareNameIdent {
4849

4950
/// The share consuming key describes that the `tenant`, who is a consumer of a shared object,
5051
/// which is created by another tenant and is identified by `share_id`.
51-
#[derive(Clone, Debug, Default, Eq, PartialEq)]
52+
#[derive(Clone, Debug, Eq, PartialEq)]
5253
pub struct ShareConsumer {
53-
pub tenant: String,
54+
pub tenant: Tenant,
5455
pub share_id: u64,
5556
}
5657

58+
impl ShareConsumer {
59+
pub fn new(tenant: impl ToTenant, share_id: u64) -> Self {
60+
Self {
61+
tenant: tenant.to_tenant(),
62+
share_id,
63+
}
64+
}
65+
}
66+
5767
impl Display for ShareConsumer {
5868
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
59-
write!(f, "'{}'/'{}'", self.tenant, self.share_id)
69+
write!(f, "'{}'/'{}'", self.tenant.name(), self.share_id)
6070
}
6171
}
6272

@@ -608,7 +618,7 @@ impl ShareMeta {
608618
self.accounts.insert(account);
609619
}
610620

611-
pub fn del_account(&mut self, account: &String) {
621+
pub fn del_account(&mut self, account: &str) {
612622
self.accounts.remove(account);
613623
}
614624

@@ -885,33 +895,32 @@ mod kvapi_key_impl {
885895

886896
/// It belongs to a tenant
887897
fn parent(&self) -> Option<String> {
888-
Some(Tenant::new(&self.tenant).to_string_key())
898+
Some(kvapi::Key::to_string_key(&self.tenant))
889899
}
890900

891901
fn to_string_key(&self) -> String {
892902
if self.share_id != 0 {
893903
kvapi::KeyBuilder::new_prefixed(Self::PREFIX)
894-
.push_str(&self.tenant)
904+
.push_str(self.tenant.name())
895905
.push_u64(self.share_id)
896906
.done()
897907
} else {
898908
kvapi::KeyBuilder::new_prefixed(Self::PREFIX)
899-
.push_str(&self.tenant)
909+
.push_str(self.tenant.name())
900910
.done()
901911
}
902912
}
903913

904914
fn from_str_key(s: &str) -> Result<Self, kvapi::KeyError> {
905915
let mut p = kvapi::KeyParser::new_prefixed(s, Self::PREFIX)?;
906916

907-
let account = p.next_str()?;
917+
let tenant = p.next_nonempty()?;
908918
let share_id = p.next_u64()?;
909919
p.done()?;
910920

911-
Ok(ShareConsumer {
912-
tenant: account,
913-
share_id,
914-
})
921+
let tenant = Tenant::new_nonempty(tenant);
922+
923+
Ok(ShareConsumer { tenant, share_id })
915924
}
916925
}
917926

src/meta/app/src/tenant/tenant.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,6 @@ pub struct Tenant {
2828
}
2929

3030
impl Tenant {
31-
// #[deprecated]
32-
pub fn new(tenant: impl ToString) -> Self {
33-
Self {
34-
tenant: tenant.to_string(),
35-
}
36-
}
37-
3831
pub fn new_or_err(tenant: impl ToString, ctx: impl Display) -> Result<Self, TenantIsEmpty> {
3932
let non_empty =
4033
NonEmptyString::new(tenant.to_string()).map_err(|_e| TenantIsEmpty::new(ctx))?;

0 commit comments

Comments
 (0)