From 1701bcc41f9c0b661abe3b104f4df15a47d5181f Mon Sep 17 00:00:00 2001 From: Adam Hendel Date: Tue, 9 Jan 2024 11:54:37 -0600 Subject: [PATCH] fix fractional cpu parsing --- tembo-operator/Cargo.lock | 2 +- tembo-operator/Cargo.toml | 2 +- tembo-operator/src/stacks/config_engines.rs | 59 +++++++++++++++------ 3 files changed, 44 insertions(+), 19 deletions(-) diff --git a/tembo-operator/Cargo.lock b/tembo-operator/Cargo.lock index 876a891ed..10d4943b5 100644 --- a/tembo-operator/Cargo.lock +++ b/tembo-operator/Cargo.lock @@ -494,7 +494,7 @@ dependencies = [ [[package]] name = "controller" -version = "0.29.2" +version = "0.29.5" dependencies = [ "actix-web", "anyhow", diff --git a/tembo-operator/Cargo.toml b/tembo-operator/Cargo.toml index 79f73218e..a43feb081 100644 --- a/tembo-operator/Cargo.toml +++ b/tembo-operator/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "controller" description = "Tembo Operator for Postgres" -version = "0.29.4" +version = "0.29.5" edition = "2021" default-run = "controller" license = "Apache-2.0" diff --git a/tembo-operator/src/stacks/config_engines.rs b/tembo-operator/src/stacks/config_engines.rs index f8d7e243f..0c27e256d 100644 --- a/tembo-operator/src/stacks/config_engines.rs +++ b/tembo-operator/src/stacks/config_engines.rs @@ -137,20 +137,20 @@ pub fn mq_config_engine(stack: &Stack) -> Vec { } // olap formula for max_parallel_workers_per_gather -fn olap_max_parallel_workers_per_gather(cpu: i32) -> i32 { +fn olap_max_parallel_workers_per_gather(cpu: f32) -> i32 { // higher of default (2) or 0.5 * cpu - let scaled = i32::max((cpu as f64 * 0.5).floor() as i32, 2); + let scaled = i32::max((cpu * 0.5).floor() as i32, 2); // cap at 8 i32::max(scaled, 8) } -fn olap_max_parallel_workers(cpu: i32) -> i32 { +fn olap_max_parallel_workers(cpu: f32) -> i32 { // higher of the default (8) or cpu - i32::max(8, cpu) + i32::max(8, cpu.round() as i32) } -fn olap_max_worker_processes(cpu: i32) -> i32 { - i32::max(1, cpu) +fn olap_max_worker_processes(cpu: f32) -> i32 { + i32::max(1, cpu.round() as i32) } // olap formula for maintenance_work_mem @@ -294,14 +294,14 @@ fn split_string(input: &str) -> Result<(f64, String), ValueError> { } // returns the vCPU count -fn parse_cpu(stack: &Stack) -> i32 { +fn parse_cpu(stack: &Stack) -> f32 { stack .infrastructure .as_ref() .expect("infra required for a configuration engine") .cpu .to_string() - .parse::() + .parse::() .expect("failed parsing cpu") } @@ -434,21 +434,13 @@ mod tests { fn test_olap_config_engine() { let stack = Stack { name: "test".to_owned(), - compute_templates: None, infrastructure: Some(Infrastructure { cpu: "4".to_string(), memory: "16Gi".to_string(), storage: "10Gi".to_string(), }), - app_services: None, - image: None, - extensions: None, - trunk_installs: None, - description: None, - stack_version: None, postgres_config_engine: Some(ConfigEngine::Standard), - postgres_config: None, - postgres_metrics: None, + ..Stack::default() }; let configs = olap_config_engine(&stack); @@ -471,4 +463,37 @@ mod tests { assert_eq!(configs[8].name, "work_mem"); assert_eq!(configs[8].value.to_string(), "90MB"); } + + #[test] + fn test_olap_config_engine_fractional_cpu() { + let stack = Stack { + name: "test".to_owned(), + infrastructure: Some(Infrastructure { + cpu: "0.5".to_string(), + memory: "8Gi".to_string(), + storage: "10Gi".to_string(), + }), + postgres_config_engine: Some(ConfigEngine::Standard), + ..Stack::default() + }; + let configs = olap_config_engine(&stack); + assert_eq!(configs[0].name, "effective_cache_size"); + assert_eq!(configs[0].value.to_string(), "5734MB"); + assert_eq!(configs[1].name, "maintenance_work_mem"); + assert_eq!(configs[1].value.to_string(), "819MB"); + assert_eq!(configs[2].name, "max_connections"); + assert_eq!(configs[2].value.to_string(), "100"); + assert_eq!(configs[3].name, "max_parallel_workers"); + assert_eq!(configs[3].value.to_string(), "8"); + assert_eq!(configs[4].name, "max_parallel_workers_per_gather"); + assert_eq!(configs[4].value.to_string(), "8"); + assert_eq!(configs[5].name, "max_wal_size"); + assert_eq!(configs[5].value.to_string(), "2GB"); + assert_eq!(configs[6].name, "max_worker_processes"); + assert_eq!(configs[6].value.to_string(), "1"); + assert_eq!(configs[7].name, "shared_buffers"); + assert_eq!(configs[7].value.to_string(), "2048MB"); + assert_eq!(configs[8].name, "work_mem"); + assert_eq!(configs[8].value.to_string(), "45MB"); + } }