use super::instance::{InstanceContext, bootstrap_ctx};
use crate::bucket::bandwidth::monitor::Monitor;
use crate::{
bucket::lifecycle::bucket_lifecycle_ops::LifecycleSys,
disk::DiskStore,
layout::endpoints::{EndpointServerPools, PoolEndpoints, SetupType},
services::tier::tier::TierConfigMgr,
store::ECStore,
};
use lazy_static::lazy_static;
use rustfs_lock::client::LockClient;
use std::{
collections::HashMap,
sync::{
Arc, OnceLock,
atomic::{AtomicBool, Ordering},
},
time::SystemTime,
};
use tokio::sync::{OnceCell, RwLock};
use tokio_util::sync::CancellationToken;
use tracing::warn;
pub const DISK_ASSUME_UNKNOWN_SIZE: u64 = 1 << 30;
pub const DISK_MIN_INODES: u64 = 1000;
pub const DISK_FILL_FRACTION: f64 = 0.99;
pub const DISK_RESERVE_FRACTION: f64 = 0.15;
static GET_METADATA_READ_VERSION_COALESCING_SERVICE_READY: AtomicBool = AtomicBool::new(false);
pub(crate) fn mark_get_metadata_read_version_coalescing_service_ready() {
GET_METADATA_READ_VERSION_COALESCING_SERVICE_READY.store(true, Ordering::Release);
}
pub(crate) fn get_metadata_read_version_coalescing_service_ready() -> bool {
GET_METADATA_READ_VERSION_COALESCING_SERVICE_READY.load(Ordering::Acquire)
}
lazy_static! {
static ref GLOBAL_RUSTFS_PORT: OnceLock<u16> = OnceLock::new();
pub static ref GLOBAL_OBJECT_API: OnceLock<Arc<ECStore>> = OnceLock::new();
pub static ref GLOBAL_ROOT_DISK_THRESHOLD: RwLock<u64> = RwLock::new(0);
pub static ref GLOBAL_LIFECYCLE_SYS: Arc<LifecycleSys> = LifecycleSys::new();
pub static ref GLOBAL_BOOT_TIME: OnceCell<SystemTime> = OnceCell::new();
pub static ref GLOBAL_LOCAL_NODE_NAME_FALLBACK: String = "127.0.0.1:9000".to_string();
pub static ref GLOBAL_LOCAL_NODE_NAME_HEX_FALLBACK: String =
rustfs_utils::crypto::hex(GLOBAL_LOCAL_NODE_NAME_FALLBACK.as_bytes());
pub static ref GLOBAL_LOCAL_LOCK_CLIENT: OnceLock<Arc<dyn LockClient>> = OnceLock::new();
pub static ref GLOBAL_LOCK_CLIENTS: OnceLock<HashMap<String, Arc<dyn LockClient>>> = OnceLock::new();
}
pub fn init_global_bucket_monitor(num_nodes: u64) {
if !current_ctx().init_bucket_monitor(num_nodes) {
warn!(
"bucket monitor already initialized, ignoring re-initialization with num_nodes={}",
num_nodes
);
}
}
pub fn get_global_bucket_monitor() -> Option<Arc<Monitor>> {
current_ctx().bucket_monitor()
}
pub fn global_rustfs_port() -> u16 {
if let Some(p) = GLOBAL_RUSTFS_PORT.get() {
*p
} else {
rustfs_config::DEFAULT_PORT
}
}
pub fn set_global_rustfs_port(value: u16) {
if GLOBAL_RUSTFS_PORT.set(value).is_err() {
warn!("global rustfs port already initialized, ignoring re-initialization");
}
}
pub fn get_global_deployment_id() -> Option<String> {
current_ctx().deployment_id().map(|v| v.to_string())
}
pub fn set_global_endpoints(eps: Vec<PoolEndpoints>) {
current_ctx().set_endpoints(EndpointServerPools::from(eps));
}
pub fn get_global_endpoints() -> EndpointServerPools {
current_ctx().endpoints().unwrap_or_default()
}
pub fn get_global_endpoints_opt() -> Option<EndpointServerPools> {
current_ctx().endpoints()
}
#[cfg(test)]
pub async fn reset_local_disk_test_state() {
let ctx = current_ctx();
ctx.local_disk_map().write().await.clear();
ctx.local_disk_id_map().write().await.clear();
ctx.local_disk_set_drives().write().await.clear();
}
pub async fn is_first_cluster_node_local() -> bool {
get_global_endpoints().first_local()
}
pub fn get_global_tier_config_mgr() -> Arc<RwLock<TierConfigMgr>> {
current_ctx().tier_config_mgr()
}
pub fn new_object_layer_fn() -> Option<Arc<ECStore>> {
GLOBAL_OBJECT_API.get().cloned()
}
pub type ObjectStoreResolver = dyn Fn() -> Option<Arc<ECStore>> + Send + Sync + 'static;
static GLOBAL_OBJECT_STORE_RESOLVER: OnceLock<Arc<ObjectStoreResolver>> = OnceLock::new();
pub fn set_object_store_resolver(resolver: Arc<ObjectStoreResolver>) -> bool {
GLOBAL_OBJECT_STORE_RESOLVER.set(resolver).is_ok()
}
pub fn resolve_object_store_handle() -> Option<Arc<ECStore>> {
GLOBAL_OBJECT_STORE_RESOLVER
.get()
.and_then(|resolver| resolver())
.or_else(new_object_layer_fn)
}
pub(crate) fn current_ctx() -> Arc<InstanceContext> {
resolve_object_store_handle()
.map(|store| store.ctx.clone())
.unwrap_or_else(bootstrap_ctx)
}
pub async fn set_object_layer(o: Arc<ECStore>) {
if GLOBAL_OBJECT_API.set(o).is_err() {
warn!("global object layer already initialized, ignoring re-initialization");
}
}
pub async fn is_dist_erasure() -> bool {
current_ctx().is_dist_erasure().await
}
pub async fn is_erasure_sd() -> bool {
current_ctx().is_erasure_sd().await
}
pub async fn is_erasure() -> bool {
current_ctx().is_erasure().await
}
pub async fn update_erasure_type(setup_type: SetupType) {
current_ctx().update_erasure_type(setup_type).await;
}
pub(crate) type TypeLocalDiskSetDrives = Vec<Vec<Vec<Option<DiskStore>>>>;
pub fn set_global_region(region: s3s::region::Region) {
current_ctx().set_region(region);
}
pub fn get_global_region() -> Option<s3s::region::Region> {
current_ctx().region()
}
pub fn get_background_services_cancel_token() -> Option<CancellationToken> {
current_ctx().background_cancel_token()
}
pub fn shutdown_background_services() {
if let Some(cancel_token) = get_background_services_cancel_token() {
cancel_token.cancel();
}
}
pub fn set_global_lock_client(client: Arc<dyn LockClient>) -> Result<(), Arc<dyn LockClient>> {
GLOBAL_LOCAL_LOCK_CLIENT.set(client)
}
pub fn get_global_lock_client() -> Option<Arc<dyn LockClient>> {
GLOBAL_LOCAL_LOCK_CLIENT.get().cloned()
}
pub fn set_global_lock_clients(
clients: HashMap<String, Arc<dyn LockClient>>,
) -> Result<(), HashMap<String, Arc<dyn LockClient>>> {
GLOBAL_LOCK_CLIENTS.set(clients)
}
pub fn get_global_lock_clients() -> Option<&'static HashMap<String, Arc<dyn LockClient>>> {
GLOBAL_LOCK_CLIENTS.get()
}