Fix:同步 OceanBase 查询超时设置 (#2156)
This commit is contained in:
3 files changed
+179
-35
No files matched your search
@@ -55,6 +55,19 @@ pub enum MysqlMode {
|
||||
OceanBaseOracle,
|
||||
}
|
||||
|
||||
fn is_oceanbase_mysql_config(config: &ConnectionConfig) -> bool {
|
||||
config.db_type == DatabaseType::Mysql
|
||||
&& config.driver_profile.as_deref().is_some_and(|profile| profile.eq_ignore_ascii_case("oceanbase"))
|
||||
}
|
||||
|
||||
fn oceanbase_mysql_setup_queries(config: &ConnectionConfig) -> Vec<String> {
|
||||
if !is_oceanbase_mysql_config(config) || config.query_timeout_secs == 0 {
|
||||
return Vec::new();
|
||||
}
|
||||
let timeout_us = config.query_timeout_secs.saturating_mul(1_000_000);
|
||||
vec![format!("SET ob_query_timeout = {timeout_us}")]
|
||||
}
|
||||
|
||||
pub enum PoolKind {
|
||||
Mysql(db::mysql::MySqlPool, MysqlMode),
|
||||
Postgres(deadpool_postgres::Pool),
|
||||
@@ -217,8 +230,16 @@ pub async fn connect_mysql_metadata_pool(
|
||||
) -> Result<(db::mysql::MySqlPool, MysqlMode), String> {
|
||||
let url = connection_url_for_endpoint(db_config, host, port);
|
||||
let idle_timeout_secs = Some(db_config.idle_timeout_secs);
|
||||
let extra_setup_queries = oceanbase_mysql_setup_queries(db_config);
|
||||
if db_config.needs_bare_mysql() {
|
||||
return match db::mysql::connect_bare_with_pool_limit(&url, connect_timeout, max_connections).await {
|
||||
return match db::mysql::connect_bare_with_pool_limit_and_setup(
|
||||
&url,
|
||||
connect_timeout,
|
||||
max_connections,
|
||||
&extra_setup_queries,
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(pool) => Ok((pool, MysqlMode::Bare)),
|
||||
Err(err) => {
|
||||
let fallback_url = mysql_metadata_fallback_url(config, db_config, host, port);
|
||||
@@ -226,9 +247,14 @@ pub async fn connect_mysql_metadata_pool(
|
||||
log::info!(
|
||||
"MySQL metadata connection without a default database failed ({err}); retrying with configured default database."
|
||||
);
|
||||
db::mysql::connect_bare_with_pool_limit(&fallback_url, connect_timeout, max_connections)
|
||||
.await
|
||||
.map(|pool| (pool, MysqlMode::Bare))
|
||||
db::mysql::connect_bare_with_pool_limit_and_setup(
|
||||
&fallback_url,
|
||||
connect_timeout,
|
||||
max_connections,
|
||||
&extra_setup_queries,
|
||||
)
|
||||
.await
|
||||
.map(|pool| (pool, MysqlMode::Bare))
|
||||
} else {
|
||||
Err(err)
|
||||
}
|
||||
@@ -236,12 +262,13 @@ pub async fn connect_mysql_metadata_pool(
|
||||
};
|
||||
}
|
||||
|
||||
match db::mysql::connect_with_ca_cert_pool_limit_and_idle(
|
||||
match db::mysql::connect_with_ca_cert_pool_limit_idle_and_setup(
|
||||
&url,
|
||||
Some(&db_config.ca_cert_path),
|
||||
connect_timeout,
|
||||
max_connections,
|
||||
idle_timeout_secs,
|
||||
&extra_setup_queries,
|
||||
)
|
||||
.await
|
||||
{
|
||||
@@ -255,12 +282,13 @@ pub async fn connect_mysql_metadata_pool(
|
||||
log::info!(
|
||||
"MySQL metadata connection without a default database failed ({err}); retrying with configured default database."
|
||||
);
|
||||
let pool = db::mysql::connect_with_ca_cert_pool_limit_and_idle(
|
||||
let pool = db::mysql::connect_with_ca_cert_pool_limit_idle_and_setup(
|
||||
&fallback_url,
|
||||
Some(&config.ca_cert_path),
|
||||
connect_timeout,
|
||||
max_connections,
|
||||
idle_timeout_secs,
|
||||
&extra_setup_queries,
|
||||
)
|
||||
.await?;
|
||||
let mode = detect_ob_oracle_mode(config, &pool).await;
|
||||
@@ -280,19 +308,38 @@ pub async fn connect_bare_metadata_pool(
|
||||
max_connections: usize,
|
||||
) -> Result<db::mysql::MySqlPool, String> {
|
||||
let url = connection_url_for_endpoint(db_config, host, port);
|
||||
let extra_setup_queries = oceanbase_mysql_setup_queries(db_config);
|
||||
if db_config.effective_database().is_none() {
|
||||
return db::mysql::connect_bare_with_pool_limit(&url, connect_timeout, max_connections).await;
|
||||
return db::mysql::connect_bare_with_pool_limit_and_setup(
|
||||
&url,
|
||||
connect_timeout,
|
||||
max_connections,
|
||||
&extra_setup_queries,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
|
||||
let mut unscoped_config = db_config.clone();
|
||||
unscoped_config.database = None;
|
||||
let unscoped_url = connection_url_for_endpoint(&unscoped_config, host, port);
|
||||
if unscoped_url == url {
|
||||
return db::mysql::connect_bare_with_pool_limit(&url, connect_timeout, max_connections).await;
|
||||
return db::mysql::connect_bare_with_pool_limit_and_setup(
|
||||
&url,
|
||||
connect_timeout,
|
||||
max_connections,
|
||||
&extra_setup_queries,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
|
||||
let preferred = db::mysql::connect_bare_with_pool_limit(&url, connect_timeout, max_connections);
|
||||
let unscoped = db::mysql::connect_bare_with_pool_limit(&unscoped_url, connect_timeout, max_connections);
|
||||
let preferred =
|
||||
db::mysql::connect_bare_with_pool_limit_and_setup(&url, connect_timeout, max_connections, &extra_setup_queries);
|
||||
let unscoped = db::mysql::connect_bare_with_pool_limit_and_setup(
|
||||
&unscoped_url,
|
||||
connect_timeout,
|
||||
max_connections,
|
||||
&extra_setup_queries,
|
||||
);
|
||||
tokio::pin!(preferred);
|
||||
tokio::pin!(unscoped);
|
||||
|
||||
@@ -2352,9 +2399,9 @@ async fn detect_ob_oracle_mode(config: &ConnectionConfig, pool: &db::mysql::MySq
|
||||
mod tests {
|
||||
use super::{
|
||||
agent_connect_timeout, connection_remote_endpoint, connection_url_for_endpoint, database_connection_config,
|
||||
metadata_connection_config, mysql_metadata_fallback_url, prestosql_jdbc_config_for_endpoint,
|
||||
redacted_connection_url_for_endpoint, uses_bare_mysql_pool, uses_tcp_probe, validate_h2_database_path,
|
||||
AppState, PoolKind, PRESTOSQL_JDBC_DRIVER_CLASS,
|
||||
metadata_connection_config, mysql_metadata_fallback_url, oceanbase_mysql_setup_queries,
|
||||
prestosql_jdbc_config_for_endpoint, redacted_connection_url_for_endpoint, uses_bare_mysql_pool, uses_tcp_probe,
|
||||
validate_h2_database_path, AppState, PoolKind, PRESTOSQL_JDBC_DRIVER_CLASS,
|
||||
};
|
||||
use crate::agent_connection::{
|
||||
agent_connect_params, mongo_legacy_error_with_auth_hint, mongo_uses_legacy_driver,
|
||||
@@ -2556,6 +2603,32 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn oceanbase_mysql_setup_queries_follow_query_timeout() {
|
||||
let mut config = mysql_config(Some("dbx"));
|
||||
config.driver_profile = Some("oceanbase".to_string());
|
||||
config.query_timeout_secs = 30;
|
||||
|
||||
assert_eq!(oceanbase_mysql_setup_queries(&config), vec!["SET ob_query_timeout = 30000000"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn oceanbase_mysql_setup_queries_skip_disabled_timeout() {
|
||||
let mut config = mysql_config(Some("dbx"));
|
||||
config.driver_profile = Some("oceanbase".to_string());
|
||||
config.query_timeout_secs = 0;
|
||||
|
||||
assert!(oceanbase_mysql_setup_queries(&config).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn oceanbase_mysql_setup_queries_do_not_apply_to_plain_mysql() {
|
||||
let mut config = mysql_config(Some("dbx"));
|
||||
config.query_timeout_secs = 30;
|
||||
|
||||
assert!(oceanbase_mysql_setup_queries(&config).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mongo_legacy_retry_covers_old_server_handshake_eof() {
|
||||
let err = r#"MongoDB connection failed: Kind: Server selection timeout: No available servers. Topology: { Type: Unknown, Servers: [ { Address: db.example.com:27017, Type: Unknown, Error: Kind: I/O error: unexpected end of file } ] }"#;
|
||||
|
||||
@@ -406,16 +406,36 @@ pub async fn connect_with_ca_cert_pool_limit_and_idle(
|
||||
fallback_timeout: Duration,
|
||||
max_connections: usize,
|
||||
idle_timeout_secs: Option<u64>,
|
||||
) -> Result<MySqlPool, String> {
|
||||
connect_with_ca_cert_pool_limit_idle_and_setup(
|
||||
url,
|
||||
ca_cert_path,
|
||||
fallback_timeout,
|
||||
max_connections,
|
||||
idle_timeout_secs,
|
||||
&[],
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn connect_with_ca_cert_pool_limit_idle_and_setup(
|
||||
url: &str,
|
||||
ca_cert_path: Option<&str>,
|
||||
fallback_timeout: Duration,
|
||||
max_connections: usize,
|
||||
idle_timeout_secs: Option<u64>,
|
||||
extra_setup_queries: &[String],
|
||||
) -> Result<MySqlPool, String> {
|
||||
let timeout = super::parse_connect_timeout_with_fallback(url, fallback_timeout);
|
||||
let pool = create_pool(url, ca_cert_path, max_connections, idle_timeout_secs)?;
|
||||
let pool = create_pool(url, ca_cert_path, max_connections, idle_timeout_secs, extra_setup_queries)?;
|
||||
let result = verify_pool_connection(&pool, timeout).await;
|
||||
|
||||
if let Err(ref e) = result {
|
||||
if mysql_error_should_retry_without_ssl(e) {
|
||||
if let Some(fallback_url) = ssl_fallback_url(url) {
|
||||
log::info!("SSL handshake failed, retrying with ssl-mode=disabled");
|
||||
let fallback_pool = create_pool(&fallback_url, None, max_connections, idle_timeout_secs)?;
|
||||
let fallback_pool =
|
||||
create_pool(&fallback_url, None, max_connections, idle_timeout_secs, extra_setup_queries)?;
|
||||
return match verify_pool_connection(&fallback_pool, timeout).await {
|
||||
Ok(()) => Ok(fallback_pool),
|
||||
Err(e) => Err(e),
|
||||
@@ -438,6 +458,7 @@ fn create_pool(
|
||||
ca_cert_path: Option<&str>,
|
||||
max_connections: usize,
|
||||
idle_timeout_secs: Option<u64>,
|
||||
extra_setup_queries: &[String],
|
||||
) -> Result<MySqlPool, String> {
|
||||
let tls_url = mysql_tls_url(url)?;
|
||||
let opts =
|
||||
@@ -458,7 +479,7 @@ fn create_pool(
|
||||
.prefer_socket(false)
|
||||
.pool_opts(Some(pool_opts))
|
||||
.tcp_keepalive(Some(MYSQL_TCP_KEEPALIVE_MS))
|
||||
.setup(mysql_setup_queries(url));
|
||||
.setup(mysql_setup_queries(url, extra_setup_queries));
|
||||
if let Some(ssl_opts) = mysql_ssl_opts(base_ssl_opts, url, ca_cert_path, &tls_url.files)? {
|
||||
builder = builder.ssl_opts(ssl_opts);
|
||||
}
|
||||
@@ -562,7 +583,7 @@ fn mysql_ssl_opts(
|
||||
Ok(Some(ssl_opts))
|
||||
}
|
||||
|
||||
fn mysql_setup_queries(url: &str) -> Vec<String> {
|
||||
fn mysql_setup_queries(url: &str, extra_setup_queries: &[String]) -> Vec<String> {
|
||||
let charset = mysql_connection_charset(url).unwrap_or("utf8mb4");
|
||||
let catalog = mysql_connection_catalog(url);
|
||||
let database = mysql_connection_database(url);
|
||||
@@ -586,6 +607,7 @@ fn mysql_setup_queries(url: &str) -> Vec<String> {
|
||||
if let Some(catalog) = catalog.as_deref() {
|
||||
queries.push(format!("SET catalog = {}", quote_identifier(catalog)));
|
||||
}
|
||||
queries.extend(extra_setup_queries.iter().cloned());
|
||||
queries
|
||||
}
|
||||
|
||||
@@ -1018,9 +1040,18 @@ pub async fn connect_bare_with_pool_limit(
|
||||
url: &str,
|
||||
fallback_timeout: Duration,
|
||||
max_connections: usize,
|
||||
) -> Result<MySqlPool, String> {
|
||||
connect_bare_with_pool_limit_and_setup(url, fallback_timeout, max_connections, &[]).await
|
||||
}
|
||||
|
||||
pub async fn connect_bare_with_pool_limit_and_setup(
|
||||
url: &str,
|
||||
fallback_timeout: Duration,
|
||||
max_connections: usize,
|
||||
extra_setup_queries: &[String],
|
||||
) -> Result<MySqlPool, String> {
|
||||
let timeout = super::parse_connect_timeout_with_fallback(url, fallback_timeout);
|
||||
let pool = create_pool(url, None, max_connections, None)?;
|
||||
let pool = create_pool(url, None, max_connections, None, extra_setup_queries)?;
|
||||
verify_pool_connection(&pool, timeout).await.map(|_| pool)
|
||||
}
|
||||
|
||||
@@ -3514,21 +3545,21 @@ UNIQUE KEY(`tenant_id`, `name``part`)
|
||||
|
||||
#[test]
|
||||
fn mysql_setup_queries_select_requested_database_before_session_init() {
|
||||
let queries = mysql_setup_queries("mysql://root:secret@localhost:3306/app?charset=utf8mb4");
|
||||
let queries = mysql_setup_queries("mysql://root:secret@localhost:3306/app?charset=utf8mb4", &[]);
|
||||
|
||||
assert_eq!(queries, vec!["USE `app`", "SET NAMES utf8mb4"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mysql_setup_queries_skip_use_when_database_missing() {
|
||||
let queries = mysql_setup_queries("mysql://root:secret@localhost:3306?charset=utf8mb4");
|
||||
let queries = mysql_setup_queries("mysql://root:secret@localhost:3306?charset=utf8mb4", &[]);
|
||||
|
||||
assert_eq!(queries, vec!["SET NAMES utf8mb4"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mysql_setup_queries_decode_database_name_from_url() {
|
||||
let queries = mysql_setup_queries("mysql://root:secret@localhost:3306/db%2Fname?charset=utf8mb4");
|
||||
let queries = mysql_setup_queries("mysql://root:secret@localhost:3306/db%2Fname?charset=utf8mb4", &[]);
|
||||
|
||||
assert_eq!(queries, vec!["USE `db/name`", "SET NAMES utf8mb4"]);
|
||||
}
|
||||
@@ -3684,29 +3715,39 @@ UNIQUE KEY(`tenant_id`, `name``part`)
|
||||
|
||||
#[test]
|
||||
fn mysql_setup_queries_default_to_utf8mb4() {
|
||||
assert_eq!(mysql_setup_queries("mysql://host:3306/db"), vec!["USE `db`", "SET NAMES utf8mb4"]);
|
||||
assert_eq!(mysql_setup_queries("mysql://host:3306/db", &[]), vec!["USE `db`", "SET NAMES utf8mb4"]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mysql_setup_queries_use_safe_custom_charset() {
|
||||
assert_eq!(
|
||||
mysql_setup_queries("mysql://host:3306/db?ssl-mode=preferred&charset=gbk"),
|
||||
mysql_setup_queries("mysql://host:3306/db?ssl-mode=preferred&charset=gbk", &[]),
|
||||
vec!["USE `db`", "SET NAMES gbk"]
|
||||
);
|
||||
assert_eq!(
|
||||
mysql_setup_queries("mysql://host:3306/db?charset=utf8mb4;DROP TABLE users"),
|
||||
mysql_setup_queries("mysql://host:3306/db?charset=utf8mb4;DROP TABLE users", &[]),
|
||||
vec!["USE `db`", "SET NAMES utf8mb4"]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mysql_setup_queries_include_extra_setup_queries() {
|
||||
let extra = vec!["SET ob_query_timeout = 30000000".to_string()];
|
||||
|
||||
assert_eq!(
|
||||
mysql_setup_queries("mysql://host:3306/db", &extra),
|
||||
vec!["USE `db`", "SET NAMES utf8mb4", "SET ob_query_timeout = 30000000"]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mysql_setup_queries_apply_explicit_time_zone() {
|
||||
assert_eq!(
|
||||
mysql_setup_queries("mysql://host:3306/db?time_zone=%2B08%3A00&charset=utf8mb4"),
|
||||
mysql_setup_queries("mysql://host:3306/db?time_zone=%2B08%3A00&charset=utf8mb4", &[]),
|
||||
vec!["USE `db`", "SET time_zone = '+08:00'", "SET NAMES utf8mb4"]
|
||||
);
|
||||
assert_eq!(
|
||||
mysql_setup_queries("mysql://host:3306/db?time-zone=Asia%2FShanghai"),
|
||||
mysql_setup_queries("mysql://host:3306/db?time-zone=Asia%2FShanghai", &[]),
|
||||
vec!["USE `db`", "SET time_zone = 'Asia/Shanghai'", "SET NAMES utf8mb4"]
|
||||
);
|
||||
}
|
||||
@@ -3714,11 +3755,11 @@ UNIQUE KEY(`tenant_id`, `name``part`)
|
||||
#[test]
|
||||
fn mysql_setup_queries_apply_jdbc_time_zone_aliases() {
|
||||
assert_eq!(
|
||||
mysql_setup_queries("mysql://host:3306/db?serverTimezone=GMT%2B8"),
|
||||
mysql_setup_queries("mysql://host:3306/db?serverTimezone=GMT%2B8", &[]),
|
||||
vec!["USE `db`", "SET time_zone = '+08:00'", "SET NAMES utf8mb4"]
|
||||
);
|
||||
assert_eq!(
|
||||
mysql_setup_queries("mysql://host:3306/db?connectionTimeZone=UTC"),
|
||||
mysql_setup_queries("mysql://host:3306/db?connectionTimeZone=UTC", &[]),
|
||||
vec!["USE `db`", "SET time_zone = '+00:00'", "SET NAMES utf8mb4"]
|
||||
);
|
||||
}
|
||||
@@ -3726,11 +3767,11 @@ UNIQUE KEY(`tenant_id`, `name``part`)
|
||||
#[test]
|
||||
fn mysql_setup_queries_apply_go_loc_when_no_explicit_time_zone_exists() {
|
||||
assert_eq!(
|
||||
mysql_setup_queries("mysql://host:3306/db?loc=Asia%2FShanghai"),
|
||||
mysql_setup_queries("mysql://host:3306/db?loc=Asia%2FShanghai", &[]),
|
||||
vec!["USE `db`", "SET time_zone = 'Asia/Shanghai'", "SET NAMES utf8mb4"]
|
||||
);
|
||||
assert_eq!(
|
||||
mysql_setup_queries("mysql://host:3306/db?time_zone=%2B08%3A00&loc=UTC"),
|
||||
mysql_setup_queries("mysql://host:3306/db?time_zone=%2B08%3A00&loc=UTC", &[]),
|
||||
vec!["USE `db`", "SET time_zone = '+08:00'", "SET NAMES utf8mb4"]
|
||||
);
|
||||
}
|
||||
@@ -3738,7 +3779,7 @@ UNIQUE KEY(`tenant_id`, `name``part`)
|
||||
#[test]
|
||||
fn mysql_setup_queries_ignore_unsafe_time_zone_values() {
|
||||
assert_eq!(
|
||||
mysql_setup_queries("mysql://host:3306/db?time_zone=%2B08%3A00%27%3BDROP%20TABLE%20users"),
|
||||
mysql_setup_queries("mysql://host:3306/db?time_zone=%2B08%3A00%27%3BDROP%20TABLE%20users", &[]),
|
||||
vec!["USE `db`", "SET NAMES utf8mb4"]
|
||||
);
|
||||
}
|
||||
@@ -3748,7 +3789,7 @@ UNIQUE KEY(`tenant_id`, `name``part`)
|
||||
// `SET catalog` is pushed last so mysql_async's back-to-front setup
|
||||
// execution (Vec::pop) runs it before `USE <database>`.
|
||||
assert_eq!(
|
||||
mysql_setup_queries("mysql://host:3306/clip?catalog=paimon_catalog"),
|
||||
mysql_setup_queries("mysql://host:3306/clip?catalog=paimon_catalog", &[]),
|
||||
vec!["USE `clip`", "SET NAMES utf8mb4", "SET catalog = `paimon_catalog`"]
|
||||
);
|
||||
}
|
||||
@@ -3756,7 +3797,7 @@ UNIQUE KEY(`tenant_id`, `name``part`)
|
||||
#[test]
|
||||
fn mysql_setup_queries_switch_catalog_without_database() {
|
||||
assert_eq!(
|
||||
mysql_setup_queries("mysql://host:3306/?catalog=paimon_catalog"),
|
||||
mysql_setup_queries("mysql://host:3306/?catalog=paimon_catalog", &[]),
|
||||
vec!["SET NAMES utf8mb4", "SET catalog = `paimon_catalog`"]
|
||||
);
|
||||
}
|
||||
@@ -3764,13 +3805,16 @@ UNIQUE KEY(`tenant_id`, `name``part`)
|
||||
#[test]
|
||||
fn mysql_setup_queries_decodes_catalog_parameter() {
|
||||
assert_eq!(
|
||||
mysql_setup_queries("mysql://host:3306/db?catalog=my%5Fcatalog"),
|
||||
mysql_setup_queries("mysql://host:3306/db?catalog=my%5Fcatalog", &[]),
|
||||
vec!["USE `db`", "SET NAMES utf8mb4", "SET catalog = `my_catalog`"]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mysql_setup_queries_omits_catalog_when_absent() {
|
||||
assert_eq!(mysql_setup_queries("mysql://host:3306/db?charset=utf8mb4"), vec!["USE `db`", "SET NAMES utf8mb4"]);
|
||||
assert_eq!(
|
||||
mysql_setup_queries("mysql://host:3306/db?charset=utf8mb4", &[]),
|
||||
vec!["USE `db`", "SET NAMES utf8mb4"]
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user