-
Notifications
You must be signed in to change notification settings - Fork 728
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add KVPbCrudApi as abstraction layer for commonly used CRUD…
… operations (#16479) * chore: refactor rename_table() * chore: refactor SchemaApi::create_index() * chore: simplify SchemaApi::get_table_meta_history() * refactor: add KVPbCrudApi as abstraction layer for commonly used CRUD operations * chore: remove unused append_update_stream_meta_requests() * chore: fixup lint
- Loading branch information
1 parent
ed39081
commit 207ee9e
Showing
6 changed files
with
222 additions
and
206 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Copyright 2021 Datafuse Labs | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use databend_common_meta_kvapi::kvapi; | ||
use databend_common_meta_kvapi::kvapi::KVApi; | ||
use databend_common_meta_types::Change; | ||
use databend_common_meta_types::MatchSeq; | ||
use databend_common_meta_types::MetaError; | ||
use databend_common_meta_types::SeqV; | ||
use databend_common_meta_types::SeqValue; | ||
use databend_common_meta_types::With; | ||
use databend_common_proto_conv::FromToProto; | ||
use fastrace::func_name; | ||
use log::debug; | ||
|
||
use crate::kv_pb_api::KVPbApi; | ||
use crate::kv_pb_api::UpsertPB; | ||
use crate::meta_txn_error::MetaTxnError; | ||
use crate::txn_backoff::txn_backoff; | ||
|
||
/// [`KVPbCrudApi`] provide generic meta-service access pattern implementations for `name -> value` mapping. | ||
/// | ||
/// `K` is the key type for name. | ||
/// `K::ValueType` is the value type. | ||
#[tonic::async_trait] | ||
pub trait KVPbCrudApi<K>: KVApi<Error = MetaError> | ||
where | ||
K: kvapi::Key + Clone + Send + Sync + 'static, | ||
K::ValueType: FromToProto + Clone + Send + Sync + 'static, | ||
{ | ||
/// Update or insert a `name -> value` mapping. | ||
/// | ||
/// The `update` function is called with the previous value and should output the updated to write back. | ||
/// - Ok(Some(x)): write back `x`. | ||
/// - Ok(None): cancel the update. | ||
/// - Err(e): return error. | ||
/// | ||
/// This function returns an embedded result, | ||
/// - the outer result is for underlying kvapi error, | ||
/// - the inner result is for business logic error. | ||
async fn upsert_with<E>( | ||
&self, | ||
name_ident: &K, | ||
update: impl Fn(Option<SeqV<K::ValueType>>) -> Result<Option<K::ValueType>, E> + Send, | ||
) -> Result<Result<Change<K::ValueType>, E>, MetaTxnError> { | ||
debug!(name_ident :? =name_ident; "KVPbCrudApi: {}", func_name!()); | ||
|
||
let mut trials = txn_backoff(None, func_name!()); | ||
loop { | ||
trials.next().unwrap()?.await; | ||
|
||
let seq_meta = self.get_pb(name_ident).await?; | ||
let seq = seq_meta.seq(); | ||
|
||
let updated = match update(seq_meta.clone()) { | ||
Ok(Some(x)) => x, | ||
Ok(None) => return Ok(Ok(Change::new(seq_meta.clone(), seq_meta))), | ||
Err(err) => return Ok(Err(err)), | ||
}; | ||
|
||
let transition = self | ||
.upsert_pb( | ||
&UpsertPB::insert(name_ident.clone(), updated).with(MatchSeq::Exact(seq)), | ||
) | ||
.await?; | ||
|
||
if transition.is_changed() { | ||
return Ok(Ok(transition)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl<K, T> KVPbCrudApi<K> for T | ||
where | ||
T: KVApi<Error = MetaError> + ?Sized, | ||
K: kvapi::Key + Clone + Send + Sync + 'static, | ||
K::ValueType: FromToProto + Clone + Send + Sync + 'static, | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.