diff options
author | Alex Auvolat <alex@adnab.me> | 2024-02-20 17:02:44 +0100 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2024-02-20 17:02:44 +0100 |
commit | 643d1aabd8f229545991217faebd09445aa9b523 (patch) | |
tree | 3a9533543ba44f7f9bdd188e6c9b07e7ea0500cf /src/rpc | |
parent | eb4a6ce1060a847be0b62c6a10ff3ba956e3f34d (diff) | |
parent | 885405d944f5f54bce12b53cf1d97f1ecd08887e (diff) | |
download | garage-643d1aabd8f229545991217faebd09445aa9b523.tar.gz garage-643d1aabd8f229545991217faebd09445aa9b523.zip |
Merge branch 'main' into next-0.10
Diffstat (limited to 'src/rpc')
-rw-r--r-- | src/rpc/system.rs | 129 | ||||
-rw-r--r-- | src/rpc/system_metrics.rs | 317 |
2 files changed, 328 insertions, 118 deletions
diff --git a/src/rpc/system.rs b/src/rpc/system.rs index 21156d15..e8844f29 100644 --- a/src/rpc/system.rs +++ b/src/rpc/system.rs @@ -3,11 +3,10 @@ use std::collections::{HashMap, HashSet}; use std::io::{Read, Write}; use std::net::{IpAddr, SocketAddr}; use std::path::{Path, PathBuf}; -use std::sync::atomic::Ordering; use std::sync::{Arc, RwLock, RwLockReadGuard}; use std::time::{Duration, Instant}; -use arc_swap::ArcSwap; +use arc_swap::ArcSwapOption; use async_trait::async_trait; use futures::join; use serde::{Deserialize, Serialize}; @@ -92,7 +91,7 @@ pub struct System { persist_peer_list: Persister<PeerList>, - local_status: ArcSwap<NodeStatus>, + pub(crate) local_status: RwLock<NodeStatus>, node_status: RwLock<HashMap<Uuid, (u64, NodeStatus)>>, pub netapp: Arc<NetApp>, @@ -101,7 +100,6 @@ pub struct System { pub(crate) system_endpoint: Arc<Endpoint<SystemRpc, System>>, rpc_listen_addr: SocketAddr, - #[cfg(any(feature = "consul-discovery", feature = "kubernetes-discovery"))] rpc_public_addr: Option<SocketAddr>, bootstrap_peers: Vec<String>, @@ -112,10 +110,10 @@ pub struct System { pub layout_manager: Arc<LayoutManager>, - metrics: SystemMetrics, + metrics: ArcSwapOption<SystemMetrics>, replication_mode: ReplicationMode, - replication_factor: usize, + pub(crate) replication_factor: usize, /// Path to metadata directory pub metadata_dir: PathBuf, @@ -171,7 +169,7 @@ pub struct ClusterHealth { pub partitions_all_ok: usize, } -#[derive(Debug, Clone, Copy)] +#[derive(Debug, Clone, Copy, Eq, PartialEq)] pub enum ClusterHealthStatus { /// All nodes are available Healthy, @@ -256,7 +254,10 @@ impl System { hex::encode(&node_key.public_key()[..8]) ); - let netapp = NetApp::new(GARAGE_VERSION_TAG, network_key, node_key); + let bind_outgoing_to = Some(config) + .filter(|x| x.rpc_bind_outgoing) + .map(|x| x.rpc_bind_addr.ip()); + let netapp = NetApp::new(GARAGE_VERSION_TAG, network_key, node_key, bind_outgoing_to); let system_endpoint = netapp.endpoint(SYSTEM_RPC_PATH.into()); // ---- setup netapp public listener and full mesh peering strategy ---- @@ -283,11 +284,8 @@ impl System { replication_mode, )?; - // ---- set up metrics and status exchange ---- - let metrics = SystemMetrics::new(replication_factor); - let mut local_status = NodeStatus::initial(replication_factor, &layout_manager); - local_status.update_disk_usage(&config.metadata_dir, &config.data_dir, &metrics); + local_status.update_disk_usage(&config.metadata_dir, &config.data_dir); // ---- if enabled, set up additionnal peer discovery methods ---- #[cfg(feature = "consul-discovery")] @@ -308,11 +306,11 @@ impl System { warn!("Kubernetes discovery is not enabled in this build."); } - // ---- done ---- + // ---- almost done ---- let sys = Arc::new(System { id: netapp.id.into(), persist_peer_list, - local_status: ArcSwap::new(Arc::new(local_status)), + local_status: RwLock::new(local_status), node_status: RwLock::new(HashMap::new()), netapp: netapp.clone(), peering: peering.clone(), @@ -320,7 +318,6 @@ impl System { replication_mode, replication_factor, rpc_listen_addr: config.rpc_bind_addr, - #[cfg(any(feature = "consul-discovery", feature = "kubernetes-discovery"))] rpc_public_addr, bootstrap_peers: config.bootstrap_peers.clone(), #[cfg(feature = "consul-discovery")] @@ -328,27 +325,39 @@ impl System { #[cfg(feature = "kubernetes-discovery")] kubernetes_discovery: config.kubernetes_discovery.clone(), layout_manager, - metrics, + metrics: ArcSwapOption::new(None), metadata_dir: config.metadata_dir.clone(), data_dir: config.data_dir.clone(), }); + sys.system_endpoint.set_handler(sys.clone()); + + let metrics = SystemMetrics::new(sys.clone()); + sys.metrics.store(Some(Arc::new(metrics))); + Ok(sys) } /// Perform bootstraping, starting the ping loop pub async fn run(self: Arc<Self>, must_exit: watch::Receiver<bool>) { join!( - self.netapp - .clone() - .listen(self.rpc_listen_addr, None, must_exit.clone()), + self.netapp.clone().listen( + self.rpc_listen_addr, + self.rpc_public_addr, + must_exit.clone() + ), self.peering.clone().run(must_exit.clone()), self.discovery_loop(must_exit.clone()), self.status_exchange_loop(must_exit.clone()), ); } + pub fn cleanup(&self) { + // Break reference cycle + self.metrics.store(None); + } + // ---- Public utilities / accessors ---- pub fn cluster_layout(&self) -> RwLockReadGuard<'_, LayoutHelper> { @@ -511,12 +520,9 @@ impl System { } }; + let hostname = self.local_status.read().unwrap().hostname.clone(); if let Err(e) = c - .publish_consul_service( - self.netapp.id, - &self.local_status.load_full().hostname.as_deref().unwrap(), - rpc_public_addr, - ) + .publish_consul_service(self.netapp.id, &hostname, rpc_public_addr) .await { error!("Error while publishing Consul service: {}", e); @@ -538,26 +544,17 @@ impl System { } }; - if let Err(e) = publish_kubernetes_node( - k, - self.netapp.id, - &self.local_status.load_full().hostname.as_deref().unwrap(), - rpc_public_addr, - ) - .await + let hostname = self.local_status.read().unwrap().hostname.clone(); + if let Err(e) = publish_kubernetes_node(k, self.netapp.id, &hostname, rpc_public_addr).await { error!("Error while publishing node to Kubernetes: {}", e); } } fn update_local_status(&self) { - let mut new_si: NodeStatus = self.local_status.load().as_ref().clone(); - - new_si.layout_digest = self.layout_manager.layout().digest(); - - new_si.update_disk_usage(&self.metadata_dir, &self.data_dir, &self.metrics); - - self.local_status.swap(Arc::new(new_si)); + let mut local_status = self.local_status.write().unwrap(); + local_status.layout_digest = self.layout_manager.layout().digest(); + local_status.update_disk_usage(&self.metadata_dir, &self.data_dir); } // --- RPC HANDLERS --- @@ -577,7 +574,7 @@ impl System { from: Uuid, info: &NodeStatus, ) -> Result<SystemRpc, Error> { - let local_info = self.local_status.load(); + let local_info = self.local_status.read().unwrap(); if local_info.replication_factor < info.replication_factor { error!("Some node have a higher replication factor ({}) than this one ({}). This is not supported and will lead to data corruption. Shutting down for safety.", @@ -589,6 +586,8 @@ impl System { self.layout_manager .handle_advertise_status(from, &info.layout_digest); + drop(local_info); + self.node_status .write() .unwrap() @@ -601,8 +600,10 @@ impl System { while !*stop_signal.borrow() { let restart_at = Instant::now() + STATUS_EXCHANGE_INTERVAL; + // Update local node status that is exchanged. self.update_local_status(); - let local_status: NodeStatus = self.local_status.load().as_ref().clone(); + + let local_status: NodeStatus = self.local_status.read().unwrap().clone(); let _ = self .rpc_helper() .broadcast( @@ -622,15 +623,17 @@ impl System { async fn discovery_loop(self: &Arc<Self>, mut stop_signal: watch::Receiver<bool>) { while !*stop_signal.borrow() { - let not_configured = self.cluster_layout().check().is_err(); - let no_peers = self.peering.get_peer_list().len() < self.replication_factor; - let expected_n_nodes = self.cluster_layout().all_nodes().len(); - let bad_peers = self + let n_connected = self .peering .get_peer_list() .iter() .filter(|p| p.is_up()) - .count() != expected_n_nodes; + .count(); + + let not_configured = self.cluster_layout().check().is_err(); + let no_peers = n_connected < self.replication_factor; + let expected_n_nodes = self.cluster_layout().all_nodes().len(); + let bad_peers = n_connected != expected_n_nodes; if not_configured || no_peers || bad_peers { info!("Doing a bootstrap/discovery step (not_configured: {}, no_peers: {}, bad_peers: {})", not_configured, no_peers, bad_peers); @@ -677,6 +680,14 @@ impl System { } } + if !not_configured && !no_peers { + // If the layout is configured, and we already have some connections + // to other nodes in the cluster, we can skip trying to connect to + // nodes that are not in the cluster layout. + let layout = self.cluster_layout(); + ping_list.retain(|(id, _)| layout.all_nodes().contains(&(*id).into())); + } + for (node_id, node_addr) in ping_list { let self2 = self.clone(); tokio::spawn(async move { @@ -787,12 +798,7 @@ impl NodeStatus { } } - fn update_disk_usage( - &mut self, - meta_dir: &Path, - data_dir: &DataDirEnum, - metrics: &SystemMetrics, - ) { + fn update_disk_usage(&mut self, meta_dir: &Path, data_dir: &DataDirEnum) { use nix::sys::statvfs::statvfs; let mount_avail = |path: &Path| match statvfs(path) { Ok(x) => { @@ -828,27 +834,6 @@ impl NodeStatus { ) })(), }; - - if let Some((avail, total)) = self.meta_disk_avail { - metrics - .values - .meta_disk_avail - .store(avail, Ordering::Relaxed); - metrics - .values - .meta_disk_total - .store(total, Ordering::Relaxed); - } - if let Some((avail, total)) = self.data_disk_avail { - metrics - .values - .data_disk_avail - .store(avail, Ordering::Relaxed); - metrics - .values - .data_disk_total - .store(total, Ordering::Relaxed); - } } } diff --git a/src/rpc/system_metrics.rs b/src/rpc/system_metrics.rs index af81b71f..fb3c983c 100644 --- a/src/rpc/system_metrics.rs +++ b/src/rpc/system_metrics.rs @@ -1,32 +1,57 @@ -use std::sync::atomic::{AtomicU64, Ordering}; -use std::sync::Arc; +use std::sync::{Arc, RwLock}; +use std::time::{Duration, Instant}; use opentelemetry::{global, metrics::*, KeyValue}; +use crate::system::{ClusterHealthStatus, System}; + /// TableMetrics reference all counter used for metrics pub struct SystemMetrics { + // Static values pub(crate) _garage_build_info: ValueObserver<u64>, pub(crate) _replication_factor: ValueObserver<u64>, + + // Disk space values from System::local_status pub(crate) _disk_avail: ValueObserver<u64>, pub(crate) _disk_total: ValueObserver<u64>, - pub(crate) values: Arc<SystemMetricsValues>, -} -#[derive(Default)] -pub struct SystemMetricsValues { - pub(crate) data_disk_total: AtomicU64, - pub(crate) data_disk_avail: AtomicU64, - pub(crate) meta_disk_total: AtomicU64, - pub(crate) meta_disk_avail: AtomicU64, + // Health report from System::health() + pub(crate) _cluster_healthy: ValueObserver<u64>, + pub(crate) _cluster_available: ValueObserver<u64>, + pub(crate) _known_nodes: ValueObserver<u64>, + pub(crate) _connected_nodes: ValueObserver<u64>, + pub(crate) _storage_nodes: ValueObserver<u64>, + pub(crate) _storage_nodes_ok: ValueObserver<u64>, + pub(crate) _partitions: ValueObserver<u64>, + pub(crate) _partitions_quorum: ValueObserver<u64>, + pub(crate) _partitions_all_ok: ValueObserver<u64>, + + // Status report for individual cluster nodes + pub(crate) _layout_node_connected: ValueObserver<u64>, + pub(crate) _layout_node_disconnected_time: ValueObserver<u64>, } impl SystemMetrics { - pub fn new(replication_factor: usize) -> Self { + pub fn new(system: Arc<System>) -> Self { let meter = global::meter("garage_system"); - let values = Arc::new(SystemMetricsValues::default()); - let values1 = values.clone(); - let values2 = values.clone(); + + let health_cache = RwLock::new((Instant::now(), system.health())); + let system2 = system.clone(); + let get_health = Arc::new(move || { + { + let cache = health_cache.read().unwrap(); + if cache.0 > Instant::now() - Duration::from_secs(1) { + return cache.1; + } + } + + let health = system2.health(); + *health_cache.write().unwrap() = (Instant::now(), health); + health + }); + Self { + // Static values _garage_build_info: meter .u64_value_observer("garage_build_info", move |observer| { observer.observe( @@ -39,39 +64,239 @@ impl SystemMetrics { }) .with_description("Garage build info") .init(), - _replication_factor: meter - .u64_value_observer("garage_replication_factor", move |observer| { - observer.observe(replication_factor as u64, &[]) - }) - .with_description("Garage replication factor setting") - .init(), - _disk_avail: meter - .u64_value_observer("garage_local_disk_avail", move |observer| { - match values1.data_disk_avail.load(Ordering::Relaxed) { - 0 => (), - x => observer.observe(x, &[KeyValue::new("volume", "data")]), - }; - match values1.meta_disk_avail.load(Ordering::Relaxed) { - 0 => (), - x => observer.observe(x, &[KeyValue::new("volume", "metadata")]), - }; - }) - .with_description("Garage available disk space on each node") - .init(), - _disk_total: meter - .u64_value_observer("garage_local_disk_total", move |observer| { - match values2.data_disk_total.load(Ordering::Relaxed) { - 0 => (), - x => observer.observe(x, &[KeyValue::new("volume", "data")]), - }; - match values2.meta_disk_total.load(Ordering::Relaxed) { - 0 => (), - x => observer.observe(x, &[KeyValue::new("volume", "metadata")]), - }; + _replication_factor: { + let replication_factor = system.replication_factor; + meter + .u64_value_observer("garage_replication_factor", move |observer| { + observer.observe(replication_factor as u64, &[]) + }) + .with_description("Garage replication factor setting") + .init() + }, + + // Disk space values from System::local_status + _disk_avail: { + let system = system.clone(); + meter + .u64_value_observer("garage_local_disk_avail", move |observer| { + let st = system.local_status.read().unwrap(); + if let Some((avail, _total)) = st.data_disk_avail { + observer.observe(avail, &[KeyValue::new("volume", "data")]); + } + if let Some((avail, _total)) = st.meta_disk_avail { + observer.observe(avail, &[KeyValue::new("volume", "metadata")]); + } + }) + .with_description("Garage available disk space on each node") + .init() + }, + _disk_total: { + let system = system.clone(); + meter + .u64_value_observer("garage_local_disk_total", move |observer| { + let st = system.local_status.read().unwrap(); + if let Some((_avail, total)) = st.data_disk_avail { + observer.observe(total, &[KeyValue::new("volume", "data")]); + } + if let Some((_avail, total)) = st.meta_disk_avail { + observer.observe(total, &[KeyValue::new("volume", "metadata")]); + } + }) + .with_description("Garage total disk space on each node") + .init() + }, + + // Health report from System::() + _cluster_healthy: { + let get_health = get_health.clone(); + meter + .u64_value_observer("cluster_healthy", move |observer| { + let h = get_health(); + if h.status == ClusterHealthStatus::Healthy { + observer.observe(1, &[]); + } else { + observer.observe(0, &[]); + } + }) + .with_description("Whether all storage nodes are connected") + .init() + }, + _cluster_available: { + let get_health = get_health.clone(); + meter.u64_value_observer("cluster_available", move |observer| { + let h = get_health(); + if h.status != ClusterHealthStatus::Unavailable { + observer.observe(1, &[]); + } else { + observer.observe(0, &[]); + } }) - .with_description("Garage total disk space on each node") - .init(), - values, + .with_description("Whether all requests can be served, even if some storage nodes are disconnected") + .init() + }, + _known_nodes: { + let get_health = get_health.clone(); + meter + .u64_value_observer("cluster_known_nodes", move |observer| { + let h = get_health(); + observer.observe(h.known_nodes as u64, &[]); + }) + .with_description("Number of nodes already seen once in the cluster") + .init() + }, + _connected_nodes: { + let get_health = get_health.clone(); + meter + .u64_value_observer("cluster_connected_nodes", move |observer| { + let h = get_health(); + observer.observe(h.connected_nodes as u64, &[]); + }) + .with_description("Number of nodes currently connected") + .init() + }, + _storage_nodes: { + let get_health = get_health.clone(); + meter + .u64_value_observer("cluster_storage_nodes", move |observer| { + let h = get_health(); + observer.observe(h.storage_nodes as u64, &[]); + }) + .with_description("Number of storage nodes declared in the current layout") + .init() + }, + _storage_nodes_ok: { + let get_health = get_health.clone(); + meter + .u64_value_observer("cluster_storage_nodes_ok", move |observer| { + let h = get_health(); + observer.observe(h.storage_nodes_ok as u64, &[]); + }) + .with_description("Number of storage nodes currently connected") + .init() + }, + _partitions: { + let get_health = get_health.clone(); + meter + .u64_value_observer("cluster_partitions", move |observer| { + let h = get_health(); + observer.observe(h.partitions as u64, &[]); + }) + .with_description("Number of partitions in the layout") + .init() + }, + _partitions_quorum: { + let get_health = get_health.clone(); + meter + .u64_value_observer("cluster_partitions_quorum", move |observer| { + let h = get_health(); + observer.observe(h.partitions_quorum as u64, &[]); + }) + .with_description( + "Number of partitions for which we have a quorum of connected nodes", + ) + .init() + }, + _partitions_all_ok: { + let get_health = get_health.clone(); + meter + .u64_value_observer("cluster_partitions_all_ok", move |observer| { + let h = get_health(); + observer.observe(h.partitions_all_ok as u64, &[]); + }) + .with_description( + "Number of partitions for which all storage nodes are connected", + ) + .init() + }, + + // Status report for individual cluster nodes + _layout_node_connected: { + let system = system.clone(); + meter + .u64_value_observer("cluster_layout_node_connected", move |observer| { + let layout = system.cluster_layout(); + let nodes = system.get_known_nodes(); + for (id, _, config) in layout.current().roles.items().iter() { + if let Some(role) = &config.0 { + let mut kv = vec![ + KeyValue::new("id", format!("{:?}", id)), + KeyValue::new("role_zone", role.zone.clone()), + ]; + match role.capacity { + Some(cap) => { + kv.push(KeyValue::new("role_capacity", cap as i64)); + kv.push(KeyValue::new("role_gateway", 0)); + } + None => { + kv.push(KeyValue::new("role_gateway", 1)); + } + } + + let value; + if let Some(node) = nodes.iter().find(|n| n.id == *id) { + value = if node.is_up { 1 } else { 0 }; + // TODO: if we add address and hostname, and those change, we + // get duplicate metrics, due to bad otel aggregation :( + // Can probably be fixed when we upgrade opentelemetry + // kv.push(KeyValue::new("address", node.addr.to_string())); + // kv.push(KeyValue::new( + // "hostname", + // node.status.hostname.clone(), + // )); + } else { + value = 0; + } + + observer.observe(value, &kv); + } + } + }) + .with_description("Connection status for nodes in the cluster layout") + .init() + }, + _layout_node_disconnected_time: { + let system = system.clone(); + meter + .u64_value_observer("cluster_layout_node_disconnected_time", move |observer| { + let layout = system.cluster_layout(); + let nodes = system.get_known_nodes(); + for (id, _, config) in layout.current().roles.items().iter() { + if let Some(role) = &config.0 { + let mut kv = vec![ + KeyValue::new("id", format!("{:?}", id)), + KeyValue::new("role_zone", role.zone.clone()), + ]; + match role.capacity { + Some(cap) => { + kv.push(KeyValue::new("role_capacity", cap as i64)); + kv.push(KeyValue::new("role_gateway", 0)); + } + None => { + kv.push(KeyValue::new("role_gateway", 1)); + } + } + + if let Some(node) = nodes.iter().find(|n| n.id == *id) { + // TODO: see comment above + // kv.push(KeyValue::new("address", node.addr.to_string())); + // kv.push(KeyValue::new( + // "hostname", + // node.status.hostname.clone(), + // )); + if node.is_up { + observer.observe(0, &kv); + } else if let Some(secs) = node.last_seen_secs_ago { + observer.observe(secs, &kv); + } + } + } + } + }) + .with_description( + "Time (in seconds) since last connection to nodes in the cluster layout", + ) + .init() + }, } } } |