From 04a62d2b97e6333645e6b1ba898bb02efdb11877 Mon Sep 17 00:00:00 2001 From: Kerollmops Date: Wed, 11 Dec 2024 14:57:07 +0100 Subject: [PATCH 1/5] Compile Meilisearch or run the dedicated binary file --- crates/xtask/src/bench/meili_process.rs | 11 +---------- crates/xtask/src/bench/mod.rs | 5 +++++ crates/xtask/src/bench/workload.rs | 25 ++++++++++++++++++++++--- 3 files changed, 28 insertions(+), 13 deletions(-) diff --git a/crates/xtask/src/bench/meili_process.rs b/crates/xtask/src/bench/meili_process.rs index 99f6f4ea6..db787e595 100644 --- a/crates/xtask/src/bench/meili_process.rs +++ b/crates/xtask/src/bench/meili_process.rs @@ -37,17 +37,8 @@ pub async fn start( master_key: Option<&str>, workload: &Workload, asset_folder: &str, + mut command: tokio::process::Command, ) -> anyhow::Result { - let mut command = tokio::process::Command::new("cargo"); - command - .arg("run") - .arg("--release") - .arg("-p") - .arg("meilisearch") - .arg("--bin") - .arg("meilisearch") - .arg("--"); - command.arg("--db-path").arg("./_xtask_benchmark.ms"); if let Some(master_key) = master_key { command.arg("--master-key").arg(master_key); diff --git a/crates/xtask/src/bench/mod.rs b/crates/xtask/src/bench/mod.rs index deec120fa..491dc33ab 100644 --- a/crates/xtask/src/bench/mod.rs +++ b/crates/xtask/src/bench/mod.rs @@ -86,6 +86,10 @@ pub struct BenchDeriveArgs { /// The maximum time in seconds we allow for fetching the task queue before timing out. #[arg(long, default_value_t = 60)] tasks_queue_timeout_secs: u64, + + /// The path to the binary to run. By default it compiles the binary with cargo. + #[arg(long)] + binary_path: Option, } pub fn run(args: BenchDeriveArgs) -> anyhow::Result<()> { @@ -170,6 +174,7 @@ pub fn run(args: BenchDeriveArgs) -> anyhow::Result<()> { args.master_key.as_deref(), workload, &args, + args.binary_path.as_deref(), ) .await?; diff --git a/crates/xtask/src/bench/workload.rs b/crates/xtask/src/bench/workload.rs index 19c8bfae8..649bd0eaf 100644 --- a/crates/xtask/src/bench/workload.rs +++ b/crates/xtask/src/bench/workload.rs @@ -1,6 +1,7 @@ use std::collections::BTreeMap; use std::fs::File; use std::io::{Seek as _, Write as _}; +use std::path::Path; use anyhow::{bail, Context as _}; use futures_util::TryStreamExt as _; @@ -85,14 +86,30 @@ pub async fn execute( master_key: Option<&str>, workload: Workload, args: &BenchDeriveArgs, + binary_path: Option<&Path>, ) -> anyhow::Result<()> { assets::fetch_assets(assets_client, &workload.assets, &args.asset_folder).await?; let workload_uuid = dashboard_client.create_workload(invocation_uuid, &workload).await?; let mut tasks = Vec::new(); - for i in 0..workload.run_count { + let run_command = match binary_path { + Some(binary_path) => tokio::process::Command::new(binary_path), + None => { + let mut command = tokio::process::Command::new("cargo"); + command + .arg("run") + .arg("--release") + .arg("-p") + .arg("meilisearch") + .arg("--bin") + .arg("meilisearch") + .arg("--"); + command + } + }; + tasks.push( execute_run( dashboard_client, @@ -102,6 +119,7 @@ pub async fn execute( master_key, &workload, args, + run_command, i, ) .await?, @@ -109,7 +127,6 @@ pub async fn execute( } let mut reports = Vec::with_capacity(workload.run_count as usize); - for task in tasks { reports.push( task.await @@ -133,13 +150,15 @@ async fn execute_run( master_key: Option<&str>, workload: &Workload, args: &BenchDeriveArgs, + run_command: tokio::process::Command, run_number: u16, ) -> anyhow::Result>> { meili_process::delete_db(); meili_process::build().await?; let meilisearch = - meili_process::start(meili_client, master_key, workload, &args.asset_folder).await?; + meili_process::start(meili_client, master_key, workload, &args.asset_folder, run_command) + .await?; let processor = run_commands( dashboard_client, From eaa897d983d2c71b6f76a453f1739980ba980558 Mon Sep 17 00:00:00 2001 From: Kerollmops Date: Wed, 11 Dec 2024 15:57:16 +0100 Subject: [PATCH 2/5] Avoid compiling when unecessary --- crates/xtask/src/bench/workload.rs | 38 +++++++++++++++--------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/crates/xtask/src/bench/workload.rs b/crates/xtask/src/bench/workload.rs index 649bd0eaf..39119428f 100644 --- a/crates/xtask/src/bench/workload.rs +++ b/crates/xtask/src/bench/workload.rs @@ -94,22 +94,6 @@ pub async fn execute( let mut tasks = Vec::new(); for i in 0..workload.run_count { - let run_command = match binary_path { - Some(binary_path) => tokio::process::Command::new(binary_path), - None => { - let mut command = tokio::process::Command::new("cargo"); - command - .arg("run") - .arg("--release") - .arg("-p") - .arg("meilisearch") - .arg("--bin") - .arg("meilisearch") - .arg("--"); - command - } - }; - tasks.push( execute_run( dashboard_client, @@ -119,7 +103,7 @@ pub async fn execute( master_key, &workload, args, - run_command, + binary_path, i, ) .await?, @@ -150,12 +134,28 @@ async fn execute_run( master_key: Option<&str>, workload: &Workload, args: &BenchDeriveArgs, - run_command: tokio::process::Command, + binary_path: Option<&Path>, run_number: u16, ) -> anyhow::Result>> { meili_process::delete_db(); - meili_process::build().await?; + let run_command = match binary_path { + Some(binary_path) => tokio::process::Command::new(binary_path), + None => { + meili_process::build().await?; + let mut command = tokio::process::Command::new("cargo"); + command + .arg("run") + .arg("--release") + .arg("-p") + .arg("meilisearch") + .arg("--bin") + .arg("meilisearch") + .arg("--"); + command + } + }; + let meilisearch = meili_process::start(meili_client, master_key, workload, &args.asset_folder, run_command) .await?; From 04a24a9239a8fbcd5e45bd2f154ef20e7ef91f59 Mon Sep 17 00:00:00 2001 From: Kerollmops Date: Wed, 11 Dec 2024 16:27:07 +0100 Subject: [PATCH 3/5] Kill Meilisearch with a TERM signal --- crates/meilisearch/src/main.rs | 5 +++ crates/xtask/src/bench/meili_process.rs | 49 +++++++++++++++++++++---- 2 files changed, 46 insertions(+), 8 deletions(-) diff --git a/crates/meilisearch/src/main.rs b/crates/meilisearch/src/main.rs index b4b46bec4..6e6245c78 100644 --- a/crates/meilisearch/src/main.rs +++ b/crates/meilisearch/src/main.rs @@ -129,6 +129,11 @@ async fn try_main() -> anyhow::Result<()> { print_launch_resume(&opt, analytics.clone(), config_read_from); + tokio::spawn(async move { + tokio::signal::ctrl_c().await.unwrap(); + std::process::exit(77); + }); + run_http(index_scheduler, auth_controller, opt, log_handle, Arc::new(analytics)).await?; Ok(()) diff --git a/crates/xtask/src/bench/meili_process.rs b/crates/xtask/src/bench/meili_process.rs index db787e595..2aff679fc 100644 --- a/crates/xtask/src/bench/meili_process.rs +++ b/crates/xtask/src/bench/meili_process.rs @@ -1,23 +1,56 @@ use std::collections::BTreeMap; +use std::time::Duration; use anyhow::{bail, Context as _}; +use tokio::process::Command; +use tokio::time; use super::assets::Asset; use super::client::Client; use super::workload::Workload; pub async fn kill(mut meilisearch: tokio::process::Child) { - if let Err(error) = meilisearch.kill().await { - tracing::warn!( - error = &error as &dyn std::error::Error, - "while terminating Meilisearch server" - ) + let Some(id) = meilisearch.id() else { return }; + + match Command::new("kill").args(["--signal=TERM", &id.to_string()]).spawn() { + Ok(mut cmd) => { + let Err(error) = cmd.wait().await else { return }; + tracing::warn!( + error = &error as &dyn std::error::Error, + "while awaiting the Meilisearch server kill" + ); + } + Err(error) => { + tracing::warn!( + error = &error as &dyn std::error::Error, + "while terminating Meilisearch server with a kill -s TERM" + ); + if let Err(error) = meilisearch.kill().await { + tracing::warn!( + error = &error as &dyn std::error::Error, + "while terminating Meilisearch server" + ) + } + return; + } + }; + + match time::timeout(Duration::from_secs(5), meilisearch.wait()).await { + Ok(_) => (), + Err(_) => { + if let Err(error) = meilisearch.kill().await { + tracing::warn!( + error = &error as &dyn std::error::Error, + "while terminating Meilisearch server" + ) + } + } } } #[tracing::instrument] pub async fn build() -> anyhow::Result<()> { - let mut command = tokio::process::Command::new("cargo"); + let mut command = Command::new("cargo"); command.arg("build").arg("--release").arg("-p").arg("meilisearch"); command.kill_on_drop(true); @@ -37,7 +70,7 @@ pub async fn start( master_key: Option<&str>, workload: &Workload, asset_folder: &str, - mut command: tokio::process::Command, + mut command: Command, ) -> anyhow::Result { command.arg("--db-path").arg("./_xtask_benchmark.ms"); if let Some(master_key) = master_key { @@ -77,7 +110,7 @@ async fn wait_for_health( return Ok(()); } - tokio::time::sleep(std::time::Duration::from_millis(500)).await; + time::sleep(Duration::from_millis(500)).await; // check whether the Meilisearch instance exited early (cut the wait) if let Some(exit_code) = meilisearch.try_wait().context("cannot check Meilisearch server process status")? From 1fdfa3f20885abe5d6dcd95eda0f7e4b2678cdd1 Mon Sep 17 00:00:00 2001 From: Kerollmops Date: Thu, 12 Dec 2024 09:26:14 +0100 Subject: [PATCH 4/5] Change the exit code to 130 when Ctrl-Ced --- crates/meilisearch/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/meilisearch/src/main.rs b/crates/meilisearch/src/main.rs index 6e6245c78..ee3bbf430 100644 --- a/crates/meilisearch/src/main.rs +++ b/crates/meilisearch/src/main.rs @@ -131,7 +131,7 @@ async fn try_main() -> anyhow::Result<()> { tokio::spawn(async move { tokio::signal::ctrl_c().await.unwrap(); - std::process::exit(77); + std::process::exit(130); }); run_http(index_scheduler, auth_controller, opt, log_handle, Arc::new(analytics)).await?; From 6c72559457366da88acf191e1844cb1d353b5127 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Renault?= Date: Thu, 12 Dec 2024 09:27:10 +0100 Subject: [PATCH 5/5] Update the binary-path description Co-authored-by: Louis Dureuil --- crates/xtask/src/bench/mod.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/crates/xtask/src/bench/mod.rs b/crates/xtask/src/bench/mod.rs index 491dc33ab..1416c21d9 100644 --- a/crates/xtask/src/bench/mod.rs +++ b/crates/xtask/src/bench/mod.rs @@ -87,7 +87,9 @@ pub struct BenchDeriveArgs { #[arg(long, default_value_t = 60)] tasks_queue_timeout_secs: u64, - /// The path to the binary to run. By default it compiles the binary with cargo. + /// The path to the binary to run. + /// + /// If unspecified, runs `cargo run` after building Meilisearch with `cargo build`. #[arg(long)] binary_path: Option, }