mirror of
https://github.com/rustfs/rustfs.git
synced 2026-08-13 08:36:54 +00:00
refactor: extract embedded runtime orchestration hooks (#3659)
This commit is contained in:
+5
-68
@@ -49,20 +49,15 @@
|
||||
use crate::config::Config;
|
||||
use crate::server::{ShutdownHandle, start_http_server};
|
||||
use crate::startup_lifecycle::{log_embedded_server_ready, publish_embedded_startup_ready};
|
||||
use crate::startup_runtime_hooks::init_embedded_runtime_hooks;
|
||||
use crate::startup_server::init_embedded_startup_listen_context;
|
||||
use crate::startup_services::init_embedded_startup_runtime_services;
|
||||
use crate::startup_shutdown::run_embedded_shutdown_cleanup;
|
||||
use crate::startup_shutdown::run_embedded_server_shutdown;
|
||||
use crate::startup_storage::{init_embedded_startup_storage_foundation, init_embedded_startup_storage_runtime};
|
||||
use rustfs_obs::{init_obs, set_global_guard};
|
||||
use rustls::crypto::aws_lc_rs::default_provider;
|
||||
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use tokio_util::sync::CancellationToken;
|
||||
use tracing::{debug, info, warn};
|
||||
|
||||
const LOG_COMPONENT_EMBEDDED: &str = "embedded";
|
||||
const LOG_SUBSYSTEM_EMBEDDED: &str = "embedded";
|
||||
|
||||
/// Tracks whether a server has been started in this process.
|
||||
static SERVER_STARTED: AtomicBool = AtomicBool::new(false);
|
||||
@@ -270,26 +265,9 @@ impl RustFSServerBuilder {
|
||||
|
||||
// --- Initialization sequence (mirrors main.rs::run) ---
|
||||
|
||||
// Observability (minimal / no-op endpoint for embedded use).
|
||||
let guard = init_obs(Some(config.obs_endpoint.clone()))
|
||||
init_embedded_runtime_hooks(config.obs_endpoint.clone())
|
||||
.await
|
||||
.map_err(|e| ServerError::Init(format!("init_obs: {e}")))?;
|
||||
set_global_guard(guard).map_err(|e| ServerError::Init(format!("set_global_guard: {e}")))?;
|
||||
|
||||
// Crypto provider.
|
||||
if let Err(err) = default_provider().install_default() {
|
||||
debug!(
|
||||
component = LOG_COMPONENT_EMBEDDED,
|
||||
subsystem = LOG_SUBSYSTEM_EMBEDDED,
|
||||
event = "crypto_provider_state",
|
||||
state = "already_installed",
|
||||
error = ?err,
|
||||
"Embedded crypto provider state changed"
|
||||
);
|
||||
}
|
||||
|
||||
// Trusted proxies.
|
||||
rustfs_trusted_proxies::init();
|
||||
.map_err(|e| ServerError::Init(e.to_string()))?;
|
||||
|
||||
let listen_context = init_embedded_startup_listen_context(&config)
|
||||
.await
|
||||
@@ -420,48 +398,7 @@ impl RustFSServer {
|
||||
}
|
||||
|
||||
async fn do_shutdown(&mut self) {
|
||||
info!(
|
||||
target: "rustfs::embedded",
|
||||
component = LOG_COMPONENT_EMBEDDED,
|
||||
subsystem = LOG_SUBSYSTEM_EMBEDDED,
|
||||
event = "embedded_server_state",
|
||||
state = "stopping",
|
||||
"Embedded server state changed"
|
||||
);
|
||||
|
||||
// Cancel background services.
|
||||
self.cancel_token.cancel();
|
||||
|
||||
run_embedded_shutdown_cleanup().await;
|
||||
|
||||
// Signal HTTP server to stop.
|
||||
if let Some(shutdown_handle) = self.shutdown_handle.take() {
|
||||
shutdown_handle.shutdown().await;
|
||||
}
|
||||
|
||||
// Clean up temp directory if we created it.
|
||||
if let Some(ref dir) = self.temp_dir
|
||||
&& let Err(e) = tokio::fs::remove_dir_all(dir).await
|
||||
{
|
||||
warn!(
|
||||
component = LOG_COMPONENT_EMBEDDED,
|
||||
subsystem = LOG_SUBSYSTEM_EMBEDDED,
|
||||
event = "embedded_shutdown_cleanup_failed",
|
||||
service = "temp_dir",
|
||||
path = %dir.display(),
|
||||
error = %e,
|
||||
"Embedded shutdown cleanup failed"
|
||||
);
|
||||
}
|
||||
|
||||
info!(
|
||||
target: "rustfs::embedded",
|
||||
component = LOG_COMPONENT_EMBEDDED,
|
||||
subsystem = LOG_SUBSYSTEM_EMBEDDED,
|
||||
event = "embedded_server_state",
|
||||
state = "stopped",
|
||||
"Embedded server state changed"
|
||||
);
|
||||
run_embedded_server_shutdown(&self.cancel_token, &mut self.shutdown_handle, self.temp_dir.as_deref()).await;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -15,9 +15,12 @@
|
||||
use crate::license::license_status;
|
||||
use rustls::crypto::aws_lc_rs::default_provider;
|
||||
use std::future::Future;
|
||||
use std::io::{Error, Result};
|
||||
use tracing::{debug, info};
|
||||
|
||||
const LOG_COMPONENT_EMBEDDED: &str = "embedded";
|
||||
const LOG_COMPONENT_MAIN: &str = "main";
|
||||
const LOG_SUBSYSTEM_EMBEDDED: &str = "embedded";
|
||||
const LOG_SUBSYSTEM_LICENSE: &str = "license";
|
||||
const LOG_SUBSYSTEM_STARTUP: &str = "startup";
|
||||
const EVENT_CRYPTO_PROVIDER_STATE: &str = "crypto_provider_state";
|
||||
@@ -100,6 +103,31 @@ pub fn install_default_crypto_provider() {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn init_embedded_runtime_hooks(obs_endpoint: String) -> Result<()> {
|
||||
let guard = rustfs_obs::init_obs(Some(obs_endpoint))
|
||||
.await
|
||||
.map_err(|err| Error::other(format!("init_obs: {err}")))?;
|
||||
rustfs_obs::set_global_guard(guard).map_err(|err| Error::other(format!("set_global_guard: {err}")))?;
|
||||
|
||||
install_embedded_default_crypto_provider();
|
||||
rustfs_trusted_proxies::init();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn install_embedded_default_crypto_provider() {
|
||||
if let Err(err) = default_provider().install_default() {
|
||||
debug!(
|
||||
component = LOG_COMPONENT_EMBEDDED,
|
||||
subsystem = LOG_SUBSYSTEM_EMBEDDED,
|
||||
event = EVENT_CRYPTO_PROVIDER_STATE,
|
||||
state = "already_installed",
|
||||
error = ?err,
|
||||
"Embedded crypto provider state changed"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{init_profiling_runtime_with, shutdown_profiling_runtime_with};
|
||||
|
||||
@@ -21,6 +21,7 @@ use crate::{
|
||||
};
|
||||
use rustfs_heal::shutdown_ahm_services;
|
||||
use rustfs_utils::get_env_bool_with_aliases;
|
||||
use std::path::Path;
|
||||
use tokio_util::sync::CancellationToken;
|
||||
use tracing::{error, info, warn};
|
||||
|
||||
@@ -33,6 +34,7 @@ const LOG_COMPONENT_EMBEDDED: &str = "embedded";
|
||||
const LOG_SUBSYSTEM_STARTUP: &str = "startup";
|
||||
const LOG_SUBSYSTEM_EMBEDDED: &str = "embedded";
|
||||
const EVENT_AUDIT_SYSTEM_STATE: &str = "audit_system_state";
|
||||
const EVENT_EMBEDDED_SERVER_STATE: &str = "embedded_server_state";
|
||||
const EVENT_EMBEDDED_SHUTDOWN_CLEANUP_FAILED: &str = "embedded_shutdown_cleanup_failed";
|
||||
const EVENT_SHUTDOWN_SIGNAL_RECEIVED: &str = "shutdown_signal_received";
|
||||
const EVENT_BACKGROUND_SERVICE_SHUTDOWN: &str = "background_service_shutdown";
|
||||
@@ -214,6 +216,52 @@ pub async fn run_embedded_shutdown_cleanup() {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn run_embedded_server_shutdown(
|
||||
ctx: &CancellationToken,
|
||||
shutdown_handle: &mut Option<ShutdownHandle>,
|
||||
temp_dir: Option<&Path>,
|
||||
) {
|
||||
info!(
|
||||
target: "rustfs::embedded",
|
||||
component = LOG_COMPONENT_EMBEDDED,
|
||||
subsystem = LOG_SUBSYSTEM_EMBEDDED,
|
||||
event = EVENT_EMBEDDED_SERVER_STATE,
|
||||
state = "stopping",
|
||||
"Embedded server state changed"
|
||||
);
|
||||
|
||||
ctx.cancel();
|
||||
|
||||
run_embedded_shutdown_cleanup().await;
|
||||
|
||||
if let Some(shutdown_handle) = shutdown_handle.take() {
|
||||
shutdown_handle.shutdown().await;
|
||||
}
|
||||
|
||||
if let Some(dir) = temp_dir
|
||||
&& let Err(err) = tokio::fs::remove_dir_all(dir).await
|
||||
{
|
||||
warn!(
|
||||
component = LOG_COMPONENT_EMBEDDED,
|
||||
subsystem = LOG_SUBSYSTEM_EMBEDDED,
|
||||
event = EVENT_EMBEDDED_SHUTDOWN_CLEANUP_FAILED,
|
||||
service = "temp_dir",
|
||||
path = %dir.display(),
|
||||
error = %err,
|
||||
"Embedded shutdown cleanup failed"
|
||||
);
|
||||
}
|
||||
|
||||
info!(
|
||||
target: "rustfs::embedded",
|
||||
component = LOG_COMPONENT_EMBEDDED,
|
||||
subsystem = LOG_SUBSYSTEM_EMBEDDED,
|
||||
event = EVENT_EMBEDDED_SERVER_STATE,
|
||||
state = "stopped",
|
||||
"Embedded server state changed"
|
||||
);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{BackgroundShutdownStep, background_shutdown_steps};
|
||||
|
||||
Reference in New Issue
Block a user