fix(jdbc): bind PostgreSQL pools to the requested database
Route generic PostgreSQL JDBC connections through a target-specific URL and pool key. Preserve URL options and driver credentials, isolate metadata roles and session cleanup, and keep existing connections alive when target selection fails. Closes #7840
This commit is contained in:
1 parent
012915da2c
commit
c3522b0879
4 files changed
+577
-39
No files matched your search
@@ -1478,14 +1478,13 @@ impl AppState {
|
||||
}?;
|
||||
let pool_database = metadata_pool_database(Some(&config), database);
|
||||
let mut base_pool_keys =
|
||||
vec![base_pool_key_for_with_catalog(Some(config.db_type), connection_id, pool_database, None, false)];
|
||||
vec![base_pool_key_for_config(Some(&config), connection_id, pool_database, None, false)];
|
||||
// MongoDB document operations use a connection-level pool and send the
|
||||
// requested database in the command. A host connection can therefore
|
||||
// legitimately be registered either under the selected database or
|
||||
// under the connection-level key; probe both without creating either.
|
||||
if config.db_type == DatabaseType::MongoDb {
|
||||
let connection_pool_key =
|
||||
base_pool_key_for_with_catalog(Some(config.db_type), connection_id, None, None, false);
|
||||
let connection_pool_key = base_pool_key_for_config(Some(&config), connection_id, None, None, false);
|
||||
if !base_pool_keys.contains(&connection_pool_key) {
|
||||
base_pool_keys.push(connection_pool_key);
|
||||
}
|
||||
@@ -2657,11 +2656,10 @@ impl AppState {
|
||||
configs.get(connection_id).ok_or("Connection config not found")?.clone()
|
||||
};
|
||||
validate_connection_url_params(&config)?;
|
||||
let db_type = Some(config.db_type);
|
||||
let validate_existing_pool = should_validate_existing_pool_before_reuse(config.db_type);
|
||||
let catalog = catalog.map(str::trim).filter(|value| !value.is_empty());
|
||||
|
||||
let base_pool_key = base_pool_key_for_with_catalog(db_type, connection_id, database, catalog, false);
|
||||
let base_pool_key = base_pool_key_for_config(Some(&config), connection_id, database, catalog, false);
|
||||
let pool_key = pool_key_for_session_role(Some(&config), base_pool_key.clone(), client_session_id, session_role);
|
||||
|
||||
loop {
|
||||
@@ -4678,14 +4676,13 @@ impl AppState {
|
||||
let configs = self.configs.read().await;
|
||||
configs.get(connection_id).cloned()
|
||||
};
|
||||
let db_type = config.as_ref().map(|config| config.db_type);
|
||||
let catalog = catalog.map(str::trim).filter(|value| !value.is_empty());
|
||||
let pool_database = if session_role == AgentSessionRole::Metadata {
|
||||
metadata_pool_database(config.as_ref(), database)
|
||||
} else {
|
||||
database
|
||||
};
|
||||
let base_pool_key = base_pool_key_for_with_catalog(db_type, connection_id, pool_database, catalog, true);
|
||||
let base_pool_key = base_pool_key_for_config(config.as_ref(), connection_id, pool_database, catalog, true);
|
||||
let pool_key = pool_key_for_session_role(config.as_ref(), base_pool_key, client_session_id, session_role);
|
||||
if self.uses_forwarded_transport(connection_id).await {
|
||||
self.remove_connection_pools(connection_id).await;
|
||||
@@ -4788,13 +4785,12 @@ impl AppState {
|
||||
let configs = self.configs.read().await;
|
||||
configs.get(connection_id).cloned()
|
||||
};
|
||||
let db_type = config.as_ref().map(|config| config.db_type);
|
||||
let pool_database = if session_role == AgentSessionRole::Metadata {
|
||||
metadata_pool_database(config.as_ref(), database)
|
||||
} else {
|
||||
database
|
||||
};
|
||||
let base_pool_key = base_pool_key_for(db_type, connection_id, pool_database, false);
|
||||
let base_pool_key = base_pool_key_for_config(config.as_ref(), connection_id, pool_database, None, false);
|
||||
let pool_key =
|
||||
pool_key_for_session_role(config.as_ref(), base_pool_key.clone(), Some(client_session_id), session_role);
|
||||
if pool_key == base_pool_key {
|
||||
@@ -4832,9 +4828,8 @@ impl AppState {
|
||||
let configs = self.configs.read().await;
|
||||
configs.get(connection_id).cloned()
|
||||
};
|
||||
let db_type = config.as_ref().map(|config| config.db_type);
|
||||
let pool_database = metadata_pool_database(config.as_ref(), database);
|
||||
let base_pool_key = base_pool_key_for(db_type, connection_id, pool_database, false);
|
||||
let base_pool_key = base_pool_key_for_config(config.as_ref(), connection_id, pool_database, None, false);
|
||||
let pool_key =
|
||||
pool_key_for_session_role(config.as_ref(), base_pool_key, client_session_id, AgentSessionRole::Metadata);
|
||||
self.detach_pool_by_key(&pool_key, true).await
|
||||
@@ -4852,9 +4847,8 @@ impl AppState {
|
||||
let configs = self.configs.read().await;
|
||||
configs.get(connection_id).cloned()
|
||||
};
|
||||
let db_type = config.as_ref().map(|config| config.db_type);
|
||||
let pool_database = metadata_pool_database(config.as_ref(), database);
|
||||
let base_pool_key = base_pool_key_for(db_type, connection_id, pool_database, false);
|
||||
let base_pool_key = base_pool_key_for_config(config.as_ref(), connection_id, pool_database, None, false);
|
||||
let pool_key =
|
||||
pool_key_for_session_role(config.as_ref(), base_pool_key, client_session_id, AgentSessionRole::Metadata);
|
||||
if let Some(session_id) = agent_session_id {
|
||||
@@ -4903,8 +4897,7 @@ impl AppState {
|
||||
let configs = self.configs.read().await;
|
||||
configs.get(connection_id).cloned()
|
||||
};
|
||||
let db_type = config.as_ref().map(|config| config.db_type);
|
||||
let base_pool_key = base_pool_key_for(db_type, connection_id, database, false);
|
||||
let base_pool_key = base_pool_key_for_config(config.as_ref(), connection_id, database, None, false);
|
||||
let pool_key = pool_key_for_session_role(config.as_ref(), base_pool_key.clone(), Some(&session), session_role);
|
||||
if pool_key == base_pool_key {
|
||||
return Ok(None);
|
||||
@@ -5059,19 +5052,24 @@ impl AppState {
|
||||
}
|
||||
|
||||
pub async fn close_database_pool(&self, connection_id: &str, database: Option<&str>) -> Result<bool, String> {
|
||||
let (db_type, default_database) = {
|
||||
let config = {
|
||||
let configs = self.configs.read().await;
|
||||
configs
|
||||
.get(connection_id)
|
||||
.map_or((None, None), |config| (Some(config.db_type), config.effective_database().map(str::to_string)))
|
||||
configs.get(connection_id).cloned()
|
||||
};
|
||||
let db_type = config.as_ref().map(|config| config.db_type);
|
||||
let default_database = config.as_ref().and_then(|config| config.effective_database());
|
||||
if database.is_some() && db_type.is_some_and(|db_type| shares_database_pool_with_connection(&db_type)) {
|
||||
return Ok(false);
|
||||
}
|
||||
let target_database = database.map(str::trim).filter(|database| !database.is_empty());
|
||||
let mut base_pool_keys = vec![base_pool_key_for(db_type, connection_id, target_database, false)];
|
||||
if target_database.is_some() && target_database == default_database.as_deref() {
|
||||
let connection_pool_key = base_pool_key_for(db_type, connection_id, None, false);
|
||||
let target_database = if config.as_ref().is_some_and(is_postgres_jdbc) {
|
||||
database.filter(|database| !database.trim().is_empty())
|
||||
} else {
|
||||
database.map(str::trim).filter(|database| !database.is_empty())
|
||||
};
|
||||
let mut base_pool_keys =
|
||||
vec![base_pool_key_for_config(config.as_ref(), connection_id, target_database, None, false)];
|
||||
if target_database.is_some() && target_database == default_database {
|
||||
let connection_pool_key = base_pool_key_for_config(config.as_ref(), connection_id, None, None, false);
|
||||
if !base_pool_keys.contains(&connection_pool_key) {
|
||||
base_pool_keys.push(connection_pool_key);
|
||||
}
|
||||
@@ -6473,9 +6471,17 @@ fn is_embedded_h2_jdbc(config: &ConnectionConfig) -> bool {
|
||||
&& !location.is_empty()
|
||||
}
|
||||
|
||||
fn is_postgres_jdbc(config: &ConnectionConfig) -> bool {
|
||||
config.db_type == DatabaseType::Jdbc
|
||||
&& config.connection_string.as_deref().is_some_and(|url| url.trim().starts_with("jdbc:postgresql:"))
|
||||
}
|
||||
|
||||
#[cfg(all(test, unix))]
|
||||
mod h2_jdbc_tests;
|
||||
|
||||
#[cfg(all(test, unix))]
|
||||
mod postgres_jdbc_tests;
|
||||
|
||||
pub fn task_client_session_id(task_kind: &str, task_id: &str) -> String {
|
||||
format!("{task_kind}:{task_id}")
|
||||
}
|
||||
@@ -6668,7 +6674,9 @@ fn pool_key_for_session_role(
|
||||
let pool_key = session_scoped_pool_key_for(config, base_pool_key, client_session_id);
|
||||
if session_role == AgentSessionRole::Metadata
|
||||
&& config.is_some_and(|config| {
|
||||
database_capabilities::is_agent_type(&config.db_type) || sqlserver_uses_legacy_driver(config)
|
||||
database_capabilities::is_agent_type(&config.db_type)
|
||||
|| sqlserver_uses_legacy_driver(config)
|
||||
|| is_postgres_jdbc(config)
|
||||
})
|
||||
{
|
||||
// The legacy SQL Server Agent borrows one connection-level pool for metadata across
|
||||
@@ -6841,6 +6849,35 @@ fn extract_auth_token_from_params(params: &str) -> Option<String> {
|
||||
.map(|(_, value)| value.trim().to_string())
|
||||
}
|
||||
|
||||
fn base_pool_key_for_config(
|
||||
config: Option<&ConnectionConfig>,
|
||||
connection_id: &str,
|
||||
database: Option<&str>,
|
||||
catalog: Option<&str>,
|
||||
include_elasticsearch_single_pool: bool,
|
||||
) -> String {
|
||||
if config.is_some_and(is_postgres_jdbc) {
|
||||
let key = match database.filter(|database| !database.trim().is_empty()) {
|
||||
Some(database) => format!(
|
||||
"{connection_id}:database:{}",
|
||||
percent_encoding::utf8_percent_encode(database, percent_encoding::NON_ALPHANUMERIC)
|
||||
),
|
||||
None => connection_id.to_string(),
|
||||
};
|
||||
return match catalog.map(str::trim).filter(|catalog| !catalog.is_empty()) {
|
||||
Some(catalog) => format!("{key}:catalog:{catalog}"),
|
||||
None => key,
|
||||
};
|
||||
}
|
||||
base_pool_key_for_with_catalog(
|
||||
config.map(|config| config.db_type),
|
||||
connection_id,
|
||||
database,
|
||||
catalog,
|
||||
include_elasticsearch_single_pool,
|
||||
)
|
||||
}
|
||||
|
||||
fn base_pool_key_for(
|
||||
db_type: Option<DatabaseType>,
|
||||
connection_id: &str,
|
||||
|
||||
@@ -0,0 +1,158 @@
|
||||
use super::*;
|
||||
|
||||
fn config(url: &str) -> ConnectionConfig {
|
||||
let mut config = super::tests::mysql_config(None);
|
||||
config.db_type = DatabaseType::Jdbc;
|
||||
config.connection_string = Some(url.into());
|
||||
config.jdbc_driver_class = Some("org.postgresql.Driver".into());
|
||||
config
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn postgres_jdbc_pool_keys_preserve_database_role_and_session_identity() {
|
||||
let config = config("jdbc:postgresql://localhost/a?sslmode=require");
|
||||
let key = |database, session, role| {
|
||||
pool_key_for_session_role(
|
||||
Some(&config),
|
||||
base_pool_key_for_config(Some(&config), "conn", database, None, false),
|
||||
session,
|
||||
role,
|
||||
)
|
||||
};
|
||||
let mut keys = std::collections::HashSet::new();
|
||||
for database in [None, Some("a"), Some("b"), Some(" a"), Some("a "), Some("a:session:x")] {
|
||||
for session in [None, Some("tab-a"), Some("tab-b")] {
|
||||
for role in [AgentSessionRole::Metadata, AgentSessionRole::Workload] {
|
||||
assert!(keys.insert(key(database, session, role)));
|
||||
}
|
||||
}
|
||||
}
|
||||
assert_eq!(key(Some("a:session:x"), None, AgentSessionRole::Workload), "conn:database:a%3Asession%3Ax");
|
||||
assert_eq!(key(Some(""), None, AgentSessionRole::Workload), "conn");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn postgres_jdbc_routing_scope_keeps_other_vendor_pool_defaults() {
|
||||
for url in [
|
||||
"jdbc:h2:file:./sample",
|
||||
"jdbc:h2:tcp://localhost/sample",
|
||||
"jdbc:h2:mem:sample",
|
||||
"jdbc:hive2://localhost/default",
|
||||
"jdbc:kingbase8://localhost/a",
|
||||
"jdbc:postgresqlx://localhost/a",
|
||||
] {
|
||||
let config = config(url);
|
||||
assert!(!is_postgres_jdbc(&config));
|
||||
assert_eq!(base_pool_key_for_config(Some(&config), "conn", Some("b"), None, false), "conn");
|
||||
}
|
||||
let config = config("jdbc:postgresql://localhost/a");
|
||||
let target = database_connection_config(&config, Some(" b "));
|
||||
assert_eq!(target.database.as_deref(), Some(" b "));
|
||||
assert_eq!(target.connection_string, config.connection_string);
|
||||
assert_eq!(database_connection_config(&config, None).database, None);
|
||||
}
|
||||
|
||||
async fn fixture() -> (AppState, std::path::PathBuf) {
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
let (mut state, directory) = super::tests::test_app_state().await;
|
||||
let plugin = directory.join("plugins/jdbc");
|
||||
std::fs::create_dir_all(&plugin).unwrap();
|
||||
let script = plugin.join("driver.py");
|
||||
std::fs::write(
|
||||
&script,
|
||||
r#"#!/usr/bin/python3
|
||||
import json, os, sys
|
||||
database = None
|
||||
for line in sys.stdin:
|
||||
request = json.loads(line)
|
||||
method, params = request['method'], request.get('params', {})
|
||||
response = {'id': request['id']}
|
||||
result = {'pid': os.getpid(), 'database': database}
|
||||
if method == 'connect':
|
||||
database = params['connection'].get('database') or 'url_default'
|
||||
if database in ('missing', 'denied'):
|
||||
response['error'] = {'message': 'target database unavailable'}
|
||||
elif method == 'listSchemas':
|
||||
result = [database + '_only', 'public']
|
||||
response.setdefault('result', result)
|
||||
print(json.dumps(response), flush=True)
|
||||
if method == 'close':
|
||||
break
|
||||
"#,
|
||||
)
|
||||
.unwrap();
|
||||
std::fs::set_permissions(&script, std::fs::Permissions::from_mode(0o755)).unwrap();
|
||||
std::fs::write(
|
||||
plugin.join("manifest.json"),
|
||||
serde_json::json!({
|
||||
"id": "jdbc", "name": "JDBC", "version": "0.1.41", "protocol_version": 1,
|
||||
"executable": "driver.py", "drivers": [{"id": "jdbc", "label": "JDBC", "kind": "external", "database_type": "jdbc"}]
|
||||
}).to_string(),
|
||||
).unwrap();
|
||||
state.plugins = PluginRegistry::new_with_app_version(directory.join("plugins"), "0.6.30");
|
||||
state.configs.write().await.insert("conn".into(), config("jdbc:postgresql://localhost/a"));
|
||||
(state, directory)
|
||||
}
|
||||
|
||||
async fn identity(state: &AppState, key: &str) -> serde_json::Value {
|
||||
let PoolKind::ExternalDriver { session, .. } = state.pool_handle(key).await.unwrap() else { panic!() };
|
||||
session.invoke("identity", serde_json::json!({})).await.unwrap()
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn postgres_jdbc_owning_entries_route_metadata_cache_hits_and_failures() {
|
||||
let (state, directory) = fixture().await;
|
||||
let mut owners = HashMap::new();
|
||||
for database in ["a", "b", "a"] {
|
||||
assert_eq!(
|
||||
crate::schema::list_schemas_core(&state, "conn", database).await.unwrap(),
|
||||
vec![format!("{database}_only"), "public".into()]
|
||||
);
|
||||
let key = state.get_or_create_metadata_pool_for_session("conn", Some(database), None).await.unwrap();
|
||||
let current = identity(&state, &key).await;
|
||||
assert_eq!(current["database"], database);
|
||||
if let Some(original) = owners.insert(database, current.clone()) {
|
||||
assert_eq!(original, current);
|
||||
}
|
||||
assert_eq!(state.existing_metadata_pool_key_for_session("conn", Some(database), None).await, Some(key));
|
||||
}
|
||||
assert_ne!(owners["a"]["pid"], owners["b"]["pid"]);
|
||||
for database in ["missing", "denied"] {
|
||||
assert!(state.get_or_create_metadata_pool_for_session("conn", Some(database), None).await.is_err());
|
||||
assert_eq!(state.existing_metadata_pool_key_for_session("conn", Some(database), None).await, None);
|
||||
}
|
||||
let database_a_pool = state.get_or_create_metadata_pool_for_session("conn", Some("a"), None).await.unwrap();
|
||||
assert_eq!(identity(&state, &database_a_pool).await, owners["a"]);
|
||||
assert!(state.close_database_pool("conn", Some("b")).await.unwrap());
|
||||
assert_eq!(identity(&state, &database_a_pool).await, owners["a"]);
|
||||
assert_eq!(state.existing_metadata_pool_key_for_session("conn", Some("b"), None).await, None);
|
||||
state.shutdown(Duration::from_secs(5)).await;
|
||||
std::fs::remove_dir_all(directory).unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn postgres_jdbc_owning_entries_separate_concurrent_roles_sessions_and_cleanup() {
|
||||
let (state, directory) = fixture().await;
|
||||
let (metadata, workload, other_database, other_tab) = tokio::join!(
|
||||
state.get_or_create_metadata_pool_for_session("conn", Some("a"), Some("tab")),
|
||||
state.get_or_create_pool_for_session("conn", Some("a"), Some("tab")),
|
||||
state.get_or_create_pool_for_session("conn", Some("b"), Some("tab")),
|
||||
state.get_or_create_pool_for_session("conn", Some("a"), Some("other")),
|
||||
);
|
||||
let keys = [metadata.unwrap(), workload.unwrap(), other_database.unwrap(), other_tab.unwrap()];
|
||||
let mut pids = std::collections::HashSet::new();
|
||||
for key in &keys {
|
||||
assert!(pids.insert(identity(&state, key).await["pid"].as_i64().unwrap()));
|
||||
}
|
||||
assert!(state.close_metadata_session_pool("conn", Some("a"), "tab").await.unwrap());
|
||||
assert!(state.pool_handle(&keys[0]).await.is_none());
|
||||
assert!(state.detach_client_session_pool("conn", Some("a"), "tab").await.unwrap());
|
||||
assert!(state.pool_handle(&keys[1]).await.is_none());
|
||||
assert_eq!(identity(&state, &keys[2]).await["database"], "b");
|
||||
assert_eq!(identity(&state, &keys[3]).await["database"], "a");
|
||||
assert!(!state.detach_metadata_pool_after_recovery("conn", Some("b"), None, None, false).await);
|
||||
let default = state.get_or_create_pool("conn", None).await.unwrap();
|
||||
assert_eq!(identity(&state, &default).await["database"], "url_default");
|
||||
state.shutdown(Duration::from_secs(5)).await;
|
||||
std::fs::remove_dir_all(directory).unwrap();
|
||||
}
|
||||
Reference in new issue
Block a user