2020-04-07 14:26:22 +00:00
|
|
|
use std::io::{Read, Write};
|
|
|
|
use std::net::SocketAddr;
|
|
|
|
use std::path::PathBuf;
|
2020-04-10 20:01:48 +00:00
|
|
|
use std::sync::Arc;
|
2020-04-17 15:09:57 +00:00
|
|
|
|
2020-04-19 17:59:59 +00:00
|
|
|
use futures_util::future::*;
|
2020-04-17 15:09:57 +00:00
|
|
|
use serde::Deserialize;
|
2020-04-11 16:51:11 +00:00
|
|
|
use tokio::sync::watch;
|
2020-04-07 14:26:22 +00:00
|
|
|
|
2020-04-11 16:51:11 +00:00
|
|
|
use crate::background::*;
|
2020-04-11 21:53:32 +00:00
|
|
|
use crate::data::*;
|
2020-04-07 14:26:22 +00:00
|
|
|
use crate::error::Error;
|
|
|
|
use crate::membership::System;
|
2020-04-18 17:21:34 +00:00
|
|
|
use crate::rpc_server::RpcServer;
|
2020-04-08 20:00:41 +00:00
|
|
|
use crate::table::*;
|
2020-04-19 15:15:48 +00:00
|
|
|
use crate::table_fullcopy::*;
|
2020-04-19 11:22:28 +00:00
|
|
|
use crate::table_sharded::*;
|
2020-04-18 17:39:08 +00:00
|
|
|
|
|
|
|
use crate::block::*;
|
|
|
|
use crate::block_ref_table::*;
|
2020-04-19 15:15:48 +00:00
|
|
|
use crate::bucket_table::*;
|
2020-04-18 17:39:08 +00:00
|
|
|
use crate::object_table::*;
|
2020-04-18 17:30:05 +00:00
|
|
|
use crate::version_table::*;
|
|
|
|
|
2020-04-19 15:15:48 +00:00
|
|
|
use crate::admin_rpc::*;
|
2020-04-18 17:39:08 +00:00
|
|
|
use crate::api_server;
|
|
|
|
|
2020-04-17 13:36:16 +00:00
|
|
|
#[derive(Deserialize, Debug, Clone)]
|
2020-04-11 16:51:11 +00:00
|
|
|
pub struct Config {
|
|
|
|
pub metadata_dir: PathBuf,
|
|
|
|
pub data_dir: PathBuf,
|
|
|
|
|
2020-04-19 17:59:59 +00:00
|
|
|
pub api_bind_addr: SocketAddr,
|
|
|
|
pub rpc_bind_addr: SocketAddr,
|
2020-04-11 16:51:11 +00:00
|
|
|
|
|
|
|
pub bootstrap_peers: Vec<SocketAddr>,
|
|
|
|
|
2020-04-23 14:40:59 +00:00
|
|
|
#[serde(default = "default_max_concurrent_rpc_requests")]
|
|
|
|
pub max_concurrent_rpc_requests: usize,
|
2020-04-22 16:51:52 +00:00
|
|
|
|
2020-04-11 16:51:11 +00:00
|
|
|
#[serde(default = "default_block_size")]
|
|
|
|
pub block_size: usize,
|
|
|
|
|
2020-04-11 17:43:29 +00:00
|
|
|
#[serde(default = "default_replication_factor")]
|
2020-04-11 16:51:11 +00:00
|
|
|
pub meta_replication_factor: usize,
|
|
|
|
|
2020-04-19 15:15:48 +00:00
|
|
|
#[serde(default = "default_epidemic_factor")]
|
|
|
|
pub meta_epidemic_factor: usize,
|
|
|
|
|
2020-04-11 17:43:29 +00:00
|
|
|
#[serde(default = "default_replication_factor")]
|
2020-04-11 16:51:11 +00:00
|
|
|
pub data_replication_factor: usize,
|
2020-04-12 11:03:55 +00:00
|
|
|
|
2020-04-12 13:51:19 +00:00
|
|
|
pub rpc_tls: Option<TlsConfig>,
|
2020-04-12 11:03:55 +00:00
|
|
|
}
|
|
|
|
|
2020-04-23 14:40:59 +00:00
|
|
|
fn default_max_concurrent_rpc_requests() -> usize {
|
2020-04-22 16:51:52 +00:00
|
|
|
12
|
|
|
|
}
|
2020-04-19 15:15:48 +00:00
|
|
|
fn default_block_size() -> usize {
|
|
|
|
1048576
|
|
|
|
}
|
|
|
|
fn default_replication_factor() -> usize {
|
|
|
|
3
|
|
|
|
}
|
|
|
|
fn default_epidemic_factor() -> usize {
|
|
|
|
3
|
|
|
|
}
|
|
|
|
|
2020-04-17 13:36:16 +00:00
|
|
|
#[derive(Deserialize, Debug, Clone)]
|
2020-04-12 11:03:55 +00:00
|
|
|
pub struct TlsConfig {
|
2020-04-12 13:51:19 +00:00
|
|
|
pub ca_cert: String,
|
|
|
|
pub node_cert: String,
|
|
|
|
pub node_key: String,
|
2020-04-11 16:51:11 +00:00
|
|
|
}
|
|
|
|
|
2020-04-08 20:00:41 +00:00
|
|
|
pub struct Garage {
|
|
|
|
pub db: sled::Db,
|
2020-04-17 13:36:16 +00:00
|
|
|
pub background: Arc<BackgroundRunner>,
|
2020-04-08 20:00:41 +00:00
|
|
|
pub system: Arc<System>,
|
2020-04-12 11:03:55 +00:00
|
|
|
pub block_manager: Arc<BlockManager>,
|
2020-04-08 20:00:41 +00:00
|
|
|
|
2020-04-19 15:15:48 +00:00
|
|
|
pub bucket_table: Arc<Table<BucketTable, TableFullReplication>>,
|
2020-04-19 11:22:28 +00:00
|
|
|
pub object_table: Arc<Table<ObjectTable, TableShardedReplication>>,
|
|
|
|
pub version_table: Arc<Table<VersionTable, TableShardedReplication>>,
|
|
|
|
pub block_ref_table: Arc<Table<BlockRefTable, TableShardedReplication>>,
|
2020-04-08 20:00:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Garage {
|
2020-04-11 16:51:11 +00:00
|
|
|
pub async fn new(
|
|
|
|
config: Config,
|
|
|
|
id: UUID,
|
|
|
|
db: sled::Db,
|
|
|
|
background: Arc<BackgroundRunner>,
|
2020-04-18 17:21:34 +00:00
|
|
|
rpc_server: &mut RpcServer,
|
2020-04-11 16:51:11 +00:00
|
|
|
) -> Arc<Self> {
|
2020-04-21 12:54:55 +00:00
|
|
|
info!("Initialize membership management system...");
|
2020-04-18 17:21:34 +00:00
|
|
|
let system = System::new(config.clone(), id, background.clone(), rpc_server);
|
2020-04-11 21:00:26 +00:00
|
|
|
|
2020-04-21 12:54:55 +00:00
|
|
|
info!("Initialize block manager...");
|
2020-04-18 17:21:34 +00:00
|
|
|
let block_manager =
|
|
|
|
BlockManager::new(&db, config.data_dir.clone(), system.clone(), rpc_server);
|
2020-04-08 20:00:41 +00:00
|
|
|
|
2020-04-19 11:22:28 +00:00
|
|
|
let data_rep_param = TableShardedReplication {
|
2020-04-12 11:03:55 +00:00
|
|
|
replication_factor: system.config.data_replication_factor,
|
|
|
|
write_quorum: (system.config.data_replication_factor + 1) / 2,
|
|
|
|
read_quorum: 1,
|
|
|
|
};
|
|
|
|
|
2020-04-19 11:22:28 +00:00
|
|
|
let meta_rep_param = TableShardedReplication {
|
2020-04-08 20:00:41 +00:00
|
|
|
replication_factor: system.config.meta_replication_factor,
|
2020-04-10 20:01:48 +00:00
|
|
|
write_quorum: (system.config.meta_replication_factor + 1) / 2,
|
|
|
|
read_quorum: (system.config.meta_replication_factor + 1) / 2,
|
2020-04-08 20:00:41 +00:00
|
|
|
};
|
|
|
|
|
2020-04-19 15:15:48 +00:00
|
|
|
let control_rep_param = TableFullReplication::new(
|
|
|
|
system.config.meta_epidemic_factor,
|
|
|
|
(system.config.meta_epidemic_factor + 1) / 2,
|
|
|
|
);
|
|
|
|
|
2020-04-21 12:54:55 +00:00
|
|
|
info!("Initialize block_ref_table...");
|
2020-04-12 20:24:53 +00:00
|
|
|
let block_ref_table = Table::new(
|
2020-04-12 11:03:55 +00:00
|
|
|
BlockRefTable {
|
|
|
|
background: background.clone(),
|
|
|
|
block_manager: block_manager.clone(),
|
2020-04-10 20:01:48 +00:00
|
|
|
},
|
2020-04-19 11:22:28 +00:00
|
|
|
data_rep_param.clone(),
|
2020-04-08 20:00:41 +00:00
|
|
|
system.clone(),
|
|
|
|
&db,
|
2020-04-12 11:03:55 +00:00
|
|
|
"block_ref".to_string(),
|
2020-04-18 17:21:34 +00:00
|
|
|
rpc_server,
|
2020-04-16 15:04:28 +00:00
|
|
|
)
|
|
|
|
.await;
|
2020-04-17 15:09:57 +00:00
|
|
|
|
2020-04-21 12:54:55 +00:00
|
|
|
info!("Initialize version_table...");
|
2020-04-12 20:24:53 +00:00
|
|
|
let version_table = Table::new(
|
2020-04-10 20:01:48 +00:00
|
|
|
VersionTable {
|
2020-04-12 11:03:55 +00:00
|
|
|
background: background.clone(),
|
|
|
|
block_ref_table: block_ref_table.clone(),
|
2020-04-10 20:01:48 +00:00
|
|
|
},
|
2020-04-19 11:22:28 +00:00
|
|
|
meta_rep_param.clone(),
|
2020-04-09 21:45:07 +00:00
|
|
|
system.clone(),
|
|
|
|
&db,
|
|
|
|
"version".to_string(),
|
2020-04-18 17:21:34 +00:00
|
|
|
rpc_server,
|
2020-04-16 15:04:28 +00:00
|
|
|
)
|
|
|
|
.await;
|
2020-04-17 15:09:57 +00:00
|
|
|
|
2020-04-21 12:54:55 +00:00
|
|
|
info!("Initialize object_table...");
|
2020-04-12 20:24:53 +00:00
|
|
|
let object_table = Table::new(
|
2020-04-12 11:03:55 +00:00
|
|
|
ObjectTable {
|
|
|
|
background: background.clone(),
|
|
|
|
version_table: version_table.clone(),
|
2020-04-10 21:11:52 +00:00
|
|
|
},
|
2020-04-19 11:22:28 +00:00
|
|
|
meta_rep_param.clone(),
|
2020-04-10 21:11:52 +00:00
|
|
|
system.clone(),
|
|
|
|
&db,
|
2020-04-12 11:03:55 +00:00
|
|
|
"object".to_string(),
|
2020-04-18 17:21:34 +00:00
|
|
|
rpc_server,
|
2020-04-16 15:04:28 +00:00
|
|
|
)
|
|
|
|
.await;
|
2020-04-08 20:00:41 +00:00
|
|
|
|
2020-04-21 12:54:55 +00:00
|
|
|
info!("Initialize bucket_table...");
|
2020-04-19 15:15:48 +00:00
|
|
|
let bucket_table = Table::new(
|
|
|
|
BucketTable,
|
|
|
|
control_rep_param.clone(),
|
|
|
|
system.clone(),
|
|
|
|
&db,
|
|
|
|
"bucket".to_string(),
|
|
|
|
rpc_server,
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
|
2020-04-21 12:54:55 +00:00
|
|
|
info!("Initialize Garage...");
|
2020-04-18 17:21:34 +00:00
|
|
|
let garage = Arc::new(Self {
|
2020-04-08 20:00:41 +00:00
|
|
|
db,
|
|
|
|
system: system.clone(),
|
2020-04-11 21:00:26 +00:00
|
|
|
block_manager,
|
2020-04-11 16:51:11 +00:00
|
|
|
background,
|
2020-04-19 15:15:48 +00:00
|
|
|
bucket_table,
|
2020-04-09 15:32:28 +00:00
|
|
|
object_table,
|
2020-04-09 21:45:07 +00:00
|
|
|
version_table,
|
2020-04-10 21:11:52 +00:00
|
|
|
block_ref_table,
|
2020-04-18 17:21:34 +00:00
|
|
|
});
|
2020-04-17 15:09:57 +00:00
|
|
|
|
2020-04-21 12:54:55 +00:00
|
|
|
info!("Crate admin RPC handler...");
|
2020-04-19 15:15:48 +00:00
|
|
|
AdminRpcHandler::new(garage.clone()).register_handler(rpc_server);
|
|
|
|
|
2020-04-21 12:54:55 +00:00
|
|
|
info!("Start block manager background thread...");
|
2020-04-17 15:09:57 +00:00
|
|
|
garage.block_manager.garage.swap(Some(garage.clone()));
|
|
|
|
garage.block_manager.clone().spawn_background_worker().await;
|
|
|
|
|
|
|
|
garage
|
2020-04-08 20:00:41 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-07 14:26:22 +00:00
|
|
|
|
|
|
|
fn read_config(config_file: PathBuf) -> Result<Config, Error> {
|
|
|
|
let mut file = std::fs::OpenOptions::new()
|
|
|
|
.read(true)
|
|
|
|
.open(config_file.as_path())?;
|
2020-04-10 20:01:48 +00:00
|
|
|
|
2020-04-07 14:26:22 +00:00
|
|
|
let mut config = String::new();
|
|
|
|
file.read_to_string(&mut config)?;
|
|
|
|
|
|
|
|
Ok(toml::from_str(&config)?)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn gen_node_id(metadata_dir: &PathBuf) -> Result<UUID, Error> {
|
|
|
|
let mut id_file = metadata_dir.clone();
|
|
|
|
id_file.push("node_id");
|
|
|
|
if id_file.as_path().exists() {
|
|
|
|
let mut f = std::fs::File::open(id_file.as_path())?;
|
|
|
|
let mut d = vec![];
|
|
|
|
f.read_to_end(&mut d)?;
|
|
|
|
if d.len() != 32 {
|
2020-04-10 20:01:48 +00:00
|
|
|
return Err(Error::Message(format!("Corrupt node_id file")));
|
2020-04-07 14:26:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut id = [0u8; 32];
|
|
|
|
id.copy_from_slice(&d[..]);
|
2020-04-07 16:10:20 +00:00
|
|
|
Ok(id.into())
|
2020-04-07 14:26:22 +00:00
|
|
|
} else {
|
2020-04-07 22:39:07 +00:00
|
|
|
let id = gen_uuid();
|
2020-04-07 14:26:22 +00:00
|
|
|
|
|
|
|
let mut f = std::fs::File::create(id_file.as_path())?;
|
2020-04-07 22:39:07 +00:00
|
|
|
f.write_all(id.as_slice())?;
|
|
|
|
Ok(id)
|
2020-04-07 14:26:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-11 16:51:11 +00:00
|
|
|
async fn shutdown_signal(send_cancel: watch::Sender<bool>) -> Result<(), Error> {
|
2020-04-10 20:01:48 +00:00
|
|
|
// Wait for the CTRL+C signal
|
|
|
|
tokio::signal::ctrl_c()
|
|
|
|
.await
|
|
|
|
.expect("failed to install CTRL+C signal handler");
|
2020-04-21 12:54:55 +00:00
|
|
|
info!("Received CTRL+C, shutting down.");
|
2020-04-11 16:51:11 +00:00
|
|
|
send_cancel.broadcast(true)?;
|
|
|
|
Ok(())
|
2020-04-07 14:26:22 +00:00
|
|
|
}
|
|
|
|
|
2020-04-11 16:51:11 +00:00
|
|
|
async fn wait_from(mut chan: watch::Receiver<bool>) -> () {
|
|
|
|
while let Some(exit_now) = chan.recv().await {
|
|
|
|
if exit_now {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2020-04-07 14:26:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn run_server(config_file: PathBuf) -> Result<(), Error> {
|
2020-04-21 12:54:55 +00:00
|
|
|
info!("Loading configuration...");
|
2020-04-10 20:01:48 +00:00
|
|
|
let config = read_config(config_file).expect("Unable to read config file");
|
2020-04-07 14:26:22 +00:00
|
|
|
|
2020-04-17 15:09:57 +00:00
|
|
|
let id = gen_node_id(&config.metadata_dir).expect("Unable to read or generate node ID");
|
2020-04-21 12:54:55 +00:00
|
|
|
info!("Node ID: {}", hex::encode(&id));
|
2020-04-17 15:09:57 +00:00
|
|
|
|
2020-04-21 12:54:55 +00:00
|
|
|
info!("Opening database...");
|
2020-04-08 20:00:41 +00:00
|
|
|
let mut db_path = config.metadata_dir.clone();
|
2020-04-09 16:43:53 +00:00
|
|
|
db_path.push("db");
|
2020-04-10 20:01:48 +00:00
|
|
|
let db = sled::open(db_path).expect("Unable to open DB");
|
2020-04-08 20:00:41 +00:00
|
|
|
|
2020-04-21 12:54:55 +00:00
|
|
|
info!("Initialize RPC server...");
|
2020-04-19 17:59:59 +00:00
|
|
|
let mut rpc_server = RpcServer::new(config.rpc_bind_addr.clone(), config.rpc_tls.clone());
|
2020-04-07 14:26:22 +00:00
|
|
|
|
2020-04-21 12:54:55 +00:00
|
|
|
info!("Initializing background runner...");
|
2020-04-18 17:21:34 +00:00
|
|
|
let (send_cancel, watch_cancel) = watch::channel(false);
|
2020-04-23 14:40:59 +00:00
|
|
|
let background = BackgroundRunner::new(16, watch_cancel.clone());
|
2020-04-17 15:09:57 +00:00
|
|
|
|
2020-04-18 17:21:34 +00:00
|
|
|
let garage = Garage::new(config, id, db, background.clone(), &mut rpc_server).await;
|
2020-04-07 14:26:22 +00:00
|
|
|
|
2020-04-21 12:54:55 +00:00
|
|
|
info!("Initializing RPC and API servers...");
|
2020-04-18 17:21:34 +00:00
|
|
|
let run_rpc_server = Arc::new(rpc_server).run(wait_from(watch_cancel.clone()));
|
2020-04-11 16:51:11 +00:00
|
|
|
let api_server = api_server::run_api_server(garage.clone(), wait_from(watch_cancel.clone()));
|
2020-04-07 14:26:22 +00:00
|
|
|
|
2020-04-11 16:51:11 +00:00
|
|
|
futures::try_join!(
|
2020-04-19 17:59:59 +00:00
|
|
|
garage.system.clone().bootstrap().map(|rv| {
|
2020-04-21 12:54:55 +00:00
|
|
|
info!("Bootstrap done");
|
2020-04-19 17:59:59 +00:00
|
|
|
Ok(rv)
|
|
|
|
}),
|
|
|
|
run_rpc_server.map(|rv| {
|
2020-04-21 12:54:55 +00:00
|
|
|
info!("RPC server exited");
|
2020-04-19 17:59:59 +00:00
|
|
|
rv
|
|
|
|
}),
|
|
|
|
api_server.map(|rv| {
|
2020-04-21 12:54:55 +00:00
|
|
|
info!("API server exited");
|
2020-04-19 17:59:59 +00:00
|
|
|
rv
|
|
|
|
}),
|
|
|
|
background.run().map(|rv| {
|
2020-04-21 12:54:55 +00:00
|
|
|
info!("Background runner exited");
|
2020-04-19 17:59:59 +00:00
|
|
|
Ok(rv)
|
|
|
|
}),
|
2020-04-11 16:51:11 +00:00
|
|
|
shutdown_signal(send_cancel),
|
|
|
|
)?;
|
2020-04-19 17:59:59 +00:00
|
|
|
|
2020-04-21 12:54:55 +00:00
|
|
|
info!("Cleaning up...");
|
2020-04-19 17:59:59 +00:00
|
|
|
|
2020-04-07 14:26:22 +00:00
|
|
|
Ok(())
|
|
|
|
}
|