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

[improvement] store client factory #1869 #1876

Merged
merged 3 commits into from Sep 19, 2021

Conversation

dantengsky
Copy link
Member

@dantengsky dantengsky commented Sep 18, 2021

I hereby agree to the terms of the CLA available at: https://databend.rs/policies/cla/

Summary

fix:

Changelog

  • Improvement

Summary

  • Crate common-fligts has been renamed to store-api-sdk

    • To emphasize that one can use this crate to utilize store apis.
    • A factory is provided like this:
     #[derive(Clone)]
     pub struct StoreApiProvider {
       ...
     }
    
     impl StoreApiProvider {
        pub fn new(conf: impl Into<StoreClientConf>) -> Self
    
        pub async fn try_get_meta_client(&self) -> Result<Arc<dyn MetaApi>> 
        pub async fn try_get_kv_client(&self) -> Result<Arc<dyn KVApi>>
        pub async fn try_get_storage_client(&self) -> Result<Arc<dyn StorageApi>>
     }
    

    since common-xxxx are not supposed to depend on crate query::configs::Config, we can not use Config directly in store-api-sdk.

    A converter form Config to StoreClientConf

     impl From<&Config> for StoreClientConf ...
    

    is provide in crate query.

  • The mock of KVStoreApi -- LocalKVStore is provided by crate store-api-sdk

    • LocalKVStore has been changed to a simple in-memory implementation
      The idea of re-use code of store-state-machine is great, but, currently,

      1. there are circular dependencies if we place StateMachine based LocalKVStore into crate store-api-sdk
      2. also, make common-xxx depend on crate store is not a good idea IMO
      3. let's rollback to the former implementations after the dependency issues resolved
    • The mock is provided as a part of a non-default feature of crate store-api-sdk
      crates that depend on it, like common-management should declare an extra dev dependency in cargo.toml, like this

            [dev-dependencies]
             common-store-api-sdk= {path = "../store-api-sdk", features= ["mocks"]}
      

      It has been added to the cargo.toml of crate common-management

    • management::namespace::local_kv_store_test.rs now using common_store_api_sdk::mocks::LocalKVStore

  • UserMgr Api changed

    to runtime dispatching style:

     pub struct NamespaceMgr {
       kv_api: Arc<dyn KVApi>,
     }
    
     impl NamespaceMgr {
       pub fn new(kv_api: Arc<dyn KVApi>) -> Self {
         NamespaceMgr { kv_api }
       }
      ...
     }
    

    Since, the provider method signature fn try_get_kv_client(&self) -> Result<Arc<dyn KVApi>> indicates that the caller site does not know the concrete type of which implemented KVApi.
    Previous static dispatching style of using Store Apis can also be implemented but seems to be not user-friendly (to compose the components).
    Same modification goes to UserMgr

  • Store Apis slightly changed

    trait StoreApis are removed since we are going to split the Store services into multiple services.

    KV/Meta/Apis no longer take &mut self, since they are all wrapped in Arc while being passed around.
    Instead, the flight client is cloned(which is lightweight, according to this), before calling the flights API.

Related Issues

Fixes #1869

Test Plan

Unit Tests

Stateless Tests

@databend-bot
Copy link
Member

Thanks for the contribution!
I have applied any labels matching special text in your PR Changelog.

Please review the labels and make any necessary changes.

@drmingdrmer
Copy link
Member

drmingdrmer commented Sep 18, 2021

I see the unit test failure. It is caused by RPC timeout.

Running the test test_meta_node_add_database takes 7 seconds on my laptop.
For CI it takes one minute and a half.

We need to find a way to deal with these time sensitive tests. 🤔

https://github.com/datafuselabs/databend/pull/1876/checks?check_run_id=3638769792

@codecov-commenter
Copy link

codecov-commenter commented Sep 18, 2021

Codecov Report

Merging #1876 (8140471) into master (b2f51d2) will increase coverage by 0%.
The diff coverage is 68%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master   #1876    +/-   ##
=======================================
  Coverage      70%     70%            
=======================================
  Files         639     631     -8     
  Lines       38280   37814   -466     
