fix(export): keep MySQL database export snapshots alive
This commit is contained in:
17 files changed
+290
-35
No files matched your search
@@ -1481,6 +1481,12 @@ pub async fn export_database_sql_core(
|
||||
request: &DatabaseExportRequest,
|
||||
on_progress: impl Fn(ExportProgress) + Sync,
|
||||
) -> Result<(), String> {
|
||||
let _snapshot_keep_alive = if let Some(snapshot_session_id) = request.snapshot_session_id.as_deref() {
|
||||
Some(crate::query::keep_manual_transaction_alive(state, snapshot_session_id).await?)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
// Emit immediately so the UI is never blank while we list schema metadata.
|
||||
emit_database_export_running(&on_progress, &request.export_id, "", 0, 0, 0, true);
|
||||
|
||||
|
||||
@@ -3676,6 +3676,63 @@ async fn begin_transaction_session(
|
||||
Ok(txn_session_id)
|
||||
}
|
||||
|
||||
pub struct ManualTransactionKeepAlive {
|
||||
task: tokio::task::JoinHandle<()>,
|
||||
sessions: Arc<tokio::sync::RwLock<std::collections::HashMap<String, TransactionSession>>>,
|
||||
txn_session_id: String,
|
||||
}
|
||||
|
||||
impl Drop for ManualTransactionKeepAlive {
|
||||
fn drop(&mut self) {
|
||||
self.task.abort();
|
||||
spawn_txn_idle_watcher_for_sessions(Arc::clone(&self.sessions), self.txn_session_id.clone());
|
||||
}
|
||||
}
|
||||
|
||||
/// Keep an existing transaction session alive while a caller prepares work for
|
||||
/// that session. The caller must retain the returned guard for the full period;
|
||||
/// dropping it restores the normal five-minute idle rollback behavior.
|
||||
pub async fn keep_manual_transaction_alive(
|
||||
state: &AppState,
|
||||
txn_session_id: &str,
|
||||
) -> Result<ManualTransactionKeepAlive, String> {
|
||||
{
|
||||
let mut sessions = state.transaction_sessions.write().await;
|
||||
let session = sessions.get_mut(txn_session_id).ok_or_else(|| {
|
||||
"Transaction session not found or expired; it may have been auto-rolled back due to inactivity".to_string()
|
||||
})?;
|
||||
if !session.busy {
|
||||
session.last_activity = std::time::Instant::now();
|
||||
}
|
||||
}
|
||||
|
||||
let sessions = Arc::clone(&state.transaction_sessions);
|
||||
let keep_alive_sessions = Arc::clone(&sessions);
|
||||
let txn_session_id = txn_session_id.to_string();
|
||||
let keep_alive_txn_session_id = txn_session_id.clone();
|
||||
let task = tokio::spawn(async move {
|
||||
const KEEPALIVE_INTERVAL: Duration = Duration::from_secs(30);
|
||||
loop {
|
||||
tokio::time::sleep(KEEPALIVE_INTERVAL).await;
|
||||
let should_continue = {
|
||||
let mut guard = sessions.write().await;
|
||||
if let Some(session) = guard.get_mut(&keep_alive_txn_session_id) {
|
||||
if !session.busy {
|
||||
session.last_activity = std::time::Instant::now();
|
||||
}
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
};
|
||||
if !should_continue {
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
Ok(ManualTransactionKeepAlive { task, sessions: keep_alive_sessions, txn_session_id })
|
||||
}
|
||||
|
||||
/// Execute SQL within an existing manual transaction session.
|
||||
pub async fn execute_in_manual_transaction(
|
||||
state: &AppState,
|
||||
@@ -3950,6 +4007,13 @@ async fn rollback_manual_txn_connection(conn: &mut TxnConnection) -> Result<(),
|
||||
/// will see a missing session or a non-expired one and exit harmlessly.
|
||||
fn spawn_txn_idle_watcher(state: &AppState, txn_session_id: String) {
|
||||
let sessions = Arc::clone(&state.transaction_sessions);
|
||||
spawn_txn_idle_watcher_for_sessions(sessions, txn_session_id);
|
||||
}
|
||||
|
||||
fn spawn_txn_idle_watcher_for_sessions(
|
||||
sessions: Arc<tokio::sync::RwLock<std::collections::HashMap<String, TransactionSession>>>,
|
||||
txn_session_id: String,
|
||||
) {
|
||||
tokio::spawn(async move {
|
||||
const TXN_IDLE_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(300);
|
||||
tokio::time::sleep(TXN_IDLE_TIMEOUT).await;
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
use dbx_core::connection::AppState;
|
||||
use dbx_core::database_export::{begin_database_backup_snapshot_core, export_database_sql_core, DatabaseExportRequest};
|
||||
use dbx_core::models::connection::{ConnectionConfig, DatabaseType};
|
||||
use dbx_core::query::{
|
||||
begin_manual_transaction, commit_manual_transaction, execute_in_manual_transaction, rollback_manual_transaction,
|
||||
};
|
||||
use dbx_core::storage::Storage;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
fn live_config(prefix: &str, db_type: DatabaseType, default_port: u16) -> ConnectionConfig {
|
||||
let host = std::env::var(format!("{prefix}_HOST")).expect("live DB host env var");
|
||||
@@ -103,3 +105,48 @@ async fn live_manual_transaction_mysql_streams_with_row_limit() {
|
||||
rollback_manual_transaction(&state, &txn).await.expect("rollback");
|
||||
let _ = std::fs::remove_file(db_path);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[ignore = "requires DBX_LIVE_MANUAL_TXN_MYSQL_* env vars pointing at readable MySQL with table t_0001"]
|
||||
async fn live_mysql_database_backup_refreshes_an_idle_snapshot_before_export() {
|
||||
let config = live_config("DBX_LIVE_MANUAL_TXN_MYSQL", DatabaseType::Mysql, 3306);
|
||||
let database = config.database.clone().expect("database");
|
||||
let (state, db_path) = app_state_with_config(config.clone()).await;
|
||||
let export_path = std::env::temp_dir().join(format!("dbx-live-backup-{}.sql", uuid::Uuid::new_v4().simple()));
|
||||
|
||||
let snapshot = begin_database_backup_snapshot_core(&state, &config.id, &database).await.expect("begin snapshot");
|
||||
{
|
||||
let mut sessions = state.transaction_sessions.write().await;
|
||||
sessions.get_mut(&snapshot.session_id).expect("snapshot session").last_activity =
|
||||
Instant::now() - Duration::from_secs(301);
|
||||
}
|
||||
|
||||
let request = DatabaseExportRequest {
|
||||
export_id: format!("live-mysql-backup-{}", uuid::Uuid::new_v4().simple()),
|
||||
connection_id: config.id.clone(),
|
||||
database: database.clone(),
|
||||
schema: database.clone(),
|
||||
file_path: export_path.to_string_lossy().to_string(),
|
||||
selected_tables: vec!["t_0001".to_string()],
|
||||
excluded_tables: Vec::new(),
|
||||
include_structure: true,
|
||||
include_data: true,
|
||||
include_objects: false,
|
||||
include_create_database: false,
|
||||
drop_table_if_exists: false,
|
||||
omit_auto_increment: false,
|
||||
fail_on_error: true,
|
||||
snapshot_session_id: Some(snapshot.session_id.clone()),
|
||||
batch_size: 100,
|
||||
};
|
||||
let export_result = export_database_sql_core(&state, &request, |_| {}).await;
|
||||
let rollback_result = rollback_manual_transaction(&state, &snapshot.session_id).await;
|
||||
|
||||
export_result.expect("export through refreshed snapshot");
|
||||
rollback_result.expect("rollback snapshot");
|
||||
let sql = std::fs::read_to_string(&export_path).expect("read exported SQL");
|
||||
assert!(sql.contains("t_0001"));
|
||||
|
||||
let _ = std::fs::remove_file(export_path);
|
||||
let _ = std::fs::remove_file(db_path);
|
||||
}
|
||||
Reference in new issue
Block a user