2022-05-24 10:16:39 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::net::SocketAddr;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use hyper::{Body, Request, Response, StatusCode};
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
|
|
use garage_util::crdt::*;
|
|
|
|
use garage_util::data::*;
|
|
|
|
|
|
|
|
use garage_rpc::layout::*;
|
2022-12-05 14:38:32 +00:00
|
|
|
use garage_rpc::system::ClusterHealthStatus;
|
2022-05-24 10:16:39 +00:00
|
|
|
|
|
|
|
use garage_model::garage::Garage;
|
|
|
|
|
|
|
|
use crate::admin::error::*;
|
2022-05-25 15:05:56 +00:00
|
|
|
use crate::helpers::{json_ok_response, parse_json_body};
|
2022-05-24 10:16:39 +00:00
|
|
|
|
|
|
|
pub async fn handle_get_cluster_status(garage: &Arc<Garage>) -> Result<Response<Body>, Error> {
|
|
|
|
let res = GetClusterStatusResponse {
|
|
|
|
node: hex::encode(garage.system.id),
|
2022-09-07 16:36:46 +00:00
|
|
|
garage_version: garage_util::version::garage_version(),
|
|
|
|
garage_features: garage_util::version::garage_features(),
|
2022-06-08 08:01:44 +00:00
|
|
|
db_engine: garage.db.engine(),
|
2022-05-24 10:16:39 +00:00
|
|
|
known_nodes: garage
|
|
|
|
.system
|
|
|
|
.get_known_nodes()
|
|
|
|
.into_iter()
|
|
|
|
.map(|i| {
|
|
|
|
(
|
|
|
|
hex::encode(i.id),
|
|
|
|
KnownNodeResp {
|
|
|
|
addr: i.addr,
|
|
|
|
is_up: i.is_up,
|
|
|
|
last_seen_secs_ago: i.last_seen_secs_ago,
|
|
|
|
hostname: i.status.hostname,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
layout: get_cluster_layout(garage),
|
|
|
|
};
|
|
|
|
|
2022-05-25 15:05:56 +00:00
|
|
|
Ok(json_ok_response(&res)?)
|
2022-05-24 10:16:39 +00:00
|
|
|
}
|
|
|
|
|
2022-12-05 14:38:32 +00:00
|
|
|
pub async fn handle_get_cluster_health(garage: &Arc<Garage>) -> Result<Response<Body>, Error> {
|
|
|
|
let health = garage.system.health();
|
|
|
|
|
|
|
|
let resp_json =
|
|
|
|
serde_json::to_string_pretty(&health).map_err(garage_util::error::Error::from)?;
|
|
|
|
Ok(Response::builder()
|
2022-12-11 17:11:12 +00:00
|
|
|
.status(StatusCode::OK)
|
2022-12-05 14:38:32 +00:00
|
|
|
.header(http::header::CONTENT_TYPE, "application/json")
|
|
|
|
.body(Body::from(resp_json))?)
|
|
|
|
}
|
|
|
|
|
2022-05-24 10:16:39 +00:00
|
|
|
pub async fn handle_connect_cluster_nodes(
|
|
|
|
garage: &Arc<Garage>,
|
|
|
|
req: Request<Body>,
|
|
|
|
) -> Result<Response<Body>, Error> {
|
|
|
|
let req = parse_json_body::<Vec<String>>(req).await?;
|
|
|
|
|
|
|
|
let res = futures::future::join_all(req.iter().map(|node| garage.system.connect(node)))
|
|
|
|
.await
|
|
|
|
.into_iter()
|
|
|
|
.map(|r| match r {
|
|
|
|
Ok(()) => ConnectClusterNodesResponse {
|
|
|
|
success: true,
|
|
|
|
error: None,
|
|
|
|
},
|
|
|
|
Err(e) => ConnectClusterNodesResponse {
|
|
|
|
success: false,
|
|
|
|
error: Some(format!("{}", e)),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
2022-05-25 15:05:56 +00:00
|
|
|
Ok(json_ok_response(&res)?)
|
2022-05-24 10:16:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn handle_get_cluster_layout(garage: &Arc<Garage>) -> Result<Response<Body>, Error> {
|
|
|
|
let res = get_cluster_layout(garage);
|
2022-05-25 15:05:56 +00:00
|
|
|
|
|
|
|
Ok(json_ok_response(&res)?)
|
2022-05-24 10:16:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn get_cluster_layout(garage: &Arc<Garage>) -> GetClusterLayoutResponse {
|
|
|
|
let layout = garage.system.get_cluster_layout();
|
|
|
|
|
|
|
|
GetClusterLayoutResponse {
|
|
|
|
version: layout.version,
|
|
|
|
roles: layout
|
|
|
|
.roles
|
|
|
|
.items()
|
|
|
|
.iter()
|
|
|
|
.filter(|(_, _, v)| v.0.is_some())
|
|
|
|
.map(|(k, _, v)| (hex::encode(k), v.0.clone()))
|
|
|
|
.collect(),
|
|
|
|
staged_role_changes: layout
|
|
|
|
.staging
|
|
|
|
.items()
|
|
|
|
.iter()
|
|
|
|
.filter(|(k, _, v)| layout.roles.get(k) != Some(v))
|
|
|
|
.map(|(k, _, v)| (hex::encode(k), v.0.clone()))
|
|
|
|
.collect(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct GetClusterStatusResponse {
|
|
|
|
node: String,
|
|
|
|
garage_version: &'static str,
|
2022-09-07 15:05:21 +00:00
|
|
|
garage_features: Option<&'static [&'static str]>,
|
2022-06-08 08:01:44 +00:00
|
|
|
db_engine: String,
|
2022-05-24 10:16:39 +00:00
|
|
|
known_nodes: HashMap<String, KnownNodeResp>,
|
|
|
|
layout: GetClusterLayoutResponse,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
struct ConnectClusterNodesResponse {
|
|
|
|
success: bool,
|
|
|
|
error: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct GetClusterLayoutResponse {
|
|
|
|
version: u64,
|
|
|
|
roles: HashMap<String, Option<NodeRole>>,
|
|
|
|
staged_role_changes: HashMap<String, Option<NodeRole>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
struct KnownNodeResp {
|
|
|
|
addr: SocketAddr,
|
|
|
|
is_up: bool,
|
|
|
|
last_seen_secs_ago: Option<u64>,
|
|
|
|
hostname: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn handle_update_cluster_layout(
|
|
|
|
garage: &Arc<Garage>,
|
|
|
|
req: Request<Body>,
|
|
|
|
) -> Result<Response<Body>, Error> {
|
|
|
|
let updates = parse_json_body::<UpdateClusterLayoutRequest>(req).await?;
|
|
|
|
|
|
|
|
let mut layout = garage.system.get_cluster_layout();
|
|
|
|
|
|
|
|
let mut roles = layout.roles.clone();
|
|
|
|
roles.merge(&layout.staging);
|
|
|
|
|
|
|
|
for (node, role) in updates {
|
|
|
|
let node = hex::decode(node).ok_or_bad_request("Invalid node identifier")?;
|
|
|
|
let node = Uuid::try_from(&node).ok_or_bad_request("Invalid node identifier")?;
|
|
|
|
|
|
|
|
layout
|
|
|
|
.staging
|
|
|
|
.merge(&roles.update_mutator(node, NodeRoleV(role)));
|
|
|
|
}
|
|
|
|
|
|
|
|
garage.system.update_cluster_layout(&layout).await?;
|
|
|
|
|
|
|
|
Ok(Response::builder()
|
2022-10-16 17:46:15 +00:00
|
|
|
.status(StatusCode::NO_CONTENT)
|
2022-05-24 10:16:39 +00:00
|
|
|
.body(Body::empty())?)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn handle_apply_cluster_layout(
|
|
|
|
garage: &Arc<Garage>,
|
|
|
|
req: Request<Body>,
|
|
|
|
) -> Result<Response<Body>, Error> {
|
|
|
|
let param = parse_json_body::<ApplyRevertLayoutRequest>(req).await?;
|
|
|
|
|
|
|
|
let layout = garage.system.get_cluster_layout();
|
|
|
|
let layout = layout.apply_staged_changes(Some(param.version))?;
|
|
|
|
garage.system.update_cluster_layout(&layout).await?;
|
|
|
|
|
|
|
|
Ok(Response::builder()
|
2022-10-16 17:46:15 +00:00
|
|
|
.status(StatusCode::NO_CONTENT)
|
2022-05-24 10:16:39 +00:00
|
|
|
.body(Body::empty())?)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn handle_revert_cluster_layout(
|
|
|
|
garage: &Arc<Garage>,
|
|
|
|
req: Request<Body>,
|
|
|
|
) -> Result<Response<Body>, Error> {
|
|
|
|
let param = parse_json_body::<ApplyRevertLayoutRequest>(req).await?;
|
|
|
|
|
|
|
|
let layout = garage.system.get_cluster_layout();
|
|
|
|
let layout = layout.revert_staged_changes(Some(param.version))?;
|
|
|
|
garage.system.update_cluster_layout(&layout).await?;
|
|
|
|
|
|
|
|
Ok(Response::builder()
|
2022-10-16 17:46:15 +00:00
|
|
|
.status(StatusCode::NO_CONTENT)
|
2022-05-24 10:16:39 +00:00
|
|
|
.body(Body::empty())?)
|
|
|
|
}
|
|
|
|
|
|
|
|
type UpdateClusterLayoutRequest = HashMap<String, Option<NodeRole>>;
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct ApplyRevertLayoutRequest {
|
|
|
|
version: u64,
|
|
|
|
}
|