2020-04-08 20:00:41 +00:00
|
|
|
use std::collections::HashMap;
|
2020-04-07 14:26:22 +00:00
|
|
|
use std::io::{Read, Write};
|
|
|
|
use std::sync::Arc;
|
|
|
|
use std::net::SocketAddr;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use futures::channel::oneshot;
|
|
|
|
use serde::Deserialize;
|
2020-04-08 21:01:49 +00:00
|
|
|
use tokio::sync::RwLock;
|
2020-04-07 14:26:22 +00:00
|
|
|
|
2020-04-07 22:39:07 +00:00
|
|
|
use crate::data::*;
|
2020-04-08 20:00:41 +00:00
|
|
|
use crate::proto::*;
|
2020-04-07 14:26:22 +00:00
|
|
|
use crate::error::Error;
|
|
|
|
use crate::membership::System;
|
|
|
|
use crate::api_server;
|
|
|
|
use crate::rpc_server;
|
2020-04-08 20:00:41 +00:00
|
|
|
use crate::table::*;
|
|
|
|
|
|
|
|
pub struct Garage {
|
|
|
|
pub db: sled::Db,
|
|
|
|
pub system: Arc<System>,
|
|
|
|
|
|
|
|
pub table_rpc_handlers: HashMap<String, Box<dyn TableRpcHandler + Sync + Send>>,
|
|
|
|
|
2020-04-09 15:32:28 +00:00
|
|
|
pub object_table: Arc<Table<ObjectTable>>,
|
2020-04-08 20:00:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Garage {
|
2020-04-08 21:01:49 +00:00
|
|
|
pub async fn new(config: Config, id: UUID, db: sled::Db) -> Arc<Self> {
|
2020-04-08 20:00:41 +00:00
|
|
|
let system = Arc::new(System::new(config, id));
|
|
|
|
|
|
|
|
let meta_rep_param = TableReplicationParams{
|
|
|
|
replication_factor: system.config.meta_replication_factor,
|
|
|
|
write_quorum: (system.config.meta_replication_factor+1)/2,
|
|
|
|
read_quorum: (system.config.meta_replication_factor+1)/2,
|
|
|
|
timeout: DEFAULT_TIMEOUT,
|
|
|
|
};
|
|
|
|
|
2020-04-09 15:32:28 +00:00
|
|
|
let object_table = Arc::new(Table::new(
|
|
|
|
ObjectTable{garage: RwLock::new(None)},
|
2020-04-08 20:00:41 +00:00
|
|
|
system.clone(),
|
|
|
|
&db,
|
2020-04-09 15:32:28 +00:00
|
|
|
"object".to_string(),
|
2020-04-08 20:00:41 +00:00
|
|
|
meta_rep_param.clone()));
|
|
|
|
|
|
|
|
let mut garage = Self{
|
|
|
|
db,
|
|
|
|
system: system.clone(),
|
|
|
|
table_rpc_handlers: HashMap::new(),
|
2020-04-09 15:32:28 +00:00
|
|
|
object_table,
|
2020-04-08 20:00:41 +00:00
|
|
|
};
|
|
|
|
garage.table_rpc_handlers.insert(
|
2020-04-09 15:32:28 +00:00
|
|
|
garage.object_table.name.clone(),
|
|
|
|
garage.object_table.clone().rpc_handler());
|
2020-04-08 21:01:49 +00:00
|
|
|
|
|
|
|
let garage = Arc::new(garage);
|
2020-04-09 15:32:28 +00:00
|
|
|
*garage.object_table.instance.garage.write().await = Some(garage.clone());
|
2020-04-08 20:00:41 +00:00
|
|
|
garage
|
|
|
|
}
|
|
|
|
}
|
2020-04-07 14:26:22 +00:00
|
|
|
|
2020-04-07 22:39:07 +00:00
|
|
|
fn default_block_size() -> usize {
|
|
|
|
1048576
|
|
|
|
}
|
|
|
|
fn default_meta_replication_factor() -> usize {
|
|
|
|
3
|
|
|
|
}
|
|
|
|
|
2020-04-07 14:26:22 +00:00
|
|
|
#[derive(Deserialize, Debug)]
|
|
|
|
pub struct Config {
|
|
|
|
pub metadata_dir: PathBuf,
|
|
|
|
pub data_dir: PathBuf,
|
|
|
|
|
|
|
|
pub api_port: u16,
|
|
|
|
pub rpc_port: u16,
|
|
|
|
|
|
|
|
pub bootstrap_peers: Vec<SocketAddr>,
|
2020-04-07 22:39:07 +00:00
|
|
|
|
|
|
|
#[serde(default = "default_block_size")]
|
|
|
|
pub block_size: usize,
|
|
|
|
|
|
|
|
#[serde(default = "default_meta_replication_factor")]
|
|
|
|
pub meta_replication_factor: usize,
|
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())?;
|
|
|
|
|
|
|
|
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 {
|
|
|
|
return Err(Error::Message(format!("Corrupt node_id file")))
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn shutdown_signal(chans: Vec<oneshot::Sender<()>>) {
|
|
|
|
// Wait for the CTRL+C signal
|
|
|
|
tokio::signal::ctrl_c()
|
|
|
|
.await
|
|
|
|
.expect("failed to install CTRL+C signal handler");
|
|
|
|
println!("Received CTRL+C, shutting down.");
|
|
|
|
for ch in chans {
|
|
|
|
ch.send(()).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn wait_from(chan: oneshot::Receiver<()>) -> () {
|
|
|
|
chan.await.unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn run_server(config_file: PathBuf) -> Result<(), Error> {
|
|
|
|
let config = read_config(config_file)
|
|
|
|
.expect("Unable to read config file");
|
|
|
|
|
2020-04-08 20:00:41 +00:00
|
|
|
let mut db_path = config.metadata_dir.clone();
|
|
|
|
db_path.push("garage_metadata");
|
|
|
|
let db = sled::open(db_path)
|
|
|
|
.expect("Unable to open DB");
|
|
|
|
|
2020-04-07 14:26:22 +00:00
|
|
|
let id = gen_node_id(&config.metadata_dir)
|
|
|
|
.expect("Unable to read or generate node ID");
|
2020-04-07 16:10:20 +00:00
|
|
|
println!("Node ID: {}", hex::encode(&id));
|
2020-04-07 14:26:22 +00:00
|
|
|
|
2020-04-08 21:01:49 +00:00
|
|
|
let garage = Garage::new(config, id, db).await;
|
2020-04-07 14:26:22 +00:00
|
|
|
|
|
|
|
let (tx1, rx1) = oneshot::channel();
|
|
|
|
let (tx2, rx2) = oneshot::channel();
|
|
|
|
|
2020-04-08 20:00:41 +00:00
|
|
|
let rpc_server = rpc_server::run_rpc_server(garage.clone(), wait_from(rx1));
|
|
|
|
let api_server = api_server::run_api_server(garage.clone(), wait_from(rx2));
|
2020-04-07 14:26:22 +00:00
|
|
|
|
|
|
|
tokio::spawn(shutdown_signal(vec![tx1, tx2]));
|
2020-04-08 20:00:41 +00:00
|
|
|
tokio::spawn(garage.system.clone().bootstrap());
|
2020-04-07 14:26:22 +00:00
|
|
|
|
|
|
|
futures::try_join!(rpc_server, api_server)?;
|
|
|
|
Ok(())
|
|
|
|
}
|