=======================================
- Hits        27088   26805   -283     
+ Misses      11192   11009   -183     
Impacted Files Coverage Δ
common/management/src/namespace/namespace_api.rs 46% <ø> (ø)
common/store-api-sdk/src/common.rs 100% <ø> (ø)
common/store-api-sdk/src/dns_resolver.rs 80% <ø> (ø)
common/store-api-sdk/src/store_client.rs 84% <0%> (ø)
...on/store-api/src/data_block_apis/data_block_api.rs 93% <ø> (ø)
common/store-api/src/kv_apis/kv_api.rs 87% <0%> (ø)
common/store-api/src/meta_apis/meta_api.rs 94% <0%> (ø)
query/src/api/rpc_service_test.rs 92% <ø> (ø)
...ry/src/catalogs/impls/catalog/metastore_catalog.rs 75% <0%> (+<1%) ⬆️
...atalogs/impls/meta_backends/remote_meta_backend.rs 0% <0%> (ø)
... and 75 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b2f51d2...8140471. Read the comment docs.

@dantengsky dantengsky changed the title refacotrying [improvement] store client factory #1869 Sep 18, 2021
@BohuTANG
Copy link
Member

/lgtm

👍

@databend-bot
Copy link
Member

Approved! Thank you for the PR @dantengsky

@databend-bot
Copy link
Member

CI Passed
Reviewer Approved
Let's Merge

5 similar comments
@databend-bot
Copy link
Member

CI Passed
Reviewer Approved
Let's Merge

@databend-bot
Copy link
Member

CI Passed
Reviewer Approved
Let's Merge

@databend-bot
Copy link
Member

CI Passed
Reviewer Approved
Let's Merge

@databend-bot
Copy link
Member

CI Passed
Reviewer Approved
Let's Merge

@databend-bot
Copy link
Member

CI Passed
Reviewer Approved
Let's Merge

Comment on lines +74 to +117
impl KVApi for LocalKVStore {
async fn upsert_kv(
&self,
key: &str,
matcher: MatchSeq,
value: Option<Vec<u8>>,
value_meta: Option<KVMeta>,
) -> Result<UpsertKVActionResult> {
let (prev, result) = {
let mut inner = self.inner.write();

if let Some((seq, v)) = inner.kv.get(key) {
let r = matcher.match_seq(*seq);
let prev = (*seq, v.clone());
if r.is_ok() {
// seq match
let mut new_v = v.clone();
new_v.meta = value_meta;
new_v.value = value.unwrap_or_default();
let new_seq = inner.next_seq();
let result = (new_seq, new_v);
inner.kv.insert(key.to_owned(), result.clone());
(Some(prev), Some(result))
} else {
// seq not match,
// store state machine use a pair of identical values to indicates
// that nothing changed, we simulate it.
let c = prev.clone();
(Some(prev), Some(c))
}
} else {
// key not found, let's insert
let new_v = KVValue {
meta: value_meta,
value: value.unwrap_or_default(),
};
let new_seq = inner.next_seq();
let result = (new_seq, new_v);
inner.kv.insert(key.to_owned(), result.clone());
(None, Some(result))
}
};
Ok(UpsertKVActionResult { prev, result })
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A re-implementation of the meta-kv is not guaranteed to be consistent with the remote meta service.
Somebody who is not aware of there are two implementations may have changed one of them without updating the other one accordingly.
That's why a local kv should use a embedded StateMachine.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change log:

a. there are circular dependencies if we place StateMachine based LocalKVStore into crate store-api-sdk
b. also, make common-xxx depend on crate store is not a good idea IMO
c. let's rollback to the former implementations after the dependency issues resolved

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We both know about the reasons. A better way is to solve the dependency cycle first.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not know what exactly the deps issues are before working on this pr.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw,maybe we should also try harder to NOT leak internal concepts like StateMachine to common-xxx directly

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

btw,maybe we should also try harder to NOT leak internal concepts like StateMachine to common-xxx directly

It seems we need a wrapper api of StateMachine?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[improvement] store client factory
5 participants