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 connections = state.connections.read().await;
let pool = connections.get(&pool_key).ok_or_else(|| "Connection not found".to_string())?;
let pool_handle = state.pool_handle(&pool_key).await;
let pool = pool_handle.as_ref().ok_or_else(|| "Connection not found".to_string())?;
match pool {
PoolKind::Agent(client) => ExplainTarget::Agent(client.clone()),
PoolKind::ExternalDriver { config, session, .. } => {
@@ -227,14 +227,18 @@ mod tests {
let storage = crate::storage::Storage::open(&dir.join("storage.db")).await.unwrap();
let state = AppState::new(storage);
state.configs.write().await.insert(config.id.clone(), config.clone());
state.connections.write().await.insert(
"oracle-jdbc".to_string(),
PoolKind::ExternalDriver {
driver_id: "jdbc".to_string(),
config: Arc::new(config),
session: session.clone(),
},
);
state
.update_connection_pools(|connections| {
connections.insert(
"oracle-jdbc".to_string(),
PoolKind::ExternalDriver {
driver_id: "jdbc".to_string(),
config: Arc::new(config),
session: session.clone(),
},
);
})
.await;
let plan = get_agent_explain_info_core(
&state,
+6 -6
View File
@@ -1013,8 +1013,8 @@ async fn ensure_etcd_dangerous_action_capability(
.get(connection_id)
.and_then(|config| crate::agent_catalog::agent_key(&config.db_type, config.driver_profile.as_deref()))
};
let connections = state.connections.read().await;
let PoolKind::Agent(client) = connections.get(connection_id).ok_or("Connection not found")? else {
let pool = state.pool_handle(connection_id).await.ok_or("Connection not found")?;
let PoolKind::Agent(client) = &pool else {
return Err("Not an agent key-value connection".to_string());
};
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> {
ensure_agent_kv_pool(state, connection_id).await?;
let connections = state.connections.read().await;
let pool = connections.get(connection_id).ok_or("Connection not found")?;
let pool_handle = state.pool_handle(connection_id).await;
let pool = pool_handle.as_ref().ok_or("Connection not found")?;
match pool {
PoolKind::Agent(client) => Ok(client.lock().await.supports_capability(AgentCapability::KvTtl)),
_ => 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()))
};
let client = {
let connections = state.connections.read().await;
match connections.get(connection_id) {
let pool_handle = state.pool_handle(connection_id).await;
match pool_handle.as_ref() {
Some(PoolKind::Agent(client)) => client.clone(),
Some(_) => return Err("Not an agent key-value connection".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 connection = agent_test_connection("dameng-1", "Dameng", DatabaseType::Dameng, "APPDB");
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 {
id: "read".to_string(),
@@ -1936,7 +1940,11 @@ for line in sys.stdin:
let state = Arc::new(AppState::new(storage));
let connection = agent_test_connection("mysql-1", "MySQL", DatabaseType::Mysql, "rs_main");
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 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> {
state.get_or_create_pool(connection_id, None).await?;
let connections = state.connections.read().await;
match connections.get(connection_id) {
let pool_handle = state.pool_handle(connection_id).await;
match pool_handle.as_ref() {
Some(PoolKind::Consul(client)) => Ok(client.clone()),
Some(_) => Err("Connection is not a Consul connection".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,
) -> Result<PostgresExtensionMembers, String> {
let pool = {
let connections = state.connections.read().await;
match connections.get(pool_key) {
let pool_handle = state.pool_handle(pool_key).await;
match pool_handle.as_ref() {
Some(crate::connection::PoolKind::Postgres(pool)) => pool.clone(),
_ => return Ok(PostgresExtensionMembers::default()),
}
@@ -1515,8 +1515,8 @@ async fn list_postgres_export_sequences(
fail_on_error: bool,
) -> Result<Vec<PostgresExportSequence>, String> {
let pool = {
let connections = state.connections.read().await;
match connections.get(pool_key) {
let pool_handle = state.pool_handle(pool_key).await;
match pool_handle.as_ref() {
Some(crate::connection::PoolKind::Postgres(pool)) => pool.clone(),
_ => return Ok(Vec::new()),
}
@@ -2718,7 +2718,8 @@ async fn export_database_sql_core_inner(
let concurrent_prefetch_is_safe =
match state.get_or_create_pool(&request.connection_id, Some(&request.database)).await {
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,
+1
View File
@@ -60,6 +60,7 @@ impl MySqlPool {
/// 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
/// must not be allowed to remove that replacement from routing.
#[cfg(test)]
pub(crate) fn is_same_pool(&self, other: &Self) -> bool {
std::sync::Arc::ptr_eq(&self.inner.metrics(), &other.inner.metrics())
}
+16
View File
@@ -305,6 +305,22 @@ pub enum RedisConnection {
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 connection: Option<Mutex<ClusterConnection>>,
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> {
ensure_document_pool(state, connection_id).await?;
let fallback_database = configured_mongo_database(state, connection_id).await;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::MongoDb(client) => match mongo_driver::list_databases(client).await {
Ok(databases) => Ok(sort_names(databases)),
Err(error) if mongo_list_databases_unauthorized(&error) => {
@@ -247,8 +247,8 @@ pub async fn list_collections_core(
database: &str,
) -> Result<Vec<CollectionInfo>, String> {
ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::MongoDb(client) => {
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();
@@ -258,7 +258,6 @@ pub async fn list_collections_core(
}
PoolKind::DynamoDb(client) => {
let client = client.clone();
drop(connections);
let names = dynamodb_driver::list_tables(&client).await?;
Ok(names
.into_iter()
@@ -306,8 +305,8 @@ pub async fn list_gridfs_files_core(
sort: Option<&str>,
) -> Result<Vec<MongoGridFsFileInfo>, String> {
ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
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()),
_ => Err("Not a MongoDB connection".to_string()),
@@ -322,8 +321,8 @@ pub async fn list_gridfs_buckets_core(
sort: Option<&str>,
) -> Result<Vec<MongoGridFsBucketInfo>, String> {
ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::MongoDb(client) => {
let names = sort_names(mongo_driver::list_collections(client, database).await?);
let bucket_names = mongo_gridfs_bucket_names(&names);
@@ -345,8 +344,8 @@ pub async fn create_gridfs_bucket_core(
bucket: &str,
) -> Result<(), String> {
ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
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()),
_ => Err("Not a MongoDB connection".to_string()),
@@ -360,8 +359,8 @@ pub async fn delete_gridfs_bucket_core(
bucket: &str,
) -> Result<(), String> {
ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
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()),
_ => Err("Not a MongoDB connection".to_string()),
@@ -376,8 +375,8 @@ pub async fn download_gridfs_file_core(
file_id: &str,
) -> Result<Vec<u8>, String> {
ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
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()),
_ => Err("Not a MongoDB connection".to_string()),
@@ -394,8 +393,8 @@ pub async fn upload_gridfs_file_core(
content_type: Option<&str>,
) -> Result<String, String> {
ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::MongoDb(client) => {
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,
) -> Result<(), String> {
ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
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()),
_ => Err("Not a MongoDB connection".to_string()),
@@ -436,8 +435,8 @@ pub async fn find_documents_core(
cursor_pagination: bool,
) -> Result<DocumentQueryResult, String> {
ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::MongoDb(client) => {
// Document browser responses must retain BSON type metadata so nested filters
// can round-trip ObjectId, Date, and int64 values through Extended JSON.
@@ -448,13 +447,11 @@ pub async fn find_documents_core(
}
PoolKind::DynamoDb(client) => {
let client = client.clone();
drop(connections);
let _ = (database, skip, projection, collation);
dynamodb_driver::find_items(&client, collection, limit, filter, sort, cursor).await
}
PoolKind::Elasticsearch(client) => {
let client = client.clone();
drop(connections);
if cursor_pagination {
elasticsearch_driver::find_documents_with_cursor(&client, collection, limit, filter, sort, cursor).await
} else {
@@ -463,7 +460,6 @@ pub async fn find_documents_core(
}
PoolKind::Easysearch(client) => {
let client = client.clone();
drop(connections);
if cursor_pagination {
easysearch_driver::find_documents_with_cursor(&client, collection, limit, filter, sort, cursor).await
} else {
@@ -472,12 +468,10 @@ pub async fn find_documents_core(
}
PoolKind::Meilisearch(client) => {
let client = client.clone();
drop(connections);
crate::db::meilisearch_driver::find_documents(&client, collection, skip, limit, filter, sort).await
}
PoolKind::VectorDb(client) => {
let client = client.clone();
drop(connections);
let _ = (filter, sort);
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>,
) -> Result<u64, String> {
ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::DynamoDb(client) => {
let client = client.clone();
drop(connections);
dynamodb_driver::count_items(&client, collection, filter).await
}
PoolKind::Elasticsearch(client) => {
let client = client.clone();
drop(connections);
elasticsearch_driver::count_documents(&client, collection, filter).await
}
PoolKind::Easysearch(client) => {
let client = client.clone();
drop(connections);
easysearch_driver::count_documents(&client, collection, filter).await
}
_ => Err("Document count is not supported for this connection".to_string()),
@@ -543,11 +534,10 @@ pub async fn describe_dynamodb_table_core(
table: &str,
) -> Result<dynamodb_driver::DynamoDbTableDescription, String> {
ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::DynamoDb(client) => {
let client = client.clone();
drop(connections);
dynamodb_driver::describe_table(&client, table).await
}
_ => Err("Not a DynamoDB connection".to_string()),
@@ -561,16 +551,14 @@ pub async fn count_elasticsearch_documents_core(
filter: Option<&str>,
) -> Result<u64, String> {
ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::Elasticsearch(client) => {
let client = client.clone();
drop(connections);
elasticsearch_driver::count_documents(&client, index, filter).await
}
PoolKind::Easysearch(client) => {
let client = client.clone();
drop(connections);
easysearch_driver::count_documents(&client, index, filter).await
}
_ => Err("Not an Elasticsearch connection".to_string()),
@@ -586,11 +574,10 @@ pub async fn elasticsearch_get_index_metadata_core(
kind: ElasticsearchIndexMetadataKind,
) -> Result<serde_json::Value, String> {
ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::Elasticsearch(client) => {
let client = client.clone();
drop(connections);
match kind {
ElasticsearchIndexMetadataKind::Mapping => {
elasticsearch_driver::get_index_mapping(&client, index).await
@@ -603,7 +590,6 @@ pub async fn elasticsearch_get_index_metadata_core(
}
PoolKind::Easysearch(client) => {
let client = client.clone();
drop(connections);
match kind {
ElasticsearchIndexMetadataKind::Mapping => easysearch_driver::get_index_mapping(&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,
) -> Result<elasticsearch_driver::ElasticsearchDeleteByQueryResult, String> {
ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::Elasticsearch(client) => {
let client = client.clone();
drop(connections);
elasticsearch_driver::delete_all_documents(&client, index).await
}
PoolKind::Easysearch(client) => {
let client = client.clone();
drop(connections);
easysearch_driver::delete_all_documents(&client, index).await
}
_ => Err("Not an Elasticsearch connection".to_string()),
@@ -651,27 +635,23 @@ pub async fn insert_document_core(
routing: Option<&str>,
) -> Result<String, String> {
ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::MongoDb(client) => mongo_driver::insert_document(client, database, collection, doc_json).await,
PoolKind::DynamoDb(client) => {
let client = client.clone();
drop(connections);
dynamodb_driver::insert_item(&client, collection, doc_json).await
}
PoolKind::Elasticsearch(client) => {
let client = client.clone();
drop(connections);
elasticsearch_driver::insert_document(&client, collection, doc_json, routing).await
}
PoolKind::Easysearch(client) => {
let client = client.clone();
drop(connections);
easysearch_driver::insert_document(&client, collection, doc_json, routing).await
}
PoolKind::Meilisearch(client) => {
let client = client.clone();
drop(connections);
crate::db::meilisearch_driver::insert_document(&client, collection, doc_json).await
}
PoolKind::Agent(client) => {
@@ -698,15 +678,12 @@ pub async fn insert_document_preserving_bson_types_core(
routing: Option<&str>,
) -> Result<String, String> {
ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::MongoDb(client) => {
mongo_driver::insert_document_extended_json(client, database, collection, doc_json).await
}
_ => {
drop(connections);
insert_document_core(state, connection_id, database, collection, doc_json, routing).await
}
_ => 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>,
) -> Result<u64, String> {
ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::MongoDb(client) => mongo_driver::update_document(client, database, collection, id, doc_json).await,
PoolKind::DynamoDb(client) => {
let client = client.clone();
drop(connections);
dynamodb_driver::update_item(&client, collection, id, doc_json).await
}
PoolKind::Elasticsearch(client) => {
let client = client.clone();
drop(connections);
// Elasticsearch requires the same custom routing value for writes
// as was used to index the document.
elasticsearch_driver::update_document(&client, collection, id, doc_json, routing).await
}
PoolKind::Easysearch(client) => {
let client = client.clone();
drop(connections);
easysearch_driver::update_document(&client, collection, id, doc_json, routing).await
}
PoolKind::Meilisearch(client) => {
let client = client.clone();
drop(connections);
crate::db::meilisearch_driver::update_document(&client, collection, id, doc_json).await
}
PoolKind::Agent(client) => {
@@ -782,29 +755,25 @@ pub async fn delete_document_core_with_type(
document_type: Option<&str>,
) -> Result<u64, String> {
ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::MongoDb(client) => mongo_driver::delete_document(client, database, collection, id).await,
PoolKind::DynamoDb(client) => {
let client = client.clone();
drop(connections);
dynamodb_driver::delete_item(&client, collection, id).await
}
PoolKind::Elasticsearch(client) => {
let client = client.clone();
drop(connections);
// Elasticsearch requires the same custom routing value for writes
// as was used to index the document.
elasticsearch_driver::delete_document(&client, collection, id, document_type, routing).await
}
PoolKind::Easysearch(client) => {
let client = client.clone();
drop(connections);
easysearch_driver::delete_document(&client, collection, id, document_type, routing).await
}
PoolKind::Meilisearch(client) => {
let client = client.clone();
drop(connections);
crate::db::meilisearch_driver::delete_document(&client, collection, id).await
}
PoolKind::Agent(client) => {
@@ -826,11 +795,10 @@ pub async fn save_meilisearch_document_batch_core(
inserts: &[String],
) -> Result<u64, String> {
ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::Meilisearch(client) => {
let client = client.clone();
drop(connections);
crate::db::meilisearch_driver::save_document_batch(&client, collection, updates, delete_ids, inserts).await
}
_ => Err("Not a Meilisearch connection".to_string()),
@@ -853,11 +821,10 @@ pub async fn meilisearch_search_documents_core(
ranking_score_threshold: Option<f64>,
) -> Result<crate::db::meilisearch_driver::MeilisearchSearchResult, String> {
ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::Meilisearch(client) => {
let client = client.clone();
drop(connections);
let hybrid = hybrid_embedder.map(|embedder| crate::db::meilisearch_driver::MeilisearchHybrid {
embedder: embedder.to_string(),
semantic_ratio: hybrid_semantic_ratio.unwrap_or(0.5),
@@ -890,11 +857,10 @@ pub async fn meilisearch_fetch_document_page_core(
offset: u64,
) -> Result<crate::db::meilisearch_driver::MeilisearchDocumentPage, String> {
ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::Meilisearch(client) => {
let client = client.clone();
drop(connections);
crate::db::meilisearch_driver::fetch_document_page(&client, index, offset, limit, filter, sort).await
}
_ => Err("Not a Meilisearch connection".to_string()),
@@ -907,11 +873,10 @@ pub async fn meilisearch_get_index_settings_core(
index: &str,
) -> Result<serde_json::Value, String> {
ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::Meilisearch(client) => {
let client = client.clone();
drop(connections);
crate::db::meilisearch_driver::get_index_settings(&client, index).await
}
_ => Err("Not a Meilisearch connection".to_string()),
@@ -925,11 +890,10 @@ pub async fn meilisearch_get_document_core(
id: &str,
) -> Result<String, String> {
ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::Meilisearch(client) => {
let client = client.clone();
drop(connections);
crate::db::meilisearch_driver::get_document(&client, index, id).await
}
_ => Err("Not a Meilisearch connection".to_string()),
@@ -943,11 +907,10 @@ pub async fn meilisearch_update_index_settings_core(
settings: &serde_json::Value,
) -> Result<(), String> {
ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::Meilisearch(client) => {
let client = client.clone();
drop(connections);
crate::db::meilisearch_driver::update_index_settings(&client, index, settings).await
}
_ => Err("Not a Meilisearch connection".to_string()),
@@ -960,11 +923,10 @@ pub async fn meilisearch_get_index_stats_core(
index: &str,
) -> Result<serde_json::Value, String> {
ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::Meilisearch(client) => {
let client = client.clone();
drop(connections);
crate::db::meilisearch_driver::get_index_stats(&client, index).await
}
_ => Err("Not a Meilisearch connection".to_string()),
@@ -977,11 +939,10 @@ pub async fn meilisearch_get_index_overview_core(
index: &str,
) -> Result<crate::db::meilisearch_driver::MeilisearchIndexOverview, String> {
ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::Meilisearch(client) => {
let client = client.clone();
drop(connections);
crate::db::meilisearch_driver::get_index_overview(&client, index).await
}
_ => 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> {
ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::Meilisearch(client) => {
let client = client.clone();
drop(connections);
crate::db::meilisearch_driver::delete_index(&client, index).await
}
_ => Err("Not a Meilisearch connection".to_string()),
@@ -1007,11 +967,10 @@ pub async fn meilisearch_delete_all_documents_core(
index: &str,
) -> Result<(), String> {
ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::Meilisearch(client) => {
let client = client.clone();
drop(connections);
crate::db::meilisearch_driver::delete_all_documents(&client, index).await
}
_ => Err("Not a Meilisearch connection".to_string()),
@@ -1023,8 +982,8 @@ async fn meilisearch_client_core(
connection_id: &str,
) -> Result<crate::db::meilisearch_driver::MeilisearchClient, String> {
ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::Meilisearch(client) => Ok(client.clone()),
_ => 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 connections = state.connections.read().await;
let connections = state.connection_pools_snapshot().await;
for (pool_key, pool) in connections.iter() {
match pool {
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> {
let pool_key = state.get_or_create_pool(connection_id, None).await?;
let connections = state.connections.read().await;
match connections.get(&pool_key) {
let pool_handle = state.pool_handle(&pool_key).await;
match pool_handle.as_ref() {
Some(PoolKind::HBase(client)) => Ok(client.clone()),
_ => 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> {
ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::MongoDb(client) => mongo_driver::create_database(client, database).await,
PoolKind::Agent(_) => Err("MongoDB legacy agent does not support create database".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> {
mongo_driver::validate_mongo_namespace_name(database, "Database")?;
ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::MongoDb(client) => mongo_driver::drop_database(client, database).await,
PoolKind::Agent(client) => {
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(collection, "Collection")?;
ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::MongoDb(client) => mongo_driver::drop_collection(client, database, collection).await,
PoolKind::Agent(client) => {
let mut client = client.lock().await;
@@ -92,8 +92,8 @@ pub async fn mongo_rename_collection_core(
new_name: &str,
) -> Result<(), String> {
ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
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()),
_ => Err("Not a MongoDB connection".to_string()),
@@ -109,8 +109,8 @@ pub async fn mongo_clone_collection_core(
) -> Result<MongoCloneCollectionResult, String> {
mongo_driver::validate_clone_collection_names(database, source_collection, target_collection)?;
ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::MongoDb(client) => {
mongo_driver::clone_collection(client, database, source_collection, target_collection).await
}
@@ -140,8 +140,8 @@ pub async fn mongo_server_version_core(
database: &str,
) -> Result<String, String> {
ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::MongoDb(client) => mongo_driver::server_version(client, database).await,
PoolKind::Agent(client) => {
let mut client = client.lock().await;
@@ -158,8 +158,8 @@ pub async fn mongo_run_command_core(
command_json: &str,
) -> Result<MongoDocumentResult, String> {
ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::MongoDb(client) => mongo_driver::run_command(client, database, command_json).await,
PoolKind::Agent(client) => {
let mut client = client.lock().await;
@@ -192,8 +192,8 @@ pub async fn mongo_collection_stats_core(
scale: Option<serde_json::Number>,
) -> Result<MongoCollectionStatsResult, String> {
ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
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()),
_ => Err("Not a MongoDB connection".to_string()),
@@ -244,8 +244,8 @@ async fn mongo_find_documents_without_total_core(
collation: Option<&str>,
) -> Result<MongoDocumentResult, String> {
ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::MongoDb(client) => {
mongo_driver::find_documents_without_total(
client, database, collection, skip, limit, filter, projection, sort, collation,
@@ -284,8 +284,8 @@ pub async fn mongo_find_one_core(
options: Option<&str>,
) -> Result<MongoDocumentResult, String> {
ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::MongoDb(client) => {
mongo_driver::find_one(client, database, collection, filter, projection, options).await
}
@@ -320,8 +320,8 @@ pub async fn mongo_explain_find_core(
verbosity: &str,
) -> Result<serde_json::Value, String> {
ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::MongoDb(client) => {
mongo_driver::explain_find(
client, database, collection, skip, limit, filter, projection, sort, collation, verbosity,
@@ -358,8 +358,8 @@ pub async fn mongo_count_documents_core(
) -> Result<u64, String> {
let accurate = mode != Some("legacy");
ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::MongoDb(client) => {
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>,
) -> Result<MongoDocumentResult, String> {
ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::MongoDb(client) => {
mongo_driver::find_documents_extended_json(
client, database, collection, skip, limit, filter, projection, sort, None,
@@ -454,8 +454,8 @@ pub async fn mongo_aggregate_documents_core(
options_json: Option<&str>,
) -> Result<MongoDocumentResult, String> {
ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::MongoDb(client) => {
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>,
) -> Result<MongoDocumentResult, String> {
ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
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.
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")?;
ensure_document_pool(state, connection_id).await?;
let is_native = {
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::MongoDb(_) => true,
PoolKind::Agent(_) => false,
_ => 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());
}
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::MongoDb(client) => mongo_driver::list_index_specs(client, database, collection).await,
_ => 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_create_index_request(keys_json, options_json)?;
ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::MongoDb(client) => {
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_create_user_request(user_json, write_concern_json)?;
ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::MongoDb(client) => {
mongo_driver::create_user(client, database, user_json, write_concern_json).await?;
Ok(1)
@@ -662,8 +662,8 @@ async fn mongo_drop_indexes_once_core(
// to Native MongoDB or the Legacy Agent.
mongo_driver::validate_drop_indexes_request(indexes_json, single)?;
ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::MongoDb(client) => {
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,
) -> Result<u64, String> {
ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::MongoDb(client) => mongo_driver::insert_documents(client, database, collection, docs_json).await,
PoolKind::Agent(client) => {
let documents: serde_json::Value =
@@ -747,8 +747,8 @@ pub async fn mongo_insert_documents_extended_json_core(
docs_json: &str,
) -> Result<u64, String> {
ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::MongoDb(client) => {
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>,
) -> Result<u64, String> {
ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::MongoDb(client) => {
mongo_driver::update_documents(client, database, collection, filter_json, update_json, many, options_json)
.await
@@ -824,8 +824,8 @@ pub async fn mongo_delete_documents_core(
many: bool,
) -> Result<u64, String> {
ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::MongoDb(client) => {
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>,
) -> Result<MongoDocumentResult, String> {
ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::MongoDb(client) => {
mongo_driver::find_one_and_update(client, database, collection, filter_json, update_json, options_json)
.await
@@ -876,8 +876,8 @@ pub async fn mongo_find_one_and_replace_core(
options_json: Option<&str>,
) -> Result<MongoDocumentResult, String> {
ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::MongoDb(client) => {
mongo_driver::find_one_and_replace(
client,
@@ -903,8 +903,8 @@ pub async fn mongo_find_one_and_delete_core(
options_json: Option<&str>,
) -> Result<MongoDocumentResult, String> {
ensure_document_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::MongoDb(client) => {
mongo_driver::find_one_and_delete(client, database, collection, filter_json, options_json).await
}
@@ -1409,6 +1409,7 @@ mod tests {
&[AgentCapability::MongoDropDatabase.as_str()],
None,
None,
None,
)
.await
}
@@ -1420,8 +1421,16 @@ mod tests {
expected_result: serde_json::Value,
capabilities: &[&str],
) -> (AppState, tempfile::TempDir) {
legacy_mongo_state_with_options(expected_method, expected_params, expected_result, capabilities, None, None)
.await
legacy_mongo_state_with_options(
expected_method,
expected_params,
expected_result,
capabilities,
None,
None,
None,
)
.await
}
#[cfg(unix)]
@@ -1438,6 +1447,7 @@ mod tests {
&[AgentCapability::MongoDropDatabase.as_str()],
Some(server_version),
None,
None,
)
.await
}
@@ -1456,6 +1466,7 @@ mod tests {
&[AgentCapability::MongoDropDatabase.as_str()],
Some(server_version),
Some(expected_error),
None,
)
.await
}
@@ -1468,6 +1479,7 @@ mod tests {
capabilities: &[&str],
server_version: Option<&str>,
expected_error: Option<&str>,
response_delay_ms: Option<u64>,
) -> (AppState, tempfile::TempDir) {
use std::io::Write;
@@ -1482,10 +1494,13 @@ mod tests {
};
let server_version = python_optional_string(server_version);
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!(
script,
r#"import json
import sys
import time
EXPECTED_METHOD = {expected_method}
EXPECTED_PARAMS = json.loads({expected_params})
@@ -1493,6 +1508,8 @@ EXPECTED_RESULT = json.loads({expected_result})
CAPABILITIES = {capabilities}
SERVER_VERSION = {server_version}
EXPECTED_ERROR = {expected_error}
RESPONSE_DELAY_SECONDS = {response_delay_ms} / 1000
REQUEST_ENTERED_PATH = {request_entered_path}
expected_calls = 0
print(json.dumps({{"ready": True}}), flush=True)
@@ -1515,6 +1532,9 @@ for line in sys.stdin:
if expected_calls > 1:
print(json.dumps({{"jsonrpc": "2.0", "id": request["id"], "error": {{"code": -1, "message": "duplicate MongoDB RPC"}}}}), flush=True)
continue
if RESPONSE_DELAY_SECONDS:
open(REQUEST_ENTERED_PATH, "w").close()
time.sleep(RESPONSE_DELAY_SECONDS)
if EXPECTED_ERROR is not None:
print(json.dumps({{"jsonrpc": "2.0", "id": request["id"], "error": {{"code": -1, "message": EXPECTED_ERROR}}}}), flush=True)
else:
@@ -1547,7 +1567,11 @@ for line in sys.stdin:
}))
.unwrap();
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)
}
@@ -1858,6 +1882,54 @@ for line in sys.stdin:
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]
fn mongo_indexes_query_result_matches_desktop_contract_and_limits_rows() {
let result = mongo_indexes_query_result(
+71 -79
View File
@@ -1674,8 +1674,7 @@ async fn sqlserver_pool_is_current(
pool_key: &str,
client: &Arc<tokio::sync::Mutex<db::sqlserver::SqlServerClient>>,
) -> bool {
let connections = state.connections.read().await;
matches!(connections.get(pool_key), Some(PoolKind::SqlServer(current)) if Arc::ptr_eq(current, client))
matches!(state.pool_handle(pool_key).await, Some(PoolKind::SqlServer(current)) if Arc::ptr_eq(&current, client))
}
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 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 connections = state.connections.read().await;
let pool = connections.get(pool_key).ok_or("Connection not found")?;
let pool = state.pool_handle(pool_key).await.ok_or("Connection not found")?;
let mut typed_agent_error = None;
#[cfg(feature = "duckdb-sidecar")]
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")]
PoolKind::DuckDbWorker(client) => {
let client = client.clone();
@@ -1793,7 +1791,6 @@ async fn do_execute_typed(
let sql = sql.to_string();
let database = database.map(str::to_string);
let max_rows = options.max_rows;
drop(connections);
match client.execute_typed(database, sql, max_rows, cancel_token, query_timeout).await {
Ok(result) => Ok(result),
Err(error) => {
@@ -1815,7 +1812,6 @@ async fn do_execute_typed(
let bare = *mode == crate::connection::MysqlMode::Bare;
let max_rows = options.max_rows;
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(
&p,
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 execution_mode = options.execution_mode;
let cancel_context = state.get_postgres_cancel_context(pool_key).await;
drop(connections);
if execution_mode == QueryExecutionMode::PostgresReadOnlyTransaction {
db::postgres::execute_query_in_read_only_transaction_with_rollback(
&p,
@@ -1920,14 +1915,12 @@ async fn do_execute_typed(
PoolKind::Sqlite(p) => {
let p = p.clone();
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))
.await
}
PoolKind::Rqlite(client) => {
let client = client.clone();
let max_rows = options.max_rows;
drop(connections);
wait_for_query_opt(
cancel_token,
query_timeout,
@@ -1938,7 +1931,6 @@ async fn do_execute_typed(
PoolKind::Turso(client) => {
let client = client.clone();
let max_rows = options.max_rows;
drop(connections);
wait_for_query_opt(
cancel_token,
query_timeout,
@@ -1949,7 +1941,6 @@ async fn do_execute_typed(
PoolKind::CloudflareD1(client) => {
let client = client.clone();
let max_rows = options.max_rows;
drop(connections);
wait_for_query_opt(
cancel_token,
query_timeout,
@@ -1961,7 +1952,6 @@ async fn do_execute_typed(
let client = client.clone();
let database = pool_key.split(':').nth(1).unwrap_or("default").to_string();
let max_rows = options.max_rows;
drop(connections);
let result = wait_for_query_opt(
cancel_token,
query_timeout,
@@ -1978,7 +1968,6 @@ async fn do_execute_typed(
let client = client.clone();
let max_rows = options.max_rows;
let execution_mode = options.execution_mode;
drop(connections);
let (mut client, lock_wait_ms) =
match lock_shared_client_with_wait(&client, cancel_token.clone(), None).await {
Ok(value) => value,
@@ -2010,7 +1999,6 @@ async fn do_execute_typed(
let client = client.clone();
let sql = sql.to_string();
let max_rows = options.max_rows;
drop(connections);
let result = wait_for_query_opt(
cancel_token,
query_timeout,
@@ -2031,7 +2019,6 @@ async fn do_execute_typed(
let client = client.clone();
let sql = sql.to_string();
let max_rows = options.max_rows;
drop(connections);
let result = wait_for_query_opt(
cancel_token,
query_timeout,
@@ -2052,7 +2039,6 @@ async fn do_execute_typed(
let client = client.clone();
let sql = sql.to_string();
let max_rows = options.max_rows;
drop(connections);
let result = wait_for_query_opt(
cancel_token,
query_timeout,
@@ -2069,7 +2055,6 @@ async fn do_execute_typed(
let client = client.clone();
let sql = sql.to_string();
let max_rows = options.max_rows;
drop(connections);
let result =
wait_for_query_opt(cancel_token, query_timeout, db::vector_driver::execute_rest_query(&client, &sql))
.await
@@ -2089,7 +2074,6 @@ async fn do_execute_typed(
let client = client.clone();
let database = pool_key.split(':').nth(1).unwrap_or("default").to_string();
let max_rows = options.max_rows;
drop(connections);
let result = wait_for_query_opt(
cancel_token,
query_timeout,
@@ -2106,7 +2090,6 @@ async fn do_execute_typed(
let client = client.clone();
let database = pool_key.split(':').nth(1).unwrap_or("default").to_string();
let max_rows = options.max_rows;
drop(connections);
let result = wait_for_query_opt(
cancel_token,
query_timeout,
@@ -2122,7 +2105,6 @@ async fn do_execute_typed(
PoolKind::VictoriaMetrics(client) => {
let client = client.clone();
let max_rows = options.max_rows;
drop(connections);
let result = wait_for_query_opt(
cancel_token,
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 max_rows = options.max_rows;
let rpc_timeout = query_timeout;
drop(connections);
if is_canceled(&cancel_token) {
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 max_rows = options.max_rows;
let plugin_timeout = query_timeout;
drop(connections);
wait_for_query_opt(cancel_token, query_timeout, async move {
if let Some(session_id) = options.result_session_id.as_deref() {
let params = external_driver_fetch_query_page_params(
@@ -2233,7 +2213,6 @@ async fn do_execute_typed(
let client = client.clone();
let sql = sql.to_string();
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.
let execution = Box::pin(db::dynamodb_driver::execute_statement(&client, &sql, max_rows));
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?;
let pool = {
let connections = state.connections.read().await;
match connections.get(&pool_key) {
let pool_handle = state.pool_handle(&pool_key).await;
match pool_handle.as_ref() {
Some(PoolKind::Postgres(pool)) => pool.clone(),
Some(_) => return Err("DROP DATABASE reconnect did not create a PostgreSQL connection".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_key = state.get_or_create_pool_for_session(connection_id, pool_database, client_session_id).await?;
let connections = state.connections.read().await;
let pool = connections.get(&pool_key).ok_or("Connection not found")?;
let pool_handle = state.pool_handle(&pool_key).await;
let pool = pool_handle.as_ref().ok_or("Connection not found")?;
match pool {
PoolKind::Agent(client) => {
let client = client.clone();
drop(connections);
let mut client = client.lock().await;
client.close_query_session(session_id).await
}
PoolKind::ExternalDriver { config, session, .. } => {
let config = config.clone();
let session = session.clone();
drop(connections);
let params = external_driver_fetch_query_page_params(config.as_ref(), session_id, 1);
session
.invoke::<serde_json::Value>("closeQuerySession", params)
@@ -2807,13 +2784,11 @@ pub async fn close_query_session(
}
PoolKind::Elasticsearch(client) => {
let client = client.clone();
drop(connections);
db::elasticsearch_driver::close_cursor(&client, session_id).await?;
Ok(true)
}
PoolKind::Easysearch(client) => {
let client = client.clone();
drop(connections);
db::easysearch_driver::close_cursor(&client, session_id).await?;
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;
let _activity_touch = state.pool_activity_touch(pool_key.as_str());
let is_sqlserver = {
let connections = state.connections.read().await;
matches!(connections.get(&pool_key), Some(PoolKind::SqlServer(_)))
};
let is_sqlserver = { matches!(state.pool_handle(&pool_key).await, Some(PoolKind::SqlServer(_))) };
if is_sqlserver {
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 connections = state.connections.read().await;
match connections.get(&pool_key) {
let pool_handle = state.pool_handle(&pool_key).await;
match pool_handle.as_ref() {
Some(PoolKind::Mysql(pool, mode)) => Some((pool.clone(), *mode)),
_ => None,
}
@@ -3595,13 +3567,12 @@ async fn execute_multi_sqlserver(
break;
}
let connections = state.connections.read().await;
let pool = connections.get(pool_key).ok_or("Connection not found")?;
let pool_handle = state.pool_handle(pool_key).await;
let pool = pool_handle.as_ref().ok_or("Connection not found")?;
let client = match pool {
PoolKind::SqlServer(c) => c.clone(),
_ => return Err("Expected SQL Server connection".to_string()),
};
drop(connections);
let (mut client_guard, lock_wait_ms) =
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 agent_client = {
let conns = state.connections.read().await;
match conns.get(&pool_key) {
let pool_handle = state.pool_handle(&pool_key).await;
match pool_handle.as_ref() {
Some(PoolKind::Agent(client)) => Some(client.clone()),
_ => None,
}
@@ -4047,10 +4018,8 @@ pub async fn execute_schema_diff_deploy(
}
}
};
let has_transactional_path = {
let conns = state.connections.read().await;
conns.get(&pool_key).is_some_and(pool_kind_has_transactional_path)
};
let has_transactional_path =
{ state.pool_handle(&pool_key).await.as_ref().is_some_and(pool_kind_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)
@@ -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.
let path = {
let conns = state.connections.read().await;
conns.get(pool_key).map(|p| match p {
state.pool_handle(pool_key).await.as_ref().map(|p| match p {
PoolKind::Postgres(pg) => TxPath::Pg(pg.clone()),
PoolKind::Mysql(mp, _mode) => TxPath::Mysql(mp.clone(), false),
PoolKind::Sqlite(sq) => TxPath::Sqlite(sq.clone()),
@@ -4736,8 +4704,8 @@ async fn begin_transaction_session(
ExternalDriver,
}
let pool_handle = {
let connections = state.connections.read().await;
match connections.get(&probe_pool_key).ok_or("Connection not found")? {
let pool = state.pool_handle(&probe_pool_key).await.ok_or("Connection not found")?;
match &pool {
PoolKind::Postgres(pg) => TxnPoolHandle::Postgres(pg.clone()),
PoolKind::Mysql(mp, _) => TxnPoolHandle::Mysql(mp.clone()),
PoolKind::Agent(_) if !consistent_snapshot => TxnPoolHandle::Agent,
@@ -4792,8 +4760,8 @@ async fn begin_transaction_session(
let agent_pool_key =
state.get_or_create_pool_for_session(connection_id, pool_database, Some(&client_session_id)).await?;
let client = {
let connections = state.connections.read().await;
match connections.get(&agent_pool_key) {
let pool_handle = state.pool_handle(&agent_pool_key).await;
match pool_handle.as_ref() {
Some(PoolKind::Agent(client)) => client.clone(),
_ => {
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 =
state.get_or_create_pool_for_session(connection_id, pool_database, Some(&client_session_id)).await?;
let (config, session) = {
let connections = state.connections.read().await;
match connections.get(&external_pool_key) {
let pool_handle = state.pool_handle(&external_pool_key).await;
match pool_handle.as_ref() {
Some(PoolKind::ExternalDriver { config, session, .. }) => (config.clone(), session.clone()),
_ => {
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();
db::dynamodb_driver::test_connection(&client, Duration::from_secs(5)).await.unwrap();
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(
&state,
@@ -6294,13 +6266,17 @@ for line in sys.stdin:
let storage = Storage::open(&dir.join("storage.db")).await.unwrap();
let state = AppState::new(storage);
state.configs.write().await.insert("conn-1".to_string(), test_connection_config(DatabaseType::Dameng));
state.connections.write().await.insert(
"conn-1".to_string(),
PoolKind::agent(crate::db::agent_driver::AgentDriverClient::shared_session(
runtime.clone(),
"session-1".to_string(),
)),
);
state
.update_connection_pools(|connections| {
connections.insert(
"conn-1".to_string(),
PoolKind::agent(crate::db::agent_driver::AgentDriverClient::shared_session(
runtime.clone(),
"session-1".to_string(),
)),
);
})
.await;
(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();
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());
runtime.kill();
@@ -6350,7 +6326,11 @@ for line in sys.stdin:
let state = AppState::new(storage);
let connection_id = "sqlite-cancel";
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));
let cancel_token = CancellationToken::new();
cancel_token.cancel();
@@ -6434,7 +6414,7 @@ for line in sys.stdin:
.unwrap_err();
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());
runtime.kill();
@@ -6451,7 +6431,7 @@ for line in sys.stdin:
.unwrap_err();
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());
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();
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());
runtime.kill();
@@ -6526,7 +6506,11 @@ for line in sys.stdin:
let state = AppState::new(storage);
let connection_id = "sqlite-batch";
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));
let sql = if failure_first {
@@ -6622,7 +6606,11 @@ for line in sys.stdin:
let state = AppState::new(storage);
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();
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));
let results = execute_multi_core_with_options(
@@ -7766,14 +7754,18 @@ for line in sys.stdin:
let client_session_id = "manual-txn-test";
let pool_key = "jdbc-conn:session:manual-txn-test";
let config = Arc::new(config);
state.connections.write().await.insert(
pool_key.to_string(),
PoolKind::ExternalDriver {
driver_id: "jdbc".to_string(),
config: config.clone(),
session: session.clone(),
},
);
state
.update_connection_pools(|connections| {
connections.insert(
pool_key.to_string(),
PoolKind::ExternalDriver {
driver_id: "jdbc".to_string(),
config: config.clone(),
session: session.clone(),
},
);
})
.await;
let cleanup_guard =
state.workload_session_pool_cleanup_guard("jdbc-conn", Some("dbx_test"), client_session_id).await.unwrap();
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();
assert_eq!(results[0].rows, vec![vec![serde_json::json!(42)]]);
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!(
std::fs::read_to_string(&calls).unwrap(),
"beginManualTransaction\nexecuteInManualTransaction\ncommitManualTransaction\n"
+8 -12
View File
@@ -1037,14 +1037,13 @@ async fn try_export_postgres_query_result_stream(
)
.await?
};
let connections = state.connections.read().await;
let Some(pool) = connections.get(&pool_key).and_then(|pool| match pool {
let pool_handle = state.pool_handle(&pool_key).await;
let Some(pool) = pool_handle.as_ref().and_then(|pool| match pool {
PoolKind::Postgres(pool) => Some(pool.clone()),
_ => None,
}) else {
return Ok(false);
};
drop(connections);
if let Some(execution_id) = request.execution_id.as_deref() {
state.running_queries.set_pool_key(execution_id, pool_key.clone());
@@ -1212,14 +1211,13 @@ async fn try_export_mysql_query_result_stream(
)
.await?
};
let connections = state.connections.read().await;
let Some((pool, bare)) = connections.get(&pool_key).and_then(|pool| match pool {
let pool_handle = state.pool_handle(&pool_key).await;
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)),
_ => None,
}) else {
return Ok(false);
};
drop(connections);
if let Some(execution_id) = request.execution_id.as_deref() {
state.running_queries.set_pool_key(execution_id, pool_key.clone());
@@ -1487,14 +1485,13 @@ async fn try_export_clickhouse_query_result_stream(
)
.await?
};
let connections = state.connections.read().await;
let Some(client) = connections.get(&pool_key).and_then(|pool| match pool {
let pool_handle = state.pool_handle(&pool_key).await;
let Some(client) = pool_handle.as_ref().and_then(|pool| match pool {
PoolKind::ClickHouse(client) => Some(client.clone()),
_ => None,
}) else {
return Ok(false);
};
drop(connections);
if let Some(execution_id) = request.execution_id.as_deref() {
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 connections = state.connections.read().await;
let Some(client) = connections.get(&pool_key).and_then(|pool| match pool {
let pool_handle = state.pool_handle(&pool_key).await;
let Some(client) = pool_handle.as_ref().and_then(|pool| match pool {
PoolKind::SqlServer(client) => Some(client.clone()),
_ => None,
}) else {
return Ok(false);
};
drop(connections);
if let Some(execution_id) = request.execution_id.as_deref() {
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,
) -> Result<Vec<RedisDatabaseInfo>, String> {
ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await;
let pool = connections.get(connection_id).ok_or("Connection not found")?;
match pool {
PoolKind::Redis(redis) => match redis {
let pool = state.pool_handle(connection_id).await.ok_or("Connection not found")?;
match &pool {
PoolKind::Redis(redis) => match redis.as_ref() {
RedisConnection::Direct(con) => {
let mut con = con.lock().await;
redis_driver::list_databases(&mut *con).await
@@ -54,10 +53,9 @@ pub async fn redis_scan_keys_batch_core(
include_types: bool,
) -> Result<RedisScanResult, String> {
ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await;
let pool = connections.get(connection_id).ok_or("Connection not found")?;
match pool {
PoolKind::Redis(redis) => match redis {
let pool = state.pool_handle(connection_id).await.ok_or("Connection not found")?;
match &pool {
PoolKind::Redis(redis) => match redis.as_ref() {
RedisConnection::Direct(con) => {
let mut con = con.lock().await;
redis_driver::select_db(&mut *con, db).await?;
@@ -85,10 +83,9 @@ pub async fn redis_scan_values_core(
count: usize,
) -> Result<RedisScanResult, String> {
ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await;
let pool = connections.get(connection_id).ok_or("Connection not found")?;
match pool {
PoolKind::Redis(redis) => match redis {
let pool = state.pool_handle(connection_id).await.ok_or("Connection not found")?;
match &pool {
PoolKind::Redis(redis) => match redis.as_ref() {
RedisConnection::Direct(con) => {
let mut con = con.lock().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,
) -> Result<RedisValue, String> {
ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await;
let pool = connections.get(connection_id).ok_or("Connection not found")?;
match pool {
let pool = state.pool_handle(connection_id).await.ok_or("Connection not found")?;
match &pool {
PoolKind::Redis(redis) => {
let key = redis_driver::redis_key_raw_to_bytes(key_raw)?;
match redis {
match redis.as_ref() {
RedisConnection::Direct(con) => {
let mut con = con.lock().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,
) -> Result<i64, String> {
ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await;
let pool = connections.get(connection_id).ok_or("Connection not found")?;
match pool {
let pool = state.pool_handle(connection_id).await.ok_or("Connection not found")?;
match &pool {
PoolKind::Redis(redis) => {
let key = redis_driver::redis_key_raw_to_bytes(key_raw)?;
match redis {
match redis.as_ref() {
RedisConnection::Direct(con) => {
let mut con = con.lock().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>,
) -> Result<RedisStreamPage, String> {
ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await;
let pool = connections.get(connection_id).ok_or("Connection not found")?;
match pool {
let pool = state.pool_handle(connection_id).await.ok_or("Connection not found")?;
match &pool {
PoolKind::Redis(redis) => {
let key = redis_driver::redis_key_raw_to_bytes(key_raw)?;
match redis {
match redis.as_ref() {
RedisConnection::Direct(con) => {
let mut con = con.lock().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,
) -> Result<Vec<RedisStreamGroup>, String> {
ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await;
let pool = connections.get(connection_id).ok_or("Connection not found")?;
match pool {
let pool = state.pool_handle(connection_id).await.ok_or("Connection not found")?;
match &pool {
PoolKind::Redis(redis) => {
let key = redis_driver::redis_key_raw_to_bytes(key_raw)?;
match redis {
match redis.as_ref() {
RedisConnection::Direct(con) => {
let mut con = con.lock().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,
) -> Result<Vec<RedisStreamConsumer>, String> {
ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await;
let pool = connections.get(connection_id).ok_or("Connection not found")?;
match pool {
let pool = state.pool_handle(connection_id).await.ok_or("Connection not found")?;
match &pool {
PoolKind::Redis(redis) => {
let key = redis_driver::redis_key_raw_to_bytes(key_raw)?;
let group = redis_driver::redis_key_raw_to_bytes(group_raw)?;
match redis {
match redis.as_ref() {
RedisConnection::Direct(con) => {
let mut con = con.lock().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>,
) -> Result<RedisStreamPendingPage, String> {
ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await;
let pool = connections.get(connection_id).ok_or("Connection not found")?;
match pool {
let pool = state.pool_handle(connection_id).await.ok_or("Connection not found")?;
match &pool {
PoolKind::Redis(redis) => {
let key = redis_driver::redis_key_raw_to_bytes(key_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()?;
match redis {
match redis.as_ref() {
RedisConnection::Direct(con) => {
let mut con = con.lock().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>,
) -> Result<(), String> {
ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await;
let pool = connections.get(connection_id).ok_or("Connection not found")?;
match pool {
let pool = state.pool_handle(connection_id).await.ok_or("Connection not found")?;
match &pool {
PoolKind::Redis(redis) => {
let key = redis_driver::redis_key_raw_to_bytes(key_raw)?;
match redis {
match redis.as_ref() {
RedisConnection::Direct(con) => {
let mut con = con.lock().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,
) -> Result<(), String> {
ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await;
let pool = connections.get(connection_id).ok_or("Connection not found")?;
match pool {
let pool = state.pool_handle(connection_id).await.ok_or("Connection not found")?;
match &pool {
PoolKind::Redis(redis) => {
let key = redis_driver::redis_key_raw_to_bytes(key_raw)?;
match redis {
match redis.as_ref() {
RedisConnection::Direct(con) => {
let mut con = con.lock().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,
) -> Result<(), String> {
ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await;
let pool = connections.get(connection_id).ok_or("Connection not found")?;
let pool = state.pool_handle(connection_id).await.ok_or("Connection not found")?;
let key = redis_driver::redis_key_raw_to_bytes(key_raw)?;
let new_key = redis_driver::redis_key_raw_to_bytes(new_key_raw)?;
if key == new_key {
return Ok(());
}
match pool {
PoolKind::Redis(redis) => match redis {
match &pool {
PoolKind::Redis(redis) => match redis.as_ref() {
RedisConnection::Direct(con) => {
let mut con = con.lock().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>,
) -> Result<(), String> {
ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::Redis(redis) => {
let key = redis_driver::redis_key_raw_to_bytes(key_raw)?;
match redis {
match redis.as_ref() {
RedisConnection::Direct(con) => {
let mut con = con.lock().await;
redis_driver::select_db(&mut *con, db).await?;
@@ -452,11 +440,11 @@ pub async fn redis_hash_del_in_db_core(
field: &str,
) -> Result<(), String> {
ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::Redis(redis) => {
let key = redis_driver::redis_key_raw_to_bytes(key_raw)?;
match redis {
match redis.as_ref() {
RedisConnection::Direct(con) => {
let mut con = con.lock().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,
) -> Result<(), String> {
ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::Redis(redis) => {
let key = redis_driver::redis_key_raw_to_bytes(key_raw)?;
match redis {
match redis.as_ref() {
RedisConnection::Direct(con) => {
let mut con = con.lock().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,
) -> Result<(), String> {
ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::Redis(redis) => {
let key = redis_driver::redis_key_raw_to_bytes(key_raw)?;
match redis {
match redis.as_ref() {
RedisConnection::Direct(con) => {
let mut con = con.lock().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,
) -> Result<(), String> {
ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::Redis(redis) => {
let key = redis_driver::redis_key_raw_to_bytes(key_raw)?;
match redis {
match redis.as_ref() {
RedisConnection::Direct(con) => {
let mut con = con.lock().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>,
) -> Result<(), String> {
ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::Redis(redis) => {
let key = redis_driver::redis_key_raw_to_bytes(key_raw)?;
match redis {
match redis.as_ref() {
RedisConnection::Direct(con) => {
let mut con = con.lock().await;
redis_driver::select_db(&mut *con, db).await?;
@@ -619,11 +607,11 @@ pub async fn redis_list_set_in_db_core(
value: &str,
) -> Result<(), String> {
ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::Redis(redis) => {
let key = redis_driver::redis_key_raw_to_bytes(key_raw)?;
match redis {
match redis.as_ref() {
RedisConnection::Direct(con) => {
let mut con = con.lock().await;
redis_driver::select_db(&mut *con, db).await?;
@@ -657,11 +645,11 @@ pub async fn redis_list_remove_in_db_core(
index: i64,
) -> Result<(), String> {
ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::Redis(redis) => {
let key = redis_driver::redis_key_raw_to_bytes(key_raw)?;
match redis {
match redis.as_ref() {
RedisConnection::Direct(con) => {
let mut con = con.lock().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>,
) -> Result<(), String> {
ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::Redis(redis) => {
let key = redis_driver::redis_key_raw_to_bytes(key_raw)?;
match redis {
match redis.as_ref() {
RedisConnection::Direct(con) => {
let mut con = con.lock().await;
redis_driver::select_db(&mut *con, db).await?;
@@ -735,11 +723,11 @@ pub async fn redis_set_remove_in_db_core(
member: &str,
) -> Result<(), String> {
ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::Redis(redis) => {
let key = redis_driver::redis_key_raw_to_bytes(key_raw)?;
match redis {
match redis.as_ref() {
RedisConnection::Direct(con) => {
let mut con = con.lock().await;
redis_driver::select_db(&mut *con, db).await?;
@@ -766,11 +754,11 @@ pub async fn redis_zadd_in_db_core(
ttl: Option<i64>,
) -> Result<(), String> {
ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::Redis(redis) => {
let key = redis_driver::redis_key_raw_to_bytes(key_raw)?;
match redis {
match redis.as_ref() {
RedisConnection::Direct(con) => {
let mut con = con.lock().await;
redis_driver::select_db(&mut *con, db).await?;
@@ -795,11 +783,11 @@ pub async fn redis_zrem_in_db_core(
member: &str,
) -> Result<(), String> {
ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::Redis(redis) => {
let key = redis_driver::redis_key_raw_to_bytes(key_raw)?;
match redis {
match redis.as_ref() {
RedisConnection::Direct(con) => {
let mut con = con.lock().await;
redis_driver::select_db(&mut *con, db).await?;
@@ -827,11 +815,11 @@ pub async fn redis_zset_update_in_db_core(
score: &str,
) -> Result<bool, String> {
ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::Redis(redis) => {
let key = redis_driver::redis_key_raw_to_bytes(key_raw)?;
match redis {
match redis.as_ref() {
RedisConnection::Direct(con) => {
let mut con = con.lock().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>,
) -> Result<(), String> {
ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::Redis(redis) => {
let key = redis_driver::redis_key_raw_to_bytes(key_raw)?;
match redis {
match redis.as_ref() {
RedisConnection::Direct(con) => {
let mut con = con.lock().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>,
) -> Result<(), String> {
ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::Redis(redis) => {
let key = redis_driver::redis_key_raw_to_bytes(key_raw)?;
match redis {
match redis.as_ref() {
RedisConnection::Direct(con) => {
let mut con = con.lock().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,
) -> Result<bool, String> {
ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
PoolKind::Redis(redis) => match redis {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::Redis(redis) => match redis.as_ref() {
RedisConnection::Direct(con) => {
let mut con = con.lock().await;
redis_driver::select_db(&mut *con, db).await?;
@@ -941,11 +929,11 @@ pub async fn redis_set_ttl_in_db_core(
ttl: i64,
) -> Result<(), String> {
ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::Redis(redis) => {
let key = redis_driver::redis_key_raw_to_bytes(key_raw)?;
match redis {
match redis.as_ref() {
RedisConnection::Direct(con) => {
let mut con = con.lock().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,
) -> Result<(), String> {
ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::Redis(redis) => {
let key = redis_driver::redis_key_raw_to_bytes(key_raw)?;
match redis {
match redis.as_ref() {
RedisConnection::Direct(con) => {
let mut con = con.lock().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],
) -> Result<u64, String> {
ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::Redis(redis) => {
let keys: Result<Vec<Vec<u8>>, String> =
key_raws.iter().map(|k| redis_driver::redis_key_raw_to_bytes(k)).collect();
let keys = keys?;
match redis {
match redis.as_ref() {
RedisConnection::Direct(con) => {
let mut con = con.lock().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> {
ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
PoolKind::Redis(redis) => match redis {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::Redis(redis) => match redis.as_ref() {
RedisConnection::Direct(con) => {
let mut con = con.lock().await;
redis_driver::select_db(&mut *con, db).await?;
@@ -1052,9 +1040,9 @@ pub async fn redis_execute_command_core(
skip_safety_check: bool,
) -> Result<RedisCommandResult, String> {
ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
PoolKind::Redis(redis) => match redis {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::Redis(redis) => match redis.as_ref() {
RedisConnection::Direct(con) => {
let mut con = con.lock().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>,
) -> Result<RedisCollectionPage, String> {
ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::Redis(redis) => {
let key = redis_driver::redis_key_raw_to_bytes(key_raw)?;
match redis {
match redis.as_ref() {
RedisConnection::Direct(con) => {
let mut con = con.lock().await;
redis_driver::select_db(&mut *con, db).await?;
@@ -1135,9 +1123,9 @@ pub async fn redis_publish_core(
message: &str,
) -> Result<u64, String> {
ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
PoolKind::Redis(redis) => match redis {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::Redis(redis) => match redis.as_ref() {
RedisConnection::Direct(con) => {
let mut con = con.lock().await;
redis_driver::select_db(&mut *con, db).await?;
@@ -1175,9 +1163,9 @@ pub async fn redis_slowlog_get_core(
node_port: Option<u16>,
) -> Result<Vec<redis_driver::RedisSlowlogEntry>, String> {
ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
PoolKind::Redis(redis) => match redis {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::Redis(redis) => match redis.as_ref() {
RedisConnection::Direct(con) => {
let mut con = con.lock().await;
// 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,
) -> Result<Vec<redis_driver::RedisNodeEndpoint>, String> {
ensure_redis_pool(state, connection_id).await?;
let connections = state.connections.read().await;
match connections.get(connection_id).ok_or("Not found")? {
PoolKind::Redis(redis) => match redis {
let pool = state.pool_handle(connection_id).await.ok_or("Not found")?;
match &pool {
PoolKind::Redis(redis) => match redis.as_ref() {
RedisConnection::Cluster(cluster) => redis_driver::cluster_master_nodes(cluster).await,
_ => 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 pool_key = state.get_or_create_pool_for_session(&request.connection_id, database, None).await?;
let (db_type, driver_profile, bare) = {
let connections = state.connections.read().await;
let Some(PoolKind::Mysql(_, mode)) = connections.get(&pool_key) else {
let pool_handle = state.pool_handle(&pool_key).await;
let Some(PoolKind::Mysql(_, mode)) = pool_handle.as_ref() else {
return Ok(None);
};
(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);
self.pool_key = state.get_or_create_pool_for_session(&self.connection_id, database, None).await?;
let pool = {
let connections = state.connections.read().await;
match connections.get(&self.pool_key) {
let pool_handle = state.pool_handle(&self.pool_key).await;
match pool_handle.as_ref() {
Some(PoolKind::Mysql(pool, _)) => pool.clone(),
Some(_) => return Err("SQL file import expected a MySQL-compatible pooled connection".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();
state
.connections
.write()
.await
.insert("gauss-stream".to_string(), crate::connection::PoolKind::Sqlite(pool.clone()));
.update_connection_pools(|connections| {
connections.insert("gauss-stream".to_string(), crate::connection::PoolKind::Sqlite(pool.clone()));
})
.await;
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);",
+23 -25
View File
@@ -519,8 +519,8 @@ enum TableExportCursorSession {
}
async fn table_export_cursor_kind(state: &AppState, pool_key: &str) -> Option<TableExportCursorKind> {
let connections = state.connections.read().await;
match connections.get(pool_key) {
let pool_handle = state.pool_handle(pool_key).await;
match pool_handle.as_ref() {
Some(PoolKind::Agent(_)) => Some(TableExportCursorKind::Agent),
Some(PoolKind::ExternalDriver { .. }) => Some(TableExportCursorKind::ExternalDriver),
_ => None,
@@ -677,12 +677,11 @@ async fn fetch_table_export_batch(
fetch_size: Some(active_batch_size),
timeout_secs: (query_timeout > 0).then_some(query_timeout),
};
let connections = state.connections.read().await;
let Some(PoolKind::Agent(client)) = connections.get(pool_key) else {
let pool_handle = state.pool_handle(pool_key).await;
let Some(PoolKind::Agent(client)) = pool_handle.as_ref() else {
return Err("Agent table read requires an agent connection".to_string());
};
let client = client.clone();
drop(connections);
let mut client = client.lock().await;
match client.start_table_read::<QueryResult>(params).await {
Ok(result) => {
@@ -731,12 +730,11 @@ async fn fetch_table_export_batch(
if let Some(session) = cursor_session.clone() {
return match session {
TableExportCursorSession::Agent(session_id) => {
let connections = state.connections.read().await;
let Some(PoolKind::Agent(client)) = connections.get(pool_key) else {
let pool_handle = state.pool_handle(pool_key).await;
let Some(PoolKind::Agent(client)) = pool_handle.as_ref() else {
return Err("Table read session requires an agent connection".to_string());
};
let client = client.clone();
drop(connections);
let mut client = client.lock().await;
match client.fetch_table_read_page::<QueryResult>(&session_id, active_batch_size).await {
Ok(result) => {
@@ -848,12 +846,11 @@ async fn close_table_export_cursor_if_open(
};
match session {
TableExportCursorSession::Agent(session_id) => {
let connections = state.connections.read().await;
let Some(PoolKind::Agent(client)) = connections.get(pool_key) else {
let pool_handle = state.pool_handle(pool_key).await;
let Some(PoolKind::Agent(client)) = pool_handle.as_ref() else {
return;
};
let client = client.clone();
drop(connections);
let mut client = client.lock().await;
let _ = client.close_table_read_session::<bool>(&session_id).await;
}
@@ -893,12 +890,11 @@ async fn stream_native_table_rows(
cancel_token: CancellationToken,
on_row: impl FnMut(&[Value]) -> Result<(), String>,
) -> Result<bool, String> {
let connections = state.connections.read().await;
match connections.get(pool_key) {
let pool_handle = state.pool_handle(pool_key).await;
match pool_handle.as_ref() {
Some(PoolKind::Mysql(pool, mode)) => {
let pool = pool.clone();
let bare = *mode == MysqlMode::Bare;
drop(connections);
crate::db::mysql::stream_query_rows(
&pool,
sql,
@@ -913,13 +909,11 @@ async fn stream_native_table_rows(
}
Some(PoolKind::Postgres(pool)) => {
let pool = pool.clone();
drop(connections);
crate::db::postgres::stream_query_rows(&pool, sql, row_limit, cancelled, on_row).await?;
Ok(true)
}
Some(PoolKind::SqlServer(client)) => {
let client = client.clone();
drop(connections);
let mut on_row = on_row;
let mut client = client.lock().await;
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 pool_key =
format!("{}:session:{}", config.id, table_export_client_session_id(&export_id).replace(':', "_"));
state.connections.write().await.insert(
pool_key,
PoolKind::ExternalDriver { driver_id: "jdbc".to_string(), config: Arc::new(config), session },
);
state
.update_connection_pools(|connections| {
connections.insert(
pool_key,
PoolKind::ExternalDriver { driver_id: "jdbc".to_string(), config: Arc::new(config), session },
);
})
.await;
let output = dir.join("export.csv");
let request = TableExportRequest {
@@ -2942,7 +2940,7 @@ mod tests {
);
assert_eq!(progress.last().and_then(|event| event.total_rows), Some(3));
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);
}
@@ -2999,7 +2997,7 @@ mod tests {
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!(fixture.state.connections.read().await.is_empty());
assert!(fixture.state.with_connection_pools(|pools| pools.is_empty()).await);
cleanup_external_driver_export_fixture(fixture);
}
@@ -3052,7 +3050,7 @@ mod tests {
std::fs::read_to_string(&fixture.calls).unwrap(),
"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);
}
@@ -3087,7 +3085,7 @@ mod tests {
assert!(cancel_requested_at.elapsed() < Duration::from_secs(2));
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;
cleanup_external_driver_export_fixture(fixture);
}
@@ -3126,7 +3124,7 @@ mod tests {
assert!(cancel_requested_at.elapsed() < Duration::from_secs(2));
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;
cleanup_external_driver_export_fixture(fixture);
}
+46 -18
View File
@@ -4541,8 +4541,8 @@ async fn execute_postgres_copy_batch(
statement_count: &mut usize,
) -> Result<(), String> {
let pool = {
let connections = state.connections.read().await;
match connections.get(pool_key) {
let pool_handle = state.pool_handle(pool_key).await;
match pool_handle.as_ref() {
Some(PoolKind::Postgres(pool)) => pool.clone(),
_ => 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;
}
let client = {
let connections = state.connections.read().await;
match connections.get(pool_key) {
let pool_handle = state.pool_handle(pool_key).await;
match pool_handle.as_ref() {
Some(PoolKind::Agent(client)) => client.clone(),
_ => 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> {
let pool = {
let connections = state.connections.read().await;
match connections.get(pool_key) {
let pool_handle = state.pool_handle(pool_key).await;
match pool_handle.as_ref() {
Some(PoolKind::Mysql(pool, _)) => pool.clone(),
_ => return None,
}
@@ -5480,8 +5480,8 @@ async fn sqlserver_bulk_import_plan_for_pool(
}
let import_plan = import_plan?;
let client = {
let connections = state.connections.read().await;
match connections.get(pool_key) {
let pool_handle = state.pool_handle(pool_key).await;
match pool_handle.as_ref() {
Some(PoolKind::SqlServer(client)) => client.clone(),
_ => return None,
}
@@ -5722,8 +5722,8 @@ async fn execute_sqlserver_bulk_rows_batch(
.await
.map_err(ImportRowsBatchError::before_write)?;
let client = {
let connections = state.connections.read().await;
match connections.get(pool_key) {
let pool_handle = state.pool_handle(pool_key).await;
match pool_handle.as_ref() {
Some(PoolKind::SqlServer(client)) => client.clone(),
_ => {
return Err(ImportRowsBatchError::before_write(
@@ -8735,7 +8735,11 @@ mod tests {
)
.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!({
"id": connection_id,
"name": "XLSX truncate tail test",
@@ -8823,7 +8827,11 @@ mod tests {
)
.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!({
"id": connection_id,
"name": "Cancel XLSX validation test",
@@ -8907,7 +8915,11 @@ mod tests {
)
.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!({
"id": connection_id,
"name": "Cancel truncate first batch test",
@@ -10475,7 +10487,11 @@ mod tests {
let pool_key = "sqlserver-cleanup-failure";
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();
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(
&state,
@@ -10486,7 +10502,7 @@ mod tests {
)
.await;
assert!(!state.connections.read().await.contains_key(pool_key));
assert!(!state.pool_handle(pool_key).await.is_some());
}
#[test]
@@ -10520,7 +10536,11 @@ mod tests {
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();
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 =
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 =
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();
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 {
_dir: dir,
state,
@@ -10848,7 +10872,11 @@ mod tests {
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();
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!({
"id": connection_id,
"name": "SQLite delimited append test",
@@ -85,8 +85,8 @@ async fn native_sqlite_pool(
) -> Result<(String, db::sqlite::SqliteHandle), String> {
let database = (!database.trim().is_empty()).then_some(database);
let pool_key = state.get_or_create_pool(connection_id, database).await?;
let connections = state.connections.read().await;
match connections.get(&pool_key) {
let pool_handle = state.pool_handle(&pool_key).await;
match pool_handle.as_ref() {
Some(PoolKind::Sqlite(pool)) => Ok((pool_key, pool.clone())),
Some(_) => Err("SQLite table rebuild is only available for native SQLite connections.".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> {
// Read-only check: block transfer operations in readonly mode
crate::query::check_read_only_for_connection(state, pool_key, sql).await?;
let connections = state.connections.read().await;
let pool = connections.get(pool_key).ok_or("Connection not found")?;
let pool_handle = state.pool_handle(pool_key).await;
let pool = pool_handle.as_ref().ok_or("Connection not found")?;
match pool {
PoolKind::Mysql(p, mode) => {
let p = p.clone();
let bare = *mode == crate::connection::MysqlMode::Bare;
drop(connections);
db::mysql::execute_query_with_max_rows(&p, sql, bare, max_rows, Default::default()).await
}
PoolKind::Postgres(p) => {
let p = p.clone();
drop(connections);
db::postgres::execute_query_with_max_rows(&p, sql, max_rows).await
}
PoolKind::Sqlite(p) => {
let p = p.clone();
drop(connections);
db::sqlite::execute_query_with_max_rows(&p, sql, max_rows).await
}
PoolKind::ClickHouse(client) => {
let client = client.clone();
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
}
PoolKind::SqlServer(client) => {
let client = client.clone();
drop(connections);
let mut client = client.lock().await;
let result = db::sqlserver::execute_query_with_max_rows(&mut client, sql, max_rows).await;
drop(client);
@@ -5132,7 +5127,6 @@ async fn execute_on_pool_once(
let client = client.clone();
let database = database_from_pool_key(pool_key).map(str::to_string);
let sql = sql.to_string();
drop(connections);
let mut client = client.lock().await;
let params = agent_execute_query_params(
&sql,
@@ -5159,7 +5153,6 @@ async fn execute_on_pool_once(
PoolKind::DuckDbWorker(client) => {
let client = client.clone();
let sql = sql.to_string();
drop(connections);
client.execute(None, sql, max_rows, None, None).await
}
_ => Err("Unsupported database type for transfer".to_string()),
@@ -5223,63 +5216,56 @@ pub async fn get_columns_for_transfer(
table: &str,
catalog: Option<&str>,
) -> Result<Vec<db::ColumnInfo>, String> {
let connections = state.connections.read().await;
let pool_handle = state.pool_handle(pool_key).await;
#[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 database = database.to_string();
let schema = schema.to_string();
let table = table.to_string();
drop(connections);
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 database = database.to_string();
let table = table.to_string();
drop(connections);
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 schema = schema.to_string();
let table = table.to_string();
drop(connections);
let mut client = client.lock().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 database = database.to_string();
let table = table.to_string();
drop(connections);
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 database = database.to_string();
let table = table.to_string();
drop(connections);
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 database = database.to_string();
let schema = schema.to_string();
let table = table.to_string();
drop(connections);
let mut client = client.lock().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 session = session.clone();
let database = database.to_string();
let schema = schema.to_string();
let table = table.to_string();
drop(connections);
return session
.invoke_with_timeout(
"getColumns",
@@ -5293,14 +5279,13 @@ pub async fn get_columns_for_transfer(
)
.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 table = table.to_string();
match pool {
PoolKind::Mysql(p, _) => {
let p = p.clone();
let catalog = normalize_external_catalog_name(catalog).map(str::to_string);
drop(connections);
if let Some(catalog) = catalog {
// Use 3-part qualified column lookup for Doris/StarRocks external catalogs
db::doris::get_catalog_columns(&p, &catalog, &schema, &table).await
@@ -5310,12 +5295,10 @@ pub async fn get_columns_for_transfer(
}
PoolKind::Postgres(p) => {
let p = p.clone();
drop(connections);
db::postgres::get_columns(&p, &schema, &table).await
}
PoolKind::Sqlite(p) => {
let p = p.clone();
drop(connections);
db::sqlite::get_columns(&p, &schema, &table).await
}
_ => Err("Unsupported database type".to_string()),
@@ -5329,21 +5312,19 @@ async fn get_postgres_indexes_for_transfer(
schema: &str,
table: &str,
) -> Result<Vec<db::IndexInfo>, String> {
let connections = state.connections.read().await;
if let Some(PoolKind::Agent(client)) = connections.get(pool_key) {
let pool_handle = state.pool_handle(pool_key).await;
if let Some(PoolKind::Agent(client)) = pool_handle.as_ref() {
let client = client.clone();
let database = database.to_string();
let schema = schema.to_string();
let table = table.to_string();
drop(connections);
let mut client = client.lock().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());
};
let pool = pool.clone();
drop(connections);
db::postgres::list_indexes(&pool, schema, table).await
}
@@ -5354,21 +5335,19 @@ async fn get_postgres_foreign_keys_for_transfer(
schema: &str,
table: &str,
) -> Result<Vec<db::ForeignKeyInfo>, String> {
let connections = state.connections.read().await;
if let Some(PoolKind::Agent(client)) = connections.get(pool_key) {
let pool_handle = state.pool_handle(pool_key).await;
if let Some(PoolKind::Agent(client)) = pool_handle.as_ref() {
let client = client.clone();
let database = database.to_string();
let schema = schema.to_string();
let table = table.to_string();
drop(connections);
let mut client = client.lock().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());
};
let pool = pool.clone();
drop(connections);
db::postgres::list_foreign_keys(&pool, schema, table).await
}
@@ -5383,8 +5362,8 @@ async fn get_postgres_owned_sequences_for_transfer(
}
let pool = {
let connections = state.connections.read().await;
match connections.get(pool_key) {
let pool_handle = state.pool_handle(pool_key).await;
match pool_handle.as_ref() {
Some(PoolKind::Postgres(pool)) => pool.clone(),
_ => return Ok(Vec::new()),
}
@@ -5430,8 +5409,8 @@ async fn get_postgres_sequence_snapshots_for_transfer(
schema: &str,
) -> Result<Vec<PostgresSequenceSnapshot>, String> {
let pool = {
let connections = state.connections.read().await;
match connections.get(pool_key) {
let pool_handle = state.pool_handle(pool_key).await;
match pool_handle.as_ref() {
Some(PoolKind::Postgres(pool)) => pool.clone(),
_ => 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 pool_key = state.get_or_create_pool(connection_id, Some(database)).await?;
{
let connections = state.connections.read().await;
native_postgres_dependency_pool(connections.get(&pool_key))
let pool_handle = state.pool_handle(&pool_key).await;
native_postgres_dependency_pool(pool_handle.as_ref())
}
} else {
None
@@ -7184,12 +7163,11 @@ async fn fetch_hive_server_transfer_batch(
let configs = state.configs.read().await;
configs.get(&request.source_connection_id).map(|config| config.query_timeout_secs).unwrap_or(0)
};
let connections = state.connections.read().await;
let Some(PoolKind::Agent(client)) = connections.get(pool_key) else {
let pool_handle = state.pool_handle(pool_key).await;
let Some(PoolKind::Agent(client)) = pool_handle.as_ref() else {
return Err("Impala transfer requires an Agent connection".to_string());
};
let client = client.clone();
drop(connections);
let mut client = client.lock().await;
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 {
return;
};
let connections = state.connections.read().await;
let Some(PoolKind::Agent(client)) = connections.get(pool_key) else {
let pool_handle = state.pool_handle(pool_key).await;
let Some(PoolKind::Agent(client)) = pool_handle.as_ref() else {
return;
};
let client = client.clone();
drop(connections);
let mut client = client.lock().await;
if let Err(error) = client.close_table_read_session::<bool>(&session_id).await {
log::warn!("[transfer] failed to close Impala transfer cursor: {error}");
@@ -7433,10 +7410,11 @@ where
// SHOW CREATE TABLE catalog.database.table using the
// existing source pool (bare MySQL — addresses any catalog).
let pool = {
let connections = state.connections.read().await;
let pool =
connections.get(source_pool_key).ok_or_else(|| "Source pool not found".to_string())?;
let PoolKind::Mysql(p, _) = pool else {
let pool = state
.pool_handle(source_pool_key)
.await
.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());
};
p.clone()
@@ -8637,10 +8615,14 @@ mod tests {
#[tokio::test]
async fn postgres_transfer_metadata_routes_agent_pools() {
let (state, dir) = test_app_state().await;
state.connections.write().await.insert(
"source:source_db".to_string(),
PoolKind::agent(crate::db::agent_driver::AgentDriverClient::test_stub()),
);
state
.update_connection_pools(|connections| {
connections.insert(
"source:source_db".to_string(),
PoolKind::agent(crate::db::agent_driver::AgentDriverClient::test_stub()),
);
})
.await;
let index_error =
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::time::Duration;
use dbx_core::agent_events::{ToolCall, ToolResult};
use dbx_core::agent_tools::{execute_tool, AgentSqlPermissions};
use dbx_core::connection::{AppState, PoolKind};
use dbx_core::models::connection::{ConnectionConfig, DatabaseType};
use dbx_core::mongo_ops::mongo_create_index_core;
use dbx_core::storage::Storage;
use mongodb::bson::{doc, Bson};
use mongodb::event::{command::CommandEvent, EventHandler};
use mongodb::options::ClientOptions;
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();
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 =
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.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 source_pool_key = format!("{source_connection_id}:{source_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.connections.write().await.insert(target_pool_key.clone(), PoolKind::Postgres(target_pool.clone()));
state
.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
.configs
.write()
@@ -363,8 +367,12 @@ async fn live_postgres_structure_only_preserves_table_indexes() {
let target_connection_id = "live-structure-only-target";
let source_pool_key = format!("{source_connection_id}:{source_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.connections.write().await.insert(target_pool_key.clone(), PoolKind::Postgres(target_pool.clone()));
state
.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
.configs
.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 target_pool_key = format!("{target_connection_id}:{target_database}");
state.connections.write().await.insert(source_pool_key.clone(), PoolKind::Postgres(source_pool.clone()));
state.connections.write().await.insert(target_pool_key.clone(), PoolKind::Postgres(target_pool.clone()));
state
.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
.configs
.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 target_pool_key = format!("{target_connection_id}:{target_database}");
state.connections.write().await.insert(source_pool_key.clone(), PoolKind::Postgres(source_pool.clone()));
state.connections.write().await.insert(target_pool_key.clone(), PoolKind::Postgres(target_pool.clone()));
state
.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
.configs
.write()
@@ -1013,8 +1029,12 @@ async fn live_postgres_transfer_creates_selected_sequence_before_referencing_tab
let target_connection_id = "live-sequence-target";
let source_pool_key = format!("{source_connection_id}:{source_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.connections.write().await.insert(target_pool_key.clone(), PoolKind::Postgres(target_pool.clone()));
state
.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
.configs
.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 pool_key = format!("{connection_id}:{database}");
state
.connections
.write()
.await
.insert(pool_key, PoolKind::SqlServer(std::sync::Arc::new(tokio::sync::Mutex::new(export_client))));
.update_connection_pools(|connections| {
connections
.insert(pool_key, PoolKind::SqlServer(std::sync::Arc::new(tokio::sync::Mutex::new(export_client))));
})
.await;
let file_path = dir.join("result.csv");
let sql = format!(
@@ -1258,10 +1259,14 @@ async fn live_sqlserver_sql_file_import_executes_go_batches() {
config.username = user;
config.password = password;
state.configs.write().await.insert(connection_id.to_string(), config);
state.connections.write().await.insert(
format!("{connection_id}:{database}"),
PoolKind::SqlServer(std::sync::Arc::new(tokio::sync::Mutex::new(client))),
);
state
.update_connection_pools(|connections| {
connections.insert(
format!("{connection_id}:{database}"),
PoolKind::SqlServer(std::sync::Arc::new(tokio::sync::Mutex::new(client))),
);
})
.await;
let script = format!(
"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");
let pool_key = format!("{connection_id}:{database}");
let connections = state.connections.read().await;
let PoolKind::SqlServer(client) = connections.get(&pool_key).expect("SQL Server pool") else {
let PoolKind::SqlServer(client) = state.pool_handle(&pool_key).await.expect("SQL Server pool") else {
panic!("expected SQL Server pool");
};
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 _ = dbx_core::db::sqlserver::execute_batch(&mut client, &cleanup).await;
drop(client);
drop(connections);
let _ = std::fs::remove_dir_all(&dir);
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 pool_key = format!("{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 sql = "WITH numbers AS (\