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

fix: ensure atomicity in create_lock_revision #16907

Merged
merged 3 commits into from
Nov 22, 2024
Merged
Changes from 2 commits
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
56 changes: 38 additions & 18 deletions src/meta/api/src/schema_api_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ use databend_common_meta_app::app_error::UnknownTableId;
use databend_common_meta_app::app_error::ViewAlreadyExists;
use databend_common_meta_app::data_mask::MaskPolicyTableIdListIdent;
use databend_common_meta_app::id_generator::IdGenerator;
use databend_common_meta_app::id_generator::IdGeneratorValue;
use databend_common_meta_app::schema::catalog_id_ident::CatalogId;
use databend_common_meta_app::schema::catalog_name_ident::CatalogNameIdentRaw;
use databend_common_meta_app::schema::database_name_ident::DatabaseNameIdent;
Expand Down Expand Up @@ -2738,30 +2739,49 @@ impl<KV: kvapi::KVApi<Error = MetaError> + ?Sized> SchemaApi for KV {
&self,
req: CreateLockRevReq,
) -> Result<CreateLockRevReply, KVAppError> {
debug!(req :? =(&req); "SchemaApi: {}", func_name!());
let ctx = func_name!();
debug!(req :? =(&req); "SchemaApi: {}", ctx);

let lock_key = &req.lock_key;
let id_generator = IdGenerator::table_lock_id();

let revision = fetch_id(self, IdGenerator::table_lock_id()).await?;
let key = lock_key.gen_key(revision);
let mut trials = txn_backoff(None, ctx);
loop {
trials.next().unwrap()?.await;

let lock_meta = LockMeta {
user: req.user.clone(),
node: req.node.clone(),
query_id: req.query_id.clone(),
created_on: Utc::now(),
acquired_on: None,
lock_type: lock_key.lock_type(),
extra_info: lock_key.get_extra_info(),
};
let current_rev = self.get_seq(&id_generator).await?;
let revision = current_rev + 1;
let key = lock_key.gen_key(revision);
let lock_meta = LockMeta {
user: req.user.clone(),
node: req.node.clone(),
query_id: req.query_id.clone(),
created_on: Utc::now(),
acquired_on: None,
lock_type: lock_key.lock_type(),
extra_info: lock_key.get_extra_info(),
};

// Revision is unique. if it presents, consider it as success.
// Thus, we could just ignore create result
let _ = self
.crud_try_insert(&key, lock_meta, Some(req.ttl), || Ok::<(), Infallible>(()))
.await?;
let condition = vec![
txn_cond_seq(&id_generator, Eq, current_rev),
// assumes lock are absent.
txn_cond_seq(&key, Eq, 0),
];
let if_then = vec![
txn_op_put(&id_generator, b"".to_vec()),
txn_op_put_pb(&key, &lock_meta, Some(req.ttl))?,
];
let txn_req = TxnRequest {
condition,
if_then,
else_then: vec![],
};
let (succ, _responses) = send_txn(self, txn_req).await?;

Ok(CreateLockRevReply { revision })
if succ {
return Ok(CreateLockRevReply { revision });
}
}
}

#[logcall::logcall]
Expand Down
Loading