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 +++++++++++++++---------------------------------------- 1 file changed, 21 insertions(+), 56 deletions(-) (limited to 'src/rpc/system.rs') 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); - } } } -- 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 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'src/rpc/system.rs') 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 -- 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/rpc/system.rs | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) (limited to 'src/rpc/system.rs') 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 -- cgit v1.2.3