chore(test): blob lifecyclc with thumbnail cleanup
renable thumbnail test, ensure that blob lifecycle correctly
trigger thumbnail cleanup on blob deletion
need to call `/api/admin/internal/trigger-gc?force=true`
This commit is contained in:
@@ -1895,6 +1895,27 @@ impl DedupService {
|
||||
/// The grace window and reference cross-checks together make the sweep safe
|
||||
/// against a concurrent uploader re-referencing a just-orphaned chunk.
|
||||
pub async fn garbage_collect(&self) -> Result<(u64, u64), DomainError> {
|
||||
self.garbage_collect_with_grace(Self::GC_ORPHAN_GRACE_SECS)
|
||||
.await
|
||||
}
|
||||
|
||||
/// Test-only variant that bypasses the orphan grace window — used by
|
||||
/// `POST /api/admin/internal/trigger-gc?force=true` so the
|
||||
/// integration suite can reap just-orphaned blobs synchronously
|
||||
/// (waiting out the production 1 h grace inside a test run is a
|
||||
/// non-starter). Drops the same rows the regular sweep would, just
|
||||
/// without the time floor. Unsafe under concurrent uploads because
|
||||
/// it reopens the TOCTOU window the grace closes — only the
|
||||
/// admin-internal route, itself gated by
|
||||
/// `OXICLOUD_ENABLE_ADMIN_INTERNAL_ENDPOINTS`, may reach here.
|
||||
pub async fn garbage_collect_force(&self) -> Result<(u64, u64), DomainError> {
|
||||
self.garbage_collect_with_grace(0).await
|
||||
}
|
||||
|
||||
async fn garbage_collect_with_grace(
|
||||
&self,
|
||||
grace_secs: i64,
|
||||
) -> Result<(u64, u64), DomainError> {
|
||||
const BATCH_SIZE: i64 = 500;
|
||||
|
||||
let mut total_deleted = 0u64;
|
||||
@@ -2001,7 +2022,7 @@ impl DedupService {
|
||||
RETURNING hash, size",
|
||||
)
|
||||
.bind(BATCH_SIZE)
|
||||
.bind(Self::GC_ORPHAN_GRACE_SECS as i32)
|
||||
.bind(grace_secs as i32)
|
||||
.fetch_all(self.maintenance_pool.as_ref())
|
||||
.await
|
||||
.map_err(|e| DomainError::internal_error("Dedup", format!("GC blobs: {e}")))?;
|
||||
|
||||
@@ -2085,6 +2085,22 @@ pub async fn internal_trigger_sweep(
|
||||
.into_response()
|
||||
}
|
||||
|
||||
/// Query parameters for `POST /api/admin/internal/trigger-gc`.
|
||||
///
|
||||
/// `force=true` bypasses the orphan-grace window so the sweep reaps
|
||||
/// just-orphaned blobs in the same call. Without this, a blob orphaned
|
||||
/// less than `GC_ORPHAN_GRACE_SECS` (1 h) ago survives the sweep — the
|
||||
/// grace exists so a concurrent uploader pinning a just-orphaned chunk
|
||||
/// can't race the row-delete → file-unlink gap. Integration tests
|
||||
/// don't have concurrent uploaders, so the test runner sets
|
||||
/// `force=true` to make the sweep deterministic within a test's
|
||||
/// runtime.
|
||||
#[derive(Debug, serde::Deserialize, Default)]
|
||||
pub struct InternalTriggerGcQuery {
|
||||
#[serde(default)]
|
||||
pub force: bool,
|
||||
}
|
||||
|
||||
/// `POST /api/admin/internal/trigger-gc` — run the blob garbage
|
||||
/// collector synchronously.
|
||||
///
|
||||
@@ -2092,10 +2108,12 @@ pub async fn internal_trigger_sweep(
|
||||
/// to the orphan-grace window) and their on-disk content. Same call
|
||||
/// as the inline post-purge GC and the periodic blob-GC sweep — just
|
||||
/// exposed under an admin route so Hurl can wait for it
|
||||
/// deterministically.
|
||||
/// deterministically. Add `?force=true` to bypass the grace window —
|
||||
/// see [`InternalTriggerGcQuery`].
|
||||
#[utoipa::path(
|
||||
post,
|
||||
path = "/api/admin/internal/trigger-gc",
|
||||
params(("force" = Option<bool>, Query, description = "Bypass the orphan-grace window (test-only)")),
|
||||
responses(
|
||||
(status = 200, description = "GC ran"),
|
||||
(status = 401, description = "Unauthorized"),
|
||||
@@ -2108,6 +2126,7 @@ pub async fn internal_trigger_sweep(
|
||||
pub async fn internal_trigger_gc(
|
||||
State(state): State<Arc<AppState>>,
|
||||
headers: HeaderMap,
|
||||
Query(query): Query<InternalTriggerGcQuery>,
|
||||
) -> axum::response::Response {
|
||||
use axum::response::IntoResponse;
|
||||
if !state.core.config.features.enable_admin_internal_endpoints {
|
||||
@@ -2116,13 +2135,19 @@ pub async fn internal_trigger_gc(
|
||||
if let Err(e) = admin_guard(&state, &headers).await {
|
||||
return e.into_response();
|
||||
}
|
||||
match state.core.dedup_service.garbage_collect().await {
|
||||
let result = if query.force {
|
||||
state.core.dedup_service.garbage_collect_force().await
|
||||
} else {
|
||||
state.core.dedup_service.garbage_collect().await
|
||||
};
|
||||
match result {
|
||||
Ok((blobs_deleted, bytes_freed)) => (
|
||||
StatusCode::OK,
|
||||
Json(serde_json::json!({
|
||||
"ok": true,
|
||||
"blobs_deleted": blobs_deleted,
|
||||
"bytes_freed": bytes_freed,
|
||||
"forced": query.force,
|
||||
})),
|
||||
)
|
||||
.into_response(),
|
||||
|
||||
Reference in New Issue
Block a user