2022-05-24 10:16:39 +00:00
|
|
|
use std::net::SocketAddr;
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
2023-06-14 15:13:41 +00:00
|
|
|
use hyper::{Body, Request, Response};
|
2022-05-24 10:16:39 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
|
|
use garage_util::crdt::*;
|
|
|
|
use garage_util::data::*;
|
|
|
|
|
2023-06-14 11:45:27 +00:00
|
|
|
use garage_rpc::layout;
|
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(),
|
2023-03-10 14:45:18 +00:00
|
|
|
rust_version: garage_util::version::rust_version(),
|
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()
|
2023-06-14 11:45:27 +00:00
|
|
|
.map(|i| KnownNodeResp {
|
|
|
|
id: hex::encode(i.id),
|
|
|
|
addr: i.addr,
|
|
|
|
is_up: i.is_up,
|
|
|
|
last_seen_secs_ago: i.last_seen_secs_ago,
|
|
|
|
hostname: i.status.hostname,
|
2022-05-24 10:16:39 +00:00
|
|
|
})
|
|
|
|
.collect(),
|
2023-06-14 15:12:37 +00:00
|
|
|
layout: format_cluster_layout(&garage.system.get_cluster_layout()),
|
2022-05-24 10:16:39 +00:00
|
|
|
};
|
|
|
|
|
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> {
|
2023-06-14 11:53:19 +00:00
|
|
|
use garage_rpc::system::ClusterHealthStatus;
|
2022-12-05 14:38:32 +00:00
|
|
|
let health = garage.system.health();
|
2023-06-14 11:53:19 +00:00
|
|
|
let health = ClusterHealth {
|
|
|
|
status: match health.status {
|
|
|
|
ClusterHealthStatus::Healthy => "healthy",
|
|
|
|
ClusterHealthStatus::Degraded => "degraded",
|
|
|
|
ClusterHealthStatus::Unavailable => "unavailable",
|
|
|
|
},
|
|
|
|
known_nodes: health.known_nodes,
|
|
|
|
connected_nodes: health.connected_nodes,
|
|
|
|
storage_nodes: health.storage_nodes,
|
|
|
|
storage_nodes_ok: health.storage_nodes_ok,
|
|
|
|
partitions: health.partitions,
|
|
|
|
partitions_quorum: health.partitions_quorum,
|
|
|
|
partitions_all_ok: health.partitions_all_ok,
|
|
|
|
};
|
2022-12-11 17:17:08 +00:00
|
|
|
Ok(json_ok_response(&health)?)
|
2022-12-05 14:38:32 +00:00
|
|
|
}
|
|
|
|
|
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> {
|
2023-06-14 15:12:37 +00:00
|
|
|
let res = format_cluster_layout(&garage.system.get_cluster_layout());
|
2022-05-25 15:05:56 +00:00
|
|
|
|
|
|
|
Ok(json_ok_response(&res)?)
|
2022-05-24 10:16:39 +00:00
|
|
|
}
|
|
|
|
|
2023-06-14 15:12:37 +00:00
|
|
|
fn format_cluster_layout(layout: &layout::ClusterLayout) -> GetClusterLayoutResponse {
|
2023-06-14 11:45:27 +00:00
|
|
|
let roles = layout
|
|
|
|
.roles
|
|
|
|
.items()
|
|
|
|
.iter()
|
|
|
|
.filter_map(|(k, _, v)| v.0.clone().map(|x| (k, x)))
|
|
|
|
.map(|(k, v)| NodeRoleResp {
|
|
|
|
id: hex::encode(k),
|
|
|
|
zone: v.zone.clone(),
|
|
|
|
capacity: v.capacity,
|
|
|
|
tags: v.tags.clone(),
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
let staged_role_changes = layout
|
|
|
|
.staging_roles
|
|
|
|
.items()
|
|
|
|
.iter()
|
|
|
|
.filter(|(k, _, v)| layout.roles.get(k) != Some(v))
|
|
|
|
.map(|(k, _, v)| match &v.0 {
|
|
|
|
None => NodeRoleChange {
|
|
|
|
id: hex::encode(k),
|
2023-06-14 15:12:37 +00:00
|
|
|
action: NodeRoleChangeEnum::Remove { remove: true },
|
2023-06-14 11:45:27 +00:00
|
|
|
},
|
|
|
|
Some(r) => NodeRoleChange {
|
|
|
|
id: hex::encode(k),
|
2023-06-14 15:12:37 +00:00
|
|
|
action: NodeRoleChangeEnum::Update {
|
|
|
|
zone: r.zone.clone(),
|
|
|
|
capacity: r.capacity,
|
|
|
|
tags: r.tags.clone(),
|
|
|
|
},
|
2023-06-14 11:45:27 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
2022-05-24 10:16:39 +00:00
|
|
|
GetClusterLayoutResponse {
|
|
|
|
version: layout.version,
|
2023-06-14 11:45:27 +00:00
|
|
|
roles,
|
|
|
|
staged_role_changes,
|
2022-05-24 10:16:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-14 11:45:27 +00:00
|
|
|
// ----
|
|
|
|
|
2023-06-14 11:53:19 +00:00
|
|
|
#[derive(Debug, Clone, Copy, Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct ClusterHealth {
|
2023-06-14 15:12:37 +00:00
|
|
|
status: &'static str,
|
|
|
|
known_nodes: usize,
|
|
|
|
connected_nodes: usize,
|
|
|
|
storage_nodes: usize,
|
|
|
|
storage_nodes_ok: usize,
|
|
|
|
partitions: usize,
|
|
|
|
partitions_quorum: usize,
|
|
|
|
partitions_all_ok: usize,
|
2023-06-14 11:53:19 +00:00
|
|
|
}
|
|
|
|
|
2022-05-24 10:16:39 +00:00
|
|
|
#[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]>,
|
2023-03-10 14:45:18 +00:00
|
|
|
rust_version: &'static str,
|
2022-06-08 08:01:44 +00:00
|
|
|
db_engine: String,
|
2023-06-14 11:45:27 +00:00
|
|
|
known_nodes: Vec<KnownNodeResp>,
|
2022-05-24 10:16:39 +00:00
|
|
|
layout: GetClusterLayoutResponse,
|
|
|
|
}
|
|
|
|
|
2023-06-14 15:12:37 +00:00
|
|
|
#[derive(Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct ApplyClusterLayoutResponse {
|
2023-06-14 15:13:41 +00:00
|
|
|
message: Vec<String>,
|
|
|
|
layout: GetClusterLayoutResponse,
|
2023-06-14 15:12:37 +00:00
|
|
|
}
|
|
|
|
|
2022-05-24 10:16:39 +00:00
|
|
|
#[derive(Serialize)]
|
2023-06-13 14:15:50 +00:00
|
|
|
#[serde(rename_all = "camelCase")]
|
2022-05-24 10:16:39 +00:00
|
|
|
struct ConnectClusterNodesResponse {
|
|
|
|
success: bool,
|
|
|
|
error: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct GetClusterLayoutResponse {
|
|
|
|
version: u64,
|
2023-06-14 11:45:27 +00:00
|
|
|
roles: Vec<NodeRoleResp>,
|
|
|
|
staged_role_changes: Vec<NodeRoleChange>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct NodeRoleResp {
|
|
|
|
id: String,
|
|
|
|
zone: String,
|
|
|
|
capacity: Option<u64>,
|
|
|
|
tags: Vec<String>,
|
2022-05-24 10:16:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize)]
|
2023-06-13 14:15:50 +00:00
|
|
|
#[serde(rename_all = "camelCase")]
|
2022-05-24 10:16:39 +00:00
|
|
|
struct KnownNodeResp {
|
2023-06-14 11:45:27 +00:00
|
|
|
id: String,
|
2022-05-24 10:16:39 +00:00
|
|
|
addr: SocketAddr,
|
|
|
|
is_up: bool,
|
|
|
|
last_seen_secs_ago: Option<u64>,
|
|
|
|
hostname: String,
|
|
|
|
}
|
|
|
|
|
2023-06-14 11:45:27 +00:00
|
|
|
// ---- update functions ----
|
|
|
|
|
2022-05-24 10:16:39 +00:00
|
|
|
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();
|
2022-11-07 18:34:40 +00:00
|
|
|
roles.merge(&layout.staging_roles);
|
2022-05-24 10:16:39 +00:00
|
|
|
|
2023-06-14 11:45:27 +00:00
|
|
|
for change in updates {
|
|
|
|
let node = hex::decode(&change.id).ok_or_bad_request("Invalid node identifier")?;
|
2022-05-24 10:16:39 +00:00
|
|
|
let node = Uuid::try_from(&node).ok_or_bad_request("Invalid node identifier")?;
|
|
|
|
|
2023-06-14 15:12:37 +00:00
|
|
|
let new_role = match change.action {
|
|
|
|
NodeRoleChangeEnum::Remove { remove: true } => None,
|
|
|
|
NodeRoleChangeEnum::Update {
|
|
|
|
zone,
|
|
|
|
capacity,
|
|
|
|
tags,
|
|
|
|
} => Some(layout::NodeRole {
|
2023-06-14 11:45:27 +00:00
|
|
|
zone,
|
|
|
|
capacity,
|
|
|
|
tags,
|
|
|
|
}),
|
|
|
|
_ => return Err(Error::bad_request("Invalid layout change")),
|
|
|
|
};
|
|
|
|
|
2022-05-24 10:16:39 +00:00
|
|
|
layout
|
2022-11-07 18:34:40 +00:00
|
|
|
.staging_roles
|
2023-06-14 11:45:27 +00:00
|
|
|
.merge(&roles.update_mutator(node, layout::NodeRoleV(new_role)));
|
2022-05-24 10:16:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
garage.system.update_cluster_layout(&layout).await?;
|
|
|
|
|
2023-06-14 15:12:37 +00:00
|
|
|
let res = format_cluster_layout(&layout);
|
2023-06-14 15:13:41 +00:00
|
|
|
Ok(json_ok_response(&res)?)
|
2022-05-24 10:16:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2022-10-05 13:29:48 +00:00
|
|
|
let (layout, msg) = layout.apply_staged_changes(Some(param.version))?;
|
|
|
|
|
2022-05-24 10:16:39 +00:00
|
|
|
garage.system.update_cluster_layout(&layout).await?;
|
|
|
|
|
2023-06-14 15:12:37 +00:00
|
|
|
let res = ApplyClusterLayoutResponse {
|
2023-06-14 15:13:41 +00:00
|
|
|
message: msg,
|
|
|
|
layout: format_cluster_layout(&layout),
|
|
|
|
};
|
|
|
|
Ok(json_ok_response(&res)?)
|
2022-05-24 10:16:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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?;
|
|
|
|
|
2023-06-14 15:12:37 +00:00
|
|
|
let res = format_cluster_layout(&layout);
|
2023-06-14 15:13:41 +00:00
|
|
|
Ok(json_ok_response(&res)?)
|
2022-05-24 10:16:39 +00:00
|
|
|
}
|
|
|
|
|
2023-06-14 11:45:27 +00:00
|
|
|
// ----
|
|
|
|
|
|
|
|
type UpdateClusterLayoutRequest = Vec<NodeRoleChange>;
|
2022-05-24 10:16:39 +00:00
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
2023-06-13 14:15:50 +00:00
|
|
|
#[serde(rename_all = "camelCase")]
|
2022-05-24 10:16:39 +00:00
|
|
|
struct ApplyRevertLayoutRequest {
|
|
|
|
version: u64,
|
|
|
|
}
|
2023-06-14 11:45:27 +00:00
|
|
|
|
|
|
|
// ----
|
|
|
|
|
2023-06-14 15:12:37 +00:00
|
|
|
#[derive(Serialize, Deserialize)]
|
2023-06-14 11:45:27 +00:00
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
struct NodeRoleChange {
|
|
|
|
id: String,
|
2023-06-14 15:12:37 +00:00
|
|
|
#[serde(flatten)]
|
|
|
|
action: NodeRoleChangeEnum,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
#[serde(untagged)]
|
|
|
|
enum NodeRoleChangeEnum {
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
Remove { remove: bool },
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
Update {
|
|
|
|
zone: String,
|
|
|
|
capacity: Option<u64>,
|
|
|
|
tags: Vec<String>,
|
|
|
|
},
|
2023-06-14 11:45:27 +00:00
|
|
|
}
|