From f52064bd0e7c93c7166f590603512919949a6c39 Mon Sep 17 00:00:00 2001 From: t8y2 <1156263951@qq.com> Date: Mon, 22 Jun 2026 18:17:11 +0800 Subject: [PATCH] fix(agent): extend metadata RPC timeout --- crates/dbx-core/src/db/agent_driver.rs | 77 +++++++++++++--- crates/dbx-core/src/schema.rs | 119 +++++++++++++++++++------ crates/dbx-core/src/transfer.rs | 2 +- 3 files changed, 157 insertions(+), 41 deletions(-) diff --git a/crates/dbx-core/src/db/agent_driver.rs b/crates/dbx-core/src/db/agent_driver.rs index 1903ef927d..d97b692fdb 100644 --- a/crates/dbx-core/src/db/agent_driver.rs +++ b/crates/dbx-core/src/db/agent_driver.rs @@ -472,28 +472,44 @@ impl AgentDriverClient { self.call_method(AgentMethod::Disconnect, serde_json::json!({})).await } - pub async fn list_databases(&mut self) -> Result { - self.call_method(AgentMethod::ListDatabases, serde_json::json!({})).await + pub async fn list_databases( + &mut self, + timeout_duration: Option, + ) -> Result { + self.call_method_with_timeout(AgentMethod::ListDatabases, serde_json::json!({}), timeout_duration).await } - pub async fn list_schemas(&mut self, database: &str) -> Result { - self.call_method(AgentMethod::ListSchemas, serde_json::json!({ "database": database })).await + pub async fn list_schemas( + &mut self, + database: &str, + timeout_duration: Option, + ) -> Result { + self.call_method_with_timeout( + AgentMethod::ListSchemas, + serde_json::json!({ "database": database }), + timeout_duration, + ) + .await } pub async fn list_tables( &mut self, database: &str, schema: &str, + timeout_duration: Option, ) -> Result { - self.call_method(AgentMethod::ListTables, agent_schema_params(database, schema)).await + self.call_method_with_timeout(AgentMethod::ListTables, agent_schema_params(database, schema), timeout_duration) + .await } pub async fn list_objects( &mut self, database: &str, schema: &str, + timeout_duration: Option, ) -> Result { - self.call_method(AgentMethod::ListObjects, agent_schema_params(database, schema)).await + self.call_method_with_timeout(AgentMethod::ListObjects, agent_schema_params(database, schema), timeout_duration) + .await } pub async fn get_object_source( @@ -502,9 +518,14 @@ impl AgentDriverClient { schema: &str, name: &str, object_type: &K, + timeout_duration: Option, ) -> Result { - self.call_method(AgentMethod::GetObjectSource, agent_object_source_params(database, schema, name, object_type)) - .await + self.call_method_with_timeout( + AgentMethod::GetObjectSource, + agent_object_source_params(database, schema, name, object_type), + timeout_duration, + ) + .await } pub async fn get_columns( @@ -512,8 +533,14 @@ impl AgentDriverClient { database: &str, schema: &str, table: &str, + timeout_duration: Option, ) -> Result { - self.call_method(AgentMethod::GetColumns, agent_schema_table_params(database, schema, table)).await + self.call_method_with_timeout( + AgentMethod::GetColumns, + agent_schema_table_params(database, schema, table), + timeout_duration, + ) + .await } pub async fn list_indexes( @@ -521,8 +548,14 @@ impl AgentDriverClient { database: &str, schema: &str, table: &str, + timeout_duration: Option, ) -> Result { - self.call_method(AgentMethod::ListIndexes, agent_schema_table_params(database, schema, table)).await + self.call_method_with_timeout( + AgentMethod::ListIndexes, + agent_schema_table_params(database, schema, table), + timeout_duration, + ) + .await } pub async fn list_foreign_keys( @@ -530,8 +563,14 @@ impl AgentDriverClient { database: &str, schema: &str, table: &str, + timeout_duration: Option, ) -> Result { - self.call_method(AgentMethod::ListForeignKeys, agent_schema_table_params(database, schema, table)).await + self.call_method_with_timeout( + AgentMethod::ListForeignKeys, + agent_schema_table_params(database, schema, table), + timeout_duration, + ) + .await } pub async fn list_triggers( @@ -539,8 +578,14 @@ impl AgentDriverClient { database: &str, schema: &str, table: &str, + timeout_duration: Option, ) -> Result { - self.call_method(AgentMethod::ListTriggers, agent_schema_table_params(database, schema, table)).await + self.call_method_with_timeout( + AgentMethod::ListTriggers, + agent_schema_table_params(database, schema, table), + timeout_duration, + ) + .await } pub async fn get_table_ddl( @@ -548,8 +593,14 @@ impl AgentDriverClient { database: &str, schema: &str, table: &str, + timeout_duration: Option, ) -> Result { - self.call_method(AgentMethod::GetTableDdl, agent_schema_table_params(database, schema, table)).await + self.call_method_with_timeout( + AgentMethod::GetTableDdl, + agent_schema_table_params(database, schema, table), + timeout_duration, + ) + .await } pub async fn execute_query(&mut self, params: Value) -> Result { diff --git a/crates/dbx-core/src/schema.rs b/crates/dbx-core/src/schema.rs index e44c04ec20..390c987039 100644 --- a/crates/dbx-core/src/schema.rs +++ b/crates/dbx-core/src/schema.rs @@ -35,16 +35,6 @@ macro_rules! try_sqlserver { }; } -macro_rules! try_agent { - ($connections:expr, $pool_key:expr, $method:ident $(, $arg:expr)*) => { - if let Some(client) = extract_pool!(&$connections, $pool_key, Agent) { - drop($connections); - let mut client = client.lock().await; - return client.$method($($arg),*).await; - } - }; -} - #[cfg(feature = "duckdb-bundled")] pub fn duckdb_query_tables(con: &duckdb::Connection) -> Result, String> { duckdb_query_tables_in_database(con, "main", "main") @@ -268,6 +258,16 @@ fn clickhouse_metadata_database<'a>(database: &'a str, schema: &'a str) -> &'a s } } +fn agent_metadata_timeout(config: Option<&ConnectionConfig>) -> Option { + let Some(config) = config else { + return Some(Duration::from_secs(60)); + }; + match config.effective_query_timeout_secs() { + 0 => None, + seconds => Some(Duration::from_secs(seconds.max(60))), + } +} + pub async fn list_databases_core(state: &AppState, connection_id: &str) -> Result, String> { retry_metadata_connection(state, connection_id, None, || list_databases_once(state, connection_id)).await } @@ -336,6 +336,7 @@ pub async fn list_sqlserver_linked_server_tables_core( async fn list_databases_once(state: &AppState, connection_id: &str) -> Result, String> { log::info!("[list_databases] connection_id={connection_id}"); + let db_config = connection_config(state, connection_id).await; { let connections = state.connections.read().await; #[cfg(feature = "duckdb-bundled")] @@ -369,7 +370,7 @@ async fn list_databases_once(state: &AppState, connection_id: &str) -> Result>(database).await { + match client.list_schemas::>(database, agent_metadata_timeout(db_config.as_ref())).await { Ok(schemas) if !schemas.is_empty() => return Ok(schemas), Ok(schemas) => { if let Some(config) = fallback_config.as_ref() { @@ -584,7 +585,10 @@ async fn list_tables_once( let fallback_config = db_config.clone(); drop(connections); let mut client = client.lock().await; - match client.list_tables::>(database, schema).await { + match client + .list_tables::>(database, schema, agent_metadata_timeout(db_config.as_ref())) + .await + { Ok(tables) if !tables.is_empty() => { return Ok(filter_table_infos(tables, filter, limit, offset, object_types)) } @@ -990,6 +994,20 @@ mod tests { assert!(!is_agent_postgres_metadata_fallback_config(&test_connection_config(DatabaseType::Postgres))); assert!(!is_agent_postgres_metadata_fallback_config(&test_connection_config(DatabaseType::Mysql))); } + + #[test] + fn agent_metadata_timeout_defaults_to_sixty_seconds_and_honors_longer_config() { + assert_eq!(super::agent_metadata_timeout(None), Some(std::time::Duration::from_secs(60))); + + let mut config = test_connection_config(DatabaseType::Oracle); + assert_eq!(super::agent_metadata_timeout(Some(&config)), Some(std::time::Duration::from_secs(60))); + + config.query_timeout_secs = 120; + assert_eq!(super::agent_metadata_timeout(Some(&config)), Some(std::time::Duration::from_secs(120))); + + config.query_timeout_secs = 0; + assert_eq!(super::agent_metadata_timeout(Some(&config)), None); + } } pub async fn list_objects_core( @@ -1104,10 +1122,14 @@ async fn list_objects_once( let fallback_config = db_config.clone(); drop(connections); if is_oracle { - return oracle_agent_list_objects(client, database, schema).await; + return oracle_agent_list_objects(client, database, schema, agent_metadata_timeout(db_config.as_ref())) + .await; } let mut client = client.lock().await; - match client.list_objects::>(database, schema).await { + match client + .list_objects::>(database, schema, agent_metadata_timeout(db_config.as_ref())) + .await + { Ok(objects) if !objects.is_empty() => return Ok(objects), Ok(objects) => { if let Some(config) = fallback_config.as_ref() { @@ -1210,10 +1232,13 @@ async fn list_completion_objects_once( let fallback_config = db_config.clone(); drop(connections); let objects = if is_oracle { - oracle_agent_list_objects(client, database, schema).await? + oracle_agent_list_objects(client, database, schema, agent_metadata_timeout(db_config.as_ref())).await? } else { let mut client = client.lock().await; - match client.list_objects::>(database, schema).await { + match client + .list_objects::>(database, schema, agent_metadata_timeout(db_config.as_ref())) + .await + { Ok(objects) if !objects.is_empty() => objects, Ok(objects) => { if let Some(config) = fallback_config.as_ref() { @@ -1418,7 +1443,15 @@ pub async fn get_columns_core( let fallback_config = db_config.clone(); drop(connections); let mut client = client.lock().await; - match client.get_columns::>(database, schema, table).await { + match client + .get_columns::>( + database, + schema, + table, + agent_metadata_timeout(db_config.as_ref()), + ) + .await + { Ok(columns) if !columns.is_empty() => return Ok(deduplicate_column_infos(columns)), Ok(columns) => { if let Some(config) = fallback_config.as_ref() { @@ -1554,7 +1587,11 @@ pub async fn list_indexes_core( { let connections = state.connections.read().await; try_sqlserver!(connections, &pool_key, list_indexes, schema, table); - try_agent!(connections, &pool_key, list_indexes, database, schema, table); + if let Some(client) = extract_pool!(&connections, &pool_key, Agent) { + drop(connections); + let mut client = client.lock().await; + return client.list_indexes(database, schema, table, agent_metadata_timeout(db_config.as_ref())).await; + } } let connections = state.connections.read().await; @@ -1592,11 +1629,18 @@ pub async fn list_foreign_keys_core( } retry_metadata_connection(state, connection_id, Some(database), || async { let pool_key = state.get_or_create_pool(connection_id, Some(database)).await?; + let db_config = connection_config(state, connection_id).await; { let connections = state.connections.read().await; try_sqlserver!(connections, &pool_key, list_foreign_keys, schema, table); - try_agent!(connections, &pool_key, list_foreign_keys, database, schema, table); + if let Some(client) = extract_pool!(&connections, &pool_key, Agent) { + drop(connections); + let mut client = client.lock().await; + return client + .list_foreign_keys(database, schema, table, agent_metadata_timeout(db_config.as_ref())) + .await; + } } let connections = state.connections.read().await; @@ -1627,11 +1671,16 @@ pub async fn list_triggers_core( } retry_metadata_connection(state, connection_id, Some(database), || async { let pool_key = state.get_or_create_pool(connection_id, Some(database)).await?; + let db_config = connection_config(state, connection_id).await; { let connections = state.connections.read().await; try_sqlserver!(connections, &pool_key, list_triggers, schema, table); - try_agent!(connections, &pool_key, list_triggers, database, schema, table); + if let Some(client) = extract_pool!(&connections, &pool_key, Agent) { + drop(connections); + let mut client = client.lock().await; + return client.list_triggers(database, schema, table, agent_metadata_timeout(db_config.as_ref())).await; + } } let connections = state.connections.read().await; @@ -1751,6 +1800,7 @@ pub async fn get_table_ddl_core( } let pool_key = state.get_or_create_pool(connection_id, Some(database)).await?; + let db_config = connection_config(state, connection_id).await; { let connections = state.connections.read().await; @@ -1790,12 +1840,15 @@ pub async fn get_table_ddl_core( let mut client = client.lock().await; return build_sqlserver_ddl(&mut client, schema, table).await; } - try_agent!(connections, &pool_key, get_table_ddl, database, schema, table); + if let Some(client) = extract_pool!(&connections, &pool_key, Agent) { + drop(connections); + let mut client = client.lock().await; + return client.get_table_ddl(database, schema, table, agent_metadata_timeout(db_config.as_ref())).await; + } } let connections = state.connections.read().await; let pool = connections.get(&pool_key).ok_or("Pool not found")?; - let db_config = connection_config(state, connection_id).await; match pool { PoolKind::Mysql(p, _) => mysql_ddl(p, table).await, @@ -2099,10 +2152,20 @@ pub async fn get_object_source_core( if db_config.as_ref().is_some_and(|config| config.db_type == DatabaseType::Oracle) && matches!(object_type, db::ObjectSourceKind::Package | db::ObjectSourceKind::PackageBody) { - oracle_agent_object_source(client, database, schema, name, &object_type).await? + oracle_agent_object_source( + client, + database, + schema, + name, + &object_type, + agent_metadata_timeout(db_config.as_ref()), + ) + .await? } else { let mut client = client.lock().await; - let result: db::ObjectSource = client.get_object_source(database, schema, name, &object_type).await?; + let result: db::ObjectSource = client + .get_object_source(database, schema, name, &object_type, agent_metadata_timeout(db_config.as_ref())) + .await?; return Ok(result); } } else { @@ -2164,6 +2227,7 @@ async fn oracle_agent_list_objects( client: Arc>, database: &str, schema: &str, + timeout_duration: Option, ) -> Result, String> { let sql = oracle_list_objects_sql(schema); let params = agent_execute_query_params( @@ -2173,7 +2237,7 @@ async fn oracle_agent_list_objects( QueryExecutionOptions { max_rows: Some(10_000), ..Default::default() }, ); let mut client = client.lock().await; - let result: db::QueryResult = client.execute_query(params).await?; + let result: db::QueryResult = client.execute_query_with_timeout(params, timeout_duration).await?; Ok(result .rows .into_iter() @@ -2201,6 +2265,7 @@ async fn oracle_agent_object_source( schema: &str, name: &str, object_type: &db::ObjectSourceKind, + timeout_duration: Option, ) -> Result { let sql = oracle_object_source_sql(schema, name, object_type); let params = agent_execute_query_params( @@ -2210,7 +2275,7 @@ async fn oracle_agent_object_source( QueryExecutionOptions { max_rows: Some(1), ..Default::default() }, ); let mut client = client.lock().await; - let result: db::QueryResult = client.execute_query(params).await?; + let result: db::QueryResult = client.execute_query_with_timeout(params, timeout_duration).await?; first_string_cell(result) } diff --git a/crates/dbx-core/src/transfer.rs b/crates/dbx-core/src/transfer.rs index 9e57670f04..0bdca50b45 100644 --- a/crates/dbx-core/src/transfer.rs +++ b/crates/dbx-core/src/transfer.rs @@ -2081,7 +2081,7 @@ pub async fn get_columns_for_transfer( let table = table.to_string(); drop(connections); let mut client = client.lock().await; - return client.get_columns(&database, &schema, &table).await; + return client.get_columns(&database, &schema, &table, None).await; } let pool = connections.get(pool_key).ok_or("Pool not found")?; let schema = schema.to_string();