Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

fix(mysql): honor the configured budget for remaining infrastructure timeouts (#13100)

zipgb111c6ac3c94
diff --git a/crates/dbx-core/src/connection/mod.rs b/crates/dbx-core/src/connection/mod.rs
index 544a7ec83b..2bc6fcf393 100644
--- a/crates/dbx-core/src/connection/mod.rs
+++ b/crates/dbx-core/src/connection/mod.rs
@@ -518,6 +518,32 @@ pub(crate) fn uses_metadata_gate(db_type: DatabaseType) -> bool {
matches!(db_type, DatabaseType::Mysql | DatabaseType::Postgres | DatabaseType::SqlServer)
}
+/// Wait budget for the metadata gate.
+///
+/// The gate serializes metadata work against a connection's pool. On a slow
+/// link each in-flight operation can spend the connection's configured connect
+/// timeout, so the previous fixed 5 s wait reported "DBX metadata pool is busy"
+/// while useful work was still making progress. Honor the configured connect
+/// timeout (already clamped to 1-300 s by `effective_connect_timeout_secs`)
+/// without going below the old 5 s floor, so fast links keep today's behavior.
+fn metadata_pool_acquire_timeout(config: Option<&ConnectionConfig>) -> Duration {
+ let configured = config.map(ConnectionConfig::effective_connect_timeout_secs);
+ Duration::from_secs(
+ configured
+ .map_or(METADATA_POOL_ACQUIRE_TIMEOUT.as_secs(), |secs| secs.max(METADATA_POOL_ACQUIRE_TIMEOUT.as_secs())),
+ )
+}
+
+/// Whether the MySQL stale-pool probe has an idle connection to validate.
+///
+/// With zero idle connections the probe would start a fresh physical connection
+/// just to abandon it at the short probe budget; on a slow link that is one
+/// wasted handshake per metadata request. In that case keep the pool and let the
+/// real checkout report any failure.
+fn mysql_probe_has_idle_connection(connections_in_pool: usize) -> bool {
+ connections_in_pool > 0
+}
+
/// Session-scoped metadata gate allowance for a database type.
///
/// A non-empty `client_session_id` normally means the caller opened a
@@ -1820,17 +1846,22 @@ impl AppState {
let mut gates = self.metadata_gates.lock().await;
gates.entry(key.clone()).or_insert_with(|| Arc::new(Semaphore::new(limit))).clone()
};
+ let acquire_timeout = {
+ let configs = self.configs.read().await;
+ metadata_pool_acquire_timeout(configs.get(connection_id))
+ };
let started = Instant::now();
let queued = gate.available_permits() == 0;
- let permit = match tokio::time::timeout(METADATA_POOL_ACQUIRE_TIMEOUT, gate.acquire_owned()).await {
+ let permit = match tokio::time::timeout(acquire_timeout, gate.acquire_owned()).await {
Ok(Ok(permit)) => permit,
Ok(Err(_)) => return Err(crate::query::METADATA_POOL_BUSY_ERROR.to_string()),
Err(_) => {
log::warn!(
- "[metadata:pool:busy] connection_id={} database={} wait_ms={} limit={}",
+ "[metadata:pool:busy] connection_id={} database={} wait_ms={} timeout_ms={} limit={}",
connection_id,
database.unwrap_or_default(),
started.elapsed().as_millis(),
+ acquire_timeout.as_millis(),
limit
);
return Err(crate::query::METADATA_POOL_BUSY_ERROR.to_string());
@@ -4500,38 +4531,50 @@ impl AppState {
match &checked.pool {
PoolKind::Mysql(pool, _) => {
let pool = pool.clone();
- match db::mysql::checkout_mysql_conn(&pool, HEALTH_CHECK_POOL_ACQUIRE_TIMEOUT).await {
- // The 500 ms probe budget is intentionally shorter than a foreground checkout. A timeout
- // while waiting, creating, or recycling is inconclusive: slow remote handshakes and active
- // metadata exports can legitimately exceed it. Removing the pool here would start competing
- // reconnects while useful work is still running.
- Err(err @ db::PoolCheckoutError::Timeout { .. }) => {
- log::debug!(
+ let connections_in_pool = pool.metrics().connections_in_pool.load(Ordering::Relaxed);
+ if !mysql_probe_has_idle_connection(connections_in_pool) {
+ log::debug!(
+ "MySQL connection pool '{pool_key}' has no idle connection; skipping the stale-pool probe"
+ );
+ false
+ } else {
+ match db::mysql::checkout_mysql_conn(&pool, HEALTH_CHECK_POOL_ACQUIRE_TIMEOUT).await {
+ // The 500 ms probe budget is intentionally shorter than a foreground checkout. A timeout
+ // while waiting, creating, or recycling is inconclusive: slow remote handshakes and active
+ // metadata exports can legitimately exceed it. Removing the pool here would start competing
+ // reconnects while useful work is still running.
+ Err(err @ db::PoolCheckoutError::Timeout { .. }) => {
+ log::debug!(
"MySQL connection pool '{pool_key}' did not finish a health checkout; keeping pool: {err}"
);
- false
- }
- Err(err) => {
- log::warn!("MySQL connection pool '{pool_key}' is stale: {err}");
- true
- }
- Ok(mut conn) => {
- // The probe runs no statement, so a shared pool can take
- // the connection back without COM_RESET_CONNECTION and the
- // setup replay, and a connection verified moments ago (by
- // this probe or the checkout that follows it) is not
- // pinged again.
- conn.reset_connection(false);
- let timeout = crate::db::connection_timeout();
- match tokio::time::timeout(timeout, db::mysql::verify_pooled_conn(&pool, &mut conn)).await {
- Ok(Ok(())) => false,
- Ok(Err(err)) => {
- log::warn!("MySQL connection pool '{pool_key}' is stale: {err}");
- true
- }
- Err(_) => {
- log::warn!("MySQL connection pool '{pool_key}' is stale: health check timed out");
- true
+ false
+ }
+ Err(err) => {
+ log::warn!("MySQL connection pool '{pool_key}' is stale: {err}");
+ true
+ }
+ Ok(mut conn) => {
+ // The probe runs no statement, so a shared pool can take
+ // the connection back without COM_RESET_CONNECTION and the
+ // setup replay, and a connection verified moments ago (by
+ // this probe or the checkout that follows it) is not
+ // pinged again.
+ conn.reset_connection(false);
+ let timeout = crate::db::connection_timeout();
+ match tokio::time::timeout(timeout, db::mysql::verify_pooled_conn(&pool, &mut conn))
+ .await
+ {
+ Ok(Ok(())) => false,
+ Ok(Err(err)) => {
+ log::warn!("MySQL connection pool '{pool_key}' is stale: {err}");
+ true
+ }
+ Err(_) => {
+ log::warn!(
+ "MySQL connection pool '{pool_key}' is stale: health check timed out"
+ );
+ true
+ }
}
}
}
@@ -9234,7 +9277,7 @@ mod tests {
}
#[tokio::test]
- async fn mysql_health_check_keeps_pool_when_connection_creation_exceeds_probe_budget() {
+ async fn mysql_stale_pool_probe_skips_checkout_when_nothing_is_idle() {
let listener = tokio::net::TcpListener::bind(("127.0.0.1", 0)).await.unwrap();
let address = listener.local_addr().unwrap();
let server = tokio::spawn(async move {
@@ -9256,7 +9299,15 @@ mod tests {
let started = Instant::now();
assert!(!state.remove_stale_connection_pool("conn").await);
- assert!(started.elapsed() >= super::HEALTH_CHECK_POOL_ACQUIRE_TIMEOUT);
+ // A pool with no idle connection has nothing to validate: the probe must
+ // not start a fresh physical connection (and abandon it at the short
+ // probe budget). It therefore returns without the 500 ms checkout wait.
+ assert!(
+ started.elapsed() < super::HEALTH_CHECK_POOL_ACQUIRE_TIMEOUT,
+ "empty-pool probe must not run into the checkout budget, took {:?}",
+ started.elapsed()
+ );
+ assert_eq!(pool.metrics().connection_count.load(std::sync::atomic::Ordering::Relaxed), 0);
assert!(matches!(
state.connections.read().await.get("conn"),
Some(PoolKind::Mysql(current, _)) if pool.is_same_pool(current)
@@ -10096,6 +10147,28 @@ sleep 30
assert_eq!(super::metadata_gate_session_allowance(DatabaseType::SqlServer), 1);
}
+ #[test]
+ fn metadata_pool_acquire_timeout_honors_configured_connect_budget() {
+ assert_eq!(super::metadata_pool_acquire_timeout(None), super::METADATA_POOL_ACQUIRE_TIMEOUT);
+
+ let mut config = mysql_config(Some("app"));
+ config.connect_timeout_secs = 300;
+ assert_eq!(super::metadata_pool_acquire_timeout(Some(&config)), Duration::from_secs(300));
+ config.connect_timeout_secs = 60;
+ assert_eq!(super::metadata_pool_acquire_timeout(Some(&config)), Duration::from_secs(60));
+
+ // The gate keeps today's 5 s floor for fast links and tiny configured values.
+ config.connect_timeout_secs = 1;
+ assert_eq!(super::metadata_pool_acquire_timeout(Some(&config)), super::METADATA_POOL_ACQUIRE_TIMEOUT);
+ }
+
+ #[test]
+ fn mysql_stale_pool_probe_only_runs_with_an_idle_connection() {
+ assert!(!super::mysql_probe_has_idle_connection(0));
+ assert!(super::mysql_probe_has_idle_connection(1));
+ assert!(super::mysql_probe_has_idle_connection(3));
+ }
+
#[test]
fn metadata_gate_is_limited_to_supported_pool_drivers() {
assert!(super::uses_metadata_gate(DatabaseType::Mysql));
diff --git a/crates/dbx-core/src/data/query_result_export.rs b/crates/dbx-core/src/data/query_result_export.rs
index d9be37ab1a..9f052b19c4 100644
--- a/crates/dbx-core/src/data/query_result_export.rs
+++ b/crates/dbx-core/src/data/query_result_export.rs
@@ -1623,10 +1623,13 @@ async fn try_export_mysql_query_result_stream(
let kill_opts = conn.opts().clone();
if let Some(execution_id) = request.execution_id.clone() {
let interrupt_kill_opts = kill_opts.clone();
+ let kill_timeout = operation_budget.checkout_timeout;
state.running_queries.register_interrupt(&execution_id, move || {
let kill_opts = interrupt_kill_opts.clone();
tokio::spawn(async move {
- if let Err(error) = crate::db::mysql::kill_query_with_opts(kill_opts, mysql_connection_id).await {
+ if let Err(error) =
+ crate::db::mysql::kill_query_with_opts(kill_opts, mysql_connection_id, kill_timeout).await
+ {
log::warn!("Failed to cancel MySQL export query {mysql_connection_id}: {error}");
}
});
@@ -1760,7 +1763,9 @@ async fn try_export_mysql_query_result_stream(
)
.await;
if stream_result.as_ref().is_err_and(|error| error == &timeout_error) {
- let _ = crate::db::mysql::kill_query_with_opts(kill_opts, mysql_connection_id).await;
+ let _ =
+ crate::db::mysql::kill_query_with_opts(kill_opts, mysql_connection_id, operation_budget.checkout_timeout)
+ .await;
}
watcher_done.cancel();
diff --git a/crates/dbx-core/src/data/sql_file_import.rs b/crates/dbx-core/src/data/sql_file_import.rs
index 505b9639e5..09fade90b9 100644
--- a/crates/dbx-core/src/data/sql_file_import.rs
+++ b/crates/dbx-core/src/data/sql_file_import.rs
@@ -364,10 +364,11 @@ impl MySqlSqlFileExecutor {
let conn = self.conn.as_mut().ok_or("MySQL SQL file executor is missing a connection".to_string())?;
let connection_id = conn.id();
let kill_opts = conn.opts().clone();
+ let kill_timeout = self.budget.checkout_timeout;
state.running_queries.register_interrupt(execution_id, move || {
let kill_opts = kill_opts.clone();
tokio::spawn(async move {
- if let Err(error) = db::mysql::kill_query_with_opts(kill_opts, connection_id).await {
+ if let Err(error) = db::mysql::kill_query_with_opts(kill_opts, connection_id, kill_timeout).await {
log::warn!("Failed to cancel MySQL SQL file import query {connection_id}: {error}");
}
});
diff --git a/crates/dbx-core/src/query/mod.rs b/crates/dbx-core/src/query/mod.rs
index 4692c5b1fa..feb70232aa 100644
--- a/crates/dbx-core/src/query/mod.rs
+++ b/crates/dbx-core/src/query/mod.rs
@@ -1839,10 +1839,16 @@ async fn do_execute_typed(
let connection_id = conn.id();
if let Some(ref execution_id) = options.execution_id {
let kill_opts = conn.opts().clone();
+ // The interrupt runs after the user asked to cancel; give the
+ // dedicated kill connection the same infrastructure budget as
+ // the query path instead of the old compile-time 5 s.
+ let kill_timeout = operation_budget.checkout_timeout;
state.running_queries.register_interrupt(execution_id, move || {
let kill_opts = kill_opts.clone();
tokio::spawn(async move {
- if let Err(error) = db::mysql::kill_query_with_opts(kill_opts, connection_id).await {
+ if let Err(error) =
+ db::mysql::kill_query_with_opts(kill_opts, connection_id, kill_timeout).await
+ {
log::warn!("Failed to cancel MySQL query {connection_id}: {error}");
}
});
diff --git a/crates/dbx-core/src/schema/mod.rs b/crates/dbx-core/src/schema/mod.rs
index 308a8a64fd..18175e2f93 100644
--- a/crates/dbx-core/src/schema/mod.rs
+++ b/crates/dbx-core/src/schema/mod.rs
@@ -11234,7 +11234,7 @@ async fn mysql_object_source(
) -> Result<String, String> {
let primary_sql = mysql_object_source_sql(database, name, kind);
let primary_column_index = mysql_object_source_ddl_column_index(kind);
- let mut conn = db::mysql::get_conn_with_timeout(pool, db::connection_timeout()).await?;
+ let mut conn = db::mysql::get_conn_with_timeout(pool, pool.checkout_timeout()).await?;
match read_mysql_object_source_row(&mut conn, &primary_sql, primary_column_index).await {
Ok(source) => Ok(source),
diff --git a/crates/dbx-core/tests/live_mysql57.rs b/crates/dbx-core/tests/live_mysql57.rs
index d77a025553..44eb1b64b6 100644
--- a/crates/dbx-core/tests/live_mysql57.rs
+++ b/crates/dbx-core/tests/live_mysql57.rs
@@ -955,7 +955,9 @@ async fn live_mysql_query_cancel_kills_running_sleep() {
});
tokio::time::sleep(std::time::Duration::from_millis(500)).await;
- dbx_core::db::mysql::kill_query_with_opts(kill_opts, connection_id).await.unwrap();
+ dbx_core::db::mysql::kill_query_with_opts(kill_opts, connection_id, dbx_core::db::connection_timeout())
+ .await
+ .unwrap();
let started = std::time::Instant::now();
let result = query.await.unwrap();
diff --git a/crates/dbx-driver-mysql/src/dolt.rs b/crates/dbx-driver-mysql/src/dolt.rs
index 90c297e83c..07bcf4e3df 100644
--- a/crates/dbx-driver-mysql/src/dolt.rs
+++ b/crates/dbx-driver-mysql/src/dolt.rs
@@ -40,7 +40,7 @@ fn is_profile(db_type: &DatabaseType, driver_profile: Option<&str>) -> bool {
}
pub async fn list_databases(pool: &MySqlPool) -> Result<Vec<DatabaseInfo>, String> {
- let mut conn = get_conn_with_timeout(pool, super::connection_timeout()).await?;
+ let mut conn = get_conn_with_timeout(pool, pool.checkout_timeout()).await?;
if let Err(error) = conn.query_drop(ENABLE_BRANCH_DATABASES_SQL).await {
log::debug!("Dolt branch database system variable is unavailable; falling back to dolt_branches: {error}");
return list_databases_from_branches(&mut conn).await;
@@ -61,7 +61,7 @@ pub async fn list_system_tables(
) -> Result<Vec<TableInfo>, String> {
// The visibility flag is session-scoped, so setup, metadata lookup, and
// cleanup must use the same pooled connection.
- let mut conn = get_conn_with_timeout(pool, super::connection_timeout()).await?;
+ let mut conn = get_conn_with_timeout(pool, pool.checkout_timeout()).await?;
conn.query_drop(ENABLE_SYSTEM_TABLES_SQL).await.map_err(|error| error.to_string())?;
let server_filter = filter.map(str::trim).filter(|filter| !filter.is_empty()).unwrap_or(SYSTEM_TABLE_PREFIX);
let tables = list_table_names_show_filtered_with_conn(&mut conn, database, Some(server_filter), &[])
diff --git a/crates/dbx-driver-mysql/src/mysql.rs b/crates/dbx-driver-mysql/src/mysql.rs
index f1fc018588..f6e77b5cf9 100644
--- a/crates/dbx-driver-mysql/src/mysql.rs
+++ b/crates/dbx-driver-mysql/src/mysql.rs
@@ -6335,9 +6335,18 @@ pub async fn kill_query(pool: &MySqlPool, connection_id: u32) -> Result<(), Stri
Ok(())
}
-pub async fn kill_query_with_opts(opts: mysql_async::Opts, connection_id: u32) -> Result<(), String> {
+/// Kills a query through a dedicated connection rebuilt from its `Opts`.
+///
+/// Cancel paths run while the original pool may be busy, so this opens a new
+/// physical connection. `timeout` is the caller's infrastructure budget (the
+/// connection's configured connect/checkout timeout), not the old compile-time
+/// 5 s: on a slow link a hardcoded budget turns cancellation into a failure.
+pub async fn kill_query_with_opts(
+ opts: mysql_async::Opts,
+ connection_id: u32,
+ timeout: Duration,
+) -> Result<(), String> {
let start = Instant::now();
- let timeout = super::connection_timeout();
let mut conn = tokio::time::timeout(timeout, mysql_async::Conn::new(opts))
.await
.map_err(|_| {
diff --git a/crates/dbx-driver-mysql/src/mysql_compatible.rs b/crates/dbx-driver-mysql/src/mysql_compatible.rs
index 815cedc27c..aa71a99dc9 100644
--- a/crates/dbx-driver-mysql/src/mysql_compatible.rs
+++ b/crates/dbx-driver-mysql/src/mysql_compatible.rs
@@ -84,7 +84,7 @@ fn catalog_table_ref(catalog: &str, database: &str, table: &str) -> String {
/// `default_catalog` in StarRocks (both with `Type=internal`); detection is
/// type-based (see `CatalogInfo::is_internal`), not name-based.
pub async fn list_catalogs(pool: &MySqlPool) -> Result<Vec<crate::db::CatalogInfo>, String> {
- let mut conn = get_conn_with_timeout(pool, super::connection_timeout()).await?;
+ let mut conn = get_conn_with_timeout(pool, pool.checkout_timeout()).await?;
let result = conn.query_iter("SHOW CATALOGS").await.map_err(|e| e.to_string())?;
let rows: Vec<mysql_async::Row> = result.collect_and_drop().await.map_err(|e| e.to_string())?;
let catalogs: Vec<crate::db::CatalogInfo> = rows
@@ -124,7 +124,7 @@ fn normalize_catalogs(mut catalogs: Vec<crate::db::CatalogInfo>) -> Vec<crate::d
/// `SHOW DATABASES FROM <catalog>` → databases in the given catalog.
pub async fn list_databases_show_from(pool: &MySqlPool, catalog: &str) -> Result<Vec<DatabaseInfo>, String> {
- let mut conn = get_conn_with_timeout(pool, super::connection_timeout()).await?;
+ let mut conn = get_conn_with_timeout(pool, pool.checkout_timeout()).await?;
let sql = format!("SHOW DATABASES FROM {}", quote_identifier(catalog));
let result = conn.query_iter(&sql).await.map_err(|e| e.to_string())?;
let rows: Vec<mysql_async::Row> = result.collect_and_drop().await.map_err(|e| e.to_string())?;
@@ -137,7 +137,7 @@ pub async fn list_databases_show_from(pool: &MySqlPool, catalog: &str) -> Result
/// not fetched (the caller only needs names + types for browsing).
pub async fn list_tables_show_from(pool: &MySqlPool, catalog: &str, database: &str) -> Result<Vec<TableInfo>, String> {
let sql = format!("SHOW TABLES FROM {}", catalog_database_ref(catalog, database));
- let mut conn = get_conn_with_timeout(pool, super::connection_timeout()).await?;
+ let mut conn = get_conn_with_timeout(pool, pool.checkout_timeout()).await?;
let result = conn.query_iter(&sql).await.map_err(|e| e.to_string())?;
let rows: Vec<mysql_async::Row> = result.collect_and_drop().await.map_err(|e| e.to_string())?;
let mut tables: Vec<TableInfo> = rows
diff --git a/crates/dbx-driver-mysql/src/oceanbase_mysql.rs b/crates/dbx-driver-mysql/src/oceanbase_mysql.rs
index 17c2668a61..7efb70c566 100644
--- a/crates/dbx-driver-mysql/src/oceanbase_mysql.rs
+++ b/crates/dbx-driver-mysql/src/oceanbase_mysql.rs
@@ -16,7 +16,7 @@ fn materialized_views_sql(database: &str) -> String {
}
async fn list_materialized_view_names(pool: &MySqlPool, database: &str) -> Result<HashSet<String>, String> {
- let mut conn = get_conn_with_timeout(pool, super::connection_timeout()).await?;
+ let mut conn = get_conn_with_timeout(pool, pool.checkout_timeout()).await?;
let result = conn.query_iter(materialized_views_sql(database)).await.map_err(|error| error.to_string())?;
let rows: Vec<mysql_async::Row> = result.collect_and_drop().await.map_err(|error| error.to_string())?;
Ok(rows
diff --git a/crates/dbx-driver-mysql/src/starrocks.rs b/crates/dbx-driver-mysql/src/starrocks.rs
index 23dab966e7..0697f44823 100644
--- a/crates/dbx-driver-mysql/src/starrocks.rs
+++ b/crates/dbx-driver-mysql/src/starrocks.rs
@@ -71,7 +71,7 @@ pub fn materialized_view_definition_sql(database: &str, name: &str) -> String {
async fn list_materialized_view_names(pool: &MySqlPool, database: &str) -> Result<HashSet<String>, String> {
let sql = materialized_views_sql(database);
- let mut conn = get_conn_with_timeout(pool, super::connection_timeout()).await?;
+ let mut conn = get_conn_with_timeout(pool, pool.checkout_timeout()).await?;
let result = conn.query_iter(&sql).await.map_err(|error| error.to_string())?;
let rows: Vec<mysql_async::Row> = result.collect_and_drop().await.map_err(|error| error.to_string())?;
Ok(rows