use ballista_core::config::LogRotationPolicy;
use ballista_core::error::BallistaError;
use ballista_core::object_store::{
runtime_env_with_s3_support, session_config_with_s3_support,
};
use ballista_executor::config::Config;
use ballista_executor::executor_process::{
ExecutorProcessConfig, start_executor_process,
};
use ballista_executor::health::spawn_health_server;
use clap::Parser;
use std::env;
use std::net::SocketAddr;
use std::sync::Arc;
use tracing_subscriber::EnvFilter;
#[cfg(feature = "mimalloc")]
#[global_allocator]
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
#[tokio::main]
async fn main() -> ballista_core::error::Result<()> {
let opt = Config::parse();
let bind_host = opt.bind_host.clone();
let bind_health_port = opt.bind_health_port;
let mut config: ExecutorProcessConfig = opt.try_into()?;
config.override_config_producer = Some(Arc::new(session_config_with_s3_support));
config.override_runtime_producer = Some(Arc::new(runtime_env_with_s3_support));
let rust_log = env::var(EnvFilter::DEFAULT_ENV);
let log_filter =
EnvFilter::new(rust_log.unwrap_or(config.special_mod_log_level.clone()));
let tracing = tracing_subscriber::fmt()
.with_ansi(false)
.with_thread_names(config.print_thread_info)
.with_thread_ids(config.print_thread_info)
.with_env_filter(log_filter);
if let Some(log_dir) = &config.log_dir {
let log_file = match config.log_rotation_policy {
LogRotationPolicy::Minutely => tracing_appender::rolling::minutely(
log_dir,
config.log_file_name_prefix(),
),
LogRotationPolicy::Hourly => {
tracing_appender::rolling::hourly(log_dir, config.log_file_name_prefix())
}
LogRotationPolicy::Daily => {
tracing_appender::rolling::daily(log_dir, config.log_file_name_prefix())
}
LogRotationPolicy::Never => {
tracing_appender::rolling::never(log_dir, config.log_file_name_prefix())
}
};
tracing.with_writer(log_file).init();
} else {
tracing.init();
}
let health = config.health.clone();
let health_addr: SocketAddr = format!("{bind_host}:{bind_health_port}")
.parse()
.map_err(|e| {
BallistaError::General(format!("invalid --bind-health-port address: {e}"))
})?;
let (health_shutdown_tx, health_shutdown_rx) = tokio::sync::oneshot::channel();
let health_handle = spawn_health_server(health_addr, health, health_shutdown_rx);
let result = start_executor_process(Arc::new(config)).await;
let _ = health_shutdown_tx.send(());
if let Err(e) = health_handle.await {
log::warn!("health server task join error: {e}");
}
result
}