fix(core): avoid holding pool registry locks during db io

This commit is contained in:
Guoyu Su authored and GitHub committed 2026-09-03 01:53:42 +08:00
1 parent 616c0daf25
commit d6fc1fe52c
30 files changed
+1436 -1110

No files matched your search

+14 -10
View File
@@ -38,8 +38,8 @@ pub async fn get_agent_explain_info_core(
} }
let target = { let target = {
let connections = state.connections.read().await; let pool_handle = state.pool_handle(&pool_key).await;
let pool = connections.get(&pool_key).ok_or_else(|| "Connection not found".to_string())?; let pool = pool_handle.as_ref().ok_or_else(|| "Connection not found".to_string())?;
match pool { match pool {
PoolKind::Agent(client) => ExplainTarget::Agent(client.clone()), PoolKind::Agent(client) => ExplainTarget::Agent(client.clone()),
PoolKind::ExternalDriver { config, session, .. } => { PoolKind::ExternalDriver { config, session, .. } => {
@@ -227,14 +227,18 @@ mod tests {
let storage = crate::storage::Storage::open(&dir.join("storage.db")).await.unwrap(); let storage = crate::storage::Storage::open(&dir.join("storage.db")).await.unwrap();
let state = AppState::new(storage); let state = AppState::new(storage);
state.configs.write().await.insert(config.id.clone(), config.clone()); state.configs.write().await.insert(config.id.clone(), config.clone());
state.connections.write().await.insert( state
"oracle-jdbc".to_string(), .update_connection_pools(|connections| {
PoolKind::ExternalDriver { connections.insert(
driver_id: "jdbc".to_string(), "oracle-jdbc".to_string(),
config: Arc::new(config), PoolKind::ExternalDriver {
session: session.clone(), driver_id: "jdbc".to_string(),
}, config: Arc::new(config),
); session: session.clone(),
},
);
})
.await;
let plan = get_agent_explain_info_core( let plan = get_agent_explain_info_core(
&state, &state,
+6 -6
View File
@@ -1013,8 +1013,8 @@ async fn ensure_etcd_dangerous_action_capability(
.get(connection_id) .get(connection_id)
.and_then(|config| crate::agent_catalog::agent_key(&config.db_type, config.driver_profile.as_deref())) .and_then(|config| crate::agent_catalog::agent_key(&config.db_type, config.driver_profile.as_deref()))
}; };
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Connection not found")?;
let PoolKind::Agent(client) = connections.get(connection_id).ok_or("Connection not found")? else { let PoolKind::Agent(client) = &pool else {
return Err("Not an agent key-value connection".to_string()); return Err("Not an agent key-value connection".to_string());
}; };
if !client.lock().await.supports_capability(capability) { if !client.lock().await.supports_capability(capability) {
@@ -1706,8 +1706,8 @@ fn kv_put_required_capabilities(options: &KvPutOptions) -> Vec<AgentCapability>
pub async fn kv_supports_ttl_core(state: &AppState, connection_id: &str) -> Result<bool, String> { pub async fn kv_supports_ttl_core(state: &AppState, connection_id: &str) -> Result<bool, String> {
ensure_agent_kv_pool(state, connection_id).await?; ensure_agent_kv_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool_handle = state.pool_handle(connection_id).await;
let pool = connections.get(connection_id).ok_or("Connection not found")?; let pool = pool_handle.as_ref().ok_or("Connection not found")?;
match pool { match pool {
PoolKind::Agent(client) => Ok(client.lock().await.supports_capability(AgentCapability::KvTtl)), PoolKind::Agent(client) => Ok(client.lock().await.supports_capability(AgentCapability::KvTtl)),
_ => Err("Not an agent key-value connection".to_string()), _ => Err("Not an agent key-value connection".to_string()),
@@ -1738,8 +1738,8 @@ async fn call_agent_kv<T: serde::de::DeserializeOwned + Send + 'static>(
.and_then(|config| crate::agent_catalog::agent_key(&config.db_type, config.driver_profile.as_deref())) .and_then(|config| crate::agent_catalog::agent_key(&config.db_type, config.driver_profile.as_deref()))
}; };
let client = { let client = {
let connections = state.connections.read().await; let pool_handle = state.pool_handle(connection_id).await;
match connections.get(connection_id) { match pool_handle.as_ref() {
Some(PoolKind::Agent(client)) => client.clone(), Some(PoolKind::Agent(client)) => client.clone(),
Some(_) => return Err("Not an agent key-value connection".to_string()), Some(_) => return Err("Not an agent key-value connection".to_string()),
None => return Err("Connection not found".to_string()), None => return Err("Connection not found".to_string()),
+10 -2
View File
@@ -1872,7 +1872,11 @@ for line in sys.stdin:
let state = Arc::new(AppState::new(storage)); let state = Arc::new(AppState::new(storage));
let connection = agent_test_connection("dameng-1", "Dameng", DatabaseType::Dameng, "APPDB"); let connection = agent_test_connection("dameng-1", "Dameng", DatabaseType::Dameng, "APPDB");
state.configs.write().await.insert(connection.id.clone(), connection); state.configs.write().await.insert(connection.id.clone(), connection);
state.connections.write().await.insert("dameng-1:APPDB".to_string(), PoolKind::agent(client)); state
.update_connection_pools(|connections| {
connections.insert("dameng-1:APPDB".to_string(), PoolKind::agent(client));
})
.await;
let read = ToolCall { let read = ToolCall {
id: "read".to_string(), id: "read".to_string(),
@@ -1936,7 +1940,11 @@ for line in sys.stdin:
let state = Arc::new(AppState::new(storage)); let state = Arc::new(AppState::new(storage));
let connection = agent_test_connection("mysql-1", "MySQL", DatabaseType::Mysql, "rs_main"); let connection = agent_test_connection("mysql-1", "MySQL", DatabaseType::Mysql, "rs_main");
state.configs.write().await.insert(connection.id.clone(), connection); state.configs.write().await.insert(connection.id.clone(), connection);
state.connections.write().await.insert("mysql-1:rs_main".to_string(), PoolKind::agent(client)); state
.update_connection_pools(|connections| {
connections.insert("mysql-1:rs_main".to_string(), PoolKind::agent(client));
})
.await;
let sql = "SHOW TRIGGERS FROM `rs_main` LIKE 'trg_order_items_after_%';"; let sql = "SHOW TRIGGERS FROM `rs_main` LIKE 'trg_order_items_after_%';";
let call = ToolCall { let call = ToolCall {
File diff suppressed because it is too large. Load diff
+2 -2
View File
@@ -224,8 +224,8 @@ impl ConsulClient {
pub(super) async fn client_for_state(state: &AppState, connection_id: &str) -> Result<ConsulClient, String> { pub(super) async fn client_for_state(state: &AppState, connection_id: &str) -> Result<ConsulClient, String> {
state.get_or_create_pool(connection_id, None).await?; state.get_or_create_pool(connection_id, None).await?;
let connections = state.connections.read().await; let pool_handle = state.pool_handle(connection_id).await;
match connections.get(connection_id) { match pool_handle.as_ref() {
Some(PoolKind::Consul(client)) => Ok(client.clone()), Some(PoolKind::Consul(client)) => Ok(client.clone()),
Some(_) => Err("Connection is not a Consul connection".to_string()), Some(_) => Err("Connection is not a Consul connection".to_string()),
None => Err("Connection not found".to_string()), None => Err("Connection not found".to_string()),
+6 -5
View File
@@ -1484,8 +1484,8 @@ async fn list_postgres_extension_members(
schema: &str, schema: &str,
) -> Result<PostgresExtensionMembers, String> { ) -> Result<PostgresExtensionMembers, String> {
let pool = { let pool = {
let connections = state.connections.read().await; let pool_handle = state.pool_handle(pool_key).await;
match connections.get(pool_key) { match pool_handle.as_ref() {
Some(crate::connection::PoolKind::Postgres(pool)) => pool.clone(), Some(crate::connection::PoolKind::Postgres(pool)) => pool.clone(),
_ => return Ok(PostgresExtensionMembers::default()), _ => return Ok(PostgresExtensionMembers::default()),
} }
@@ -1515,8 +1515,8 @@ async fn list_postgres_export_sequences(
fail_on_error: bool, fail_on_error: bool,
) -> Result<Vec<PostgresExportSequence>, String> { ) -> Result<Vec<PostgresExportSequence>, String> {
let pool = { let pool = {
let connections = state.connections.read().await; let pool_handle = state.pool_handle(pool_key).await;
match connections.get(pool_key) { match pool_handle.as_ref() {
Some(crate::connection::PoolKind::Postgres(pool)) => pool.clone(), Some(crate::connection::PoolKind::Postgres(pool)) => pool.clone(),
_ => return Ok(Vec::new()), _ => return Ok(Vec::new()),
} }
@@ -2718,7 +2718,8 @@ async fn export_database_sql_core_inner(
let concurrent_prefetch_is_safe = let concurrent_prefetch_is_safe =
match state.get_or_create_pool(&request.connection_id, Some(&request.database)).await { match state.get_or_create_pool(&request.connection_id, Some(&request.database)).await {
Ok(metadata_pool_key) => { Ok(metadata_pool_key) => {
concurrent_metadata_prefetch_allowed(state.connections.read().await.get(&metadata_pool_key)) let pool = state.pool_handle(&metadata_pool_key).await;
concurrent_metadata_prefetch_allowed(pool.as_ref())
} }
// 建池失败时不预取,让写出循环的直查路径按原有方式报告错误 // 建池失败时不预取,让写出循环的直查路径按原有方式报告错误
Err(_) => false, Err(_) => false,
+1
View File
@@ -60,6 +60,7 @@ impl MySqlPool {
/// generation. Pool options are not an identity: a reconnect creates a new /// generation. Pool options are not an identity: a reconnect creates a new
/// pool with identical options, and a late health probe for the old pool /// pool with identical options, and a late health probe for the old pool
/// must not be allowed to remove that replacement from routing. /// must not be allowed to remove that replacement from routing.
#[cfg(test)]
pub(crate) fn is_same_pool(&self, other: &Self) -> bool { pub(crate) fn is_same_pool(&self, other: &Self) -> bool {
std::sync::Arc::ptr_eq(&self.inner.metrics(), &other.inner.metrics()) std::sync::Arc::ptr_eq(&self.inner.metrics(), &other.inner.metrics())
} }
+16
View File
@@ -305,6 +305,22 @@ pub enum RedisConnection {
Cluster(RedisClusterPool), Cluster(RedisClusterPool),
} }
#[cfg(test)]
pub(crate) fn redis_connection_test_stub() -> RedisConnection {
RedisConnection::Cluster(RedisClusterPool {
connection: None,
seed_nodes: Vec::new(),
seed_routes: Vec::new(),
slot_ranges: Vec::new(),
node_routes: Vec::new(),
tls: false,
tls_insecure: false,
username: String::new(),
password: String::new(),
scan_sessions: Box::new(Mutex::new(RedisClusterScanSessions::default())),
})
}
pub struct RedisClusterPool { pub struct RedisClusterPool {
pub connection: Option<Mutex<ClusterConnection>>, pub connection: Option<Mutex<ClusterConnection>>,
pub seed_nodes: Vec<RedisNodeEndpoint>, pub seed_nodes: Vec<RedisNodeEndpoint>,
+61 -102
View File
@@ -54,8 +54,8 @@ async fn ensure_document_pool(state: &AppState, connection_id: &str) -> Result<(
pub async fn list_databases_core(state: &AppState, connection_id: &str) -> Result<Vec<String>, String> { pub async fn list_databases_core(state: &AppState, connection_id: &str) -> Result<Vec<String>, String> {
ensure_document_pool(state, connection_id).await?; ensure_document_pool(state, connection_id).await?;
let fallback_database = configured_mongo_database(state, connection_id).await; let fallback_database = configured_mongo_database(state, connection_id).await;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::MongoDb(client) => match mongo_driver::list_databases(client).await { PoolKind::MongoDb(client) => match mongo_driver::list_databases(client).await {
Ok(databases) => Ok(sort_names(databases)), Ok(databases) => Ok(sort_names(databases)),
Err(error) if mongo_list_databases_unauthorized(&error) => { Err(error) if mongo_list_databases_unauthorized(&error) => {
@@ -247,8 +247,8 @@ pub async fn list_collections_core(
database: &str, database: &str,
) -> Result<Vec<CollectionInfo>, String> { ) -> Result<Vec<CollectionInfo>, String> {
ensure_document_pool(state, connection_id).await?; ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::MongoDb(client) => { PoolKind::MongoDb(client) => {
let specs = sort_mongo_collection_specs(mongo_driver::list_collection_specs(client, database).await?); let specs = sort_mongo_collection_specs(mongo_driver::list_collection_specs(client, database).await?);
let names: Vec<String> = specs.iter().map(|spec| spec.name.clone()).collect(); let names: Vec<String> = specs.iter().map(|spec| spec.name.clone()).collect();
@@ -258,7 +258,6 @@ pub async fn list_collections_core(
} }
PoolKind::DynamoDb(client) => { PoolKind::DynamoDb(client) => {
let client = client.clone(); let client = client.clone();
drop(connections);
let names = dynamodb_driver::list_tables(&client).await?; let names = dynamodb_driver::list_tables(&client).await?;
Ok(names Ok(names
.into_iter() .into_iter()
@@ -306,8 +305,8 @@ pub async fn list_gridfs_files_core(
sort: Option<&str>, sort: Option<&str>,
) -> Result<Vec<MongoGridFsFileInfo>, String> { ) -> Result<Vec<MongoGridFsFileInfo>, String> {
ensure_document_pool(state, connection_id).await?; ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::MongoDb(client) => mongo_driver::list_gridfs_files(client, database, bucket, filter, sort).await, PoolKind::MongoDb(client) => mongo_driver::list_gridfs_files(client, database, bucket, filter, sort).await,
PoolKind::Agent(_) => Err("MongoDB legacy agent does not support GridFS file browsing".to_string()), PoolKind::Agent(_) => Err("MongoDB legacy agent does not support GridFS file browsing".to_string()),
_ => Err("Not a MongoDB connection".to_string()), _ => Err("Not a MongoDB connection".to_string()),
@@ -322,8 +321,8 @@ pub async fn list_gridfs_buckets_core(
sort: Option<&str>, sort: Option<&str>,
) -> Result<Vec<MongoGridFsBucketInfo>, String> { ) -> Result<Vec<MongoGridFsBucketInfo>, String> {
ensure_document_pool(state, connection_id).await?; ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::MongoDb(client) => { PoolKind::MongoDb(client) => {
let names = sort_names(mongo_driver::list_collections(client, database).await?); let names = sort_names(mongo_driver::list_collections(client, database).await?);
let bucket_names = mongo_gridfs_bucket_names(&names); let bucket_names = mongo_gridfs_bucket_names(&names);
@@ -345,8 +344,8 @@ pub async fn create_gridfs_bucket_core(
bucket: &str, bucket: &str,
) -> Result<(), String> { ) -> Result<(), String> {
ensure_document_pool(state, connection_id).await?; ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::MongoDb(client) => mongo_driver::create_gridfs_bucket(client, database, bucket).await, PoolKind::MongoDb(client) => mongo_driver::create_gridfs_bucket(client, database, bucket).await,
PoolKind::Agent(_) => Err("MongoDB legacy agent does not support GridFS bucket creation".to_string()), PoolKind::Agent(_) => Err("MongoDB legacy agent does not support GridFS bucket creation".to_string()),
_ => Err("Not a MongoDB connection".to_string()), _ => Err("Not a MongoDB connection".to_string()),
@@ -360,8 +359,8 @@ pub async fn delete_gridfs_bucket_core(
bucket: &str, bucket: &str,
) -> Result<(), String> { ) -> Result<(), String> {
ensure_document_pool(state, connection_id).await?; ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::MongoDb(client) => mongo_driver::delete_gridfs_bucket(client, database, bucket).await, PoolKind::MongoDb(client) => mongo_driver::delete_gridfs_bucket(client, database, bucket).await,
PoolKind::Agent(_) => Err("MongoDB legacy agent does not support GridFS bucket deletion".to_string()), PoolKind::Agent(_) => Err("MongoDB legacy agent does not support GridFS bucket deletion".to_string()),
_ => Err("Not a MongoDB connection".to_string()), _ => Err("Not a MongoDB connection".to_string()),
@@ -376,8 +375,8 @@ pub async fn download_gridfs_file_core(
file_id: &str, file_id: &str,
) -> Result<Vec<u8>, String> { ) -> Result<Vec<u8>, String> {
ensure_document_pool(state, connection_id).await?; ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::MongoDb(client) => mongo_driver::download_gridfs_file(client, database, bucket, file_id).await, PoolKind::MongoDb(client) => mongo_driver::download_gridfs_file(client, database, bucket, file_id).await,
PoolKind::Agent(_) => Err("MongoDB legacy agent does not support GridFS download".to_string()), PoolKind::Agent(_) => Err("MongoDB legacy agent does not support GridFS download".to_string()),
_ => Err("Not a MongoDB connection".to_string()), _ => Err("Not a MongoDB connection".to_string()),
@@ -394,8 +393,8 @@ pub async fn upload_gridfs_file_core(
content_type: Option<&str>, content_type: Option<&str>,
) -> Result<String, String> { ) -> Result<String, String> {
ensure_document_pool(state, connection_id).await?; ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::MongoDb(client) => { PoolKind::MongoDb(client) => {
mongo_driver::upload_gridfs_file(client, database, bucket, file_name, data, content_type).await mongo_driver::upload_gridfs_file(client, database, bucket, file_name, data, content_type).await
} }
@@ -412,8 +411,8 @@ pub async fn delete_gridfs_file_core(
file_id: &str, file_id: &str,
) -> Result<(), String> { ) -> Result<(), String> {
ensure_document_pool(state, connection_id).await?; ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::MongoDb(client) => mongo_driver::delete_gridfs_file(client, database, bucket, file_id).await, PoolKind::MongoDb(client) => mongo_driver::delete_gridfs_file(client, database, bucket, file_id).await,
PoolKind::Agent(_) => Err("MongoDB legacy agent does not support GridFS file deletion".to_string()), PoolKind::Agent(_) => Err("MongoDB legacy agent does not support GridFS file deletion".to_string()),
_ => Err("Not a MongoDB connection".to_string()), _ => Err("Not a MongoDB connection".to_string()),
@@ -436,8 +435,8 @@ pub async fn find_documents_core(
cursor_pagination: bool, cursor_pagination: bool,
) -> Result<DocumentQueryResult, String> { ) -> Result<DocumentQueryResult, String> {
ensure_document_pool(state, connection_id).await?; ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::MongoDb(client) => { PoolKind::MongoDb(client) => {
// Document browser responses must retain BSON type metadata so nested filters // Document browser responses must retain BSON type metadata so nested filters
// can round-trip ObjectId, Date, and int64 values through Extended JSON. // can round-trip ObjectId, Date, and int64 values through Extended JSON.
@@ -448,13 +447,11 @@ pub async fn find_documents_core(
} }
PoolKind::DynamoDb(client) => { PoolKind::DynamoDb(client) => {
let client = client.clone(); let client = client.clone();
drop(connections);
let _ = (database, skip, projection, collation); let _ = (database, skip, projection, collation);
dynamodb_driver::find_items(&client, collection, limit, filter, sort, cursor).await dynamodb_driver::find_items(&client, collection, limit, filter, sort, cursor).await
} }
PoolKind::Elasticsearch(client) => { PoolKind::Elasticsearch(client) => {
let client = client.clone(); let client = client.clone();
drop(connections);
if cursor_pagination { if cursor_pagination {
elasticsearch_driver::find_documents_with_cursor(&client, collection, limit, filter, sort, cursor).await elasticsearch_driver::find_documents_with_cursor(&client, collection, limit, filter, sort, cursor).await
} else { } else {
@@ -463,7 +460,6 @@ pub async fn find_documents_core(
} }
PoolKind::Easysearch(client) => { PoolKind::Easysearch(client) => {
let client = client.clone(); let client = client.clone();
drop(connections);
if cursor_pagination { if cursor_pagination {
easysearch_driver::find_documents_with_cursor(&client, collection, limit, filter, sort, cursor).await easysearch_driver::find_documents_with_cursor(&client, collection, limit, filter, sort, cursor).await
} else { } else {
@@ -472,12 +468,10 @@ pub async fn find_documents_core(
} }
PoolKind::Meilisearch(client) => { PoolKind::Meilisearch(client) => {
let client = client.clone(); let client = client.clone();
drop(connections);
crate::db::meilisearch_driver::find_documents(&client, collection, skip, limit, filter, sort).await crate::db::meilisearch_driver::find_documents(&client, collection, skip, limit, filter, sort).await
} }
PoolKind::VectorDb(client) => { PoolKind::VectorDb(client) => {
let client = client.clone(); let client = client.clone();
drop(connections);
let _ = (filter, sort); let _ = (filter, sort);
vector_driver::find_documents(&client, database, collection, skip, limit).await vector_driver::find_documents(&client, database, collection, skip, limit).await
} }
@@ -516,21 +510,18 @@ pub async fn count_document_store_documents_core(
filter: Option<&str>, filter: Option<&str>,
) -> Result<u64, String> { ) -> Result<u64, String> {
ensure_document_pool(state, connection_id).await?; ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::DynamoDb(client) => { PoolKind::DynamoDb(client) => {
let client = client.clone(); let client = client.clone();
drop(connections);
dynamodb_driver::count_items(&client, collection, filter).await dynamodb_driver::count_items(&client, collection, filter).await
} }
PoolKind::Elasticsearch(client) => { PoolKind::Elasticsearch(client) => {
let client = client.clone(); let client = client.clone();
drop(connections);
elasticsearch_driver::count_documents(&client, collection, filter).await elasticsearch_driver::count_documents(&client, collection, filter).await
} }
PoolKind::Easysearch(client) => { PoolKind::Easysearch(client) => {
let client = client.clone(); let client = client.clone();
drop(connections);
easysearch_driver::count_documents(&client, collection, filter).await easysearch_driver::count_documents(&client, collection, filter).await
} }
_ => Err("Document count is not supported for this connection".to_string()), _ => Err("Document count is not supported for this connection".to_string()),
@@ -543,11 +534,10 @@ pub async fn describe_dynamodb_table_core(
table: &str, table: &str,
) -> Result<dynamodb_driver::DynamoDbTableDescription, String> { ) -> Result<dynamodb_driver::DynamoDbTableDescription, String> {
ensure_document_pool(state, connection_id).await?; ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::DynamoDb(client) => { PoolKind::DynamoDb(client) => {
let client = client.clone(); let client = client.clone();
drop(connections);
dynamodb_driver::describe_table(&client, table).await dynamodb_driver::describe_table(&client, table).await
} }
_ => Err("Not a DynamoDB connection".to_string()), _ => Err("Not a DynamoDB connection".to_string()),
@@ -561,16 +551,14 @@ pub async fn count_elasticsearch_documents_core(
filter: Option<&str>, filter: Option<&str>,
) -> Result<u64, String> { ) -> Result<u64, String> {
ensure_document_pool(state, connection_id).await?; ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::Elasticsearch(client) => { PoolKind::Elasticsearch(client) => {
let client = client.clone(); let client = client.clone();
drop(connections);
elasticsearch_driver::count_documents(&client, index, filter).await elasticsearch_driver::count_documents(&client, index, filter).await
} }
PoolKind::Easysearch(client) => { PoolKind::Easysearch(client) => {
let client = client.clone(); let client = client.clone();
drop(connections);
easysearch_driver::count_documents(&client, index, filter).await easysearch_driver::count_documents(&client, index, filter).await
} }
_ => Err("Not an Elasticsearch connection".to_string()), _ => Err("Not an Elasticsearch connection".to_string()),
@@ -586,11 +574,10 @@ pub async fn elasticsearch_get_index_metadata_core(
kind: ElasticsearchIndexMetadataKind, kind: ElasticsearchIndexMetadataKind,
) -> Result<serde_json::Value, String> { ) -> Result<serde_json::Value, String> {
ensure_document_pool(state, connection_id).await?; ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::Elasticsearch(client) => { PoolKind::Elasticsearch(client) => {
let client = client.clone(); let client = client.clone();
drop(connections);
match kind { match kind {
ElasticsearchIndexMetadataKind::Mapping => { ElasticsearchIndexMetadataKind::Mapping => {
elasticsearch_driver::get_index_mapping(&client, index).await elasticsearch_driver::get_index_mapping(&client, index).await
@@ -603,7 +590,6 @@ pub async fn elasticsearch_get_index_metadata_core(
} }
PoolKind::Easysearch(client) => { PoolKind::Easysearch(client) => {
let client = client.clone(); let client = client.clone();
drop(connections);
match kind { match kind {
ElasticsearchIndexMetadataKind::Mapping => easysearch_driver::get_index_mapping(&client, index).await, ElasticsearchIndexMetadataKind::Mapping => easysearch_driver::get_index_mapping(&client, index).await,
ElasticsearchIndexMetadataKind::Settings => easysearch_driver::get_index_settings(&client, index).await, ElasticsearchIndexMetadataKind::Settings => easysearch_driver::get_index_settings(&client, index).await,
@@ -621,16 +607,14 @@ pub async fn elasticsearch_delete_all_documents_core(
index: &str, index: &str,
) -> Result<elasticsearch_driver::ElasticsearchDeleteByQueryResult, String> { ) -> Result<elasticsearch_driver::ElasticsearchDeleteByQueryResult, String> {
ensure_document_pool(state, connection_id).await?; ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::Elasticsearch(client) => { PoolKind::Elasticsearch(client) => {
let client = client.clone(); let client = client.clone();
drop(connections);
elasticsearch_driver::delete_all_documents(&client, index).await elasticsearch_driver::delete_all_documents(&client, index).await
} }
PoolKind::Easysearch(client) => { PoolKind::Easysearch(client) => {
let client = client.clone(); let client = client.clone();
drop(connections);
easysearch_driver::delete_all_documents(&client, index).await easysearch_driver::delete_all_documents(&client, index).await
} }
_ => Err("Not an Elasticsearch connection".to_string()), _ => Err("Not an Elasticsearch connection".to_string()),
@@ -651,27 +635,23 @@ pub async fn insert_document_core(
routing: Option<&str>, routing: Option<&str>,
) -> Result<String, String> { ) -> Result<String, String> {
ensure_document_pool(state, connection_id).await?; ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::MongoDb(client) => mongo_driver::insert_document(client, database, collection, doc_json).await, PoolKind::MongoDb(client) => mongo_driver::insert_document(client, database, collection, doc_json).await,
PoolKind::DynamoDb(client) => { PoolKind::DynamoDb(client) => {
let client = client.clone(); let client = client.clone();
drop(connections);
dynamodb_driver::insert_item(&client, collection, doc_json).await dynamodb_driver::insert_item(&client, collection, doc_json).await
} }
PoolKind::Elasticsearch(client) => { PoolKind::Elasticsearch(client) => {
let client = client.clone(); let client = client.clone();
drop(connections);
elasticsearch_driver::insert_document(&client, collection, doc_json, routing).await elasticsearch_driver::insert_document(&client, collection, doc_json, routing).await
} }
PoolKind::Easysearch(client) => { PoolKind::Easysearch(client) => {
let client = client.clone(); let client = client.clone();
drop(connections);
easysearch_driver::insert_document(&client, collection, doc_json, routing).await easysearch_driver::insert_document(&client, collection, doc_json, routing).await
} }
PoolKind::Meilisearch(client) => { PoolKind::Meilisearch(client) => {
let client = client.clone(); let client = client.clone();
drop(connections);
crate::db::meilisearch_driver::insert_document(&client, collection, doc_json).await crate::db::meilisearch_driver::insert_document(&client, collection, doc_json).await
} }
PoolKind::Agent(client) => { PoolKind::Agent(client) => {
@@ -698,15 +678,12 @@ pub async fn insert_document_preserving_bson_types_core(
routing: Option<&str>, routing: Option<&str>,
) -> Result<String, String> { ) -> Result<String, String> {
ensure_document_pool(state, connection_id).await?; ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::MongoDb(client) => { PoolKind::MongoDb(client) => {
mongo_driver::insert_document_extended_json(client, database, collection, doc_json).await mongo_driver::insert_document_extended_json(client, database, collection, doc_json).await
} }
_ => { _ => insert_document_core(state, connection_id, database, collection, doc_json, routing).await,
drop(connections);
insert_document_core(state, connection_id, database, collection, doc_json, routing).await
}
} }
} }
@@ -720,29 +697,25 @@ pub async fn update_document_core(
routing: Option<&str>, routing: Option<&str>,
) -> Result<u64, String> { ) -> Result<u64, String> {
ensure_document_pool(state, connection_id).await?; ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::MongoDb(client) => mongo_driver::update_document(client, database, collection, id, doc_json).await, PoolKind::MongoDb(client) => mongo_driver::update_document(client, database, collection, id, doc_json).await,
PoolKind::DynamoDb(client) => { PoolKind::DynamoDb(client) => {
let client = client.clone(); let client = client.clone();
drop(connections);
dynamodb_driver::update_item(&client, collection, id, doc_json).await dynamodb_driver::update_item(&client, collection, id, doc_json).await
} }
PoolKind::Elasticsearch(client) => { PoolKind::Elasticsearch(client) => {
let client = client.clone(); let client = client.clone();
drop(connections);
// Elasticsearch requires the same custom routing value for writes // Elasticsearch requires the same custom routing value for writes
// as was used to index the document. // as was used to index the document.
elasticsearch_driver::update_document(&client, collection, id, doc_json, routing).await elasticsearch_driver::update_document(&client, collection, id, doc_json, routing).await
} }
PoolKind::Easysearch(client) => { PoolKind::Easysearch(client) => {
let client = client.clone(); let client = client.clone();
drop(connections);
easysearch_driver::update_document(&client, collection, id, doc_json, routing).await easysearch_driver::update_document(&client, collection, id, doc_json, routing).await
} }
PoolKind::Meilisearch(client) => { PoolKind::Meilisearch(client) => {
let client = client.clone(); let client = client.clone();
drop(connections);
crate::db::meilisearch_driver::update_document(&client, collection, id, doc_json).await crate::db::meilisearch_driver::update_document(&client, collection, id, doc_json).await
} }
PoolKind::Agent(client) => { PoolKind::Agent(client) => {
@@ -782,29 +755,25 @@ pub async fn delete_document_core_with_type(
document_type: Option<&str>, document_type: Option<&str>,
) -> Result<u64, String> { ) -> Result<u64, String> {
ensure_document_pool(state, connection_id).await?; ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::MongoDb(client) => mongo_driver::delete_document(client, database, collection, id).await, PoolKind::MongoDb(client) => mongo_driver::delete_document(client, database, collection, id).await,
PoolKind::DynamoDb(client) => { PoolKind::DynamoDb(client) => {
let client = client.clone(); let client = client.clone();
drop(connections);
dynamodb_driver::delete_item(&client, collection, id).await dynamodb_driver::delete_item(&client, collection, id).await
} }
PoolKind::Elasticsearch(client) => { PoolKind::Elasticsearch(client) => {
let client = client.clone(); let client = client.clone();
drop(connections);
// Elasticsearch requires the same custom routing value for writes // Elasticsearch requires the same custom routing value for writes
// as was used to index the document. // as was used to index the document.
elasticsearch_driver::delete_document(&client, collection, id, document_type, routing).await elasticsearch_driver::delete_document(&client, collection, id, document_type, routing).await
} }
PoolKind::Easysearch(client) => { PoolKind::Easysearch(client) => {
let client = client.clone(); let client = client.clone();
drop(connections);
easysearch_driver::delete_document(&client, collection, id, document_type, routing).await easysearch_driver::delete_document(&client, collection, id, document_type, routing).await
} }
PoolKind::Meilisearch(client) => { PoolKind::Meilisearch(client) => {
let client = client.clone(); let client = client.clone();
drop(connections);
crate::db::meilisearch_driver::delete_document(&client, collection, id).await crate::db::meilisearch_driver::delete_document(&client, collection, id).await
} }
PoolKind::Agent(client) => { PoolKind::Agent(client) => {
@@ -826,11 +795,10 @@ pub async fn save_meilisearch_document_batch_core(
inserts: &[String], inserts: &[String],
) -> Result<u64, String> { ) -> Result<u64, String> {
ensure_document_pool(state, connection_id).await?; ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::Meilisearch(client) => { PoolKind::Meilisearch(client) => {
let client = client.clone(); let client = client.clone();
drop(connections);
crate::db::meilisearch_driver::save_document_batch(&client, collection, updates, delete_ids, inserts).await crate::db::meilisearch_driver::save_document_batch(&client, collection, updates, delete_ids, inserts).await
} }
_ => Err("Not a Meilisearch connection".to_string()), _ => Err("Not a Meilisearch connection".to_string()),
@@ -853,11 +821,10 @@ pub async fn meilisearch_search_documents_core(
ranking_score_threshold: Option<f64>, ranking_score_threshold: Option<f64>,
) -> Result<crate::db::meilisearch_driver::MeilisearchSearchResult, String> { ) -> Result<crate::db::meilisearch_driver::MeilisearchSearchResult, String> {
ensure_document_pool(state, connection_id).await?; ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::Meilisearch(client) => { PoolKind::Meilisearch(client) => {
let client = client.clone(); let client = client.clone();
drop(connections);
let hybrid = hybrid_embedder.map(|embedder| crate::db::meilisearch_driver::MeilisearchHybrid { let hybrid = hybrid_embedder.map(|embedder| crate::db::meilisearch_driver::MeilisearchHybrid {
embedder: embedder.to_string(), embedder: embedder.to_string(),
semantic_ratio: hybrid_semantic_ratio.unwrap_or(0.5), semantic_ratio: hybrid_semantic_ratio.unwrap_or(0.5),
@@ -890,11 +857,10 @@ pub async fn meilisearch_fetch_document_page_core(
offset: u64, offset: u64,
) -> Result<crate::db::meilisearch_driver::MeilisearchDocumentPage, String> { ) -> Result<crate::db::meilisearch_driver::MeilisearchDocumentPage, String> {
ensure_document_pool(state, connection_id).await?; ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::Meilisearch(client) => { PoolKind::Meilisearch(client) => {
let client = client.clone(); let client = client.clone();
drop(connections);
crate::db::meilisearch_driver::fetch_document_page(&client, index, offset, limit, filter, sort).await crate::db::meilisearch_driver::fetch_document_page(&client, index, offset, limit, filter, sort).await
} }
_ => Err("Not a Meilisearch connection".to_string()), _ => Err("Not a Meilisearch connection".to_string()),
@@ -907,11 +873,10 @@ pub async fn meilisearch_get_index_settings_core(
index: &str, index: &str,
) -> Result<serde_json::Value, String> { ) -> Result<serde_json::Value, String> {
ensure_document_pool(state, connection_id).await?; ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::Meilisearch(client) => { PoolKind::Meilisearch(client) => {
let client = client.clone(); let client = client.clone();
drop(connections);
crate::db::meilisearch_driver::get_index_settings(&client, index).await crate::db::meilisearch_driver::get_index_settings(&client, index).await
} }
_ => Err("Not a Meilisearch connection".to_string()), _ => Err("Not a Meilisearch connection".to_string()),
@@ -925,11 +890,10 @@ pub async fn meilisearch_get_document_core(
id: &str, id: &str,
) -> Result<String, String> { ) -> Result<String, String> {
ensure_document_pool(state, connection_id).await?; ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::Meilisearch(client) => { PoolKind::Meilisearch(client) => {
let client = client.clone(); let client = client.clone();
drop(connections);
crate::db::meilisearch_driver::get_document(&client, index, id).await crate::db::meilisearch_driver::get_document(&client, index, id).await
} }
_ => Err("Not a Meilisearch connection".to_string()), _ => Err("Not a Meilisearch connection".to_string()),
@@ -943,11 +907,10 @@ pub async fn meilisearch_update_index_settings_core(
settings: &serde_json::Value, settings: &serde_json::Value,
) -> Result<(), String> { ) -> Result<(), String> {
ensure_document_pool(state, connection_id).await?; ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::Meilisearch(client) => { PoolKind::Meilisearch(client) => {
let client = client.clone(); let client = client.clone();
drop(connections);
crate::db::meilisearch_driver::update_index_settings(&client, index, settings).await crate::db::meilisearch_driver::update_index_settings(&client, index, settings).await
} }
_ => Err("Not a Meilisearch connection".to_string()), _ => Err("Not a Meilisearch connection".to_string()),
@@ -960,11 +923,10 @@ pub async fn meilisearch_get_index_stats_core(
index: &str, index: &str,
) -> Result<serde_json::Value, String> { ) -> Result<serde_json::Value, String> {
ensure_document_pool(state, connection_id).await?; ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::Meilisearch(client) => { PoolKind::Meilisearch(client) => {
let client = client.clone(); let client = client.clone();
drop(connections);
crate::db::meilisearch_driver::get_index_stats(&client, index).await crate::db::meilisearch_driver::get_index_stats(&client, index).await
} }
_ => Err("Not a Meilisearch connection".to_string()), _ => Err("Not a Meilisearch connection".to_string()),
@@ -977,11 +939,10 @@ pub async fn meilisearch_get_index_overview_core(
index: &str, index: &str,
) -> Result<crate::db::meilisearch_driver::MeilisearchIndexOverview, String> { ) -> Result<crate::db::meilisearch_driver::MeilisearchIndexOverview, String> {
ensure_document_pool(state, connection_id).await?; ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::Meilisearch(client) => { PoolKind::Meilisearch(client) => {
let client = client.clone(); let client = client.clone();
drop(connections);
crate::db::meilisearch_driver::get_index_overview(&client, index).await crate::db::meilisearch_driver::get_index_overview(&client, index).await
} }
_ => Err("Not a Meilisearch connection".to_string()), _ => Err("Not a Meilisearch connection".to_string()),
@@ -990,11 +951,10 @@ pub async fn meilisearch_get_index_overview_core(
pub async fn meilisearch_delete_index_core(state: &AppState, connection_id: &str, index: &str) -> Result<(), String> { pub async fn meilisearch_delete_index_core(state: &AppState, connection_id: &str, index: &str) -> Result<(), String> {
ensure_document_pool(state, connection_id).await?; ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::Meilisearch(client) => { PoolKind::Meilisearch(client) => {
let client = client.clone(); let client = client.clone();
drop(connections);
crate::db::meilisearch_driver::delete_index(&client, index).await crate::db::meilisearch_driver::delete_index(&client, index).await
} }
_ => Err("Not a Meilisearch connection".to_string()), _ => Err("Not a Meilisearch connection".to_string()),
@@ -1007,11 +967,10 @@ pub async fn meilisearch_delete_all_documents_core(
index: &str, index: &str,
) -> Result<(), String> { ) -> Result<(), String> {
ensure_document_pool(state, connection_id).await?; ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::Meilisearch(client) => { PoolKind::Meilisearch(client) => {
let client = client.clone(); let client = client.clone();
drop(connections);
crate::db::meilisearch_driver::delete_all_documents(&client, index).await crate::db::meilisearch_driver::delete_all_documents(&client, index).await
} }
_ => Err("Not a Meilisearch connection".to_string()), _ => Err("Not a Meilisearch connection".to_string()),
@@ -1023,8 +982,8 @@ async fn meilisearch_client_core(
connection_id: &str, connection_id: &str,
) -> Result<crate::db::meilisearch_driver::MeilisearchClient, String> { ) -> Result<crate::db::meilisearch_driver::MeilisearchClient, String> {
ensure_document_pool(state, connection_id).await?; ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::Meilisearch(client) => Ok(client.clone()), PoolKind::Meilisearch(client) => Ok(client.clone()),
_ => Err("Not a Meilisearch connection".to_string()), _ => Err("Not a Meilisearch connection".to_string()),
} }
+1 -1
View File
@@ -147,7 +147,7 @@ async fn collect_runtime_seeds(state: &AppState) -> Vec<RuntimeSeed> {
} }
let configs = state.configs.read().await; let configs = state.configs.read().await;
let connections = state.connections.read().await; let connections = state.connection_pools_snapshot().await;
for (pool_key, pool) in connections.iter() { for (pool_key, pool) in connections.iter() {
match pool { match pool {
PoolKind::Agent(client) => { PoolKind::Agent(client) => {
+2 -2
View File
@@ -3,8 +3,8 @@ use crate::db::hbase_driver::{self, HBasePutRowInput, HBaseRow, HBaseScanResult,
async fn client(state: &AppState, connection_id: &str) -> Result<hbase_driver::HBaseClient, String> { async fn client(state: &AppState, connection_id: &str) -> Result<hbase_driver::HBaseClient, String> {
let pool_key = state.get_or_create_pool(connection_id, None).await?; let pool_key = state.get_or_create_pool(connection_id, None).await?;
let connections = state.connections.read().await; let pool_handle = state.pool_handle(&pool_key).await;
match connections.get(&pool_key) { match pool_handle.as_ref() {
Some(PoolKind::HBase(client)) => Ok(client.clone()), Some(PoolKind::HBase(client)) => Ok(client.clone()),
_ => Err("Not an HBase connection".to_string()), _ => Err("Not an HBase connection".to_string()),
} }
+129 -57
View File
@@ -29,8 +29,8 @@ pub async fn mongo_list_collections_core(
pub async fn mongo_create_database_core(state: &AppState, connection_id: &str, database: &str) -> Result<(), String> { pub async fn mongo_create_database_core(state: &AppState, connection_id: &str, database: &str) -> Result<(), String> {
ensure_document_pool(state, connection_id).await?; ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::MongoDb(client) => mongo_driver::create_database(client, database).await, PoolKind::MongoDb(client) => mongo_driver::create_database(client, database).await,
PoolKind::Agent(_) => Err("MongoDB legacy agent does not support create database".to_string()), PoolKind::Agent(_) => Err("MongoDB legacy agent does not support create database".to_string()),
_ => Err("Not a MongoDB connection".to_string()), _ => Err("Not a MongoDB connection".to_string()),
@@ -40,8 +40,8 @@ pub async fn mongo_create_database_core(state: &AppState, connection_id: &str, d
pub async fn mongo_drop_database_core(state: &AppState, connection_id: &str, database: &str) -> Result<(), String> { pub async fn mongo_drop_database_core(state: &AppState, connection_id: &str, database: &str) -> Result<(), String> {
mongo_driver::validate_mongo_namespace_name(database, "Database")?; mongo_driver::validate_mongo_namespace_name(database, "Database")?;
ensure_document_pool(state, connection_id).await?; ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::MongoDb(client) => mongo_driver::drop_database(client, database).await, PoolKind::MongoDb(client) => mongo_driver::drop_database(client, database).await,
PoolKind::Agent(client) => { PoolKind::Agent(client) => {
let mut client = client.lock().await; let mut client = client.lock().await;
@@ -67,8 +67,8 @@ pub async fn mongo_drop_collection_core(
mongo_driver::validate_mongo_namespace_name(database, "Database")?; mongo_driver::validate_mongo_namespace_name(database, "Database")?;
mongo_driver::validate_mongo_namespace_name(collection, "Collection")?; mongo_driver::validate_mongo_namespace_name(collection, "Collection")?;
ensure_document_pool(state, connection_id).await?; ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::MongoDb(client) => mongo_driver::drop_collection(client, database, collection).await, PoolKind::MongoDb(client) => mongo_driver::drop_collection(client, database, collection).await,
PoolKind::Agent(client) => { PoolKind::Agent(client) => {
let mut client = client.lock().await; let mut client = client.lock().await;
@@ -92,8 +92,8 @@ pub async fn mongo_rename_collection_core(
new_name: &str, new_name: &str,
) -> Result<(), String> { ) -> Result<(), String> {
ensure_document_pool(state, connection_id).await?; ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::MongoDb(client) => mongo_driver::rename_collection(client, database, collection, new_name).await, PoolKind::MongoDb(client) => mongo_driver::rename_collection(client, database, collection, new_name).await,
PoolKind::Agent(_) => Err("MongoDB legacy agent does not support rename collection".to_string()), PoolKind::Agent(_) => Err("MongoDB legacy agent does not support rename collection".to_string()),
_ => Err("Not a MongoDB connection".to_string()), _ => Err("Not a MongoDB connection".to_string()),
@@ -109,8 +109,8 @@ pub async fn mongo_clone_collection_core(
) -> Result<MongoCloneCollectionResult, String> { ) -> Result<MongoCloneCollectionResult, String> {
mongo_driver::validate_clone_collection_names(database, source_collection, target_collection)?; mongo_driver::validate_clone_collection_names(database, source_collection, target_collection)?;
ensure_document_pool(state, connection_id).await?; ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::MongoDb(client) => { PoolKind::MongoDb(client) => {
mongo_driver::clone_collection(client, database, source_collection, target_collection).await mongo_driver::clone_collection(client, database, source_collection, target_collection).await
} }
@@ -140,8 +140,8 @@ pub async fn mongo_server_version_core(
database: &str, database: &str,
) -> Result<String, String> { ) -> Result<String, String> {
ensure_document_pool(state, connection_id).await?; ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::MongoDb(client) => mongo_driver::server_version(client, database).await, PoolKind::MongoDb(client) => mongo_driver::server_version(client, database).await,
PoolKind::Agent(client) => { PoolKind::Agent(client) => {
let mut client = client.lock().await; let mut client = client.lock().await;
@@ -158,8 +158,8 @@ pub async fn mongo_run_command_core(
command_json: &str, command_json: &str,
) -> Result<MongoDocumentResult, String> { ) -> Result<MongoDocumentResult, String> {
ensure_document_pool(state, connection_id).await?; ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::MongoDb(client) => mongo_driver::run_command(client, database, command_json).await, PoolKind::MongoDb(client) => mongo_driver::run_command(client, database, command_json).await,
PoolKind::Agent(client) => { PoolKind::Agent(client) => {
let mut client = client.lock().await; let mut client = client.lock().await;
@@ -192,8 +192,8 @@ pub async fn mongo_collection_stats_core(
scale: Option<serde_json::Number>, scale: Option<serde_json::Number>,
) -> Result<MongoCollectionStatsResult, String> { ) -> Result<MongoCollectionStatsResult, String> {
ensure_document_pool(state, connection_id).await?; ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::MongoDb(client) => mongo_driver::collection_stats(client, database, collection, scale).await, PoolKind::MongoDb(client) => mongo_driver::collection_stats(client, database, collection, scale).await,
PoolKind::Agent(_) => Err("MongoDB legacy agent does not support collection stats helpers".to_string()), PoolKind::Agent(_) => Err("MongoDB legacy agent does not support collection stats helpers".to_string()),
_ => Err("Not a MongoDB connection".to_string()), _ => Err("Not a MongoDB connection".to_string()),
@@ -244,8 +244,8 @@ async fn mongo_find_documents_without_total_core(
collation: Option<&str>, collation: Option<&str>,
) -> Result<MongoDocumentResult, String> { ) -> Result<MongoDocumentResult, String> {
ensure_document_pool(state, connection_id).await?; ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::MongoDb(client) => { PoolKind::MongoDb(client) => {
mongo_driver::find_documents_without_total( mongo_driver::find_documents_without_total(
client, database, collection, skip, limit, filter, projection, sort, collation, client, database, collection, skip, limit, filter, projection, sort, collation,
@@ -284,8 +284,8 @@ pub async fn mongo_find_one_core(
options: Option<&str>, options: Option<&str>,
) -> Result<MongoDocumentResult, String> { ) -> Result<MongoDocumentResult, String> {
ensure_document_pool(state, connection_id).await?; ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::MongoDb(client) => { PoolKind::MongoDb(client) => {
mongo_driver::find_one(client, database, collection, filter, projection, options).await mongo_driver::find_one(client, database, collection, filter, projection, options).await
} }
@@ -320,8 +320,8 @@ pub async fn mongo_explain_find_core(
verbosity: &str, verbosity: &str,
) -> Result<serde_json::Value, String> { ) -> Result<serde_json::Value, String> {
ensure_document_pool(state, connection_id).await?; ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::MongoDb(client) => { PoolKind::MongoDb(client) => {
mongo_driver::explain_find( mongo_driver::explain_find(
client, database, collection, skip, limit, filter, projection, sort, collation, verbosity, client, database, collection, skip, limit, filter, projection, sort, collation, verbosity,
@@ -358,8 +358,8 @@ pub async fn mongo_count_documents_core(
) -> Result<u64, String> { ) -> Result<u64, String> {
let accurate = mode != Some("legacy"); let accurate = mode != Some("legacy");
ensure_document_pool(state, connection_id).await?; ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::MongoDb(client) => { PoolKind::MongoDb(client) => {
mongo_driver::count_documents(client, database, collection, filter, accurate).await mongo_driver::count_documents(client, database, collection, filter, accurate).await
} }
@@ -406,8 +406,8 @@ pub async fn mongo_find_documents_extended_json_core(
sort: Option<&str>, sort: Option<&str>,
) -> Result<MongoDocumentResult, String> { ) -> Result<MongoDocumentResult, String> {
ensure_document_pool(state, connection_id).await?; ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::MongoDb(client) => { PoolKind::MongoDb(client) => {
mongo_driver::find_documents_extended_json( mongo_driver::find_documents_extended_json(
client, database, collection, skip, limit, filter, projection, sort, None, client, database, collection, skip, limit, filter, projection, sort, None,
@@ -454,8 +454,8 @@ pub async fn mongo_aggregate_documents_core(
options_json: Option<&str>, options_json: Option<&str>,
) -> Result<MongoDocumentResult, String> { ) -> Result<MongoDocumentResult, String> {
ensure_document_pool(state, connection_id).await?; ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::MongoDb(client) => { PoolKind::MongoDb(client) => {
mongo_driver::aggregate_documents(client, database, collection, pipeline_json, max_rows, options_json).await mongo_driver::aggregate_documents(client, database, collection, pipeline_json, max_rows, options_json).await
} }
@@ -490,8 +490,8 @@ pub async fn mongo_distinct_core(
filter: Option<&str>, filter: Option<&str>,
) -> Result<MongoDocumentResult, String> { ) -> Result<MongoDocumentResult, String> {
ensure_document_pool(state, connection_id).await?; ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::MongoDb(client) => mongo_driver::distinct(client, database, collection, field, filter).await, PoolKind::MongoDb(client) => mongo_driver::distinct(client, database, collection, field, filter).await,
// The legacy agent protocol has no distinct method and no read that could stand in for it. // The legacy agent protocol has no distinct method and no read that could stand in for it.
PoolKind::Agent(_) => Err("MongoDB legacy agent does not support distinct".to_string()), PoolKind::Agent(_) => Err("MongoDB legacy agent does not support distinct".to_string()),
@@ -515,8 +515,8 @@ pub async fn mongo_list_index_specs_core(
mongo_driver::validate_mongo_namespace_name(collection, "Collection")?; mongo_driver::validate_mongo_namespace_name(collection, "Collection")?;
ensure_document_pool(state, connection_id).await?; ensure_document_pool(state, connection_id).await?;
let is_native = { let is_native = {
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::MongoDb(_) => true, PoolKind::MongoDb(_) => true,
PoolKind::Agent(_) => false, PoolKind::Agent(_) => false,
_ => return Err("Not a MongoDB connection".to_string()), _ => return Err("Not a MongoDB connection".to_string()),
@@ -529,8 +529,8 @@ pub async fn mongo_list_index_specs_core(
return Ok(indexes.iter().map(mongo_driver::index_spec_from_index_info).collect()); return Ok(indexes.iter().map(mongo_driver::index_spec_from_index_info).collect());
} }
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::MongoDb(client) => mongo_driver::list_index_specs(client, database, collection).await, PoolKind::MongoDb(client) => mongo_driver::list_index_specs(client, database, collection).await,
_ => Err("Not a MongoDB connection".to_string()), _ => Err("Not a MongoDB connection".to_string()),
} }
@@ -550,8 +550,8 @@ pub async fn mongo_create_index_core(
mongo_driver::validate_mongo_namespace_name(collection, "Collection")?; mongo_driver::validate_mongo_namespace_name(collection, "Collection")?;
mongo_driver::validate_create_index_request(keys_json, options_json)?; mongo_driver::validate_create_index_request(keys_json, options_json)?;
ensure_document_pool(state, connection_id).await?; ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::MongoDb(client) => { PoolKind::MongoDb(client) => {
mongo_driver::create_index(client, database, collection, keys_json, options_json).await mongo_driver::create_index(client, database, collection, keys_json, options_json).await
} }
@@ -586,8 +586,8 @@ pub async fn mongo_create_user_core(
mongo_driver::validate_mongo_namespace_name(database, "Database")?; mongo_driver::validate_mongo_namespace_name(database, "Database")?;
mongo_driver::validate_create_user_request(user_json, write_concern_json)?; mongo_driver::validate_create_user_request(user_json, write_concern_json)?;
ensure_document_pool(state, connection_id).await?; ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::MongoDb(client) => { PoolKind::MongoDb(client) => {
mongo_driver::create_user(client, database, user_json, write_concern_json).await?; mongo_driver::create_user(client, database, user_json, write_concern_json).await?;
Ok(1) Ok(1)
@@ -662,8 +662,8 @@ async fn mongo_drop_indexes_once_core(
// to Native MongoDB or the Legacy Agent. // to Native MongoDB or the Legacy Agent.
mongo_driver::validate_drop_indexes_request(indexes_json, single)?; mongo_driver::validate_drop_indexes_request(indexes_json, single)?;
ensure_document_pool(state, connection_id).await?; ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::MongoDb(client) => { PoolKind::MongoDb(client) => {
mongo_driver::drop_indexes(client, database, collection, indexes_json, single).await mongo_driver::drop_indexes(client, database, collection, indexes_json, single).await
} }
@@ -700,8 +700,8 @@ pub async fn mongo_insert_documents_core(
docs_json: &str, docs_json: &str,
) -> Result<u64, String> { ) -> Result<u64, String> {
ensure_document_pool(state, connection_id).await?; ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::MongoDb(client) => mongo_driver::insert_documents(client, database, collection, docs_json).await, PoolKind::MongoDb(client) => mongo_driver::insert_documents(client, database, collection, docs_json).await,
PoolKind::Agent(client) => { PoolKind::Agent(client) => {
let documents: serde_json::Value = let documents: serde_json::Value =
@@ -747,8 +747,8 @@ pub async fn mongo_insert_documents_extended_json_core(
docs_json: &str, docs_json: &str,
) -> Result<u64, String> { ) -> Result<u64, String> {
ensure_document_pool(state, connection_id).await?; ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::MongoDb(client) => { PoolKind::MongoDb(client) => {
mongo_driver::insert_documents_extended_json(client, database, collection, docs_json).await mongo_driver::insert_documents_extended_json(client, database, collection, docs_json).await
} }
@@ -780,8 +780,8 @@ pub async fn mongo_update_documents_core(
options_json: Option<&str>, options_json: Option<&str>,
) -> Result<u64, String> { ) -> Result<u64, String> {
ensure_document_pool(state, connection_id).await?; ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::MongoDb(client) => { PoolKind::MongoDb(client) => {
mongo_driver::update_documents(client, database, collection, filter_json, update_json, many, options_json) mongo_driver::update_documents(client, database, collection, filter_json, update_json, many, options_json)
.await .await
@@ -824,8 +824,8 @@ pub async fn mongo_delete_documents_core(
many: bool, many: bool,
) -> Result<u64, String> { ) -> Result<u64, String> {
ensure_document_pool(state, connection_id).await?; ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::MongoDb(client) => { PoolKind::MongoDb(client) => {
mongo_driver::delete_documents(client, database, collection, filter_json, many).await mongo_driver::delete_documents(client, database, collection, filter_json, many).await
} }
@@ -855,8 +855,8 @@ pub async fn mongo_find_one_and_update_core(
options_json: Option<&str>, options_json: Option<&str>,
) -> Result<MongoDocumentResult, String> { ) -> Result<MongoDocumentResult, String> {
ensure_document_pool(state, connection_id).await?; ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::MongoDb(client) => { PoolKind::MongoDb(client) => {
mongo_driver::find_one_and_update(client, database, collection, filter_json, update_json, options_json) mongo_driver::find_one_and_update(client, database, collection, filter_json, update_json, options_json)
.await .await
@@ -876,8 +876,8 @@ pub async fn mongo_find_one_and_replace_core(
options_json: Option<&str>, options_json: Option<&str>,
) -> Result<MongoDocumentResult, String> { ) -> Result<MongoDocumentResult, String> {
ensure_document_pool(state, connection_id).await?; ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::MongoDb(client) => { PoolKind::MongoDb(client) => {
mongo_driver::find_one_and_replace( mongo_driver::find_one_and_replace(
client, client,
@@ -903,8 +903,8 @@ pub async fn mongo_find_one_and_delete_core(
options_json: Option<&str>, options_json: Option<&str>,
) -> Result<MongoDocumentResult, String> { ) -> Result<MongoDocumentResult, String> {
ensure_document_pool(state, connection_id).await?; ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::MongoDb(client) => { PoolKind::MongoDb(client) => {
mongo_driver::find_one_and_delete(client, database, collection, filter_json, options_json).await mongo_driver::find_one_and_delete(client, database, collection, filter_json, options_json).await
} }
@@ -1409,6 +1409,7 @@ mod tests {
&[AgentCapability::MongoDropDatabase.as_str()], &[AgentCapability::MongoDropDatabase.as_str()],
None, None,
None, None,
None,
) )
.await .await
} }
@@ -1420,8 +1421,16 @@ mod tests {
expected_result: serde_json::Value, expected_result: serde_json::Value,
capabilities: &[&str], capabilities: &[&str],
) -> (AppState, tempfile::TempDir) { ) -> (AppState, tempfile::TempDir) {
legacy_mongo_state_with_options(expected_method, expected_params, expected_result, capabilities, None, None) legacy_mongo_state_with_options(
.await expected_method,
expected_params,
expected_result,
capabilities,
None,
None,
None,
)
.await
} }
#[cfg(unix)] #[cfg(unix)]
@@ -1438,6 +1447,7 @@ mod tests {
&[AgentCapability::MongoDropDatabase.as_str()], &[AgentCapability::MongoDropDatabase.as_str()],
Some(server_version), Some(server_version),
None, None,
None,
) )
.await .await
} }
@@ -1456,6 +1466,7 @@ mod tests {
&[AgentCapability::MongoDropDatabase.as_str()], &[AgentCapability::MongoDropDatabase.as_str()],
Some(server_version), Some(server_version),
Some(expected_error), Some(expected_error),
None,
) )
.await .await
} }
@@ -1468,6 +1479,7 @@ mod tests {
capabilities: &[&str], capabilities: &[&str],
server_version: Option<&str>, server_version: Option<&str>,
expected_error: Option<&str>, expected_error: Option<&str>,
response_delay_ms: Option<u64>,
) -> (AppState, tempfile::TempDir) { ) -> (AppState, tempfile::TempDir) {
use std::io::Write; use std::io::Write;
@@ -1482,10 +1494,13 @@ mod tests {
}; };
let server_version = python_optional_string(server_version); let server_version = python_optional_string(server_version);
let expected_error = python_optional_string(expected_error); let expected_error = python_optional_string(expected_error);
let response_delay_ms = response_delay_ms.unwrap_or_default();
let request_entered_path = serde_json::to_string(&directory.path().join("request-entered")).unwrap();
write!( write!(
script, script,
r#"import json r#"import json
import sys import sys
import time
EXPECTED_METHOD = {expected_method} EXPECTED_METHOD = {expected_method}
EXPECTED_PARAMS = json.loads({expected_params}) EXPECTED_PARAMS = json.loads({expected_params})
@@ -1493,6 +1508,8 @@ EXPECTED_RESULT = json.loads({expected_result})
CAPABILITIES = {capabilities} CAPABILITIES = {capabilities}
SERVER_VERSION = {server_version} SERVER_VERSION = {server_version}
EXPECTED_ERROR = {expected_error} EXPECTED_ERROR = {expected_error}
RESPONSE_DELAY_SECONDS = {response_delay_ms} / 1000
REQUEST_ENTERED_PATH = {request_entered_path}
expected_calls = 0 expected_calls = 0
print(json.dumps({{"ready": True}}), flush=True) print(json.dumps({{"ready": True}}), flush=True)
@@ -1515,6 +1532,9 @@ for line in sys.stdin:
if expected_calls > 1: if expected_calls > 1:
print(json.dumps({{"jsonrpc": "2.0", "id": request["id"], "error": {{"code": -1, "message": "duplicate MongoDB RPC"}}}}), flush=True) print(json.dumps({{"jsonrpc": "2.0", "id": request["id"], "error": {{"code": -1, "message": "duplicate MongoDB RPC"}}}}), flush=True)
continue continue
if RESPONSE_DELAY_SECONDS:
open(REQUEST_ENTERED_PATH, "w").close()
time.sleep(RESPONSE_DELAY_SECONDS)
if EXPECTED_ERROR is not None: if EXPECTED_ERROR is not None:
print(json.dumps({{"jsonrpc": "2.0", "id": request["id"], "error": {{"code": -1, "message": EXPECTED_ERROR}}}}), flush=True) print(json.dumps({{"jsonrpc": "2.0", "id": request["id"], "error": {{"code": -1, "message": EXPECTED_ERROR}}}}), flush=True)
else: else:
@@ -1547,7 +1567,11 @@ for line in sys.stdin:
})) }))
.unwrap(); .unwrap();
state.configs.write().await.insert("legacy".to_string(), config); state.configs.write().await.insert("legacy".to_string(), config);
state.connections.write().await.insert("legacy".to_string(), PoolKind::agent(client)); state
.update_connection_pools(|connections| {
connections.insert("legacy".to_string(), PoolKind::agent(client));
})
.await;
(state, directory) (state, directory)
} }
@@ -1858,6 +1882,54 @@ for line in sys.stdin:
assert_eq!(name, "email_1"); assert_eq!(name, "email_1");
} }
#[cfg(unix)]
#[tokio::test]
async fn stalled_legacy_create_index_does_not_block_registry_writes() {
let keys_json = r#"{"email":1}"#;
let options_json = r#"{"name":"email_1"}"#;
let (state, directory) = legacy_mongo_state_with_options(
"create_index",
serde_json::json!({
"database": "app",
"collection": "users",
"keys_json": keys_json,
"options_json": options_json,
}),
serde_json::json!({ "name": "email_1" }),
&[AgentCapability::MongoDropDatabase.as_str()],
None,
None,
Some(500),
)
.await;
let state = std::sync::Arc::new(state);
let index_state = std::sync::Arc::clone(&state);
let index_task = tokio::spawn(async move {
mongo_create_index_core(&index_state, "legacy", "app", "users", keys_json, Some(options_json)).await
});
let request_entered = directory.path().join("request-entered");
tokio::time::timeout(std::time::Duration::from_secs(2), async {
while !request_entered.exists() {
tokio::time::sleep(std::time::Duration::from_millis(10)).await;
}
})
.await
.expect("legacy createIndex request must reach the Agent");
tokio::time::timeout(
std::time::Duration::from_millis(100),
state.update_connection_pools(|connections| {
connections.insert("other-connection".to_string(), PoolKind::MessageQueue);
}),
)
.await
.expect("a stalled Agent call must not retain the registry lock");
assert_eq!(index_task.await.expect("index task join").as_deref(), Ok("email_1"));
assert!(state.pool_handle("other-connection").await.is_some());
}
#[test] #[test]
fn mongo_indexes_query_result_matches_desktop_contract_and_limits_rows() { fn mongo_indexes_query_result_matches_desktop_contract_and_limits_rows() {
let result = mongo_indexes_query_result( let result = mongo_indexes_query_result(
+71 -79
View File
@@ -1674,8 +1674,7 @@ async fn sqlserver_pool_is_current(
pool_key: &str, pool_key: &str,
client: &Arc<tokio::sync::Mutex<db::sqlserver::SqlServerClient>>, client: &Arc<tokio::sync::Mutex<db::sqlserver::SqlServerClient>>,
) -> bool { ) -> bool {
let connections = state.connections.read().await; matches!(state.pool_handle(pool_key).await, Some(PoolKind::SqlServer(current)) if Arc::ptr_eq(&current, client))
matches!(connections.get(pool_key), Some(PoolKind::SqlServer(current)) if Arc::ptr_eq(current, client))
} }
pub fn query_timeout_duration(timeout_secs: Option<u64>) -> Option<Duration> { pub fn query_timeout_duration(timeout_secs: Option<u64>) -> Option<Duration> {
@@ -1769,13 +1768,12 @@ async fn do_execute_typed(
let operation_budget = operation_budget_for_pool_key(state, pool_key, query_timeout).await; let operation_budget = operation_budget_for_pool_key(state, pool_key, query_timeout).await;
let pool_db_type = connection_database_type_for_pool_key(state, pool_key).await; let pool_db_type = connection_database_type_for_pool_key(state, pool_key).await;
let mysql_catalog_dialect = connection_mysql_catalog_dialect_for_pool_key(state, pool_key).await; let mysql_catalog_dialect = connection_mysql_catalog_dialect_for_pool_key(state, pool_key).await;
let connections = state.connections.read().await; let pool = state.pool_handle(pool_key).await.ok_or("Connection not found")?;
let pool = connections.get(pool_key).ok_or("Connection not found")?;
let mut typed_agent_error = None; let mut typed_agent_error = None;
#[cfg(feature = "duckdb-sidecar")] #[cfg(feature = "duckdb-sidecar")]
let mut typed_duckdb_error = None; let mut typed_duckdb_error = None;
let result: Result<db::QueryResult, String> = match pool { let result: Result<db::QueryResult, String> = match &pool {
#[cfg(feature = "duckdb-sidecar")] #[cfg(feature = "duckdb-sidecar")]
PoolKind::DuckDbWorker(client) => { PoolKind::DuckDbWorker(client) => {
let client = client.clone(); let client = client.clone();
@@ -1793,7 +1791,6 @@ async fn do_execute_typed(
let sql = sql.to_string(); let sql = sql.to_string();
let database = database.map(str::to_string); let database = database.map(str::to_string);
let max_rows = options.max_rows; let max_rows = options.max_rows;
drop(connections);
match client.execute_typed(database, sql, max_rows, cancel_token, query_timeout).await { match client.execute_typed(database, sql, max_rows, cancel_token, query_timeout).await {
Ok(result) => Ok(result), Ok(result) => Ok(result),
Err(error) => { Err(error) => {
@@ -1815,7 +1812,6 @@ async fn do_execute_typed(
let bare = *mode == crate::connection::MysqlMode::Bare; let bare = *mode == crate::connection::MysqlMode::Bare;
let max_rows = options.max_rows; let max_rows = options.max_rows;
let max_result_bytes = options.max_result_bytes.filter(|value| *value > 0); let max_result_bytes = options.max_result_bytes.filter(|value| *value > 0);
drop(connections);
let mut conn = match db::mysql::get_conn_with_health_check_with_cancel( let mut conn = match db::mysql::get_conn_with_health_check_with_cancel(
&p, &p,
operation_budget.checkout_timeout, operation_budget.checkout_timeout,
@@ -1880,7 +1876,6 @@ async fn do_execute_typed(
let prefer_text_protocol = postgres_prefers_text_protocol(pool_db_type); let prefer_text_protocol = postgres_prefers_text_protocol(pool_db_type);
let execution_mode = options.execution_mode; let execution_mode = options.execution_mode;
let cancel_context = state.get_postgres_cancel_context(pool_key).await; let cancel_context = state.get_postgres_cancel_context(pool_key).await;
drop(connections);
if execution_mode == QueryExecutionMode::PostgresReadOnlyTransaction { if execution_mode == QueryExecutionMode::PostgresReadOnlyTransaction {
db::postgres::execute_query_in_read_only_transaction_with_rollback( db::postgres::execute_query_in_read_only_transaction_with_rollback(
&p, &p,
@@ -1920,14 +1915,12 @@ async fn do_execute_typed(
PoolKind::Sqlite(p) => { PoolKind::Sqlite(p) => {
let p = p.clone(); let p = p.clone();
let max_rows = options.max_rows; let max_rows = options.max_rows;
drop(connections);
wait_for_query_opt(cancel_token, query_timeout, db::sqlite::execute_query_with_max_rows(&p, sql, max_rows)) wait_for_query_opt(cancel_token, query_timeout, db::sqlite::execute_query_with_max_rows(&p, sql, max_rows))
.await .await
} }
PoolKind::Rqlite(client) => { PoolKind::Rqlite(client) => {
let client = client.clone(); let client = client.clone();
let max_rows = options.max_rows; let max_rows = options.max_rows;
drop(connections);
wait_for_query_opt( wait_for_query_opt(
cancel_token, cancel_token,
query_timeout, query_timeout,
@@ -1938,7 +1931,6 @@ async fn do_execute_typed(
PoolKind::Turso(client) => { PoolKind::Turso(client) => {
let client = client.clone(); let client = client.clone();
let max_rows = options.max_rows; let max_rows = options.max_rows;
drop(connections);
wait_for_query_opt( wait_for_query_opt(
cancel_token, cancel_token,
query_timeout, query_timeout,
@@ -1949,7 +1941,6 @@ async fn do_execute_typed(
PoolKind::CloudflareD1(client) => { PoolKind::CloudflareD1(client) => {
let client = client.clone(); let client = client.clone();
let max_rows = options.max_rows; let max_rows = options.max_rows;
drop(connections);
wait_for_query_opt( wait_for_query_opt(
cancel_token, cancel_token,
query_timeout, query_timeout,
@@ -1961,7 +1952,6 @@ async fn do_execute_typed(
let client = client.clone(); let client = client.clone();
let database = pool_key.split(':').nth(1).unwrap_or("default").to_string(); let database = pool_key.split(':').nth(1).unwrap_or("default").to_string();
let max_rows = options.max_rows; let max_rows = options.max_rows;
drop(connections);
let result = wait_for_query_opt( let result = wait_for_query_opt(
cancel_token, cancel_token,
query_timeout, query_timeout,
@@ -1978,7 +1968,6 @@ async fn do_execute_typed(
let client = client.clone(); let client = client.clone();
let max_rows = options.max_rows; let max_rows = options.max_rows;
let execution_mode = options.execution_mode; let execution_mode = options.execution_mode;
drop(connections);
let (mut client, lock_wait_ms) = let (mut client, lock_wait_ms) =
match lock_shared_client_with_wait(&client, cancel_token.clone(), None).await { match lock_shared_client_with_wait(&client, cancel_token.clone(), None).await {
Ok(value) => value, Ok(value) => value,
@@ -2010,7 +1999,6 @@ async fn do_execute_typed(
let client = client.clone(); let client = client.clone();
let sql = sql.to_string(); let sql = sql.to_string();
let max_rows = options.max_rows; let max_rows = options.max_rows;
drop(connections);
let result = wait_for_query_opt( let result = wait_for_query_opt(
cancel_token, cancel_token,
query_timeout, query_timeout,
@@ -2031,7 +2019,6 @@ async fn do_execute_typed(
let client = client.clone(); let client = client.clone();
let sql = sql.to_string(); let sql = sql.to_string();
let max_rows = options.max_rows; let max_rows = options.max_rows;
drop(connections);
let result = wait_for_query_opt( let result = wait_for_query_opt(
cancel_token, cancel_token,
query_timeout, query_timeout,
@@ -2052,7 +2039,6 @@ async fn do_execute_typed(
let client = client.clone(); let client = client.clone();
let sql = sql.to_string(); let sql = sql.to_string();
let max_rows = options.max_rows; let max_rows = options.max_rows;
drop(connections);
let result = wait_for_query_opt( let result = wait_for_query_opt(
cancel_token, cancel_token,
query_timeout, query_timeout,
@@ -2069,7 +2055,6 @@ async fn do_execute_typed(
let client = client.clone(); let client = client.clone();
let sql = sql.to_string(); let sql = sql.to_string();
let max_rows = options.max_rows; let max_rows = options.max_rows;
drop(connections);
let result = let result =
wait_for_query_opt(cancel_token, query_timeout, db::vector_driver::execute_rest_query(&client, &sql)) wait_for_query_opt(cancel_token, query_timeout, db::vector_driver::execute_rest_query(&client, &sql))
.await .await
@@ -2089,7 +2074,6 @@ async fn do_execute_typed(
let client = client.clone(); let client = client.clone();
let database = pool_key.split(':').nth(1).unwrap_or("default").to_string(); let database = pool_key.split(':').nth(1).unwrap_or("default").to_string();
let max_rows = options.max_rows; let max_rows = options.max_rows;
drop(connections);
let result = wait_for_query_opt( let result = wait_for_query_opt(
cancel_token, cancel_token,
query_timeout, query_timeout,
@@ -2106,7 +2090,6 @@ async fn do_execute_typed(
let client = client.clone(); let client = client.clone();
let database = pool_key.split(':').nth(1).unwrap_or("default").to_string(); let database = pool_key.split(':').nth(1).unwrap_or("default").to_string();
let max_rows = options.max_rows; let max_rows = options.max_rows;
drop(connections);
let result = wait_for_query_opt( let result = wait_for_query_opt(
cancel_token, cancel_token,
query_timeout, query_timeout,
@@ -2122,7 +2105,6 @@ async fn do_execute_typed(
PoolKind::VictoriaMetrics(client) => { PoolKind::VictoriaMetrics(client) => {
let client = client.clone(); let client = client.clone();
let max_rows = options.max_rows; let max_rows = options.max_rows;
drop(connections);
let result = wait_for_query_opt( let result = wait_for_query_opt(
cancel_token, cancel_token,
query_timeout, query_timeout,
@@ -2144,7 +2126,6 @@ async fn do_execute_typed(
let schema = schema_for_execution_context(pool_db_type, schema).map(|s| s.to_string()); let schema = schema_for_execution_context(pool_db_type, schema).map(|s| s.to_string());
let max_rows = options.max_rows; let max_rows = options.max_rows;
let rpc_timeout = query_timeout; let rpc_timeout = query_timeout;
drop(connections);
if is_canceled(&cancel_token) { if is_canceled(&cancel_token) {
return Err(canceled_error().into()); return Err(canceled_error().into());
} }
@@ -2203,7 +2184,6 @@ async fn do_execute_typed(
let database = database.unwrap_or_else(|| config.effective_database().unwrap_or("")).to_string(); let database = database.unwrap_or_else(|| config.effective_database().unwrap_or("")).to_string();
let max_rows = options.max_rows; let max_rows = options.max_rows;
let plugin_timeout = query_timeout; let plugin_timeout = query_timeout;
drop(connections);
wait_for_query_opt(cancel_token, query_timeout, async move { wait_for_query_opt(cancel_token, query_timeout, async move {
if let Some(session_id) = options.result_session_id.as_deref() { if let Some(session_id) = options.result_session_id.as_deref() {
let params = external_driver_fetch_query_page_params( let params = external_driver_fetch_query_page_params(
@@ -2233,7 +2213,6 @@ async fn do_execute_typed(
let client = client.clone(); let client = client.clone();
let sql = sql.to_string(); let sql = sql.to_string();
let max_rows = options.max_rows.unwrap_or(MAX_ROWS); let max_rows = options.max_rows.unwrap_or(MAX_ROWS);
drop(connections);
// Keep the AWS SDK cold-path future off this already-large query dispatcher stack. // Keep the AWS SDK cold-path future off this already-large query dispatcher stack.
let execution = Box::pin(db::dynamodb_driver::execute_statement(&client, &sql, max_rows)); let execution = Box::pin(db::dynamodb_driver::execute_statement(&client, &sql, max_rows));
wait_for_query_opt(cancel_token, query_timeout, execution).await wait_for_query_opt(cancel_token, query_timeout, execution).await
@@ -2726,8 +2705,8 @@ async fn execute_postgres_drop_database(
check_read_only_for_connection(state, &pool_key, sql).await?; check_read_only_for_connection(state, &pool_key, sql).await?;
let pool = { let pool = {
let connections = state.connections.read().await; let pool_handle = state.pool_handle(&pool_key).await;
match connections.get(&pool_key) { match pool_handle.as_ref() {
Some(PoolKind::Postgres(pool)) => pool.clone(), Some(PoolKind::Postgres(pool)) => pool.clone(),
Some(_) => return Err("DROP DATABASE reconnect did not create a PostgreSQL connection".to_string()), Some(_) => return Err("DROP DATABASE reconnect did not create a PostgreSQL connection".to_string()),
None => return Err("Connection not found".to_string()), None => return Err("Connection not found".to_string()),
@@ -2786,19 +2765,17 @@ pub async fn close_query_session(
let pool_database = query_pool_database(database, catalog); let pool_database = query_pool_database(database, catalog);
let pool_key = state.get_or_create_pool_for_session(connection_id, pool_database, client_session_id).await?; let pool_key = state.get_or_create_pool_for_session(connection_id, pool_database, client_session_id).await?;
let connections = state.connections.read().await; let pool_handle = state.pool_handle(&pool_key).await;
let pool = connections.get(&pool_key).ok_or("Connection not found")?; let pool = pool_handle.as_ref().ok_or("Connection not found")?;
match pool { match pool {
PoolKind::Agent(client) => { PoolKind::Agent(client) => {
let client = client.clone(); let client = client.clone();
drop(connections);
let mut client = client.lock().await; let mut client = client.lock().await;
client.close_query_session(session_id).await client.close_query_session(session_id).await
} }
PoolKind::ExternalDriver { config, session, .. } => { PoolKind::ExternalDriver { config, session, .. } => {
let config = config.clone(); let config = config.clone();
let session = session.clone(); let session = session.clone();
drop(connections);
let params = external_driver_fetch_query_page_params(config.as_ref(), session_id, 1); let params = external_driver_fetch_query_page_params(config.as_ref(), session_id, 1);
session session
.invoke::<serde_json::Value>("closeQuerySession", params) .invoke::<serde_json::Value>("closeQuerySession", params)
@@ -2807,13 +2784,11 @@ pub async fn close_query_session(
} }
PoolKind::Elasticsearch(client) => { PoolKind::Elasticsearch(client) => {
let client = client.clone(); let client = client.clone();
drop(connections);
db::elasticsearch_driver::close_cursor(&client, session_id).await?; db::elasticsearch_driver::close_cursor(&client, session_id).await?;
Ok(true) Ok(true)
} }
PoolKind::Easysearch(client) => { PoolKind::Easysearch(client) => {
let client = client.clone(); let client = client.clone();
drop(connections);
db::easysearch_driver::close_cursor(&client, session_id).await?; db::easysearch_driver::close_cursor(&client, session_id).await?;
Ok(true) Ok(true)
} }
@@ -2954,10 +2929,7 @@ pub async fn execute_multi_core_with_options_for_client_and_progress_typed(
state.touch_pool_activity(&pool_key).await; state.touch_pool_activity(&pool_key).await;
let _activity_touch = state.pool_activity_touch(pool_key.as_str()); let _activity_touch = state.pool_activity_touch(pool_key.as_str());
let is_sqlserver = { let is_sqlserver = { matches!(state.pool_handle(&pool_key).await, Some(PoolKind::SqlServer(_))) };
let connections = state.connections.read().await;
matches!(connections.get(&pool_key), Some(PoolKind::SqlServer(_)))
};
if is_sqlserver { if is_sqlserver {
return execute_multi_sqlserver(state, &pool_key, sql, cancel_token, options).await.map_err(Into::into); return execute_multi_sqlserver(state, &pool_key, sql, cancel_token, options).await.map_err(Into::into);
@@ -3015,8 +2987,8 @@ pub async fn execute_multi_core_with_options_for_client_and_progress_typed(
} }
let mysql_pool = { let mysql_pool = {
let connections = state.connections.read().await; let pool_handle = state.pool_handle(&pool_key).await;
match connections.get(&pool_key) { match pool_handle.as_ref() {
Some(PoolKind::Mysql(pool, mode)) => Some((pool.clone(), *mode)), Some(PoolKind::Mysql(pool, mode)) => Some((pool.clone(), *mode)),
_ => None, _ => None,
} }
@@ -3595,13 +3567,12 @@ async fn execute_multi_sqlserver(
break; break;
} }
let connections = state.connections.read().await; let pool_handle = state.pool_handle(pool_key).await;
let pool = connections.get(pool_key).ok_or("Connection not found")?; let pool = pool_handle.as_ref().ok_or("Connection not found")?;
let client = match pool { let client = match pool {
PoolKind::SqlServer(c) => c.clone(), PoolKind::SqlServer(c) => c.clone(),
_ => return Err("Expected SQL Server connection".to_string()), _ => return Err("Expected SQL Server connection".to_string()),
}; };
drop(connections);
let (mut client_guard, lock_wait_ms) = let (mut client_guard, lock_wait_ms) =
match lock_shared_client_with_wait(&client, cancel_token.clone(), query_timeout).await { match lock_shared_client_with_wait(&client, cancel_token.clone(), query_timeout).await {
@@ -3698,8 +3669,8 @@ pub async fn execute_statements(
let mysql_dialect = connection_mysql_query_dialect(state, connection_id).await; let mysql_dialect = connection_mysql_query_dialect(state, connection_id).await;
let agent_client = { let agent_client = {
let conns = state.connections.read().await; let pool_handle = state.pool_handle(&pool_key).await;
match conns.get(&pool_key) { match pool_handle.as_ref() {
Some(PoolKind::Agent(client)) => Some(client.clone()), Some(PoolKind::Agent(client)) => Some(client.clone()),
_ => None, _ => None,
} }
@@ -4047,10 +4018,8 @@ pub async fn execute_schema_diff_deploy(
} }
} }
}; };
let has_transactional_path = { let has_transactional_path =
let conns = state.connections.read().await; { state.pool_handle(&pool_key).await.as_ref().is_some_and(pool_kind_has_transactional_path) };
conns.get(&pool_key).is_some_and(pool_kind_has_transactional_path)
};
let atomicity = classify_schema_diff_atomicity(db_type, &parsed, has_transactional_path); let atomicity = classify_schema_diff_atomicity(db_type, &parsed, has_transactional_path);
match execute_statements_in_transaction_on_pool(state, &pool_key, connection_id, database, &parsed, schema, None) match execute_statements_in_transaction_on_pool(state, &pool_key, connection_id, database, &parsed, schema, None)
@@ -4190,8 +4159,7 @@ pub async fn execute_statements_in_transaction_on_pool_typed(
// Clone the pool handle within the lock, then drop it before any async work. // Clone the pool handle within the lock, then drop it before any async work.
let path = { let path = {
let conns = state.connections.read().await; state.pool_handle(pool_key).await.as_ref().map(|p| match p {
conns.get(pool_key).map(|p| match p {
PoolKind::Postgres(pg) => TxPath::Pg(pg.clone()), PoolKind::Postgres(pg) => TxPath::Pg(pg.clone()),
PoolKind::Mysql(mp, _mode) => TxPath::Mysql(mp.clone(), false), PoolKind::Mysql(mp, _mode) => TxPath::Mysql(mp.clone(), false),
PoolKind::Sqlite(sq) => TxPath::Sqlite(sq.clone()), PoolKind::Sqlite(sq) => TxPath::Sqlite(sq.clone()),
@@ -4736,8 +4704,8 @@ async fn begin_transaction_session(
ExternalDriver, ExternalDriver,
} }
let pool_handle = { let pool_handle = {
let connections = state.connections.read().await; let pool = state.pool_handle(&probe_pool_key).await.ok_or("Connection not found")?;
match connections.get(&probe_pool_key).ok_or("Connection not found")? { match &pool {
PoolKind::Postgres(pg) => TxnPoolHandle::Postgres(pg.clone()), PoolKind::Postgres(pg) => TxnPoolHandle::Postgres(pg.clone()),
PoolKind::Mysql(mp, _) => TxnPoolHandle::Mysql(mp.clone()), PoolKind::Mysql(mp, _) => TxnPoolHandle::Mysql(mp.clone()),
PoolKind::Agent(_) if !consistent_snapshot => TxnPoolHandle::Agent, PoolKind::Agent(_) if !consistent_snapshot => TxnPoolHandle::Agent,
@@ -4792,8 +4760,8 @@ async fn begin_transaction_session(
let agent_pool_key = let agent_pool_key =
state.get_or_create_pool_for_session(connection_id, pool_database, Some(&client_session_id)).await?; state.get_or_create_pool_for_session(connection_id, pool_database, Some(&client_session_id)).await?;
let client = { let client = {
let connections = state.connections.read().await; let pool_handle = state.pool_handle(&agent_pool_key).await;
match connections.get(&agent_pool_key) { match pool_handle.as_ref() {
Some(PoolKind::Agent(client)) => client.clone(), Some(PoolKind::Agent(client)) => client.clone(),
_ => { _ => {
let _ = state.close_client_session_pool(connection_id, pool_database, &client_session_id).await; let _ = state.close_client_session_pool(connection_id, pool_database, &client_session_id).await;
@@ -4830,8 +4798,8 @@ async fn begin_transaction_session(
let external_pool_key = let external_pool_key =
state.get_or_create_pool_for_session(connection_id, pool_database, Some(&client_session_id)).await?; state.get_or_create_pool_for_session(connection_id, pool_database, Some(&client_session_id)).await?;
let (config, session) = { let (config, session) = {
let connections = state.connections.read().await; let pool_handle = state.pool_handle(&external_pool_key).await;
match connections.get(&external_pool_key) { match pool_handle.as_ref() {
Some(PoolKind::ExternalDriver { config, session, .. }) => (config.clone(), session.clone()), Some(PoolKind::ExternalDriver { config, session, .. }) => (config.clone(), session.clone()),
_ => { _ => {
let _ = state.close_client_session_pool(connection_id, pool_database, &client_session_id).await; let _ = state.close_client_session_pool(connection_id, pool_database, &client_session_id).await;
@@ -6215,7 +6183,11 @@ for line in sys.stdin:
let client = db::dynamodb_driver::connect(&config, host, config.port).unwrap(); let client = db::dynamodb_driver::connect(&config, host, config.port).unwrap();
db::dynamodb_driver::test_connection(&client, Duration::from_secs(5)).await.unwrap(); db::dynamodb_driver::test_connection(&client, Duration::from_secs(5)).await.unwrap();
state.configs.write().await.insert(config.id.clone(), config.clone()); state.configs.write().await.insert(config.id.clone(), config.clone());
state.connections.write().await.insert(config.id.clone(), PoolKind::DynamoDb(client)); state
.update_connection_pools(|connections| {
connections.insert(config.id.clone(), PoolKind::DynamoDb(client));
})
.await;
let results = execute_multi_core_with_options_for_client_and_progress_typed( let results = execute_multi_core_with_options_for_client_and_progress_typed(
&state, &state,
@@ -6294,13 +6266,17 @@ for line in sys.stdin:
let storage = Storage::open(&dir.join("storage.db")).await.unwrap(); let storage = Storage::open(&dir.join("storage.db")).await.unwrap();
let state = AppState::new(storage); let state = AppState::new(storage);
state.configs.write().await.insert("conn-1".to_string(), test_connection_config(DatabaseType::Dameng)); state.configs.write().await.insert("conn-1".to_string(), test_connection_config(DatabaseType::Dameng));
state.connections.write().await.insert( state
"conn-1".to_string(), .update_connection_pools(|connections| {
PoolKind::agent(crate::db::agent_driver::AgentDriverClient::shared_session( connections.insert(
runtime.clone(), "conn-1".to_string(),
"session-1".to_string(), PoolKind::agent(crate::db::agent_driver::AgentDriverClient::shared_session(
)), runtime.clone(),
); "session-1".to_string(),
)),
);
})
.await;
(state, dir, runtime) (state, dir, runtime)
} }
@@ -6312,7 +6288,7 @@ for line in sys.stdin:
let error = execute_sql_statement(&state, "conn-1", "", "SELECT 1", None, None).await.unwrap_err(); let error = execute_sql_statement(&state, "conn-1", "", "SELECT 1", None, None).await.unwrap_err();
assert!(error.contains("injected Agent failure")); assert!(error.contains("injected Agent failure"));
assert!(!state.connections.read().await.contains_key("conn-1")); assert!(!state.pool_handle("conn-1").await.is_some());
assert!(runtime.is_failed()); assert!(runtime.is_failed());
runtime.kill(); runtime.kill();
@@ -6350,7 +6326,11 @@ for line in sys.stdin:
let state = AppState::new(storage); let state = AppState::new(storage);
let connection_id = "sqlite-cancel"; let connection_id = "sqlite-cancel";
let sqlite = db::sqlite::connect_path_create_if_missing(dir.join("query.db").to_str().unwrap()).await.unwrap(); let sqlite = db::sqlite::connect_path_create_if_missing(dir.join("query.db").to_str().unwrap()).await.unwrap();
state.connections.write().await.insert(connection_id.to_string(), PoolKind::Sqlite(sqlite)); state
.update_connection_pools(|connections| {
connections.insert(connection_id.to_string(), PoolKind::Sqlite(sqlite));
})
.await;
state.configs.write().await.insert(connection_id.to_string(), test_connection_config(DatabaseType::Sqlite)); state.configs.write().await.insert(connection_id.to_string(), test_connection_config(DatabaseType::Sqlite));
let cancel_token = CancellationToken::new(); let cancel_token = CancellationToken::new();
cancel_token.cancel(); cancel_token.cancel();
@@ -6434,7 +6414,7 @@ for line in sys.stdin:
.unwrap_err(); .unwrap_err();
assert!(error.contains("injected Agent failure")); assert!(error.contains("injected Agent failure"));
assert!(!state.connections.read().await.contains_key("conn-1")); assert!(!state.pool_handle("conn-1").await.is_some());
assert!(runtime.is_failed()); assert!(runtime.is_failed());
runtime.kill(); runtime.kill();
@@ -6451,7 +6431,7 @@ for line in sys.stdin:
.unwrap_err(); .unwrap_err();
assert!(error.contains("injected Agent failure")); assert!(error.contains("injected Agent failure"));
assert!(!state.connections.read().await.contains_key("conn-1")); assert!(!state.pool_handle("conn-1").await.is_some());
assert!(runtime.is_failed()); assert!(runtime.is_failed());
runtime.kill(); runtime.kill();
@@ -6465,7 +6445,7 @@ for line in sys.stdin:
let error = execute_sql_statement(&state, "conn-1", "", "SELECT 1", None, None).await.unwrap_err(); let error = execute_sql_statement(&state, "conn-1", "", "SELECT 1", None, None).await.unwrap_err();
assert!(error.contains("injected Agent failure")); assert!(error.contains("injected Agent failure"));
assert!(!state.connections.read().await.contains_key("conn-1")); assert!(!state.pool_handle("conn-1").await.is_some());
assert!(!runtime.is_failed()); assert!(!runtime.is_failed());
runtime.kill(); runtime.kill();
@@ -6526,7 +6506,11 @@ for line in sys.stdin:
let state = AppState::new(storage); let state = AppState::new(storage);
let connection_id = "sqlite-batch"; let connection_id = "sqlite-batch";
let sqlite = db::sqlite::connect_path_create_if_missing(dir.join("query.db").to_str().unwrap()).await.unwrap(); let sqlite = db::sqlite::connect_path_create_if_missing(dir.join("query.db").to_str().unwrap()).await.unwrap();
state.connections.write().await.insert(connection_id.to_string(), PoolKind::Sqlite(sqlite)); state
.update_connection_pools(|connections| {
connections.insert(connection_id.to_string(), PoolKind::Sqlite(sqlite));
})
.await;
state.configs.write().await.insert(connection_id.to_string(), test_connection_config(DatabaseType::Sqlite)); state.configs.write().await.insert(connection_id.to_string(), test_connection_config(DatabaseType::Sqlite));
let sql = if failure_first { let sql = if failure_first {
@@ -6622,7 +6606,11 @@ for line in sys.stdin:
let state = AppState::new(storage); let state = AppState::new(storage);
let connection_id = "gaussdb-on-error-stop"; let connection_id = "gaussdb-on-error-stop";
let sqlite = db::sqlite::connect_path_create_if_missing(dir.join("query.db").to_str().unwrap()).await.unwrap(); let sqlite = db::sqlite::connect_path_create_if_missing(dir.join("query.db").to_str().unwrap()).await.unwrap();
state.connections.write().await.insert(connection_id.to_string(), PoolKind::Sqlite(sqlite)); state
.update_connection_pools(|connections| {
connections.insert(connection_id.to_string(), PoolKind::Sqlite(sqlite));
})
.await;
state.configs.write().await.insert(connection_id.to_string(), test_connection_config(DatabaseType::Gaussdb)); state.configs.write().await.insert(connection_id.to_string(), test_connection_config(DatabaseType::Gaussdb));
let results = execute_multi_core_with_options( let results = execute_multi_core_with_options(
@@ -7766,14 +7754,18 @@ for line in sys.stdin:
let client_session_id = "manual-txn-test"; let client_session_id = "manual-txn-test";
let pool_key = "jdbc-conn:session:manual-txn-test"; let pool_key = "jdbc-conn:session:manual-txn-test";
let config = Arc::new(config); let config = Arc::new(config);
state.connections.write().await.insert( state
pool_key.to_string(), .update_connection_pools(|connections| {
PoolKind::ExternalDriver { connections.insert(
driver_id: "jdbc".to_string(), pool_key.to_string(),
config: config.clone(), PoolKind::ExternalDriver {
session: session.clone(), driver_id: "jdbc".to_string(),
}, config: config.clone(),
); session: session.clone(),
},
);
})
.await;
let cleanup_guard = let cleanup_guard =
state.workload_session_pool_cleanup_guard("jdbc-conn", Some("dbx_test"), client_session_id).await.unwrap(); state.workload_session_pool_cleanup_guard("jdbc-conn", Some("dbx_test"), client_session_id).await.unwrap();
state.transaction_sessions.write().await.insert( state.transaction_sessions.write().await.insert(
@@ -7799,7 +7791,7 @@ for line in sys.stdin:
execute_in_manual_transaction(&state, "txn-test", "SELECT 42", "dbx_test", None, Some(10)).await.unwrap(); execute_in_manual_transaction(&state, "txn-test", "SELECT 42", "dbx_test", None, Some(10)).await.unwrap();
assert_eq!(results[0].rows, vec![vec![serde_json::json!(42)]]); assert_eq!(results[0].rows, vec![vec![serde_json::json!(42)]]);
commit_manual_transaction(&state, "txn-test").await.unwrap(); commit_manual_transaction(&state, "txn-test").await.unwrap();
assert!(!state.connections.read().await.contains_key(pool_key)); assert!(!state.pool_handle(pool_key).await.is_some());
assert_eq!( assert_eq!(
std::fs::read_to_string(&calls).unwrap(), std::fs::read_to_string(&calls).unwrap(),
"beginManualTransaction\nexecuteInManualTransaction\ncommitManualTransaction\n" "beginManualTransaction\nexecuteInManualTransaction\ncommitManualTransaction\n"
+8 -12
View File
@@ -1037,14 +1037,13 @@ async fn try_export_postgres_query_result_stream(
) )
.await? .await?
}; };
let connections = state.connections.read().await; let pool_handle = state.pool_handle(&pool_key).await;
let Some(pool) = connections.get(&pool_key).and_then(|pool| match pool { let Some(pool) = pool_handle.as_ref().and_then(|pool| match pool {
PoolKind::Postgres(pool) => Some(pool.clone()), PoolKind::Postgres(pool) => Some(pool.clone()),
_ => None, _ => None,
}) else { }) else {
return Ok(false); return Ok(false);
}; };
drop(connections);
if let Some(execution_id) = request.execution_id.as_deref() { if let Some(execution_id) = request.execution_id.as_deref() {
state.running_queries.set_pool_key(execution_id, pool_key.clone()); state.running_queries.set_pool_key(execution_id, pool_key.clone());
@@ -1212,14 +1211,13 @@ async fn try_export_mysql_query_result_stream(
) )
.await? .await?
}; };
let connections = state.connections.read().await; let pool_handle = state.pool_handle(&pool_key).await;
let Some((pool, bare)) = connections.get(&pool_key).and_then(|pool| match pool { let Some((pool, bare)) = pool_handle.as_ref().and_then(|pool| match pool {
PoolKind::Mysql(pool, mode) => Some((pool.clone(), *mode == crate::connection::MysqlMode::Bare)), PoolKind::Mysql(pool, mode) => Some((pool.clone(), *mode == crate::connection::MysqlMode::Bare)),
_ => None, _ => None,
}) else { }) else {
return Ok(false); return Ok(false);
}; };
drop(connections);
if let Some(execution_id) = request.execution_id.as_deref() { if let Some(execution_id) = request.execution_id.as_deref() {
state.running_queries.set_pool_key(execution_id, pool_key.clone()); state.running_queries.set_pool_key(execution_id, pool_key.clone());
@@ -1487,14 +1485,13 @@ async fn try_export_clickhouse_query_result_stream(
) )
.await? .await?
}; };
let connections = state.connections.read().await; let pool_handle = state.pool_handle(&pool_key).await;
let Some(client) = connections.get(&pool_key).and_then(|pool| match pool { let Some(client) = pool_handle.as_ref().and_then(|pool| match pool {
PoolKind::ClickHouse(client) => Some(client.clone()), PoolKind::ClickHouse(client) => Some(client.clone()),
_ => None, _ => None,
}) else { }) else {
return Ok(false); return Ok(false);
}; };
drop(connections);
if let Some(execution_id) = request.execution_id.as_deref() { if let Some(execution_id) = request.execution_id.as_deref() {
state.running_queries.set_pool_key(execution_id, pool_key.clone()); state.running_queries.set_pool_key(execution_id, pool_key.clone());
@@ -1659,14 +1656,13 @@ async fn try_export_sqlserver_query_result_stream(
} }
let pool_key = state.get_or_create_pool(&request.connection_id, Some(&request.database)).await?; let pool_key = state.get_or_create_pool(&request.connection_id, Some(&request.database)).await?;
let connections = state.connections.read().await; let pool_handle = state.pool_handle(&pool_key).await;
let Some(client) = connections.get(&pool_key).and_then(|pool| match pool { let Some(client) = pool_handle.as_ref().and_then(|pool| match pool {
PoolKind::SqlServer(client) => Some(client.clone()), PoolKind::SqlServer(client) => Some(client.clone()),
_ => None, _ => None,
}) else { }) else {
return Ok(false); return Ok(false);
}; };
drop(connections);
if let Some(execution_id) = request.execution_id.as_deref() { if let Some(execution_id) = request.execution_id.as_deref() {
state.running_queries.set_pool_key(execution_id, pool_key); state.running_queries.set_pool_key(execution_id, pool_key);
+111 -123
View File
@@ -13,10 +13,9 @@ pub async fn redis_list_databases_core(
connection_id: &str, connection_id: &str,
) -> Result<Vec<RedisDatabaseInfo>, String> { ) -> Result<Vec<RedisDatabaseInfo>, String> {
ensure_redis_pool(state, connection_id).await?; ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Connection not found")?;
let pool = connections.get(connection_id).ok_or("Connection not found")?; match &pool {
match pool { PoolKind::Redis(redis) => match redis.as_ref() {
PoolKind::Redis(redis) => match redis {
RedisConnection::Direct(con) => { RedisConnection::Direct(con) => {
let mut con = con.lock().await; let mut con = con.lock().await;
redis_driver::list_databases(&mut *con).await redis_driver::list_databases(&mut *con).await
@@ -54,10 +53,9 @@ pub async fn redis_scan_keys_batch_core(
include_types: bool, include_types: bool,
) -> Result<RedisScanResult, String> { ) -> Result<RedisScanResult, String> {
ensure_redis_pool(state, connection_id).await?; ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Connection not found")?;
let pool = connections.get(connection_id).ok_or("Connection not found")?; match &pool {
match pool { PoolKind::Redis(redis) => match redis.as_ref() {
PoolKind::Redis(redis) => match redis {
RedisConnection::Direct(con) => { RedisConnection::Direct(con) => {
let mut con = con.lock().await; let mut con = con.lock().await;
redis_driver::select_db(&mut *con, db).await?; redis_driver::select_db(&mut *con, db).await?;
@@ -85,10 +83,9 @@ pub async fn redis_scan_values_core(
count: usize, count: usize,
) -> Result<RedisScanResult, String> { ) -> Result<RedisScanResult, String> {
ensure_redis_pool(state, connection_id).await?; ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Connection not found")?;
let pool = connections.get(connection_id).ok_or("Connection not found")?; match &pool {
match pool { PoolKind::Redis(redis) => match redis.as_ref() {
PoolKind::Redis(redis) => match redis {
RedisConnection::Direct(con) => { RedisConnection::Direct(con) => {
let mut con = con.lock().await; let mut con = con.lock().await;
redis_driver::select_db(&mut *con, db).await?; redis_driver::select_db(&mut *con, db).await?;
@@ -115,12 +112,11 @@ pub async fn redis_get_value_in_db_core(
key_raw: &str, key_raw: &str,
) -> Result<RedisValue, String> { ) -> Result<RedisValue, String> {
ensure_redis_pool(state, connection_id).await?; ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Connection not found")?;
let pool = connections.get(connection_id).ok_or("Connection not found")?; match &pool {
match pool {
PoolKind::Redis(redis) => { PoolKind::Redis(redis) => {
let key = redis_driver::redis_key_raw_to_bytes(key_raw)?; let key = redis_driver::redis_key_raw_to_bytes(key_raw)?;
match redis { match redis.as_ref() {
RedisConnection::Direct(con) => { RedisConnection::Direct(con) => {
let mut con = con.lock().await; let mut con = con.lock().await;
redis_driver::select_db(&mut *con, db).await?; redis_driver::select_db(&mut *con, db).await?;
@@ -146,12 +142,11 @@ pub async fn redis_get_ttl_in_db_core(
key_raw: &str, key_raw: &str,
) -> Result<i64, String> { ) -> Result<i64, String> {
ensure_redis_pool(state, connection_id).await?; ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Connection not found")?;
let pool = connections.get(connection_id).ok_or("Connection not found")?; match &pool {
match pool {
PoolKind::Redis(redis) => { PoolKind::Redis(redis) => {
let key = redis_driver::redis_key_raw_to_bytes(key_raw)?; let key = redis_driver::redis_key_raw_to_bytes(key_raw)?;
match redis { match redis.as_ref() {
RedisConnection::Direct(con) => { RedisConnection::Direct(con) => {
let mut con = con.lock().await; let mut con = con.lock().await;
redis_driver::select_db(&mut *con, db).await?; redis_driver::select_db(&mut *con, db).await?;
@@ -176,12 +171,11 @@ pub async fn redis_stream_entries_in_db_core(
cursor: Option<&str>, cursor: Option<&str>,
) -> Result<RedisStreamPage, String> { ) -> Result<RedisStreamPage, String> {
ensure_redis_pool(state, connection_id).await?; ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Connection not found")?;
let pool = connections.get(connection_id).ok_or("Connection not found")?; match &pool {
match pool {
PoolKind::Redis(redis) => { PoolKind::Redis(redis) => {
let key = redis_driver::redis_key_raw_to_bytes(key_raw)?; let key = redis_driver::redis_key_raw_to_bytes(key_raw)?;
match redis { match redis.as_ref() {
RedisConnection::Direct(con) => { RedisConnection::Direct(con) => {
let mut con = con.lock().await; let mut con = con.lock().await;
redis_driver::select_db(&mut *con, db).await?; redis_driver::select_db(&mut *con, db).await?;
@@ -205,12 +199,11 @@ pub async fn redis_stream_groups_in_db_core(
key_raw: &str, key_raw: &str,
) -> Result<Vec<RedisStreamGroup>, String> { ) -> Result<Vec<RedisStreamGroup>, String> {
ensure_redis_pool(state, connection_id).await?; ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Connection not found")?;
let pool = connections.get(connection_id).ok_or("Connection not found")?; match &pool {
match pool {
PoolKind::Redis(redis) => { PoolKind::Redis(redis) => {
let key = redis_driver::redis_key_raw_to_bytes(key_raw)?; let key = redis_driver::redis_key_raw_to_bytes(key_raw)?;
match redis { match redis.as_ref() {
RedisConnection::Direct(con) => { RedisConnection::Direct(con) => {
let mut con = con.lock().await; let mut con = con.lock().await;
redis_driver::select_db(&mut *con, db).await?; redis_driver::select_db(&mut *con, db).await?;
@@ -235,13 +228,12 @@ pub async fn redis_stream_consumers_in_db_core(
group_raw: &str, group_raw: &str,
) -> Result<Vec<RedisStreamConsumer>, String> { ) -> Result<Vec<RedisStreamConsumer>, String> {
ensure_redis_pool(state, connection_id).await?; ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Connection not found")?;
let pool = connections.get(connection_id).ok_or("Connection not found")?; match &pool {
match pool {
PoolKind::Redis(redis) => { PoolKind::Redis(redis) => {
let key = redis_driver::redis_key_raw_to_bytes(key_raw)?; let key = redis_driver::redis_key_raw_to_bytes(key_raw)?;
let group = redis_driver::redis_key_raw_to_bytes(group_raw)?; let group = redis_driver::redis_key_raw_to_bytes(group_raw)?;
match redis { match redis.as_ref() {
RedisConnection::Direct(con) => { RedisConnection::Direct(con) => {
let mut con = con.lock().await; let mut con = con.lock().await;
redis_driver::select_db(&mut *con, db).await?; redis_driver::select_db(&mut *con, db).await?;
@@ -268,14 +260,13 @@ pub async fn redis_stream_pending_in_db_core(
consumer_raw: Option<&str>, consumer_raw: Option<&str>,
) -> Result<RedisStreamPendingPage, String> { ) -> Result<RedisStreamPendingPage, String> {
ensure_redis_pool(state, connection_id).await?; ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Connection not found")?;
let pool = connections.get(connection_id).ok_or("Connection not found")?; match &pool {
match pool {
PoolKind::Redis(redis) => { PoolKind::Redis(redis) => {
let key = redis_driver::redis_key_raw_to_bytes(key_raw)?; let key = redis_driver::redis_key_raw_to_bytes(key_raw)?;
let group = redis_driver::redis_key_raw_to_bytes(group_raw)?; let group = redis_driver::redis_key_raw_to_bytes(group_raw)?;
let consumer = consumer_raw.map(redis_driver::redis_key_raw_to_bytes).transpose()?; let consumer = consumer_raw.map(redis_driver::redis_key_raw_to_bytes).transpose()?;
match redis { match redis.as_ref() {
RedisConnection::Direct(con) => { RedisConnection::Direct(con) => {
let mut con = con.lock().await; let mut con = con.lock().await;
redis_driver::select_db(&mut *con, db).await?; redis_driver::select_db(&mut *con, db).await?;
@@ -311,12 +302,11 @@ pub async fn redis_set_string_in_db_core(
ttl: Option<i64>, ttl: Option<i64>,
) -> Result<(), String> { ) -> Result<(), String> {
ensure_redis_pool(state, connection_id).await?; ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Connection not found")?;
let pool = connections.get(connection_id).ok_or("Connection not found")?; match &pool {
match pool {
PoolKind::Redis(redis) => { PoolKind::Redis(redis) => {
let key = redis_driver::redis_key_raw_to_bytes(key_raw)?; let key = redis_driver::redis_key_raw_to_bytes(key_raw)?;
match redis { match redis.as_ref() {
RedisConnection::Direct(con) => { RedisConnection::Direct(con) => {
let mut con = con.lock().await; let mut con = con.lock().await;
redis_driver::select_db(&mut *con, db).await?; redis_driver::select_db(&mut *con, db).await?;
@@ -344,12 +334,11 @@ pub async fn redis_delete_key_in_db_core(
key_raw: &str, key_raw: &str,
) -> Result<(), String> { ) -> Result<(), String> {
ensure_redis_pool(state, connection_id).await?; ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Connection not found")?;
let pool = connections.get(connection_id).ok_or("Connection not found")?; match &pool {
match pool {
PoolKind::Redis(redis) => { PoolKind::Redis(redis) => {
let key = redis_driver::redis_key_raw_to_bytes(key_raw)?; let key = redis_driver::redis_key_raw_to_bytes(key_raw)?;
match redis { match redis.as_ref() {
RedisConnection::Direct(con) => { RedisConnection::Direct(con) => {
let mut con = con.lock().await; let mut con = con.lock().await;
redis_driver::select_db(&mut *con, db).await?; redis_driver::select_db(&mut *con, db).await?;
@@ -374,15 +363,14 @@ pub async fn redis_rename_key_in_db_core(
new_key_raw: &str, new_key_raw: &str,
) -> Result<(), String> { ) -> Result<(), String> {
ensure_redis_pool(state, connection_id).await?; ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Connection not found")?;
let pool = connections.get(connection_id).ok_or("Connection not found")?;
let key = redis_driver::redis_key_raw_to_bytes(key_raw)?; let key = redis_driver::redis_key_raw_to_bytes(key_raw)?;
let new_key = redis_driver::redis_key_raw_to_bytes(new_key_raw)?; let new_key = redis_driver::redis_key_raw_to_bytes(new_key_raw)?;
if key == new_key { if key == new_key {
return Ok(()); return Ok(());
} }
match pool { match &pool {
PoolKind::Redis(redis) => match redis { PoolKind::Redis(redis) => match redis.as_ref() {
RedisConnection::Direct(con) => { RedisConnection::Direct(con) => {
let mut con = con.lock().await; let mut con = con.lock().await;
redis_driver::select_db(&mut *con, db).await?; redis_driver::select_db(&mut *con, db).await?;
@@ -419,11 +407,11 @@ pub async fn redis_hash_set_in_db_core(
ttl: Option<i64>, ttl: Option<i64>,
) -> Result<(), String> { ) -> Result<(), String> {
ensure_redis_pool(state, connection_id).await?; ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::Redis(redis) => { PoolKind::Redis(redis) => {
let key = redis_driver::redis_key_raw_to_bytes(key_raw)?; let key = redis_driver::redis_key_raw_to_bytes(key_raw)?;
match redis { match redis.as_ref() {
RedisConnection::Direct(con) => { RedisConnection::Direct(con) => {
let mut con = con.lock().await; let mut con = con.lock().await;
redis_driver::select_db(&mut *con, db).await?; redis_driver::select_db(&mut *con, db).await?;
@@ -452,11 +440,11 @@ pub async fn redis_hash_del_in_db_core(
field: &str, field: &str,
) -> Result<(), String> { ) -> Result<(), String> {
ensure_redis_pool(state, connection_id).await?; ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::Redis(redis) => { PoolKind::Redis(redis) => {
let key = redis_driver::redis_key_raw_to_bytes(key_raw)?; let key = redis_driver::redis_key_raw_to_bytes(key_raw)?;
match redis { match redis.as_ref() {
RedisConnection::Direct(con) => { RedisConnection::Direct(con) => {
let mut con = con.lock().await; let mut con = con.lock().await;
redis_driver::select_db(&mut *con, db).await?; redis_driver::select_db(&mut *con, db).await?;
@@ -489,11 +477,11 @@ pub async fn redis_hash_field_update_in_db_core(
value: &str, value: &str,
) -> Result<(), String> { ) -> Result<(), String> {
ensure_redis_pool(state, connection_id).await?; ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::Redis(redis) => { PoolKind::Redis(redis) => {
let key = redis_driver::redis_key_raw_to_bytes(key_raw)?; let key = redis_driver::redis_key_raw_to_bytes(key_raw)?;
match redis { match redis.as_ref() {
RedisConnection::Direct(con) => { RedisConnection::Direct(con) => {
let mut con = con.lock().await; let mut con = con.lock().await;
redis_driver::select_db(&mut *con, db).await?; redis_driver::select_db(&mut *con, db).await?;
@@ -519,11 +507,11 @@ pub async fn redis_hash_field_set_ttl_in_db_core(
ttl: i64, ttl: i64,
) -> Result<(), String> { ) -> Result<(), String> {
ensure_redis_pool(state, connection_id).await?; ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::Redis(redis) => { PoolKind::Redis(redis) => {
let key = redis_driver::redis_key_raw_to_bytes(key_raw)?; let key = redis_driver::redis_key_raw_to_bytes(key_raw)?;
match redis { match redis.as_ref() {
RedisConnection::Direct(con) => { RedisConnection::Direct(con) => {
let mut con = con.lock().await; let mut con = con.lock().await;
redis_driver::select_db(&mut *con, db).await?; redis_driver::select_db(&mut *con, db).await?;
@@ -549,11 +537,11 @@ pub async fn redis_hash_field_set_expire_at_in_db_core(
expire_at: i64, expire_at: i64,
) -> Result<(), String> { ) -> Result<(), String> {
ensure_redis_pool(state, connection_id).await?; ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::Redis(redis) => { PoolKind::Redis(redis) => {
let key = redis_driver::redis_key_raw_to_bytes(key_raw)?; let key = redis_driver::redis_key_raw_to_bytes(key_raw)?;
match redis { match redis.as_ref() {
RedisConnection::Direct(con) => { RedisConnection::Direct(con) => {
let mut con = con.lock().await; let mut con = con.lock().await;
redis_driver::select_db(&mut *con, db).await?; redis_driver::select_db(&mut *con, db).await?;
@@ -589,11 +577,11 @@ pub async fn redis_list_push_in_db_core(
ttl: Option<i64>, ttl: Option<i64>,
) -> Result<(), String> { ) -> Result<(), String> {
ensure_redis_pool(state, connection_id).await?; ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::Redis(redis) => { PoolKind::Redis(redis) => {
let key = redis_driver::redis_key_raw_to_bytes(key_raw)?; let key = redis_driver::redis_key_raw_to_bytes(key_raw)?;
match redis { match redis.as_ref() {
RedisConnection::Direct(con) => { RedisConnection::Direct(con) => {
let mut con = con.lock().await; let mut con = con.lock().await;
redis_driver::select_db(&mut *con, db).await?; redis_driver::select_db(&mut *con, db).await?;
@@ -619,11 +607,11 @@ pub async fn redis_list_set_in_db_core(
value: &str, value: &str,
) -> Result<(), String> { ) -> Result<(), String> {
ensure_redis_pool(state, connection_id).await?; ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::Redis(redis) => { PoolKind::Redis(redis) => {
let key = redis_driver::redis_key_raw_to_bytes(key_raw)?; let key = redis_driver::redis_key_raw_to_bytes(key_raw)?;
match redis { match redis.as_ref() {
RedisConnection::Direct(con) => { RedisConnection::Direct(con) => {
let mut con = con.lock().await; let mut con = con.lock().await;
redis_driver::select_db(&mut *con, db).await?; redis_driver::select_db(&mut *con, db).await?;
@@ -657,11 +645,11 @@ pub async fn redis_list_remove_in_db_core(
index: i64, index: i64,
) -> Result<(), String> { ) -> Result<(), String> {
ensure_redis_pool(state, connection_id).await?; ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::Redis(redis) => { PoolKind::Redis(redis) => {
let key = redis_driver::redis_key_raw_to_bytes(key_raw)?; let key = redis_driver::redis_key_raw_to_bytes(key_raw)?;
match redis { match redis.as_ref() {
RedisConnection::Direct(con) => { RedisConnection::Direct(con) => {
let mut con = con.lock().await; let mut con = con.lock().await;
redis_driver::select_db(&mut *con, db).await?; redis_driver::select_db(&mut *con, db).await?;
@@ -697,11 +685,11 @@ pub async fn redis_set_add_in_db_core(
ttl: Option<i64>, ttl: Option<i64>,
) -> Result<(), String> { ) -> Result<(), String> {
ensure_redis_pool(state, connection_id).await?; ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::Redis(redis) => { PoolKind::Redis(redis) => {
let key = redis_driver::redis_key_raw_to_bytes(key_raw)?; let key = redis_driver::redis_key_raw_to_bytes(key_raw)?;
match redis { match redis.as_ref() {
RedisConnection::Direct(con) => { RedisConnection::Direct(con) => {
let mut con = con.lock().await; let mut con = con.lock().await;
redis_driver::select_db(&mut *con, db).await?; redis_driver::select_db(&mut *con, db).await?;
@@ -735,11 +723,11 @@ pub async fn redis_set_remove_in_db_core(
member: &str, member: &str,
) -> Result<(), String> { ) -> Result<(), String> {
ensure_redis_pool(state, connection_id).await?; ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::Redis(redis) => { PoolKind::Redis(redis) => {
let key = redis_driver::redis_key_raw_to_bytes(key_raw)?; let key = redis_driver::redis_key_raw_to_bytes(key_raw)?;
match redis { match redis.as_ref() {
RedisConnection::Direct(con) => { RedisConnection::Direct(con) => {
let mut con = con.lock().await; let mut con = con.lock().await;
redis_driver::select_db(&mut *con, db).await?; redis_driver::select_db(&mut *con, db).await?;
@@ -766,11 +754,11 @@ pub async fn redis_zadd_in_db_core(
ttl: Option<i64>, ttl: Option<i64>,
) -> Result<(), String> { ) -> Result<(), String> {
ensure_redis_pool(state, connection_id).await?; ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::Redis(redis) => { PoolKind::Redis(redis) => {
let key = redis_driver::redis_key_raw_to_bytes(key_raw)?; let key = redis_driver::redis_key_raw_to_bytes(key_raw)?;
match redis { match redis.as_ref() {
RedisConnection::Direct(con) => { RedisConnection::Direct(con) => {
let mut con = con.lock().await; let mut con = con.lock().await;
redis_driver::select_db(&mut *con, db).await?; redis_driver::select_db(&mut *con, db).await?;
@@ -795,11 +783,11 @@ pub async fn redis_zrem_in_db_core(
member: &str, member: &str,
) -> Result<(), String> { ) -> Result<(), String> {
ensure_redis_pool(state, connection_id).await?; ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::Redis(redis) => { PoolKind::Redis(redis) => {
let key = redis_driver::redis_key_raw_to_bytes(key_raw)?; let key = redis_driver::redis_key_raw_to_bytes(key_raw)?;
match redis { match redis.as_ref() {
RedisConnection::Direct(con) => { RedisConnection::Direct(con) => {
let mut con = con.lock().await; let mut con = con.lock().await;
redis_driver::select_db(&mut *con, db).await?; redis_driver::select_db(&mut *con, db).await?;
@@ -827,11 +815,11 @@ pub async fn redis_zset_update_in_db_core(
score: &str, score: &str,
) -> Result<bool, String> { ) -> Result<bool, String> {
ensure_redis_pool(state, connection_id).await?; ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::Redis(redis) => { PoolKind::Redis(redis) => {
let key = redis_driver::redis_key_raw_to_bytes(key_raw)?; let key = redis_driver::redis_key_raw_to_bytes(key_raw)?;
match redis { match redis.as_ref() {
RedisConnection::Direct(con) => { RedisConnection::Direct(con) => {
let mut con = con.lock().await; let mut con = con.lock().await;
redis_driver::select_db(&mut *con, db).await?; redis_driver::select_db(&mut *con, db).await?;
@@ -858,11 +846,11 @@ pub async fn redis_stream_add_in_db_core(
ttl: Option<i64>, ttl: Option<i64>,
) -> Result<(), String> { ) -> Result<(), String> {
ensure_redis_pool(state, connection_id).await?; ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::Redis(redis) => { PoolKind::Redis(redis) => {
let key = redis_driver::redis_key_raw_to_bytes(key_raw)?; let key = redis_driver::redis_key_raw_to_bytes(key_raw)?;
match redis { match redis.as_ref() {
RedisConnection::Direct(con) => { RedisConnection::Direct(con) => {
let mut con = con.lock().await; let mut con = con.lock().await;
redis_driver::select_db(&mut *con, db).await?; redis_driver::select_db(&mut *con, db).await?;
@@ -888,11 +876,11 @@ pub async fn redis_json_set_in_db_core(
ttl: Option<i64>, ttl: Option<i64>,
) -> Result<(), String> { ) -> Result<(), String> {
ensure_redis_pool(state, connection_id).await?; ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::Redis(redis) => { PoolKind::Redis(redis) => {
let key = redis_driver::redis_key_raw_to_bytes(key_raw)?; let key = redis_driver::redis_key_raw_to_bytes(key_raw)?;
match redis { match redis.as_ref() {
RedisConnection::Direct(con) => { RedisConnection::Direct(con) => {
let mut con = con.lock().await; let mut con = con.lock().await;
redis_driver::select_db(&mut *con, db).await?; redis_driver::select_db(&mut *con, db).await?;
@@ -915,9 +903,9 @@ pub async fn redis_check_json_module_in_db_core(
db: u32, db: u32,
) -> Result<bool, String> { ) -> Result<bool, String> {
ensure_redis_pool(state, connection_id).await?; ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::Redis(redis) => match redis { PoolKind::Redis(redis) => match redis.as_ref() {
RedisConnection::Direct(con) => { RedisConnection::Direct(con) => {
let mut con = con.lock().await; let mut con = con.lock().await;
redis_driver::select_db(&mut *con, db).await?; redis_driver::select_db(&mut *con, db).await?;
@@ -941,11 +929,11 @@ pub async fn redis_set_ttl_in_db_core(
ttl: i64, ttl: i64,
) -> Result<(), String> { ) -> Result<(), String> {
ensure_redis_pool(state, connection_id).await?; ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::Redis(redis) => { PoolKind::Redis(redis) => {
let key = redis_driver::redis_key_raw_to_bytes(key_raw)?; let key = redis_driver::redis_key_raw_to_bytes(key_raw)?;
match redis { match redis.as_ref() {
RedisConnection::Direct(con) => { RedisConnection::Direct(con) => {
let mut con = con.lock().await; let mut con = con.lock().await;
redis_driver::select_db(&mut *con, db).await?; redis_driver::select_db(&mut *con, db).await?;
@@ -970,11 +958,11 @@ pub async fn redis_set_expire_at_in_db_core(
expire_at: i64, expire_at: i64,
) -> Result<(), String> { ) -> Result<(), String> {
ensure_redis_pool(state, connection_id).await?; ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::Redis(redis) => { PoolKind::Redis(redis) => {
let key = redis_driver::redis_key_raw_to_bytes(key_raw)?; let key = redis_driver::redis_key_raw_to_bytes(key_raw)?;
match redis { match redis.as_ref() {
RedisConnection::Direct(con) => { RedisConnection::Direct(con) => {
let mut con = con.lock().await; let mut con = con.lock().await;
redis_driver::select_db(&mut *con, db).await?; redis_driver::select_db(&mut *con, db).await?;
@@ -998,13 +986,13 @@ pub async fn redis_delete_keys_in_db_core(
key_raws: &[String], key_raws: &[String],
) -> Result<u64, String> { ) -> Result<u64, String> {
ensure_redis_pool(state, connection_id).await?; ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::Redis(redis) => { PoolKind::Redis(redis) => {
let keys: Result<Vec<Vec<u8>>, String> = let keys: Result<Vec<Vec<u8>>, String> =
key_raws.iter().map(|k| redis_driver::redis_key_raw_to_bytes(k)).collect(); key_raws.iter().map(|k| redis_driver::redis_key_raw_to_bytes(k)).collect();
let keys = keys?; let keys = keys?;
match redis { match redis.as_ref() {
RedisConnection::Direct(con) => { RedisConnection::Direct(con) => {
let mut con = con.lock().await; let mut con = con.lock().await;
redis_driver::select_db(&mut *con, db).await?; redis_driver::select_db(&mut *con, db).await?;
@@ -1027,9 +1015,9 @@ pub async fn redis_delete_keys_in_db_core(
pub async fn redis_flush_db_core(state: &AppState, connection_id: &str, db: u32) -> Result<(), String> { pub async fn redis_flush_db_core(state: &AppState, connection_id: &str, db: u32) -> Result<(), String> {
ensure_redis_pool(state, connection_id).await?; ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::Redis(redis) => match redis { PoolKind::Redis(redis) => match redis.as_ref() {
RedisConnection::Direct(con) => { RedisConnection::Direct(con) => {
let mut con = con.lock().await; let mut con = con.lock().await;
redis_driver::select_db(&mut *con, db).await?; redis_driver::select_db(&mut *con, db).await?;
@@ -1052,9 +1040,9 @@ pub async fn redis_execute_command_core(
skip_safety_check: bool, skip_safety_check: bool,
) -> Result<RedisCommandResult, String> { ) -> Result<RedisCommandResult, String> {
ensure_redis_pool(state, connection_id).await?; ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::Redis(redis) => match redis { PoolKind::Redis(redis) => match redis.as_ref() {
RedisConnection::Direct(con) => { RedisConnection::Direct(con) => {
let mut con = con.lock().await; let mut con = con.lock().await;
redis_driver::execute_console_command(&mut *con, db, command, skip_safety_check).await redis_driver::execute_console_command(&mut *con, db, command, skip_safety_check).await
@@ -1104,11 +1092,11 @@ pub async fn redis_load_more_in_db_core(
sort_direction: Option<&str>, sort_direction: Option<&str>,
) -> Result<RedisCollectionPage, String> { ) -> Result<RedisCollectionPage, String> {
ensure_redis_pool(state, connection_id).await?; ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::Redis(redis) => { PoolKind::Redis(redis) => {
let key = redis_driver::redis_key_raw_to_bytes(key_raw)?; let key = redis_driver::redis_key_raw_to_bytes(key_raw)?;
match redis { match redis.as_ref() {
RedisConnection::Direct(con) => { RedisConnection::Direct(con) => {
let mut con = con.lock().await; let mut con = con.lock().await;
redis_driver::select_db(&mut *con, db).await?; redis_driver::select_db(&mut *con, db).await?;
@@ -1135,9 +1123,9 @@ pub async fn redis_publish_core(
message: &str, message: &str,
) -> Result<u64, String> { ) -> Result<u64, String> {
ensure_redis_pool(state, connection_id).await?; ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::Redis(redis) => match redis { PoolKind::Redis(redis) => match redis.as_ref() {
RedisConnection::Direct(con) => { RedisConnection::Direct(con) => {
let mut con = con.lock().await; let mut con = con.lock().await;
redis_driver::select_db(&mut *con, db).await?; redis_driver::select_db(&mut *con, db).await?;
@@ -1175,9 +1163,9 @@ pub async fn redis_slowlog_get_core(
node_port: Option<u16>, node_port: Option<u16>,
) -> Result<Vec<redis_driver::RedisSlowlogEntry>, String> { ) -> Result<Vec<redis_driver::RedisSlowlogEntry>, String> {
ensure_redis_pool(state, connection_id).await?; ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::Redis(redis) => match redis { PoolKind::Redis(redis) => match redis.as_ref() {
RedisConnection::Direct(con) => { RedisConnection::Direct(con) => {
let mut con = con.lock().await; let mut con = con.lock().await;
// SLOWLOG is a server-level command, no select_db needed // SLOWLOG is a server-level command, no select_db needed
@@ -1203,9 +1191,9 @@ pub async fn redis_cluster_master_nodes_core(
connection_id: &str, connection_id: &str,
) -> Result<Vec<redis_driver::RedisNodeEndpoint>, String> { ) -> Result<Vec<redis_driver::RedisNodeEndpoint>, String> {
ensure_redis_pool(state, connection_id).await?; ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match connections.get(connection_id).ok_or("Not found")? { match &pool {
PoolKind::Redis(redis) => match redis { PoolKind::Redis(redis) => match redis.as_ref() {
RedisConnection::Cluster(cluster) => redis_driver::cluster_master_nodes(cluster).await, RedisConnection::Cluster(cluster) => redis_driver::cluster_master_nodes(cluster).await,
_ => Ok(Vec::new()), _ => Ok(Vec::new()),
}, },
File diff suppressed because it is too large. Load diff
+8 -8
View File
@@ -161,8 +161,8 @@ impl MySqlSqlFileExecutor {
let database = (!database.is_empty()).then_some(database); let database = (!database.is_empty()).then_some(database);
let pool_key = state.get_or_create_pool_for_session(&request.connection_id, database, None).await?; let pool_key = state.get_or_create_pool_for_session(&request.connection_id, database, None).await?;
let (db_type, driver_profile, bare) = { let (db_type, driver_profile, bare) = {
let connections = state.connections.read().await; let pool_handle = state.pool_handle(&pool_key).await;
let Some(PoolKind::Mysql(_, mode)) = connections.get(&pool_key) else { let Some(PoolKind::Mysql(_, mode)) = pool_handle.as_ref() else {
return Ok(None); return Ok(None);
}; };
(Some(target.db_type), target.driver_profile.as_deref(), *mode == crate::connection::MysqlMode::Bare) (Some(target.db_type), target.driver_profile.as_deref(), *mode == crate::connection::MysqlMode::Bare)
@@ -303,8 +303,8 @@ impl MySqlSqlFileExecutor {
let database = (!database.is_empty()).then_some(database); let database = (!database.is_empty()).then_some(database);
self.pool_key = state.get_or_create_pool_for_session(&self.connection_id, database, None).await?; self.pool_key = state.get_or_create_pool_for_session(&self.connection_id, database, None).await?;
let pool = { let pool = {
let connections = state.connections.read().await; let pool_handle = state.pool_handle(&self.pool_key).await;
match connections.get(&self.pool_key) { match pool_handle.as_ref() {
Some(PoolKind::Mysql(pool, _)) => pool.clone(), Some(PoolKind::Mysql(pool, _)) => pool.clone(),
Some(_) => return Err("SQL file import expected a MySQL-compatible pooled connection".to_string()), Some(_) => return Err("SQL file import expected a MySQL-compatible pooled connection".to_string()),
None => return Err("Connection not found".to_string()), None => return Err("Connection not found".to_string()),
@@ -2276,10 +2276,10 @@ mod tests {
let pool = crate::db::sqlite::connect_path(":memory:").await.unwrap(); let pool = crate::db::sqlite::connect_path(":memory:").await.unwrap();
state state
.connections .update_connection_pools(|connections| {
.write() connections.insert("gauss-stream".to_string(), crate::connection::PoolKind::Sqlite(pool.clone()));
.await })
.insert("gauss-stream".to_string(), crate::connection::PoolKind::Sqlite(pool.clone())); .await;
let path = temporary_sql_file( let path = temporary_sql_file(
b"CREATE TABLE side_effects(value INTEGER);\nINSERT INTO missing_before_control VALUES (1);\nINSERT INTO side_effects VALUES (1);\n\\set ON_ERROR_STOP on\nINSERT INTO missing_after_control VALUES (1);\nINSERT INTO side_effects VALUES (2);", b"CREATE TABLE side_effects(value INTEGER);\nINSERT INTO missing_before_control VALUES (1);\nINSERT INTO side_effects VALUES (1);\n\\set ON_ERROR_STOP on\nINSERT INTO missing_after_control VALUES (1);\nINSERT INTO side_effects VALUES (2);",
+23 -25
View File
@@ -519,8 +519,8 @@ enum TableExportCursorSession {
} }
async fn table_export_cursor_kind(state: &AppState, pool_key: &str) -> Option<TableExportCursorKind> { async fn table_export_cursor_kind(state: &AppState, pool_key: &str) -> Option<TableExportCursorKind> {
let connections = state.connections.read().await; let pool_handle = state.pool_handle(pool_key).await;
match connections.get(pool_key) { match pool_handle.as_ref() {
Some(PoolKind::Agent(_)) => Some(TableExportCursorKind::Agent), Some(PoolKind::Agent(_)) => Some(TableExportCursorKind::Agent),
Some(PoolKind::ExternalDriver { .. }) => Some(TableExportCursorKind::ExternalDriver), Some(PoolKind::ExternalDriver { .. }) => Some(TableExportCursorKind::ExternalDriver),
_ => None, _ => None,
@@ -677,12 +677,11 @@ async fn fetch_table_export_batch(
fetch_size: Some(active_batch_size), fetch_size: Some(active_batch_size),
timeout_secs: (query_timeout > 0).then_some(query_timeout), timeout_secs: (query_timeout > 0).then_some(query_timeout),
}; };
let connections = state.connections.read().await; let pool_handle = state.pool_handle(pool_key).await;
let Some(PoolKind::Agent(client)) = connections.get(pool_key) else { let Some(PoolKind::Agent(client)) = pool_handle.as_ref() else {
return Err("Agent table read requires an agent connection".to_string()); return Err("Agent table read requires an agent connection".to_string());
}; };
let client = client.clone(); let client = client.clone();
drop(connections);
let mut client = client.lock().await; let mut client = client.lock().await;
match client.start_table_read::<QueryResult>(params).await { match client.start_table_read::<QueryResult>(params).await {
Ok(result) => { Ok(result) => {
@@ -731,12 +730,11 @@ async fn fetch_table_export_batch(
if let Some(session) = cursor_session.clone() { if let Some(session) = cursor_session.clone() {
return match session { return match session {
TableExportCursorSession::Agent(session_id) => { TableExportCursorSession::Agent(session_id) => {
let connections = state.connections.read().await; let pool_handle = state.pool_handle(pool_key).await;
let Some(PoolKind::Agent(client)) = connections.get(pool_key) else { let Some(PoolKind::Agent(client)) = pool_handle.as_ref() else {
return Err("Table read session requires an agent connection".to_string()); return Err("Table read session requires an agent connection".to_string());
}; };
let client = client.clone(); let client = client.clone();
drop(connections);
let mut client = client.lock().await; let mut client = client.lock().await;
match client.fetch_table_read_page::<QueryResult>(&session_id, active_batch_size).await { match client.fetch_table_read_page::<QueryResult>(&session_id, active_batch_size).await {
Ok(result) => { Ok(result) => {
@@ -848,12 +846,11 @@ async fn close_table_export_cursor_if_open(
}; };
match session { match session {
TableExportCursorSession::Agent(session_id) => { TableExportCursorSession::Agent(session_id) => {
let connections = state.connections.read().await; let pool_handle = state.pool_handle(pool_key).await;
let Some(PoolKind::Agent(client)) = connections.get(pool_key) else { let Some(PoolKind::Agent(client)) = pool_handle.as_ref() else {
return; return;
}; };
let client = client.clone(); let client = client.clone();
drop(connections);
let mut client = client.lock().await; let mut client = client.lock().await;
let _ = client.close_table_read_session::<bool>(&session_id).await; let _ = client.close_table_read_session::<bool>(&session_id).await;
} }
@@ -893,12 +890,11 @@ async fn stream_native_table_rows(
cancel_token: CancellationToken, cancel_token: CancellationToken,
on_row: impl FnMut(&[Value]) -> Result<(), String>, on_row: impl FnMut(&[Value]) -> Result<(), String>,
) -> Result<bool, String> { ) -> Result<bool, String> {
let connections = state.connections.read().await; let pool_handle = state.pool_handle(pool_key).await;
match connections.get(pool_key) { match pool_handle.as_ref() {
Some(PoolKind::Mysql(pool, mode)) => { Some(PoolKind::Mysql(pool, mode)) => {
let pool = pool.clone(); let pool = pool.clone();
let bare = *mode == MysqlMode::Bare; let bare = *mode == MysqlMode::Bare;
drop(connections);
crate::db::mysql::stream_query_rows( crate::db::mysql::stream_query_rows(
&pool, &pool,
sql, sql,
@@ -913,13 +909,11 @@ async fn stream_native_table_rows(
} }
Some(PoolKind::Postgres(pool)) => { Some(PoolKind::Postgres(pool)) => {
let pool = pool.clone(); let pool = pool.clone();
drop(connections);
crate::db::postgres::stream_query_rows(&pool, sql, row_limit, cancelled, on_row).await?; crate::db::postgres::stream_query_rows(&pool, sql, row_limit, cancelled, on_row).await?;
Ok(true) Ok(true)
} }
Some(PoolKind::SqlServer(client)) => { Some(PoolKind::SqlServer(client)) => {
let client = client.clone(); let client = client.clone();
drop(connections);
let mut on_row = on_row; let mut on_row = on_row;
let mut client = client.lock().await; let mut client = client.lock().await;
crate::db::sqlserver::stream_first_result_set(&mut client, sql, row_limit, Some(cancel_token), |item| { crate::db::sqlserver::stream_first_result_set(&mut client, sql, row_limit, Some(cancel_token), |item| {
@@ -2188,10 +2182,14 @@ mod tests {
let export_id = format!("export-{}", uuid::Uuid::new_v4()); let export_id = format!("export-{}", uuid::Uuid::new_v4());
let pool_key = let pool_key =
format!("{}:session:{}", config.id, table_export_client_session_id(&export_id).replace(':', "_")); format!("{}:session:{}", config.id, table_export_client_session_id(&export_id).replace(':', "_"));
state.connections.write().await.insert( state
pool_key, .update_connection_pools(|connections| {
PoolKind::ExternalDriver { driver_id: "jdbc".to_string(), config: Arc::new(config), session }, connections.insert(
); pool_key,
PoolKind::ExternalDriver { driver_id: "jdbc".to_string(), config: Arc::new(config), session },
);
})
.await;
let output = dir.join("export.csv"); let output = dir.join("export.csv");
let request = TableExportRequest { let request = TableExportRequest {
@@ -2942,7 +2940,7 @@ mod tests {
); );
assert_eq!(progress.last().and_then(|event| event.total_rows), Some(3)); assert_eq!(progress.last().and_then(|event| event.total_rows), Some(3));
assert!(matches!(progress.last().map(|event| &event.status), Some(ExportStatus::Done))); assert!(matches!(progress.last().map(|event| &event.status), Some(ExportStatus::Done)));
assert!(fixture.state.connections.read().await.is_empty()); assert!(fixture.state.with_connection_pools(|pools| pools.is_empty()).await);
cleanup_external_driver_export_fixture(fixture); cleanup_external_driver_export_fixture(fixture);
} }
@@ -2999,7 +2997,7 @@ mod tests {
run_external_driver_export(&fixture).await.expect("row-limited JDBC export should succeed"); run_external_driver_export(&fixture).await.expect("row-limited JDBC export should succeed");
assert_eq!(std::fs::read_to_string(&fixture.calls).unwrap(), "executeQueryPage\ncloseQuerySession\n"); assert_eq!(std::fs::read_to_string(&fixture.calls).unwrap(), "executeQueryPage\ncloseQuerySession\n");
assert!(fixture.state.connections.read().await.is_empty()); assert!(fixture.state.with_connection_pools(|pools| pools.is_empty()).await);
cleanup_external_driver_export_fixture(fixture); cleanup_external_driver_export_fixture(fixture);
} }
@@ -3052,7 +3050,7 @@ mod tests {
std::fs::read_to_string(&fixture.calls).unwrap(), std::fs::read_to_string(&fixture.calls).unwrap(),
"executeQueryPage\nfetchQueryPage\ncloseQuerySession\n" "executeQueryPage\nfetchQueryPage\ncloseQuerySession\n"
); );
assert!(fixture.state.connections.read().await.is_empty()); assert!(fixture.state.with_connection_pools(|pools| pools.is_empty()).await);
cleanup_external_driver_export_fixture(fixture); cleanup_external_driver_export_fixture(fixture);
} }
@@ -3087,7 +3085,7 @@ mod tests {
assert!(cancel_requested_at.elapsed() < Duration::from_secs(2)); assert!(cancel_requested_at.elapsed() < Duration::from_secs(2));
assert!(matches!(progress.last().map(|event| &event.status), Some(ExportStatus::Cancelled))); assert!(matches!(progress.last().map(|event| &event.status), Some(ExportStatus::Cancelled)));
assert!(fixture.state.connections.read().await.is_empty()); assert!(fixture.state.with_connection_pools(|pools| pools.is_empty()).await);
clear_export_cancelled(&fixture.request.export_id).await; clear_export_cancelled(&fixture.request.export_id).await;
cleanup_external_driver_export_fixture(fixture); cleanup_external_driver_export_fixture(fixture);
} }
@@ -3126,7 +3124,7 @@ mod tests {
assert!(cancel_requested_at.elapsed() < Duration::from_secs(2)); assert!(cancel_requested_at.elapsed() < Duration::from_secs(2));
assert!(matches!(progress.last().map(|event| &event.status), Some(ExportStatus::Cancelled))); assert!(matches!(progress.last().map(|event| &event.status), Some(ExportStatus::Cancelled)));
assert!(fixture.state.connections.read().await.is_empty()); assert!(fixture.state.with_connection_pools(|pools| pools.is_empty()).await);
clear_export_cancelled(&fixture.request.export_id).await; clear_export_cancelled(&fixture.request.export_id).await;
cleanup_external_driver_export_fixture(fixture); cleanup_external_driver_export_fixture(fixture);
} }
+46 -18
View File
@@ -4541,8 +4541,8 @@ async fn execute_postgres_copy_batch(
statement_count: &mut usize, statement_count: &mut usize,
) -> Result<(), String> { ) -> Result<(), String> {
let pool = { let pool = {
let connections = state.connections.read().await; let pool_handle = state.pool_handle(pool_key).await;
match connections.get(pool_key) { match pool_handle.as_ref() {
Some(PoolKind::Postgres(pool)) => pool.clone(), Some(PoolKind::Postgres(pool)) => pool.clone(),
_ => return Err("PostgreSQL pool not found for COPY import".to_string()), _ => return Err("PostgreSQL pool not found for COPY import".to_string()),
} }
@@ -5333,8 +5333,8 @@ async fn kingbase_oracle_compatibility_mode(state: &AppState, pool_key: &str, db
return false; return false;
} }
let client = { let client = {
let connections = state.connections.read().await; let pool_handle = state.pool_handle(pool_key).await;
match connections.get(pool_key) { match pool_handle.as_ref() {
Some(PoolKind::Agent(client)) => client.clone(), Some(PoolKind::Agent(client)) => client.clone(),
_ => return false, _ => return false,
} }
@@ -5350,8 +5350,8 @@ async fn kingbase_oracle_compatibility_mode(state: &AppState, pool_key: &str, db
async fn mysql_import_sql_hard_limit(state: &AppState, pool_key: &str) -> Option<usize> { async fn mysql_import_sql_hard_limit(state: &AppState, pool_key: &str) -> Option<usize> {
let pool = { let pool = {
let connections = state.connections.read().await; let pool_handle = state.pool_handle(pool_key).await;
match connections.get(pool_key) { match pool_handle.as_ref() {
Some(PoolKind::Mysql(pool, _)) => pool.clone(), Some(PoolKind::Mysql(pool, _)) => pool.clone(),
_ => return None, _ => return None,
} }
@@ -5480,8 +5480,8 @@ async fn sqlserver_bulk_import_plan_for_pool(
} }
let import_plan = import_plan?; let import_plan = import_plan?;
let client = { let client = {
let connections = state.connections.read().await; let pool_handle = state.pool_handle(pool_key).await;
match connections.get(pool_key) { match pool_handle.as_ref() {
Some(PoolKind::SqlServer(client)) => client.clone(), Some(PoolKind::SqlServer(client)) => client.clone(),
_ => return None, _ => return None,
} }
@@ -5722,8 +5722,8 @@ async fn execute_sqlserver_bulk_rows_batch(
.await .await
.map_err(ImportRowsBatchError::before_write)?; .map_err(ImportRowsBatchError::before_write)?;
let client = { let client = {
let connections = state.connections.read().await; let pool_handle = state.pool_handle(pool_key).await;
match connections.get(pool_key) { match pool_handle.as_ref() {
Some(PoolKind::SqlServer(client)) => client.clone(), Some(PoolKind::SqlServer(client)) => client.clone(),
_ => { _ => {
return Err(ImportRowsBatchError::before_write( return Err(ImportRowsBatchError::before_write(
@@ -8735,7 +8735,11 @@ mod tests {
) )
.await .await
.unwrap(); .unwrap();
state.connections.write().await.insert(pool_key.clone(), PoolKind::Sqlite(sqlite.clone())); state
.update_connection_pools(|connections| {
connections.insert(pool_key.clone(), PoolKind::Sqlite(sqlite.clone()));
})
.await;
let config: ConnectionConfig = serde_json::from_value(serde_json::json!({ let config: ConnectionConfig = serde_json::from_value(serde_json::json!({
"id": connection_id, "id": connection_id,
"name": "XLSX truncate tail test", "name": "XLSX truncate tail test",
@@ -8823,7 +8827,11 @@ mod tests {
) )
.await .await
.unwrap(); .unwrap();
state.connections.write().await.insert(pool_key.clone(), PoolKind::Sqlite(sqlite.clone())); state
.update_connection_pools(|connections| {
connections.insert(pool_key.clone(), PoolKind::Sqlite(sqlite.clone()));
})
.await;
let config: ConnectionConfig = serde_json::from_value(serde_json::json!({ let config: ConnectionConfig = serde_json::from_value(serde_json::json!({
"id": connection_id, "id": connection_id,
"name": "Cancel XLSX validation test", "name": "Cancel XLSX validation test",
@@ -8907,7 +8915,11 @@ mod tests {
) )
.await .await
.unwrap(); .unwrap();
state.connections.write().await.insert(pool_key.clone(), PoolKind::Sqlite(sqlite.clone())); state
.update_connection_pools(|connections| {
connections.insert(pool_key.clone(), PoolKind::Sqlite(sqlite.clone()));
})
.await;
let config: ConnectionConfig = serde_json::from_value(serde_json::json!({ let config: ConnectionConfig = serde_json::from_value(serde_json::json!({
"id": connection_id, "id": connection_id,
"name": "Cancel truncate first batch test", "name": "Cancel truncate first batch test",
@@ -10475,7 +10487,11 @@ mod tests {
let pool_key = "sqlserver-cleanup-failure"; let pool_key = "sqlserver-cleanup-failure";
let database_path = dir.path().join("target.db"); let database_path = dir.path().join("target.db");
let sqlite = crate::db::sqlite::connect_path_create_if_missing(database_path.to_str().unwrap()).await.unwrap(); let sqlite = crate::db::sqlite::connect_path_create_if_missing(database_path.to_str().unwrap()).await.unwrap();
state.connections.write().await.insert(pool_key.to_string(), PoolKind::Sqlite(sqlite)); state
.update_connection_pools(|connections| {
connections.insert(pool_key.to_string(), PoolKind::Sqlite(sqlite));
})
.await;
invalidate_sqlserver_pool_after_staging_cleanup_failure( invalidate_sqlserver_pool_after_staging_cleanup_failure(
&state, &state,
@@ -10486,7 +10502,7 @@ mod tests {
) )
.await; .await;
assert!(!state.connections.read().await.contains_key(pool_key)); assert!(!state.pool_handle(pool_key).await.is_some());
} }
#[test] #[test]
@@ -10520,7 +10536,11 @@ mod tests {
let database_path = dir.path().join("target.db"); let database_path = dir.path().join("target.db");
let sqlite = crate::db::sqlite::connect_path_create_if_missing(database_path.to_str().unwrap()).await.unwrap(); let sqlite = crate::db::sqlite::connect_path_create_if_missing(database_path.to_str().unwrap()).await.unwrap();
crate::db::sqlite::execute_query(&sqlite, "CREATE TABLE items (payload TEXT)").await.unwrap(); crate::db::sqlite::execute_query(&sqlite, "CREATE TABLE items (payload TEXT)").await.unwrap();
state.connections.write().await.insert(pool_key.to_string(), PoolKind::Sqlite(sqlite.clone())); state
.update_connection_pools(|connections| {
connections.insert(pool_key.to_string(), PoolKind::Sqlite(sqlite.clone()));
})
.await;
let rows = let rows =
vec![vec![serde_json::json!("a".repeat(300 * 1024))], vec![serde_json::json!("b".repeat(300 * 1024))]]; vec![vec![serde_json::json!("a".repeat(300 * 1024))], vec![serde_json::json!("b".repeat(300 * 1024))]];
@@ -10736,7 +10756,11 @@ mod tests {
let sqlite = let sqlite =
crate::db::sqlite::connect_path_create_if_missing(database_path.to_str().unwrap()).await.unwrap(); crate::db::sqlite::connect_path_create_if_missing(database_path.to_str().unwrap()).await.unwrap();
crate::db::sqlite::execute_query(&sqlite, "CREATE TABLE items (id INTEGER PRIMARY KEY)").await.unwrap(); crate::db::sqlite::execute_query(&sqlite, "CREATE TABLE items (id INTEGER PRIMARY KEY)").await.unwrap();
state.connections.write().await.insert(pool_key.clone(), PoolKind::Sqlite(sqlite.clone())); state
.update_connection_pools(|connections| {
connections.insert(pool_key.clone(), PoolKind::Sqlite(sqlite.clone()));
})
.await;
Self { Self {
_dir: dir, _dir: dir,
state, state,
@@ -10848,7 +10872,11 @@ mod tests {
let database_path = dir.path().join("target.db"); let database_path = dir.path().join("target.db");
let sqlite = crate::db::sqlite::connect_path_create_if_missing(database_path.to_str().unwrap()).await.unwrap(); let sqlite = crate::db::sqlite::connect_path_create_if_missing(database_path.to_str().unwrap()).await.unwrap();
crate::db::sqlite::execute_query(&sqlite, "CREATE TABLE items (id INTEGER, name TEXT)").await.unwrap(); crate::db::sqlite::execute_query(&sqlite, "CREATE TABLE items (id INTEGER, name TEXT)").await.unwrap();
state.connections.write().await.insert(pool_key.clone(), PoolKind::Sqlite(sqlite.clone())); state
.update_connection_pools(|connections| {
connections.insert(pool_key.clone(), PoolKind::Sqlite(sqlite.clone()));
})
.await;
let config: ConnectionConfig = serde_json::from_value(serde_json::json!({ let config: ConnectionConfig = serde_json::from_value(serde_json::json!({
"id": connection_id, "id": connection_id,
"name": "SQLite delimited append test", "name": "SQLite delimited append test",
@@ -85,8 +85,8 @@ async fn native_sqlite_pool(
) -> Result<(String, db::sqlite::SqliteHandle), String> { ) -> Result<(String, db::sqlite::SqliteHandle), String> {
let database = (!database.trim().is_empty()).then_some(database); let database = (!database.trim().is_empty()).then_some(database);
let pool_key = state.get_or_create_pool(connection_id, database).await?; let pool_key = state.get_or_create_pool(connection_id, database).await?;
let connections = state.connections.read().await; let pool_handle = state.pool_handle(&pool_key).await;
match connections.get(&pool_key) { match pool_handle.as_ref() {
Some(PoolKind::Sqlite(pool)) => Ok((pool_key, pool.clone())), Some(PoolKind::Sqlite(pool)) => Ok((pool_key, pool.clone())),
Some(_) => Err("SQLite table rebuild is only available for native SQLite connections.".to_string()), Some(_) => Err("SQLite table rebuild is only available for native SQLite connections.".to_string()),
None => Err("SQLite connection pool not found.".to_string()), None => Err("SQLite connection pool not found.".to_string()),
+40 -58
View File
@@ -5094,35 +5094,30 @@ async fn execute_on_pool_once(
) -> Result<db::QueryResult, String> { ) -> Result<db::QueryResult, String> {
// Read-only check: block transfer operations in readonly mode // Read-only check: block transfer operations in readonly mode
crate::query::check_read_only_for_connection(state, pool_key, sql).await?; crate::query::check_read_only_for_connection(state, pool_key, sql).await?;
let connections = state.connections.read().await; let pool_handle = state.pool_handle(pool_key).await;
let pool = connections.get(pool_key).ok_or("Connection not found")?; let pool = pool_handle.as_ref().ok_or("Connection not found")?;
match pool { match pool {
PoolKind::Mysql(p, mode) => { PoolKind::Mysql(p, mode) => {
let p = p.clone(); let p = p.clone();
let bare = *mode == crate::connection::MysqlMode::Bare; let bare = *mode == crate::connection::MysqlMode::Bare;
drop(connections);
db::mysql::execute_query_with_max_rows(&p, sql, bare, max_rows, Default::default()).await db::mysql::execute_query_with_max_rows(&p, sql, bare, max_rows, Default::default()).await
} }
PoolKind::Postgres(p) => { PoolKind::Postgres(p) => {
let p = p.clone(); let p = p.clone();
drop(connections);
db::postgres::execute_query_with_max_rows(&p, sql, max_rows).await db::postgres::execute_query_with_max_rows(&p, sql, max_rows).await
} }
PoolKind::Sqlite(p) => { PoolKind::Sqlite(p) => {
let p = p.clone(); let p = p.clone();
drop(connections);
db::sqlite::execute_query_with_max_rows(&p, sql, max_rows).await db::sqlite::execute_query_with_max_rows(&p, sql, max_rows).await
} }
PoolKind::ClickHouse(client) => { PoolKind::ClickHouse(client) => {
let client = client.clone(); let client = client.clone();
let database = database_from_pool_key(pool_key).unwrap_or("default").to_string(); let database = database_from_pool_key(pool_key).unwrap_or("default").to_string();
drop(connections);
db::clickhouse_driver::execute_query_with_max_rows(&client, &database, sql, max_rows).await db::clickhouse_driver::execute_query_with_max_rows(&client, &database, sql, max_rows).await
} }
PoolKind::SqlServer(client) => { PoolKind::SqlServer(client) => {
let client = client.clone(); let client = client.clone();
drop(connections);
let mut client = client.lock().await; let mut client = client.lock().await;
let result = db::sqlserver::execute_query_with_max_rows(&mut client, sql, max_rows).await; let result = db::sqlserver::execute_query_with_max_rows(&mut client, sql, max_rows).await;
drop(client); drop(client);
@@ -5132,7 +5127,6 @@ async fn execute_on_pool_once(
let client = client.clone(); let client = client.clone();
let database = database_from_pool_key(pool_key).map(str::to_string); let database = database_from_pool_key(pool_key).map(str::to_string);
let sql = sql.to_string(); let sql = sql.to_string();
drop(connections);
let mut client = client.lock().await; let mut client = client.lock().await;
let params = agent_execute_query_params( let params = agent_execute_query_params(
&sql, &sql,
@@ -5159,7 +5153,6 @@ async fn execute_on_pool_once(
PoolKind::DuckDbWorker(client) => { PoolKind::DuckDbWorker(client) => {
let client = client.clone(); let client = client.clone();
let sql = sql.to_string(); let sql = sql.to_string();
drop(connections);
client.execute(None, sql, max_rows, None, None).await client.execute(None, sql, max_rows, None, None).await
} }
_ => Err("Unsupported database type for transfer".to_string()), _ => Err("Unsupported database type for transfer".to_string()),
@@ -5223,63 +5216,56 @@ pub async fn get_columns_for_transfer(
table: &str, table: &str,
catalog: Option<&str>, catalog: Option<&str>,
) -> Result<Vec<db::ColumnInfo>, String> { ) -> Result<Vec<db::ColumnInfo>, String> {
let connections = state.connections.read().await; let pool_handle = state.pool_handle(pool_key).await;
#[cfg(feature = "duckdb-sidecar")] #[cfg(feature = "duckdb-sidecar")]
if let Some(PoolKind::DuckDbWorker(client)) = connections.get(pool_key) { if let Some(PoolKind::DuckDbWorker(client)) = pool_handle.as_ref() {
let client = client.clone(); let client = client.clone();
let database = database.to_string(); let database = database.to_string();
let schema = schema.to_string(); let schema = schema.to_string();
let table = table.to_string(); let table = table.to_string();
drop(connections);
return client.list_columns(database, schema, table).await; return client.list_columns(database, schema, table).await;
} }
if let Some(PoolKind::ClickHouse(client)) = connections.get(pool_key) { if let Some(PoolKind::ClickHouse(client)) = pool_handle.as_ref() {
let client = client.clone(); let client = client.clone();
let database = database.to_string(); let database = database.to_string();
let table = table.to_string(); let table = table.to_string();
drop(connections);
return db::clickhouse_driver::get_columns(&client, &database, &table).await; return db::clickhouse_driver::get_columns(&client, &database, &table).await;
} }
if let Some(PoolKind::SqlServer(client)) = connections.get(pool_key) { if let Some(PoolKind::SqlServer(client)) = pool_handle.as_ref() {
let client = client.clone(); let client = client.clone();
let schema = schema.to_string(); let schema = schema.to_string();
let table = table.to_string(); let table = table.to_string();
drop(connections);
let mut client = client.lock().await; let mut client = client.lock().await;
return db::sqlserver::get_columns(&mut client, &schema, &table).await; return db::sqlserver::get_columns(&mut client, &schema, &table).await;
} }
if let Some(PoolKind::InfluxDb(client)) = connections.get(pool_key) { if let Some(PoolKind::InfluxDb(client)) = pool_handle.as_ref() {
let client = client.clone(); let client = client.clone();
let database = database.to_string(); let database = database.to_string();
let table = table.to_string(); let table = table.to_string();
drop(connections);
return db::influxdb_driver::get_columns(&client, &database, &table).await; return db::influxdb_driver::get_columns(&client, &database, &table).await;
} }
if let Some(PoolKind::InfluxDb3(client)) = connections.get(pool_key) { if let Some(PoolKind::InfluxDb3(client)) = pool_handle.as_ref() {
let client = client.clone(); let client = client.clone();
let database = database.to_string(); let database = database.to_string();
let table = table.to_string(); let table = table.to_string();
drop(connections);
return db::influxdb3_driver::get_columns(&client, &database, &table).await; return db::influxdb3_driver::get_columns(&client, &database, &table).await;
} }
if let Some(PoolKind::Agent(client)) = connections.get(pool_key) { if let Some(PoolKind::Agent(client)) = pool_handle.as_ref() {
let client = client.clone(); let client = client.clone();
let database = database.to_string(); let database = database.to_string();
let schema = schema.to_string(); let schema = schema.to_string();
let table = table.to_string(); let table = table.to_string();
drop(connections);
let mut client = client.lock().await; let mut client = client.lock().await;
return client.get_columns(&database, &schema, &table, None).await; return client.get_columns(&database, &schema, &table, None).await;
} }
if let Some(PoolKind::ExternalDriver { config, session, .. }) = connections.get(pool_key) { if let Some(PoolKind::ExternalDriver { config, session, .. }) = pool_handle.as_ref() {
let config = config.clone(); let config = config.clone();
let session = session.clone(); let session = session.clone();
let database = database.to_string(); let database = database.to_string();
let schema = schema.to_string(); let schema = schema.to_string();
let table = table.to_string(); let table = table.to_string();
drop(connections);
return session return session
.invoke_with_timeout( .invoke_with_timeout(
"getColumns", "getColumns",
@@ -5293,14 +5279,13 @@ pub async fn get_columns_for_transfer(
) )
.await; .await;
} }
let pool = connections.get(pool_key).ok_or("Pool not found")?; let pool = pool_handle.as_ref().ok_or("Pool not found")?;
let schema = schema.to_string(); let schema = schema.to_string();
let table = table.to_string(); let table = table.to_string();
match pool { match pool {
PoolKind::Mysql(p, _) => { PoolKind::Mysql(p, _) => {
let p = p.clone(); let p = p.clone();
let catalog = normalize_external_catalog_name(catalog).map(str::to_string); let catalog = normalize_external_catalog_name(catalog).map(str::to_string);
drop(connections);
if let Some(catalog) = catalog { if let Some(catalog) = catalog {
// Use 3-part qualified column lookup for Doris/StarRocks external catalogs // Use 3-part qualified column lookup for Doris/StarRocks external catalogs
db::doris::get_catalog_columns(&p, &catalog, &schema, &table).await db::doris::get_catalog_columns(&p, &catalog, &schema, &table).await
@@ -5310,12 +5295,10 @@ pub async fn get_columns_for_transfer(
} }
PoolKind::Postgres(p) => { PoolKind::Postgres(p) => {
let p = p.clone(); let p = p.clone();
drop(connections);
db::postgres::get_columns(&p, &schema, &table).await db::postgres::get_columns(&p, &schema, &table).await
} }
PoolKind::Sqlite(p) => { PoolKind::Sqlite(p) => {
let p = p.clone(); let p = p.clone();
drop(connections);
db::sqlite::get_columns(&p, &schema, &table).await db::sqlite::get_columns(&p, &schema, &table).await
} }
_ => Err("Unsupported database type".to_string()), _ => Err("Unsupported database type".to_string()),
@@ -5329,21 +5312,19 @@ async fn get_postgres_indexes_for_transfer(
schema: &str, schema: &str,
table: &str, table: &str,
) -> Result<Vec<db::IndexInfo>, String> { ) -> Result<Vec<db::IndexInfo>, String> {
let connections = state.connections.read().await; let pool_handle = state.pool_handle(pool_key).await;
if let Some(PoolKind::Agent(client)) = connections.get(pool_key) { if let Some(PoolKind::Agent(client)) = pool_handle.as_ref() {
let client = client.clone(); let client = client.clone();
let database = database.to_string(); let database = database.to_string();
let schema = schema.to_string(); let schema = schema.to_string();
let table = table.to_string(); let table = table.to_string();
drop(connections);
let mut client = client.lock().await; let mut client = client.lock().await;
return client.list_indexes(&database, &schema, &table, None).await; return client.list_indexes(&database, &schema, &table, None).await;
} }
let Some(PoolKind::Postgres(pool)) = connections.get(pool_key) else { let Some(PoolKind::Postgres(pool)) = pool_handle.as_ref() else {
return Err("PostgreSQL pool not found".to_string()); return Err("PostgreSQL pool not found".to_string());
}; };
let pool = pool.clone(); let pool = pool.clone();
drop(connections);
db::postgres::list_indexes(&pool, schema, table).await db::postgres::list_indexes(&pool, schema, table).await
} }
@@ -5354,21 +5335,19 @@ async fn get_postgres_foreign_keys_for_transfer(
schema: &str, schema: &str,
table: &str, table: &str,
) -> Result<Vec<db::ForeignKeyInfo>, String> { ) -> Result<Vec<db::ForeignKeyInfo>, String> {
let connections = state.connections.read().await; let pool_handle = state.pool_handle(pool_key).await;
if let Some(PoolKind::Agent(client)) = connections.get(pool_key) { if let Some(PoolKind::Agent(client)) = pool_handle.as_ref() {
let client = client.clone(); let client = client.clone();
let database = database.to_string(); let database = database.to_string();
let schema = schema.to_string(); let schema = schema.to_string();
let table = table.to_string(); let table = table.to_string();
drop(connections);
let mut client = client.lock().await; let mut client = client.lock().await;
return client.list_foreign_keys(&database, &schema, &table, None).await; return client.list_foreign_keys(&database, &schema, &table, None).await;
} }
let Some(PoolKind::Postgres(pool)) = connections.get(pool_key) else { let Some(PoolKind::Postgres(pool)) = pool_handle.as_ref() else {
return Err("PostgreSQL pool not found".to_string()); return Err("PostgreSQL pool not found".to_string());
}; };
let pool = pool.clone(); let pool = pool.clone();
drop(connections);
db::postgres::list_foreign_keys(&pool, schema, table).await db::postgres::list_foreign_keys(&pool, schema, table).await
} }
@@ -5383,8 +5362,8 @@ async fn get_postgres_owned_sequences_for_transfer(
} }
let pool = { let pool = {
let connections = state.connections.read().await; let pool_handle = state.pool_handle(pool_key).await;
match connections.get(pool_key) { match pool_handle.as_ref() {
Some(PoolKind::Postgres(pool)) => pool.clone(), Some(PoolKind::Postgres(pool)) => pool.clone(),
_ => return Ok(Vec::new()), _ => return Ok(Vec::new()),
} }
@@ -5430,8 +5409,8 @@ async fn get_postgres_sequence_snapshots_for_transfer(
schema: &str, schema: &str,
) -> Result<Vec<PostgresSequenceSnapshot>, String> { ) -> Result<Vec<PostgresSequenceSnapshot>, String> {
let pool = { let pool = {
let connections = state.connections.read().await; let pool_handle = state.pool_handle(pool_key).await;
match connections.get(pool_key) { match pool_handle.as_ref() {
Some(PoolKind::Postgres(pool)) => pool.clone(), Some(PoolKind::Postgres(pool)) => pool.clone(),
_ => return Ok(Vec::new()), _ => return Ok(Vec::new()),
} }
@@ -6763,8 +6742,8 @@ pub async fn sort_tables_by_fk_dependency_with_foreign_keys(
let postgres_pool = if db_type == DatabaseType::Postgres { let postgres_pool = if db_type == DatabaseType::Postgres {
let pool_key = state.get_or_create_pool(connection_id, Some(database)).await?; let pool_key = state.get_or_create_pool(connection_id, Some(database)).await?;
{ {
let connections = state.connections.read().await; let pool_handle = state.pool_handle(&pool_key).await;
native_postgres_dependency_pool(connections.get(&pool_key)) native_postgres_dependency_pool(pool_handle.as_ref())
} }
} else { } else {
None None
@@ -7184,12 +7163,11 @@ async fn fetch_hive_server_transfer_batch(
let configs = state.configs.read().await; let configs = state.configs.read().await;
configs.get(&request.source_connection_id).map(|config| config.query_timeout_secs).unwrap_or(0) configs.get(&request.source_connection_id).map(|config| config.query_timeout_secs).unwrap_or(0)
}; };
let connections = state.connections.read().await; let pool_handle = state.pool_handle(pool_key).await;
let Some(PoolKind::Agent(client)) = connections.get(pool_key) else { let Some(PoolKind::Agent(client)) = pool_handle.as_ref() else {
return Err("Impala transfer requires an Agent connection".to_string()); return Err("Impala transfer requires an Agent connection".to_string());
}; };
let client = client.clone(); let client = client.clone();
drop(connections);
let mut client = client.lock().await; let mut client = client.lock().await;
let result = if cursor.started { let result = if cursor.started {
@@ -7226,12 +7204,11 @@ async fn close_hive_server_transfer_cursor(state: &AppState, pool_key: &str, cur
let Some(session_id) = cursor.session_id.take() else { let Some(session_id) = cursor.session_id.take() else {
return; return;
}; };
let connections = state.connections.read().await; let pool_handle = state.pool_handle(pool_key).await;
let Some(PoolKind::Agent(client)) = connections.get(pool_key) else { let Some(PoolKind::Agent(client)) = pool_handle.as_ref() else {
return; return;
}; };
let client = client.clone(); let client = client.clone();
drop(connections);
let mut client = client.lock().await; let mut client = client.lock().await;
if let Err(error) = client.close_table_read_session::<bool>(&session_id).await { if let Err(error) = client.close_table_read_session::<bool>(&session_id).await {
log::warn!("[transfer] failed to close Impala transfer cursor: {error}"); log::warn!("[transfer] failed to close Impala transfer cursor: {error}");
@@ -7433,10 +7410,11 @@ where
// SHOW CREATE TABLE catalog.database.table using the // SHOW CREATE TABLE catalog.database.table using the
// existing source pool (bare MySQL — addresses any catalog). // existing source pool (bare MySQL — addresses any catalog).
let pool = { let pool = {
let connections = state.connections.read().await; let pool = state
let pool = .pool_handle(source_pool_key)
connections.get(source_pool_key).ok_or_else(|| "Source pool not found".to_string())?; .await
let PoolKind::Mysql(p, _) = pool else { .ok_or_else(|| "Source pool not found".to_string())?;
let PoolKind::Mysql(p, _) = &pool else {
return Err("Source pool must be MySQL-family for catalog DDL".to_string()); return Err("Source pool must be MySQL-family for catalog DDL".to_string());
}; };
p.clone() p.clone()
@@ -8637,10 +8615,14 @@ mod tests {
#[tokio::test] #[tokio::test]
async fn postgres_transfer_metadata_routes_agent_pools() { async fn postgres_transfer_metadata_routes_agent_pools() {
let (state, dir) = test_app_state().await; let (state, dir) = test_app_state().await;
state.connections.write().await.insert( state
"source:source_db".to_string(), .update_connection_pools(|connections| {
PoolKind::agent(crate::db::agent_driver::AgentDriverClient::test_stub()), connections.insert(
); "source:source_db".to_string(),
PoolKind::agent(crate::db::agent_driver::AgentDriverClient::test_stub()),
);
})
.await;
let index_error = let index_error =
get_postgres_indexes_for_transfer(&state, "source:source_db", "source_db", "source_schema", "items") get_postgres_indexes_for_transfer(&state, "source:source_db", "source_db", "source_schema", "items")
@@ -1,10 +1,13 @@
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use std::time::Duration;
use dbx_core::agent_events::{ToolCall, ToolResult}; use dbx_core::agent_events::{ToolCall, ToolResult};
use dbx_core::agent_tools::{execute_tool, AgentSqlPermissions}; use dbx_core::agent_tools::{execute_tool, AgentSqlPermissions};
use dbx_core::connection::{AppState, PoolKind}; use dbx_core::connection::{AppState, PoolKind};
use dbx_core::models::connection::{ConnectionConfig, DatabaseType}; use dbx_core::models::connection::{ConnectionConfig, DatabaseType};
use dbx_core::mongo_ops::mongo_create_index_core;
use dbx_core::storage::Storage; use dbx_core::storage::Storage;
use mongodb::bson::{doc, Bson};
use mongodb::event::{command::CommandEvent, EventHandler}; use mongodb::event::{command::CommandEvent, EventHandler};
use mongodb::options::ClientOptions; use mongodb::options::ClientOptions;
use mongodb::Client; use mongodb::Client;
@@ -107,7 +110,11 @@ async fn mongodb_agent_enforces_limits_and_find_skips_total_count() {
let client = Client::with_options(options).unwrap(); let client = Client::with_options(options).unwrap();
state.configs.write().await.insert(config.id.clone(), config.clone()); state.configs.write().await.insert(config.id.clone(), config.clone());
state.connections.write().await.insert(config.id.clone(), PoolKind::MongoDb(client)); state
.update_connection_pools(|connections| {
connections.insert(config.id.clone(), PoolKind::MongoDb(client));
})
.await;
let zero_limit = let zero_limit =
call_agent_tool_with_limit(&state, &config.id, &database, "db.products.find({}).limit(0)", Some(0)).await; call_agent_tool_with_limit(&state, &config.id, &database, "db.products.find({}).limit(0)", Some(0)).await;
@@ -128,3 +135,126 @@ async fn mongodb_agent_enforces_limits_and_find_skips_total_count() {
assert!(!distinct.is_error, "{}", distinct.content); assert!(!distinct.is_error, "{}", distinct.content);
assert!(distinct.content.contains("(7 rows,"), "{}", distinct.content); assert!(distinct.content.contains("(7 rows,"), "{}", distinct.content);
} }
#[tokio::test]
#[ignore = "requires DBX_LIVE_MONGODB_42_URL pointing at MongoDB 4.2 with enableTestCommands=1"]
async fn stalled_mongodb_index_build_does_not_block_another_connection() {
let uri = std::env::var("DBX_LIVE_MONGODB_42_URL").expect("DBX_LIVE_MONGODB_42_URL");
let directory = tempfile::tempdir().unwrap();
let storage = Storage::open(&directory.path().join("storage.db")).await.unwrap();
let state = Arc::new(AppState::new(storage));
let primary_id = "live-mongodb-42-index";
let database = "dbx_issue_7720_primary";
let collection = "records";
let index_name = "issue_7720_pool_registry";
let client = Client::with_uri_str(&uri).await.expect("MongoDB 4.2 client");
let primary_config: ConnectionConfig = serde_json::from_value(serde_json::json!({
"id": primary_id,
"name": "MongoDB 4.2 stalled index",
"db_type": DatabaseType::MongoDb,
"host": "127.0.0.1",
"port": 17720,
"username": "",
"password": "",
"database": database,
"connection_string": uri,
"driver_profile": "mongodb-native",
"connect_timeout_secs": 5,
"query_timeout_secs": 30,
"idle_timeout_secs": 60,
"keepalive_interval_secs": 0
}))
.unwrap();
state.configs.write().await.insert(primary_id.to_string(), primary_config);
state
.update_connection_pools(|connections| {
connections.insert(primary_id.to_string(), PoolKind::MongoDb(client.clone()));
})
.await;
let database_handle = client.database(database);
let _ = database_handle.run_command(doc! { "dropIndexes": collection, "index": index_name }).await;
let enabled = client
.database("admin")
.run_command(doc! { "configureFailPoint": "hangAfterStartingIndexBuild", "mode": "alwaysOn" })
.await
.expect("enable index-build failpoint");
let entered = match enabled.get("count") {
Some(Bson::Int32(value)) => i64::from(*value),
Some(Bson::Int64(value)) => *value,
value => panic!("unexpected failpoint counter: {value:?}"),
};
let index_state = Arc::clone(&state);
let mut index_task = tokio::spawn(async move {
mongo_create_index_core(
&index_state,
primary_id,
database,
collection,
r#"{"pool_registry_probe":1}"#,
Some(&format!(r#"{{"name":"{index_name}"}}"#)),
)
.await
});
let admin_handle = client.database("admin");
let wait_for_index = admin_handle.run_command(doc! {
"waitForFailPoint": "hangAfterStartingIndexBuild",
"timesEntered": entered + 1,
"maxTimeMS": 5_000_i64
});
let early_index_result = tokio::select! {
wait_result = wait_for_index => {
if let Err(error) = wait_result {
admin_handle
.run_command(doc! { "configureFailPoint": "hangAfterStartingIndexBuild", "mode": "off" })
.await
.expect("disable index-build failpoint after wait failure");
index_task.abort();
let _ = (&mut index_task).await;
panic!("index build must reach the failpoint: {error}");
}
None
},
index_result = &mut index_task => Some(index_result),
};
if let Some(index_result) = early_index_result {
admin_handle
.run_command(doc! { "configureFailPoint": "hangAfterStartingIndexBuild", "mode": "off" })
.await
.expect("disable index-build failpoint after early completion");
panic!("index build ended before reaching the failpoint: {index_result:?}");
}
let sqlite_path = directory.path().join("other-connection.db");
std::fs::File::create(&sqlite_path).expect("create SQLite fixture");
let other_config: ConnectionConfig = serde_json::from_value(serde_json::json!({
"id": "other-connection",
"name": "Other connection",
"db_type": DatabaseType::Sqlite,
"host": sqlite_path.to_string_lossy(),
"port": 0,
"username": "",
"password": "",
"connect_timeout_secs": 5,
"query_timeout_secs": 30,
"idle_timeout_secs": 60,
"keepalive_interval_secs": 0
}))
.unwrap();
state.configs.write().await.insert(other_config.id.clone(), other_config);
let other_connection =
tokio::time::timeout(Duration::from_secs(2), state.get_or_create_pool("other-connection", None)).await;
client
.database("admin")
.run_command(doc! { "configureFailPoint": "hangAfterStartingIndexBuild", "mode": "off" })
.await
.expect("disable index-build failpoint");
let index_result = index_task.await.expect("index task join");
let _ = database_handle.run_command(doc! { "dropIndexes": collection, "index": index_name }).await;
assert!(other_connection.expect("other connection must not wait on the registry lock").is_ok());
assert_eq!(index_result.as_deref(), Ok(index_name));
}
+30 -10
View File
@@ -199,8 +199,12 @@ async fn live_postgres_transfer_upserts_generated_always_identity_values() {
let target_connection_id = "live-always-target"; let target_connection_id = "live-always-target";
let source_pool_key = format!("{source_connection_id}:{source_database}"); let source_pool_key = format!("{source_connection_id}:{source_database}");
let target_pool_key = format!("{target_connection_id}:{target_database}"); let target_pool_key = format!("{target_connection_id}:{target_database}");
state.connections.write().await.insert(source_pool_key.clone(), PoolKind::Postgres(source_pool.clone())); state
state.connections.write().await.insert(target_pool_key.clone(), PoolKind::Postgres(target_pool.clone())); .update_connection_pools(|connections| {
connections.insert(source_pool_key.clone(), PoolKind::Postgres(source_pool.clone()));
connections.insert(target_pool_key.clone(), PoolKind::Postgres(target_pool.clone()));
})
.await;
state state
.configs .configs
.write() .write()
@@ -363,8 +367,12 @@ async fn live_postgres_structure_only_preserves_table_indexes() {
let target_connection_id = "live-structure-only-target"; let target_connection_id = "live-structure-only-target";
let source_pool_key = format!("{source_connection_id}:{source_database}"); let source_pool_key = format!("{source_connection_id}:{source_database}");
let target_pool_key = format!("{target_connection_id}:{target_database}"); let target_pool_key = format!("{target_connection_id}:{target_database}");
state.connections.write().await.insert(source_pool_key.clone(), PoolKind::Postgres(source_pool.clone())); state
state.connections.write().await.insert(target_pool_key.clone(), PoolKind::Postgres(target_pool.clone())); .update_connection_pools(|connections| {
connections.insert(source_pool_key.clone(), PoolKind::Postgres(source_pool.clone()));
connections.insert(target_pool_key.clone(), PoolKind::Postgres(target_pool.clone()));
})
.await;
state state
.configs .configs
.write() .write()
@@ -608,8 +616,12 @@ async fn live_postgres_transfer_preserves_data_and_schema_objects() {
let source_pool_key = format!("{source_connection_id}:{source_database}"); let source_pool_key = format!("{source_connection_id}:{source_database}");
let target_pool_key = format!("{target_connection_id}:{target_database}"); let target_pool_key = format!("{target_connection_id}:{target_database}");
state.connections.write().await.insert(source_pool_key.clone(), PoolKind::Postgres(source_pool.clone())); state
state.connections.write().await.insert(target_pool_key.clone(), PoolKind::Postgres(target_pool.clone())); .update_connection_pools(|connections| {
connections.insert(source_pool_key.clone(), PoolKind::Postgres(source_pool.clone()));
connections.insert(target_pool_key.clone(), PoolKind::Postgres(target_pool.clone()));
})
.await;
state state
.configs .configs
.write() .write()
@@ -896,8 +908,12 @@ async fn live_postgres_transfer_skips_create_ddl_for_existing_target_table() {
let source_pool_key = format!("{source_connection_id}:{source_database}"); let source_pool_key = format!("{source_connection_id}:{source_database}");
let target_pool_key = format!("{target_connection_id}:{target_database}"); let target_pool_key = format!("{target_connection_id}:{target_database}");
state.connections.write().await.insert(source_pool_key.clone(), PoolKind::Postgres(source_pool.clone())); state
state.connections.write().await.insert(target_pool_key.clone(), PoolKind::Postgres(target_pool.clone())); .update_connection_pools(|connections| {
connections.insert(source_pool_key.clone(), PoolKind::Postgres(source_pool.clone()));
connections.insert(target_pool_key.clone(), PoolKind::Postgres(target_pool.clone()));
})
.await;
state state
.configs .configs
.write() .write()
@@ -1013,8 +1029,12 @@ async fn live_postgres_transfer_creates_selected_sequence_before_referencing_tab
let target_connection_id = "live-sequence-target"; let target_connection_id = "live-sequence-target";
let source_pool_key = format!("{source_connection_id}:{source_database}"); let source_pool_key = format!("{source_connection_id}:{source_database}");
let target_pool_key = format!("{target_connection_id}:{target_database}"); let target_pool_key = format!("{target_connection_id}:{target_database}");
state.connections.write().await.insert(source_pool_key.clone(), PoolKind::Postgres(source_pool.clone())); state
state.connections.write().await.insert(target_pool_key.clone(), PoolKind::Postgres(target_pool.clone())); .update_connection_pools(|connections| {
connections.insert(source_pool_key.clone(), PoolKind::Postgres(source_pool.clone()));
connections.insert(target_pool_key.clone(), PoolKind::Postgres(target_pool.clone()));
})
.await;
state state
.configs .configs
.write() .write()
@@ -0,0 +1,65 @@
use std::sync::Arc;
use std::time::Duration;
use dbx_core::connection::AppState;
use dbx_core::models::connection::{ConnectionConfig, DatabaseType};
use dbx_core::redis_ops::redis_execute_command_core;
use dbx_core::storage::Storage;
#[tokio::test]
#[ignore = "requires DBX_LIVE_REDIS_HOST and DBX_LIVE_REDIS_PORT"]
async fn blocking_redis_command_does_not_block_another_connection() {
let host = std::env::var("DBX_LIVE_REDIS_HOST").expect("DBX_LIVE_REDIS_HOST");
let port = std::env::var("DBX_LIVE_REDIS_PORT").expect("DBX_LIVE_REDIS_PORT").parse::<u16>().expect("Redis port");
let directory = tempfile::tempdir().unwrap();
let storage = Storage::open(&directory.path().join("storage.db")).await.unwrap();
let state = Arc::new(AppState::new(storage));
let redis_config: ConnectionConfig = serde_json::from_value(serde_json::json!({
"id": "live-redis-blocking",
"name": "Live blocking Redis",
"db_type": DatabaseType::Redis,
"host": host,
"port": port,
"username": "",
"password": "",
"database": null,
"connect_timeout_secs": 5,
"query_timeout_secs": 5,
"idle_timeout_secs": 60,
"keepalive_interval_secs": 0
}))
.unwrap();
state.configs.write().await.insert(redis_config.id.clone(), redis_config.clone());
state.get_or_create_pool(&redis_config.id, None).await.expect("open Redis connection");
let redis_state = Arc::clone(&state);
let redis_id = redis_config.id.clone();
let blocking = tokio::spawn(async move {
redis_execute_command_core(&redis_state, &redis_id, 0, "BLPOP dbx:issue:7720:missing 1", true).await
});
tokio::time::sleep(Duration::from_millis(100)).await;
let sqlite_path = directory.path().join("other-connection.db");
std::fs::File::create(&sqlite_path).expect("create SQLite fixture");
let sqlite_config: ConnectionConfig = serde_json::from_value(serde_json::json!({
"id": "other-connection",
"name": "Other connection",
"db_type": DatabaseType::Sqlite,
"host": sqlite_path.to_string_lossy(),
"port": 0,
"username": "",
"password": "",
"database": null,
"connect_timeout_secs": 5,
"query_timeout_secs": 5,
"idle_timeout_secs": 60,
"keepalive_interval_secs": 0
}))
.unwrap();
state.configs.write().await.insert(sqlite_config.id.clone(), sqlite_config);
let other_connection =
tokio::time::timeout(Duration::from_millis(300), state.get_or_create_pool("other-connection", None)).await;
assert!(other_connection.expect("other connection must not wait on Redis").is_ok());
assert!(blocking.await.expect("Redis task join").is_ok());
}
@@ -1167,10 +1167,11 @@ async fn live_sqlserver_query_result_export_streams_cte_query_to_csv() {
let connection_id = "live-sqlserver-export"; let connection_id = "live-sqlserver-export";
let pool_key = format!("{connection_id}:{database}"); let pool_key = format!("{connection_id}:{database}");
state state
.connections .update_connection_pools(|connections| {
.write() connections
.await .insert(pool_key, PoolKind::SqlServer(std::sync::Arc::new(tokio::sync::Mutex::new(export_client))));
.insert(pool_key, PoolKind::SqlServer(std::sync::Arc::new(tokio::sync::Mutex::new(export_client)))); })
.await;
let file_path = dir.join("result.csv"); let file_path = dir.join("result.csv");
let sql = format!( let sql = format!(
@@ -1258,10 +1259,14 @@ async fn live_sqlserver_sql_file_import_executes_go_batches() {
config.username = user; config.username = user;
config.password = password; config.password = password;
state.configs.write().await.insert(connection_id.to_string(), config); state.configs.write().await.insert(connection_id.to_string(), config);
state.connections.write().await.insert( state
format!("{connection_id}:{database}"), .update_connection_pools(|connections| {
PoolKind::SqlServer(std::sync::Arc::new(tokio::sync::Mutex::new(client))), connections.insert(
); format!("{connection_id}:{database}"),
PoolKind::SqlServer(std::sync::Arc::new(tokio::sync::Mutex::new(client))),
);
})
.await;
let script = format!( let script = format!(
"CREATE TABLE [dbo].[{table}] (id INT NOT NULL);\n\ "CREATE TABLE [dbo].[{table}] (id INT NOT NULL);\n\
@@ -1292,8 +1297,7 @@ async fn live_sqlserver_sql_file_import_executes_go_batches() {
.expect("execute SQL Server file with GO batches"); .expect("execute SQL Server file with GO batches");
let pool_key = format!("{connection_id}:{database}"); let pool_key = format!("{connection_id}:{database}");
let connections = state.connections.read().await; let PoolKind::SqlServer(client) = state.pool_handle(&pool_key).await.expect("SQL Server pool") else {
let PoolKind::SqlServer(client) = connections.get(&pool_key).expect("SQL Server pool") else {
panic!("expected SQL Server pool"); panic!("expected SQL Server pool");
}; };
let mut client = client.lock().await; let mut client = client.lock().await;
@@ -1301,7 +1305,6 @@ async fn live_sqlserver_sql_file_import_executes_go_batches() {
let cleanup = format!("DROP PROCEDURE [dbo].[{procedure}]; DROP TABLE [dbo].[{table}];"); let cleanup = format!("DROP PROCEDURE [dbo].[{procedure}]; DROP TABLE [dbo].[{table}];");
let _ = dbx_core::db::sqlserver::execute_batch(&mut client, &cleanup).await; let _ = dbx_core::db::sqlserver::execute_batch(&mut client, &cleanup).await;
drop(client); drop(client);
drop(connections);
let _ = std::fs::remove_dir_all(&dir); let _ = std::fs::remove_dir_all(&dir);
assert!(done_seen.load(Ordering::Relaxed)); assert!(done_seen.load(Ordering::Relaxed));
@@ -89,7 +89,11 @@ async fn live_sqlserver_xlsx_export_can_outlive_query_timeout_while_rows_keep_ar
let connection_id = "live-sqlserver-xlsx-export"; let connection_id = "live-sqlserver-xlsx-export";
let pool_key = format!("{connection_id}:{database}"); let pool_key = format!("{connection_id}:{database}");
state.configs.write().await.insert(connection_id.to_string(), live_sqlserver_config(connection_id, &database)); state.configs.write().await.insert(connection_id.to_string(), live_sqlserver_config(connection_id, &database));
state.connections.write().await.insert(pool_key, PoolKind::SqlServer(Arc::new(tokio::sync::Mutex::new(client)))); state
.update_connection_pools(|connections| {
connections.insert(pool_key, PoolKind::SqlServer(Arc::new(tokio::sync::Mutex::new(client))));
})
.await;
let file_path = dir.join("result.xlsx"); let file_path = dir.join("result.xlsx");
let sql = "WITH numbers AS (\ let sql = "WITH numbers AS (\
+80 -41
View File
@@ -256,10 +256,7 @@ async fn sync_mongo_legacy_driver_fallback(state: &WebState, config: &Connection
if config.db_type != DatabaseType::MongoDb { if config.db_type != DatabaseType::MongoDb {
return Ok(()); return Ok(());
} }
let uses_legacy_agent = { let uses_legacy_agent = matches!(state.app.pool_handle(&config.id).await, Some(PoolKind::Agent(_)));
let connections = state.app.connections.read().await;
matches!(connections.get(&config.id), Some(PoolKind::Agent(_)))
};
if !uses_legacy_agent { if !uses_legacy_agent {
return Ok(()); return Ok(());
} }
@@ -314,12 +311,9 @@ async fn run_temporary_connection_test(
// runs before either a successful result or an error is returned. // runs before either a successful result or an error is returned.
let result: Result<ConnectionTestResult, String> = async { let result: Result<ConnectionTestResult, String> = async {
let success_message = if pool_result.is_ok() && config.db_type == DatabaseType::Consul { let success_message = if pool_result.is_ok() && config.db_type == DatabaseType::Consul {
let client = { let client = match app.pool_handle(&temp_id).await {
let connections = app.connections.read().await; Some(PoolKind::Consul(client)) => Some(client),
match connections.get(&temp_id) { _ => None,
Some(PoolKind::Consul(client)) => Some(client.clone()),
_ => None,
}
}; };
if let Some(client) = client { if let Some(client) = client {
let configured_target = let configured_target =
@@ -1040,7 +1034,7 @@ mod tests {
assert_eq!(detailed.0.message, "Connection successful"); assert_eq!(detailed.0.message, "Connection successful");
assert_eq!(detailed.0.database_info, None); assert_eq!(detailed.0.database_info, None);
assert!(state.app.configs.read().await.keys().all(|key| !key.starts_with("__test_"))); assert!(state.app.configs.read().await.keys().all(|key| !key.starts_with("__test_")));
assert!(state.app.connections.read().await.keys().all(|key| !key.starts_with("__test_"))); assert!(state.app.with_connection_pools(|pools| pools.keys().all(|key| !key.starts_with("__test_"))).await);
let _ = std::fs::remove_dir_all(dir); let _ = std::fs::remove_dir_all(dir);
} }
@@ -1055,7 +1049,7 @@ mod tests {
assert!(error.contains("CONSUL_AGENT_TARGET_MISMATCH"), "unexpected error: {error}"); assert!(error.contains("CONSUL_AGENT_TARGET_MISMATCH"), "unexpected error: {error}");
assert!(state.app.configs.read().await.keys().all(|key| !key.starts_with("__test_"))); assert!(state.app.configs.read().await.keys().all(|key| !key.starts_with("__test_")));
assert!(state.app.connections.read().await.keys().all(|key| !key.starts_with("__test_"))); assert!(state.app.with_connection_pools(|pools| pools.keys().all(|key| !key.starts_with("__test_"))).await);
let _ = std::fs::remove_dir_all(dir); let _ = std::fs::remove_dir_all(dir);
} }
@@ -1103,7 +1097,7 @@ mod tests {
assert_eq!(result.0, "Connection successful"); assert_eq!(result.0, "Connection successful");
assert!(state.app.configs.read().await.keys().all(|key| !key.starts_with("__test_"))); assert!(state.app.configs.read().await.keys().all(|key| !key.starts_with("__test_")));
assert!(state.app.connections.read().await.keys().all(|key| !key.starts_with("__test_"))); assert!(state.app.with_connection_pools(|pools| pools.keys().all(|key| !key.starts_with("__test_"))).await);
let cached = state.app.mq_registry.cached_connection_ids().await; let cached = state.app.mq_registry.cached_connection_ids().await;
assert!( assert!(
cached.iter().all(|id| !id.starts_with("__test_")), cached.iter().all(|id| !id.starts_with("__test_")),
@@ -1126,7 +1120,12 @@ mod tests {
.await .await
.unwrap(); .unwrap();
state.app.configs.write().await.insert(initial.id.clone(), initial.clone()); state.app.configs.write().await.insert(initial.id.clone(), initial.clone());
state.app.connections.write().await.insert(initial.id.clone(), PoolKind::Sqlite(pool.clone())); state
.app
.update_connection_pools(|connections| {
connections.insert(initial.id.clone(), PoolKind::Sqlite(pool.clone()));
})
.await;
let mut invalid = initial.clone(); let mut invalid = initial.clone();
invalid.attached_databases.push(AttachedDatabaseConfig { invalid.attached_databases.push(AttachedDatabaseConfig {
@@ -1163,7 +1162,7 @@ mod tests {
.unwrap_err(); .unwrap_err();
assert!(proxy_error.message.contains("in-memory main database"), "{}", proxy_error.message); assert!(proxy_error.message.contains("in-memory main database"), "{}", proxy_error.message);
assert!(state.app.connections.read().await.contains_key(&initial.id)); assert!(state.app.pool_handle(&initial.id).await.is_some());
assert_eq!(state.app.configs.read().await.get(&initial.id), Some(&initial)); assert_eq!(state.app.configs.read().await.get(&initial.id), Some(&initial));
let retained = dbx_core::db::sqlite::execute_query(&pool, "SELECT value FROM retained;").await.unwrap(); let retained = dbx_core::db::sqlite::execute_query(&pool, "SELECT value FROM retained;").await.unwrap();
assert_eq!(retained.rows[0][0], serde_json::json!("yes")); assert_eq!(retained.rows[0][0], serde_json::json!("yes"));
@@ -1359,7 +1358,12 @@ mod tests {
let config = mq_config("mq-info", "http://127.0.0.1:8080"); let config = mq_config("mq-info", "http://127.0.0.1:8080");
state.app.storage.save_connections(std::slice::from_ref(&config)).await.unwrap(); state.app.storage.save_connections(std::slice::from_ref(&config)).await.unwrap();
state.app.configs.write().await.insert(config.id.clone(), config.clone()); state.app.configs.write().await.insert(config.id.clone(), config.clone());
state.app.connections.write().await.insert(config.id.clone(), PoolKind::MessageQueue); state
.app
.update_connection_pools(|connections| {
connections.insert(config.id.clone(), PoolKind::MessageQueue);
})
.await;
let database_info = DatabaseConnectionInfo { let database_info = DatabaseConnectionInfo {
product_name: Some("Apache Pulsar".to_string()), product_name: Some("Apache Pulsar".to_string()),
product_version: Some("3.3.0".to_string()), product_version: Some("3.3.0".to_string()),
@@ -1376,7 +1380,7 @@ mod tests {
.await; .await;
assert!(result.is_ok()); assert!(result.is_ok());
assert!(state.app.connections.read().await.contains_key(&config.id)); assert!(state.app.pool_handle(&config.id).await.is_some());
assert_eq!(state.app.configs.read().await[&config.id].database_info, Some(database_info.clone())); assert_eq!(state.app.configs.read().await[&config.id].database_info, Some(database_info.clone()));
assert_eq!(state.app.storage.load_connections().await.unwrap()[0].database_info, Some(database_info)); assert_eq!(state.app.storage.load_connections().await.unwrap()[0].database_info, Some(database_info));
@@ -1389,7 +1393,12 @@ mod tests {
let (state, dir) = test_web_state().await; let (state, dir) = test_web_state().await;
let initial = mq_config("mq-conn", "http://127.0.0.1:8080"); let initial = mq_config("mq-conn", "http://127.0.0.1:8080");
state.app.configs.write().await.insert(initial.id.clone(), initial.clone()); state.app.configs.write().await.insert(initial.id.clone(), initial.clone());
state.app.connections.write().await.insert(initial.id.clone(), PoolKind::MessageQueue); state
.app
.update_connection_pools(|connections| {
connections.insert(initial.id.clone(), PoolKind::MessageQueue);
})
.await;
let first = state.app.mq_registry.get_or_build(&initial).await.unwrap().adapter; let first = state.app.mq_registry.get_or_build(&initial).await.unwrap().adapter;
let updated = mq_config("mq-conn", "http://127.0.0.1:8081"); let updated = mq_config("mq-conn", "http://127.0.0.1:8081");
@@ -1415,7 +1424,7 @@ mod tests {
let second = state.app.mq_registry.get_or_build(&updated).await.unwrap().adapter; let second = state.app.mq_registry.get_or_build(&updated).await.unwrap().adapter;
assert!(!Arc::ptr_eq(&first, &second)); assert!(!Arc::ptr_eq(&first, &second));
assert!(!state.app.connections.read().await.contains_key(&initial.id)); assert!(!state.app.pool_handle(&initial.id).await.is_some());
let _ = std::fs::remove_dir_all(dir); let _ = std::fs::remove_dir_all(dir);
} }
@@ -1426,7 +1435,12 @@ mod tests {
let (state, dir) = test_web_state().await; let (state, dir) = test_web_state().await;
let initial = mq_config("mq-conn", "http://127.0.0.1:8080"); let initial = mq_config("mq-conn", "http://127.0.0.1:8080");
state.app.configs.write().await.insert(initial.id.clone(), initial.clone()); state.app.configs.write().await.insert(initial.id.clone(), initial.clone());
state.app.connections.write().await.insert(initial.id.clone(), PoolKind::MessageQueue); state
.app
.update_connection_pools(|connections| {
connections.insert(initial.id.clone(), PoolKind::MessageQueue);
})
.await;
let first = state.app.mq_registry.get_or_build(&initial).await.unwrap().adapter; let first = state.app.mq_registry.get_or_build(&initial).await.unwrap().adapter;
let updated = mq_config("mq-conn", &spawn_pulsar_clusters_server().await); let updated = mq_config("mq-conn", &spawn_pulsar_clusters_server().await);
@@ -1648,7 +1662,12 @@ mod tests {
let updated = mq_config("mq-conn", "http://127.0.0.1:8081"); let updated = mq_config("mq-conn", "http://127.0.0.1:8081");
state.app.storage.save_connections(std::slice::from_ref(&updated)).await.unwrap(); state.app.storage.save_connections(std::slice::from_ref(&updated)).await.unwrap();
state.app.configs.write().await.insert(initial.id.clone(), initial.clone()); state.app.configs.write().await.insert(initial.id.clone(), initial.clone());
state.app.connections.write().await.insert(initial.id.clone(), PoolKind::MessageQueue); state
.app
.update_connection_pools(|connections| {
connections.insert(initial.id.clone(), PoolKind::MessageQueue);
})
.await;
let result = load_connections(State(state.clone()), HeaderMap::new()).await; let result = load_connections(State(state.clone()), HeaderMap::new()).await;
assert!(result.is_ok()); assert!(result.is_ok());
@@ -1661,7 +1680,7 @@ mod tests {
.and_then(serde_json::Value::as_str); .and_then(serde_json::Value::as_str);
assert_eq!(cached_admin_url, Some("http://127.0.0.1:8081")); assert_eq!(cached_admin_url, Some("http://127.0.0.1:8081"));
drop(configs); drop(configs);
assert!(!state.app.connections.read().await.contains_key(&initial.id)); assert!(!state.app.pool_handle(&initial.id).await.is_some());
let _ = std::fs::remove_dir_all(dir); let _ = std::fs::remove_dir_all(dir);
} }
@@ -1838,7 +1857,12 @@ mod tests {
configs.insert(kept.id.clone(), kept.clone()); configs.insert(kept.id.clone(), kept.clone());
configs.insert(removed.id.clone(), removed.clone()); configs.insert(removed.id.clone(), removed.clone());
} }
state.app.connections.write().await.insert(removed.id.clone(), PoolKind::MessageQueue); state
.app
.update_connection_pools(|connections| {
connections.insert(removed.id.clone(), PoolKind::MessageQueue);
})
.await;
let result = save_connections( let result = save_connections(
State(state.clone()), State(state.clone()),
@@ -1848,7 +1872,7 @@ mod tests {
.await; .await;
assert!(result.is_ok()); assert!(result.is_ok());
assert!(!state.app.connections.read().await.contains_key(&removed.id)); assert!(!state.app.pool_handle(&removed.id).await.is_some());
let _ = std::fs::remove_dir_all(dir); let _ = std::fs::remove_dir_all(dir);
} }
@@ -1863,11 +1887,13 @@ mod tests {
let conn_pool = dbx_core::db::sqlite::connect_path(&conn_path.to_string_lossy()).await.unwrap(); let conn_pool = dbx_core::db::sqlite::connect_path(&conn_path.to_string_lossy()).await.unwrap();
let conn2_pool = dbx_core::db::sqlite::connect_path(&conn2_path.to_string_lossy()).await.unwrap(); let conn2_pool = dbx_core::db::sqlite::connect_path(&conn2_path.to_string_lossy()).await.unwrap();
{ state
let mut connections = state.app.connections.write().await; .app
connections.insert("conn".to_string(), PoolKind::Sqlite(conn_pool)); .update_connection_pools(|connections| {
connections.insert("conn2".to_string(), PoolKind::Sqlite(conn2_pool)); connections.insert("conn".to_string(), PoolKind::Sqlite(conn_pool));
} connections.insert("conn2".to_string(), PoolKind::Sqlite(conn2_pool));
})
.await;
let result = disconnect_db( let result = disconnect_db(
State(state.clone()), State(state.clone()),
@@ -1876,9 +1902,10 @@ mod tests {
.await; .await;
assert!(result.is_ok()); assert!(result.is_ok());
let connections = state.app.connections.read().await; let (has_conn, has_conn2) =
assert!(!connections.contains_key("conn")); state.app.with_connection_pools(|pools| (pools.contains_key("conn"), pools.contains_key("conn2"))).await;
assert!(connections.contains_key("conn2")); assert!(!has_conn);
assert!(has_conn2);
let _ = std::fs::remove_dir_all(dir); let _ = std::fs::remove_dir_all(dir);
} }
@@ -1891,7 +1918,12 @@ mod tests {
let conn_pool = dbx_core::db::sqlite::connect_path(&conn_path.to_string_lossy()).await.unwrap(); let conn_pool = dbx_core::db::sqlite::connect_path(&conn_path.to_string_lossy()).await.unwrap();
state.app.begin_connection_attempt_with_client_attempt("conn", Some(1)).await; state.app.begin_connection_attempt_with_client_attempt("conn", Some(1)).await;
let current_attempt = state.app.begin_connection_attempt_with_client_attempt("conn", Some(2)).await; let current_attempt = state.app.begin_connection_attempt_with_client_attempt("conn", Some(2)).await;
state.app.connections.write().await.insert("conn".to_string(), PoolKind::Sqlite(conn_pool)); state
.app
.update_connection_pools(|connections| {
connections.insert("conn".to_string(), PoolKind::Sqlite(conn_pool));
})
.await;
let result = disconnect_db( let result = disconnect_db(
State(state.clone()), State(state.clone()),
@@ -1900,7 +1932,7 @@ mod tests {
.await; .await;
assert!(result.is_ok()); assert!(result.is_ok());
assert!(state.app.connections.read().await.contains_key("conn")); assert!(state.app.pool_handle("conn").await.is_some());
assert!(state.app.ensure_current_connection_attempt("conn", Some(current_attempt)).await.is_ok()); assert!(state.app.ensure_current_connection_attempt("conn", Some(current_attempt)).await.is_ok());
let result = disconnect_db( let result = disconnect_db(
@@ -1910,7 +1942,7 @@ mod tests {
.await; .await;
assert!(result.is_ok()); assert!(result.is_ok());
assert!(!state.app.connections.read().await.contains_key("conn")); assert!(!state.app.pool_handle("conn").await.is_some());
assert!(state.app.ensure_current_connection_attempt("conn", Some(current_attempt)).await.is_err()); assert!(state.app.ensure_current_connection_attempt("conn", Some(current_attempt)).await.is_err());
let _ = std::fs::remove_dir_all(dir); let _ = std::fs::remove_dir_all(dir);
@@ -1923,10 +1955,12 @@ mod tests {
std::fs::File::create(&conn_path).unwrap(); std::fs::File::create(&conn_path).unwrap();
let conn_pool = dbx_core::db::sqlite::connect_path(&conn_path.to_string_lossy()).await.unwrap(); let conn_pool = dbx_core::db::sqlite::connect_path(&conn_path.to_string_lossy()).await.unwrap();
{ state
let mut connections = state.app.connections.write().await; .app
connections.insert("conn".to_string(), PoolKind::Sqlite(conn_pool)); .update_connection_pools(|connections| {
} connections.insert("conn".to_string(), PoolKind::Sqlite(conn_pool));
})
.await;
{ {
let mut configs = state.app.configs.write().await; let mut configs = state.app.configs.write().await;
configs.insert("conn".to_string(), sqlite_config("conn", &conn_path.to_string_lossy())); configs.insert("conn".to_string(), sqlite_config("conn", &conn_path.to_string_lossy()));
@@ -1951,7 +1985,12 @@ mod tests {
let (state, dir) = test_web_state().await; let (state, dir) = test_web_state().await;
let config = mq_config("mq-conn", "http://127.0.0.1:8080"); let config = mq_config("mq-conn", "http://127.0.0.1:8080");
state.app.configs.write().await.insert(config.id.clone(), config.clone()); state.app.configs.write().await.insert(config.id.clone(), config.clone());
state.app.connections.write().await.insert(config.id.clone(), PoolKind::MessageQueue); state
.app
.update_connection_pools(|connections| {
connections.insert(config.id.clone(), PoolKind::MessageQueue);
})
.await;
let first = state.app.mq_registry.get_or_build(&config).await.unwrap().adapter; let first = state.app.mq_registry.get_or_build(&config).await.unwrap().adapter;
let result = disconnect_db( let result = disconnect_db(
@@ -1961,7 +2000,7 @@ mod tests {
.await; .await;
assert!(result.is_ok()); assert!(result.is_ok());
assert!(!state.app.connections.read().await.contains_key(&config.id)); assert!(!state.app.pool_handle(&config.id).await.is_some());
let second = state.app.mq_registry.get_or_build(&config).await.unwrap().adapter; let second = state.app.mq_registry.get_or_build(&config).await.unwrap().adapter;
assert!(!Arc::ptr_eq(&first, &second)); assert!(!Arc::ptr_eq(&first, &second));
+30 -16
View File
@@ -420,7 +420,11 @@ mod tests {
.await .await
.unwrap(); .unwrap();
state.configs.write().await.insert(initial.id.clone(), initial.clone()); state.configs.write().await.insert(initial.id.clone(), initial.clone());
state.connections.write().await.insert(initial.id.clone(), PoolKind::Sqlite(pool.clone())); state
.update_connection_pools(|connections| {
connections.insert(initial.id.clone(), PoolKind::Sqlite(pool.clone()));
})
.await;
let mut invalid = initial.clone(); let mut invalid = initial.clone();
invalid.attached_databases.push(AttachedDatabaseConfig { invalid.attached_databases.push(AttachedDatabaseConfig {
@@ -430,7 +434,7 @@ mod tests {
let error = save_connection_configs(&state, &[invalid]).await.unwrap_err(); let error = save_connection_configs(&state, &[invalid]).await.unwrap_err();
assert!(error.contains("in-memory main database"), "{error}"); assert!(error.contains("in-memory main database"), "{error}");
assert!(state.connections.read().await.contains_key(&initial.id)); assert!(state.pool_handle(&initial.id).await.is_some());
assert_eq!(state.configs.read().await.get(&initial.id), Some(&initial)); assert_eq!(state.configs.read().await.get(&initial.id), Some(&initial));
let retained = dbx_core::db::sqlite::execute_query(&pool, "SELECT value FROM retained;").await.unwrap(); let retained = dbx_core::db::sqlite::execute_query(&pool, "SELECT value FROM retained;").await.unwrap();
assert_eq!(retained.rows[0][0], serde_json::json!("yes")); assert_eq!(retained.rows[0][0], serde_json::json!("yes"));
@@ -642,7 +646,11 @@ mod tests {
let state = AppState::new_with_plugin_dir(storage, dir.join("plugins")); let state = AppState::new_with_plugin_dir(storage, dir.join("plugins"));
let initial = mq_config("mq-conn", "http://127.0.0.1:8080"); let initial = mq_config("mq-conn", "http://127.0.0.1:8080");
state.configs.write().await.insert(initial.id.clone(), initial.clone()); state.configs.write().await.insert(initial.id.clone(), initial.clone());
state.connections.write().await.insert(initial.id.clone(), PoolKind::MessageQueue); state
.update_connection_pools(|connections| {
connections.insert(initial.id.clone(), PoolKind::MessageQueue);
})
.await;
let first = state.mq_registry.get_or_build(&initial).await.unwrap().adapter; let first = state.mq_registry.get_or_build(&initial).await.unwrap().adapter;
let updated = mq_config("mq-conn", "http://127.0.0.1:8081"); let updated = mq_config("mq-conn", "http://127.0.0.1:8081");
@@ -661,7 +669,7 @@ mod tests {
let second = state.mq_registry.get_or_build(&updated).await.unwrap().adapter; let second = state.mq_registry.get_or_build(&updated).await.unwrap().adapter;
assert!(!std::sync::Arc::ptr_eq(&first, &second)); assert!(!std::sync::Arc::ptr_eq(&first, &second));
assert!(!state.connections.read().await.contains_key(&initial.id)); assert!(state.pool_handle(&initial.id).await.is_none());
let _ = std::fs::remove_dir_all(dir); let _ = std::fs::remove_dir_all(dir);
} }
@@ -677,7 +685,11 @@ mod tests {
let updated = mq_config("mq-conn", "http://127.0.0.1:8081"); let updated = mq_config("mq-conn", "http://127.0.0.1:8081");
state.storage.save_connections(std::slice::from_ref(&updated)).await.unwrap(); state.storage.save_connections(std::slice::from_ref(&updated)).await.unwrap();
state.configs.write().await.insert(initial.id.clone(), initial.clone()); state.configs.write().await.insert(initial.id.clone(), initial.clone());
state.connections.write().await.insert(initial.id.clone(), PoolKind::MessageQueue); state
.update_connection_pools(|connections| {
connections.insert(initial.id.clone(), PoolKind::MessageQueue);
})
.await;
let loaded = load_connection_configs(&state).await.unwrap(); let loaded = load_connection_configs(&state).await.unwrap();
@@ -692,7 +704,7 @@ mod tests {
.and_then(serde_json::Value::as_str) .and_then(serde_json::Value::as_str)
.map(str::to_string); .map(str::to_string);
assert_eq!(cached_admin_url.as_deref(), Some("http://127.0.0.1:8081")); assert_eq!(cached_admin_url.as_deref(), Some("http://127.0.0.1:8081"));
assert!(!state.connections.read().await.contains_key(&initial.id)); assert!(state.pool_handle(&initial.id).await.is_none());
let _ = std::fs::remove_dir_all(dir); let _ = std::fs::remove_dir_all(dir);
} }
@@ -804,11 +816,15 @@ mod tests {
configs.insert(kept.id.clone(), kept.clone()); configs.insert(kept.id.clone(), kept.clone());
configs.insert(removed.id.clone(), removed.clone()); configs.insert(removed.id.clone(), removed.clone());
} }
state.connections.write().await.insert(removed.id.clone(), PoolKind::MessageQueue); state
.update_connection_pools(|connections| {
connections.insert(removed.id.clone(), PoolKind::MessageQueue);
})
.await;
save_connection_configs(&state, std::slice::from_ref(&kept)).await.unwrap(); save_connection_configs(&state, std::slice::from_ref(&kept)).await.unwrap();
assert!(!state.connections.read().await.contains_key(&removed.id)); assert!(state.pool_handle(&removed.id).await.is_none());
let _ = std::fs::remove_dir_all(dir); let _ = std::fs::remove_dir_all(dir);
} }
@@ -1696,19 +1712,17 @@ pub async fn connect_db(
), ),
DatabaseType::Redis => { DatabaseType::Redis => {
let con = if db_config.uses_redis_cluster() { let con = if db_config.uses_redis_cluster() {
PoolKind::Redis(db::redis_driver::RedisConnection::Cluster( db::redis_driver::RedisConnection::Cluster(state.connect_redis_cluster(&id, &db_config).await?)
state.connect_redis_cluster(&id, &db_config).await?,
))
} else if db_config.uses_redis_sentinel() { } else if db_config.uses_redis_sentinel() {
PoolKind::Redis(db::redis_driver::RedisConnection::Direct(tokio::sync::Mutex::new( db::redis_driver::RedisConnection::Direct(tokio::sync::Mutex::new(
state.connect_redis_sentinel(&id, &db_config).await?, state.connect_redis_sentinel(&id, &db_config).await?,
))) ))
} else { } else {
PoolKind::Redis(db::redis_driver::RedisConnection::Direct(tokio::sync::Mutex::new( db::redis_driver::RedisConnection::Direct(tokio::sync::Mutex::new(
db::redis_driver::connect_standalone(&db_config, &host, port, connect_timeout).await?, db::redis_driver::connect_standalone(&db_config, &host, port, connect_timeout).await?,
))) ))
}; };
con PoolKind::Redis(Arc::new(con))
} }
#[cfg(feature = "duckdb-sidecar")] #[cfg(feature = "duckdb-sidecar")]
DatabaseType::DuckDb => state.create_duckdb_pool(&db_config).await?, DatabaseType::DuckDb => state.create_duckdb_pool(&db_config).await?,
+2 -3
View File
@@ -12,10 +12,9 @@ async fn get_mqtt_client(
state: &AppState, state: &AppState,
connection_id: &str, connection_id: &str,
) -> Result<Arc<dbx_core::mqtt::client::MqttClient>, String> { ) -> Result<Arc<dbx_core::mqtt::client::MqttClient>, String> {
let connections = state.connections.read().await; let pool = state.pool_handle(connection_id).await.ok_or_else(|| format!("连接 {} 未建立", connection_id))?;
let pool = connections.get(connection_id).ok_or_else(|| format!("连接 {} 未建立", connection_id))?;
match pool { match pool {
PoolKind::Mqtt(client) => Ok(Arc::clone(client)), PoolKind::Mqtt(client) => Ok(client),
_ => Err(format!("连接 {} 不是 MQTT 类型", connection_id)), _ => Err(format!("连接 {} 不是 MQTT 类型", connection_id)),
} }
} }
+2 -3
View File
@@ -54,9 +54,8 @@ async fn sqlite_source_path(state: &Arc<AppState>, connection_id: &str) -> Resul
} }
async fn existing_sqlite_pool(state: &Arc<AppState>, pool_key: &str) -> Result<Option<SqliteHandle>, String> { async fn existing_sqlite_pool(state: &Arc<AppState>, pool_key: &str) -> Result<Option<SqliteHandle>, String> {
let connections = state.connections.read().await; match state.pool_handle(pool_key).await {
match connections.get(pool_key) { Some(PoolKind::Sqlite(pool)) => Ok(Some(pool)),
Some(PoolKind::Sqlite(pool)) => Ok(Some(pool.clone())),
Some(_) => Err("SQLite backup is only available for SQLite connections".to_string()), Some(_) => Err("SQLite backup is only available for SQLite connections".to_string()),
None => Ok(None), None => Ok(None),
} }