Skip to content

Commit 873844b

Browse files
authored
Merge pull request #9118 from RinChanNOWWW/global-config
chore: make inner `Config` in crate `databend-query` as a global singeleton.
2 parents 4534798 + c1b569f commit 873844b

38 files changed

+320
-169
lines changed

Cargo.lock

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/binaries/query/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ async fn main_entrypoint() -> Result<()> {
124124
let hostname = conf.query.clickhouse_http_handler_host.clone();
125125
let listening = format!("{}:{}", hostname, conf.query.clickhouse_http_handler_port);
126126

127-
let mut srv = HttpHandler::create(HttpHandlerKind::Clickhouse, conf.clone())?;
127+
let mut srv = HttpHandler::create(HttpHandlerKind::Clickhouse)?;
128128
let listening = srv.start(listening.parse()?).await?;
129129
shutdown_handle.add_service(srv);
130130

@@ -140,7 +140,7 @@ async fn main_entrypoint() -> Result<()> {
140140
let hostname = conf.query.http_handler_host.clone();
141141
let listening = format!("{}:{}", hostname, conf.query.http_handler_port);
142142

143-
let mut srv = HttpHandler::create(HttpHandlerKind::Query, conf.clone())?;
143+
let mut srv = HttpHandler::create(HttpHandlerKind::Query)?;
144144
let listening = srv.start(listening.parse()?).await?;
145145
shutdown_handle.add_service(srv);
146146

src/query/catalog/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ test = false
1010

1111
[dependencies]
1212
common-base = { path = "../../common/base" }
13-
common-config = { path = "../config" }
1413
common-datablocks = { path = "../datablocks" }
1514
common-datavalues = { path = "../datavalues" }
1615
common-exception = { path = "../../common/exception" }

src/query/catalog/src/table_context.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use std::time::SystemTime;
1919

2020
use common_base::base::Progress;
2121
use common_base::base::ProgressValues;
22-
use common_config::Config;
2322
use common_datablocks::DataBlock;
2423
use common_exception::Result;
2524
use common_functions::scalars::FunctionContext;
@@ -77,7 +76,6 @@ pub trait TableContext: Send + Sync {
7776
fn get_current_catalog(&self) -> String;
7877
fn get_aborting(&self) -> Arc<AtomicBool>;
7978
fn get_current_database(&self) -> String;
80-
fn get_config(&self) -> Config;
8179
fn get_current_user(&self) -> Result<UserInfo>;
8280
fn get_current_role(&self) -> Option<RoleInfo>;
8381
fn get_fuse_version(&self) -> String;

src/query/config/src/global.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright 2022 Datafuse Labs.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
use std::sync::Arc;
16+
17+
use common_base::base::Singleton;
18+
use common_exception::Result;
19+
use once_cell::sync::OnceCell;
20+
21+
use crate::Config;
22+
23+
pub struct GlobalConfig;
24+
25+
static GLOBAL_CONFIG: OnceCell<Singleton<Arc<Config>>> = OnceCell::new();
26+
27+
impl GlobalConfig {
28+
pub fn init(config: Config, v: Singleton<Arc<Config>>) -> Result<()> {
29+
v.init(Arc::new(config))?;
30+
GLOBAL_CONFIG.set(v).ok();
31+
Ok(())
32+
}
33+
34+
pub fn instance() -> Arc<Config> {
35+
match GLOBAL_CONFIG.get() {
36+
None => panic!("GlobalConfig is not init"),
37+
Some(config) => config.get(),
38+
}
39+
}
40+
}

src/query/config/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,18 @@
1818
///
1919
/// - [`inner::Config`] which will be exposed as [`crate::Config`] will be used in all business logic.
2020
/// - [`outer_v0::Config`] is the outer config for [`inner::Config`] which will be exposed to end-users.
21+
/// - [`global::GlobalConfig`] is a global config singleton of [`crate::Config`].
2122
///
2223
/// It's safe to refactor [`inner::Config`] in anyway, as long as it satisfied the following traits
2324
///
2425
/// - `TryInto<inner::Config> for outer_v0::Config`
2526
/// - `From<inner::Config> for outer_v0::Config`
27+
mod global;
2628
mod inner;
2729
mod outer_v0;
2830
mod version;
2931

32+
pub use global::GlobalConfig;
3033
pub use inner::CatalogConfig;
3134
pub use inner::CatalogHiveConfig;
3235
pub use inner::Config;

src/query/config/tests/it/global.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright 2022 Datafuse Labs.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
use std::cell::UnsafeCell;
16+
use std::sync::Arc;
17+
18+
use common_base::base::SingletonImpl;
19+
use common_config::Config;
20+
use common_config::GlobalConfig;
21+
use common_exception::Result;
22+
use once_cell::sync::OnceCell;
23+
24+
struct ConfigSingleton {
25+
config: UnsafeCell<Option<Arc<Config>>>,
26+
}
27+
28+
unsafe impl Send for ConfigSingleton {}
29+
30+
unsafe impl Sync for ConfigSingleton {}
31+
32+
static GLOBAL: OnceCell<Arc<ConfigSingleton>> = OnceCell::new();
33+
34+
impl SingletonImpl<Arc<Config>> for ConfigSingleton {
35+
fn get(&self) -> Arc<Config> {
36+
unsafe {
37+
match &*self.config.get() {
38+
None => panic!("GlobalConfig is not init"),
39+
Some(config) => config.clone(),
40+
}
41+
}
42+
}
43+
44+
fn init(&self, value: Arc<Config>) -> Result<()> {
45+
unsafe {
46+
*(self.config.get() as *mut Option<Arc<Config>>) = Some(value);
47+
Ok(())
48+
}
49+
}
50+
}
51+
52+
#[test]
53+
fn test_global_config() -> Result<()> {
54+
let config_singleton = GLOBAL.get_or_init(|| {
55+
Arc::new(ConfigSingleton {
56+
config: UnsafeCell::new(None),
57+
})
58+
});
59+
60+
let mut config = Config::default();
61+
62+
GlobalConfig::init(config.clone(), config_singleton.clone())?;
63+
assert_eq!(GlobalConfig::instance().as_ref(), &config);
64+
65+
config.cmd = "test".to_string();
66+
67+
GlobalConfig::init(config.clone(), config_singleton.clone())?;
68+
assert_eq!(GlobalConfig::instance().as_ref(), &config);
69+
70+
Ok(())
71+
}

src/query/config/tests/it/main.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2022 Datafuse Labs.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
mod global;

src/query/service/src/api/http/v1/config.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
use common_config::GlobalConfig;
1516
use poem::web::Json;
1617
use poem::IntoResponse;
1718

18-
use crate::sessions::SessionManager;
19-
2019
#[poem::handler]
2120
pub async fn config_handler() -> poem::Result<impl IntoResponse> {
22-
Ok(Json(SessionManager::instance().get_conf().into_outer()))
21+
Ok(Json(GlobalConfig::instance().as_ref().clone().into_outer()))
2322
}

src/query/service/src/api/http/v1/tenant_tables.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,14 @@ use chrono::DateTime;
1616
use chrono::Utc;
1717
use common_catalog::catalog::CatalogManager;
1818
use common_catalog::catalog_kind::CATALOG_DEFAULT;
19+
use common_config::GlobalConfig;
1920
use common_exception::Result;
2021
use poem::web::Json;
2122
use poem::web::Path;
2223
use poem::IntoResponse;
2324
use serde::Deserialize;
2425
use serde::Serialize;
2526

26-
use crate::sessions::SessionManager;
27-
2827
#[derive(Serialize, Deserialize, Eq, PartialEq, Debug, Default)]
2928
pub struct TenantTablesResponse {
3029
pub tables: Vec<TenantTableInfo>,
@@ -82,8 +81,7 @@ pub async fn list_tenant_tables_handler(
8281
// This handler returns the statistics about the tables of the current tenant.
8382
#[poem::handler]
8483
pub async fn list_tables_handler() -> poem::Result<impl IntoResponse> {
85-
let session_mgr = SessionManager::instance();
86-
let tenant = &session_mgr.get_conf().query.tenant_id;
84+
let tenant = &GlobalConfig::instance().query.tenant_id;
8785
if tenant.is_empty() {
8886
return Ok(Json(TenantTablesResponse { tables: vec![] }));
8987
}

0 commit comments

Comments
 (0)