diff --git a/compute_tools/src/bin/compute_ctl.rs b/compute_tools/src/bin/compute_ctl.rs index 7786d7af9c..f3b787209d 100644 --- a/compute_tools/src/bin/compute_ctl.rs +++ b/compute_tools/src/bin/compute_ctl.rs @@ -105,7 +105,7 @@ fn main() -> Result<()> { tenant, timeline, pageserver_connstr, - metrics: ComputeMetrics::new(), + metrics: ComputeMetrics::default(), state: RwLock::new(ComputeState::new()), }; let compute = Arc::new(compute_state); diff --git a/compute_tools/src/checker.rs b/compute_tools/src/checker.rs index b6ba1692f9..ee1605c814 100644 --- a/compute_tools/src/checker.rs +++ b/compute_tools/src/checker.rs @@ -5,7 +5,7 @@ use tokio_postgres::NoTls; use crate::compute::ComputeNode; -pub fn create_writablity_check_data(client: &mut Client) -> Result<()> { +pub fn create_writability_check_data(client: &mut Client) -> Result<()> { let query = " CREATE TABLE IF NOT EXISTS health_check ( id serial primary key, diff --git a/compute_tools/src/compute.rs b/compute_tools/src/compute.rs index 7ebb98077a..c2c9ab2230 100644 --- a/compute_tools/src/compute.rs +++ b/compute_tools/src/compute.rs @@ -27,7 +27,7 @@ use log::{info, warn}; use postgres::{Client, NoTls}; use serde::{Serialize, Serializer}; -use crate::checker::create_writablity_check_data; +use crate::checker::create_writability_check_data; use crate::config; use crate::pg_helpers::*; use crate::spec::*; @@ -91,7 +91,7 @@ pub enum ComputeStatus { Failed, } -#[derive(Serialize)] +#[derive(Default, Serialize)] pub struct ComputeMetrics { pub sync_safekeepers_ms: AtomicU64, pub basebackup_ms: AtomicU64, @@ -99,23 +99,6 @@ pub struct ComputeMetrics { pub total_startup_ms: AtomicU64, } -impl ComputeMetrics { - pub fn new() -> Self { - Self { - sync_safekeepers_ms: AtomicU64::new(0), - basebackup_ms: AtomicU64::new(0), - config_ms: AtomicU64::new(0), - total_startup_ms: AtomicU64::new(0), - } - } -} - -impl Default for ComputeMetrics { - fn default() -> Self { - Self::new() - } -} - impl ComputeNode { pub fn set_status(&self, status: ComputeStatus) { self.state.write().unwrap().status = status; @@ -292,7 +275,7 @@ impl ComputeNode { handle_databases(&self.spec, &mut client)?; handle_role_deletions(self, &mut client)?; handle_grants(self, &mut client)?; - create_writablity_check_data(&mut client)?; + create_writability_check_data(&mut client)?; // 'Close' connection drop(client); diff --git a/compute_tools/src/monitor.rs b/compute_tools/src/monitor.rs index 58cdf796bc..1588f5d62e 100644 --- a/compute_tools/src/monitor.rs +++ b/compute_tools/src/monitor.rs @@ -74,10 +74,8 @@ fn watch_compute_activity(compute: &ComputeNode) { } } - // Sort idle backend `state_change` timestamps. The last one corresponds - // to the last activity. - idle_backs.sort(); - if let Some(last) = idle_backs.last() { + // Get idle backend `state_change` with the max timestamp. + if let Some(last) = idle_backs.iter().max() { last_active = *last; } } diff --git a/compute_tools/src/pg_helpers.rs b/compute_tools/src/pg_helpers.rs index 289f223bda..ff422f1cf5 100644 --- a/compute_tools/src/pg_helpers.rs +++ b/compute_tools/src/pg_helpers.rs @@ -119,16 +119,9 @@ pub trait GenericOptionsSearch { impl GenericOptionsSearch for GenericOptions { /// Lookup option by name fn find(&self, name: &str) -> Option { - match &self { - Some(ops) => { - let op = ops.iter().find(|s| s.name == name); - match op { - Some(op) => op.value.clone(), - None => None, - } - } - None => None, - } + let ops = self.as_ref()?; + let op = ops.iter().find(|s| s.name == name)?; + op.value.clone() } } @@ -161,6 +154,14 @@ impl Role { } impl Database { + pub fn new(name: PgIdent, owner: PgIdent) -> Self { + Self { + name, + owner, + options: None, + } + } + /// Serialize a list of database parameters into a Postgres-acceptable /// string of arguments. /// NB: `TEMPLATE` is actually also an identifier, but so far we only need @@ -219,11 +220,7 @@ pub fn get_existing_dbs(client: &mut Client) -> Result> { &[], )? .iter() - .map(|row| Database { - name: row.get("datname"), - owner: row.get("owner"), - options: None, - }) + .map(|row| Database::new(row.get("datname"), row.get("owner"))) .collect(); Ok(postgres_dbs) diff --git a/compute_tools/tests/pg_helpers_tests.rs b/compute_tools/tests/pg_helpers_tests.rs index 24cad4663a..431d9794bc 100644 --- a/compute_tools/tests/pg_helpers_tests.rs +++ b/compute_tools/tests/pg_helpers_tests.rs @@ -38,4 +38,33 @@ mod pg_helpers_tests { assert_eq!(ident.pg_quote(), "\"\"\"name\"\";\\n select 1;\""); } + + #[test] + fn generic_options_search() { + let generic_options: GenericOptions = Some(vec![ + GenericOption { + name: "present_value".into(), + value: Some("value".into()), + vartype: "string".into(), + }, + GenericOption { + name: "missed_value".into(), + value: None, + vartype: "int".into(), + }, + ]); + assert_eq!(generic_options.find("present_value"), Some("value".into())); + assert_eq!(generic_options.find("missed_value"), None); + assert_eq!(generic_options.find("invalid_value"), None); + + let empty_generic_options: GenericOptions = Some(vec![]); + assert_eq!(empty_generic_options.find("present_value"), None); + assert_eq!(empty_generic_options.find("missed_value"), None); + assert_eq!(empty_generic_options.find("invalid_value"), None); + + let none_generic_options: GenericOptions = None; + assert_eq!(none_generic_options.find("present_value"), None); + assert_eq!(none_generic_options.find("missed_value"), None); + assert_eq!(none_generic_options.find("invalid_value"), None); + } }