fix: 修复实时日志与数据库切换可靠性
Axolotl desktop CI / guardrails (push) Has been cancelled
Axolotl desktop CI / desktop (macos-latest) (push) Has been cancelled
Axolotl desktop CI / desktop (ubuntu-latest) (push) Has been cancelled
Axolotl desktop CI / desktop (windows-latest) (push) Has been cancelled
Axolotl desktop CI / website (push) Has been cancelled
Repository checks / typos (push) Has been cancelled
Repository checks / tombi (push) Has been cancelled
Rust checks / shear (push) Has been cancelled
Sync source to CNB / Sync Git ref (push) Has been cancelled
Sync LobeHub models / sync (push) Has been cancelled

This commit is contained in:
Disy committed 2026-09-27 14:18:25 +08:00
1 parent 2efaa4fb35
commit ce0f41c973
30 files changed
+814 -461

No files matched your search

+133 -24
View File
@@ -4,7 +4,7 @@ use sqlx::migrate::{Migration, Migrator};
use sqlx::sqlite::{
SqliteConnectOptions, SqliteJournalMode, SqlitePoolOptions,
};
use sqlx::{Pool, Sqlite};
use sqlx::{ConnectOptions, Connection, Pool, Sqlite};
use std::path::{Path, PathBuf};
use std::time::Duration;
@@ -126,17 +126,7 @@ pub async fn copy_release_database_to_beta(
.into());
}
let release_pool = open_app_db_pool(&release_path).await?;
let escaped_path = beta_path.to_string_lossy().replace('\'', "''");
sqlx::query(&format!("VACUUM INTO '{escaped_path}'"))
.execute(&release_pool)
.await?;
release_pool.close().await;
if let Err(error) = open_app_db_pool(&beta_path).await {
let _ = tokio::fs::remove_file(&beta_path).await;
return Err(error);
}
copy_app_database(&release_path, &beta_path).await?;
tracing::info!(
source = %release_path.display(),
@@ -207,16 +197,7 @@ pub async fn copy_database_between_channels(
}
crate::util::io::create_dir_all(&target_dir).await?;
let temporary_path = target_dir.join(format!("{LEGACY_APP_DB_FILE}.tmp"));
let source_pool = open_app_db_pool(&source_path).await?;
let escaped_path = temporary_path.to_string_lossy().replace('\'', "''");
let result = sqlx::query(&format!("VACUUM INTO '{escaped_path}'"))
.execute(&source_pool)
.await;
source_pool.close().await;
result?;
tokio::fs::rename(&temporary_path, &target_path).await?;
copy_app_database(&source_path, &target_path).await?;
tracing::info!(
source = %source_path.display(),
destination = %target_path.display(),
@@ -225,6 +206,87 @@ pub async fn copy_database_between_channels(
Ok(())
}
async fn copy_app_database(source: &Path, target: &Path) -> crate::Result<()> {
let temporary = sidecar_path(target, ".copying");
remove_database_files(&temporary).await?;
let source_pool = open_app_db_pool(source).await?;
let escaped_path = temporary.to_string_lossy().replace('\'', "''");
let snapshot_result = sqlx::query(&format!("VACUUM INTO '{escaped_path}'"))
.execute(&source_pool)
.await;
source_pool.close().await;
if let Err(error) = snapshot_result {
let _ = remove_database_files(&temporary).await;
return Err(error.into());
}
if let Err(error) = verify_database_file(&temporary).await {
let _ = remove_database_files(&temporary).await;
return Err(error);
}
let previous = sidecar_path(target, ".replacing");
remove_database_files(&previous).await?;
let had_target = target.try_exists()?;
if had_target {
super::db_backup::archive_database_files(target, &previous).await?;
} else {
super::db_backup::remove_database_sidecars(target).await?;
}
if let Err(error) = tokio::fs::rename(&temporary, target).await {
if had_target {
super::db_backup::restore_archived_database_files(
&previous, target,
)
.await?;
}
let _ = remove_database_files(&temporary).await;
return Err(error.into());
}
if let Err(error) = remove_database_files(&previous).await {
tracing::warn!(
database = %previous.display(),
error = %error,
"Copied channel database but could not remove the previous snapshot"
);
}
Ok(())
}
async fn verify_database_file(path: &Path) -> crate::Result<()> {
let options = SqliteConnectOptions::new()
.filename(path)
.busy_timeout(Duration::from_secs(30))
.read_only(true)
.create_if_missing(false);
let mut connection = options.connect().await?;
let rows = sqlx::query_scalar::<_, String>("PRAGMA quick_check(1)")
.fetch_all(&mut connection)
.await?;
connection.close().await?;
if rows.as_slice() != ["ok"] {
return Err(crate::ErrorKind::FSError(format!(
"Database snapshot {} failed its integrity check: {}",
path.display(),
rows.join("; ")
))
.into());
}
Ok(())
}
async fn remove_database_files(database: &Path) -> crate::Result<()> {
match tokio::fs::remove_file(database).await {
Ok(()) => {}
Err(error) if error.kind() == std::io::ErrorKind::NotFound => {}
Err(error) => return Err(error.into()),
}
super::db_backup::remove_database_sidecars(database).await
}
pub async fn backup_current_app_db_for_update(
app_identifier: &str,
target_version: &str,
@@ -1461,6 +1523,53 @@ mod tests {
const INSTANCE_POST_UPGRADE_NOTICES_MIGRATION_VERSION: i64 = 20260824000000;
const MCARCHIVE_PROVIDER_FILE_ID_MIGRATION_VERSION: i64 = 20260823020000;
#[tokio::test]
async fn channel_copy_replaces_database_and_removes_stale_sidecars() {
let temp = tempfile::tempdir().unwrap();
let source = temp.path().join("beta").join(LEGACY_APP_DB_FILE);
let target = temp.path().join("release").join(LEGACY_APP_DB_FILE);
tokio::fs::create_dir_all(source.parent().unwrap())
.await
.unwrap();
tokio::fs::create_dir_all(target.parent().unwrap())
.await
.unwrap();
for (path, marker) in [(&source, "beta"), (&target, "release")] {
let pool = open_app_db_pool(path).await.unwrap();
sqlx::query("CREATE TABLE marker (value TEXT NOT NULL)")
.execute(&pool)
.await
.unwrap();
sqlx::query("INSERT INTO marker (value) VALUES (?)")
.bind(marker)
.execute(&pool)
.await
.unwrap();
pool.close().await;
}
tokio::fs::write(sidecar_path(&target, "-wal"), b"stale wal")
.await
.unwrap();
tokio::fs::write(sidecar_path(&target, "-shm"), b"stale shm")
.await
.unwrap();
copy_app_database(&source, &target).await.unwrap();
assert!(!sidecar_path(&target, "-wal").exists());
assert!(!sidecar_path(&target, "-shm").exists());
let pool = open_app_db_pool(&target).await.unwrap();
let marker: String = sqlx::query_scalar("SELECT value FROM marker")
.fetch_one(&pool)
.await
.unwrap();
pool.close().await;
assert_eq!(marker, "beta");
assert!(!sidecar_path(&target, ".copying").exists());
assert!(!sidecar_path(&target, ".replacing").exists());
}
#[tokio::test]
async fn post_upgrade_notices_migrate_fresh_and_existing_databases() {
for previous_schema in [false, true] {
@@ -3956,7 +4065,7 @@ mod tests {
assert_eq!(
translation,
(
"microsoft".to_string(),
"google".to_string(),
String::new(),
String::new(),
String::new(),
@@ -4065,7 +4174,7 @@ mod tests {
assert_eq!(
translation,
(
"microsoft".to_string(),
"google".to_string(),
String::new(),
String::new(),
"https://api.openai.com/v1".to_string(),
+91 -3
View File
@@ -39,6 +39,10 @@ async fn restore_corrupt_app_db_from(
"App database integrity check failed"
);
if restore_app_db_without_corrupt_sidecars(db_path, backup_dir).await? {
return Ok(());
}
let Some(backup_path) = latest_healthy_app_db_backup(backup_dir).await?
else {
return Err(crate::ErrorKind::FSError(format!(
@@ -98,6 +102,49 @@ async fn restore_corrupt_app_db_from(
Ok(())
}
async fn restore_app_db_without_corrupt_sidecars(
db_path: &Path,
backup_dir: &Path,
) -> crate::Result<bool> {
let mut has_sidecars = false;
for suffix in ["-wal", "-shm"] {
has_sidecars |= sqlite_sidecar_path(db_path, suffix).try_exists()?;
}
if !has_sidecars {
return Ok(false);
}
let staging_path = next_restore_staging_path(db_path).await?;
tokio::fs::copy(db_path, &staging_path).await?;
let healthy_main = matches!(
check_database_integrity(&staging_path).await?,
IntegrityStatus::Healthy
) && is_app_database(&staging_path).await?;
if !healthy_main {
cleanup_staged_database(&staging_path).await;
return Ok(false);
}
crate::util::io::create_dir_all(backup_dir).await?;
let corrupt_path = next_corrupt_database_path(backup_dir).await?;
if let Err(error) = archive_database_files(db_path, &corrupt_path).await {
cleanup_staged_database(&staging_path).await;
return Err(error);
}
if let Err(error) = tokio::fs::rename(&staging_path, db_path).await {
restore_archived_database_files(&corrupt_path, db_path).await?;
cleanup_staged_database(&staging_path).await;
return Err(error.into());
}
tracing::warn!(
database = %db_path.display(),
corrupt_archive = %corrupt_path.display(),
"Recovered app database by removing corrupt SQLite sidecars"
);
Ok(true)
}
async fn check_database_integrity(
db_path: &Path,
) -> crate::Result<IntegrityStatus> {
@@ -200,7 +247,7 @@ async fn is_app_database(db_path: &Path) -> crate::Result<bool> {
Ok(table_count == REQUIRED_APP_DB_TABLES.len() as i64)
}
async fn archive_database_files(
pub(crate) async fn archive_database_files(
db_path: &Path,
archive_path: &Path,
) -> crate::Result<()> {
@@ -226,7 +273,7 @@ async fn archive_database_files(
Ok(())
}
async fn restore_archived_database_files(
pub(crate) async fn restore_archived_database_files(
archive_path: &Path,
db_path: &Path,
) -> crate::Result<()> {
@@ -285,7 +332,9 @@ fn sqlite_sidecar_path(db_path: &Path, suffix: &str) -> PathBuf {
PathBuf::from(path)
}
async fn remove_database_sidecars(db_path: &Path) -> crate::Result<()> {
pub(crate) async fn remove_database_sidecars(
db_path: &Path,
) -> crate::Result<()> {
for suffix in ["-wal", "-shm"] {
let path = sqlite_sidecar_path(db_path, suffix);
match tokio::fs::remove_file(path).await {
@@ -766,6 +815,45 @@ mod tests {
assert_eq!(tokio::fs::read(&db_path).await.unwrap(), corrupt_bytes);
}
#[tokio::test]
async fn corrupt_sidecars_recover_from_the_healthy_main_database() {
let temp = tempfile::tempdir().unwrap();
let db_path = temp.path().join("release").join("app.db");
let backup_dir =
temp.path().join("Backups").join("app-db").join("release");
tokio::fs::create_dir_all(db_path.parent().unwrap())
.await
.unwrap();
create_test_app_db(&db_path, "release").await;
tokio::fs::write(sqlite_sidecar_path(&db_path, "-wal"), b"bad wal")
.await
.unwrap();
tokio::fs::write(sqlite_sidecar_path(&db_path, "-shm"), b"bad shm")
.await
.unwrap();
assert!(
restore_app_db_without_corrupt_sidecars(&db_path, &backup_dir)
.await
.unwrap()
);
assert_eq!(read_marker(&db_path).await, "release");
assert!(!sqlite_sidecar_path(&db_path, "-wal").exists());
assert!(!sqlite_sidecar_path(&db_path, "-shm").exists());
let mut entries = tokio::fs::read_dir(&backup_dir).await.unwrap();
let mut archived_database = false;
let mut archived_wal = false;
let mut archived_shm = false;
while let Some(entry) = entries.next_entry().await.unwrap() {
let name = entry.file_name().to_string_lossy().into_owned();
archived_database |= name.ends_with(".db");
archived_wal |= name.ends_with(".db-wal");
archived_shm |= name.ends_with(".db-shm");
}
assert!(archived_database && archived_wal && archived_shm);
}
#[tokio::test]
async fn update_backup_creates_a_healthy_channel_snapshot() {
let temp = tempfile::tempdir().unwrap();
@@ -16,6 +16,12 @@ use chrono::{DateTime, TimeZone, Utc};
use sqlx::{Executor, Row, Sqlite, SqlitePool, Transaction};
use uuid::Uuid;
async fn begin_content_write(
pool: &SqlitePool,
) -> crate::Result<Transaction<'static, Sqlite>> {
Ok(pool.begin_with("BEGIN IMMEDIATE").await?)
}
/// Ensures the instance a content write is about to reference still exists.
///
/// Runs inside the write transaction so a concurrent instance deletion that
@@ -662,7 +668,7 @@ pub(crate) async fn upsert_instance_file_from_parts(
input: UpsertInstanceFile<'_>,
pool: &SqlitePool,
) -> crate::Result<InstanceFile> {
let mut tx = pool.begin().await?;
let mut tx = begin_content_write(pool).await?;
let file =
upsert_instance_file_from_parts_in_transaction(input, &mut tx).await?;
tx.commit().await?;
@@ -726,7 +732,7 @@ pub(crate) async fn rename_instance_file(
enabled: bool,
pool: &SqlitePool,
) -> crate::Result<Option<InstanceFile>> {
let mut tx = pool.begin().await?;
let mut tx = begin_content_write(pool).await?;
let file = rename_instance_file_in_transaction(
instance_id,
@@ -1106,7 +1112,7 @@ pub(crate) async fn upsert_content_entry_from_parts(
input: UpsertContentEntry<'_>,
pool: &SqlitePool,
) -> crate::Result<ContentEntry> {
let mut tx = pool.begin().await?;
let mut tx = begin_content_write(pool).await?;
let entry =
upsert_content_entry_from_parts_in_transaction(input, &mut tx).await?;
tx.commit().await?;
@@ -1230,7 +1236,7 @@ pub(crate) async fn upsert_content_provider_ref(
origin: bool,
pool: &SqlitePool,
) -> crate::Result<()> {
let mut tx = pool.begin().await?;
let mut tx = begin_content_write(pool).await?;
upsert_content_provider_ref_in_transaction(
content_entry_id,
provider_ref,
@@ -2583,6 +2589,7 @@ mod tests {
provider TEXT NOT NULL,
provider_project_id TEXT NOT NULL,
provider_release_id TEXT NULL,
provider_file_id TEXT NULL,
is_origin INTEGER NOT NULL DEFAULT 0
);
CREATE UNIQUE INDEX instance_content_provider_refs_identity
@@ -539,7 +539,7 @@ pub(crate) async fn persist_resolved_plan_dependency_edges(
.chain(plan.dependencies.iter())
.collect::<Vec<_>>();
let mut tx = state.pool.begin().await?;
let mut tx = begin_content_write(&state.pool).await?;
for (index, dependency) in plan.dependencies.iter().enumerate() {
let Some(parent) = dependency
.dependent_on_version_id
@@ -254,8 +254,8 @@ mod tests {
instance_rows::get_instance_by_id(&instance.id, &state.pool)
.await?
.expect("instance row");
let canonical_root = minecraft.path().canonicalize()?;
let canonical_json = json_path.canonicalize()?;
let canonical_root = crate::util::io::canonicalize(minecraft.path())?;
let canonical_json = crate::util::io::canonicalize(&json_path)?;
assert_eq!(stored.linked_launcher.as_deref(), Some("generic"));
assert_eq!(
stored.linked_launcher_root.as_deref(),
+119 -14
View File
@@ -117,8 +117,21 @@ async fn record_post_upgrade_launch_best_effort(
}
struct LogRingBuffer {
lines: VecDeque<String>,
lines: VecDeque<SequencedLogLine>,
byte_len: usize,
last_sequence: u64,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SequencedLogLine {
pub sequence: u64,
pub message: String,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct LiveLogSnapshot {
pub lines: Vec<SequencedLogLine>,
pub last_sequence: u64,
}
impl LogRingBuffer {
@@ -126,25 +139,37 @@ impl LogRingBuffer {
Self {
lines: VecDeque::new(),
byte_len: 0,
last_sequence: 0,
}
}
fn push(&mut self, line: String) {
let line_len = line.len();
fn push(&mut self, message: String) -> u64 {
self.last_sequence = self.last_sequence.saturating_add(1);
let sequence = self.last_sequence;
let line = SequencedLogLine { sequence, message };
let line_len = line.message.len();
while self.lines.len() >= LOG_BUFFER_CAPACITY
|| self.byte_len.saturating_add(line_len) > LOG_BUFFER_BYTE_CAPACITY
{
let Some(removed) = self.lines.pop_front() else {
break;
};
self.byte_len = self.byte_len.saturating_sub(removed.len());
self.byte_len = self.byte_len.saturating_sub(removed.message.len());
}
self.byte_len += line_len;
self.lines.push_back(line);
sequence
}
fn get_all(&self) -> Vec<String> {
self.lines.iter().cloned().collect()
self.lines.iter().map(|line| line.message.clone()).collect()
}
fn snapshot(&self) -> LiveLogSnapshot {
LiveLogSnapshot {
lines: self.lines.iter().cloned().collect(),
last_sequence: self.last_sequence,
}
}
fn clear(&mut self) {
@@ -157,11 +182,24 @@ static LOG_BUFFERS: LazyLock<DashMap<String, LogRingBuffer>> =
LazyLock::new(DashMap::new);
pub fn push_log_line(instance_id: &str, line: String) {
let line = truncate_live_log_text(&line);
LOG_BUFFERS
push_log_lines(instance_id, std::iter::once(line));
}
fn push_log_lines(
instance_id: &str,
lines: impl IntoIterator<Item = String>,
) -> Option<(u64, u64)> {
let mut buffer = LOG_BUFFERS
.entry(instance_id.to_string())
.or_insert_with(LogRingBuffer::new)
.push(line);
.or_insert_with(LogRingBuffer::new);
let mut first_sequence = None;
let mut last_sequence = 0;
for line in lines {
let sequence = buffer.push(truncate_live_log_text(&line));
first_sequence.get_or_insert(sequence);
last_sequence = sequence;
}
first_sequence.map(|first| (first, last_sequence))
}
fn truncate_live_log_text(value: &str) -> String {
@@ -228,6 +266,16 @@ pub fn get_log_buffer(instance_id: &str) -> Vec<String> {
.unwrap_or_default()
}
pub fn get_live_log_snapshot(instance_id: &str) -> LiveLogSnapshot {
LOG_BUFFERS
.get(instance_id)
.map(|buffer| buffer.snapshot())
.unwrap_or(LiveLogSnapshot {
lines: Vec::new(),
last_sequence: 0,
})
}
pub fn clear_log_buffer(instance_id: &str) {
if let Some(mut buf) = LOG_BUFFERS.get_mut(instance_id) {
buf.clear();
@@ -920,14 +968,20 @@ impl Process {
event.throwable =
event.throwable.as_deref().map(truncate_live_log_text);
let mut lines = Vec::new();
if let Some(formatted) = Self::format_log4j_entry(&event) {
push_log_line(instance_id, formatted.trim_end().to_string());
lines.push(formatted.trim_end().to_string());
}
if let Some(ref throwable) = event.throwable {
for line in throwable.lines().filter(|l| !l.is_empty()) {
push_log_line(instance_id, line.to_string());
lines.push(line.to_string());
}
}
let Some((first_sequence, last_sequence)) =
push_log_lines(instance_id, lines)
else {
return;
};
#[cfg(feature = "tauri")]
{
@@ -936,6 +990,8 @@ impl Process {
"log",
LogPayload {
instance_id: instance_id.to_string(),
first_sequence,
last_sequence,
event: LogEvent::Log4j(event),
},
);
@@ -943,13 +999,17 @@ impl Process {
}
#[cfg(not(feature = "tauri"))]
{
let _ = (instance_id, event);
let _ = (instance_id, event, first_sequence, last_sequence);
}
}
fn emit_legacy_log(instance_id: &str, message: &str) {
let message = truncate_live_log_text(message);
push_log_line(instance_id, message.clone());
let Some((first_sequence, last_sequence)) =
push_log_lines(instance_id, std::iter::once(message.clone()))
else {
return;
};
#[cfg(feature = "tauri")]
{
@@ -958,6 +1018,8 @@ impl Process {
"log",
LogPayload {
instance_id: instance_id.to_string(),
first_sequence,
last_sequence,
event: LogEvent::Legacy { message },
},
);
@@ -965,7 +1027,7 @@ impl Process {
}
#[cfg(not(feature = "tauri"))]
{
let _ = (instance_id, message);
let _ = (instance_id, message, first_sequence, last_sequence);
}
}
@@ -1312,6 +1374,49 @@ fn take_next_log4j_frame(buffer: &mut String) -> Option<String> {
mod post_upgrade_tests {
use super::*;
#[test]
fn live_log_snapshot_sequences_batches_and_preserves_position_after_clear()
{
let instance_id = Uuid::new_v4().to_string();
assert_eq!(
push_log_lines(
&instance_id,
["first".to_string(), "second".to_string()]
),
Some((1, 2))
);
assert_eq!(
get_live_log_snapshot(&instance_id),
LiveLogSnapshot {
lines: vec![
SequencedLogLine {
sequence: 1,
message: "first".to_string(),
},
SequencedLogLine {
sequence: 2,
message: "second".to_string(),
},
],
last_sequence: 2,
}
);
clear_log_buffer(&instance_id);
push_log_line(&instance_id, "third".to_string());
assert_eq!(
get_live_log_snapshot(&instance_id),
LiveLogSnapshot {
lines: vec![SequencedLogLine {
sequence: 3,
message: "third".to_string(),
}],
last_sequence: 3,
}
);
remove_log_buffer(&instance_id);
}
#[cfg(windows)]
#[tokio::test]
async fn waiting_for_one_process_leaves_registry_available() {