aKode fase 5: rinomina interna (crate akode-*, akode-rs/, akode-cli/, binario akode, AKODE_*, ~/.akode, snapshot)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
220
akode-rs/state/src/runtime/reclamation_tests.rs
Normal file
220
akode-rs/state/src/runtime/reclamation_tests.rs
Normal file
@@ -0,0 +1,220 @@
|
||||
use super::*;
|
||||
use crate::migrations::runtime_logs_migrator;
|
||||
use crate::runtime::test_support::unique_temp_dir;
|
||||
use akode_utils_absolute_path::test_support::PathExt;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
#[tokio::test]
|
||||
async fn reclaims_current_and_legacy_logs_with_independent_directory_locks() -> anyhow::Result<()> {
|
||||
let directory = unique_temp_dir();
|
||||
tokio::fs::create_dir_all(&directory).await?;
|
||||
let legacy = SqliteConfig::new_for_testing(directory.as_path().abs());
|
||||
let sqlite = legacy.clone().with_local_logs_home(directory.join("log"));
|
||||
let mut pools = Vec::new();
|
||||
let mut sizes = Vec::new();
|
||||
for config in [&sqlite, &legacy] {
|
||||
let pool = config
|
||||
.open_logs_db(&runtime_logs_migrator(), /*telemetry_override*/ None)
|
||||
.await?;
|
||||
sqlx::query(
|
||||
"WITH RECURSIVE n(i) AS (VALUES(1) UNION ALL SELECT i+1 FROM n WHERE i < 4096) \
|
||||
INSERT INTO logs(ts,ts_nanos,level,target,feedback_log_body) \
|
||||
SELECT unixepoch(),0,'INFO','fixture',printf('%032768d',i) FROM n",
|
||||
)
|
||||
.execute(&pool)
|
||||
.await?;
|
||||
sqlx::query("DELETE FROM logs WHERE id%4 != 0")
|
||||
.execute(&pool)
|
||||
.await?;
|
||||
sqlx::query("PRAGMA wal_checkpoint(TRUNCATE)")
|
||||
.execute(&pool)
|
||||
.await?;
|
||||
sizes.push(tokio::fs::metadata(config.logs_db_path()).await?.len());
|
||||
pools.push(pool);
|
||||
}
|
||||
let rows = "SELECT id, feedback_log_body FROM logs ORDER BY id";
|
||||
let expected: Vec<(i64, String)> = sqlx::query_as(rows).fetch_all(&pools[0]).await?;
|
||||
let mut scheduled = sqlite
|
||||
.reclamation_db_paths()
|
||||
.into_iter()
|
||||
.map(|db| (db, Instant::now(), ReclamationState::default()))
|
||||
.collect::<Vec<_>>();
|
||||
let mut owners = BTreeMap::new();
|
||||
let (_shutdown, receiver) = watch::channel(());
|
||||
|
||||
// A legacy process owns its directory. Current logs must still be reclaimed.
|
||||
let legacy_owner = try_ownership(legacy.home()).await?.unwrap();
|
||||
for (index, config) in [&sqlite, &legacy].into_iter().enumerate() {
|
||||
tokio::time::timeout(Duration::from_secs(10), async {
|
||||
loop {
|
||||
for (_, due, _) in &mut scheduled {
|
||||
*due = Instant::now();
|
||||
}
|
||||
visit(&sqlite, &mut scheduled, &mut owners, &receiver).await;
|
||||
if config.logs_are_on_nfs().await.unwrap()
|
||||
|| tokio::fs::metadata(config.logs_db_path())
|
||||
.await
|
||||
.unwrap()
|
||||
.len()
|
||||
< sizes[index]
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
})
|
||||
.await?;
|
||||
if index == 0 {
|
||||
assert_eq!(
|
||||
tokio::fs::metadata(legacy.logs_db_path()).await?.len(),
|
||||
sizes[1]
|
||||
);
|
||||
legacy_owner.unlock()?;
|
||||
}
|
||||
}
|
||||
for pool in pools {
|
||||
assert_eq!(
|
||||
sqlx::query_as::<_, (i64, String)>(rows)
|
||||
.fetch_all(&pool)
|
||||
.await?,
|
||||
expected
|
||||
);
|
||||
pool.close().await;
|
||||
}
|
||||
drop(owners);
|
||||
drop(legacy_owner);
|
||||
tokio::fs::remove_dir_all(directory).await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn interrupted_reclamation_releases_writer_and_resumes_without_data_loss()
|
||||
-> anyhow::Result<()> {
|
||||
let sqlite = SqliteConfig::new_for_testing(unique_temp_dir().abs());
|
||||
tokio::fs::create_dir_all(sqlite.home()).await?;
|
||||
let pool = sqlite
|
||||
.open_logs_db(&runtime_logs_migrator(), /*telemetry_override*/ None)
|
||||
.await?;
|
||||
sqlx::query(
|
||||
"WITH RECURSIVE n(i) AS (VALUES(1) UNION ALL SELECT i+1 FROM n WHERE i < 4096) \
|
||||
INSERT INTO logs(ts,ts_nanos,level,target,feedback_log_body,thread_id,process_uuid) \
|
||||
SELECT unixepoch(),0,'INFO','fixture',printf('%032768d',i),'thread-'||i,'process-'||(i%17) FROM n",
|
||||
)
|
||||
.execute(&pool)
|
||||
.await?;
|
||||
sqlx::query("DELETE FROM logs WHERE id%4 != 0")
|
||||
.execute(&pool)
|
||||
.await?;
|
||||
sqlx::query("PRAGMA wal_checkpoint(TRUNCATE)")
|
||||
.execute(&pool)
|
||||
.await?;
|
||||
let rows = "SELECT id, feedback_log_body FROM logs ORDER BY id";
|
||||
let mut expected: Vec<(i64, String)> = sqlx::query_as(rows).fetch_all(&pool).await?;
|
||||
let free_before: i64 = sqlx::query_scalar("PRAGMA freelist_count")
|
||||
.fetch_one(&pool)
|
||||
.await?;
|
||||
let (shutdown, receiver) = watch::channel(());
|
||||
let mut connection = pool.acquire().await?.detach();
|
||||
sqlx::raw_sql(
|
||||
"PRAGMA busy_timeout = 0; PRAGMA wal_autocheckpoint = 0; PRAGMA cache_size = -16384;",
|
||||
)
|
||||
.execute(&mut connection)
|
||||
.await?;
|
||||
// Request shutdown inside the first vacuum commit, while SQLite still owns the
|
||||
// writer lock. The production progress handler must stop this pass safely.
|
||||
connection.lock_handle().await?.set_commit_hook(move || {
|
||||
shutdown.send_replace(());
|
||||
true
|
||||
});
|
||||
let interrupted = reclaim_pages(
|
||||
&mut connection,
|
||||
Budget {
|
||||
// Shutdown, rather than machine speed, determines the interruption point.
|
||||
deadline: Some(Instant::now() + Duration::from_secs(30)),
|
||||
pages: PASS_PAGES,
|
||||
},
|
||||
&receiver,
|
||||
)
|
||||
.await?;
|
||||
assert!(receiver.has_changed()?, "must reach the vacuum commit");
|
||||
assert!(interrupted.pages <= BATCH_PAGES);
|
||||
connection.close().await?;
|
||||
let free_after: i64 = sqlx::query_scalar("PRAGMA freelist_count")
|
||||
.fetch_one(&pool)
|
||||
.await?;
|
||||
// An interruption at COMMIT may leave the first batch committed or rolled back.
|
||||
assert!((0..=i64::from(BATCH_PAGES)).contains(&(free_before - free_after)));
|
||||
assert_eq!(
|
||||
sqlx::query_as::<_, (i64, String)>(rows)
|
||||
.fetch_all(&pool)
|
||||
.await?,
|
||||
expected
|
||||
);
|
||||
|
||||
// A foreground write must acquire the lock immediately, without a busy retry.
|
||||
let mut writer = pool.acquire().await?;
|
||||
sqlx::query("PRAGMA busy_timeout = 0")
|
||||
.execute(&mut *writer)
|
||||
.await?;
|
||||
let mut transaction = writer.begin_with("BEGIN IMMEDIATE").await?;
|
||||
expected[0].1 = "foreground write after interruption".to_string();
|
||||
sqlx::query("UPDATE logs SET feedback_log_body = ? WHERE id = ?")
|
||||
.bind(&expected[0].1)
|
||||
.bind(expected[0].0)
|
||||
.execute(&mut *transaction)
|
||||
.await?;
|
||||
transaction.commit().await?;
|
||||
drop(writer);
|
||||
|
||||
// Flush earlier writes so only the resumed pass can shrink the main file.
|
||||
sqlx::query("PRAGMA wal_checkpoint(TRUNCATE)")
|
||||
.execute(&pool)
|
||||
.await?;
|
||||
let bytes_before_retry = tokio::fs::metadata(sqlite.logs_db_path()).await?.len();
|
||||
let free_before_retry: i64 = sqlx::query_scalar("PRAGMA freelist_count")
|
||||
.fetch_one(&pool)
|
||||
.await?;
|
||||
let (_shutdown, receiver) = watch::channel(());
|
||||
let resumed = reclaim(
|
||||
&sqlite,
|
||||
&sqlite.logs_db_path(),
|
||||
Budget {
|
||||
deadline: Some(Instant::now() + Duration::from_secs(30)),
|
||||
pages: BATCH_PAGES,
|
||||
},
|
||||
&receiver,
|
||||
)
|
||||
.await?;
|
||||
let free_after_retry: i64 = sqlx::query_scalar("PRAGMA freelist_count")
|
||||
.fetch_one(&pool)
|
||||
.await?;
|
||||
assert_eq!(
|
||||
free_before_retry - free_after_retry,
|
||||
i64::from(resumed.pages)
|
||||
);
|
||||
let bytes_after_retry = tokio::fs::metadata(sqlite.logs_db_path()).await?.len();
|
||||
if sqlite.is_nfs().await? {
|
||||
assert_eq!(resumed.pages, 0);
|
||||
assert_eq!(bytes_after_retry, bytes_before_retry);
|
||||
} else {
|
||||
assert!(resumed.pages > 0);
|
||||
assert!(
|
||||
bytes_after_retry < bytes_before_retry,
|
||||
"reclamation must shrink the SQLite file: {bytes_before_retry} -> {bytes_after_retry} bytes"
|
||||
);
|
||||
}
|
||||
assert_eq!(
|
||||
sqlx::query_as::<_, (i64, String)>(rows)
|
||||
.fetch_all(&pool)
|
||||
.await?,
|
||||
expected
|
||||
);
|
||||
assert_eq!(
|
||||
sqlx::query_scalar::<_, String>("PRAGMA integrity_check")
|
||||
.fetch_all(&pool)
|
||||
.await?,
|
||||
vec!["ok"]
|
||||
);
|
||||
pool.close().await;
|
||||
tokio::fs::remove_dir_all(sqlite.home()).await?;
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user