From 935e5088e7d1df34592bc44fba8afcf9d950558e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Duarte?= Date: Mon, 1 Dec 2025 11:49:18 +0000 Subject: [PATCH 01/10] Disable ANALYZE on read-only replica --- crates/autopilot/src/database/mod.rs | 9 +++++---- crates/autopilot/src/run.rs | 17 +++++++++++------ 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/crates/autopilot/src/database/mod.rs b/crates/autopilot/src/database/mod.rs index 38bd9a50aa..97b8182cd6 100644 --- a/crates/autopilot/src/database/mod.rs +++ b/crates/autopilot/src/database/mod.rs @@ -161,11 +161,12 @@ impl Metrics { } } -pub fn run_database_metrics_work(db: Postgres) { +pub fn run_database_metrics_work(db: Postgres, run_analyze: bool) { let span = tracing::info_span!("database_metrics"); - // Spawn the task for updating large table statistics - tokio::spawn(update_large_tables_stats(db.clone()).instrument(span.clone())); - + if run_analyze { + // Spawn the task for updating large table statistics + tokio::spawn(update_large_tables_stats(db.clone()).instrument(span.clone())); + } // Spawn the task for database metrics tokio::task::spawn(database_metrics(db).instrument(span)); } diff --git a/crates/autopilot/src/run.rs b/crates/autopilot/src/run.rs index 25a21e66a8..e637d4a45c 100644 --- a/crates/autopilot/src/run.rs +++ b/crates/autopilot/src/run.rs @@ -173,17 +173,22 @@ pub async fn run(args: Arguments, shutdown_controller: ShutdownController) { .await .unwrap(); - let db_read = if let Some(db_read_url) = args.db_read_url + let (db_read, run_analyze) = if let Some(db_read_url) = args.db_read_url && args.db_write_url != db_read_url { - Postgres::new(db_read_url.as_str(), args.insert_batch_size) - .await - .expect("failed to create read replica database") + ( + Postgres::new(db_read_url.as_str(), args.insert_batch_size) + .await + .expect("failed to create read replica database"), + // If the DB is in read-only mode, running ANALYZE is not possible and will trigger and + // error https://www.postgresql.org/docs/current/hot-standby.html + false, + ) } else { - db_write.clone() + (db_write.clone(), true) }; - crate::database::run_database_metrics_work(db_read.clone()); + crate::database::run_database_metrics_work(db_read.clone(), run_analyze); let http_factory = HttpClientFactory::new(&args.http_client); let web3 = shared::ethrpc::web3( From 38aeabcad2ffed68f54eb8bef334f7ad76d438ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Duarte?= Date: Wed, 3 Dec 2025 12:52:01 +0000 Subject: [PATCH 02/10] Change the connection being used --- crates/autopilot/src/run.rs | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/crates/autopilot/src/run.rs b/crates/autopilot/src/run.rs index e637d4a45c..3bc63442f0 100644 --- a/crates/autopilot/src/run.rs +++ b/crates/autopilot/src/run.rs @@ -173,22 +173,19 @@ pub async fn run(args: Arguments, shutdown_controller: ShutdownController) { .await .unwrap(); - let (db_read, run_analyze) = if let Some(db_read_url) = args.db_read_url + let db_read = if let Some(db_read_url) = args.db_read_url && args.db_write_url != db_read_url { - ( - Postgres::new(db_read_url.as_str(), args.insert_batch_size) - .await - .expect("failed to create read replica database"), - // If the DB is in read-only mode, running ANALYZE is not possible and will trigger and - // error https://www.postgresql.org/docs/current/hot-standby.html - false, - ) + Postgres::new(db_read_url.as_str(), args.insert_batch_size) + .await + .expect("failed to create read replica database") } else { - (db_write.clone(), true) + db_write.clone() }; - crate::database::run_database_metrics_work(db_read.clone(), run_analyze); + // If the DB is in read-only mode, running ANALYZE is not possible and will + // trigger and error https://www.postgresql.org/docs/current/hot-standby.html + crate::database::run_database_metrics_work(db_read.clone()); let http_factory = HttpClientFactory::new(&args.http_client); let web3 = shared::ethrpc::web3( From 872c04fb9e5e2fe70b6dd073e6a9fcd25a178f8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Duarte?= Date: Wed, 3 Dec 2025 12:52:04 +0000 Subject: [PATCH 03/10] Change the connection being used --- crates/autopilot/src/database/mod.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/crates/autopilot/src/database/mod.rs b/crates/autopilot/src/database/mod.rs index 97b8182cd6..5f37629277 100644 --- a/crates/autopilot/src/database/mod.rs +++ b/crates/autopilot/src/database/mod.rs @@ -161,12 +161,10 @@ impl Metrics { } } -pub fn run_database_metrics_work(db: Postgres, run_analyze: bool) { +pub fn run_database_metrics_work(db: Postgres) { let span = tracing::info_span!("database_metrics"); - if run_analyze { - // Spawn the task for updating large table statistics - tokio::spawn(update_large_tables_stats(db.clone()).instrument(span.clone())); - } + // Spawn the task for updating large table statistics + tokio::spawn(update_large_tables_stats(db.clone()).instrument(span.clone())); // Spawn the task for database metrics tokio::task::spawn(database_metrics(db).instrument(span)); } From d4ac5a827e7e3bff20cfa338ef8ceca34cd0fd6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Duarte?= Date: Wed, 3 Dec 2025 12:53:00 +0000 Subject: [PATCH 04/10] Fix the db conn --- crates/autopilot/src/run.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/autopilot/src/run.rs b/crates/autopilot/src/run.rs index 3bc63442f0..8aeead0dbc 100644 --- a/crates/autopilot/src/run.rs +++ b/crates/autopilot/src/run.rs @@ -185,7 +185,7 @@ pub async fn run(args: Arguments, shutdown_controller: ShutdownController) { // If the DB is in read-only mode, running ANALYZE is not possible and will // trigger and error https://www.postgresql.org/docs/current/hot-standby.html - crate::database::run_database_metrics_work(db_read.clone()); + crate::database::run_database_metrics_work(db_write.clone()); let http_factory = HttpClientFactory::new(&args.http_client); let web3 = shared::ethrpc::web3( From 3a24f20a01c4d41a1e4eee29822bfa6df547c1b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Duarte?= Date: Thu, 4 Dec 2025 10:07:59 +0000 Subject: [PATCH 05/10] Fix lint --- crates/autopilot/src/run.rs | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/crates/autopilot/src/run.rs b/crates/autopilot/src/run.rs index 8aeead0dbc..5823554e7b 100644 --- a/crates/autopilot/src/run.rs +++ b/crates/autopilot/src/run.rs @@ -173,16 +173,6 @@ pub async fn run(args: Arguments, shutdown_controller: ShutdownController) { .await .unwrap(); - let db_read = if let Some(db_read_url) = args.db_read_url - && args.db_write_url != db_read_url - { - Postgres::new(db_read_url.as_str(), args.insert_batch_size) - .await - .expect("failed to create read replica database") - } else { - db_write.clone() - }; - // If the DB is in read-only mode, running ANALYZE is not possible and will // trigger and error https://www.postgresql.org/docs/current/hot-standby.html crate::database::run_database_metrics_work(db_write.clone()); From 508817066a516a9e2b20ffb74698e178bc881884 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Duarte?= Date: Fri, 5 Dec 2025 13:50:03 +0000 Subject: [PATCH 06/10] Remove args --- crates/autopilot/src/arguments.rs | 7 ------- 1 file changed, 7 deletions(-) diff --git a/crates/autopilot/src/arguments.rs b/crates/autopilot/src/arguments.rs index 6174b51c97..f851f0f373 100644 --- a/crates/autopilot/src/arguments.rs +++ b/crates/autopilot/src/arguments.rs @@ -65,11 +65,6 @@ pub struct Arguments { #[clap(long, env, default_value = "postgresql://")] pub db_write_url: Url, - /// Url of the Postgres database replica. By default it's the same as - /// db_write_url - #[clap(long, env)] - pub db_read_url: Option, - /// The number of order events to insert in a single batch. #[clap(long, env, default_value = "500")] pub insert_batch_size: NonZeroUsize, @@ -389,7 +384,6 @@ impl std::fmt::Display for Arguments { order_events_cleanup_interval, order_events_cleanup_threshold, db_write_url, - db_read_url, insert_batch_size, native_price_estimation_results_required, max_settlement_transaction_wait, @@ -417,7 +411,6 @@ impl std::fmt::Display for Arguments { writeln!(f, "ethflow_indexing_start: {ethflow_indexing_start:?}")?; writeln!(f, "metrics_address: {metrics_address}")?; display_secret_option(f, "db_write_url", Some(&db_write_url))?; - display_secret_option(f, "db_read_url", db_read_url.as_ref())?; writeln!(f, "skip_event_sync: {skip_event_sync}")?; writeln!(f, "allowed_tokens: {allowed_tokens:?}")?; writeln!(f, "unsupported_tokens: {unsupported_tokens:?}")?; From fb015e59c523c79b8e0603d0d15ffda8b6e40d47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Duarte?= Date: Mon, 8 Dec 2025 11:10:46 +0000 Subject: [PATCH 07/10] fix e2e tests --- crates/e2e/src/setup/services.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/crates/e2e/src/setup/services.rs b/crates/e2e/src/setup/services.rs index 3930a37d47..27a8e9a416 100644 --- a/crates/e2e/src/setup/services.rs +++ b/crates/e2e/src/setup/services.rs @@ -134,8 +134,6 @@ impl<'a> Services<'a> { "--hooks-contract-address={:?}", self.contracts.hooks.address() ), - "--db-read-url".to_string(), - LOCAL_READ_ONLY_DB_URL.clone(), ] .into_iter() } From fcd9400d85d4050cbb27b5c53efd054b2e05ae10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Duarte?= Date: Mon, 8 Dec 2025 11:25:07 +0000 Subject: [PATCH 08/10] lint --- crates/e2e/src/setup/services.rs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/crates/e2e/src/setup/services.rs b/crates/e2e/src/setup/services.rs index 27a8e9a416..31d7da4c1c 100644 --- a/crates/e2e/src/setup/services.rs +++ b/crates/e2e/src/setup/services.rs @@ -39,12 +39,6 @@ pub const TRADES_ENDPOINT: &str = "/api/v1/trades"; pub const VERSION_ENDPOINT: &str = "/api/v1/version"; pub const SOLVER_COMPETITION_ENDPOINT: &str = "/api/v2/solver_competition"; const LOCAL_DB_URL: &str = "postgresql://"; -static LOCAL_READ_ONLY_DB_URL: LazyLock = LazyLock::new(|| { - format!( - "postgresql://readonly@localhost/{db}", - db = std::env::var("USER").unwrap() - ) -}); fn order_status_endpoint(uid: &OrderUid) -> String { format!("/api/v1/orders/{uid}/status") From e7e012e8846ff0c16f48568f3de108aa3a375287 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Duarte?= Date: Mon, 8 Dec 2025 11:25:26 +0000 Subject: [PATCH 09/10] lint --- crates/e2e/src/setup/services.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/crates/e2e/src/setup/services.rs b/crates/e2e/src/setup/services.rs index 31d7da4c1c..2fc65db56d 100644 --- a/crates/e2e/src/setup/services.rs +++ b/crates/e2e/src/setup/services.rs @@ -23,7 +23,6 @@ use { std::{ collections::{HashMap, hash_map::Entry}, ops::DerefMut, - sync::LazyLock, time::Duration, }, tokio::task::JoinHandle, From 1659d21c8aba41e60e699ae7825cabf7f75c0802 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Duarte?= Date: Mon, 8 Dec 2025 11:57:25 +0000 Subject: [PATCH 10/10] move args to api --- crates/e2e/src/setup/services.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/crates/e2e/src/setup/services.rs b/crates/e2e/src/setup/services.rs index 2fc65db56d..6642297a85 100644 --- a/crates/e2e/src/setup/services.rs +++ b/crates/e2e/src/setup/services.rs @@ -23,6 +23,7 @@ use { std::{ collections::{HashMap, hash_map::Entry}, ops::DerefMut, + sync::LazyLock, time::Duration, }, tokio::task::JoinHandle, @@ -38,6 +39,12 @@ pub const TRADES_ENDPOINT: &str = "/api/v1/trades"; pub const VERSION_ENDPOINT: &str = "/api/v1/version"; pub const SOLVER_COMPETITION_ENDPOINT: &str = "/api/v2/solver_competition"; const LOCAL_DB_URL: &str = "postgresql://"; +static LOCAL_READ_ONLY_DB_URL: LazyLock = LazyLock::new(|| { + format!( + "postgresql://readonly@localhost/{db}", + db = std::env::var("USER").unwrap() + ) +}); fn order_status_endpoint(uid: &OrderUid) -> String { format!("/api/v1/orders/{uid}/status") @@ -227,6 +234,8 @@ impl<'a> Services<'a> { "orderbook".to_string(), "--quote-timeout=10s".to_string(), "--quote-verification=enforce-when-possible".to_string(), + "--db-read-url".to_string(), + LOCAL_READ_ONLY_DB_URL.clone(), ] .into_iter() .chain(self.api_autopilot_solver_arguments())