Skip to content

Commit bc36a9d

Browse files
authored
refactor: simplify mask policy storage structure (#18836)
* refactor: simplify masking policy storage structure 1. discard TableMeta::column_mask_policy 2. RowAccessPolicyColumnMap rename to SecurityPolicyColumnMap 3. old version use MaskPolicyTableIdListIdent store policy and table id reference. In pr use MaskPolicyTableIdIdent. * delete mask_policy_table_id.proto * fix flaky test * drop Option
1 parent 28f2fc5 commit bc36a9d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+926
-462
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/common/exception/src/exception_code.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,8 @@ build_exceptions! {
289289
AlterTableError(1132),
290290
/// Constraint error
291291
ConstraintError(1133),
292+
/// Unknown row policy
293+
UnknownMaskPolicy(1134),
292294
}
293295

294296
// Sequence Errors [1124-1126, 3101-3102]

src/meta/api/src/data_mask_api.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use databend_common_meta_app::data_mask::CreateDatamaskReq;
1717
use databend_common_meta_app::data_mask::DataMaskId;
1818
use databend_common_meta_app::data_mask::DataMaskNameIdent;
1919
use databend_common_meta_app::data_mask::DatamaskMeta;
20+
use databend_common_meta_app::tenant::Tenant;
2021
use databend_common_meta_types::MetaError;
2122
use databend_common_meta_types::SeqV;
2223

@@ -40,4 +41,10 @@ pub trait DatamaskApi: Send + Sync {
4041
&self,
4142
name_ident: &DataMaskNameIdent,
4243
) -> Result<Option<SeqV<DatamaskMeta>>, MetaError>;
44+
45+
async fn get_data_mask_by_id(
46+
&self,
47+
tenant: &Tenant,
48+
policy_id: u64,
49+
) -> Result<Option<SeqV<DatamaskMeta>>, MetaError>;
4350
}

src/meta/api/src/data_mask_api_impl.rs

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use databend_common_meta_app::data_mask::MaskPolicyTableIdListIdent;
2323
use databend_common_meta_app::data_mask::MaskpolicyTableIdList;
2424
use databend_common_meta_app::id_generator::IdGenerator;
2525
use databend_common_meta_app::schema::CreateOption;
26-
use databend_common_meta_app::schema::TableId;
26+
use databend_common_meta_app::tenant::Tenant;
2727
use databend_common_meta_app::KeyWithTenant;
2828
use databend_common_meta_kvapi::kvapi;
2929
use databend_common_meta_types::MetaError;
@@ -40,7 +40,6 @@ use crate::txn_backoff::txn_backoff;
4040
use crate::txn_condition_util::txn_cond_eq_seq;
4141
use crate::txn_core_util::send_txn;
4242
use crate::txn_core_util::txn_delete_exact;
43-
use crate::txn_core_util::txn_replace_exact;
4443
use crate::txn_op_builder_util::txn_op_put_pb;
4544

4645
/// DatamaskApi is implemented upon kvapi::KVApi.
@@ -82,8 +81,6 @@ impl<KV: kvapi::KVApi<Error = MetaError>> DatamaskApi for KV {
8281

8382
txn_delete_exact(&mut txn, &id_ident, seq_meta.seq);
8483

85-
clear_table_column_mask_policy(self, name_ident, &mut txn).await?;
86-
8784
curr_seq = seq_id.seq;
8885
}
8986
};
@@ -111,8 +108,9 @@ impl<KV: kvapi::KVApi<Error = MetaError>> DatamaskApi for KV {
111108
let id_list = MaskpolicyTableIdList::default();
112109
txn.condition.push(txn_cond_eq_seq(name_ident, curr_seq));
113110
txn.if_then.extend(vec![
114-
txn_op_put_pb(name_ident, &id, None)?, // name -> db_id
115-
txn_op_put_pb(&id_ident, &meta, None)?, // id -> meta
111+
txn_op_put_pb(name_ident, &id, None)?, // name -> db_id
112+
txn_op_put_pb(&id_ident, &meta, None)?, // id -> meta
113+
// TODO: Tentative retention for compatibility MaskPolicyTableIdListIdent related logic. It can be directly deleted later
116114
txn_op_put_pb(&id_list_key, &id_list, None)?, // data mask name -> id_list
117115
]);
118116

@@ -143,7 +141,6 @@ impl<KV: kvapi::KVApi<Error = MetaError>> DatamaskApi for KV {
143141
let mut trials = txn_backoff(None, func_name!());
144142
loop {
145143
trials.next().unwrap()?.await;
146-
147144
let mut txn = TxnRequest::default();
148145

149146
let res = self.get_id_and_value(name_ident).await?;
@@ -157,9 +154,8 @@ impl<KV: kvapi::KVApi<Error = MetaError>> DatamaskApi for KV {
157154

158155
txn_delete_exact(&mut txn, name_ident, seq_id.seq);
159156
txn_delete_exact(&mut txn, &id_ident, seq_meta.seq);
160-
157+
// TODO: Tentative retention for compatibility MaskPolicyTableIdListIdent related logic. It can be directly deleted later
161158
clear_table_column_mask_policy(self, name_ident, &mut txn).await?;
162-
163159
let (succ, _responses) = send_txn(self, txn).await?;
164160
debug!(succ = succ;"{}", func_name!());
165161

@@ -179,6 +175,20 @@ impl<KV: kvapi::KVApi<Error = MetaError>> DatamaskApi for KV {
179175

180176
Ok(res.map(|(_, seq_meta)| seq_meta))
181177
}
178+
179+
async fn get_data_mask_by_id(
180+
&self,
181+
tenant: &Tenant,
182+
policy_id: u64,
183+
) -> Result<Option<SeqV<DatamaskMeta>>, MetaError> {
184+
debug!(req :? =(policy_id); "DatamaskApi: {}", func_name!());
185+
186+
let id = DataMaskId::new(policy_id);
187+
let id_ident = DataMaskIdIdent::new_generic(tenant, id);
188+
189+
let res = self.get_pb(&id_ident).await?;
190+
Ok(res)
191+
}
182192
}
183193

184194
async fn clear_table_column_mask_policy(
@@ -195,30 +205,5 @@ async fn clear_table_column_mask_policy(
195205
};
196206

197207
txn_delete_exact(txn, &id_list_key, seq_id_list.seq);
198-
199-
// remove mask policy from table meta
200-
for table_id in seq_id_list.data.id_list.into_iter() {
201-
let tbid = TableId { table_id };
202-
203-
let seq_meta = kv_api.get_pb(&tbid).await?;
204-
205-
let Some(seq_meta) = seq_meta else {
206-
continue;
207-
};
208-
209-
let (seq, mut meta) = (seq_meta.seq, seq_meta.data);
210-
211-
if let Some(column_mask_policy) = meta.column_mask_policy {
212-
let new_column_mask_policy = column_mask_policy
213-
.into_iter()
214-
.filter(|(_, name)| name != name_ident.name())
215-
.collect();
216-
217-
meta.column_mask_policy = Some(new_column_mask_policy);
218-
219-
txn_replace_exact(txn, &tbid, seq, &meta)?;
220-
}
221-
}
222-
223208
Ok(())
224209
}

0 commit comments

Comments
 (0)