From 31469808c1b1976ed2bc519ce238973b663b2cf8 Mon Sep 17 00:00:00 2001 From: Chojan Shang Date: Tue, 5 Jul 2022 10:41:59 +0800 Subject: [PATCH 1/3] chore(common/meta/store): init common-meta-store crate --- Cargo.lock | 4 ++++ Cargo.toml | 1 + common/meta/store/Cargo.toml | 19 +++++++++++++++++++ common/meta/store/src/lib.rs | 13 +++++++++++++ common/meta/store/tests/it/main.rs | 13 +++++++++++++ 5 files changed, 50 insertions(+) create mode 100644 common/meta/store/Cargo.toml create mode 100644 common/meta/store/src/lib.rs create mode 100644 common/meta/store/tests/it/main.rs diff --git a/Cargo.lock b/Cargo.lock index c99848d72e63..5cab2fb61f6c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1366,6 +1366,10 @@ dependencies = [ "tempfile", ] +[[package]] +name = "common-meta-store" +version = "0.1.0" + [[package]] name = "common-meta-types" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 8a03ea878390..d47a3cb1558c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,7 @@ members = [ "common/meta/embedded", "common/meta/raft-store", "common/meta/sled-store", + "common/meta/store", "common/meta/types", "common/metrics", "common/planners", diff --git a/common/meta/store/Cargo.toml b/common/meta/store/Cargo.toml new file mode 100644 index 000000000000..fdaf48983372 --- /dev/null +++ b/common/meta/store/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "common-meta-store" +version = "0.1.0" +description = "MetaStore is impl with either a local embedded meta store, or a grpc-client of metasrv" +authors = ["Databend Authors "] +license = "Apache-2.0" +publish = false +edition = "2021" + +[lib] +doctest = false +test = false + +[dependencies] +# Workspace dependencies + +# Crates.io dependencies + +[dev-dependencies] diff --git a/common/meta/store/src/lib.rs b/common/meta/store/src/lib.rs new file mode 100644 index 000000000000..a6ee3d6a2656 --- /dev/null +++ b/common/meta/store/src/lib.rs @@ -0,0 +1,13 @@ +// 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. diff --git a/common/meta/store/tests/it/main.rs b/common/meta/store/tests/it/main.rs new file mode 100644 index 000000000000..a6ee3d6a2656 --- /dev/null +++ b/common/meta/store/tests/it/main.rs @@ -0,0 +1,13 @@ +// 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. From 4b21e64b8c27ba38d446e1b6ab6d56c9f96725ae Mon Sep 17 00:00:00 2001 From: Chojan Shang Date: Tue, 5 Jul 2022 11:03:35 +0800 Subject: [PATCH 2/3] chore(common/meta/store): copy query common meta store --- Cargo.lock | 10 +++ common/meta/store/Cargo.toml | 8 +++ common/meta/store/src/lib.rs | 104 +++++++++++++++++++++++++++++ common/meta/store/tests/it/main.rs | 13 ---- 4 files changed, 122 insertions(+), 13 deletions(-) delete mode 100644 common/meta/store/tests/it/main.rs diff --git a/Cargo.lock b/Cargo.lock index 5cab2fb61f6c..9e9ae01184fc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1369,6 +1369,16 @@ dependencies = [ [[package]] name = "common-meta-store" version = "0.1.0" +dependencies = [ + "async-trait 0.1.56 (registry+https://github.com/rust-lang/crates.io-index)", + "common-exception", + "common-grpc", + "common-meta-api", + "common-meta-embedded", + "common-meta-grpc", + "common-meta-types", + "common-tracing", +] [[package]] name = "common-meta-types" diff --git a/common/meta/store/Cargo.toml b/common/meta/store/Cargo.toml index fdaf48983372..18673d1fc9a6 100644 --- a/common/meta/store/Cargo.toml +++ b/common/meta/store/Cargo.toml @@ -13,7 +13,15 @@ test = false [dependencies] # Workspace dependencies +common-exception = { path = "../../exception" } +common-grpc = { path = "../../grpc" } +common-meta-api = { path = "../api" } +common-meta-embedded = { path = "../embedded" } +common-meta-grpc = { path = "../grpc" } +common-meta-types = { path = "../types" } +common-tracing = { path = "../../tracing" } # Crates.io dependencies +async-trait = "0.1.56" [dev-dependencies] diff --git a/common/meta/store/src/lib.rs b/common/meta/store/src/lib.rs index a6ee3d6a2656..619c553b5e68 100644 --- a/common/meta/store/src/lib.rs +++ b/common/meta/store/src/lib.rs @@ -11,3 +11,107 @@ // 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 std::sync::Arc; + +use common_exception::Result; +use common_grpc::RpcClientConf; +use common_meta_api::KVApi; +use common_meta_embedded::MetaEmbedded; +use common_meta_grpc::ClientHandle; +use common_meta_grpc::MetaGrpcClient; +use common_meta_types::GetKVReply; +use common_meta_types::ListKVReply; +use common_meta_types::MGetKVReply; +use common_meta_types::MetaError; +use common_meta_types::TxnReply; +use common_meta_types::TxnRequest; +use common_meta_types::UpsertKVReply; +use common_meta_types::UpsertKVReq; +use common_tracing::tracing; + +#[derive(Clone)] +pub struct MetaStoreProvider { + rpc_conf: RpcClientConf, +} + +/// MetaStore is impl with either a local embedded meta store, or a grpc-client of metasrv +#[derive(Clone)] +pub enum MetaStore { + L(Arc), + R(Arc), +} + +impl MetaStore { + pub fn arc(self) -> Arc { + Arc::new(self) + } + + pub fn is_local(&self) -> bool { + match self { + MetaStore::L(_) => true, + MetaStore::R(_) => false, + } + } +} + +#[async_trait::async_trait] +impl KVApi for MetaStore { + async fn upsert_kv(&self, act: UpsertKVReq) -> std::result::Result { + match self { + MetaStore::L(x) => x.upsert_kv(act).await, + MetaStore::R(x) => x.upsert_kv(act).await, + } + } + + async fn get_kv(&self, key: &str) -> std::result::Result { + match self { + MetaStore::L(x) => x.get_kv(key).await, + MetaStore::R(x) => x.get_kv(key).await, + } + } + + async fn mget_kv(&self, key: &[String]) -> std::result::Result { + match self { + MetaStore::L(x) => x.mget_kv(key).await, + MetaStore::R(x) => x.mget_kv(key).await, + } + } + + async fn prefix_list_kv(&self, prefix: &str) -> std::result::Result { + match self { + MetaStore::L(x) => x.prefix_list_kv(prefix).await, + MetaStore::R(x) => x.prefix_list_kv(prefix).await, + } + } + + async fn transaction(&self, txn: TxnRequest) -> std::result::Result { + match self { + MetaStore::L(x) => x.transaction(txn).await, + MetaStore::R(x) => x.transaction(txn).await, + } + } +} + +impl MetaStoreProvider { + pub fn new(rpc_conf: RpcClientConf) -> Self { + MetaStoreProvider { rpc_conf } + } + + pub async fn try_get_meta_store(&self) -> Result { + if self.rpc_conf.local_mode() { + tracing::info!( + conf = debug(&self.rpc_conf), + "use embedded meta, data will be removed when process exits" + ); + + // NOTE: This can only be used for test: data will be removed when program quit. + let meta_store = MetaEmbedded::get_meta().await?; + Ok(MetaStore::L(meta_store)) + } else { + tracing::info!(conf = debug(&self.rpc_conf), "use remote meta"); + let client = MetaGrpcClient::try_new(&self.rpc_conf)?; + Ok(MetaStore::R(client)) + } + } +} diff --git a/common/meta/store/tests/it/main.rs b/common/meta/store/tests/it/main.rs deleted file mode 100644 index a6ee3d6a2656..000000000000 --- a/common/meta/store/tests/it/main.rs +++ /dev/null @@ -1,13 +0,0 @@ -// 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. From ed3fc08f138f7790676b4cf954b240f20b811925 Mon Sep 17 00:00:00 2001 From: Chojan Shang Date: Tue, 5 Jul 2022 11:16:40 +0800 Subject: [PATCH 3/3] refactor(query): migrate to common-meta-store --- Cargo.lock | 1 + query/Cargo.toml | 1 + query/src/catalogs/default/catalog_context.rs | 2 +- query/src/catalogs/default/mutable_catalog.rs | 2 +- query/src/clusters/cluster.rs | 2 +- query/src/common/meta/meta_store.rs | 118 ------------------ query/src/common/meta/mod.rs | 18 --- query/src/common/mod.rs | 3 - query/src/users/user_api.rs | 2 +- 9 files changed, 6 insertions(+), 143 deletions(-) delete mode 100644 query/src/common/meta/meta_store.rs delete mode 100644 query/src/common/meta/mod.rs diff --git a/Cargo.lock b/Cargo.lock index 9e9ae01184fc..f367b7b9b3a5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2055,6 +2055,7 @@ dependencies = [ "common-meta-grpc", "common-meta-raft-store", "common-meta-sled-store", + "common-meta-store", "common-meta-types", "common-metrics", "common-planners", diff --git a/query/Cargo.toml b/query/Cargo.toml index 443cab0ddc9e..39d3799ce120 100644 --- a/query/Cargo.toml +++ b/query/Cargo.toml @@ -47,6 +47,7 @@ common-meta-embedded = { path = "../common/meta/embedded" } common-meta-grpc = { path = "../common/meta/grpc" } common-meta-raft-store = { path = "../common/meta/raft-store" } common-meta-sled-store = { path = "../common/meta/sled-store" } +common-meta-store = { path = "../common/meta/store" } common-meta-types = { path = "../common/meta/types" } common-metrics = { path = "../common/metrics" } common-planners = { path = "../common/planners" } diff --git a/query/src/catalogs/default/catalog_context.rs b/query/src/catalogs/default/catalog_context.rs index 031eaae91429..fa180eb637a9 100644 --- a/query/src/catalogs/default/catalog_context.rs +++ b/query/src/catalogs/default/catalog_context.rs @@ -16,8 +16,8 @@ use std::sync::Arc; use common_base::infallible::RwLock; use common_datablocks::InMemoryData; +use common_meta_store::MetaStore; -use crate::common::MetaStore; use crate::databases::DatabaseFactory; use crate::storages::StorageFactory; diff --git a/query/src/catalogs/default/mutable_catalog.rs b/query/src/catalogs/default/mutable_catalog.rs index e6f55872f4d4..2275bd1517fe 100644 --- a/query/src/catalogs/default/mutable_catalog.rs +++ b/query/src/catalogs/default/mutable_catalog.rs @@ -48,12 +48,12 @@ use common_meta_app::schema::UpdateTableMetaReply; use common_meta_app::schema::UpdateTableMetaReq; use common_meta_app::schema::UpsertTableOptionReply; use common_meta_app::schema::UpsertTableOptionReq; +use common_meta_store::MetaStoreProvider; use common_meta_types::MetaId; use common_tracing::tracing; use super::catalog_context::CatalogContext; use crate::catalogs::catalog::Catalog; -use crate::common::MetaStoreProvider; use crate::databases::Database; use crate::databases::DatabaseContext; use crate::databases::DatabaseFactory; diff --git a/query/src/clusters/cluster.rs b/query/src/clusters/cluster.rs index 5d73be48193c..b42b565fdc17 100644 --- a/query/src/clusters/cluster.rs +++ b/query/src/clusters/cluster.rs @@ -34,6 +34,7 @@ use common_grpc::ConnectionFactory; use common_management::ClusterApi; use common_management::ClusterMgr; use common_meta_api::KVApi; +use common_meta_store::MetaStoreProvider; use common_meta_types::NodeInfo; use common_metrics::label_counter_with_val_and_labels; use common_tracing::tracing; @@ -46,7 +47,6 @@ use rand::thread_rng; use rand::Rng; use crate::api::FlightClient; -use crate::common::MetaStoreProvider; use crate::Config; pub struct ClusterDiscovery { diff --git a/query/src/common/meta/meta_store.rs b/query/src/common/meta/meta_store.rs deleted file mode 100644 index 6dcdd1dd3133..000000000000 --- a/query/src/common/meta/meta_store.rs +++ /dev/null @@ -1,118 +0,0 @@ -// 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 std::sync::Arc; - -use common_exception::Result; -use common_grpc::RpcClientConf; -use common_meta_api::KVApi; -use common_meta_embedded::MetaEmbedded; -use common_meta_grpc::ClientHandle; -use common_meta_grpc::MetaGrpcClient; -use common_meta_types::GetKVReply; -use common_meta_types::ListKVReply; -use common_meta_types::MGetKVReply; -use common_meta_types::MetaError; -use common_meta_types::TxnReply; -use common_meta_types::TxnRequest; -use common_meta_types::UpsertKVReply; -use common_meta_types::UpsertKVReq; -use common_tracing::tracing; - -#[derive(Clone)] -pub struct MetaStoreProvider { - rpc_conf: RpcClientConf, -} - -/// MetaStore is impl with either a local embedded meta store, or a grpc-client of metasrv -#[derive(Clone)] -pub enum MetaStore { - L(Arc), - R(Arc), -} - -impl MetaStore { - pub fn arc(self) -> Arc { - Arc::new(self) - } - - pub fn is_local(&self) -> bool { - match self { - MetaStore::L(_) => true, - MetaStore::R(_) => false, - } - } -} - -#[async_trait::async_trait] -impl KVApi for MetaStore { - async fn upsert_kv(&self, act: UpsertKVReq) -> std::result::Result { - match self { - MetaStore::L(x) => x.upsert_kv(act).await, - MetaStore::R(x) => x.upsert_kv(act).await, - } - } - - async fn get_kv(&self, key: &str) -> std::result::Result { - match self { - MetaStore::L(x) => x.get_kv(key).await, - MetaStore::R(x) => x.get_kv(key).await, - } - } - - async fn mget_kv(&self, key: &[String]) -> std::result::Result { - match self { - MetaStore::L(x) => x.mget_kv(key).await, - MetaStore::R(x) => x.mget_kv(key).await, - } - } - - async fn prefix_list_kv(&self, prefix: &str) -> std::result::Result { - match self { - MetaStore::L(x) => x.prefix_list_kv(prefix).await, - MetaStore::R(x) => x.prefix_list_kv(prefix).await, - } - } - - async fn transaction(&self, txn: TxnRequest) -> std::result::Result { - match self { - MetaStore::L(x) => x.transaction(txn).await, - MetaStore::R(x) => x.transaction(txn).await, - } - } -} - -impl MetaStoreProvider { - pub fn new(rpc_conf: RpcClientConf) -> Self { - MetaStoreProvider { rpc_conf } - } - - pub async fn try_get_meta_store(&self) -> Result { - if self.rpc_conf.local_mode() { - tracing::info!( - conf = debug(&self.rpc_conf), - "use embedded meta, data will be removed when process exits" - ); - - // NOTE: This can only be used for test: data will be removed when program quit. - let meta_store = MetaEmbedded::get_meta().await?; - Ok(MetaStore::L(meta_store)) - } else { - tracing::info!(conf = debug(&self.rpc_conf), "use remote meta"); - let client = MetaGrpcClient::try_new(&self.rpc_conf)?; - Ok(MetaStore::R(client)) - } - } -} diff --git a/query/src/common/meta/mod.rs b/query/src/common/meta/mod.rs deleted file mode 100644 index 10cd1b6d7d0c..000000000000 --- a/query/src/common/meta/mod.rs +++ /dev/null @@ -1,18 +0,0 @@ -// 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. - -mod meta_store; - -pub use meta_store::MetaStore; -pub use meta_store::MetaStoreProvider; diff --git a/query/src/common/mod.rs b/query/src/common/mod.rs index bfa1dd6a0ba3..af942ea361a8 100644 --- a/query/src/common/mod.rs +++ b/query/src/common/mod.rs @@ -16,11 +16,8 @@ pub mod context_function; mod evaluator; mod expression_evaluator; mod hashtable; -mod meta; pub mod service; pub use evaluator::*; pub use expression_evaluator::ExpressionEvaluator; pub use hashtable::*; -pub use meta::MetaStore; -pub use meta::MetaStoreProvider; diff --git a/query/src/users/user_api.rs b/query/src/users/user_api.rs index bce524e37492..8b034706e9f4 100644 --- a/query/src/users/user_api.rs +++ b/query/src/users/user_api.rs @@ -26,8 +26,8 @@ use common_management::UdfMgr; use common_management::UserApi; use common_management::UserMgr; use common_meta_api::KVApi; +use common_meta_store::MetaStoreProvider; -use crate::common::MetaStoreProvider; use crate::Config; pub struct UserApiProvider {