diff --git a/apps/desktop/src/stores/queryStore.ts b/apps/desktop/src/stores/queryStore.ts index 225b20b1f4..94f931e6c5 100644 --- a/apps/desktop/src/stores/queryStore.ts +++ b/apps/desktop/src/stores/queryStore.ts @@ -46,6 +46,11 @@ import type { SavedSqlFile } from "@/types/database"; const STORAGE_KEY = "dbx-open-tabs"; const ACTIVE_TAB_KEY = "dbx-active-tab"; const ORACLE_LIKE_METADATA_TYPES = new Set(["oracle", "dameng", "oceanbase-oracle"]); +const BACKGROUND_CLIENT_SESSION_SUFFIXES = ["count", "explain", "export"] as const; + +function tabClientSessionId(tab: Pick, suffix?: (typeof BACKGROUND_CLIENT_SESSION_SUFFIXES)[number]): string { + return suffix ? `${tab.id}:${suffix}` : tab.id; +} function resultRunCacheKey(tabId: string, runId: string): string { return `tab:${tabId}:run:${runId}`; @@ -169,12 +174,19 @@ export const useQueryStore = defineStore("query", () => { } } + async function closeClientSessionId(connectionId: string, database: string, clientSessionId: string, logContext: Record = {}) { + try { + await api.closeClientConnectionSession(connectionId, database, clientSessionId); + } catch (error) { + console.warn("[DBX][client-session:close:error]", { ...logContext, clientSessionId, error }); + } + } + async function closeClientConnectionSession(tab: QueryTab | undefined) { if (!tab?.connectionId) return; - try { - await api.closeClientConnectionSession(tab.connectionId, tab.database, tab.id); - } catch (error) { - console.warn("[DBX][client-session:close:error]", { tabId: tab.id, error }); + const clientSessionIds = [tabClientSessionId(tab), ...BACKGROUND_CLIENT_SESSION_SUFFIXES.map((suffix) => tabClientSessionId(tab, suffix))]; + for (const clientSessionId of clientSessionIds) { + await closeClientSessionId(tab.connectionId, tab.database, clientSessionId, { tabId: tab.id }); } } @@ -1198,6 +1210,7 @@ export const useQueryStore = defineStore("query", () => { return; } const countSql = options.countSql; + const clientSessionId = tabClientSessionId({ id: options.tabId }, "count"); if (typeof options.pageLimit === "number" && resultRowCount < options.pageLimit) { setQueryTotalRowCountIfCurrent(options.tabId, options.executionId, options.result, (options.pageOffset ?? 0) + resultRowCount); @@ -1207,7 +1220,10 @@ export const useQueryStore = defineStore("query", () => { void (async () => { try { console.info("[DBX][executeTabSql:count:start]", { traceId: options.traceId, elapsed: options.elapsed() }); - const countResult = await api.executeQuery(options.connectionId, options.database, countSql, options.schema, undefined, { timeoutSecs: options.timeoutSecs }); + const countResult = await api.executeQuery(options.connectionId, options.database, countSql, options.schema, undefined, { + clientSessionId, + timeoutSecs: options.timeoutSecs, + }); const total = Number(countResult.rows?.[0]?.[0] ?? 0); if (!Number.isFinite(total) || total < 0) { setQueryTotalRowCountIfCurrent(options.tabId, options.executionId, options.result, undefined); @@ -1226,6 +1242,8 @@ export const useQueryStore = defineStore("query", () => { elapsed: options.elapsed(), error, }); + } finally { + void closeClientSessionId(options.connectionId, options.database, clientSessionId, { tabId: options.tabId }); } })(); } @@ -1567,7 +1585,7 @@ export const useQueryStore = defineStore("query", () => { } console.info("[DBX][executeTabSql:execute-multi:start]", { traceId, elapsed: elapsed() }); - const clientSessionId = tab.mode === "query" ? tab.id : undefined; + const clientSessionId = tab.mode === "query" || tab.mode === "data" ? tabClientSessionId(tab) : undefined; const executionOptions = { ...(typeof pageLimit === "number" ? useAgentResultSession @@ -1770,8 +1788,10 @@ export const useQueryStore = defineStore("query", () => { } tab.explainSql = built.sql; + const clientSessionId = tabClientSessionId(tab, "explain"); try { const result = await api.executeQuery(tab.connectionId, tab.database, built.sql, tab.schema, executionId, { + clientSessionId, timeoutSecs: queryTimeoutSecs, }); const current = tabs.value.find((t) => t.id === id); @@ -1791,6 +1811,7 @@ export const useQueryStore = defineStore("query", () => { current.isExplaining = false; current.explainExecutionId = undefined; } + void closeClientSessionId(tab.connectionId, tab.database, clientSessionId, { tabId: tab.id }); } return { ok: true as const, sql: built.sql }; } @@ -2011,33 +2032,39 @@ export const useQueryStore = defineStore("query", () => { let columns: string[] = []; let executionTimeMs = 0; let offset = 0; + const clientSessionId = tabClientSessionId(tab, "export"); - while (true) { - const sql = await api.buildTableSelectSql({ - databaseType: effectiveDbType, - schema: tableMeta.schema, - tableName: tableMeta.tableName, - columns: tableMeta.columns.map((column) => column.name), - primaryKeys, - fallbackOrderColumns, - whereInput: tab.whereInput, - orderBy, - limit: pageLimit, - offset, - }); - const results = await api.executeMulti(tab.connectionId, tab.database, sql, undefined, undefined, { - maxRows: pageLimit, - fetchSize: pageLimit, - timeoutSecs: queryTimeoutSecs, - }); - const result = results[0]; - if (!result) break; - if (columns.length === 0) columns = result.columns; - rows.push(...result.rows); - executionTimeMs += result.execution_time_ms ?? 0; - onProgress?.({ rowsExported: rows.length, totalRows }); - if (result.rows.length < pageLimit) break; - offset += result.rows.length; + try { + while (true) { + const sql = await api.buildTableSelectSql({ + databaseType: effectiveDbType, + schema: tableMeta.schema, + tableName: tableMeta.tableName, + columns: tableMeta.columns.map((column) => column.name), + primaryKeys, + fallbackOrderColumns, + whereInput: tab.whereInput, + orderBy, + limit: pageLimit, + offset, + }); + const results = await api.executeMulti(tab.connectionId, tab.database, sql, undefined, undefined, { + maxRows: pageLimit, + fetchSize: pageLimit, + clientSessionId, + timeoutSecs: queryTimeoutSecs, + }); + const result = results[0]; + if (!result) break; + if (columns.length === 0) columns = result.columns; + rows.push(...result.rows); + executionTimeMs += result.execution_time_ms ?? 0; + onProgress?.({ rowsExported: rows.length, totalRows }); + if (result.rows.length < pageLimit) break; + offset += result.rows.length; + } + } finally { + void closeClientSessionId(tab.connectionId, tab.database, clientSessionId, { tabId: tab.id }); } return { @@ -2071,7 +2098,7 @@ export const useQueryStore = defineStore("query", () => { let executionTimeMs = 0; let offset = 0; let sessionId: string | undefined; - const clientSessionId = `${tab.id}:export`; + const clientSessionId = tabClientSessionId(tab, "export"); try { while (true) { @@ -2091,7 +2118,7 @@ export const useQueryStore = defineStore("query", () => { clientSessionId, timeoutSecs: queryTimeoutSecs, } - : { maxRows: plan.pageLimit, fetchSize: plan.pageLimit, timeoutSecs: queryTimeoutSecs }; + : { maxRows: plan.pageLimit, fetchSize: plan.pageLimit, clientSessionId, timeoutSecs: queryTimeoutSecs }; const results = await api.executeMulti(tab.connectionId, tab.database, plan.sqlToExecute, tab.schema, undefined, executionOptions); const result = results[0]; if (!result) break; @@ -2106,6 +2133,7 @@ export const useQueryStore = defineStore("query", () => { } } finally { if (sessionId) void api.closeQuerySession(tab.connectionId, tab.database, sessionId, clientSessionId); + void closeClientSessionId(tab.connectionId, tab.database, clientSessionId, { tabId: tab.id }); } return { diff --git a/crates/dbx-core/assets/agent-protocol-v1.json b/crates/dbx-core/assets/agent-protocol-v1.json index ada128177f..12d0356e90 100644 --- a/crates/dbx-core/assets/agent-protocol-v1.json +++ b/crates/dbx-core/assets/agent-protocol-v1.json @@ -38,6 +38,7 @@ "handshake", "connect", "test_connection", + "validate_connection", "list_databases", "list_schemas", "list_tables", diff --git a/crates/dbx-core/src/connection.rs b/crates/dbx-core/src/connection.rs index 317ce8a77b..c970849b69 100644 --- a/crates/dbx-core/src/connection.rs +++ b/crates/dbx-core/src/connection.rs @@ -82,8 +82,8 @@ pub enum PoolKind { } pub struct AppState { - pub connections: RwLock>, - keepalive_tasks: RwLock>>, + pub connections: Arc>>, + keepalive_tasks: Arc>>>, pub configs: RwLock>, pub running_queries: RunningQueries, pub tunnels: TunnelManager, @@ -261,8 +261,8 @@ impl AppState { app_version: impl Into, ) -> Self { Self { - connections: RwLock::new(HashMap::new()), - keepalive_tasks: RwLock::new(HashMap::new()), + connections: Arc::new(RwLock::new(HashMap::new())), + keepalive_tasks: Arc::new(RwLock::new(HashMap::new())), configs: RwLock::new(HashMap::new()), running_queries: RunningQueries::default(), tunnels: TunnelManager::new(), @@ -348,13 +348,23 @@ impl AppState { let key = pool_key.to_string(); let interval = Duration::from_secs(interval_secs.max(1)); let timeout = Duration::from_secs(config.effective_connect_timeout_secs().max(1)); + let connections = self.connections.clone(); + let keepalive_tasks = self.keepalive_tasks.clone(); let handle = tokio::spawn(async move { loop { tokio::time::sleep(interval).await; let result = tokio::time::timeout(timeout, ping_keepalive_target(&mut target, timeout)).await; match result { Ok(Ok(())) => {} - Ok(Err(err)) => log::warn!("Connection keepalive failed for '{key}': {err}"), + Ok(Err(err)) => { + log::warn!("Connection keepalive failed for '{key}': {err}; invalidating pool"); + keepalive_tasks.write().await.remove(&key); + let removed = connections.write().await.remove(&key); + if let Some(pool) = removed { + close_pool_kind_with_timeout(key, pool).await; + } + break; + } Err(_) => log::warn!("Connection keepalive timed out for '{key}' after {}s", timeout.as_secs()), } } @@ -1053,7 +1063,8 @@ impl AppState { let client = client.clone(); drop(connections); let mut agent = client.lock().await; - match agent.test_connection(serde_json::json!({})).await { + let timeout = crate::db::connection_timeout(); + match agent.validate_connection(Some(timeout)).await { Ok(_) => false, Err(err) => { log::warn!("Agent connection pool '{pool_key}' is stale: {err}"); @@ -1535,6 +1546,7 @@ enum KeepaliveTarget { Elasticsearch(db::elasticsearch_driver::EsClient), VectorDb(db::vector_driver::VectorClient), InfluxDb(db::influxdb_driver::InfluxdbClient), + Agent(Arc>), } fn keepalive_target_from_pool(pool: &PoolKind, config: &ConnectionConfig) -> Option { @@ -1552,6 +1564,7 @@ fn keepalive_target_from_pool(pool: &PoolKind, config: &ConnectionConfig) -> Opt PoolKind::Elasticsearch(client) => Some(KeepaliveTarget::Elasticsearch(client.clone())), PoolKind::VectorDb(client) => Some(KeepaliveTarget::VectorDb(client.clone())), PoolKind::InfluxDb(client) => Some(KeepaliveTarget::InfluxDb(client.clone())), + PoolKind::Agent(client) => Some(KeepaliveTarget::Agent(client.clone())), _ => None, } } @@ -1573,12 +1586,26 @@ async fn ping_keepalive_target(target: &mut KeepaliveTarget, timeout: Duration) } KeepaliveTarget::ClickHouse(client) => db::clickhouse_driver::test_connection(client, timeout).await, KeepaliveTarget::SqlServer(client) => { - let mut client = client.lock().await; + let Ok(mut client) = client.try_lock() else { + return Ok(()); + }; db::sqlserver::test_connection(&mut client).await } KeepaliveTarget::Elasticsearch(client) => db::elasticsearch_driver::test_connection(client, timeout).await, KeepaliveTarget::VectorDb(client) => db::vector_driver::test_connection(client, timeout).await, KeepaliveTarget::InfluxDb(client) => db::influxdb_driver::test_connection(client, timeout).await, + KeepaliveTarget::Agent(client) => { + let Ok(mut client) = client.try_lock() else { + return Ok(()); + }; + match client.validate_connection(Some(timeout)).await { + Ok(_) => Ok(()), + Err(err) => { + client.kill(); + Err(err) + } + } + } } } diff --git a/crates/dbx-core/src/db/agent_driver.rs b/crates/dbx-core/src/db/agent_driver.rs index d97b692fdb..5c20930756 100644 --- a/crates/dbx-core/src/db/agent_driver.rs +++ b/crates/dbx-core/src/db/agent_driver.rs @@ -8,6 +8,7 @@ use std::time::Duration; use serde::de::DeserializeOwned; use serde::{Deserialize, Serialize}; use serde_json::Value; +use tokio_util::sync::CancellationToken; pub const AGENT_PROTOCOL_VERSION: u32 = 1; const RPC_TIMEOUT_SECS: u64 = 30; @@ -114,6 +115,7 @@ pub enum AgentMethod { Handshake, Connect, TestConnection, + ValidateConnection, ListDatabases, ListSchemas, ListTables, @@ -135,10 +137,11 @@ pub enum AgentMethod { } impl AgentMethod { - pub const ALL: [Self; 21] = [ + pub const ALL: [Self; 22] = [ Self::Handshake, Self::Connect, Self::TestConnection, + Self::ValidateConnection, Self::ListDatabases, Self::ListSchemas, Self::ListTables, @@ -164,6 +167,7 @@ impl AgentMethod { Self::Handshake => "handshake", Self::Connect => "connect", Self::TestConnection => "test_connection", + Self::ValidateConnection => "validate_connection", Self::ListDatabases => "list_databases", Self::ListSchemas => "list_schemas", Self::ListTables => "list_tables", @@ -370,6 +374,19 @@ impl AgentDriverClient { method: &str, params: Value, timeout_duration: Option, + ) -> Result { + self.call_with_timeout_and_cancel(method, params, timeout_duration, None).await + } + + /// Send a JSON-RPC 2.0 request and wait for the response. + /// If cancellation happens while a response is pending, kill the agent + /// process because the stdio stream cannot safely skip that response. + pub async fn call_with_timeout_and_cancel( + &mut self, + method: &str, + params: Value, + timeout_duration: Option, + cancel_token: Option, ) -> Result { self.next_id += 1; let id = self.next_id; @@ -427,15 +444,41 @@ impl AgentDriverClient { (reader, result) }); - let (returned_reader, result) = match timeout_duration { - Some(duration) => match tokio::time::timeout(duration, response_task).await { + let (returned_reader, result) = match (timeout_duration, cancel_token) { + (Some(duration), Some(token)) => { + tokio::select! { + biased; + _ = token.cancelled() => { + self.kill(); + return Err("Query canceled".to_string()); + } + result = tokio::time::timeout(duration, response_task) => match result { + Ok(result) => result, + Err(_) => { + self.kill(); + return Err(format!("Agent RPC call timed out ({}s)", duration.as_secs())); + } + }, + } + } + (Some(duration), None) => match tokio::time::timeout(duration, response_task).await { Ok(result) => result, Err(_) => { self.kill(); return Err(format!("Agent RPC call timed out ({}s)", duration.as_secs())); } }, - None => response_task.await, + (None, Some(token)) => { + tokio::select! { + biased; + _ = token.cancelled() => { + self.kill(); + return Err("Query canceled".to_string()); + } + result = response_task => result, + } + } + (None, None) => response_task.await, } .map_err(|e| format!("Agent RPC task failed: {e}"))?; @@ -460,6 +503,16 @@ impl AgentDriverClient { self.call_with_timeout(method.as_str(), params, timeout_duration).await } + pub async fn call_method_with_timeout_and_cancel( + &mut self, + method: AgentMethod, + params: Value, + timeout_duration: Option, + cancel_token: Option, + ) -> Result { + self.call_with_timeout_and_cancel(method.as_str(), params, timeout_duration, cancel_token).await + } + pub async fn connect(&mut self, params: Value) -> Result { self.call_method(AgentMethod::Connect, params).await } @@ -468,6 +521,10 @@ impl AgentDriverClient { self.call_method(AgentMethod::TestConnection, params).await } + pub async fn validate_connection(&mut self, timeout_duration: Option) -> Result { + self.call_method_with_timeout(AgentMethod::ValidateConnection, serde_json::json!({}), timeout_duration).await + } + pub async fn disconnect(&mut self) -> Result { self.call_method(AgentMethod::Disconnect, serde_json::json!({})).await } @@ -615,6 +672,16 @@ impl AgentDriverClient { self.call_method_with_timeout(AgentMethod::ExecuteQuery, params, timeout_duration).await } + pub async fn execute_query_with_timeout_and_cancel( + &mut self, + params: Value, + timeout_duration: Option, + cancel_token: Option, + ) -> Result { + self.call_method_with_timeout_and_cancel(AgentMethod::ExecuteQuery, params, timeout_duration, cancel_token) + .await + } + pub async fn execute_query_page( &mut self, params: Value, @@ -630,6 +697,16 @@ impl AgentDriverClient { self.call_method_with_timeout(AgentMethod::ExecuteQueryPage, params, timeout_duration).await } + pub async fn execute_query_page_with_timeout_and_cancel( + &mut self, + params: Value, + timeout_duration: Option, + cancel_token: Option, + ) -> Result { + self.call_method_with_timeout_and_cancel(AgentMethod::ExecuteQueryPage, params, timeout_duration, cancel_token) + .await + } + pub async fn fetch_query_page(&mut self, params: Value) -> Result { self.call_method(AgentMethod::FetchQueryPage, params).await } @@ -642,6 +719,16 @@ impl AgentDriverClient { self.call_method_with_timeout(AgentMethod::FetchQueryPage, params, timeout_duration).await } + pub async fn fetch_query_page_with_timeout_and_cancel( + &mut self, + params: Value, + timeout_duration: Option, + cancel_token: Option, + ) -> Result { + self.call_method_with_timeout_and_cancel(AgentMethod::FetchQueryPage, params, timeout_duration, cancel_token) + .await + } + pub async fn get_explain_info(&mut self, params: Value) -> Result { self.call_method(AgentMethod::GetExplainInfo, params).await } @@ -1153,6 +1240,7 @@ mod tests { assert_eq!(AgentMethod::Handshake.as_str(), "handshake"); assert_eq!(AgentMethod::Connect.as_str(), "connect"); assert_eq!(AgentMethod::TestConnection.as_str(), "test_connection"); + assert_eq!(AgentMethod::ValidateConnection.as_str(), "validate_connection"); assert_eq!(AgentMethod::ListDatabases.as_str(), "list_databases"); assert_eq!(AgentMethod::ListSchemas.as_str(), "list_schemas"); assert_eq!(AgentMethod::ListTables.as_str(), "list_tables"); diff --git a/crates/dbx-core/src/db/postgres.rs b/crates/dbx-core/src/db/postgres.rs index ebdcfeaf7f..9916a497b2 100644 --- a/crates/dbx-core/src/db/postgres.rs +++ b/crates/dbx-core/src/db/postgres.rs @@ -9,13 +9,15 @@ use rustls::crypto::{verify_tls12_signature, verify_tls13_signature, CryptoProvi use rustls::pki_types::{CertificateDer, PrivateKeyDer, ServerName, UnixTime}; use rustls::server::ParsedCertificate; use std::fs::File; +use std::future::Future; use std::io::BufReader; use std::str::FromStr; use std::sync::Arc; use std::time::{Duration, Instant}; use tokio_postgres::config::SslMode; use tokio_postgres::types::{FromSql, Type}; -use tokio_postgres::{Row, SimpleQueryMessage}; +use tokio_postgres::{NoTls, Row, SimpleQueryMessage}; +use tokio_util::sync::CancellationToken; use super::file_validator::validate_file_path; use crate::sql::starts_with_executable_sql_keyword; @@ -1330,6 +1332,24 @@ pub async fn execute_query_with_max_rows( } } +pub async fn execute_query_with_max_rows_and_cancel( + pool: &Pool, + sql: &str, + max_rows: Option, + cancel_token: Option, + timeout_duration: Option, +) -> Result { + let client = pool.get().await.map_err(|e| e.to_string())?; + let pg_cancel_token = client.cancel_token(); + wait_postgres_query( + pg_cancel_token, + cancel_token, + timeout_duration, + execute_query_with_max_rows_inner(&client, sql, max_rows), + ) + .await +} + pub async fn execute_query_with_schema(pool: &Pool, schema: &str, sql: &str) -> Result { execute_query_with_schema_and_max_rows(pool, schema, sql, None).await } @@ -1389,6 +1409,131 @@ pub async fn execute_query_with_schema_and_max_rows( result } +pub async fn execute_query_with_schema_and_max_rows_and_cancel( + pool: &Pool, + schema: &str, + sql: &str, + max_rows: Option, + cancel_token: Option, + timeout_duration: Option, +) -> Result { + let start = Instant::now(); + let checkout_start = Instant::now(); + let client = pool.get().await.map_err(|e| e.to_string())?; + log::info!( + "[postgres][execute_with_schema:pool:done] elapsed_ms={} total_ms={} schema={}", + checkout_start.elapsed().as_millis(), + start.elapsed().as_millis(), + schema + ); + if is_transaction_recovery_statement(sql) { + log::info!( + "[postgres][execute_with_schema:skip-search-path] total_ms={} reason=transaction-recovery", + start.elapsed().as_millis() + ); + let pg_cancel_token = client.cancel_token(); + return wait_postgres_query( + pg_cancel_token, + cancel_token, + timeout_duration, + execute_query_with_max_rows_inner(&client, sql, max_rows), + ) + .await; + } + + let set_schema_start = Instant::now(); + client.execute(&format!("SET search_path TO {}", pg_quote_ident(schema)), &[]).await.map_err(pg_error_to_string)?; + log::info!( + "[postgres][execute_with_schema:set-search-path:done] elapsed_ms={} total_ms={}", + set_schema_start.elapsed().as_millis(), + start.elapsed().as_millis() + ); + + let query_start = Instant::now(); + let pg_cancel_token = client.cancel_token(); + let result = wait_postgres_query( + pg_cancel_token, + cancel_token, + timeout_duration, + execute_query_with_max_rows_inner(&client, sql, max_rows), + ) + .await; + if result.is_ok() { + clear_postgres_caches_after_ddl(pool, Some(&client), sql); + } + log::info!( + "[postgres][execute_with_schema:query:done] elapsed_ms={} total_ms={} ok={}", + query_start.elapsed().as_millis(), + start.elapsed().as_millis(), + result.is_ok() + ); + + let reset_start = Instant::now(); + let _ = client.execute("RESET search_path", &[]).await; + log::info!( + "[postgres][execute_with_schema:reset-search-path:done] elapsed_ms={} total_ms={}", + reset_start.elapsed().as_millis(), + start.elapsed().as_millis() + ); + + result +} + +async fn wait_postgres_query( + pg_cancel_token: tokio_postgres::CancelToken, + cancel_token: Option, + timeout_duration: Option, + future: F, +) -> Result +where + F: Future>, +{ + match (cancel_token, timeout_duration) { + (Some(token), Some(duration)) => { + tokio::select! { + biased; + _ = token.cancelled() => { + cancel_postgres_query(pg_cancel_token).await; + Err(crate::query::canceled_error()) + } + result = tokio::time::timeout(duration, future) => match result { + Ok(result) => result, + Err(_) => { + cancel_postgres_query(pg_cancel_token).await; + Err(format!("Query timed out after {} seconds", duration.as_secs())) + } + }, + } + } + (None, Some(duration)) => match tokio::time::timeout(duration, future).await { + Ok(result) => result, + Err(_) => { + cancel_postgres_query(pg_cancel_token).await; + Err(format!("Query timed out after {} seconds", duration.as_secs())) + } + }, + (Some(token), None) => { + tokio::select! { + biased; + _ = token.cancelled() => { + cancel_postgres_query(pg_cancel_token).await; + Err(crate::query::canceled_error()) + } + result = future => result, + } + } + (None, None) => future.await, + } +} + +async fn cancel_postgres_query(pg_cancel_token: tokio_postgres::CancelToken) { + match tokio::time::timeout(Duration::from_secs(2), pg_cancel_token.cancel_query(NoTls)).await { + Ok(Ok(())) => {} + Ok(Err(err)) => log::warn!("Failed to send PostgreSQL cancel request: {err}"), + Err(_) => log::warn!("Timed out sending PostgreSQL cancel request"), + } +} + fn is_transaction_recovery_statement(sql: &str) -> bool { starts_with_executable_sql_keyword(sql, &["ROLLBACK", "ABORT", "COMMIT", "END"]) } diff --git a/crates/dbx-core/src/query.rs b/crates/dbx-core/src/query.rs index 4ebfae5041..6fb32027b4 100644 --- a/crates/dbx-core/src/query.rs +++ b/crates/dbx-core/src/query.rs @@ -877,21 +877,21 @@ pub async fn do_execute( let p = p.clone(); let schema = schema.map(|s| s.to_string()); let max_rows = options.max_rows; + let query_timeout = query_timeout; drop(connections); if let Some(schema) = schema { - wait_for_query_opt( + db::postgres::execute_query_with_schema_and_max_rows_and_cancel( + &p, + &schema, + sql, + max_rows, cancel_token, query_timeout, - db::postgres::execute_query_with_schema_and_max_rows(&p, &schema, sql, max_rows), ) .await } else { - wait_for_query_opt( - cancel_token, - query_timeout, - db::postgres::execute_query_with_max_rows(&p, sql, max_rows), - ) - .await + db::postgres::execute_query_with_max_rows_and_cancel(&p, sql, max_rows, cancel_token, query_timeout) + .await } } PoolKind::Sqlite(p) => { @@ -1024,21 +1024,39 @@ pub async fn do_execute( let max_rows = options.max_rows; let rpc_timeout = query_timeout; drop(connections); - let result = wait_for_query_opt(cancel_token, query_timeout, async move { - let mut client = client.lock().await; + if is_canceled(&cancel_token) { + return Err(canceled_error()); + } + let cancel_for_agent = cancel_token.clone(); + let result = async move { + let mut client = match cancel_for_agent.as_ref() { + Some(token) => { + tokio::select! { + biased; + _ = token.cancelled() => return Err(canceled_error()), + guard = client.lock() => guard, + } + } + None => client.lock().await, + }; if let Some(session_id) = options.result_session_id.as_deref() { let params = agent_fetch_query_page_params(session_id, options.page_size.unwrap_or(MAX_ROWS)); - client.fetch_query_page_with_timeout(params, rpc_timeout).await + client.fetch_query_page_with_timeout_and_cancel(params, rpc_timeout, cancel_for_agent.clone()).await } else if options.page_size.is_some() { let params = agent_execute_query_page_params(&sql, database.as_deref(), schema.as_deref(), options); - client.execute_query_page_with_timeout(params, rpc_timeout).await + client + .execute_query_page_with_timeout_and_cancel(params, rpc_timeout, cancel_for_agent.clone()) + .await } else { let params = agent_execute_query_params(&sql, database.as_deref(), schema.as_deref(), options); - client.execute_query_with_timeout(params, rpc_timeout).await + client.execute_query_with_timeout_and_cancel(params, rpc_timeout, cancel_for_agent.clone()).await } - }) + } .await .map(|result| normalize_query_result_for_js(truncate_result_with_max_rows(result, max_rows))); + if matches!(result.as_ref(), Err(err) if err == QUERY_CANCELED) { + state.remove_pool_by_key(pool_key).await; + } if matches!(result.as_ref(), Err(err) if should_discard_pool_after_error(pool_db_type, err)) { state.remove_pool_by_key(pool_key).await; } diff --git a/crates/dbx-core/src/schema.rs b/crates/dbx-core/src/schema.rs index 390c987039..ee0c72ad69 100644 --- a/crates/dbx-core/src/schema.rs +++ b/crates/dbx-core/src/schema.rs @@ -2112,6 +2112,20 @@ pub async fn get_object_source_core( schema: &str, name: &str, object_type: db::ObjectSourceKind, +) -> Result { + retry_metadata_connection(state, connection_id, Some(database), || { + get_object_source_once(state, connection_id, database, schema, name, object_type.clone()) + }) + .await +} + +async fn get_object_source_once( + state: &AppState, + connection_id: &str, + database: &str, + schema: &str, + name: &str, + object_type: db::ObjectSourceKind, ) -> Result { let pool_key = state.get_or_create_pool(connection_id, Some(database)).await?; let db_config = connection_config(state, connection_id).await;