From 00d479358d31b445bfbe6d7ee3c37520be7e6d85 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Tue, 20 Feb 2024 11:35:18 +0100 Subject: [peer-metrics] refactor/simplify SystemMetrics --- src/rpc/system.rs | 77 +++++++++++++---------------------------------- src/rpc/system_metrics.rs | 52 ++++++++++++-------------------- 2 files changed, 41 insertions(+), 88 deletions(-) diff --git a/src/rpc/system.rs b/src/rpc/system.rs index 147ec4d6..4a505f58 100644 --- a/src/rpc/system.rs +++ b/src/rpc/system.rs @@ -3,11 +3,9 @@ use std::collections::HashMap; 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}; use std::time::{Duration, Instant}; -use arc_swap::ArcSwap; use async_trait::async_trait; use futures::join; use serde::{Deserialize, Serialize}; @@ -88,7 +86,7 @@ pub struct System { persist_cluster_layout: Persister, persist_peer_list: Persister, - local_status: ArcSwap, + local_status: Arc>, node_status: RwLock>, pub netapp: Arc, @@ -106,7 +104,7 @@ pub struct System { #[cfg(feature = "kubernetes-discovery")] kubernetes_discovery: Option, - metrics: SystemMetrics, + _metrics: SystemMetrics, replication_mode: ReplicationMode, replication_factor: usize, @@ -280,10 +278,11 @@ impl System { } }; - let metrics = SystemMetrics::new(replication_factor); - let mut local_status = NodeStatus::initial(replication_factor, &cluster_layout); - local_status.update_disk_usage(&config.metadata_dir, &config.data_dir, &metrics); + local_status.update_disk_usage(&config.metadata_dir, &config.data_dir); + let local_status = Arc::new(RwLock::new(local_status)); + + let metrics = SystemMetrics::new(replication_factor, local_status.clone()); let ring = Ring::new(cluster_layout, replication_factor); let (update_ring, ring) = watch::channel(Arc::new(ring)); @@ -357,7 +356,7 @@ impl System { id: netapp.id.into(), persist_cluster_layout, persist_peer_list, - local_status: ArcSwap::new(Arc::new(local_status)), + local_status, node_status: RwLock::new(HashMap::new()), netapp: netapp.clone(), peering: peering.clone(), @@ -377,7 +376,7 @@ impl System { consul_discovery, #[cfg(feature = "kubernetes-discovery")] kubernetes_discovery: config.kubernetes_discovery.clone(), - metrics, + _metrics: metrics, ring, update_ring: Mutex::new(update_ring), @@ -546,12 +545,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, - rpc_public_addr, - ) + .publish_consul_service(self.netapp.id, &hostname, rpc_public_addr) .await { error!("Error while publishing Consul service: {}", e); @@ -573,13 +569,8 @@ impl System { } }; - if let Err(e) = publish_kubernetes_node( - k, - self.netapp.id, - &self.local_status.load_full().hostname, - 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); } @@ -596,15 +587,13 @@ impl System { } fn update_local_status(&self) { - let mut new_si: NodeStatus = self.local_status.load().as_ref().clone(); + let mut local_status = self.local_status.write().unwrap(); let ring = self.ring.borrow(); - new_si.cluster_layout_version = ring.layout.version; - new_si.cluster_layout_staging_hash = ring.layout.staging_hash; - - new_si.update_disk_usage(&self.metadata_dir, &self.data_dir, &self.metrics); + local_status.cluster_layout_version = ring.layout.version; + local_status.cluster_layout_staging_hash = ring.layout.staging_hash; - self.local_status.swap(Arc::new(new_si)); + local_status.update_disk_usage(&self.metadata_dir, &self.data_dir); } // --- RPC HANDLERS --- @@ -629,7 +618,7 @@ impl System { from: Uuid, info: &NodeStatus, ) -> Result { - 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.", @@ -644,6 +633,8 @@ impl System { tokio::spawn(self.clone().pull_cluster_layout(from)); } + drop(local_info); + self.node_status .write() .unwrap() @@ -708,7 +699,7 @@ impl System { let restart_at = Instant::now() + STATUS_EXCHANGE_INTERVAL; 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 .broadcast( @@ -893,12 +884,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) => { @@ -934,27 +920,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..7b390c25 100644 --- a/src/rpc/system_metrics.rs +++ b/src/rpc/system_metrics.rs @@ -1,31 +1,22 @@ -use std::sync::atomic::{AtomicU64, Ordering}; -use std::sync::Arc; +use std::sync::{Arc, RwLock}; use opentelemetry::{global, metrics::*, KeyValue}; +use crate::system::NodeStatus; + /// TableMetrics reference all counter used for metrics pub struct SystemMetrics { pub(crate) _garage_build_info: ValueObserver, pub(crate) _replication_factor: ValueObserver, pub(crate) _disk_avail: ValueObserver, pub(crate) _disk_total: ValueObserver, - pub(crate) values: Arc, -} - -#[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, } impl SystemMetrics { - pub fn new(replication_factor: usize) -> Self { + pub fn new(replication_factor: usize, local_status: Arc>) -> Self { let meter = global::meter("garage_system"); - let values = Arc::new(SystemMetricsValues::default()); - let values1 = values.clone(); - let values2 = values.clone(); + let st1 = local_status.clone(); + let st2 = local_status.clone(); Self { _garage_build_info: meter .u64_value_observer("garage_build_info", move |observer| { @@ -47,31 +38,28 @@ impl SystemMetrics { .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")]), - }; + let st = st1.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: 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")]), - }; + let st = st2.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(), - values, } } } -- cgit v1.2.3 From 3cdf69f07924d120c572577495789774535daafe Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Tue, 20 Feb 2024 12:37:55 +0100 Subject: [peer-metrics] Add metrics for cluster health, like GetClusterHealth admin API --- src/rpc/system.rs | 12 ++- src/rpc/system_metrics.rs | 188 ++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 172 insertions(+), 28 deletions(-) diff --git a/src/rpc/system.rs b/src/rpc/system.rs index 4a505f58..8ecefd84 100644 --- a/src/rpc/system.rs +++ b/src/rpc/system.rs @@ -104,7 +104,7 @@ pub struct System { #[cfg(feature = "kubernetes-discovery")] kubernetes_discovery: Option, - _metrics: SystemMetrics, + metrics: SystemMetrics, replication_mode: ReplicationMode, replication_factor: usize, @@ -168,7 +168,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, @@ -376,7 +376,7 @@ impl System { consul_discovery, #[cfg(feature = "kubernetes-discovery")] kubernetes_discovery: config.kubernetes_discovery.clone(), - _metrics: metrics, + metrics, ring, update_ring: Mutex::new(update_ring), @@ -698,7 +698,13 @@ impl System { while !*stop_signal.borrow() { let restart_at = Instant::now() + STATUS_EXCHANGE_INTERVAL; + // Update local node status that is exchanged. + // Status variables are exported into Prometheus in SystemMetrics, + // so we take the opportunity to also update here the health status + // that is reported in those metrics. self.update_local_status(); + *self.metrics.health.write().unwrap() = Some(self.health()); + let local_status: NodeStatus = self.local_status.read().unwrap().clone(); let _ = self .rpc diff --git a/src/rpc/system_metrics.rs b/src/rpc/system_metrics.rs index 7b390c25..ee07672e 100644 --- a/src/rpc/system_metrics.rs +++ b/src/rpc/system_metrics.rs @@ -2,22 +2,40 @@ use std::sync::{Arc, RwLock}; use opentelemetry::{global, metrics::*, KeyValue}; -use crate::system::NodeStatus; +use crate::system::{ClusterHealth, ClusterHealthStatus, NodeStatus}; /// TableMetrics reference all counter used for metrics pub struct SystemMetrics { + pub(crate) health: Arc>>, + + // Static values pub(crate) _garage_build_info: ValueObserver, pub(crate) _replication_factor: ValueObserver, + + // Disk space values from System::local_status pub(crate) _disk_avail: ValueObserver, pub(crate) _disk_total: ValueObserver, + + // Health report from System::health() + pub(crate) _cluster_healthy: ValueObserver, + pub(crate) _cluster_available: ValueObserver, + pub(crate) _known_nodes: ValueObserver, + pub(crate) _connected_nodes: ValueObserver, + pub(crate) _storage_nodes: ValueObserver, + pub(crate) _storage_nodes_ok: ValueObserver, + pub(crate) _partitions: ValueObserver, + pub(crate) _partitions_quorum: ValueObserver, + pub(crate) _partitions_all_ok: ValueObserver, } impl SystemMetrics { pub fn new(replication_factor: usize, local_status: Arc>) -> Self { let meter = global::meter("garage_system"); - let st1 = local_status.clone(); - let st2 = local_status.clone(); + let health = Arc::new(RwLock::new(None)); Self { + health: health.clone(), + + // Static values _garage_build_info: meter .u64_value_observer("garage_build_info", move |observer| { observer.observe( @@ -36,30 +54,150 @@ impl SystemMetrics { }) .with_description("Garage replication factor setting") .init(), - _disk_avail: meter - .u64_value_observer("garage_local_disk_avail", move |observer| { - let st = st1.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: meter - .u64_value_observer("garage_local_disk_total", move |observer| { - let st = st2.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")]); + + // Disk space values from System::local_status + _disk_avail: { + let status = local_status.clone(); + meter + .u64_value_observer("garage_local_disk_avail", move |observer| { + let st = 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 status = local_status.clone(); + meter + .u64_value_observer("garage_local_disk_total", move |observer| { + let st = 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::health() + _cluster_healthy: { + let health = health.clone(); + meter + .u64_value_observer("cluster_healthy", move |observer| { + if let Some(h) = health.read().unwrap().as_ref() { + if h.status == ClusterHealthStatus::Healthy { + observer.observe(1, &[]); + } else { + observer.observe(0, &[]); + } + } + }) + .with_description("Whether all storage nodes are connected") + .init() + }, + _cluster_available: { + let health = health.clone(); + meter.u64_value_observer("cluster_available", move |observer| { + if let Some(h) = health.read().unwrap().as_ref() { + if h.status != ClusterHealthStatus::Unavailable { + observer.observe(1, &[]); + } else { + observer.observe(0, &[]); + } } }) - .with_description("Garage total disk space on each node") - .init(), + .with_description("Whether all requests can be served, even if some storage nodes are disconnected") + .init() + }, + _known_nodes: { + let health = health.clone(); + meter + .u64_value_observer("cluster_known_nodes", move |observer| { + if let Some(h) = health.read().unwrap().as_ref() { + observer.observe(h.known_nodes as u64, &[]); + } + }) + .with_description("Number of nodes already seen once in the cluster") + .init() + }, + _connected_nodes: { + let health = health.clone(); + meter + .u64_value_observer("cluster_connected_nodes", move |observer| { + if let Some(h) = health.read().unwrap().as_ref() { + observer.observe(h.connected_nodes as u64, &[]); + } + }) + .with_description("Number of nodes currently connected") + .init() + }, + _storage_nodes: { + let health = health.clone(); + meter + .u64_value_observer("cluster_storage_nodes", move |observer| { + if let Some(h) = health.read().unwrap().as_ref() { + observer.observe(h.storage_nodes as u64, &[]); + } + }) + .with_description("Number of storage nodes declared in the current layout") + .init() + }, + _storage_nodes_ok: { + let health = health.clone(); + meter + .u64_value_observer("cluster_storage_nodes_ok", move |observer| { + if let Some(h) = health.read().unwrap().as_ref() { + observer.observe(h.storage_nodes_ok as u64, &[]); + } + }) + .with_description("Number of storage nodes currently connected") + .init() + }, + _partitions: { + let health = health.clone(); + meter + .u64_value_observer("cluster_partitions", move |observer| { + if let Some(h) = health.read().unwrap().as_ref() { + observer.observe(h.partitions as u64, &[]); + } + }) + .with_description("Number of partitions in the layout") + .init() + }, + _partitions_quorum: { + let health = health.clone(); + meter + .u64_value_observer("cluster_partitions_quorum", move |observer| { + if let Some(h) = health.read().unwrap().as_ref() { + 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 health = health.clone(); + meter + .u64_value_observer("cluster_partitions_all_ok", move |observer| { + if let Some(h) = health.read().unwrap().as_ref() { + observer.observe(h.partitions_all_ok as u64, &[]); + } + }) + .with_description( + "Number of partitions for which all storage nodes are connected", + ) + .init() + }, } } } -- cgit v1.2.3 From 182a23cc1207c97922a24182c15e2b228015e8f4 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Tue, 20 Feb 2024 14:20:58 +0100 Subject: [peer-metrics] refactor SystemMetrics to hold a reference to System --- src/garage/server.rs | 1 + src/rpc/system.rs | 28 ++++++----- src/rpc/system_metrics.rs | 126 ++++++++++++++++++++++++---------------------- 3 files changed, 83 insertions(+), 72 deletions(-) diff --git a/src/garage/server.rs b/src/garage/server.rs index 51b06b8e..6323f957 100644 --- a/src/garage/server.rs +++ b/src/garage/server.rs @@ -162,6 +162,7 @@ pub async fn run_server(config_file: PathBuf, secrets: Secrets) -> Result<(), Er info!("Netapp exited"); // Drop all references so that stuff can terminate properly + garage.system.cleanup(); drop(garage); // Await for all background tasks to end diff --git a/src/rpc/system.rs b/src/rpc/system.rs index 8ecefd84..f4fdcace 100644 --- a/src/rpc/system.rs +++ b/src/rpc/system.rs @@ -6,6 +6,7 @@ use std::path::{Path, PathBuf}; use std::sync::{Arc, RwLock}; use std::time::{Duration, Instant}; +use arc_swap::ArcSwapOption; use async_trait::async_trait; use futures::join; use serde::{Deserialize, Serialize}; @@ -86,7 +87,7 @@ pub struct System { persist_cluster_layout: Persister, persist_peer_list: Persister, - local_status: Arc>, + pub(crate) local_status: RwLock, node_status: RwLock>, pub netapp: Arc, @@ -104,10 +105,10 @@ pub struct System { #[cfg(feature = "kubernetes-discovery")] kubernetes_discovery: Option, - metrics: SystemMetrics, + metrics: ArcSwapOption, replication_mode: ReplicationMode, - replication_factor: usize, + pub(crate) replication_factor: usize, /// The ring pub ring: watch::Receiver>, @@ -280,9 +281,6 @@ impl System { let mut local_status = NodeStatus::initial(replication_factor, &cluster_layout); local_status.update_disk_usage(&config.metadata_dir, &config.data_dir); - let local_status = Arc::new(RwLock::new(local_status)); - - let metrics = SystemMetrics::new(replication_factor, local_status.clone()); let ring = Ring::new(cluster_layout, replication_factor); let (update_ring, ring) = watch::channel(Arc::new(ring)); @@ -356,7 +354,7 @@ impl System { id: netapp.id.into(), persist_cluster_layout, persist_peer_list, - local_status, + local_status: RwLock::new(local_status), node_status: RwLock::new(HashMap::new()), netapp: netapp.clone(), peering: peering.clone(), @@ -376,14 +374,19 @@ impl System { consul_discovery, #[cfg(feature = "kubernetes-discovery")] kubernetes_discovery: config.kubernetes_discovery.clone(), - metrics, + metrics: ArcSwapOption::new(None), ring, update_ring: Mutex::new(update_ring), 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) } @@ -401,6 +404,11 @@ impl System { ); } + pub fn cleanup(&self) { + // Break reference cycle + self.metrics.store(None); + } + // ---- Administrative operations (directly available and // also available through RPC) ---- @@ -699,11 +707,7 @@ impl System { let restart_at = Instant::now() + STATUS_EXCHANGE_INTERVAL; // Update local node status that is exchanged. - // Status variables are exported into Prometheus in SystemMetrics, - // so we take the opportunity to also update here the health status - // that is reported in those metrics. self.update_local_status(); - *self.metrics.health.write().unwrap() = Some(self.health()); let local_status: NodeStatus = self.local_status.read().unwrap().clone(); let _ = self diff --git a/src/rpc/system_metrics.rs b/src/rpc/system_metrics.rs index ee07672e..ad4aca2f 100644 --- a/src/rpc/system_metrics.rs +++ b/src/rpc/system_metrics.rs @@ -1,13 +1,12 @@ use std::sync::{Arc, RwLock}; +use std::time::{Duration, Instant}; use opentelemetry::{global, metrics::*, KeyValue}; -use crate::system::{ClusterHealth, ClusterHealthStatus, NodeStatus}; +use crate::system::{ClusterHealthStatus, System}; /// TableMetrics reference all counter used for metrics pub struct SystemMetrics { - pub(crate) health: Arc>>, - // Static values pub(crate) _garage_build_info: ValueObserver, pub(crate) _replication_factor: ValueObserver, @@ -29,12 +28,25 @@ pub struct SystemMetrics { } impl SystemMetrics { - pub fn new(replication_factor: usize, local_status: Arc>) -> Self { + pub fn new(system: Arc) -> Self { let meter = global::meter("garage_system"); - let health = Arc::new(RwLock::new(None)); - Self { - health: health.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| { @@ -48,19 +60,22 @@ 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(), + _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 status = local_status.clone(); + let system = system.clone(); meter .u64_value_observer("garage_local_disk_avail", move |observer| { - let st = status.read().unwrap(); + let st = system.local_status.read().unwrap(); if let Some((avail, _total)) = st.data_disk_avail { observer.observe(avail, &[KeyValue::new("volume", "data")]); } @@ -72,10 +87,10 @@ impl SystemMetrics { .init() }, _disk_total: { - let status = local_status.clone(); + let system = system.clone(); meter .u64_value_observer("garage_local_disk_total", move |observer| { - let st = status.read().unwrap(); + let st = system.local_status.read().unwrap(); if let Some((_avail, total)) = st.data_disk_avail { observer.observe(total, &[KeyValue::new("volume", "data")]); } @@ -87,98 +102,90 @@ impl SystemMetrics { .init() }, - // Health report from System::health() + // Health report from System::() _cluster_healthy: { - let health = health.clone(); + let get_health = get_health.clone(); meter .u64_value_observer("cluster_healthy", move |observer| { - if let Some(h) = health.read().unwrap().as_ref() { - if h.status == ClusterHealthStatus::Healthy { - observer.observe(1, &[]); - } else { - observer.observe(0, &[]); - } + 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 health = health.clone(); + let get_health = get_health.clone(); meter.u64_value_observer("cluster_available", move |observer| { - if let Some(h) = health.read().unwrap().as_ref() { - if h.status != ClusterHealthStatus::Unavailable { - observer.observe(1, &[]); - } else { - observer.observe(0, &[]); - } + let h = get_health(); + if h.status != ClusterHealthStatus::Unavailable { + observer.observe(1, &[]); + } else { + observer.observe(0, &[]); } }) .with_description("Whether all requests can be served, even if some storage nodes are disconnected") .init() }, _known_nodes: { - let health = health.clone(); + let get_health = get_health.clone(); meter .u64_value_observer("cluster_known_nodes", move |observer| { - if let Some(h) = health.read().unwrap().as_ref() { - observer.observe(h.known_nodes as u64, &[]); - } + 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 health = health.clone(); + let get_health = get_health.clone(); meter .u64_value_observer("cluster_connected_nodes", move |observer| { - if let Some(h) = health.read().unwrap().as_ref() { - observer.observe(h.connected_nodes as u64, &[]); - } + let h = get_health(); + observer.observe(h.connected_nodes as u64, &[]); }) .with_description("Number of nodes currently connected") .init() }, _storage_nodes: { - let health = health.clone(); + let get_health = get_health.clone(); meter .u64_value_observer("cluster_storage_nodes", move |observer| { - if let Some(h) = health.read().unwrap().as_ref() { - observer.observe(h.storage_nodes as u64, &[]); - } + 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 health = health.clone(); + let get_health = get_health.clone(); meter .u64_value_observer("cluster_storage_nodes_ok", move |observer| { - if let Some(h) = health.read().unwrap().as_ref() { - observer.observe(h.storage_nodes_ok as u64, &[]); - } + let h = get_health(); + observer.observe(h.storage_nodes_ok as u64, &[]); }) .with_description("Number of storage nodes currently connected") .init() }, _partitions: { - let health = health.clone(); + let get_health = get_health.clone(); meter .u64_value_observer("cluster_partitions", move |observer| { - if let Some(h) = health.read().unwrap().as_ref() { - observer.observe(h.partitions as u64, &[]); - } + let h = get_health(); + observer.observe(h.partitions as u64, &[]); }) .with_description("Number of partitions in the layout") .init() }, _partitions_quorum: { - let health = health.clone(); + let get_health = get_health.clone(); meter .u64_value_observer("cluster_partitions_quorum", move |observer| { - if let Some(h) = health.read().unwrap().as_ref() { - observer.observe(h.partitions_quorum as u64, &[]); - } + 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", @@ -186,12 +193,11 @@ impl SystemMetrics { .init() }, _partitions_all_ok: { - let health = health.clone(); + let get_health = get_health.clone(); meter .u64_value_observer("cluster_partitions_all_ok", move |observer| { - if let Some(h) = health.read().unwrap().as_ref() { - observer.observe(h.partitions_all_ok as u64, &[]); - } + let h = get_health(); + observer.observe(h.partitions_all_ok as u64, &[]); }) .with_description( "Number of partitions for which all storage nodes are connected", -- cgit v1.2.3 From b868493da9b7cf6e5703ee5d068ae34938fbff34 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Tue, 20 Feb 2024 14:49:08 +0100 Subject: [peer-metrics] add basic cluster node status metrics (fix #545) --- src/rpc/system_metrics.rs | 94 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) diff --git a/src/rpc/system_metrics.rs b/src/rpc/system_metrics.rs index ad4aca2f..ffbef6df 100644 --- a/src/rpc/system_metrics.rs +++ b/src/rpc/system_metrics.rs @@ -3,6 +3,7 @@ use std::time::{Duration, Instant}; use opentelemetry::{global, metrics::*, KeyValue}; +use crate::ring::Ring; use crate::system::{ClusterHealthStatus, System}; /// TableMetrics reference all counter used for metrics @@ -25,6 +26,10 @@ pub struct SystemMetrics { pub(crate) _partitions: ValueObserver, pub(crate) _partitions_quorum: ValueObserver, pub(crate) _partitions_all_ok: ValueObserver, + + // Status report for individual cluster nodes + pub(crate) _layout_node_connected: ValueObserver, + pub(crate) _layout_node_disconnected_time: ValueObserver, } impl SystemMetrics { @@ -204,6 +209,95 @@ impl SystemMetrics { ) .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 ring: Arc = system.ring.borrow().clone(); + let nodes = system.get_known_nodes(); + for (id, _, config) in ring.layout.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 ring: Arc = system.ring.borrow().clone(); + let nodes = system.get_known_nodes(); + for (id, _, config) in ring.layout.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() + }, } } } -- cgit v1.2.3 From bcd571ef57662a1dc86ef5bb0c055d3f8bd9e3bc Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Tue, 20 Feb 2024 14:59:04 +0100 Subject: [peer-metrics] add documentation for new cluster status metrics --- doc/book/reference-manual/monitoring.md | 106 ++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) diff --git a/doc/book/reference-manual/monitoring.md b/doc/book/reference-manual/monitoring.md index 97c533d3..f392c133 100644 --- a/doc/book/reference-manual/monitoring.md +++ b/doc/book/reference-manual/monitoring.md @@ -27,6 +27,112 @@ Exposes the Garage replication factor configured on the node garage_replication_factor 3 ``` +#### `garage_local_disk_avail` and `garage_local_disk_total` (gauge) + +Reports the available and total disk space on each node, for data and metadata separately. + +``` +garage_local_disk_avail{volume="data"} 540341960704 +garage_local_disk_avail{volume="metadata"} 540341960704 +garage_local_disk_total{volume="data"} 763063566336 +garage_local_disk_total{volume="metadata"} 763063566336 +``` + +### Cluster health status metrics + +#### `cluster_healthy` (gauge) + +Whether all storage nodes are connected (0 or 1) + +``` +cluster_healthy 0 +``` + +#### `cluster_available` (gauge) + +Whether all requests can be served, even if some storage nodes are disconnected + +``` +cluster_available 1 +``` + +#### `cluster_connected_nodes` (gauge) + +Number of nodes currently connected + +``` +cluster_connected_nodes 3 +``` + +#### `cluster_known_nodes` (gauge) + +Number of nodes already seen once in the cluster + +``` +cluster_known_nodes 3 +``` + +#### `cluster_layout_node_connected` (gauge) + +Connection status for individual nodes of the cluster layout + +``` +cluster_layout_node_connected{id="62b218d848e86a64",role_capacity="1000000000",role_gateway="0",role_zone="dc1"} 1 +cluster_layout_node_connected{id="a11c7cf18af29737",role_capacity="1000000000",role_gateway="0",role_zone="dc1"} 0 +cluster_layout_node_connected{id="a235ac7695e0c54d",role_capacity="1000000000",role_gateway="0",role_zone="dc1"} 1 +cluster_layout_node_connected{id="b10c110e4e854e5a",role_capacity="1000000000",role_gateway="0",role_zone="dc1"} 1 +``` + +#### `cluster_layout_node_disconnected_time` (gauge) + +Time (in seconds) since last connection to individual nodes of the cluster layout + +``` +cluster_layout_node_disconnected_time{id="62b218d848e86a64",role_capacity="1000000000",role_gateway="0",role_zone="dc1"} 0 +cluster_layout_node_disconnected_time{id="a235ac7695e0c54d",role_capacity="1000000000",role_gateway="0",role_zone="dc1"} 0 +cluster_layout_node_disconnected_time{id="b10c110e4e854e5a",role_capacity="1000000000",role_gateway="0",role_zone="dc1"} 0 +``` + +#### `cluster_storage_nodes` (gauge) + +Number of storage nodes declared in the current layout + +``` +cluster_storage_nodes 4 +``` + +#### `cluster_storage_nodes_ok` (gauge) + +Number of storage nodes currently connected + +``` +cluster_storage_nodes_ok 3 +``` + +#### `cluster_partitions` (gauge) + +Number of partitions in the layout (this is always 256) + +``` +cluster_partitions 256 +``` + +#### `cluster_partitions_all_ok` (gauge) + +Number of partitions for which all storage nodes are connected + +``` +cluster_partitions_all_ok 64 +``` + +#### `cluster_partitions_quorum` (gauge) + +Number of partitions for which we have a quorum of connected nodes and all requests can be served + +``` +cluster_partitions_quorum 256 +``` + ### Metrics of the API endpoints #### `api_admin_request_counter` (counter) -- cgit v1.2.3