diff --git a/backend/ee-repo-ref.txt b/backend/ee-repo-ref.txt index 3293bef380..6d0e7b18fc 100644 --- a/backend/ee-repo-ref.txt +++ b/backend/ee-repo-ref.txt @@ -1 +1 @@ -b012f1c9ecd42a6f99808cad65c519e2a3bc35c1 +23291f201b34b6f3d8e08b11783413a22a43a15f diff --git a/backend/tests/autoscaling_count.rs b/backend/tests/autoscaling_count.rs new file mode 100644 index 0000000000..31bbb0c726 --- /dev/null +++ b/backend/tests/autoscaling_count.rs @@ -0,0 +1,136 @@ +//! The worker count autoscaling decides from, driven through `apply_all_autoscaling` with a +//! dry-run integration against planted `worker_ping` rows. + +#![cfg(all(feature = "enterprise", feature = "private"))] + +use sqlx::{Pool, Postgres}; +use windmill_autoscaling::apply_all_autoscaling; + +async fn setup(db: &Pool) -> anyhow::Result<()> { + let updated = sqlx::query( + r#"UPDATE config SET config = config || '{"autoscaling": {"enabled": true, "min_workers": 2, + "max_workers": 12, "integration": {"type": "dryrun"}}}'::jsonb + WHERE name = 'worker__default'"#, + ) + .execute(db) + .await? + .rows_affected(); + assert_eq!(updated, 1, "the default worker group config is seeded"); + Ok(()) +} + +async fn plant_workers(db: &Pool, n: i32, draining: bool) -> anyhow::Result<()> { + sqlx::query( + "INSERT INTO worker_ping (worker, worker_instance, worker_group, started_at, draining) + SELECT 'wk-' || $2::text || '-' || i, 'pod-' || $2::text || '-' || i, 'default', + now() - interval '1 hour', $2 + FROM generate_series(1, $1) AS i", + ) + .bind(n) + .bind(draining) + .execute(db) + .await?; + Ok(()) +} + +/// `apply_all_autoscaling` runs only for the holder of a 15s lock, so each tick backdates it. +async fn tick(db: &Pool) -> anyhow::Result<()> { + sqlx::query( + "INSERT INTO concurrency_locks (id, last_locked_at) VALUES ('autoscaling', now() - interval '1 hour') + ON CONFLICT (id) DO UPDATE SET last_locked_at = EXCLUDED.last_locked_at", + ) + .execute(db) + .await?; + apply_all_autoscaling(db).await +} + +async fn events(db: &Pool) -> anyhow::Result> { + Ok(sqlx::query_as( + "SELECT desired_workers, reason FROM autoscaling_event WHERE worker_group = 'default' ORDER BY id", + ) + .fetch_all(db) + .await?) +} + +#[sqlx::test(migrations = "./migrations")] +async fn draining_workers_are_not_counted(db: Pool) -> anyhow::Result<()> { + setup(&db).await?; + plant_workers(&db, 12, false).await?; + plant_workers(&db, 2, true).await?; + + tick(&db).await?; + + assert_eq!(events(&db).await?, vec![]); + Ok(()) +} + +#[sqlx::test(migrations = "./migrations")] +async fn above_max_applies_max_once_per_cooldown(db: Pool) -> anyhow::Result<()> { + setup(&db).await?; + plant_workers(&db, 14, false).await?; + + tick(&db).await?; + tick(&db).await?; + + let events = events(&db).await?; + assert_eq!(events.len(), 1, "{events:?}"); + assert_eq!(events[0].0, 12); + + sqlx::query("UPDATE autoscaling_event SET applied_at = applied_at - interval '10 minutes'") + .execute(&db) + .await?; + tick(&db).await?; + assert_eq!(self::events(&db).await?.len(), 2); + Ok(()) +} + +/// A group restarting in place marks every worker draining while its pods remain replicas. +#[sqlx::test(migrations = "./migrations")] +async fn restarting_group_is_not_scaled_below_last_applied(db: Pool) -> anyhow::Result<()> { + setup(&db).await?; + sqlx::query( + "INSERT INTO autoscaling_event (worker_group, event_type, desired_workers, reason) + VALUES ('default', 'scaleout', 10, 'planted')", + ) + .execute(&db) + .await?; + plant_workers(&db, 1, false).await?; + plant_workers(&db, 9, true).await?; + + tick(&db).await?; + assert_eq!(events(&db).await?.len(), 1); + + sqlx::query("UPDATE autoscaling_event SET applied_at = applied_at - interval '10 minutes'") + .execute(&db) + .await?; + tick(&db).await?; + let events = events(&db).await?; + assert_eq!(events.last().map(|e| e.0), Some(10), "{events:?}"); + Ok(()) +} + +/// Idle restarted workers beside ones still draining must not make one scale-in remove the +/// draining pods too. +#[sqlx::test(migrations = "./migrations")] +async fn partially_restarted_group_scales_in_one_step(db: Pool) -> anyhow::Result<()> { + setup(&db).await?; + sqlx::query( + "INSERT INTO autoscaling_event (worker_group, event_type, desired_workers, reason, applied_at) + VALUES ('default', 'scaleout', 10, 'planted', now() - interval '10 minutes')", + ) + .execute(&db) + .await?; + plant_workers(&db, 4, false).await?; + plant_workers(&db, 6, true).await?; + sqlx::query( + "UPDATE worker_ping SET occupancy_rate_15s = 0, occupancy_rate_5m = 0, occupancy_rate_30m = 0", + ) + .execute(&db) + .await?; + + tick(&db).await?; + + let events = events(&db).await?; + assert_eq!(events.last().map(|e| e.0), Some(8), "{events:?}"); + Ok(()) +}