refactor(config): move global config accessors (#3360)

This commit is contained in:
安正超
2026-06-11 19:16:02 +08:00
committed by GitHub
parent ed3851782c
commit b4524033e3
15 changed files with 131 additions and 65 deletions
+25 -1
View File
@@ -14,11 +14,12 @@
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::{LazyLock, OnceLock};
use std::sync::{LazyLock, OnceLock, RwLock};
use crate::{COMMENT_KEY, DEFAULT_DELIMITER};
pub static DEFAULT_KVS: LazyLock<OnceLock<HashMap<String, KVS>>> = LazyLock::new(OnceLock::new);
pub static GLOBAL_SERVER_CONFIG: LazyLock<RwLock<Option<Config>>> = LazyLock::new(|| RwLock::new(None));
#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)]
pub struct KV {
@@ -174,6 +175,16 @@ pub fn register_default_kvs(kvs: HashMap<String, KVS>) {
let _ = DEFAULT_KVS.set(p);
}
pub fn get_global_server_config() -> Option<Config> {
GLOBAL_SERVER_CONFIG.read().ok().and_then(|guard| (*guard).clone())
}
pub fn set_global_server_config(cfg: Config) {
if let Ok(mut guard) = GLOBAL_SERVER_CONFIG.write() {
*guard = Some(cfg);
}
}
#[cfg(test)]
mod tests {
use super::*;
@@ -226,4 +237,17 @@ mod tests {
);
assert_eq!(loaded.merge(), loaded);
}
#[test]
fn global_server_config_set_and_get_roundtrip() {
let mut cfg = Config(HashMap::new());
let mut kvs = KVS::new();
kvs.insert("standard".to_string(), "EC:4".to_string());
cfg.0
.insert("storage_class".to_string(), HashMap::from([(DEFAULT_DELIMITER.to_string(), kvs)]));
set_global_server_config(cfg.clone());
assert_eq!(get_global_server_config(), Some(cfg));
}
}
+5 -13
View File
@@ -34,14 +34,16 @@ use rustfs_config::notify::{
NOTIFY_POSTGRES_SUB_SYS, NOTIFY_PULSAR_SUB_SYS, NOTIFY_REDIS_SUB_SYS, NOTIFY_WEBHOOK_SUB_SYS,
};
use rustfs_config::oidc::IDENTITY_OPENID_SUB_SYS;
use rustfs_config::server_config::{Config, register_default_kvs};
use rustfs_config::server_config::register_default_kvs;
use std::collections::HashMap;
use std::sync::LazyLock;
use std::sync::{Arc, RwLock};
// RUSTFS_COMPAT_TODO(CFG-008): keep old ecstore global server-config accessor path while runtime consumers migrate. Remove after all consumers import these accessors from rustfs_config::server_config.
pub use rustfs_config::server_config::{get_global_server_config, set_global_server_config};
pub static GLOBAL_STORAGE_CLASS: LazyLock<RwLock<storageclass::Config>> =
LazyLock::new(|| RwLock::new(storageclass::Config::default()));
pub static GLOBAL_SERVER_CONFIG: LazyLock<RwLock<Option<Config>>> = LazyLock::new(|| RwLock::new(None));
pub static GLOBAL_CONFIG_SYS: LazyLock<ConfigSys> = LazyLock::new(ConfigSys::new);
pub static RUSTFS_CONFIG_PREFIX: &str = "config";
@@ -69,16 +71,6 @@ impl ConfigSys {
}
}
pub fn get_global_server_config() -> Option<Config> {
GLOBAL_SERVER_CONFIG.read().ok().and_then(|guard| (*guard).clone())
}
pub fn set_global_server_config(cfg: Config) {
if let Ok(mut guard) = GLOBAL_SERVER_CONFIG.write() {
*guard = Some(cfg);
}
}
pub fn get_global_storage_class() -> Option<storageclass::Config> {
GLOBAL_STORAGE_CLASS.read().ok().map(|guard| (*guard).clone())
}
@@ -132,7 +124,7 @@ pub fn init() {
#[cfg(test)]
mod tests {
use super::*;
use rustfs_config::server_config::KVS;
use rustfs_config::server_config::{Config, KVS};
use rustfs_config::{
DEFAULT_DELIMITER, DEFAULT_HEAL_BITROT_CYCLE_SECS, DEFAULT_SCANNER_SPEED, HEAL_BITROT_CYCLE, SCANNER_CYCLE_MAX_OBJECTS,
SCANNER_DELAY, SCANNER_MAX_WAIT, SCANNER_SPEED, SCANNER_SUB_SYS,
+4 -4
View File
@@ -33,8 +33,8 @@ use crate::bucket::utils::check_object_args;
use crate::bucket::utils::check_put_object_args;
use crate::bucket::utils::check_put_object_part_args;
use crate::bucket::utils::{check_valid_bucket_name, check_valid_bucket_name_strict, is_meta_bucketname};
use crate::config::get_global_storage_class;
use crate::config::storageclass;
use crate::config::{get_global_server_config, get_global_storage_class};
use crate::disk::endpoint::{Endpoint, EndpointType};
use crate::disk::{DiskAPI, DiskInfo, DiskInfoOptions};
use crate::error::{Error, Result};
@@ -77,7 +77,7 @@ use lazy_static::lazy_static;
use rand::RngExt as _;
use rustfs_common::heal_channel::{HealItemType, HealOpts};
use rustfs_common::{GLOBAL_LOCAL_NODE_NAME, GLOBAL_RUSTFS_ADDR, GLOBAL_RUSTFS_HOST, GLOBAL_RUSTFS_PORT};
use rustfs_config::server_config::Config;
use rustfs_config::server_config::{Config, get_global_server_config, set_global_server_config};
use rustfs_filemeta::FileInfo;
use rustfs_lock::{LocalClient, LockClient, NamespaceLockWrapper};
use rustfs_madmin::heal_commands::HealResultItem;
@@ -216,12 +216,12 @@ impl std::fmt::Debug for ECStore {
impl ECStore {
/// Get server configuration (delegates to global)
pub fn get_server_config(&self) -> Option<Config> {
crate::config::get_global_server_config()
get_global_server_config()
}
/// Set server configuration (delegates to global)
pub fn set_server_config(&self, cfg: Config) {
crate::config::set_global_server_config(cfg);
set_global_server_config(cfg);
}
/// Get storage class configuration (delegates to global)
+1 -1
View File
@@ -26,9 +26,9 @@ use openidconnect::{
};
use reqwest::Client;
use rustfs_config::oidc::*;
use rustfs_config::server_config::get_global_server_config;
use rustfs_config::server_config::{Config as ServerConfig, KVS};
use rustfs_config::{DEFAULT_DELIMITER, ENABLE_KEY, EnableState};
use rustfs_ecstore::config::get_global_server_config;
use rustfs_policy::policy::{ClaimLookup, get_claim_case_insensitive};
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
+1 -1
View File
@@ -664,7 +664,7 @@ pub fn apply_scanner_runtime_config(config: &ServerConfig) -> Result<(), Scanner
}
pub(crate) fn refresh_scanner_runtime_config_from_global() -> Result<(), ScannerRuntimeConfigError> {
let config = rustfs_ecstore::config::get_global_server_config();
let config = rustfs_config::server_config::get_global_server_config();
let resolved = lookup_scanner_runtime_config(config.as_ref())?;
apply_resolved_runtime_config(resolved);
Ok(())
+1 -1
View File
@@ -78,7 +78,7 @@ fn scanner_cycle_max_duration() -> Option<Duration> {
}
fn resolve_scanner_runtime_config() -> crate::runtime_config::ScannerRuntimeConfig {
let config = rustfs_ecstore::config::get_global_server_config();
let config = rustfs_config::server_config::get_global_server_config();
match lookup_scanner_runtime_config(config.as_ref()) {
Ok(config) => config,
Err(err) => {