feat(mcp): add native MySQL session transactions
This commit is contained in:
1 parent
01bd07bb97
commit
1294708eea
27 files changed
+5621
-91
No files matched your search
Generated
+3
@@ -2259,6 +2259,7 @@ dependencies = [
|
|||||||
"dbx-core",
|
"dbx-core",
|
||||||
"dirs",
|
"dirs",
|
||||||
"log",
|
"log",
|
||||||
|
"mysql_async",
|
||||||
"reqwest 0.12.28",
|
"reqwest 0.12.28",
|
||||||
"rmcp",
|
"rmcp",
|
||||||
"rustls 0.23.43",
|
"rustls 0.23.43",
|
||||||
@@ -2266,6 +2267,7 @@ dependencies = [
|
|||||||
"schemars 1.2.2",
|
"schemars 1.2.2",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
|
"sqlparser",
|
||||||
"tempfile",
|
"tempfile",
|
||||||
"tokio",
|
"tokio",
|
||||||
"tokio-rustls 0.26.4",
|
"tokio-rustls 0.26.4",
|
||||||
@@ -7343,6 +7345,7 @@ dependencies = [
|
|||||||
"pastey",
|
"pastey",
|
||||||
"pin-project-lite",
|
"pin-project-lite",
|
||||||
"rand 0.10.2",
|
"rand 0.10.2",
|
||||||
|
"reqwest 0.13.4",
|
||||||
"rmcp-macros",
|
"rmcp-macros",
|
||||||
"schemars 1.2.2",
|
"schemars 1.2.2",
|
||||||
"serde",
|
"serde",
|
||||||
|
|||||||
@@ -7974,6 +7974,9 @@ export default withEnglishFallback({
|
|||||||
mcpToolExecuteQuery: "SQL / Mongo əmrlərini icra et",
|
mcpToolExecuteQuery: "SQL / Mongo əmrlərini icra et",
|
||||||
mcpToolExecuteBatch: "SQL əmrlərini toplu icra et",
|
mcpToolExecuteBatch: "SQL əmrlərini toplu icra et",
|
||||||
mcpToolOpenSession: "Sorğu sessiyasını aç",
|
mcpToolOpenSession: "Sorğu sessiyasını aç",
|
||||||
|
mcpToolBeginTransaction: "Tranzaksiyanı başlat",
|
||||||
|
mcpToolCommitTransaction: "Tranzaksiyanı təsdiqlə",
|
||||||
|
mcpToolRollbackTransaction: "Tranzaksiyanı geri qaytar",
|
||||||
mcpToolCloseSession: "Sorğu sessiyasını bağla",
|
mcpToolCloseSession: "Sorğu sessiyasını bağla",
|
||||||
mcpToolExecuteRedisCommand: "Redis əmrini icra et",
|
mcpToolExecuteRedisCommand: "Redis əmrini icra et",
|
||||||
mcpToolPeekMessages: "Kafka mesajlarını oxu",
|
mcpToolPeekMessages: "Kafka mesajlarını oxu",
|
||||||
|
|||||||
@@ -8240,6 +8240,9 @@ export default {
|
|||||||
mcpToolExecuteQuery: "Execute SQL / Mongo commands",
|
mcpToolExecuteQuery: "Execute SQL / Mongo commands",
|
||||||
mcpToolExecuteBatch: "Execute SQL batch",
|
mcpToolExecuteBatch: "Execute SQL batch",
|
||||||
mcpToolOpenSession: "Open query session",
|
mcpToolOpenSession: "Open query session",
|
||||||
|
mcpToolBeginTransaction: "Begin transaction",
|
||||||
|
mcpToolCommitTransaction: "Commit transaction",
|
||||||
|
mcpToolRollbackTransaction: "Roll back transaction",
|
||||||
mcpToolCloseSession: "Close query session",
|
mcpToolCloseSession: "Close query session",
|
||||||
mcpToolExecuteRedisCommand: "Execute Redis command",
|
mcpToolExecuteRedisCommand: "Execute Redis command",
|
||||||
mcpToolPeekMessages: "Read Kafka messages",
|
mcpToolPeekMessages: "Read Kafka messages",
|
||||||
|
|||||||
@@ -7734,6 +7734,9 @@ export default withEnglishFallback({
|
|||||||
mcpToolExecuteQuery: "Ejecutar SQL / comandos Mongo",
|
mcpToolExecuteQuery: "Ejecutar SQL / comandos Mongo",
|
||||||
mcpToolExecuteBatch: "Ejecutar lote SQL",
|
mcpToolExecuteBatch: "Ejecutar lote SQL",
|
||||||
mcpToolOpenSession: "Abrir sesión de consulta",
|
mcpToolOpenSession: "Abrir sesión de consulta",
|
||||||
|
mcpToolBeginTransaction: "Iniciar transacción",
|
||||||
|
mcpToolCommitTransaction: "Confirmar transacción",
|
||||||
|
mcpToolRollbackTransaction: "Revertir transacción",
|
||||||
mcpToolCloseSession: "Cerrar sesión de consulta",
|
mcpToolCloseSession: "Cerrar sesión de consulta",
|
||||||
mcpToolExecuteRedisCommand: "Ejecutar comando Redis",
|
mcpToolExecuteRedisCommand: "Ejecutar comando Redis",
|
||||||
mcpToolPeekMessages: "Leer mensajes de Kafka",
|
mcpToolPeekMessages: "Leer mensajes de Kafka",
|
||||||
|
|||||||
@@ -7733,6 +7733,9 @@ export default withEnglishFallback({
|
|||||||
mcpToolExecuteQuery: "Esegui SQL / comandi Mongo",
|
mcpToolExecuteQuery: "Esegui SQL / comandi Mongo",
|
||||||
mcpToolExecuteBatch: "Esegui batch SQL",
|
mcpToolExecuteBatch: "Esegui batch SQL",
|
||||||
mcpToolOpenSession: "Apri sessione di query",
|
mcpToolOpenSession: "Apri sessione di query",
|
||||||
|
mcpToolBeginTransaction: "Avvia transazione",
|
||||||
|
mcpToolCommitTransaction: "Conferma transazione",
|
||||||
|
mcpToolRollbackTransaction: "Annulla transazione",
|
||||||
mcpToolCloseSession: "Chiudi sessione di query",
|
mcpToolCloseSession: "Chiudi sessione di query",
|
||||||
mcpToolExecuteRedisCommand: "Esegui comando Redis",
|
mcpToolExecuteRedisCommand: "Esegui comando Redis",
|
||||||
mcpToolPeekMessages: "Leggi messaggi Kafka",
|
mcpToolPeekMessages: "Leggi messaggi Kafka",
|
||||||
|
|||||||
@@ -7738,6 +7738,9 @@ export default withEnglishFallback({
|
|||||||
mcpToolExecuteQuery: "SQL / Mongo コマンドを実行",
|
mcpToolExecuteQuery: "SQL / Mongo コマンドを実行",
|
||||||
mcpToolExecuteBatch: "SQL バッチを実行",
|
mcpToolExecuteBatch: "SQL バッチを実行",
|
||||||
mcpToolOpenSession: "クエリセッションを開く",
|
mcpToolOpenSession: "クエリセッションを開く",
|
||||||
|
mcpToolBeginTransaction: "トランザクションを開始",
|
||||||
|
mcpToolCommitTransaction: "トランザクションをコミット",
|
||||||
|
mcpToolRollbackTransaction: "トランザクションをロールバック",
|
||||||
mcpToolCloseSession: "クエリセッションを閉じる",
|
mcpToolCloseSession: "クエリセッションを閉じる",
|
||||||
mcpToolExecuteRedisCommand: "Redis コマンドを実行",
|
mcpToolExecuteRedisCommand: "Redis コマンドを実行",
|
||||||
mcpToolPeekMessages: "Kafka メッセージを読み取る",
|
mcpToolPeekMessages: "Kafka メッセージを読み取る",
|
||||||
|
|||||||
@@ -7503,6 +7503,9 @@ export default withEnglishFallback({
|
|||||||
mcpToolExecuteQuery: "SQL / Mongo 명령 실행",
|
mcpToolExecuteQuery: "SQL / Mongo 명령 실행",
|
||||||
mcpToolExecuteBatch: "SQL 배치 실행",
|
mcpToolExecuteBatch: "SQL 배치 실행",
|
||||||
mcpToolOpenSession: "쿼리 세션 열기",
|
mcpToolOpenSession: "쿼리 세션 열기",
|
||||||
|
mcpToolBeginTransaction: "트랜잭션 시작",
|
||||||
|
mcpToolCommitTransaction: "트랜잭션 커밋",
|
||||||
|
mcpToolRollbackTransaction: "트랜잭션 롤백",
|
||||||
mcpToolCloseSession: "쿼리 세션 닫기",
|
mcpToolCloseSession: "쿼리 세션 닫기",
|
||||||
mcpToolExecuteRedisCommand: "Redis 명령 실행",
|
mcpToolExecuteRedisCommand: "Redis 명령 실행",
|
||||||
mcpToolPeekMessages: "Kafka 메시지 읽기",
|
mcpToolPeekMessages: "Kafka 메시지 읽기",
|
||||||
|
|||||||
@@ -7735,6 +7735,9 @@ export default withEnglishFallback({
|
|||||||
mcpToolExecuteQuery: "Executar SQL / comandos Mongo",
|
mcpToolExecuteQuery: "Executar SQL / comandos Mongo",
|
||||||
mcpToolExecuteBatch: "Executar lote SQL",
|
mcpToolExecuteBatch: "Executar lote SQL",
|
||||||
mcpToolOpenSession: "Abrir sessão de consulta",
|
mcpToolOpenSession: "Abrir sessão de consulta",
|
||||||
|
mcpToolBeginTransaction: "Iniciar transação",
|
||||||
|
mcpToolCommitTransaction: "Confirmar transação",
|
||||||
|
mcpToolRollbackTransaction: "Reverter transação",
|
||||||
mcpToolCloseSession: "Fechar sessão de consulta",
|
mcpToolCloseSession: "Fechar sessão de consulta",
|
||||||
mcpToolExecuteRedisCommand: "Executar comando Redis",
|
mcpToolExecuteRedisCommand: "Executar comando Redis",
|
||||||
mcpToolPeekMessages: "Ler mensagens Kafka",
|
mcpToolPeekMessages: "Ler mensagens Kafka",
|
||||||
|
|||||||
@@ -7872,6 +7872,9 @@ export default withEnglishFallback({
|
|||||||
mcpToolExecuteQuery: "SQL / Mongo komutlarını çalıştır",
|
mcpToolExecuteQuery: "SQL / Mongo komutlarını çalıştır",
|
||||||
mcpToolExecuteBatch: "SQL toplu işlemini çalıştır",
|
mcpToolExecuteBatch: "SQL toplu işlemini çalıştır",
|
||||||
mcpToolOpenSession: "Sorgu oturumu aç",
|
mcpToolOpenSession: "Sorgu oturumu aç",
|
||||||
|
mcpToolBeginTransaction: "İşlemi başlat",
|
||||||
|
mcpToolCommitTransaction: "İşlemi onayla",
|
||||||
|
mcpToolRollbackTransaction: "İşlemi geri al",
|
||||||
mcpToolCloseSession: "Sorgu oturumunu kapat",
|
mcpToolCloseSession: "Sorgu oturumunu kapat",
|
||||||
mcpToolExecuteRedisCommand: "Redis komutunu çalıştır",
|
mcpToolExecuteRedisCommand: "Redis komutunu çalıştır",
|
||||||
mcpToolPeekMessages: "Kafka mesajlarını oku",
|
mcpToolPeekMessages: "Kafka mesajlarını oku",
|
||||||
|
|||||||
@@ -8215,6 +8215,9 @@ export default withEnglishFallback({
|
|||||||
mcpToolExecuteQuery: "执行 SQL / Mongo 命令",
|
mcpToolExecuteQuery: "执行 SQL / Mongo 命令",
|
||||||
mcpToolExecuteBatch: "批量执行 SQL",
|
mcpToolExecuteBatch: "批量执行 SQL",
|
||||||
mcpToolOpenSession: "打开查询会话",
|
mcpToolOpenSession: "打开查询会话",
|
||||||
|
mcpToolBeginTransaction: "开始事务",
|
||||||
|
mcpToolCommitTransaction: "提交事务",
|
||||||
|
mcpToolRollbackTransaction: "回滚事务",
|
||||||
mcpToolCloseSession: "关闭查询会话",
|
mcpToolCloseSession: "关闭查询会话",
|
||||||
mcpToolExecuteRedisCommand: "执行 Redis 命令",
|
mcpToolExecuteRedisCommand: "执行 Redis 命令",
|
||||||
mcpToolPeekMessages: "读取 Kafka 消息",
|
mcpToolPeekMessages: "读取 Kafka 消息",
|
||||||
|
|||||||
@@ -7104,6 +7104,9 @@ export default withEnglishFallback({
|
|||||||
mcpToolExecuteQuery: "執行 SQL / Mongo 命令",
|
mcpToolExecuteQuery: "執行 SQL / Mongo 命令",
|
||||||
mcpToolExecuteBatch: "批次執行 SQL",
|
mcpToolExecuteBatch: "批次執行 SQL",
|
||||||
mcpToolOpenSession: "開啟查詢工作階段",
|
mcpToolOpenSession: "開啟查詢工作階段",
|
||||||
|
mcpToolBeginTransaction: "開始交易",
|
||||||
|
mcpToolCommitTransaction: "提交交易",
|
||||||
|
mcpToolRollbackTransaction: "回滾交易",
|
||||||
mcpToolCloseSession: "關閉查詢工作階段",
|
mcpToolCloseSession: "關閉查詢工作階段",
|
||||||
mcpToolExecuteRedisCommand: "執行 Redis 命令",
|
mcpToolExecuteRedisCommand: "執行 Redis 命令",
|
||||||
mcpToolPeekMessages: "讀取 Kafka 訊息",
|
mcpToolPeekMessages: "讀取 Kafka 訊息",
|
||||||
|
|||||||
@@ -32,6 +32,9 @@ export const MCP_TOOL_OPTIONS = [
|
|||||||
{ name: "dbx_execute_query", labelKey: "settings.mcpToolExecuteQuery" },
|
{ name: "dbx_execute_query", labelKey: "settings.mcpToolExecuteQuery" },
|
||||||
{ name: "dbx_execute_batch", labelKey: "settings.mcpToolExecuteBatch" },
|
{ name: "dbx_execute_batch", labelKey: "settings.mcpToolExecuteBatch" },
|
||||||
{ name: "dbx_open_session", labelKey: "settings.mcpToolOpenSession" },
|
{ name: "dbx_open_session", labelKey: "settings.mcpToolOpenSession" },
|
||||||
|
{ name: "dbx_begin_transaction", labelKey: "settings.mcpToolBeginTransaction" },
|
||||||
|
{ name: "dbx_commit_transaction", labelKey: "settings.mcpToolCommitTransaction" },
|
||||||
|
{ name: "dbx_rollback_transaction", labelKey: "settings.mcpToolRollbackTransaction" },
|
||||||
{ name: "dbx_close_session", labelKey: "settings.mcpToolCloseSession" },
|
{ name: "dbx_close_session", labelKey: "settings.mcpToolCloseSession" },
|
||||||
{ name: "dbx_execute_redis_command", labelKey: "settings.mcpToolExecuteRedisCommand" },
|
{ name: "dbx_execute_redis_command", labelKey: "settings.mcpToolExecuteRedisCommand" },
|
||||||
{ name: "dbx_peek_messages", labelKey: "settings.mcpToolPeekMessages" },
|
{ name: "dbx_peek_messages", labelKey: "settings.mcpToolPeekMessages" },
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
|
|||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
use tokio::sync::{watch, Mutex, OwnedSemaphorePermit, RwLock, Semaphore};
|
use tokio::sync::{watch, Mutex, OwnedSemaphorePermit, RwLock, Semaphore};
|
||||||
|
use tokio_util::sync::CancellationToken;
|
||||||
|
|
||||||
use mysql_async::prelude::Queryable;
|
use mysql_async::prelude::Queryable;
|
||||||
use mysql_async::Row as MysqlRow;
|
use mysql_async::Row as MysqlRow;
|
||||||
@@ -337,12 +338,36 @@ macro_rules! agent_connection_pool_database_type {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct ConnectionLifecycleSnapshot {
|
||||||
|
generation: u64,
|
||||||
|
cancellation: CancellationToken,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ConnectionLifecycleSnapshot {
|
||||||
|
pub fn cancellation(&self) -> &CancellationToken {
|
||||||
|
&self.cancellation
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct ConnectionLifecycle {
|
||||||
|
generation: u64,
|
||||||
|
cancellation: CancellationToken,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct SharedResourceBudget {
|
||||||
|
capacity: usize,
|
||||||
|
semaphore: Arc<Semaphore>,
|
||||||
|
}
|
||||||
|
|
||||||
pub struct AppState {
|
pub struct AppState {
|
||||||
connections: Arc<RwLock<ConnectionPoolRegistry>>,
|
connections: Arc<RwLock<ConnectionPoolRegistry>>,
|
||||||
task_supervisor: TaskSupervisor,
|
task_supervisor: TaskSupervisor,
|
||||||
pool_activity: Arc<RwLock<HashMap<String, PoolActivity>>>,
|
pool_activity: Arc<RwLock<HashMap<String, PoolActivity>>>,
|
||||||
draining_pools: Arc<std::sync::Mutex<HashMap<String, watch::Sender<bool>>>>,
|
draining_pools: Arc<std::sync::Mutex<HashMap<String, watch::Sender<bool>>>>,
|
||||||
connection_attempts: RwLock<HashMap<String, ConnectionAttemptState>>,
|
connection_attempts: RwLock<HashMap<String, ConnectionAttemptState>>,
|
||||||
|
connection_lifecycles: std::sync::Mutex<HashMap<String, ConnectionLifecycle>>,
|
||||||
|
shared_resource_budgets: std::sync::Mutex<HashMap<String, SharedResourceBudget>>,
|
||||||
pub configs: RwLock<HashMap<String, ConnectionConfig>>,
|
pub configs: RwLock<HashMap<String, ConnectionConfig>>,
|
||||||
pub running_queries: RunningQueries,
|
pub running_queries: RunningQueries,
|
||||||
pub tunnels: TunnelManager,
|
pub tunnels: TunnelManager,
|
||||||
@@ -1244,6 +1269,68 @@ fn mysql_metadata_fallback_url(
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl AppState {
|
impl AppState {
|
||||||
|
pub fn shared_resource_budget(&self, name: &str, capacity: usize) -> Result<Arc<Semaphore>, String> {
|
||||||
|
if capacity == 0 {
|
||||||
|
return Err("Shared resource budget capacity must be greater than zero".to_string());
|
||||||
|
}
|
||||||
|
let mut budgets = self.shared_resource_budgets.lock().unwrap_or_else(|error| error.into_inner());
|
||||||
|
if let Some(budget) = budgets.get(name) {
|
||||||
|
if budget.capacity != capacity {
|
||||||
|
return Err(format!(
|
||||||
|
"Shared resource budget {name:?} already has capacity {}, not {capacity}",
|
||||||
|
budget.capacity
|
||||||
|
));
|
||||||
|
}
|
||||||
|
return Ok(budget.semaphore.clone());
|
||||||
|
}
|
||||||
|
let semaphore = Arc::new(Semaphore::new(capacity));
|
||||||
|
budgets.insert(name.to_string(), SharedResourceBudget { capacity, semaphore: semaphore.clone() });
|
||||||
|
Ok(semaphore)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn connection_lifecycle_snapshot(&self, connection_id: &str) -> ConnectionLifecycleSnapshot {
|
||||||
|
let mut lifecycles = self.connection_lifecycles.lock().unwrap_or_else(|error| error.into_inner());
|
||||||
|
let lifecycle = lifecycles
|
||||||
|
.entry(connection_id.to_string())
|
||||||
|
.or_insert_with(|| ConnectionLifecycle { generation: 0, cancellation: CancellationToken::new() });
|
||||||
|
ConnectionLifecycleSnapshot { generation: lifecycle.generation, cancellation: lifecycle.cancellation.clone() }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn connection_lifecycle_is_current(&self, connection_id: &str, snapshot: &ConnectionLifecycleSnapshot) -> bool {
|
||||||
|
self.connection_lifecycles.lock().unwrap_or_else(|error| error.into_inner()).get(connection_id).is_some_and(
|
||||||
|
|lifecycle| lifecycle.generation == snapshot.generation && !snapshot.cancellation.is_cancelled(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn invalidate_connection_lifecycle(&self, connection_id: &str) {
|
||||||
|
let previous = {
|
||||||
|
let mut lifecycles = self.connection_lifecycles.lock().unwrap_or_else(|error| error.into_inner());
|
||||||
|
let lifecycle = lifecycles
|
||||||
|
.entry(connection_id.to_string())
|
||||||
|
.or_insert_with(|| ConnectionLifecycle { generation: 0, cancellation: CancellationToken::new() });
|
||||||
|
let previous = std::mem::replace(&mut lifecycle.cancellation, CancellationToken::new());
|
||||||
|
lifecycle.generation = lifecycle.generation.wrapping_add(1);
|
||||||
|
previous
|
||||||
|
};
|
||||||
|
previous.cancel();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn invalidate_all_connection_lifecycles(&self) {
|
||||||
|
let previous = {
|
||||||
|
let mut lifecycles = self.connection_lifecycles.lock().unwrap_or_else(|error| error.into_inner());
|
||||||
|
lifecycles
|
||||||
|
.values_mut()
|
||||||
|
.map(|lifecycle| {
|
||||||
|
lifecycle.generation = lifecycle.generation.wrapping_add(1);
|
||||||
|
std::mem::replace(&mut lifecycle.cancellation, CancellationToken::new())
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
};
|
||||||
|
for cancellation in previous {
|
||||||
|
cancellation.cancel();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Return an owned pool handle. The registry read lock is released before
|
/// Return an owned pool handle. The registry read lock is released before
|
||||||
/// the caller can perform any asynchronous database operation.
|
/// the caller can perform any asynchronous database operation.
|
||||||
pub async fn pool_handle(&self, pool_key: &str) -> Option<PoolKind> {
|
pub async fn pool_handle(&self, pool_key: &str) -> Option<PoolKind> {
|
||||||
@@ -1398,6 +1485,8 @@ impl AppState {
|
|||||||
pool_activity: Arc::new(RwLock::new(HashMap::new())),
|
pool_activity: Arc::new(RwLock::new(HashMap::new())),
|
||||||
draining_pools: Arc::new(std::sync::Mutex::new(HashMap::new())),
|
draining_pools: Arc::new(std::sync::Mutex::new(HashMap::new())),
|
||||||
connection_attempts: RwLock::new(HashMap::new()),
|
connection_attempts: RwLock::new(HashMap::new()),
|
||||||
|
connection_lifecycles: std::sync::Mutex::new(HashMap::new()),
|
||||||
|
shared_resource_budgets: std::sync::Mutex::new(HashMap::new()),
|
||||||
configs: RwLock::new(HashMap::new()),
|
configs: RwLock::new(HashMap::new()),
|
||||||
running_queries: RunningQueries::default(),
|
running_queries: RunningQueries::default(),
|
||||||
tunnels: TunnelManager::new(data_dir),
|
tunnels: TunnelManager::new(data_dir),
|
||||||
@@ -2064,6 +2153,7 @@ impl AppState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn shutdown(&self, deadline: Duration) {
|
pub async fn shutdown(&self, deadline: Duration) {
|
||||||
|
self.invalidate_all_connection_lifecycles();
|
||||||
self.running_queries.cancel_all();
|
self.running_queries.cancel_all();
|
||||||
let removed_pools = self.drain_all_connection_pools().await;
|
let removed_pools = self.drain_all_connection_pools().await;
|
||||||
self.transaction_sessions.write().await.clear();
|
self.transaction_sessions.write().await.clear();
|
||||||
@@ -5251,6 +5341,7 @@ impl AppState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn remove_connection_pools(&self, connection_id: &str) {
|
pub async fn remove_connection_pools(&self, connection_id: &str) {
|
||||||
|
self.invalidate_connection_lifecycle(connection_id);
|
||||||
self.rollback_manual_transaction_sessions(connection_id).await;
|
self.rollback_manual_transaction_sessions(connection_id).await;
|
||||||
let removed = self.drain_connection_pools(connection_id).await;
|
let removed = self.drain_connection_pools(connection_id).await;
|
||||||
self.clear_metadata_gates_for_connection(connection_id).await;
|
self.clear_metadata_gates_for_connection(connection_id).await;
|
||||||
@@ -5266,6 +5357,7 @@ impl AppState {
|
|||||||
/// user disconnect still goes through remove_connection_pools* and does
|
/// user disconnect still goes through remove_connection_pools* and does
|
||||||
/// send connection/disconnect.
|
/// send connection/disconnect.
|
||||||
pub async fn drop_connection_pools_without_close(&self, connection_id: &str) {
|
pub async fn drop_connection_pools_without_close(&self, connection_id: &str) {
|
||||||
|
self.invalidate_connection_lifecycle(connection_id);
|
||||||
self.rollback_manual_transaction_sessions(connection_id).await;
|
self.rollback_manual_transaction_sessions(connection_id).await;
|
||||||
let removed = self.drain_connection_pools(connection_id).await;
|
let removed = self.drain_connection_pools(connection_id).await;
|
||||||
self.clear_metadata_gates_for_connection(connection_id).await;
|
self.clear_metadata_gates_for_connection(connection_id).await;
|
||||||
@@ -5273,6 +5365,7 @@ impl AppState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn remove_connection_pools_detached(&self, connection_id: &str) {
|
pub async fn remove_connection_pools_detached(&self, connection_id: &str) {
|
||||||
|
self.invalidate_connection_lifecycle(connection_id);
|
||||||
self.rollback_manual_transaction_sessions(connection_id).await;
|
self.rollback_manual_transaction_sessions(connection_id).await;
|
||||||
let removed = self.drain_connection_pools(connection_id).await;
|
let removed = self.drain_connection_pools(connection_id).await;
|
||||||
self.clear_metadata_gates_for_connection(connection_id).await;
|
self.clear_metadata_gates_for_connection(connection_id).await;
|
||||||
@@ -7387,6 +7480,64 @@ mod tests {
|
|||||||
(AppState::new(storage), dir)
|
(AppState::new(storage), dir)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn connection_lifecycle_invalidation_rotates_generation_and_cancels_previous_snapshot() {
|
||||||
|
let (state, dir) = test_app_state().await;
|
||||||
|
let first = state.connection_lifecycle_snapshot("conn");
|
||||||
|
assert!(state.connection_lifecycle_is_current("conn", &first));
|
||||||
|
|
||||||
|
state.invalidate_connection_lifecycle("conn");
|
||||||
|
|
||||||
|
tokio::time::timeout(Duration::from_millis(100), first.cancellation().cancelled())
|
||||||
|
.await
|
||||||
|
.expect("previous lifecycle must be cancelled");
|
||||||
|
assert!(!state.connection_lifecycle_is_current("conn", &first));
|
||||||
|
let second = state.connection_lifecycle_snapshot("conn");
|
||||||
|
assert!(state.connection_lifecycle_is_current("conn", &second));
|
||||||
|
assert!(!second.cancellation().is_cancelled());
|
||||||
|
let _ = std::fs::remove_dir_all(dir);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn every_connection_pool_removal_boundary_invalidates_lifecycle_snapshots() {
|
||||||
|
let (state, dir) = test_app_state().await;
|
||||||
|
|
||||||
|
let removed = state.connection_lifecycle_snapshot("removed");
|
||||||
|
state.remove_connection_pools("removed").await;
|
||||||
|
assert!(removed.cancellation().is_cancelled());
|
||||||
|
|
||||||
|
let dropped = state.connection_lifecycle_snapshot("dropped");
|
||||||
|
state.drop_connection_pools_without_close("dropped").await;
|
||||||
|
assert!(dropped.cancellation().is_cancelled());
|
||||||
|
|
||||||
|
let detached = state.connection_lifecycle_snapshot("detached");
|
||||||
|
state.remove_connection_pools_detached("detached").await;
|
||||||
|
assert!(detached.cancellation().is_cancelled());
|
||||||
|
|
||||||
|
let shutdown = state.connection_lifecycle_snapshot("shutdown");
|
||||||
|
state.shutdown(Duration::from_millis(100)).await;
|
||||||
|
assert!(shutdown.cancellation().is_cancelled());
|
||||||
|
let _ = std::fs::remove_dir_all(dir);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn named_resource_budget_is_shared_across_app_state_views() {
|
||||||
|
let (state, dir) = test_app_state().await;
|
||||||
|
let first = state.shared_resource_budget("fixed-owner", 2).unwrap();
|
||||||
|
let second = state.shared_resource_budget("fixed-owner", 2).unwrap();
|
||||||
|
assert!(Arc::ptr_eq(&first, &second));
|
||||||
|
|
||||||
|
let first_permit = first.clone().try_acquire_owned().unwrap();
|
||||||
|
let second_permit = second.clone().try_acquire_owned().unwrap();
|
||||||
|
assert!(first.clone().try_acquire_owned().is_err());
|
||||||
|
drop(first_permit);
|
||||||
|
assert!(second.clone().try_acquire_owned().is_ok());
|
||||||
|
drop(second_permit);
|
||||||
|
|
||||||
|
assert!(state.shared_resource_budget("fixed-owner", 3).is_err());
|
||||||
|
let _ = std::fs::remove_dir_all(dir);
|
||||||
|
}
|
||||||
|
|
||||||
fn agent_pool_stub() -> PoolKind {
|
fn agent_pool_stub() -> PoolKind {
|
||||||
PoolKind::agent(crate::db::agent_driver::AgentDriverClient::test_stub())
|
PoolKind::agent(crate::db::agent_driver::AgentDriverClient::test_stub())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use chrono::{NaiveDate, NaiveDateTime, NaiveTime};
|
use chrono::{NaiveDate, NaiveDateTime, NaiveTime};
|
||||||
use futures::StreamExt;
|
use futures::StreamExt;
|
||||||
use mysql_async::consts::{ColumnFlags, ColumnType};
|
use mysql_async::consts::{ColumnFlags, ColumnType, StatusFlags};
|
||||||
use mysql_async::prelude::*;
|
use mysql_async::prelude::*;
|
||||||
use percent_encoding::percent_decode_str;
|
use percent_encoding::percent_decode_str;
|
||||||
use rust_decimal::Decimal;
|
use rust_decimal::Decimal;
|
||||||
@@ -191,6 +191,47 @@ pub struct MySqlQueryResult {
|
|||||||
pub large_value_cells: Vec<LargeValueCell>,
|
pub large_value_cells: Vec<LargeValueCell>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Result of one MCP fixed-session statement executed through MySQL's text
|
||||||
|
/// protocol. The transaction bit comes from the final server OK/EOF packet,
|
||||||
|
/// never from SQL text or client-side state inference.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct MySqlTransactionExecution {
|
||||||
|
pub result: QueryResult,
|
||||||
|
pub in_transaction: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Error classification retained by the fixed-session owner. Server errors
|
||||||
|
/// are recoverable protocol responses; every other driver error means the
|
||||||
|
/// connection state cannot be trusted or replayed.
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
|
pub enum MySqlTransactionError {
|
||||||
|
Server { code: u16, message: String, state: String },
|
||||||
|
Transport(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
fn transaction_error_from_mysql_error(error: mysql_async::Error) -> MySqlTransactionError {
|
||||||
|
match error {
|
||||||
|
mysql_async::Error::Server(error) => {
|
||||||
|
MySqlTransactionError::Server { code: error.code, message: error.message, state: error.state }
|
||||||
|
}
|
||||||
|
error => MySqlTransactionError::Transport(error.to_string()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn transaction_status_from_last_ok(conn: &mysql_async::Conn) -> Result<bool, MySqlTransactionError> {
|
||||||
|
conn.last_ok_packet()
|
||||||
|
.map(|packet| packet.status_flags().contains(StatusFlags::SERVER_STATUS_IN_TRANS))
|
||||||
|
.ok_or_else(|| MySqlTransactionError::Transport("MySQL did not return a final status packet".to_string()))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Read transaction state after a recoverable server ERR packet. mysql_async
|
||||||
|
/// intentionally clears the cached OK packet for ERR, so COM_PING is required
|
||||||
|
/// to obtain fresh status from the same physical connection.
|
||||||
|
pub async fn ping_transaction_status_on_conn(conn: &mut mysql_async::Conn) -> Result<bool, MySqlTransactionError> {
|
||||||
|
conn.ping().await.map_err(transaction_error_from_mysql_error)?;
|
||||||
|
transaction_status_from_last_ok(conn)
|
||||||
|
}
|
||||||
|
|
||||||
impl MySqlQueryResult {
|
impl MySqlQueryResult {
|
||||||
fn exact(result: QueryResult) -> Self {
|
fn exact(result: QueryResult) -> Self {
|
||||||
Self { result, large_value_cells: Vec::new() }
|
Self { result, large_value_cells: Vec::new() }
|
||||||
@@ -5274,6 +5315,79 @@ pub async fn execute_query_on_conn_with_max_rows(
|
|||||||
.map(|result| result.result)
|
.map(|result| result.result)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Execute exactly one policy-approved MCP transaction statement using
|
||||||
|
/// COM_QUERY/text protocol. This path never retries, rewrites or replays user
|
||||||
|
/// SQL. It fully drains the result before exposing the final server status.
|
||||||
|
pub async fn execute_transaction_statement_on_conn(
|
||||||
|
conn: &mut mysql_async::Conn,
|
||||||
|
sql: &str,
|
||||||
|
max_rows: Option<usize>,
|
||||||
|
) -> Result<MySqlTransactionExecution, MySqlTransactionError> {
|
||||||
|
let started_at = Instant::now();
|
||||||
|
let row_limit = query_result_row_limit(max_rows);
|
||||||
|
let mut query = conn.query_iter(sql).await.map_err(transaction_error_from_mysql_error)?;
|
||||||
|
let columns: Vec<String> = query.columns_ref().iter().map(|column| column.name_str().to_string()).collect();
|
||||||
|
let column_types: Vec<String> = query.columns_ref().iter().map(mysql_column_type_name).collect();
|
||||||
|
|
||||||
|
let result = if columns.is_empty() {
|
||||||
|
let affected_rows = query.affected_rows();
|
||||||
|
let warnings = query.warnings();
|
||||||
|
let info = query.info().into_owned();
|
||||||
|
query.drop_result().await.map_err(transaction_error_from_mysql_error)?;
|
||||||
|
let messages = collect_mysql_server_messages(conn, warnings, &info).await;
|
||||||
|
QueryResult {
|
||||||
|
columns,
|
||||||
|
column_types,
|
||||||
|
column_sortables: Vec::new(),
|
||||||
|
spatial_columns: Vec::new(),
|
||||||
|
spatial_values: Vec::new(),
|
||||||
|
rows: Vec::new(),
|
||||||
|
affected_rows,
|
||||||
|
execution_time_ms: started_at.elapsed().as_millis(),
|
||||||
|
truncated: false,
|
||||||
|
session_id: None,
|
||||||
|
has_more: false,
|
||||||
|
elasticsearch_raw_body: None,
|
||||||
|
messages,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
let mut spatial_columns = mysql_spatial_column_builder(query.columns_ref());
|
||||||
|
let mut rows = Vec::with_capacity(row_limit.min(128));
|
||||||
|
let mut spatial_values = Vec::new();
|
||||||
|
let mut truncated = false;
|
||||||
|
while let Some(row) = query.next().await.map_err(transaction_error_from_mysql_error)? {
|
||||||
|
if rows.len() < row_limit {
|
||||||
|
let (values, srids) = mysql_row_to_json_with_srids(&row, &mut spatial_columns);
|
||||||
|
spatial_values.push(srids);
|
||||||
|
rows.push(values);
|
||||||
|
} else {
|
||||||
|
truncated = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// `next` stops at the first result-set boundary. Drain every remaining
|
||||||
|
// result set before reading the final status or allowing another
|
||||||
|
// operation on this physical connection.
|
||||||
|
query.drop_result().await.map_err(transaction_error_from_mysql_error)?;
|
||||||
|
QueryResult {
|
||||||
|
columns,
|
||||||
|
column_types,
|
||||||
|
column_sortables: Vec::new(),
|
||||||
|
spatial_columns: spatial_columns.finish(),
|
||||||
|
spatial_values,
|
||||||
|
rows,
|
||||||
|
affected_rows: 0,
|
||||||
|
execution_time_ms: started_at.elapsed().as_millis(),
|
||||||
|
truncated,
|
||||||
|
session_id: None,
|
||||||
|
has_more: false,
|
||||||
|
elasticsearch_raw_body: None,
|
||||||
|
messages: Vec::new(),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let in_transaction = transaction_status_from_last_ok(conn)?;
|
||||||
|
Ok(MySqlTransactionExecution { result, in_transaction })
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn execute_query_on_conn_with_limits(
|
pub async fn execute_query_on_conn_with_limits(
|
||||||
conn: &mut mysql_async::Conn,
|
conn: &mut mysql_async::Conn,
|
||||||
sql: &str,
|
sql: &str,
|
||||||
@@ -6060,6 +6174,24 @@ mod tests {
|
|||||||
use mysql_async::{consts::ColumnType, Column, Value};
|
use mysql_async::{consts::ColumnType, Column, Value};
|
||||||
use mysql_common::row::new_row;
|
use mysql_common::row::new_row;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn transaction_error_preserves_server_code_without_treating_it_as_transport_failure() {
|
||||||
|
let error = mysql_async::Error::Server(mysql_async::ServerError {
|
||||||
|
code: 1062,
|
||||||
|
message: "Duplicate entry".to_string(),
|
||||||
|
state: "23000".to_string(),
|
||||||
|
});
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
transaction_error_from_mysql_error(error),
|
||||||
|
MySqlTransactionError::Server {
|
||||||
|
code: 1062,
|
||||||
|
message: "Duplicate entry".to_string(),
|
||||||
|
state: "23000".to_string(),
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
fn mysql_test_row(values: Vec<Value>) -> mysql_async::Row {
|
fn mysql_test_row(values: Vec<Value>) -> mysql_async::Row {
|
||||||
let columns = values
|
let columns = values
|
||||||
.iter()
|
.iter()
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ default = ["duckdb-sidecar", "mq-admin", "sqlite-bundled"]
|
|||||||
duckdb-sidecar = ["dbx-core/duckdb-sidecar"]
|
duckdb-sidecar = ["dbx-core/duckdb-sidecar"]
|
||||||
mq-admin = ["dbx-core/mq-admin"]
|
mq-admin = ["dbx-core/mq-admin"]
|
||||||
sqlite-bundled = ["dbx-core/sqlite-bundled"]
|
sqlite-bundled = ["dbx-core/sqlite-bundled"]
|
||||||
|
transaction-test-hooks = []
|
||||||
|
|
||||||
[[bin]]
|
[[bin]]
|
||||||
name = "dbx-mcp"
|
name = "dbx-mcp"
|
||||||
@@ -19,6 +20,7 @@ async-trait = "0.1"
|
|||||||
dbx-core = { path = "../dbx-core", default-features = false }
|
dbx-core = { path = "../dbx-core", default-features = false }
|
||||||
dirs = "6"
|
dirs = "6"
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
|
mysql_async = { version = "0.37", default-features = false, features = ["default-rustls", "client_ed25519", "chrono", "rust_decimal"] }
|
||||||
axum = "0.8"
|
axum = "0.8"
|
||||||
chrono = "0.4"
|
chrono = "0.4"
|
||||||
rmcp = { version = "2.2.0", features = ["client", "transport-io", "transport-streamable-http-server"] }
|
rmcp = { version = "2.2.0", features = ["client", "transport-io", "transport-streamable-http-server"] }
|
||||||
@@ -27,6 +29,7 @@ rustls = { version = "0.23", features = ["aws-lc-rs"] }
|
|||||||
schemars = "1"
|
schemars = "1"
|
||||||
serde = { version = "1", features = ["derive"] }
|
serde = { version = "1", features = ["derive"] }
|
||||||
serde_json = "1"
|
serde_json = "1"
|
||||||
|
sqlparser = { version = "0.62", features = ["visitor"] }
|
||||||
tokio = { version = "1", features = ["fs", "macros", "net", "rt-multi-thread", "signal"] }
|
tokio = { version = "1", features = ["fs", "macros", "net", "rt-multi-thread", "signal"] }
|
||||||
tokio-util = "0.7"
|
tokio-util = "0.7"
|
||||||
tower-http = { version = "0.6", features = ["cors"] }
|
tower-http = { version = "0.6", features = ["cors"] }
|
||||||
@@ -34,6 +37,7 @@ uuid = { version = "1", features = ["v4"] }
|
|||||||
url = "2"
|
url = "2"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
|
rmcp = { version = "2.2.0", features = ["transport-streamable-http-client-reqwest"] }
|
||||||
rustls-pemfile = "2.2"
|
rustls-pemfile = "2.2"
|
||||||
tempfile = "3"
|
tempfile = "3"
|
||||||
tokio = { version = "1", features = ["net", "io-util"] }
|
tokio = { version = "1", features = ["net", "io-util"] }
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ use async_trait::async_trait;
|
|||||||
use dbx_core::{
|
use dbx_core::{
|
||||||
agent_events::{ToolCall, ToolResult},
|
agent_events::{ToolCall, ToolResult},
|
||||||
agent_tools::{self, format_query_result_as_text, AgentSqlPermissions, QueryCellWindow},
|
agent_tools::{self, format_query_result_as_text, AgentSqlPermissions, QueryCellWindow},
|
||||||
connection::{connection_configs_pool_equivalent, AppState},
|
connection::{connection_configs_pool_equivalent, AppState, ConnectionLifecycleSnapshot},
|
||||||
db::{mongo_driver::MongoIndexSpec, redis_driver::RedisCommandResult, ColumnInfo, TableInfo},
|
db::{mongo_driver::MongoIndexSpec, redis_driver::RedisCommandResult, ColumnInfo, TableInfo},
|
||||||
history::HistoryEntry,
|
history::HistoryEntry,
|
||||||
mcp_policy::{connection_group_paths, McpConnectionGroupPath},
|
mcp_policy::{connection_group_paths, McpConnectionGroupPath},
|
||||||
@@ -21,7 +21,10 @@ use tokio::sync::Mutex;
|
|||||||
|
|
||||||
use reqwest::header::{HeaderMap, HeaderName, HeaderValue};
|
use reqwest::header::{HeaderMap, HeaderName, HeaderValue};
|
||||||
|
|
||||||
use crate::mongo::MongoCommand;
|
use crate::{
|
||||||
|
mongo::MongoCommand,
|
||||||
|
transaction::{MysqlTransactionIo, TransactionOwner, TransactionOwnerConfig, TransactionOwnerRegistry},
|
||||||
|
};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
|
#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
|
||||||
pub struct ConnectionSummary {
|
pub struct ConnectionSummary {
|
||||||
@@ -119,12 +122,34 @@ pub struct BatchStatementResult {
|
|||||||
/// false.
|
/// false.
|
||||||
#[serde(skip_serializing_if = "is_false")]
|
#[serde(skip_serializing_if = "is_false")]
|
||||||
pub merged: bool,
|
pub merged: bool,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub transaction_state: Option<crate::transaction::TransactionState>,
|
||||||
|
#[serde(skip_serializing_if = "Option::is_none")]
|
||||||
|
pub transaction_outcome: Option<crate::transaction::TransactionOutcome>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_false(value: &bool) -> bool {
|
fn is_false(value: &bool) -> bool {
|
||||||
!*value
|
!*value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn native_mysql_transaction_connection(connection: &ConnectionConfig) -> bool {
|
||||||
|
connection.db_type == DatabaseType::Mysql
|
||||||
|
&& connection.driver_profile.as_deref().is_none_or(|profile| {
|
||||||
|
let profile = profile.trim();
|
||||||
|
profile.is_empty() || profile.eq_ignore_ascii_case("mysql")
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn transaction_operation_timeout(
|
||||||
|
connection: &ConnectionConfig,
|
||||||
|
configured: std::time::Duration,
|
||||||
|
) -> std::time::Duration {
|
||||||
|
match connection.effective_query_timeout_secs() {
|
||||||
|
0 => configured,
|
||||||
|
seconds => std::time::Duration::from_secs(seconds),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<dbx_core::query::ExecuteMultiResult> for BatchStatementResult {
|
impl From<dbx_core::query::ExecuteMultiResult> for BatchStatementResult {
|
||||||
fn from(result: dbx_core::query::ExecuteMultiResult) -> Self {
|
fn from(result: dbx_core::query::ExecuteMultiResult) -> Self {
|
||||||
let error_message = result.error.as_ref().and_then(|error| error.detail().map(str::to_owned)).or_else(|| {
|
let error_message = result.error.as_ref().and_then(|error| error.detail().map(str::to_owned)).or_else(|| {
|
||||||
@@ -136,6 +161,8 @@ impl From<dbx_core::query::ExecuteMultiResult> for BatchStatementResult {
|
|||||||
statement_index: result.statement_index,
|
statement_index: result.statement_index,
|
||||||
error_message,
|
error_message,
|
||||||
merged: false,
|
merged: false,
|
||||||
|
transaction_state: None,
|
||||||
|
transaction_outcome: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -160,7 +187,15 @@ fn batch_statement_result_from_json(value: &Value) -> Result<BatchStatementResul
|
|||||||
.and_then(Value::as_str)
|
.and_then(Value::as_str)
|
||||||
.map(str::to_owned)
|
.map(str::to_owned)
|
||||||
.or_else(|| value.pointer("/rows/0/0").and_then(Value::as_str).map(str::to_owned));
|
.or_else(|| value.pointer("/rows/0/0").and_then(Value::as_str).map(str::to_owned));
|
||||||
Ok(BatchStatementResult { result, execution_error, statement_index, error_message, merged: false })
|
Ok(BatchStatementResult {
|
||||||
|
result,
|
||||||
|
execution_error,
|
||||||
|
statement_index,
|
||||||
|
error_message,
|
||||||
|
merged: false,
|
||||||
|
transaction_state: None,
|
||||||
|
transaction_outcome: None,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Wire-level options for a documentation snapshot. Mirrors
|
/// Wire-level options for a documentation snapshot. Mirrors
|
||||||
@@ -242,6 +277,15 @@ pub trait DbxBackend: Send + Sync {
|
|||||||
let _ = (connection, database, sql, max_rows, timeout_secs);
|
let _ = (connection, database, sql, max_rows, timeout_secs);
|
||||||
Err("SQL queries are not supported by this backend.".to_string())
|
Err("SQL queries are not supported by this backend.".to_string())
|
||||||
}
|
}
|
||||||
|
async fn open_transaction_owner(
|
||||||
|
&self,
|
||||||
|
connection: &ConnectionConfig,
|
||||||
|
database: &str,
|
||||||
|
client_session_id: &str,
|
||||||
|
) -> Result<Arc<TransactionOwner>, String> {
|
||||||
|
let _ = (connection, database, client_session_id);
|
||||||
|
Err("TRANSACTION_UNSUPPORTED: fixed-session transactions require a native local MySQL connection.".to_string())
|
||||||
|
}
|
||||||
/// Execute a multi-statement SQL script, returning one result per statement.
|
/// Execute a multi-statement SQL script, returning one result per statement.
|
||||||
///
|
///
|
||||||
/// The script text is split using the database-dialect-aware splitter so
|
/// The script text is split using the database-dialect-aware splitter so
|
||||||
@@ -374,6 +418,8 @@ pub trait DbxBackend: Send + Sync {
|
|||||||
pub struct LocalBackend {
|
pub struct LocalBackend {
|
||||||
state: Arc<AppState>,
|
state: Arc<AppState>,
|
||||||
data_dir: std::path::PathBuf,
|
data_dir: std::path::PathBuf,
|
||||||
|
transaction_owners: Arc<TransactionOwnerRegistry>,
|
||||||
|
transaction_owner_config: TransactionOwnerConfig,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
@@ -391,6 +437,12 @@ pub struct WebBackend {
|
|||||||
connected: Mutex<HashMap<String, ConnectionConfig>>,
|
connected: Mutex<HashMap<String, ConnectionConfig>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Drop for LocalBackend {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
self.transaction_owners.invalidate_all();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Manual impl: the derived one would print `password` (and the session cookie
|
// Manual impl: the derived one would print `password` (and the session cookie
|
||||||
// inside `auth`) in plaintext. Redact credentials and skip lockable state.
|
// inside `auth`) in plaintext. Redact credentials and skip lockable state.
|
||||||
impl std::fmt::Debug for WebBackend {
|
impl std::fmt::Debug for WebBackend {
|
||||||
@@ -580,10 +632,51 @@ impl WebBackend {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl LocalBackend {
|
impl LocalBackend {
|
||||||
|
fn spawn_connection_lifecycle_watcher(
|
||||||
|
&self,
|
||||||
|
lifecycle: ConnectionLifecycleSnapshot,
|
||||||
|
owner: &Arc<TransactionOwner>,
|
||||||
|
) -> tokio::task::JoinHandle<()> {
|
||||||
|
let cancellation = lifecycle.cancellation().clone();
|
||||||
|
let owner_closed = owner.closed_token();
|
||||||
|
let watched_owner = Arc::downgrade(owner);
|
||||||
|
tokio::spawn(async move {
|
||||||
|
tokio::select! {
|
||||||
|
_ = cancellation.cancelled() => {
|
||||||
|
if let Some(owner) = watched_owner.upgrade() {
|
||||||
|
owner.invalidate();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ = owner_closed.cancelled() => {}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn finalize_transaction_owner(
|
||||||
|
&self,
|
||||||
|
connection_id: &str,
|
||||||
|
lifecycle: ConnectionLifecycleSnapshot,
|
||||||
|
owner: Arc<TransactionOwner>,
|
||||||
|
) -> Result<Arc<TransactionOwner>, String> {
|
||||||
|
self.spawn_connection_lifecycle_watcher(lifecycle.clone(), &owner);
|
||||||
|
self.transaction_owners.register(connection_id, &owner);
|
||||||
|
if !self.state.connection_lifecycle_is_current(connection_id, &lifecycle) {
|
||||||
|
owner.invalidate();
|
||||||
|
let _ = owner.close().await;
|
||||||
|
return Err("The connection was removed while its fixed transaction session was opening.".to_string());
|
||||||
|
}
|
||||||
|
Ok(owner)
|
||||||
|
}
|
||||||
|
|
||||||
/// Reuse an already initialized DBX application state, for hosts that
|
/// Reuse an already initialized DBX application state, for hosts that
|
||||||
/// embed MCP alongside their own HTTP server.
|
/// embed MCP alongside their own HTTP server.
|
||||||
pub fn from_app_state(state: Arc<AppState>, data_dir: PathBuf) -> Self {
|
pub fn from_app_state(state: Arc<AppState>, data_dir: PathBuf) -> Self {
|
||||||
Self { state, data_dir }
|
Self {
|
||||||
|
state,
|
||||||
|
data_dir,
|
||||||
|
transaction_owners: Arc::new(TransactionOwnerRegistry::default()),
|
||||||
|
transaction_owner_config: TransactionOwnerConfig::default(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn open(path: &Path) -> Result<Self, String> {
|
pub async fn open(path: &Path) -> Result<Self, String> {
|
||||||
@@ -613,7 +706,18 @@ impl LocalBackend {
|
|||||||
let config_map: HashMap<String, ConnectionConfig> =
|
let config_map: HashMap<String, ConnectionConfig> =
|
||||||
configs.into_iter().map(|config| (config.id.clone(), config)).collect();
|
configs.into_iter().map(|config| (config.id.clone(), config)).collect();
|
||||||
*state.configs.write().await = config_map;
|
*state.configs.write().await = config_map;
|
||||||
Ok(Self { state, data_dir })
|
Ok(Self {
|
||||||
|
state,
|
||||||
|
data_dir,
|
||||||
|
transaction_owners: Arc::new(TransactionOwnerRegistry::default()),
|
||||||
|
transaction_owner_config: TransactionOwnerConfig::default(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "transaction-test-hooks")]
|
||||||
|
pub fn with_transaction_test_config(mut self, config: TransactionOwnerConfig) -> Self {
|
||||||
|
self.transaction_owner_config = config;
|
||||||
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn state(&self) -> &Arc<AppState> {
|
pub fn state(&self) -> &Arc<AppState> {
|
||||||
@@ -711,6 +815,7 @@ impl LocalBackend {
|
|||||||
};
|
};
|
||||||
|
|
||||||
for id in pool_ids_to_drop {
|
for id in pool_ids_to_drop {
|
||||||
|
self.transaction_owners.invalidate_connection(&id);
|
||||||
self.state.remove_connection_pools_detached(&id).await;
|
self.state.remove_connection_pools_detached(&id).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -875,6 +980,57 @@ impl DbxBackend for LocalBackend {
|
|||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn open_transaction_owner(
|
||||||
|
&self,
|
||||||
|
connection: &ConnectionConfig,
|
||||||
|
database: &str,
|
||||||
|
client_session_id: &str,
|
||||||
|
) -> Result<Arc<TransactionOwner>, String> {
|
||||||
|
if !native_mysql_transaction_connection(connection) {
|
||||||
|
return Err("TRANSACTION_UNSUPPORTED: fixed-session transactions require a native local MySQL connection."
|
||||||
|
.to_string());
|
||||||
|
}
|
||||||
|
let lifecycle = self.state.connection_lifecycle_snapshot(&connection.id);
|
||||||
|
let resource_permit =
|
||||||
|
self.state.shared_resource_budget("dbx-mcp-transaction-owners", 32)?.try_acquire_owned().map_err(|_| {
|
||||||
|
"Too many open transaction-enabled MCP sessions (max 32). Close an existing transaction session first."
|
||||||
|
.to_string()
|
||||||
|
})?;
|
||||||
|
let pool_database = (!database.trim().is_empty()).then_some(database);
|
||||||
|
let pool_key =
|
||||||
|
self.state.get_or_create_pool_for_session(&connection.id, pool_database, Some(client_session_id)).await?;
|
||||||
|
let pool = match self.state.pool_handle(&pool_key).await {
|
||||||
|
Some(dbx_core::connection::PoolKind::Mysql(pool, dbx_core::connection::MysqlMode::Normal)) => pool,
|
||||||
|
_ => {
|
||||||
|
let _ = self.state.close_client_session_pool(&connection.id, pool_database, client_session_id).await;
|
||||||
|
return Err(
|
||||||
|
"TRANSACTION_UNSUPPORTED: fixed-session transactions require the native MySQL driver.".to_string()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let conn = match dbx_core::db::mysql::get_conn_with_health_check(&pool).await {
|
||||||
|
Ok(conn) => conn,
|
||||||
|
Err(error) => {
|
||||||
|
let _ = self.state.close_client_session_pool(&connection.id, pool_database, client_session_id).await;
|
||||||
|
return Err(format!("Failed to acquire the fixed MySQL session connection: {error}"));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let mut owner_config = self.transaction_owner_config;
|
||||||
|
owner_config.operation_timeout = transaction_operation_timeout(connection, owner_config.operation_timeout);
|
||||||
|
let owner = TransactionOwner::spawn_with_resource_permit(
|
||||||
|
MysqlTransactionIo::new(
|
||||||
|
conn,
|
||||||
|
self.state.clone(),
|
||||||
|
connection.id.clone(),
|
||||||
|
database.to_string(),
|
||||||
|
client_session_id.to_string(),
|
||||||
|
),
|
||||||
|
owner_config,
|
||||||
|
resource_permit,
|
||||||
|
);
|
||||||
|
self.finalize_transaction_owner(&connection.id, lifecycle, owner).await
|
||||||
|
}
|
||||||
|
|
||||||
async fn execute_query(
|
async fn execute_query(
|
||||||
&self,
|
&self,
|
||||||
connection: &ConnectionConfig,
|
connection: &ConnectionConfig,
|
||||||
@@ -952,6 +1108,7 @@ impl DbxBackend for LocalBackend {
|
|||||||
let removed = self.state.storage.remove_connection_for_mcp(connection_id).await?;
|
let removed = self.state.storage.remove_connection_for_mcp(connection_id).await?;
|
||||||
if removed {
|
if removed {
|
||||||
self.state.configs.write().await.remove(connection_id);
|
self.state.configs.write().await.remove(connection_id);
|
||||||
|
self.transaction_owners.invalidate_connection(connection_id);
|
||||||
self.state.remove_connection_pools_detached(connection_id).await;
|
self.state.remove_connection_pools_detached(connection_id).await;
|
||||||
}
|
}
|
||||||
Ok(removed)
|
Ok(removed)
|
||||||
@@ -2464,7 +2621,10 @@ mod tests {
|
|||||||
use std::{
|
use std::{
|
||||||
io::{Read, Write},
|
io::{Read, Write},
|
||||||
net::TcpListener,
|
net::TcpListener,
|
||||||
sync::mpsc,
|
sync::{
|
||||||
|
atomic::{AtomicUsize, Ordering},
|
||||||
|
mpsc,
|
||||||
|
},
|
||||||
time::Duration,
|
time::Duration,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -2505,6 +2665,50 @@ mod tests {
|
|||||||
assert!(parse_database_type("unknown").is_err());
|
assert!(parse_database_type("unknown").is_err());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn native_mysql_transaction_gate_accepts_the_builtin_mysql_profile_only() {
|
||||||
|
let mut connection: ConnectionConfig = serde_json::from_value(json!({
|
||||||
|
"id": "mysql",
|
||||||
|
"name": "mysql",
|
||||||
|
"db_type": "mysql",
|
||||||
|
"host": "",
|
||||||
|
"port": 3306,
|
||||||
|
"username": "",
|
||||||
|
"password": "",
|
||||||
|
"database": "test",
|
||||||
|
"ssl": false
|
||||||
|
}))
|
||||||
|
.unwrap();
|
||||||
|
assert!(native_mysql_transaction_connection(&connection));
|
||||||
|
connection.driver_profile = Some("mysql".to_string());
|
||||||
|
assert!(native_mysql_transaction_connection(&connection));
|
||||||
|
connection.driver_profile = Some("MySQL".to_string());
|
||||||
|
assert!(native_mysql_transaction_connection(&connection));
|
||||||
|
for profile in ["dolt", "tidb", "oceanbase", "custom-profile"] {
|
||||||
|
connection.driver_profile = Some(profile.to_string());
|
||||||
|
assert!(!native_mysql_transaction_connection(&connection), "unexpectedly accepted {profile}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn unlimited_connection_timeout_preserves_bounded_transaction_default() {
|
||||||
|
let mut connection: ConnectionConfig = serde_json::from_value(json!({
|
||||||
|
"id": "mysql",
|
||||||
|
"name": "mysql",
|
||||||
|
"db_type": "mysql",
|
||||||
|
"host": "",
|
||||||
|
"port": 3306,
|
||||||
|
"username": "",
|
||||||
|
"password": "",
|
||||||
|
"database": "test",
|
||||||
|
"ssl": false
|
||||||
|
}))
|
||||||
|
.unwrap();
|
||||||
|
connection.query_timeout_secs = 0;
|
||||||
|
|
||||||
|
assert_eq!(transaction_operation_timeout(&connection, Duration::from_secs(300)), Duration::from_secs(300));
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parses_nested_current_and_legacy_connection_group_paths() {
|
fn parses_nested_current_and_legacy_connection_group_paths() {
|
||||||
let layout = json!({
|
let layout = json!({
|
||||||
@@ -3833,6 +4037,91 @@ mod tests {
|
|||||||
assert_eq!(backend.state().agent_manager.base_dir(), &agent_dir);
|
assert_eq!(backend.state().agent_manager.base_dir(), &agent_dir);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct LifecycleTestIo {
|
||||||
|
disconnects: Arc<AtomicUsize>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl crate::transaction::TransactionIo for LifecycleTestIo {
|
||||||
|
async fn execute(
|
||||||
|
&mut self,
|
||||||
|
_sql: &str,
|
||||||
|
_max_rows: Option<usize>,
|
||||||
|
) -> Result<crate::transaction::TransactionIoSuccess, crate::transaction::TransactionIoError> {
|
||||||
|
panic!("lifecycle test owner must stay idle")
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn ping_in_transaction(&mut self) -> Result<bool, crate::transaction::TransactionIoError> {
|
||||||
|
panic!("lifecycle test owner must not probe")
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn disconnect(&mut self) {
|
||||||
|
self.disconnects.fetch_add(1, Ordering::SeqCst);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn local_transaction_owner_rejects_stale_connection_lifecycle_before_registration() {
|
||||||
|
let data_dir = tempfile::tempdir().unwrap();
|
||||||
|
let storage = Storage::open(&data_dir.path().join("dbx.db")).await.unwrap();
|
||||||
|
let state = Arc::new(AppState::new(storage));
|
||||||
|
let backend = LocalBackend::from_app_state(state.clone(), data_dir.path().to_path_buf());
|
||||||
|
let lifecycle = state.connection_lifecycle_snapshot("conn");
|
||||||
|
state.invalidate_connection_lifecycle("conn");
|
||||||
|
let disconnects = Arc::new(AtomicUsize::new(0));
|
||||||
|
let owner = TransactionOwner::spawn(
|
||||||
|
LifecycleTestIo { disconnects: disconnects.clone() },
|
||||||
|
TransactionOwnerConfig::default(),
|
||||||
|
);
|
||||||
|
|
||||||
|
let error = backend.finalize_transaction_owner("conn", lifecycle, owner.clone()).await.unwrap_err();
|
||||||
|
|
||||||
|
assert!(error.contains("removed"), "{error}");
|
||||||
|
owner.wait_closed().await;
|
||||||
|
assert_eq!(disconnects.load(Ordering::SeqCst), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn shared_app_state_removal_cancels_a_registered_local_transaction_owner() {
|
||||||
|
let data_dir = tempfile::tempdir().unwrap();
|
||||||
|
let storage = Storage::open(&data_dir.path().join("dbx.db")).await.unwrap();
|
||||||
|
let state = Arc::new(AppState::new(storage));
|
||||||
|
let backend = LocalBackend::from_app_state(state.clone(), data_dir.path().to_path_buf());
|
||||||
|
let lifecycle = state.connection_lifecycle_snapshot("conn");
|
||||||
|
let disconnects = Arc::new(AtomicUsize::new(0));
|
||||||
|
let owner = TransactionOwner::spawn(
|
||||||
|
LifecycleTestIo { disconnects: disconnects.clone() },
|
||||||
|
TransactionOwnerConfig::default(),
|
||||||
|
);
|
||||||
|
backend.finalize_transaction_owner("conn", lifecycle, owner.clone()).await.unwrap();
|
||||||
|
|
||||||
|
state.remove_connection_pools_detached("conn").await;
|
||||||
|
owner.wait_closed().await;
|
||||||
|
|
||||||
|
assert_eq!(disconnects.load(Ordering::SeqCst), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn connection_lifecycle_watcher_exits_after_normal_owner_disposal() {
|
||||||
|
let data_dir = tempfile::tempdir().unwrap();
|
||||||
|
let storage = Storage::open(&data_dir.path().join("dbx.db")).await.unwrap();
|
||||||
|
let state = Arc::new(AppState::new(storage));
|
||||||
|
let backend = LocalBackend::from_app_state(state.clone(), data_dir.path().to_path_buf());
|
||||||
|
let lifecycle = state.connection_lifecycle_snapshot("conn");
|
||||||
|
let owner = TransactionOwner::spawn(
|
||||||
|
LifecycleTestIo { disconnects: Arc::new(AtomicUsize::new(0)) },
|
||||||
|
TransactionOwnerConfig::default(),
|
||||||
|
);
|
||||||
|
let watcher = backend.spawn_connection_lifecycle_watcher(lifecycle, &owner);
|
||||||
|
|
||||||
|
owner.close().await.unwrap();
|
||||||
|
|
||||||
|
tokio::time::timeout(Duration::from_millis(100), watcher)
|
||||||
|
.await
|
||||||
|
.expect("lifecycle watcher must not outlive a normally disposed owner")
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn local_backend_standalone_open_skips_plugin_dbx_engine_gate() {
|
async fn local_backend_standalone_open_skips_plugin_dbx_engine_gate() {
|
||||||
let data_dir = tempfile::tempdir().unwrap();
|
let data_dir = tempfile::tempdir().unwrap();
|
||||||
|
|||||||
+403
-4
@@ -2,7 +2,10 @@ use std::{io, sync::Arc};
|
|||||||
|
|
||||||
use axum::{http::Method, middleware, routing::get, Router};
|
use axum::{http::Method, middleware, routing::get, Router};
|
||||||
use rmcp::transport::{
|
use rmcp::transport::{
|
||||||
streamable_http_server::{session::local::LocalSessionManager, tower::StreamableHttpService},
|
streamable_http_server::{
|
||||||
|
session::{local::LocalSessionManager, SessionManager},
|
||||||
|
tower::StreamableHttpService,
|
||||||
|
},
|
||||||
StreamableHttpServerConfig,
|
StreamableHttpServerConfig,
|
||||||
};
|
};
|
||||||
use tokio_util::sync::CancellationToken;
|
use tokio_util::sync::CancellationToken;
|
||||||
@@ -25,7 +28,7 @@ pub fn streamable_http_router(
|
|||||||
allowed_hosts: Vec<String>,
|
allowed_hosts: Vec<String>,
|
||||||
web_mode: bool,
|
web_mode: bool,
|
||||||
) -> Router {
|
) -> Router {
|
||||||
build_streamable_http_router(backend, path, auth, allowed_hosts, web_mode, None)
|
build_streamable_http_router(backend, path, auth, allowed_hosts, web_mode, None, Default::default())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build_streamable_http_router(
|
fn build_streamable_http_router(
|
||||||
@@ -35,6 +38,7 @@ fn build_streamable_http_router(
|
|||||||
allowed_hosts: Vec<String>,
|
allowed_hosts: Vec<String>,
|
||||||
web_mode: bool,
|
web_mode: bool,
|
||||||
cancellation: Option<CancellationToken>,
|
cancellation: Option<CancellationToken>,
|
||||||
|
session_manager: Arc<LocalSessionManager>,
|
||||||
) -> Router {
|
) -> Router {
|
||||||
let mut rmcp_config = StreamableHttpServerConfig::default().with_allowed_hosts(allowed_hosts);
|
let mut rmcp_config = StreamableHttpServerConfig::default().with_allowed_hosts(allowed_hosts);
|
||||||
if let Some(cancellation) = cancellation {
|
if let Some(cancellation) = cancellation {
|
||||||
@@ -44,7 +48,7 @@ fn build_streamable_http_router(
|
|||||||
let scope = McpScope::from_env();
|
let scope = McpScope::from_env();
|
||||||
let service: StreamableHttpService<DbxMcpServer, LocalSessionManager> = StreamableHttpService::new(
|
let service: StreamableHttpService<DbxMcpServer, LocalSessionManager> = StreamableHttpService::new(
|
||||||
move || Ok(DbxMcpServer::with_runtime_options(server_backend.clone(), scope.clone(), web_mode)),
|
move || Ok(DbxMcpServer::with_runtime_options(server_backend.clone(), scope.clone(), web_mode)),
|
||||||
Default::default(),
|
session_manager,
|
||||||
rmcp_config,
|
rmcp_config,
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -98,6 +102,7 @@ pub async fn serve_streamable_http_on_listener(
|
|||||||
cancellation: CancellationToken,
|
cancellation: CancellationToken,
|
||||||
listener: tokio::net::TcpListener,
|
listener: tokio::net::TcpListener,
|
||||||
) -> io::Result<()> {
|
) -> io::Result<()> {
|
||||||
|
let session_manager = Arc::new(LocalSessionManager::default());
|
||||||
let mcp_router = build_streamable_http_router(
|
let mcp_router = build_streamable_http_router(
|
||||||
backend,
|
backend,
|
||||||
&config.path,
|
&config.path,
|
||||||
@@ -105,14 +110,408 @@ pub async fn serve_streamable_http_on_listener(
|
|||||||
config.allowed_hosts,
|
config.allowed_hosts,
|
||||||
false,
|
false,
|
||||||
Some(cancellation.child_token()),
|
Some(cancellation.child_token()),
|
||||||
|
session_manager.clone(),
|
||||||
);
|
);
|
||||||
let router = Router::new().route("/healthz", get(health)).route("/readyz", get(health)).merge(mcp_router);
|
let router = Router::new().route("/healthz", get(health)).route("/readyz", get(health)).merge(mcp_router);
|
||||||
|
|
||||||
eprintln!("DBX MCP Streamable HTTP listening on http://{}{}", config.bind_addr, config.path);
|
eprintln!("DBX MCP Streamable HTTP listening on http://{}{}", config.bind_addr, config.path);
|
||||||
|
|
||||||
axum::serve(listener, router)
|
let result = axum::serve(listener, router)
|
||||||
.with_graceful_shutdown(async move {
|
.with_graceful_shutdown(async move {
|
||||||
cancellation.cancelled().await;
|
cancellation.cancelled().await;
|
||||||
})
|
})
|
||||||
|
.await;
|
||||||
|
close_local_sessions_bounded(&session_manager).await;
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn close_local_sessions_bounded(session_manager: &Arc<LocalSessionManager>) {
|
||||||
|
let session_ids = session_manager.sessions.read().await.keys().cloned().collect::<Vec<_>>();
|
||||||
|
let cleanup = async {
|
||||||
|
for session_id in session_ids {
|
||||||
|
let _ = session_manager.close_session(&session_id).await;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
if tokio::time::timeout(std::time::Duration::from_secs(10), cleanup).await.is_err() {
|
||||||
|
log::warn!("Timed out draining MCP HTTP protocol sessions during shutdown");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||||
|
|
||||||
|
use async_trait::async_trait;
|
||||||
|
use dbx_core::{
|
||||||
|
agent_events::ToolResult, agent_tools::AgentSqlPermissions, models::connection::ConnectionConfig,
|
||||||
|
storage::McpGlobalPolicy,
|
||||||
|
};
|
||||||
|
use rmcp::{
|
||||||
|
model::CallToolRequestParams,
|
||||||
|
service::ServiceExt,
|
||||||
|
transport::{
|
||||||
|
streamable_http_client::StreamableHttpClientTransportConfig,
|
||||||
|
streamable_http_server::session::local::SessionConfig, StreamableHttpClientTransport,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
use serde_json::{json, Value};
|
||||||
|
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
use crate::{
|
||||||
|
backend::DbxBackend,
|
||||||
|
transaction::{
|
||||||
|
TransactionIo, TransactionIoError, TransactionIoSuccess, TransactionOwner, TransactionOwnerConfig,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
struct HttpTestIo {
|
||||||
|
sql: Arc<std::sync::Mutex<Vec<String>>>,
|
||||||
|
disconnects: Arc<AtomicUsize>,
|
||||||
|
in_transaction: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl TransactionIo for HttpTestIo {
|
||||||
|
async fn execute(
|
||||||
|
&mut self,
|
||||||
|
sql: &str,
|
||||||
|
_max_rows: Option<usize>,
|
||||||
|
) -> Result<TransactionIoSuccess, TransactionIoError> {
|
||||||
|
self.sql.lock().unwrap().push(sql.to_string());
|
||||||
|
match sql {
|
||||||
|
"START TRANSACTION" => self.in_transaction = true,
|
||||||
|
"COMMIT" | "ROLLBACK" => self.in_transaction = false,
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
Ok(TransactionIoSuccess {
|
||||||
|
result: dbx_core::db::QueryResult {
|
||||||
|
columns: Vec::new(),
|
||||||
|
column_types: Vec::new(),
|
||||||
|
column_sortables: Vec::new(),
|
||||||
|
spatial_columns: Vec::new(),
|
||||||
|
spatial_values: Vec::new(),
|
||||||
|
rows: Vec::new(),
|
||||||
|
affected_rows: 0,
|
||||||
|
execution_time_ms: 0,
|
||||||
|
truncated: false,
|
||||||
|
session_id: None,
|
||||||
|
has_more: false,
|
||||||
|
elasticsearch_raw_body: None,
|
||||||
|
messages: Vec::new(),
|
||||||
|
},
|
||||||
|
in_transaction: self.in_transaction,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn ping_in_transaction(&mut self) -> Result<bool, TransactionIoError> {
|
||||||
|
Ok(self.in_transaction)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn disconnect(&mut self) {
|
||||||
|
self.disconnects.fetch_add(1, Ordering::SeqCst);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct HttpTestBackend {
|
||||||
|
connection: ConnectionConfig,
|
||||||
|
sql: Arc<std::sync::Mutex<Vec<String>>>,
|
||||||
|
disconnects: Arc<AtomicUsize>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl HttpTestBackend {
|
||||||
|
fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
connection: serde_json::from_value(json!({
|
||||||
|
"id": "mysql",
|
||||||
|
"name": "mysql",
|
||||||
|
"db_type": "mysql",
|
||||||
|
"host": "",
|
||||||
|
"port": 3306,
|
||||||
|
"username": "",
|
||||||
|
"password": "",
|
||||||
|
"database": "app",
|
||||||
|
"ssl": false
|
||||||
|
}))
|
||||||
|
.unwrap(),
|
||||||
|
sql: Arc::new(std::sync::Mutex::new(Vec::new())),
|
||||||
|
disconnects: Arc::new(AtomicUsize::new(0)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[async_trait]
|
||||||
|
impl DbxBackend for HttpTestBackend {
|
||||||
|
async fn load_mcp_global_policy(&self) -> Result<McpGlobalPolicy, String> {
|
||||||
|
Ok(McpGlobalPolicy { read_only: false, allow_dangerous_sql: true, ..Default::default() })
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn load_connections(&self) -> Result<Vec<ConnectionConfig>, String> {
|
||||||
|
Ok(vec![self.connection.clone()])
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn execute_agent_tool(
|
||||||
|
&self,
|
||||||
|
_connection: &ConnectionConfig,
|
||||||
|
_database: &str,
|
||||||
|
tool_name: &str,
|
||||||
|
_arguments: Value,
|
||||||
|
_permissions: AgentSqlPermissions,
|
||||||
|
) -> ToolResult {
|
||||||
|
ToolResult {
|
||||||
|
tool_call_id: "http-test".to_string(),
|
||||||
|
tool_name: tool_name.to_string(),
|
||||||
|
content: "unused".to_string(),
|
||||||
|
is_error: false,
|
||||||
|
explain_data: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn open_transaction_owner(
|
||||||
|
&self,
|
||||||
|
_connection: &ConnectionConfig,
|
||||||
|
_database: &str,
|
||||||
|
_client_session_id: &str,
|
||||||
|
) -> Result<Arc<TransactionOwner>, String> {
|
||||||
|
Ok(TransactionOwner::spawn(
|
||||||
|
HttpTestIo { sql: self.sql.clone(), disconnects: self.disconnects.clone(), in_transaction: false },
|
||||||
|
TransactionOwnerConfig { cleanup_timeout: std::time::Duration::from_millis(50), ..Default::default() },
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn add_connection_for_mcp(&self, config: ConnectionConfig) -> Result<ConnectionConfig, String> {
|
||||||
|
Ok(config)
|
||||||
|
}
|
||||||
|
async fn duplicate_connection_for_mcp(
|
||||||
|
&self,
|
||||||
|
_source_id: &str,
|
||||||
|
_copy_id: &str,
|
||||||
|
_copy_name: &str,
|
||||||
|
) -> Result<ConnectionConfig, String> {
|
||||||
|
Err("unused".to_string())
|
||||||
|
}
|
||||||
|
async fn remove_connection_for_mcp(&self, _connection_id: &str) -> Result<bool, String> {
|
||||||
|
Ok(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn start_http_test_server(
|
||||||
|
backend: Arc<HttpTestBackend>,
|
||||||
|
keep_alive: std::time::Duration,
|
||||||
|
) -> (String, Arc<LocalSessionManager>, CancellationToken, tokio::task::JoinHandle<()>) {
|
||||||
|
// A single default provider avoids the "No rustls crypto provider is
|
||||||
|
// configured" panic when tests build reqwest clients in workspace
|
||||||
|
// builds where multiple rustls crypto features are present; the
|
||||||
|
// install is idempotent, so subsequent calls are no-ops.
|
||||||
|
let _ = rustls::crypto::aws_lc_rs::default_provider().install_default();
|
||||||
|
let listener = tokio::net::TcpListener::bind(("127.0.0.1", 0)).await.unwrap();
|
||||||
|
let address = listener.local_addr().unwrap();
|
||||||
|
let mut session_config = SessionConfig::default();
|
||||||
|
session_config.keep_alive = Some(keep_alive);
|
||||||
|
let mut local_manager = LocalSessionManager::default();
|
||||||
|
local_manager.session_config = session_config;
|
||||||
|
let manager = Arc::new(local_manager);
|
||||||
|
let cancellation = CancellationToken::new();
|
||||||
|
let router = build_streamable_http_router(
|
||||||
|
backend,
|
||||||
|
"/mcp",
|
||||||
|
HttpAuth::new("http-test-token".to_string(), Vec::<String>::new(), true).unwrap(),
|
||||||
|
vec![address.to_string()],
|
||||||
|
false,
|
||||||
|
Some(cancellation.child_token()),
|
||||||
|
manager.clone(),
|
||||||
|
);
|
||||||
|
let shutdown = cancellation.clone();
|
||||||
|
let shutdown_manager = manager.clone();
|
||||||
|
let task = tokio::spawn(async move {
|
||||||
|
axum::serve(listener, router)
|
||||||
|
.with_graceful_shutdown(async move { shutdown.cancelled().await })
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
close_local_sessions_bounded(&shutdown_manager).await;
|
||||||
|
});
|
||||||
|
(format!("http://{address}/mcp"), manager, cancellation, task)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn open_active_transaction(url: &str) -> (rmcp::service::RunningService<rmcp::RoleClient, ()>, String) {
|
||||||
|
let transport = StreamableHttpClientTransport::from_config(
|
||||||
|
StreamableHttpClientTransportConfig::with_uri(url.to_string()).auth_header("http-test-token"),
|
||||||
|
);
|
||||||
|
let client = ().serve(transport).await.unwrap();
|
||||||
|
let opened = client
|
||||||
|
.call_tool(
|
||||||
|
CallToolRequestParams::new("dbx_open_session").with_arguments(
|
||||||
|
serde_json::from_value(json!({
|
||||||
|
"connection_id": "mysql",
|
||||||
|
"database": "app",
|
||||||
|
"enable_transactions": true
|
||||||
|
}))
|
||||||
|
.unwrap(),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
let session_id = opened.structured_content.unwrap()["session_id"].as_str().unwrap().to_string();
|
||||||
|
let begun = client
|
||||||
|
.call_tool(
|
||||||
|
CallToolRequestParams::new("dbx_begin_transaction")
|
||||||
|
.with_arguments(serde_json::from_value(json!({"session_id": session_id.clone()})).unwrap()),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
assert_ne!(begun.is_error, Some(true));
|
||||||
|
(client, session_id)
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn wait_for_disposal(backend: &HttpTestBackend) {
|
||||||
|
tokio::time::timeout(std::time::Duration::from_secs(1), async {
|
||||||
|
loop {
|
||||||
|
if backend.disconnects.load(Ordering::SeqCst) == 1
|
||||||
|
&& backend.sql.lock().unwrap().iter().any(|sql| sql == "ROLLBACK")
|
||||||
|
{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
tokio::task::yield_now().await;
|
||||||
|
}
|
||||||
|
})
|
||||||
.await
|
.await
|
||||||
|
.expect("HTTP session cleanup must roll back and disconnect the owner");
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn open_and_drop_raw_sse(url: &str, outer_session_id: &str) {
|
||||||
|
let parsed = url::Url::parse(url).unwrap();
|
||||||
|
let host = parsed.host_str().unwrap();
|
||||||
|
let port = parsed.port_or_known_default().unwrap();
|
||||||
|
let mut stream = tokio::net::TcpStream::connect((host, port)).await.unwrap();
|
||||||
|
let path = match parsed.query() {
|
||||||
|
Some(query) => format!("{}?{query}", parsed.path()),
|
||||||
|
None => parsed.path().to_string(),
|
||||||
|
};
|
||||||
|
let request = format!(
|
||||||
|
"GET {path} HTTP/1.1\r\nHost: {host}:{port}\r\nAuthorization: Bearer http-test-token\r\nAccept: text/event-stream\r\nMcp-Session-Id: {outer_session_id}\r\nMcp-Protocol-Version: 2025-06-18\r\nConnection: keep-alive\r\n\r\n"
|
||||||
|
);
|
||||||
|
stream.write_all(request.as_bytes()).await.unwrap();
|
||||||
|
|
||||||
|
let headers = tokio::time::timeout(std::time::Duration::from_secs(1), async {
|
||||||
|
let mut response = Vec::new();
|
||||||
|
let mut buffer = [0_u8; 1024];
|
||||||
|
loop {
|
||||||
|
let read = stream.read(&mut buffer).await.unwrap();
|
||||||
|
assert!(read > 0, "raw SSE connection closed before HTTP headers");
|
||||||
|
response.extend_from_slice(&buffer[..read]);
|
||||||
|
if response.windows(4).any(|window| window == b"\r\n\r\n") {
|
||||||
|
break response;
|
||||||
|
}
|
||||||
|
assert!(response.len() <= 16 * 1024, "raw SSE response headers exceeded 16 KiB");
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
.expect("raw SSE GET must return HTTP headers");
|
||||||
|
let headers = std::str::from_utf8(&headers).unwrap();
|
||||||
|
assert!(headers.starts_with("HTTP/1.1 200 "), "raw SSE GET did not return HTTP 200: {headers}");
|
||||||
|
assert!(headers.to_ascii_lowercase().contains("content-type: text/event-stream"));
|
||||||
|
|
||||||
|
drop(stream);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn authenticated_http_delete_rolls_back_and_disconnects_inner_owner() {
|
||||||
|
let backend = Arc::new(HttpTestBackend::new());
|
||||||
|
let (url, manager, cancellation, server_task) =
|
||||||
|
start_http_test_server(backend.clone(), std::time::Duration::from_secs(30)).await;
|
||||||
|
let (client, _) = open_active_transaction(&url).await;
|
||||||
|
let outer_session_id = manager.sessions.read().await.keys().next().unwrap().to_string();
|
||||||
|
|
||||||
|
let response = reqwest::Client::new()
|
||||||
|
.delete(&url)
|
||||||
|
.bearer_auth("http-test-token")
|
||||||
|
.header("mcp-session-id", outer_session_id)
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
assert!(response.status().is_success(), "DELETE returned {}", response.status());
|
||||||
|
wait_for_disposal(&backend).await;
|
||||||
|
|
||||||
|
drop(client);
|
||||||
|
cancellation.cancel();
|
||||||
|
server_task.await.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn http_inactivity_expiry_rolls_back_and_disconnects_inner_owner() {
|
||||||
|
let backend = Arc::new(HttpTestBackend::new());
|
||||||
|
let (url, manager, cancellation, server_task) =
|
||||||
|
start_http_test_server(backend.clone(), std::time::Duration::from_millis(50)).await;
|
||||||
|
let (client, _) = open_active_transaction(&url).await;
|
||||||
|
|
||||||
|
wait_for_disposal(&backend).await;
|
||||||
|
tokio::time::timeout(std::time::Duration::from_secs(1), async {
|
||||||
|
while !manager.sessions.read().await.is_empty() {
|
||||||
|
tokio::task::yield_now().await;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.await
|
||||||
|
.expect("expired outer HTTP session must be removed");
|
||||||
|
|
||||||
|
drop(client);
|
||||||
|
cancellation.cancel();
|
||||||
|
server_task.await.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn transient_http_connections_preserve_one_outer_session() {
|
||||||
|
let backend = Arc::new(HttpTestBackend::new());
|
||||||
|
let (url, manager, cancellation, server_task) =
|
||||||
|
start_http_test_server(backend.clone(), std::time::Duration::from_secs(30)).await;
|
||||||
|
let (client, inner_session_id) = open_active_transaction(&url).await;
|
||||||
|
let outer_session_id = manager.sessions.read().await.keys().next().unwrap().to_string();
|
||||||
|
|
||||||
|
open_and_drop_raw_sse(&url, &outer_session_id).await;
|
||||||
|
|
||||||
|
let query = client
|
||||||
|
.call_tool(
|
||||||
|
CallToolRequestParams::new("dbx_execute_query").with_arguments(
|
||||||
|
serde_json::from_value(json!({
|
||||||
|
"connection_id": "mysql",
|
||||||
|
"database": "app",
|
||||||
|
"session_id": inner_session_id,
|
||||||
|
"sql": "SELECT 1"
|
||||||
|
}))
|
||||||
|
.unwrap(),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
assert_ne!(query.is_error, Some(true));
|
||||||
|
assert_eq!(query.structured_content.as_ref().unwrap()["transaction_state"], "active");
|
||||||
|
assert_eq!(manager.sessions.read().await.len(), 1);
|
||||||
|
assert_eq!(backend.disconnects.load(Ordering::SeqCst), 0);
|
||||||
|
assert!(!backend.sql.lock().unwrap().iter().any(|sql| sql == "ROLLBACK"));
|
||||||
|
|
||||||
|
let response = reqwest::Client::new()
|
||||||
|
.delete(&url)
|
||||||
|
.bearer_auth("http-test-token")
|
||||||
|
.header("mcp-session-id", outer_session_id)
|
||||||
|
.send()
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
assert!(response.status().is_success());
|
||||||
|
wait_for_disposal(&backend).await;
|
||||||
|
drop(client);
|
||||||
|
cancellation.cancel();
|
||||||
|
server_task.await.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn http_service_shutdown_rolls_back_and_disconnects_inner_owner() {
|
||||||
|
let backend = Arc::new(HttpTestBackend::new());
|
||||||
|
let (url, _manager, cancellation, server_task) =
|
||||||
|
start_http_test_server(backend.clone(), std::time::Duration::from_secs(30)).await;
|
||||||
|
let (client, _) = open_active_transaction(&url).await;
|
||||||
|
|
||||||
|
cancellation.cancel();
|
||||||
|
server_task.await.unwrap();
|
||||||
|
wait_for_disposal(&backend).await;
|
||||||
|
drop(client);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -8,6 +8,7 @@ pub mod paths;
|
|||||||
pub mod runtime;
|
pub mod runtime;
|
||||||
pub mod server;
|
pub mod server;
|
||||||
pub mod session;
|
pub mod session;
|
||||||
|
pub mod transaction;
|
||||||
pub mod transport;
|
pub mod transport;
|
||||||
|
|
||||||
pub use backend::{ConnectionSummary, DbxBackend, LocalBackend, WebBackend};
|
pub use backend::{ConnectionSummary, DbxBackend, LocalBackend, WebBackend};
|
||||||
|
|||||||
+1671
-49
File diff suppressed because it is too large.
Load diff
+107
-29
@@ -5,6 +5,8 @@ use std::time::{Duration, Instant};
|
|||||||
use tokio::sync::Mutex;
|
use tokio::sync::Mutex;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
use crate::transaction::TransactionOwner;
|
||||||
|
|
||||||
/// Idle time after which an MCP session is considered expired and its pinned
|
/// Idle time after which an MCP session is considered expired and its pinned
|
||||||
/// backend connection pool may be reclaimed.
|
/// backend connection pool may be reclaimed.
|
||||||
const SESSION_IDLE_TTL: Duration = Duration::from_secs(30 * 60);
|
const SESSION_IDLE_TTL: Duration = Duration::from_secs(30 * 60);
|
||||||
@@ -22,6 +24,7 @@ pub struct McpSession {
|
|||||||
/// Value forwarded to the backend as `client_session_id`, pinning all
|
/// Value forwarded to the backend as `client_session_id`, pinning all
|
||||||
/// queries in this session to the same connection pool.
|
/// queries in this session to the same connection pool.
|
||||||
pub client_session_id: String,
|
pub client_session_id: String,
|
||||||
|
pub transaction_owner: Option<Arc<TransactionOwner>>,
|
||||||
last_used: Instant,
|
last_used: Instant,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,6 +46,7 @@ impl<T> McpSessionStoreResult<T> {
|
|||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
struct McpSessionState {
|
struct McpSessionState {
|
||||||
|
opening: HashMap<String, McpSession>,
|
||||||
active: HashMap<String, McpSession>,
|
active: HashMap<String, McpSession>,
|
||||||
closing: HashMap<String, McpSession>,
|
closing: HashMap<String, McpSession>,
|
||||||
}
|
}
|
||||||
@@ -63,8 +67,8 @@ impl McpSessionStore {
|
|||||||
/// reached; expired sessions are swept before the cap is enforced.
|
/// reached; expired sessions are swept before the cap is enforced.
|
||||||
pub async fn open(&self, connection_id: &str, database: &str) -> McpSessionStoreResult<Result<McpSession, String>> {
|
pub async fn open(&self, connection_id: &str, database: &str) -> McpSessionStoreResult<Result<McpSession, String>> {
|
||||||
let mut state = self.state.lock().await;
|
let mut state = self.state.lock().await;
|
||||||
let expired = sweep_expired(&mut state.active);
|
let expired = sweep_expired(&mut state);
|
||||||
if state.active.len() + state.closing.len() >= MAX_SESSIONS {
|
if session_count(&state) >= MAX_SESSIONS {
|
||||||
return McpSessionStoreResult::new(
|
return McpSessionStoreResult::new(
|
||||||
Err(format!(
|
Err(format!(
|
||||||
"Too many open MCP sessions (max {MAX_SESSIONS}). Close unused sessions with dbx_close_session."
|
"Too many open MCP sessions (max {MAX_SESSIONS}). Close unused sessions with dbx_close_session."
|
||||||
@@ -72,25 +76,55 @@ impl McpSessionStore {
|
|||||||
expired,
|
expired,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
let id = format!("mcp-session-{}", Uuid::new_v4());
|
let session = new_session(connection_id, database);
|
||||||
let session = McpSession {
|
state.active.insert(session.id.clone(), session.clone());
|
||||||
id: id.clone(),
|
|
||||||
connection_id: connection_id.to_string(),
|
|
||||||
database: database.to_string(),
|
|
||||||
// Prefixed so these pools are easy to distinguish from desktop UI
|
|
||||||
// sessions in backend diagnostics. `:` is normalized away by the
|
|
||||||
// backend pool key sanitizer.
|
|
||||||
client_session_id: format!("mcp:{id}"),
|
|
||||||
last_used: Instant::now(),
|
|
||||||
};
|
|
||||||
state.active.insert(id, session.clone());
|
|
||||||
McpSessionStoreResult::new(Ok(session), expired)
|
McpSessionStoreResult::new(Ok(session), expired)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn reserve_opening(
|
||||||
|
&self,
|
||||||
|
connection_id: &str,
|
||||||
|
database: &str,
|
||||||
|
) -> McpSessionStoreResult<Result<McpSession, String>> {
|
||||||
|
let mut state = self.state.lock().await;
|
||||||
|
let expired = sweep_expired(&mut state);
|
||||||
|
if session_count(&state) >= MAX_SESSIONS {
|
||||||
|
return McpSessionStoreResult::new(
|
||||||
|
Err(format!(
|
||||||
|
"Too many open MCP sessions (max {MAX_SESSIONS}). Close unused sessions with dbx_close_session."
|
||||||
|
)),
|
||||||
|
expired,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
let session = new_session(connection_id, database);
|
||||||
|
state.opening.insert(session.id.clone(), session.clone());
|
||||||
|
McpSessionStoreResult::new(Ok(session), expired)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn promote_opening(&self, session_id: &str, owner: Arc<TransactionOwner>) -> Result<McpSession, String> {
|
||||||
|
let mut state = self.state.lock().await;
|
||||||
|
let mut session =
|
||||||
|
state.opening.remove(session_id).ok_or_else(|| "MCP session opening was cancelled".to_string())?;
|
||||||
|
session.transaction_owner = Some(owner);
|
||||||
|
state.active.insert(session.id.clone(), session.clone());
|
||||||
|
Ok(session)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn cancel_opening(&self, session_id: &str) -> Option<McpSession> {
|
||||||
|
self.state.lock().await.opening.remove(session_id)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn attach_transaction_owner(&self, session_id: &str, owner: Arc<TransactionOwner>) -> Result<(), String> {
|
||||||
|
let mut state = self.state.lock().await;
|
||||||
|
let session = state.active.get_mut(session_id).ok_or_else(|| "MCP session is no longer active".to_string())?;
|
||||||
|
session.transaction_owner = Some(owner);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
/// Resolve a session id and refresh its idle timer.
|
/// Resolve a session id and refresh its idle timer.
|
||||||
pub async fn resolve(&self, session_id: &str) -> McpSessionStoreResult<Option<McpSession>> {
|
pub async fn resolve(&self, session_id: &str) -> McpSessionStoreResult<Option<McpSession>> {
|
||||||
let mut state = self.state.lock().await;
|
let mut state = self.state.lock().await;
|
||||||
let expired = sweep_expired(&mut state.active);
|
let expired = sweep_expired(&mut state);
|
||||||
let session = state.active.get_mut(session_id).map(|session| {
|
let session = state.active.get_mut(session_id).map(|session| {
|
||||||
session.last_used = Instant::now();
|
session.last_used = Instant::now();
|
||||||
session.clone()
|
session.clone()
|
||||||
@@ -102,10 +136,10 @@ impl McpSessionStore {
|
|||||||
/// cap until the backend pool is closed successfully.
|
/// cap until the backend pool is closed successfully.
|
||||||
pub async fn begin_close(&self, session_id: &str) -> McpSessionStoreResult<Option<McpSession>> {
|
pub async fn begin_close(&self, session_id: &str) -> McpSessionStoreResult<Option<McpSession>> {
|
||||||
let mut state = self.state.lock().await;
|
let mut state = self.state.lock().await;
|
||||||
let expired = sweep_expired(&mut state.active);
|
let expired = sweep_expired(&mut state);
|
||||||
let session = state.active.remove(session_id);
|
let session = state.active.remove(session_id);
|
||||||
if let Some(session) = &session {
|
if let Some(session) = &session {
|
||||||
state.closing.insert(session.id.clone(), session.clone());
|
state.closing.entry(session.id.clone()).or_insert_with(|| session.clone());
|
||||||
}
|
}
|
||||||
McpSessionStoreResult::new(session, expired)
|
McpSessionStoreResult::new(session, expired)
|
||||||
}
|
}
|
||||||
@@ -129,16 +163,40 @@ impl McpSessionStore {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn sweep_expired(sessions: &mut HashMap<String, McpSession>) -> Vec<McpSession> {
|
fn new_session(connection_id: &str, database: &str) -> McpSession {
|
||||||
|
let id = format!("mcp-session-{}", Uuid::new_v4());
|
||||||
|
McpSession {
|
||||||
|
id: id.clone(),
|
||||||
|
connection_id: connection_id.to_string(),
|
||||||
|
database: database.to_string(),
|
||||||
|
// Prefixed so these pools are easy to distinguish from desktop UI
|
||||||
|
// sessions in backend diagnostics. `:` is normalized away by the
|
||||||
|
// backend pool key sanitizer.
|
||||||
|
client_session_id: format!("mcp:{id}"),
|
||||||
|
transaction_owner: None,
|
||||||
|
last_used: Instant::now(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn session_count(state: &McpSessionState) -> usize {
|
||||||
|
state.opening.len() + state.active.len() + state.closing.len()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn sweep_expired(state: &mut McpSessionState) -> Vec<McpSession> {
|
||||||
let now = Instant::now();
|
let now = Instant::now();
|
||||||
let mut expired = Vec::new();
|
let expired_ids = state
|
||||||
sessions.retain(|_, session| {
|
.active
|
||||||
let active = now.duration_since(session.last_used) < SESSION_IDLE_TTL;
|
.iter()
|
||||||
if !active {
|
.filter(|(_, session)| now.duration_since(session.last_used) >= SESSION_IDLE_TTL)
|
||||||
expired.push(session.clone());
|
.map(|(id, _)| id.clone())
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
let mut expired = Vec::with_capacity(expired_ids.len());
|
||||||
|
for id in expired_ids {
|
||||||
|
if let Some(session) = state.active.remove(&id) {
|
||||||
|
state.closing.insert(id, session.clone());
|
||||||
|
expired.push(session);
|
||||||
}
|
}
|
||||||
active
|
}
|
||||||
});
|
|
||||||
expired
|
expired
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -165,6 +223,7 @@ mod tests {
|
|||||||
assert!(expired.is_empty());
|
assert!(expired.is_empty());
|
||||||
assert_eq!(closing.unwrap().id, session.id);
|
assert_eq!(closing.unwrap().id, session.id);
|
||||||
assert!(store.resolve(&session.id).await.into_parts().0.is_none());
|
assert!(store.resolve(&session.id).await.into_parts().0.is_none());
|
||||||
|
assert!(store.begin_close(&session.id).await.into_parts().0.is_none());
|
||||||
store.finish_close(&session.id).await;
|
store.finish_close(&session.id).await;
|
||||||
assert!(store.begin_close(&session.id).await.into_parts().0.is_none());
|
assert!(store.begin_close(&session.id).await.into_parts().0.is_none());
|
||||||
}
|
}
|
||||||
@@ -180,7 +239,7 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn expired_sessions_are_swept_and_free_capacity() {
|
async fn expired_sessions_remain_counted_until_cleanup_finishes() {
|
||||||
let store = McpSessionStore::new();
|
let store = McpSessionStore::new();
|
||||||
let mut session_ids = Vec::new();
|
let mut session_ids = Vec::new();
|
||||||
for _ in 0..MAX_SESSIONS {
|
for _ in 0..MAX_SESSIONS {
|
||||||
@@ -190,11 +249,15 @@ mod tests {
|
|||||||
for session_id in &session_ids {
|
for session_id in &session_ids {
|
||||||
store.expire_for_test(session_id).await;
|
store.expire_for_test(session_id).await;
|
||||||
}
|
}
|
||||||
// All sessions are expired: the sweep must reclaim them before the cap
|
// Expiry transfers ownership to closing. Capacity is not reusable until
|
||||||
// check, return them for backend cleanup, and allow a new session.
|
// the detached cleanup worker confirms physical disposal.
|
||||||
let (opened, expired) = store.open("conn-1", "").await.into_parts();
|
let (opened, expired) = store.open("conn-1", "").await.into_parts();
|
||||||
opened.unwrap();
|
assert!(opened.unwrap_err().contains("Too many open MCP sessions"));
|
||||||
assert_eq!(expired.len(), MAX_SESSIONS);
|
assert_eq!(expired.len(), MAX_SESSIONS);
|
||||||
|
for session in expired {
|
||||||
|
store.finish_close(&session.id).await;
|
||||||
|
}
|
||||||
|
store.open("conn-1", "").await.into_parts().0.unwrap();
|
||||||
|
|
||||||
// Resolving an expired session must fail instead of silently pinning a
|
// Resolving an expired session must fail instead of silently pinning a
|
||||||
// fresh backend connection.
|
// fresh backend connection.
|
||||||
@@ -205,4 +268,19 @@ mod tests {
|
|||||||
assert_eq!(expired.len(), 1);
|
assert_eq!(expired.len(), 1);
|
||||||
assert_eq!(expired[0].id, session.id);
|
assert_eq!(expired[0].id, session.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn provisional_openings_count_toward_cap_until_promoted_or_cancelled() {
|
||||||
|
let store = McpSessionStore::new();
|
||||||
|
let mut openings = Vec::new();
|
||||||
|
for _ in 0..MAX_SESSIONS {
|
||||||
|
openings.push(store.reserve_opening("conn-1", "").await.into_parts().0.unwrap());
|
||||||
|
}
|
||||||
|
assert!(store.reserve_opening("conn-1", "").await.into_parts().0.is_err());
|
||||||
|
assert!(store.open("conn-1", "").await.into_parts().0.is_err());
|
||||||
|
|
||||||
|
let cancelled = openings.pop().unwrap();
|
||||||
|
assert!(store.cancel_opening(&cancelled.id).await.is_some());
|
||||||
|
store.open("conn-1", "").await.into_parts().0.unwrap();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
File diff suppressed because it is too large.
Load diff
@@ -328,9 +328,9 @@ async fn initializes_lists_tools_and_calls_a_tool() {
|
|||||||
let tools = client.peer().list_tools(None).await.expect("list tools");
|
let tools = client.peer().list_tools(None).await.expect("list tools");
|
||||||
let names = tools.tools.iter().map(|tool| tool.name.as_ref()).collect::<Vec<_>>();
|
let names = tools.tools.iter().map(|tool| tool.name.as_ref()).collect::<Vec<_>>();
|
||||||
#[cfg(feature = "mq-admin")]
|
#[cfg(feature = "mq-admin")]
|
||||||
assert_eq!(names.len(), 19);
|
assert_eq!(names.len(), 22);
|
||||||
#[cfg(not(feature = "mq-admin"))]
|
#[cfg(not(feature = "mq-admin"))]
|
||||||
assert_eq!(names.len(), 17);
|
assert_eq!(names.len(), 20);
|
||||||
#[cfg(feature = "mq-admin")]
|
#[cfg(feature = "mq-admin")]
|
||||||
assert!(names.contains(&"dbx_peek_messages"));
|
assert!(names.contains(&"dbx_peek_messages"));
|
||||||
#[cfg(not(feature = "mq-admin"))]
|
#[cfg(not(feature = "mq-admin"))]
|
||||||
@@ -344,6 +344,9 @@ async fn initializes_lists_tools_and_calls_a_tool() {
|
|||||||
assert!(names.contains(&"dbx_list_routines"));
|
assert!(names.contains(&"dbx_list_routines"));
|
||||||
assert!(names.contains(&"dbx_get_routine_source"));
|
assert!(names.contains(&"dbx_get_routine_source"));
|
||||||
assert!(names.contains(&"dbx_open_session"));
|
assert!(names.contains(&"dbx_open_session"));
|
||||||
|
assert!(names.contains(&"dbx_begin_transaction"));
|
||||||
|
assert!(names.contains(&"dbx_commit_transaction"));
|
||||||
|
assert!(names.contains(&"dbx_rollback_transaction"));
|
||||||
assert!(names.contains(&"dbx_close_session"));
|
assert!(names.contains(&"dbx_close_session"));
|
||||||
#[cfg(feature = "mq-admin")]
|
#[cfg(feature = "mq-admin")]
|
||||||
assert!(names.contains(&"dbx_send_message"));
|
assert!(names.contains(&"dbx_send_message"));
|
||||||
|
|||||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1 @@
|
|||||||
|
pub mod mysql_wire_proxy;
|
||||||
@@ -0,0 +1,386 @@
|
|||||||
|
use std::{
|
||||||
|
io,
|
||||||
|
net::SocketAddr,
|
||||||
|
sync::{
|
||||||
|
atomic::{AtomicBool, AtomicUsize, Ordering},
|
||||||
|
Arc,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
use tokio::{
|
||||||
|
io::{AsyncRead, AsyncReadExt, AsyncWriteExt},
|
||||||
|
net::{TcpListener, TcpStream},
|
||||||
|
sync::Mutex,
|
||||||
|
};
|
||||||
|
use tokio_util::sync::CancellationToken;
|
||||||
|
|
||||||
|
enum FaultDirective {
|
||||||
|
None,
|
||||||
|
DropCommitAck,
|
||||||
|
DropQueryResponse { marker: String },
|
||||||
|
}
|
||||||
|
|
||||||
|
enum PendingResponse {
|
||||||
|
Contention,
|
||||||
|
DropCommitAck,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct ProxyState {
|
||||||
|
fault: Mutex<FaultDirective>,
|
||||||
|
contention_marker: Mutex<Option<String>>,
|
||||||
|
contention_forwarded: AtomicBool,
|
||||||
|
contention_response_pending: AtomicBool,
|
||||||
|
commit_tracking: AtomicBool,
|
||||||
|
commit_forward_count: AtomicUsize,
|
||||||
|
commit_ok_observed: AtomicBool,
|
||||||
|
commit_ack_dropped: AtomicBool,
|
||||||
|
dropped_query_forwarded: AtomicBool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for ProxyState {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
fault: Mutex::new(FaultDirective::None),
|
||||||
|
contention_marker: Mutex::new(None),
|
||||||
|
contention_forwarded: AtomicBool::new(false),
|
||||||
|
contention_response_pending: AtomicBool::new(false),
|
||||||
|
commit_tracking: AtomicBool::new(false),
|
||||||
|
commit_forward_count: AtomicUsize::new(0),
|
||||||
|
commit_ok_observed: AtomicBool::new(false),
|
||||||
|
commit_ack_dropped: AtomicBool::new(false),
|
||||||
|
dropped_query_forwarded: AtomicBool::new(false),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct MysqlWireProxy {
|
||||||
|
listen_addr: SocketAddr,
|
||||||
|
state: Arc<ProxyState>,
|
||||||
|
cancellation: CancellationToken,
|
||||||
|
task: Option<tokio::task::JoinHandle<()>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MysqlWireProxy {
|
||||||
|
pub async fn start(target_host: String, target_port: u16) -> io::Result<Self> {
|
||||||
|
let listener = TcpListener::bind(("127.0.0.1", 0)).await?;
|
||||||
|
let listen_addr = listener.local_addr()?;
|
||||||
|
let state = Arc::new(ProxyState::default());
|
||||||
|
let cancellation = CancellationToken::new();
|
||||||
|
let task_state = state.clone();
|
||||||
|
let task_cancellation = cancellation.clone();
|
||||||
|
let task = tokio::spawn(async move {
|
||||||
|
loop {
|
||||||
|
let accepted = tokio::select! {
|
||||||
|
_ = task_cancellation.cancelled() => break,
|
||||||
|
accepted = listener.accept() => accepted,
|
||||||
|
};
|
||||||
|
let Ok((client, _)) = accepted else { break };
|
||||||
|
let target_host = target_host.clone();
|
||||||
|
let state = task_state.clone();
|
||||||
|
let cancellation = task_cancellation.child_token();
|
||||||
|
tokio::spawn(async move {
|
||||||
|
let Ok(server) = TcpStream::connect((target_host.as_str(), target_port)).await else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
let _ = proxy_connection(client, server, state, cancellation).await;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
Ok(Self { listen_addr, state, cancellation, task: Some(task) })
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn listen_addr(&self) -> SocketAddr {
|
||||||
|
self.listen_addr
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn set_contention_marker(&self, marker: impl Into<String>) {
|
||||||
|
*self.state.contention_marker.lock().await = Some(marker.into());
|
||||||
|
self.state.contention_forwarded.store(false, Ordering::SeqCst);
|
||||||
|
self.state.contention_response_pending.store(false, Ordering::SeqCst);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn contention_forwarded_and_pending(&self) -> bool {
|
||||||
|
self.state.contention_forwarded.load(Ordering::SeqCst)
|
||||||
|
&& self.state.contention_response_pending.load(Ordering::SeqCst)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn arm_drop_commit_ack(&self) {
|
||||||
|
*self.state.fault.lock().await = FaultDirective::DropCommitAck;
|
||||||
|
self.state.commit_tracking.store(true, Ordering::SeqCst);
|
||||||
|
self.state.commit_forward_count.store(0, Ordering::SeqCst);
|
||||||
|
self.state.commit_ok_observed.store(false, Ordering::SeqCst);
|
||||||
|
self.state.commit_ack_dropped.store(false, Ordering::SeqCst);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn commit_forward_count(&self) -> usize {
|
||||||
|
self.state.commit_forward_count.load(Ordering::SeqCst)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn commit_ok_observed(&self) -> bool {
|
||||||
|
self.state.commit_ok_observed.load(Ordering::SeqCst)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn commit_ack_dropped(&self) -> bool {
|
||||||
|
self.state.commit_ack_dropped.load(Ordering::SeqCst)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn arm_drop_query_response(&self, marker: impl Into<String>) {
|
||||||
|
*self.state.fault.lock().await = FaultDirective::DropQueryResponse { marker: marker.into() };
|
||||||
|
self.state.dropped_query_forwarded.store(false, Ordering::SeqCst);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn dropped_query_forwarded(&self) -> bool {
|
||||||
|
self.state.dropped_query_forwarded.load(Ordering::SeqCst)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn shutdown(mut self) {
|
||||||
|
self.cancellation.cancel();
|
||||||
|
if let Some(task) = self.task.take() {
|
||||||
|
let _ = task.await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Drop for MysqlWireProxy {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
self.cancellation.cancel();
|
||||||
|
if let Some(task) = self.task.take() {
|
||||||
|
task.abort();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn proxy_connection(
|
||||||
|
client: TcpStream,
|
||||||
|
server: TcpStream,
|
||||||
|
state: Arc<ProxyState>,
|
||||||
|
cancellation: CancellationToken,
|
||||||
|
) -> io::Result<()> {
|
||||||
|
let (mut client_reader, mut client_writer) = client.into_split();
|
||||||
|
let (mut server_reader, mut server_writer) = server.into_split();
|
||||||
|
let pending = Arc::new(Mutex::new(None::<PendingResponse>));
|
||||||
|
let client_pending = pending.clone();
|
||||||
|
let client_state = state.clone();
|
||||||
|
let client_cancellation = cancellation.clone();
|
||||||
|
let upstream = async move {
|
||||||
|
loop {
|
||||||
|
let packet = tokio::select! {
|
||||||
|
_ = client_cancellation.cancelled() => break,
|
||||||
|
packet = read_mysql_packet(&mut client_reader) => packet?,
|
||||||
|
};
|
||||||
|
let Some(packet) = packet else { break };
|
||||||
|
let sql = mysql_query_sql(&packet);
|
||||||
|
let contention = if let Some(sql) = sql {
|
||||||
|
client_state.contention_marker.lock().await.as_deref().is_some_and(|marker| sql.contains(marker))
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
};
|
||||||
|
if contention {
|
||||||
|
client_state.contention_forwarded.store(true, Ordering::SeqCst);
|
||||||
|
client_state.contention_response_pending.store(true, Ordering::SeqCst);
|
||||||
|
*client_pending.lock().await = Some(PendingResponse::Contention);
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut drop_after_forward = false;
|
||||||
|
if let Some(sql) = sql {
|
||||||
|
if sql.trim().eq_ignore_ascii_case("COMMIT") && client_state.commit_tracking.load(Ordering::SeqCst) {
|
||||||
|
client_state.commit_forward_count.fetch_add(1, Ordering::SeqCst);
|
||||||
|
}
|
||||||
|
let mut fault = client_state.fault.lock().await;
|
||||||
|
match &*fault {
|
||||||
|
FaultDirective::DropCommitAck if sql.trim().eq_ignore_ascii_case("COMMIT") => {
|
||||||
|
*client_pending.lock().await = Some(PendingResponse::DropCommitAck);
|
||||||
|
*fault = FaultDirective::None;
|
||||||
|
}
|
||||||
|
FaultDirective::DropQueryResponse { marker } if sql.contains(marker) => {
|
||||||
|
client_state.dropped_query_forwarded.store(true, Ordering::SeqCst);
|
||||||
|
drop_after_forward = true;
|
||||||
|
*fault = FaultDirective::None;
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
server_writer.write_all(&packet).await?;
|
||||||
|
if drop_after_forward {
|
||||||
|
client_cancellation.cancel();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
io::Result::Ok(())
|
||||||
|
};
|
||||||
|
|
||||||
|
let downstream_state = state;
|
||||||
|
let downstream_cancellation = cancellation.clone();
|
||||||
|
let downstream = async move {
|
||||||
|
loop {
|
||||||
|
let packet = tokio::select! {
|
||||||
|
_ = downstream_cancellation.cancelled() => break,
|
||||||
|
packet = read_mysql_packet(&mut server_reader) => packet?,
|
||||||
|
};
|
||||||
|
let Some(packet) = packet else { break };
|
||||||
|
match pending.lock().await.take() {
|
||||||
|
Some(PendingResponse::Contention) => {
|
||||||
|
downstream_state.contention_response_pending.store(false, Ordering::SeqCst);
|
||||||
|
}
|
||||||
|
Some(PendingResponse::DropCommitAck) if packet.get(4) == Some(&0x00) => {
|
||||||
|
downstream_state.commit_ok_observed.store(true, Ordering::SeqCst);
|
||||||
|
downstream_state.commit_ack_dropped.store(true, Ordering::SeqCst);
|
||||||
|
downstream_cancellation.cancel();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
Some(PendingResponse::DropCommitAck) | None => {}
|
||||||
|
}
|
||||||
|
client_writer.write_all(&packet).await?;
|
||||||
|
}
|
||||||
|
io::Result::Ok(())
|
||||||
|
};
|
||||||
|
|
||||||
|
let (upstream, downstream) = tokio::join!(upstream, downstream);
|
||||||
|
upstream?;
|
||||||
|
downstream?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn read_mysql_packet(reader: &mut (impl AsyncRead + Unpin)) -> io::Result<Option<Vec<u8>>> {
|
||||||
|
let mut header = [0_u8; 4];
|
||||||
|
match reader.read(&mut header[..1]).await? {
|
||||||
|
0 => return Ok(None),
|
||||||
|
1 => {
|
||||||
|
reader.read_exact(&mut header[1..]).await?;
|
||||||
|
}
|
||||||
|
_ => unreachable!("one-byte read returned more than one byte"),
|
||||||
|
}
|
||||||
|
let payload_len = usize::from(header[0]) | (usize::from(header[1]) << 8) | (usize::from(header[2]) << 16);
|
||||||
|
let mut packet = Vec::with_capacity(4 + payload_len);
|
||||||
|
packet.extend_from_slice(&header);
|
||||||
|
packet.resize(4 + payload_len, 0);
|
||||||
|
reader.read_exact(&mut packet[4..]).await?;
|
||||||
|
Ok(Some(packet))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn mysql_query_sql(packet: &[u8]) -> Option<&str> {
|
||||||
|
let header: [u8; 4] = packet.get(..4)?.try_into().ok()?;
|
||||||
|
let payload_len = usize::from(header[0]) | (usize::from(header[1]) << 8) | (usize::from(header[2]) << 16);
|
||||||
|
if packet.len() != 4 + payload_len || packet.get(4) != Some(&0x03) {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
std::str::from_utf8(packet.get(5..)?).ok()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::{mysql_query_sql, read_mysql_packet, MysqlWireProxy};
|
||||||
|
|
||||||
|
fn packet(sequence: u8, payload: &[u8]) -> Vec<u8> {
|
||||||
|
let length = payload.len();
|
||||||
|
let mut encoded =
|
||||||
|
vec![(length & 0xff) as u8, ((length >> 8) & 0xff) as u8, ((length >> 16) & 0xff) as u8, sequence];
|
||||||
|
encoded.extend_from_slice(payload);
|
||||||
|
encoded
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn packet_reader_handles_fragmented_headers_and_coalesced_packets() {
|
||||||
|
let first = packet(0, &[0x03, b'S', b'E', b'L', b'E', b'C', b'T', b' ', b'1']);
|
||||||
|
let second = packet(1, &[0x00, 0x01]);
|
||||||
|
let bytes = [first.clone(), second.clone()].concat();
|
||||||
|
let (mut writer, mut reader) = tokio::io::duplex(64);
|
||||||
|
let sender = tokio::spawn(async move {
|
||||||
|
use tokio::io::AsyncWriteExt;
|
||||||
|
for byte in bytes {
|
||||||
|
writer.write_all(&[byte]).await.unwrap();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let first_read = read_mysql_packet(&mut reader).await.unwrap().unwrap();
|
||||||
|
let second_read = read_mysql_packet(&mut reader).await.unwrap().unwrap();
|
||||||
|
assert_eq!(first_read, first);
|
||||||
|
assert_eq!(second_read, second);
|
||||||
|
assert!(read_mysql_packet(&mut reader).await.unwrap().is_none());
|
||||||
|
sender.await.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn query_decoder_accepts_only_complete_com_query_payloads() {
|
||||||
|
assert_eq!(mysql_query_sql(&packet(0, b"\x03COMMIT")), Some("COMMIT"));
|
||||||
|
assert_eq!(mysql_query_sql(&packet(0, b"\x03SELECT 1")), Some("SELECT 1"));
|
||||||
|
assert_eq!(mysql_query_sql(&packet(0, b"\x01COMMIT")), None);
|
||||||
|
assert_eq!(mysql_query_sql(&[7, 0, 0, 0, 0x03, b'C']), None);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn proxy_forwards_commit_observes_ok_and_drops_only_the_downstream_ack() {
|
||||||
|
use tokio::io::AsyncWriteExt;
|
||||||
|
|
||||||
|
let upstream_listener = tokio::net::TcpListener::bind(("127.0.0.1", 0)).await.unwrap();
|
||||||
|
let upstream_address = upstream_listener.local_addr().unwrap();
|
||||||
|
let proxy = MysqlWireProxy::start(upstream_address.ip().to_string(), upstream_address.port()).await.unwrap();
|
||||||
|
proxy.arm_drop_commit_ack().await;
|
||||||
|
let upstream = tokio::spawn(async move {
|
||||||
|
let (mut socket, _) = upstream_listener.accept().await.unwrap();
|
||||||
|
assert_eq!(read_mysql_packet(&mut socket).await.unwrap().unwrap(), packet(0, b"\x03COMMIT"));
|
||||||
|
socket.write_all(&packet(1, &[0x00, 0x00, 0x00])).await.unwrap();
|
||||||
|
});
|
||||||
|
|
||||||
|
let mut client = tokio::net::TcpStream::connect(proxy.listen_addr()).await.unwrap();
|
||||||
|
client.write_all(&packet(0, b"\x03COMMIT")).await.unwrap();
|
||||||
|
assert!(read_mysql_packet(&mut client).await.unwrap().is_none());
|
||||||
|
upstream.await.unwrap();
|
||||||
|
assert_eq!(proxy.commit_forward_count(), 1);
|
||||||
|
assert!(proxy.commit_ok_observed());
|
||||||
|
assert!(proxy.commit_ack_dropped());
|
||||||
|
proxy.shutdown().await;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn commit_counter_tracks_every_forward_after_the_fault_is_consumed() {
|
||||||
|
use tokio::io::AsyncWriteExt;
|
||||||
|
|
||||||
|
let upstream_listener = tokio::net::TcpListener::bind(("127.0.0.1", 0)).await.unwrap();
|
||||||
|
let upstream_address = upstream_listener.local_addr().unwrap();
|
||||||
|
let proxy = MysqlWireProxy::start(upstream_address.ip().to_string(), upstream_address.port()).await.unwrap();
|
||||||
|
proxy.arm_drop_commit_ack().await;
|
||||||
|
let upstream = tokio::spawn(async move {
|
||||||
|
for _ in 0..2 {
|
||||||
|
let (mut socket, _) = upstream_listener.accept().await.unwrap();
|
||||||
|
assert_eq!(read_mysql_packet(&mut socket).await.unwrap().unwrap(), packet(0, b"\x03COMMIT"));
|
||||||
|
socket.write_all(&packet(1, &[0x00, 0x00, 0x00])).await.unwrap();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
let mut first = tokio::net::TcpStream::connect(proxy.listen_addr()).await.unwrap();
|
||||||
|
first.write_all(&packet(0, b"\x03COMMIT")).await.unwrap();
|
||||||
|
assert!(read_mysql_packet(&mut first).await.unwrap().is_none());
|
||||||
|
|
||||||
|
let mut second = tokio::net::TcpStream::connect(proxy.listen_addr()).await.unwrap();
|
||||||
|
second.write_all(&packet(0, b"\x03COMMIT")).await.unwrap();
|
||||||
|
assert!(read_mysql_packet(&mut second).await.unwrap().is_some());
|
||||||
|
|
||||||
|
upstream.await.unwrap();
|
||||||
|
assert_eq!(proxy.commit_forward_count(), 2);
|
||||||
|
proxy.shutdown().await;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test]
|
||||||
|
async fn dropping_proxy_releases_its_listener() {
|
||||||
|
let upstream_listener = tokio::net::TcpListener::bind(("127.0.0.1", 0)).await.unwrap();
|
||||||
|
let upstream_address = upstream_listener.local_addr().unwrap();
|
||||||
|
let proxy = MysqlWireProxy::start(upstream_address.ip().to_string(), upstream_address.port()).await.unwrap();
|
||||||
|
let proxy_address = proxy.listen_addr();
|
||||||
|
|
||||||
|
drop(proxy);
|
||||||
|
|
||||||
|
let rebound = tokio::time::timeout(std::time::Duration::from_secs(1), async {
|
||||||
|
loop {
|
||||||
|
match tokio::net::TcpListener::bind(proxy_address).await {
|
||||||
|
Ok(listener) => break listener,
|
||||||
|
Err(error) if error.kind() == std::io::ErrorKind::AddrInUse => tokio::task::yield_now().await,
|
||||||
|
Err(error) => panic!("unexpected listener rebind error: {error}"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
|
||||||
|
assert!(rebound.is_ok(), "dropping the proxy must release its listener");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -104,6 +104,9 @@ DBX MCP 当前提供以下工具(具体可用工具取决于构建功能及权
|
|||||||
| `dbx_execute_query` | 执行 SQL 或支持的 MongoDB shell 命令,默认返回 100 行,可用 `max_rows` 参数提高到最多 1000 行 |
|
| `dbx_execute_query` | 执行 SQL 或支持的 MongoDB shell 命令,默认返回 100 行,可用 `max_rows` 参数提高到最多 1000 行 |
|
||||||
| `dbx_execute_batch` | 一次执行包含多条语句的 SQL 脚本,按语句返回结果(多语句脚本搭配 `use_transaction` 时返回单个合并结果) |
|
| `dbx_execute_batch` | 一次执行包含多条语句的 SQL 脚本,按语句返回结果(多语句脚本搭配 `use_transaction` 时返回单个合并结果) |
|
||||||
| `dbx_open_session` | 为 SQL 连接打开固定后端连接的有状态查询会话 |
|
| `dbx_open_session` | 为 SQL 连接打开固定后端连接的有状态查询会话 |
|
||||||
|
| `dbx_begin_transaction` | 在已启用事务的原生 MySQL 会话上开始事务 |
|
||||||
|
| `dbx_commit_transaction` | 在 MySQL 确认 `COMMIT` 后提交当前会话事务 |
|
||||||
|
| `dbx_rollback_transaction` | 回滚当前会话事务 |
|
||||||
| `dbx_close_session` | 关闭会话并释放固定连接资源 |
|
| `dbx_close_session` | 关闭会话并释放固定连接资源 |
|
||||||
| `dbx_execute_redis_command` | 执行 Redis 命令 |
|
| `dbx_execute_redis_command` | 执行 Redis 命令 |
|
||||||
| `dbx_peek_messages` | 读取 Kafka 消息,不提交消费位点 |
|
| `dbx_peek_messages` | 读取 Kafka 消息,不提交消费位点 |
|
||||||
@@ -163,6 +166,16 @@ DBX MCP 当前提供以下工具(具体可用工具取决于构建功能及权
|
|||||||
|
|
||||||
<Callout type="warn">有状态会话不会放宽 SQL 策略。`USE` 只有在会话中才有意义,但写入、DDL、生产保护、连接只读和数据库权限仍按每次请求重新检查。</Callout>
|
<Callout type="warn">有状态会话不会放宽 SQL 策略。`USE` 只有在会话中才有意义,但写入、DDL、生产保护、连接只读和数据库权限仍按每次请求重新检查。</Callout>
|
||||||
|
|
||||||
|
### 原生 MySQL 显式事务
|
||||||
|
|
||||||
|
调用 `dbx_open_session` 时传入 `enable_transactions: true`,DBX 会立即保留一条物理原生 MySQL 连接。随后依次调用 `dbx_begin_transaction`、带该 `session_id` 的一个或多个 `dbx_execute_query`/`dbx_execute_batch`,最后调用 `dbx_commit_transaction` 或 `dbx_rollback_transaction`。事务前后的查询仍使用同一条受管连接。`use_transaction` 仍不能与 `session_id` 同时使用;它是前文所述的独立原子批处理功能。
|
||||||
|
|
||||||
|
该 opt-in 模式仅支持本地原生 MySQL 连接。DBX Web/`DBX_WEB_URL`、外部驱动 profile、兼容引擎及其他数据库类型会返回 `TRANSACTION_UNSUPPORTED`,不会静默退化为自动提交。事务会话每次操作只接受一条经过解析的 `SELECT`(含锁定读)、`INSERT`、`UPDATE`、`DELETE` 或 `REPLACE`;原始事务控制、DDL/临时 DDL、`CALL`、`LOAD`、表锁、`SET`、`USE`、XA、文件/输出子句、可执行注释、优化器 hint 及多语句均会被拒绝。完整批次持有同一个操作槽位,并为每条语句返回执行后的事务状态。
|
||||||
|
|
||||||
|
响应包含 `transaction_state`(`idle`、`active` 或 `unknown`),并在适用时包含 `transaction_outcome`(`committed`、`rolled_back` 或 `unknown`)。普通 MySQL 语句错误可能仍让事务保持 active;DBX 会在同一连接上重新探测服务器状态后再返回。超时、取消、传输故障或丢失 `COMMIT` 确认会把结果永久标为 `unknown`,禁止该会话继续执行 SQL,并在不重放语句或 `COMMIT` 的前提下销毁连接。显式正值的连接查询超时同样用于事务操作;当连接配置为“无限制”(`0`)时,事务 owner 仍使用默认 300 秒操作上限,确保 busy 操作保持有界。关闭、过期、协议会话删除和服务退出会对状态已知的 active 事务执行有界回滚,然后销毁连接。
|
||||||
|
|
||||||
|
<Callout type="warn">回滚保证依赖 InnoDB 等事务型存储。触发器、存储函数、非事务表及外部副作用可能产生 MySQL 无法回滚的效果。DBX 会在排队完成后重新检查当前连接/数据库范围、工具 allowlist、只读与生产保护以及已确认 SQL。即使写权限被撤销,仍允许原会话执行回滚和关闭以完成清理。</Callout>
|
||||||
|
|
||||||
## 数据库访问
|
## 数据库访问
|
||||||
|
|
||||||
### DBX Desktop 原生 Streamable HTTP
|
### DBX Desktop 原生 Streamable HTTP
|
||||||
|
|||||||
@@ -104,6 +104,9 @@ DBX MCP provides the following tools (availability depends on build features and
|
|||||||
| `dbx_execute_query` | Execute SQL or a supported MongoDB shell command, returning 100 rows by default (up to 1000 with the `max_rows` parameter) |
|
| `dbx_execute_query` | Execute SQL or a supported MongoDB shell command, returning 100 rows by default (up to 1000 with the `max_rows` parameter) |
|
||||||
| `dbx_execute_batch` | Execute a SQL script containing multiple statements in one call, returning a result per statement (or a single merged result with `use_transaction` on a multi-statement script) |
|
| `dbx_execute_batch` | Execute a SQL script containing multiple statements in one call, returning a result per statement (or a single merged result with `use_transaction` on a multi-statement script) |
|
||||||
| `dbx_open_session` | Open a stateful SQL query session pinned to one backend connection |
|
| `dbx_open_session` | Open a stateful SQL query session pinned to one backend connection |
|
||||||
|
| `dbx_begin_transaction` | Begin a transaction on a transaction-enabled native MySQL session |
|
||||||
|
| `dbx_commit_transaction` | Commit the active session transaction after MySQL acknowledges `COMMIT` |
|
||||||
|
| `dbx_rollback_transaction` | Roll back the active session transaction |
|
||||||
| `dbx_close_session` | Close a session and release its pinned connection resources |
|
| `dbx_close_session` | Close a session and release its pinned connection resources |
|
||||||
| `dbx_execute_redis_command` | Execute a Redis command |
|
| `dbx_execute_redis_command` | Execute a Redis command |
|
||||||
| `dbx_peek_messages` | Read Kafka messages without committing consumer offsets |
|
| `dbx_peek_messages` | Read Kafka messages without committing consumer offsets |
|
||||||
@@ -163,6 +166,16 @@ Sessions support SQL connections only and are bound to one connection and databa
|
|||||||
|
|
||||||
<Callout type="warn">A stateful session does not widen SQL policy. `USE` is meaningful only inside a session, while writes, DDL, production protection, connection read-only state, and database privileges are rechecked for every request.</Callout>
|
<Callout type="warn">A stateful session does not widen SQL policy. `USE` is meaningful only inside a session, while writes, DDL, production protection, connection read-only state, and database privileges are rechecked for every request.</Callout>
|
||||||
|
|
||||||
|
### Explicit native MySQL transactions
|
||||||
|
|
||||||
|
Set `enable_transactions: true` when calling `dbx_open_session` to reserve one physical native MySQL connection eagerly. Then use `dbx_begin_transaction`, one or more `dbx_execute_query` or `dbx_execute_batch` calls with the returned `session_id`, and finally `dbx_commit_transaction` or `dbx_rollback_transaction`. Queries before and after a transaction still use the same owned connection. `use_transaction` remains incompatible with `session_id`; it is the separate atomic-batch feature described above.
|
||||||
|
|
||||||
|
This opt-in mode is available only for local native MySQL connections. DBX Web/`DBX_WEB_URL`, external driver profiles, compatibility engines, and other database types return `TRANSACTION_UNSUPPORTED` instead of falling back to auto-commit. Transaction-session SQL accepts one parsed `SELECT` (including locking reads), `INSERT`, `UPDATE`, `DELETE`, or `REPLACE` statement per operation. It rejects raw transaction control, DDL and temporary DDL, `CALL`, `LOAD`, table locks, `SET`, `USE`, XA, file/output clauses, executable comments, optimizer hints, and multi-statements. Complete batches hold one operation slot and report the resulting transaction state for every statement.
|
||||||
|
|
||||||
|
Responses include `transaction_state` (`idle`, `active`, or `unknown`) and, when known, `transaction_outcome` (`committed`, `rolled_back`, or `unknown`). A normal MySQL statement error can leave the transaction active; DBX probes fresh state on the same connection before reporting it. A timeout, cancellation, transport failure, or lost `COMMIT` acknowledgement makes the outcome `unknown`, permanently blocks more SQL on that session, and disposes the connection without replaying the statement or `COMMIT`. A positive connection query timeout applies to transaction operations; when the connection is configured as unlimited (`0`), transaction owners still apply their default 300-second operation cap so busy work remains bounded. Closing, expiry, protocol-session deletion, and server shutdown attempt bounded rollback for a known active transaction and then dispose the connection.
|
||||||
|
|
||||||
|
<Callout type="warn">Rollback guarantees require transactional storage such as InnoDB. Triggers, stored functions, nontransactional tables, and external side effects can have effects that MySQL cannot roll back. DBX rechecks the current connection/database scope, tool allowlist, read-only and production protections, and confirmed SQL after queue waiting. Rollback and close remain available for cleanup after write permission is revoked.</Callout>
|
||||||
|
|
||||||
## Database Access
|
## Database Access
|
||||||
|
|
||||||
### Native Streamable HTTP for DBX Desktop
|
### Native Streamable HTTP for DBX Desktop
|
||||||
|
|||||||
Reference in new issue
Block a user