feat(mcp): execute sql batches with opt-in rollbackable transactions

This commit is contained in:
Abeautifulsnow authored and GitHub committed 2026-09-04 13:43:06 +08:00
1 parent 7390ac109c
commit 543bf66905
18 files changed
+2496 -219

No files matched your search

+33 -1
View File
@@ -2248,7 +2248,15 @@ impl AppState {
| DatabaseType::Kwdb
| DatabaseType::Questdb
| DatabaseType::OpenGauss => {
let pg_pool = db::postgres::connect(&url, connect_timeout).await?;
// A session pool must have one physical client: sequential
// statements can otherwise lose temp tables/SET state when a
// pool checkout selects another PostgreSQL connection.
let pg_pool = db::postgres::connect_with_max_connections(
&url,
connect_timeout,
postgres_pool_max_connections_for_session(client_session_id),
)
.await?;
// Build TLS cancel context for reconstructing TLS connection during cancel
if let Some(ctx) = db::postgres::build_postgres_cancel_context(&url) {
self.postgres_cancel_contexts.write().await.insert(pool_key.clone(), ctx);
@@ -5397,6 +5405,14 @@ fn mysql_pool_max_connections_for_session(client_session_id: Option<&str>) -> us
}
}
fn postgres_pool_max_connections_for_session(client_session_id: Option<&str>) -> usize {
if normalize_client_session_id(client_session_id).is_some() {
1
} else {
10
}
}
fn redis_cluster_transport_prefix(connection_id: &str) -> String {
format!("{connection_id}:redis-cluster:")
}
@@ -7729,6 +7745,22 @@ mod tests {
assert_eq!(super::mysql_pool_max_connections_for_session(Some("tab-1")), 1);
}
#[test]
fn postgres_pool_size_keeps_session_pools_single_connection() {
assert_eq!(super::postgres_pool_max_connections_for_session(None), 10);
assert_eq!(super::postgres_pool_max_connections_for_session(Some("")), 10);
assert_eq!(super::postgres_pool_max_connections_for_session(Some("mcp:batch-1")), 1);
}
#[test]
fn stale_mysql_pool_observation_does_not_remove_replacement_generation() {
let checked = crate::db::mysql::MySqlPool::new("mysql://root@127.0.0.1:3306/app", 10);
let checked_clone = checked.clone();
let replacement = crate::db::mysql::MySqlPool::new("mysql://root@127.0.0.1:3306/app", 10);
assert!(checked.is_same_pool(&checked_clone));
assert!(!checked.is_same_pool(&replacement));
}
#[tokio::test]
async fn stale_postgres_cleanup_preserves_concurrent_replacement_publication() {
let (state, dir) = test_app_state().await;
+61 -5
View File
@@ -2558,9 +2558,15 @@ impl AgentDriverClient {
database: Option<&str>,
statements: &[String],
schema: Option<&str>,
timeout_duration: Option<Duration>,
) -> Result<T, String> {
self.invalidate_cached_query();
self.call_method(AgentMethod::ExecuteTransaction, agent_transaction_params(database, statements, schema)).await
self.call_method_with_timeout(
AgentMethod::ExecuteTransaction,
agent_transaction_params(database, statements, schema),
timeout_duration,
)
.await
}
pub async fn execute_transaction_typed<T: DeserializeOwned + Send + 'static>(
@@ -2568,10 +2574,15 @@ impl AgentDriverClient {
database: Option<&str>,
statements: &[String],
schema: Option<&str>,
timeout_duration: Option<Duration>,
) -> Result<T, AgentCallError> {
self.invalidate_cached_query();
self.call_method_typed(AgentMethod::ExecuteTransaction, agent_transaction_params(database, statements, schema))
.await
self.call_method_typed_with_timeout(
AgentMethod::ExecuteTransaction,
agent_transaction_params(database, statements, schema),
timeout_duration,
)
.await
}
pub async fn begin_manual_transaction<T: DeserializeOwned + Send + 'static>(
@@ -4161,7 +4172,7 @@ mod tests {
let script_path = std::env::temp_dir().join(format!("dbx-agent-{prefix}-{}.py", uuid::Uuid::new_v4()));
std::fs::write(
&script_path,
r#"import json, sys, threading
r#"import json, sys, threading, time
print(json.dumps({'ready': True}), flush=True)
state_lock = threading.Lock()
output_lock = threading.Lock()
@@ -4232,6 +4243,12 @@ def respond(req):
return
write_response(req, {'sql': sql, 'count': current_count})
return
if method == 'execute_transaction':
statements = params.get('statements', [])
if statements == ['slow']:
time.sleep(1.2)
write_response(req, {'ok': True})
return
write_response(req, {'ok': True})
for line in sys.stdin:
@@ -4486,7 +4503,10 @@ for line in sys.stdin:
)
.await
.unwrap();
client.execute_transaction::<serde_json::Value>(None, &["UPDATE t SET a = 2".to_string()], None).await.unwrap();
client
.execute_transaction::<serde_json::Value>(None, &["UPDATE t SET a = 2".to_string()], None, None)
.await
.unwrap();
client
.execute_query_cached_with_timeout::<serde_json::Value>(
"transaction-invalidation".to_string(),
@@ -4515,6 +4535,42 @@ for line in sys.stdin:
let _ = std::fs::remove_file(script_path);
}
#[tokio::test]
async fn execute_transaction_typed_applies_timeout_when_agent_stalls() {
let (runtime, script_path) = spawn_stateful_test_runtime("transaction-timeout-test").await;
let mut client = AgentDriverClient::shared_session(runtime.clone(), "transaction-timeout-session".to_string());
// A single slow statement with a short timeout must surface a typed timeout
// error instead of waiting for the agent indefinitely.
let error = client
.execute_transaction_typed::<serde_json::Value>(
None,
&["slow".to_string()],
None,
Some(Duration::from_millis(75)),
)
.await
.unwrap_err();
assert!(matches!(
error,
AgentCallError::Timeout {
stage: AgentErrorStage::Execute,
operation_outcome: AgentOperationOutcome::Unknown,
}
));
// A fast transaction with a comfortable timeout still succeeds and
// invalidates the cached query, proving the timeout path did not break it.
let result: serde_json::Value = client
.execute_transaction_typed(None, &["UPDATE t SET a = 2".to_string()], None, Some(Duration::from_secs(2)))
.await
.unwrap();
assert_eq!(result, serde_json::json!({"ok": true}));
runtime.kill();
let _ = std::fs::remove_file(script_path);
}
#[tokio::test]
async fn shared_runtime_tolerates_post_startup_driver_stdout_noise() {
let script_path =
+42 -6
View File
@@ -2043,20 +2043,45 @@ async fn stream_query_rows_text_on_client(
}
pub async fn connect(url: &str, fallback_timeout: Duration) -> Result<Pool, String> {
connect_with_max_connections(url, fallback_timeout, 10).await
}
/// Creates a PostgreSQL pool with an explicit checkout bound.
///
/// Session-scoped DBX pools use a single connection so temporary tables and
/// other connection-local state cannot migrate between physical clients.
pub async fn connect_with_max_connections(
url: &str,
fallback_timeout: Duration,
max_connections: usize,
) -> Result<Pool, String> {
#[cfg(all(windows, target_vendor = "win7"))]
{
connect_with_optional_local_timezone(url, fallback_timeout, None).await
connect_with_optional_local_timezone_with_max_connections(url, fallback_timeout, None, max_connections).await
}
#[cfg(not(all(windows, target_vendor = "win7")))]
{
let timezone = iana_time_zone::get_timezone().unwrap_or_else(|_| "UTC".to_string());
connect_with_local_timezone(url, fallback_timeout, &timezone).await
connect_with_local_timezone_with_max_connections(url, fallback_timeout, &timezone, max_connections).await
}
}
/// Test-only thin wrappers (the library now connects through
/// `connect_with_max_connections`, which carries the pool bound).
#[cfg(test)]
async fn connect_with_local_timezone(url: &str, fallback_timeout: Duration, timezone: &str) -> Result<Pool, String> {
connect_with_optional_local_timezone(url, fallback_timeout, Some(timezone)).await
connect_with_local_timezone_with_max_connections(url, fallback_timeout, timezone, 10).await
}
async fn connect_with_local_timezone_with_max_connections(
url: &str,
fallback_timeout: Duration,
timezone: &str,
max_connections: usize,
) -> Result<Pool, String> {
connect_with_optional_local_timezone_with_max_connections(url, fallback_timeout, Some(timezone), max_connections)
.await
}
/// Identity of one physical backend connection for notice attribution:
@@ -2247,23 +2272,33 @@ async fn drain_postgres_notices(client: &deadpool_postgres::Client) -> Vec<Query
}
}
#[cfg(test)]
async fn connect_with_optional_local_timezone(
url: &str,
fallback_timeout: Duration,
timezone: Option<&str>,
) -> Result<Pool, String> {
connect_with_optional_local_timezone_with_max_connections(url, fallback_timeout, timezone, 10).await
}
async fn connect_with_optional_local_timezone_with_max_connections(
url: &str,
fallback_timeout: Duration,
timezone: Option<&str>,
max_connections: usize,
) -> Result<Pool, String> {
let url_with_keepalive = inject_postgres_keepalive_params(url);
let _ = rustls::crypto::aws_lc_rs::default_provider().install_default();
let timeout = super::parse_connect_timeout_with_fallback(url, fallback_timeout);
let first_attempt = connect_postgres_pool_attempt(&url_with_keepalive, timeout).await;
let first_attempt = connect_postgres_pool_attempt(&url_with_keepalive, timeout, max_connections).await;
let (pool, client) = match first_attempt {
Err(error) if postgres_error_should_retry_without_tls(&error) => {
let Some(fallback_url) = postgres_ssl_fallback_url(&url_with_keepalive) else {
return Err(error);
};
log::info!("PostgreSQL TLS handshake failed in sslmode=prefer; retrying without TLS");
connect_postgres_pool_attempt(&fallback_url, timeout).await?
connect_postgres_pool_attempt(&fallback_url, timeout, max_connections).await?
}
result => result?,
};
@@ -2285,6 +2320,7 @@ async fn connect_with_optional_local_timezone(
async fn connect_postgres_pool_attempt(
url: &str,
timeout: Duration,
max_connections: usize,
) -> Result<(Pool, deadpool_postgres::Client), String> {
let postgres_url = postgres_connection_url(url)?;
super::with_connection_timeout("PostgreSQL", timeout, async {
@@ -2320,7 +2356,7 @@ async fn connect_postgres_pool_attempt(
)
};
let pool = Pool::builder(mgr)
.max_size(10)
.max_size(max_connections.max(1))
.runtime(Runtime::Tokio1)
.wait_timeout(Some(timeout))
.create_timeout(Some(timeout))
File diff suppressed because it is too large. Load diff