feat(gaussdb): support multi-host connections
This commit is contained in:
1 parent
c0a6d6f2f9
commit
336fbe094b
16 files changed
+358
-25
No files matched your search
@@ -4695,7 +4695,51 @@ pub async fn probe_connection_endpoint(config: &ConnectionConfig, host: &str, po
|
||||
return Ok(());
|
||||
}
|
||||
let timeout = std::time::Duration::from_secs(config.effective_connect_timeout_secs());
|
||||
db::probe_tcp_endpoint(&format!("{:?}", config.db_type), host, port, timeout).await
|
||||
|
||||
let entries = connection_probe_endpoints(host, port);
|
||||
|
||||
if entries.is_empty() {
|
||||
return Err("no host entries to probe".to_string());
|
||||
}
|
||||
|
||||
// Probe each node sequentially; return success on the first reachable node.
|
||||
// This matches the failover semantics of the real connection path.
|
||||
let mut last_error = String::new();
|
||||
for (entry_host, entry_port) in &entries {
|
||||
match db::probe_tcp_endpoint(&format!("{:?}", config.db_type), entry_host, *entry_port, timeout).await {
|
||||
Ok(()) => return Ok(()),
|
||||
Err(e) => last_error = e,
|
||||
}
|
||||
}
|
||||
Err(last_error)
|
||||
}
|
||||
|
||||
fn connection_probe_endpoints(host: &str, default_port: u16) -> Vec<(String, u16)> {
|
||||
host.split(',').filter_map(|part| parse_connection_probe_endpoint(part.trim(), default_port)).collect()
|
||||
}
|
||||
|
||||
fn parse_connection_probe_endpoint(endpoint: &str, default_port: u16) -> Option<(String, u16)> {
|
||||
if endpoint.is_empty() {
|
||||
return None;
|
||||
}
|
||||
if let Some(rest) = endpoint.strip_prefix('[') {
|
||||
let close = rest.find(']')?;
|
||||
let host = rest[..close].to_string();
|
||||
let port = rest
|
||||
.get(close + 1..)
|
||||
.and_then(|suffix| suffix.strip_prefix(':'))
|
||||
.and_then(|value| value.parse::<u16>().ok())
|
||||
.unwrap_or(default_port);
|
||||
return Some((host, port));
|
||||
}
|
||||
if endpoint.matches(':').count() == 1 {
|
||||
if let Some((host, raw_port)) = endpoint.rsplit_once(':') {
|
||||
if let Ok(port) = raw_port.parse::<u16>() {
|
||||
return Some((host.to_string(), port));
|
||||
}
|
||||
}
|
||||
}
|
||||
Some((endpoint.to_string(), default_port))
|
||||
}
|
||||
|
||||
fn validate_h2_file_connection(config: &ConnectionConfig) -> Result<(), String> {
|
||||
@@ -4782,16 +4826,16 @@ async fn detect_ob_oracle_mode(config: &ConnectionConfig, pool: &db::mysql::MySq
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{
|
||||
agent_connect_timeout, connection_remote_endpoint, connection_url_for_endpoint, database_connection_config,
|
||||
database_connection_config_with_catalog, gaussdb_identifier_quote_from_query_result,
|
||||
gaussdb_m_jdbc_config_for_endpoint, gaussdb_uses_m_jdbc_driver, metadata_connection_config,
|
||||
mysql_metadata_fallback_url, mysql_pool_setup_queries, oceanbase_mysql_query_timeout_sql,
|
||||
oceanbase_mysql_setup_queries, prestosql_jdbc_config_for_endpoint, redacted_connection_url_for_endpoint,
|
||||
redis_sentinel_transport_id, redis_sentinel_transport_prefix, sqlserver_legacy_agent_config,
|
||||
sqlserver_legacy_driver_error, sqlserver_uses_legacy_driver, task_client_session_id,
|
||||
upsert_connection_url_param, uses_bare_mysql_pool, uses_tcp_probe, validate_connection_url_params,
|
||||
validate_h2_database_path, AppState, MysqlMode, PoolKind, GAUSSDB_M_JDBC_DRIVER_CLASS,
|
||||
GAUSSDB_M_JDBC_DRIVER_PROFILE, PRESTOSQL_JDBC_DRIVER_CLASS,
|
||||
agent_connect_timeout, connection_probe_endpoints, connection_remote_endpoint, connection_url_for_endpoint,
|
||||
database_connection_config, database_connection_config_with_catalog,
|
||||
gaussdb_identifier_quote_from_query_result, gaussdb_m_jdbc_config_for_endpoint, gaussdb_uses_m_jdbc_driver,
|
||||
metadata_connection_config, mysql_metadata_fallback_url, mysql_pool_setup_queries,
|
||||
oceanbase_mysql_query_timeout_sql, oceanbase_mysql_setup_queries, prestosql_jdbc_config_for_endpoint,
|
||||
redacted_connection_url_for_endpoint, redis_sentinel_transport_id, redis_sentinel_transport_prefix,
|
||||
sqlserver_legacy_agent_config, sqlserver_legacy_driver_error, sqlserver_uses_legacy_driver,
|
||||
task_client_session_id, upsert_connection_url_param, uses_bare_mysql_pool, uses_tcp_probe,
|
||||
validate_connection_url_params, validate_h2_database_path, AppState, MysqlMode, PoolKind,
|
||||
GAUSSDB_M_JDBC_DRIVER_CLASS, GAUSSDB_M_JDBC_DRIVER_PROFILE, PRESTOSQL_JDBC_DRIVER_CLASS,
|
||||
};
|
||||
use crate::agent_connection::{
|
||||
agent_connect_params, mongo_legacy_error_with_auth_hint, mongo_uses_legacy_driver,
|
||||
@@ -5834,6 +5878,15 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gaussdb_probe_endpoints_parse_legacy_and_ipv6_hosts() {
|
||||
assert_eq!(connection_probe_endpoints("db.example.com:5433", 5432), vec![("db.example.com".to_string(), 5433)]);
|
||||
assert_eq!(
|
||||
connection_probe_endpoints("[2001:db8::1]:5433,[2001:db8::2]:5434", 5432),
|
||||
vec![("2001:db8::1".to_string(), 5433), ("2001:db8::2".to_string(), 5434)]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn kwdb_endpoint_url_uses_postgres_scheme_for_native_driver() {
|
||||
let mut config = mysql_config(None);
|
||||
|
||||
@@ -992,7 +992,11 @@ impl ConnectionConfig {
|
||||
}
|
||||
DatabaseType::Postgres | DatabaseType::Redshift => {
|
||||
let suffix = if params.is_empty() { String::new() } else { format!("?{params}") };
|
||||
format!("postgres://{host}:{port}{db_part}{suffix}")
|
||||
if is_multi_host(raw_host) {
|
||||
format!("postgres://{raw_host}{db_part}{suffix}")
|
||||
} else {
|
||||
format!("postgres://{host}:{port}{db_part}{suffix}")
|
||||
}
|
||||
}
|
||||
DatabaseType::ClickHouse => clickhouse_http_url(self, raw_host, port),
|
||||
DatabaseType::Rqlite => rqlite_http_url(self, raw_host, port),
|
||||
@@ -1038,7 +1042,14 @@ impl ConnectionConfig {
|
||||
DatabaseType::Uxdb => format!("uxdb://{host}:{port}{db_part}"),
|
||||
DatabaseType::Vastbase => format!("vastbase://{host}:{port}{db_part}"),
|
||||
DatabaseType::Goldendb => format!("goldendb://{host}:{port}{db_part}"),
|
||||
DatabaseType::Gaussdb => format!("gaussdb://{host}:{port}{db_part}"),
|
||||
DatabaseType::Gaussdb => {
|
||||
if is_multi_host(raw_host) {
|
||||
format!("gaussdb://{raw_host}{db_part}")
|
||||
} else {
|
||||
let (gaussdb_host, gaussdb_port) = gaussdb_single_host_port(raw_host, port);
|
||||
format!("gaussdb://{gaussdb_host}:{gaussdb_port}{db_part}")
|
||||
}
|
||||
}
|
||||
DatabaseType::Kwdb => format!("kwdb://{host}:{port}{db_part}"),
|
||||
DatabaseType::Yashandb => format!("yashandb://{host}:{port}{db_part}"),
|
||||
DatabaseType::Databricks => format!("databricks://{host}:{port}{db_part}"),
|
||||
@@ -1134,7 +1145,12 @@ impl ConnectionConfig {
|
||||
}
|
||||
DatabaseType::Postgres | DatabaseType::Redshift => {
|
||||
let suffix = if params.is_empty() { String::new() } else { format!("?{params}") };
|
||||
format!("postgres://{}:{}@{host}:{port}{db_part}{suffix}", username, password)
|
||||
if is_multi_host(raw_host) {
|
||||
// Multi-host: host1:port1,host2:port2 — each host already has its port embedded
|
||||
format!("postgres://{}:{}@{raw_host}{db_part}{suffix}", username, password)
|
||||
} else {
|
||||
format!("postgres://{}:{}@{host}:{port}{db_part}{suffix}", username, password)
|
||||
}
|
||||
}
|
||||
DatabaseType::ClickHouse => clickhouse_http_url(self, raw_host, port),
|
||||
DatabaseType::Rqlite => rqlite_http_url(self, raw_host, port),
|
||||
@@ -1202,7 +1218,13 @@ impl ConnectionConfig {
|
||||
format!("goldendb://{}:{}@{host}:{port}{db_part}", username, password)
|
||||
}
|
||||
DatabaseType::Gaussdb => {
|
||||
format!("gaussdb://{}:{}@{host}:{port}{db_part}", username, password)
|
||||
if is_multi_host(raw_host) {
|
||||
// Multi-host: host1:port1,host2:port2 — each host already has its port embedded
|
||||
format!("gaussdb://{}:{}@{raw_host}{db_part}", username, password)
|
||||
} else {
|
||||
let (gaussdb_host, gaussdb_port) = gaussdb_single_host_port(raw_host, port);
|
||||
format!("gaussdb://{}:{}@{gaussdb_host}:{gaussdb_port}{db_part}", username, password)
|
||||
}
|
||||
}
|
||||
DatabaseType::Kwdb => {
|
||||
format!("kwdb://{}:{}@{host}:{port}{db_part}", username, password)
|
||||
@@ -2167,6 +2189,39 @@ fn bracket_ipv6(host: &str) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns `true` when `host` contains two or more comma-separated entries
|
||||
/// where each entry already embeds its own `:port` suffix.
|
||||
///
|
||||
/// A single entry such as `db.example.com:5432` is **not** multi-host —
|
||||
/// the scalar `port` parameter must be appended separately.
|
||||
fn is_multi_host(host: &str) -> bool {
|
||||
let count = host.split(',').count();
|
||||
count >= 2
|
||||
}
|
||||
|
||||
fn gaussdb_single_host_port(host: &str, default_port: u16) -> (String, u16) {
|
||||
let host = host.trim();
|
||||
if let Some(close) = host.strip_prefix('[').and_then(|value| value.find(']').map(|index| index + 1)) {
|
||||
let bracketed_host = &host[..=close];
|
||||
if let Some(port) = host
|
||||
.get(close + 1..)
|
||||
.and_then(|suffix| suffix.strip_prefix(':'))
|
||||
.and_then(|value| value.parse::<u16>().ok())
|
||||
{
|
||||
return (bracketed_host.to_string(), port);
|
||||
}
|
||||
return (bracketed_host.to_string(), default_port);
|
||||
}
|
||||
if host.matches(':').count() == 1 {
|
||||
if let Some((single_host, raw_port)) = host.rsplit_once(':') {
|
||||
if let Ok(port) = raw_port.parse::<u16>() {
|
||||
return (bracket_ipv6(single_host), port);
|
||||
}
|
||||
}
|
||||
}
|
||||
(bracket_ipv6(host), default_port)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{
|
||||
@@ -3210,6 +3265,28 @@ mod tests {
|
||||
assert_eq!(config.connection_url(), "gaussdb://gaussdb:secret@10.1.2.3:2883/postgres");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gaussdb_url_normalizes_legacy_single_host_port() {
|
||||
let mut config = mysql_config("gaussdb", "secret", None);
|
||||
config.db_type = DatabaseType::Gaussdb;
|
||||
config.host = "db.example.com:5433".to_string();
|
||||
config.port = 5432;
|
||||
|
||||
assert_eq!(config.connection_url(), "gaussdb://gaussdb:secret@db.example.com:5433/postgres");
|
||||
assert_eq!(config.redacted_connection_url(), "gaussdb://db.example.com:5433/postgres");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn gaussdb_url_supports_single_and_multi_host_ipv6() {
|
||||
let mut config = mysql_config("gaussdb", "secret", None);
|
||||
config.db_type = DatabaseType::Gaussdb;
|
||||
config.host = "[2001:db8::1]:5433".to_string();
|
||||
assert_eq!(config.connection_url(), "gaussdb://gaussdb:secret@[2001:db8::1]:5433/postgres");
|
||||
|
||||
config.host = "[2001:db8::1]:5433,[2001:db8::2]:5434".to_string();
|
||||
assert_eq!(config.connection_url(), "gaussdb://gaussdb:secret@[2001:db8::1]:5433,[2001:db8::2]:5434/postgres");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn kwdb_url_defaults_to_defaultdb_database() {
|
||||
let mut config = mysql_config("root", "secret", None);
|
||||
|
||||
Reference in new issue
Block a user