use crate::catalog::ClusterCatalog;
use crate::error::Result;
use crate::lifecycle_state::ClusterLifecycleTracker;
use crate::transport::NexarTransport;
use super::bootstrap_fn::bootstrap;
use super::config::{ClusterConfig, ClusterState};
use super::join::join;
use super::probe::should_bootstrap;
use super::restart::restart;
pub async fn start_cluster(
config: &ClusterConfig,
catalog: &ClusterCatalog,
transport: &NexarTransport,
lifecycle: &ClusterLifecycleTracker,
) -> Result<ClusterState> {
if catalog.is_bootstrapped()? {
lifecycle.to_restarting();
return restart(config, catalog, transport).inspect_err(|e| {
lifecycle.to_failed(format!("restart failed: {e}"));
});
}
let is_seed = config.seed_nodes.contains(&config.listen_addr);
if is_seed && should_bootstrap(config, transport).await {
lifecycle.to_bootstrapping();
bootstrap(config, catalog).inspect_err(|e| {
lifecycle.to_failed(format!("bootstrap failed: {e}"));
})
} else {
join(config, catalog, transport, lifecycle).await
}
}