fix(jdbc): keep query pagination cursor sessions
This commit is contained in:
1 parent
73f4762b8d
commit
593635a94d
8 files changed
+462
-10
No files matched your search
@@ -1223,6 +1223,11 @@ impl AppState {
|
||||
close_removed_pools_in_background(removed);
|
||||
}
|
||||
|
||||
pub async fn remove_external_driver_pools(&self, driver_id: &str) {
|
||||
let removed = self.drain_external_driver_pools(driver_id).await;
|
||||
close_removed_pools(removed).await;
|
||||
}
|
||||
|
||||
async fn drain_connection_pools(&self, connection_id: &str) -> Vec<(String, PoolKind)> {
|
||||
let pool_prefix = format!("{connection_id}:");
|
||||
let keys_to_remove: Vec<String> = self
|
||||
@@ -1248,6 +1253,30 @@ impl AppState {
|
||||
removed
|
||||
}
|
||||
|
||||
async fn drain_external_driver_pools(&self, driver_id: &str) -> Vec<(String, PoolKind)> {
|
||||
let keys_to_remove: Vec<String> = self
|
||||
.connections
|
||||
.read()
|
||||
.await
|
||||
.iter()
|
||||
.filter_map(|(key, pool)| match pool {
|
||||
PoolKind::ExternalDriver { driver_id: pool_driver_id, .. } if pool_driver_id == driver_id => {
|
||||
Some(key.clone())
|
||||
}
|
||||
_ => None,
|
||||
})
|
||||
.collect();
|
||||
self.stop_keepalive_tasks(&keys_to_remove).await;
|
||||
let mut conns = self.connections.write().await;
|
||||
let mut removed = Vec::with_capacity(keys_to_remove.len());
|
||||
for key in keys_to_remove {
|
||||
if let Some(pool) = conns.remove(&key) {
|
||||
removed.push((key, pool));
|
||||
}
|
||||
}
|
||||
removed
|
||||
}
|
||||
|
||||
async fn uses_forwarded_transport(&self, connection_id: &str) -> bool {
|
||||
let configs = self.configs.read().await;
|
||||
configs.get(connection_id).is_some_and(|config| config.has_effective_transport_layers())
|
||||
@@ -1429,7 +1458,9 @@ pub async fn close_pool_kind(pool: PoolKind) {
|
||||
let _ = client.disconnect().await;
|
||||
}
|
||||
PoolKind::ExternalTabular(_) => {}
|
||||
PoolKind::ExternalDriver { .. } => {}
|
||||
PoolKind::ExternalDriver { session, .. } => {
|
||||
session.shutdown().await;
|
||||
}
|
||||
PoolKind::MessageQueue => {}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -330,6 +330,10 @@ impl PluginDriverSession {
|
||||
let _ = process.child.kill().await;
|
||||
}
|
||||
|
||||
pub async fn shutdown(&self) {
|
||||
self.kill().await;
|
||||
}
|
||||
|
||||
pub async fn pid(&self) -> Option<u32> {
|
||||
let process = self.process.lock().await;
|
||||
process.child.id()
|
||||
@@ -509,7 +513,10 @@ fn resolve_plugin_executable(plugin_dir: &Path, executable: &str) -> PathBuf {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{read_decoded_plugin_response, read_plugin_line, InstalledPlugin, PluginManifest};
|
||||
use super::{
|
||||
read_decoded_plugin_response, read_plugin_line, InstalledPlugin, PluginDriverManifest, PluginDriverSession,
|
||||
PluginManifest, PluginRuntimeEnv,
|
||||
};
|
||||
use std::path::PathBuf;
|
||||
use tokio::io::BufReader;
|
||||
|
||||
@@ -547,4 +554,59 @@ mod tests {
|
||||
|
||||
assert_eq!(result["ok"], true);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
#[tokio::test]
|
||||
async fn shutdown_kills_plugin_child_process() {
|
||||
let dir = std::env::temp_dir().join(format!("dbx-plugin-shutdown-test-{}", uuid::Uuid::new_v4()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let executable = dir.join("plugin.sh");
|
||||
std::fs::write(&executable, "#!/bin/sh\nsleep 30\n").unwrap();
|
||||
{
|
||||
use std::os::unix::fs::PermissionsExt;
|
||||
|
||||
let mut permissions = std::fs::metadata(&executable).unwrap().permissions();
|
||||
permissions.set_mode(0o755);
|
||||
std::fs::set_permissions(&executable, permissions).unwrap();
|
||||
}
|
||||
let plugin = InstalledPlugin {
|
||||
manifest: PluginManifest {
|
||||
id: "jdbc".to_string(),
|
||||
name: "JDBC".to_string(),
|
||||
version: "test".to_string(),
|
||||
protocol_version: 1,
|
||||
description: String::new(),
|
||||
executable: Some("plugin.sh".to_string()),
|
||||
drivers: vec![PluginDriverManifest {
|
||||
id: "jdbc".to_string(),
|
||||
label: "JDBC".to_string(),
|
||||
kind: "external".to_string(),
|
||||
database_type: Some("jdbc".to_string()),
|
||||
}],
|
||||
},
|
||||
path: dir.clone(),
|
||||
};
|
||||
|
||||
let session = PluginDriverSession::start(plugin, "jdbc".to_string(), PluginRuntimeEnv::default())
|
||||
.await
|
||||
.expect("session should start");
|
||||
let pid = session.pid().await.expect("child should have a pid");
|
||||
|
||||
session.shutdown().await;
|
||||
tokio::time::sleep(std::time::Duration::from_millis(50)).await;
|
||||
|
||||
assert!(!process_exists(pid));
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
fn process_exists(pid: u32) -> bool {
|
||||
std::process::Command::new("kill")
|
||||
.arg("-0")
|
||||
.arg(pid.to_string())
|
||||
.stderr(std::process::Stdio::null())
|
||||
.status()
|
||||
.map(|status| status.success())
|
||||
.unwrap_or(false)
|
||||
}
|
||||
}
|
||||
@@ -1033,9 +1033,22 @@ pub async fn do_execute(
|
||||
let plugin_timeout = query_timeout;
|
||||
drop(connections);
|
||||
wait_for_query_opt(cancel_token, query_timeout, async move {
|
||||
let params =
|
||||
external_driver_query_params(config.as_ref(), &sql, &database, schema.as_deref(), &options);
|
||||
session.invoke_with_timeout::<db::QueryResult>("executeQuery", params, plugin_timeout).await
|
||||
if let Some(session_id) = options.result_session_id.as_deref() {
|
||||
let params = external_driver_fetch_query_page_params(
|
||||
config.as_ref(),
|
||||
session_id,
|
||||
options.page_size.unwrap_or(MAX_ROWS),
|
||||
);
|
||||
session.invoke_with_timeout::<db::QueryResult>("fetchQueryPage", params, plugin_timeout).await
|
||||
} else if options.page_size.is_some() {
|
||||
let params =
|
||||
external_driver_query_params(config.as_ref(), &sql, &database, schema.as_deref(), &options);
|
||||
session.invoke_with_timeout::<db::QueryResult>("executeQueryPage", params, plugin_timeout).await
|
||||
} else {
|
||||
let params =
|
||||
external_driver_query_params(config.as_ref(), &sql, &database, schema.as_deref(), &options);
|
||||
session.invoke_with_timeout::<db::QueryResult>("executeQuery", params, plugin_timeout).await
|
||||
}
|
||||
})
|
||||
.await
|
||||
.map(|result| normalize_query_result_for_js(truncate_result_with_max_rows(result, max_rows)))
|
||||
@@ -1063,9 +1076,24 @@ fn external_driver_query_params(
|
||||
if let Some(timeout_secs) = options.timeout_secs {
|
||||
params["timeoutSecs"] = serde_json::json!(timeout_secs);
|
||||
}
|
||||
if let Some(page_size) = options.page_size {
|
||||
params["pageSize"] = serde_json::json!(page_size);
|
||||
}
|
||||
params
|
||||
}
|
||||
|
||||
fn external_driver_fetch_query_page_params(
|
||||
config: &crate::models::connection::ConnectionConfig,
|
||||
session_id: &str,
|
||||
page_size: usize,
|
||||
) -> serde_json::Value {
|
||||
serde_json::json!({
|
||||
"connection": config,
|
||||
"sessionId": session_id,
|
||||
"pageSize": page_size,
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn execute_sql_statement(
|
||||
state: &AppState,
|
||||
connection_id: &str,
|
||||
@@ -1242,6 +1270,16 @@ pub async fn close_query_session(
|
||||
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)
|
||||
.await
|
||||
.map(|value| value.get("ok").and_then(|ok| ok.as_bool()).unwrap_or(false))
|
||||
}
|
||||
_ => Ok(false),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user