refactor(workspace): split SQL and database driver crates

This commit is contained in:
t8y2 committed 2026-09-25 15:51:58 +08:00
1 parent b99c60aff6
commit e0da2d09d3
180 files changed
+1422 -538

No files matched your search

@@ -0,0 +1,149 @@
use std::collections::HashSet;
use crate::database_manifest;
use crate::models::connection::DatabaseType;
pub fn agent_key(db_type: &DatabaseType, driver_profile: Option<&str>) -> Option<&'static str> {
let entry = database_manifest::entry(db_type)?;
if let Some(driver_profile) = driver_profile {
if let Some(profile) =
entry.driver_profiles.iter().find(|profile| profile.profile.eq_ignore_ascii_case(driver_profile))
{
return Some(profile.agent_key.as_str());
}
}
entry.agent_key.as_deref()
}
pub fn is_agent_type(db_type: &DatabaseType) -> bool {
database_manifest::is_agent_runtime(db_type)
}
pub fn driver_store_entries() -> impl Iterator<Item = (&'static str, &'static str)> {
let mut entries = database_manifest::entries()
.iter()
.flat_map(|entry| {
let base = entry.driver_store_visible.then(|| {
entry
.agent_key
.as_deref()
.map(|key| (entry.driver_store_order.unwrap_or(u32::MAX), key, entry.label.as_str()))
});
let profiles = entry.driver_profiles.iter().filter(|profile| profile.store_visible).map(|profile| {
(
profile.store_order.unwrap_or(u32::MAX),
profile.package_key.as_deref().unwrap_or(profile.agent_key.as_str()),
profile.label.as_str(),
)
});
let managed = entry
.managed_drivers
.iter()
.filter(|driver| driver.store_visible)
.map(|driver| (driver.store_order.unwrap_or(u32::MAX), driver.key.as_str(), driver.label.as_str()));
base.flatten().into_iter().chain(profiles).chain(managed)
})
.collect::<Vec<_>>();
entries.sort_by_key(|(order, _, _)| *order);
let mut seen = HashSet::new();
entries.into_iter().filter(move |(_, key, _)| seen.insert(*key)).map(|(_, key, label)| (key, label))
}
pub fn label_for_key(agent_key: &str) -> Option<&'static str> {
for entry in database_manifest::entries() {
if entry.agent_key.as_deref() == Some(agent_key) {
return Some(entry.label.as_str());
}
if let Some(profile) = entry
.driver_profiles
.iter()
.find(|profile| profile.package_key.as_deref() == Some(agent_key) || profile.agent_key == agent_key)
{
return Some(profile.label.as_str());
}
if let Some(driver) = entry.managed_drivers.iter().find(|driver| driver.key == agent_key) {
return Some(driver.label.as_str());
}
}
None
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn h2_profiles_share_the_same_agent() {
assert_eq!(agent_key(&DatabaseType::H2, None), Some("h2"));
assert_eq!(agent_key(&DatabaseType::H2, Some("h2")), Some("h2"));
assert_eq!(agent_key(&DatabaseType::H2, Some("h2-legacy")), Some("h2"));
assert_eq!(agent_key(&DatabaseType::H2, Some("h2-v1")), Some("h2"));
assert_eq!(agent_key(&DatabaseType::H2, Some("h2-v2")), Some("h2"));
assert_eq!(agent_key(&DatabaseType::H2, Some("h2-v3")), Some("h2"));
assert_eq!(agent_key(&DatabaseType::H2, Some("h2-custom")), Some("h2"));
assert_eq!(label_for_key("h2-legacy"), Some("H2 2.1 Legacy"));
assert!(!driver_store_entries().any(|(key, _)| key == "h2-legacy"));
}
#[test]
fn etcd_v2_profile_uses_dedicated_agent() {
assert_eq!(agent_key(&DatabaseType::Etcd, None), Some("etcd"));
assert_eq!(agent_key(&DatabaseType::Etcd, Some("etcd")), Some("etcd"));
assert_eq!(agent_key(&DatabaseType::Etcd, Some("etcd-v2")), Some("etcd2"));
assert_eq!(agent_key(&DatabaseType::Etcd, Some("etcd-custom")), Some("etcd"));
assert_eq!(label_for_key("etcd2"), Some("etcd 2.x (v2 API)"));
// etcd2 ships its own binary, version, and registry entry, so it must
// stay visible in the driver store for install/upgrade/uninstall.
assert!(driver_store_entries().any(|(key, label)| key == "etcd2" && label == "etcd 2.x (v2 API)"));
}
#[test]
fn duckdb_is_available_in_driver_store_without_using_agent_runtime() {
assert!(driver_store_entries().any(|(key, label)| key == "duckdb" && label == "DuckDB"));
assert_eq!(label_for_key("duckdb"), Some("DuckDB"));
assert!(!is_agent_type(&DatabaseType::DuckDb));
}
#[test]
fn sqlite_ssh_worker_is_available_in_driver_store_without_using_agent_runtime() {
assert!(driver_store_entries().any(|(key, label)| key == "sqlite-worker" && label == "SQLite SSH Worker"));
assert_eq!(label_for_key("sqlite-worker"), Some("SQLite SSH Worker"));
assert!(!is_agent_type(&DatabaseType::Sqlite));
}
#[test]
fn impala_reuses_hive_agent_without_duplicate_store_entry() {
assert_eq!(agent_key(&DatabaseType::Impala, None), Some("hive"));
assert_eq!(driver_store_entries().filter(|(key, _)| *key == "hive").count(), 1);
}
#[test]
fn kyuubi_reuses_hive_agent_without_duplicate_store_entry() {
assert_eq!(agent_key(&DatabaseType::Kyuubi, None), Some("hive"));
assert_eq!(driver_store_entries().filter(|(key, _)| *key == "hive").count(), 1);
}
#[test]
fn cache_profile_uses_dedicated_agent_under_iris() {
assert_eq!(agent_key(&DatabaseType::Iris, None), Some("iris"));
assert_eq!(agent_key(&DatabaseType::Iris, Some("iris")), Some("iris"));
assert_eq!(agent_key(&DatabaseType::Iris, Some("cache")), Some("cache"));
assert_eq!(label_for_key("cache"), Some("InterSystems Caché"));
// The Caché agent ships its own shaded CacheDB driver, so it needs its
// own driver store entry for install/upgrade/uninstall.
assert!(driver_store_entries().any(|(key, label)| key == "cache" && label == "InterSystems Caché"));
}
#[test]
fn manifest_agent_keys_match_catalog_defaults() {
for entry in database_manifest::entries().iter().filter(|entry| entry.agent_key.is_some()) {
assert_eq!(
agent_key(&entry.db_type, None),
entry.agent_key.as_deref(),
"agent key drift for {:?}",
entry.db_type
);
}
}
}
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,68 @@
//! Recovery decisions derived from typed JDBC Agent failures.
use crate::db::agent_driver::{AgentCallError, AgentErrorCategory, AgentSessionDisposition};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RecoveryScope {
UserOperation,
ReadOnlyMetadata { retried: bool },
Keepalive,
ConnectionOpen,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RecoveryDecision {
KeepSession,
RetryReadOnlyMetadata,
QuarantineSession,
ReplaceRuntime,
}
impl RecoveryDecision {
pub fn discards_session(self) -> bool {
!matches!(self, Self::KeepSession)
}
pub fn replaces_runtime(self) -> bool {
matches!(self, Self::ReplaceRuntime)
}
}
pub struct RecoveryPolicy;
impl RecoveryPolicy {
pub fn decide(error: &AgentCallError, scope: RecoveryScope) -> RecoveryDecision {
match error {
AgentCallError::ContractViolation { .. } => RecoveryDecision::QuarantineSession,
AgentCallError::Transport { .. } => RecoveryDecision::ReplaceRuntime,
AgentCallError::Timeout { .. } | AgentCallError::Canceled { .. } => RecoveryDecision::QuarantineSession,
AgentCallError::Structured { context, .. } => {
if matches!(context.category, AgentErrorCategory::Timeout | AgentErrorCategory::Canceled) {
return RecoveryDecision::QuarantineSession;
}
Self::from_disposition(Some(context.session_disposition), Some(context.category), scope)
}
AgentCallError::Legacy { hints, .. } => {
Self::from_disposition(hints.session_disposition, hints.category, scope)
}
}
}
fn from_disposition(
disposition: Option<AgentSessionDisposition>,
category: Option<AgentErrorCategory>,
scope: RecoveryScope,
) -> RecoveryDecision {
match disposition {
Some(AgentSessionDisposition::ReplaceRuntime) => RecoveryDecision::ReplaceRuntime,
Some(AgentSessionDisposition::Quarantine)
if category == Some(AgentErrorCategory::Connection)
&& matches!(scope, RecoveryScope::ReadOnlyMetadata { retried: false }) =>
{
RecoveryDecision::RetryReadOnlyMetadata
}
Some(AgentSessionDisposition::Quarantine) => RecoveryDecision::QuarantineSession,
Some(AgentSessionDisposition::Keep) | None => RecoveryDecision::KeepSession,
}
}
}
@@ -0,0 +1,469 @@
use serde::de::DeserializeOwned;
use std::time::Duration;
use crate::agent_manager::{AgentManager, DEFAULT_JRE_KEY};
use crate::agent_recovery::{RecoveryPolicy, RecoveryScope};
use crate::database_capabilities;
use crate::db::agent_driver::{AgentCallError, AgentDriverClient, AgentMethod, AgentRuntimeClient};
use crate::models::connection::DatabaseType;
pub struct SharedConnectionOpenError {
pub message: String,
pub runtime: Option<std::sync::Arc<AgentRuntimeClient>>,
}
impl From<String> for SharedConnectionOpenError {
fn from(message: String) -> Self {
Self { message, runtime: None }
}
}
pub fn db_type_to_agent_key(db_type: &DatabaseType, driver_profile: Option<&str>) -> Option<&'static str> {
database_capabilities::agent_key(db_type, driver_profile)
}
pub fn is_agent_type(db_type: &DatabaseType) -> bool {
database_capabilities::is_agent_type(db_type)
}
pub async fn stop_daemons(manager: &AgentManager) {
manager.daemons.lock().await.clear();
let runtimes = std::mem::take(&mut *manager.connection_runtimes.lock().await);
for runtime in runtimes.into_values().filter_map(|cell| cell.get().cloned()) {
runtime.kill_and_wait().await;
}
}
pub async fn stop_daemon_by_key(manager: &AgentManager, agent_key: &str) {
manager.daemons.lock().await.remove(agent_key);
let runtimes = {
let mut runtimes = manager.connection_runtimes.lock().await;
let matching =
runtimes.keys().filter(|key| key.starts_with(&format!("{agent_key}|"))).cloned().collect::<Vec<_>>();
matching
.into_iter()
.filter_map(|key| runtimes.remove(&key).and_then(|cell| cell.get().cloned()))
.collect::<Vec<_>>()
};
for runtime in runtimes {
runtime.kill_and_wait().await;
}
}
pub async fn restart_daemon_by_key(manager: &AgentManager, agent_key: &str) -> Result<(), String> {
manager.daemons.lock().await.remove(agent_key);
let client = spawn_client_for_key(manager, agent_key, &[]).await?;
manager.daemons.lock().await.insert(agent_key.to_string(), client);
Ok(())
}
pub async fn spawn_connection_client(
manager: &AgentManager,
db_type: &DatabaseType,
driver_profile: Option<&str>,
extra_java_args: &[String],
) -> Result<AgentDriverClient, String> {
let keys = runtime_agent_key_candidates(db_type, driver_profile)
.ok_or_else(|| format!("{:?} is not an agent-driven database type", db_type))?;
spawn_first_available_client(manager, &keys, extra_java_args).await
}
pub async fn spawn_shared_connection_client(
manager: &AgentManager,
db_type: &DatabaseType,
driver_profile: Option<&str>,
extra_java_args: &[String],
agent_session_id: String,
connect_params: serde_json::Value,
connect_timeout: Duration,
) -> Result<AgentDriverClient, SharedConnectionOpenError> {
let keys = runtime_agent_key_candidates(db_type, driver_profile)
.ok_or_else(|| format!("{:?} is not an agent-driven database type", db_type))?;
let key = first_installed_agent_key(manager, &keys).unwrap_or(keys[0]);
let state = manager.load_state();
let jre_key = state.installed_drivers.get(key).map(|driver| driver.jre.as_str()).unwrap_or(DEFAULT_JRE_KEY);
let launch = manager.resolve_agent_launch_spec_with_extra_args(&state, key, jre_key, extra_java_args)?;
let runtime_key = shared_runtime_key(key, &launch);
let mut session_params = connect_params;
session_params
.as_object_mut()
.ok_or_else(|| "Agent connect parameters must be an object".to_string())?
.insert("agentSessionId".to_string(), serde_json::Value::String(agent_session_id.clone()));
let (runtime_cell, runtime) = loop {
let runtime_cell = {
let mut runtimes = manager.connection_runtimes.lock().await;
if runtimes.get(&runtime_key).and_then(|cell| cell.get()).is_some_and(|runtime| runtime.is_failed()) {
runtimes.remove(&runtime_key);
}
runtimes
.entry(runtime_key.clone())
.or_insert_with(|| std::sync::Arc::new(tokio::sync::OnceCell::new()))
.clone()
};
let runtime = runtime_cell
.get_or_try_init(|| AgentRuntimeClient::spawn(launch.clone(), manager.agent_app_version()))
.await?
.clone();
let mut runtimes = manager.connection_runtimes.lock().await;
if reserve_runtime_locked(&mut runtimes, &runtime_key, &runtime_cell, &runtime) {
break (runtime_cell, runtime);
}
};
if let Err(err) = runtime
.call_typed::<serde_json::Value>(AgentMethod::OpenSession.as_str(), session_params, Some(connect_timeout), None)
.await
{
let open_error = shared_connection_open_error(err, runtime.clone());
forget_unused_runtime_after_failed_open(manager, &runtime_key, &runtime_cell, &runtime).await;
return Err(open_error);
}
Ok(AgentDriverClient::shared_session(runtime, agent_session_id))
}
fn shared_connection_open_error(
error: AgentCallError,
runtime: std::sync::Arc<AgentRuntimeClient>,
) -> SharedConnectionOpenError {
let runtime = RecoveryPolicy::decide(&error, RecoveryScope::ConnectionOpen).replaces_runtime().then_some(runtime);
SharedConnectionOpenError { message: error.into_legacy_string(), runtime }
}
async fn forget_unused_runtime_after_failed_open(
manager: &AgentManager,
runtime_key: &str,
runtime_cell: &std::sync::Arc<tokio::sync::OnceCell<std::sync::Arc<AgentRuntimeClient>>>,
runtime: &std::sync::Arc<AgentRuntimeClient>,
) {
if AgentRuntimeClient::decrement_session_count(runtime) != 0 {
return;
}
remove_unused_runtime_if_current(manager, runtime_key, runtime_cell, runtime).await;
}
async fn remove_unused_runtime_if_current(
manager: &AgentManager,
runtime_key: &str,
runtime_cell: &std::sync::Arc<tokio::sync::OnceCell<std::sync::Arc<AgentRuntimeClient>>>,
runtime: &std::sync::Arc<AgentRuntimeClient>,
) {
// Keep reservation and map-entry validation under the same lock as openers.
let mut runtimes = manager.connection_runtimes.lock().await;
if runtime.active_session_count() == 0
&& runtimes.get(runtime_key).is_some_and(|current| std::sync::Arc::ptr_eq(current, runtime_cell))
{
runtimes.remove(runtime_key);
}
}
fn reserve_runtime_locked(
runtimes: &mut std::collections::HashMap<
String,
std::sync::Arc<tokio::sync::OnceCell<std::sync::Arc<AgentRuntimeClient>>>,
>,
runtime_key: &str,
runtime_cell: &std::sync::Arc<tokio::sync::OnceCell<std::sync::Arc<AgentRuntimeClient>>>,
runtime: &std::sync::Arc<AgentRuntimeClient>,
) -> bool {
if runtimes.get(runtime_key).is_some_and(|current| std::sync::Arc::ptr_eq(current, runtime_cell))
&& !runtime.is_failed()
{
runtime.increment_session_count();
true
} else {
false
}
}
fn shared_runtime_key(agent_key: &str, launch: &crate::db::agent_driver::AgentLaunchSpec) -> String {
format!(
"{}|{}|{}|{}",
agent_key,
launch.program.display(),
launch.args.join("\u{1f}"),
launch.working_dir.as_ref().map(|path| path.display().to_string()).unwrap_or_default()
)
}
pub async fn call_daemon<T: DeserializeOwned + Send + 'static>(
manager: &AgentManager,
db_type: &DatabaseType,
driver_profile: Option<&str>,
method: &str,
params: serde_json::Value,
) -> Result<T, String> {
let keys = runtime_agent_key_candidates(db_type, driver_profile)
.ok_or_else(|| format!("{:?} is not an agent-driven database type", db_type))?;
let key = first_installed_agent_key(manager, &keys).unwrap_or(keys[0]).to_string();
let mut daemons = manager.daemons.lock().await;
if !daemons.contains_key(&key) {
let client = spawn_client_for_key(manager, &key, &[]).await?;
daemons.insert(key.clone(), client);
}
let client = daemons.get_mut(&key).unwrap();
match client.call::<T>(method, params.clone()).await {
Ok(result) => Ok(result),
Err(err) => {
log::warn!("[agent] daemon call failed, respawning: {err}");
daemons.remove(&key);
let mut new_client = spawn_client_for_key(manager, &key, &[]).await?;
let result = new_client.call::<T>(method, params).await?;
daemons.insert(key, new_client);
Ok(result)
}
}
}
pub async fn call_daemon_with_timeout<T: DeserializeOwned + Send + 'static>(
manager: &AgentManager,
db_type: &DatabaseType,
driver_profile: Option<&str>,
method: &str,
params: serde_json::Value,
timeout_duration: Option<Duration>,
) -> Result<T, String> {
let keys = runtime_agent_key_candidates(db_type, driver_profile)
.ok_or_else(|| format!("{:?} is not an agent-driven database type", db_type))?;
let key = first_installed_agent_key(manager, &keys).unwrap_or(keys[0]).to_string();
let mut daemons = manager.daemons.lock().await;
if !daemons.contains_key(&key) {
let client = spawn_client_for_key(manager, &key, &[]).await?;
daemons.insert(key.clone(), client);
}
let client = daemons.get_mut(&key).unwrap();
match client.call_with_timeout::<T>(method, params.clone(), timeout_duration).await {
Ok(result) => Ok(result),
Err(err) => {
log::warn!("[agent] daemon call failed, respawning: {err}");
daemons.remove(&key);
let mut new_client = spawn_client_for_key(manager, &key, &[]).await?;
let result = new_client.call_with_timeout::<T>(method, params, timeout_duration).await?;
daemons.insert(key, new_client);
Ok(result)
}
}
}
pub async fn call_daemon_method<T: DeserializeOwned + Send + 'static>(
manager: &AgentManager,
db_type: &DatabaseType,
driver_profile: Option<&str>,
method: AgentMethod,
params: serde_json::Value,
) -> Result<T, String> {
call_daemon(manager, db_type, driver_profile, method.as_str(), params).await
}
pub async fn call_daemon_method_with_timeout<T: DeserializeOwned + Send + 'static>(
manager: &AgentManager,
db_type: &DatabaseType,
driver_profile: Option<&str>,
method: AgentMethod,
params: serde_json::Value,
timeout_duration: Option<Duration>,
) -> Result<T, String> {
call_daemon_with_timeout(manager, db_type, driver_profile, method.as_str(), params, timeout_duration).await
}
fn runtime_agent_key_candidates(db_type: &DatabaseType, driver_profile: Option<&str>) -> Option<Vec<&'static str>> {
let primary = db_type_to_agent_key(db_type, driver_profile)?;
Some(vec![primary])
}
fn first_installed_agent_key<'a>(manager: &AgentManager, keys: &'a [&'static str]) -> Option<&'a str> {
keys.iter().copied().find(|key| manager.is_driver_installed(key))
}
async fn spawn_first_available_client(
manager: &AgentManager,
keys: &[&'static str],
extra_java_args: &[String],
) -> Result<AgentDriverClient, String> {
let mut last_error = None;
for key in keys {
match spawn_client_for_key(manager, key, extra_java_args).await {
Ok(client) => return Ok(client),
Err(err) => last_error = Some(err),
}
}
Err(last_error.unwrap_or_else(|| "No agent driver candidates available".to_string()))
}
async fn spawn_client_for_key(
manager: &AgentManager,
key: &str,
extra_java_args: &[String],
) -> Result<AgentDriverClient, String> {
let state = manager.load_state();
let jre_key = state.installed_drivers.get(key).map(|driver| driver.jre.as_str()).unwrap_or(DEFAULT_JRE_KEY);
let launch = manager.resolve_agent_launch_spec_with_extra_args(&state, key, jre_key, extra_java_args)?;
let mut client = AgentDriverClient::spawn(launch).await?;
client.try_optional_handshake(manager.agent_app_version()).await;
Ok(client)
}
#[cfg(test)]
mod tests {
use super::*;
use std::path::PathBuf;
async fn test_shared_runtime(
name: &str,
) -> (
AgentManager,
std::sync::Arc<tokio::sync::OnceCell<std::sync::Arc<AgentRuntimeClient>>>,
std::sync::Arc<AgentRuntimeClient>,
PathBuf,
) {
let test_id = uuid::Uuid::new_v4();
let script_path = std::env::temp_dir().join(format!("dbx-agent-runtime-{name}-{test_id}.py"));
std::fs::write(
&script_path,
r#"import json, sys
print(json.dumps({'ready': True}), flush=True)
for line in sys.stdin:
req = json.loads(line)
result = {'protocolVersion': 2, 'agentProtocolVersion': 2, 'capabilities': ['multi_session']} if req['method'] == 'handshake' else {'ok': True}
print(json.dumps({'jsonrpc': '2.0', 'id': req['id'], 'result': result}), flush=True)
"#,
)
.unwrap();
let python = if cfg!(windows) { "python" } else { "python3" };
let runtime = AgentRuntimeClient::spawn(
crate::db::agent_driver::AgentLaunchSpec::new(python)
.with_args([script_path.to_string_lossy().to_string()]),
"test",
)
.await
.unwrap();
let cell = std::sync::Arc::new(tokio::sync::OnceCell::new());
assert!(cell.set(runtime.clone()).is_ok());
let manager_dir = std::env::temp_dir().join(format!("dbx-agent-manager-{name}-{test_id}"));
let manager = AgentManager::new_with_base_dir_and_app_version(manager_dir, "test");
(manager, cell, runtime, script_path)
}
#[test]
fn prestosql_does_not_use_agent_driver() {
assert_eq!(runtime_agent_key_candidates(&DatabaseType::PrestoSql, None), None);
}
#[test]
fn trino_uses_only_trino_agent_driver() {
assert_eq!(runtime_agent_key_candidates(&DatabaseType::Trino, None).unwrap(), vec!["trino"]);
}
#[test]
fn shared_runtime_key_includes_launch_fingerprint() {
let base = crate::db::agent_driver::AgentLaunchSpec::new(PathBuf::from("oracle-agent"))
.with_args(["--mode", "stdio"])
.with_working_dir(PathBuf::from("/tmp/oracle"));
let different_args = crate::db::agent_driver::AgentLaunchSpec::new(PathBuf::from("oracle-agent"))
.with_args(["--mode", "debug"])
.with_working_dir(PathBuf::from("/tmp/oracle"));
assert_eq!(shared_runtime_key("oracle", &base), shared_runtime_key("oracle", &base));
assert_ne!(shared_runtime_key("oracle", &base), shared_runtime_key("oracle", &different_args));
}
#[tokio::test]
async fn failed_open_forgets_runtime_when_no_other_session_uses_it() {
let (manager, cell, runtime, script_path) = test_shared_runtime("failed-open-unused").await;
let runtime_key = "kingbase|test";
manager.connection_runtimes.lock().await.insert(runtime_key.to_string(), cell.clone());
runtime.increment_session_count();
forget_unused_runtime_after_failed_open(&manager, runtime_key, &cell, &runtime).await;
assert!(!manager.connection_runtimes.lock().await.contains_key(runtime_key));
assert_eq!(runtime.active_session_count(), 0);
runtime.kill();
let _ = std::fs::remove_file(script_path);
}
#[tokio::test]
async fn failed_open_keeps_runtime_while_another_session_is_reserved() {
let (manager, cell, runtime, script_path) = test_shared_runtime("failed-open-in-use").await;
let runtime_key = "oracle|test";
manager.connection_runtimes.lock().await.insert(runtime_key.to_string(), cell.clone());
runtime.increment_session_count();
runtime.increment_session_count();
forget_unused_runtime_after_failed_open(&manager, runtime_key, &cell, &runtime).await;
assert!(manager.connection_runtimes.lock().await.contains_key(runtime_key));
assert_eq!(runtime.active_session_count(), 1);
AgentRuntimeClient::decrement_session_count(&runtime);
runtime.kill();
let _ = std::fs::remove_file(script_path);
}
#[tokio::test]
async fn replace_runtime_open_error_reports_runtime_without_killing_it_directly() {
let (manager, _cell, runtime, script_path) = test_shared_runtime("replace-runtime-open-error").await;
let error = AgentCallError::Legacy {
rpc_code: Some(-1),
message: "capacity exhausted".to_string(),
hints: crate::db::agent_driver::LegacyAgentHints {
category: Some(crate::db::agent_driver::AgentErrorCategory::Resource),
session_disposition: Some(crate::db::agent_driver::AgentSessionDisposition::ReplaceRuntime),
..Default::default()
},
};
let open_error = shared_connection_open_error(error, runtime.clone());
assert!(open_error.runtime.as_ref().is_some_and(|failed| std::sync::Arc::ptr_eq(failed, &runtime)));
assert!(!runtime.is_failed());
runtime.kill();
drop(manager);
let _ = std::fs::remove_file(script_path);
}
#[tokio::test]
async fn failed_open_cleanup_cannot_remove_runtime_after_reservation() {
let (manager, cell, runtime, script_path) = test_shared_runtime("failed-open-race").await;
let runtime_key = "oracle|test";
manager.connection_runtimes.lock().await.insert(runtime_key.to_string(), cell.clone());
runtime.increment_session_count();
assert_eq!(AgentRuntimeClient::decrement_session_count(&runtime), 0);
let mut runtimes = manager.connection_runtimes.lock().await;
assert!(reserve_runtime_locked(&mut runtimes, runtime_key, &cell, &runtime));
drop(runtimes);
remove_unused_runtime_if_current(&manager, runtime_key, &cell, &runtime).await;
assert!(manager.connection_runtimes.lock().await.contains_key(runtime_key));
assert_eq!(runtime.active_session_count(), 1);
AgentRuntimeClient::decrement_session_count(&runtime);
runtime.kill();
let _ = std::fs::remove_file(script_path);
}
#[tokio::test]
async fn stopping_driver_runtime_removes_and_terminates_shared_process() {
let (manager, cell, runtime, script_path) = test_shared_runtime("stop-driver-runtime").await;
let runtime_key = "dameng|test";
manager.connection_runtimes.lock().await.insert(runtime_key.to_string(), cell);
stop_daemon_by_key(&manager, "dameng").await;
assert!(!manager.connection_runtimes.lock().await.contains_key(runtime_key));
assert!(runtime.is_failed());
let call_result =
runtime.call::<serde_json::Value>("ping", serde_json::json!({}), Some(Duration::from_secs(1)), None).await;
assert_eq!(call_result.unwrap_err(), "Agent runtime is unavailable");
let _ = std::fs::remove_file(script_path);
}
}
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,84 @@
use crate::agent_catalog;
use crate::database_manifest;
use crate::models::connection::DatabaseType;
pub fn agent_key(db_type: &DatabaseType, driver_profile: Option<&str>) -> Option<&'static str> {
agent_catalog::agent_key(db_type, driver_profile)
}
pub fn is_agent_type(db_type: &DatabaseType) -> bool {
database_manifest::is_agent_runtime(db_type)
}
pub fn is_single_connection_pool(db_type: &DatabaseType) -> bool {
database_manifest::entry(db_type).is_some_and(|entry| entry.single_connection_pool)
}
pub fn is_metadata_connection_scoped(db_type: &DatabaseType) -> bool {
database_manifest::entry(db_type).is_some_and(|entry| entry.metadata_connection_scoped)
}
pub fn skips_tcp_probe(db_type: &DatabaseType) -> bool {
database_manifest::entry(db_type).is_some_and(|entry| entry.skip_tcp_probe) || is_agent_type(db_type)
}
/// Database types whose connection backs onto a single local file (or may, in the
/// case of H2 file mode). Used to decide whether to expose a "reveal in file
/// manager" affordance. Whether the H2 connection is actually in file mode must
/// be determined separately by parsing the JDBC URL.
pub fn is_local_file_db_type(db_type: &DatabaseType) -> bool {
database_manifest::entry(db_type).is_some_and(|entry| entry.local_file)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn local_file_db_types_match_expected_set() {
assert!(is_local_file_db_type(&DatabaseType::Sqlite));
assert!(is_local_file_db_type(&DatabaseType::DuckDb));
assert!(is_local_file_db_type(&DatabaseType::Access));
assert!(is_local_file_db_type(&DatabaseType::H2));
}
#[test]
fn non_local_file_db_types_rejected() {
assert!(!is_local_file_db_type(&DatabaseType::Mysql));
assert!(!is_local_file_db_type(&DatabaseType::Postgres));
assert!(!is_local_file_db_type(&DatabaseType::Redis));
assert!(!is_local_file_db_type(&DatabaseType::MongoDb));
assert!(!is_local_file_db_type(&DatabaseType::Turso));
assert!(!is_local_file_db_type(&DatabaseType::CloudflareD1));
assert!(!is_local_file_db_type(&DatabaseType::Rqlite));
}
#[test]
fn cloudflare_d1_uses_a_single_http_pool_without_tcp_probe() {
assert!(is_single_connection_pool(&DatabaseType::CloudflareD1));
assert!(skips_tcp_probe(&DatabaseType::CloudflareD1));
}
#[test]
fn manifest_connection_capabilities_are_used_for_every_registered_driver() {
for entry in database_manifest::entries() {
assert_eq!(
is_single_connection_pool(&entry.db_type),
entry.single_connection_pool,
"single connection pool drift for {:?}",
entry.db_type
);
assert_eq!(
is_metadata_connection_scoped(&entry.db_type),
entry.metadata_connection_scoped,
"metadata scope drift for {:?}",
entry.db_type
);
assert!(
skips_tcp_probe(&entry.db_type) || !is_agent_type(&entry.db_type),
"agent driver {:?} must skip the TCP probe",
entry.db_type
);
}
}
}
+21
View File
@@ -0,0 +1,21 @@
#![recursion_limit = "256"]
pub use dbx_platform::download::DownloadSource;
pub use dbx_platform::{path_utils, process};
pub use dbx_sql_core::sql_error_position;
pub use dbx_types::{database_manifest, models, types};
pub mod agent_catalog;
pub mod agent_connection;
pub mod agent_driver;
pub mod agent_manager;
pub mod agent_offline_export;
pub mod agent_recovery;
pub mod agent_runtime;
pub mod agent_service;
pub mod backend_error;
pub mod database_capabilities;
pub mod db {
pub use crate::agent_driver;
}