diff --git a/crates/forge-core/src/store/oci.rs b/crates/forge-core/src/store/oci.rs index 0335564..61acb16 100644 --- a/crates/forge-core/src/store/oci.rs +++ b/crates/forge-core/src/store/oci.rs @@ -30,9 +30,16 @@ impl Store { /// package already has changes nothing. pub async fn oci_link_blob(&self, package_id: &str, digest: &str, size: u64) -> Result<()> { let conn = self.inner.conn.lock().await; + // A link made again starts its grace period over: the sweep keeps a recent link (see + // `oci_sweep_unreferenced`), and the push that made it is still on its way to a manifest. conn.execute( - "INSERT OR IGNORE INTO oci_blobs (package_id, digest, size, created_at) \ - VALUES (?1, ?2, ?3, ?4)", + "DELETE FROM oci_blobs WHERE package_id = ?1 AND digest = ?2", + (package_id.to_string(), digest.to_string()), + ) + .await + .map_err(db_err)?; + conn.execute( + "INSERT INTO oci_blobs (package_id, digest, size, created_at) VALUES (?1, ?2, ?3, ?4)", ( package_id.to_string(), digest.to_string(), @@ -289,17 +296,25 @@ impl Store { } /// The sweep of garbage collection for one package (mark and sweep). Drops every blob link of - /// `package_id` that no manifest of the package refers to, and returns the digests dropped. + /// `package_id` that no manifest of the package refers to and that was made before + /// `linked_before`, and returns the digests dropped. A newer link is a push whose manifest is + /// not yet PUT, so it is kept. An open upload session names no digest until it finishes, and + /// finishing links the blob with a fresh stamp, so the grace period covers sessions too. /// The caller deletes the bytes of each digest that no package links any more. - pub async fn oci_sweep_unreferenced(&self, package_id: &str) -> Result> { + pub async fn oci_sweep_unreferenced( + &self, + package_id: &str, + linked_before: &str, + ) -> Result> { let unreferenced = { let conn = self.read_conn().await; fetch_all( &conn, - "SELECT digest FROM oci_blobs WHERE package_id = ?1 AND digest NOT IN \ + "SELECT digest FROM oci_blobs WHERE package_id = ?1 AND created_at < ?2 \ + AND digest NOT IN \ (SELECT r.digest FROM oci_manifest_refs r JOIN oci_manifests m \ ON m.id = r.manifest_id WHERE m.package_id = ?1)", - (package_id.to_string(),), + (package_id.to_string(), linked_before.to_string()), |row| col_text(row, 0), ) .await? diff --git a/crates/forge-core/tests/store_basic.rs b/crates/forge-core/tests/store_basic.rs index 9fe11f8..2ca28c1 100644 --- a/crates/forge-core/tests/store_basic.rs +++ b/crates/forge-core/tests/store_basic.rs @@ -567,3 +567,40 @@ async fn set_username_display_changes_only_capitalization() { let still = store.user_by_id(&user.id).await.unwrap().expect("found"); assert_eq!(still.username, "Tahinli"); } + +#[tokio::test] +async fn oci_sweep_keeps_a_blob_linked_within_the_grace_period() { + let dir = tempfile::tempdir().expect("temp dir"); + let store = open_store(&db_path(&dir)).await; + let user = store + .create_user("tahinli", None, "h", false) + .await + .expect("user"); + let package = store + .ensure_package(&user.id, "oci", "app", "app") + .await + .expect("package"); + let digest = format!("sha256:{}", "a".repeat(64)); + store + .oci_link_blob(&package.id, &digest, 10) + .await + .expect("link"); + + // A link made within the grace period belongs to a push whose manifest is not yet PUT. + let grace_cutoff = forge_core::time::stamp_ago(Duration::from_secs(3600)); + let kept = store + .oci_sweep_unreferenced(&package.id, &grace_cutoff) + .await + .expect("sweep"); + assert!(kept.is_empty(), "a recent link is kept"); + assert!(store.oci_digest_linked(&digest).await.expect("linked")); + + // Once the link is older than the cutoff, the unreferenced digest is dropped. + let later_cutoff = stamp_after(Duration::from_secs(3600)); + let dropped = store + .oci_sweep_unreferenced(&package.id, &later_cutoff) + .await + .expect("sweep"); + assert_eq!(dropped, vec![digest.clone()]); + assert!(!store.oci_digest_linked(&digest).await.expect("linked")); +} diff --git a/crates/forge-web/src/oci/gc.rs b/crates/forge-web/src/oci/gc.rs index d1cc608..a40f43d 100644 --- a/crates/forge-web/src/oci/gc.rs +++ b/crates/forge-web/src/oci/gc.rs @@ -2,13 +2,16 @@ //! tag is deleted, never on a timer. //! //! Mark: the digests a package's manifests refer to. Sweep: the blob links of that package that no -//! manifest refers to are dropped. Then each dropped digest whose bytes no package links any more -//! is deleted from the blob store. A blob pushed but not yet named by a manifest is swept too, so a -//! push that races a delete fails at its manifest with `MANIFEST_BLOB_UNKNOWN` and can be retried. +//! manifest refers to are dropped, unless the link is newer than [`GRACE`] (a push still on its way +//! to its manifest). Then each dropped digest whose bytes no package links any more is deleted from +//! the blob store. A push that outlives the grace fails at its manifest with `MANIFEST_BLOB_UNKNOWN` +//! and can be retried. //! //! One process-wide lock serializes the sweep with the links and the manifest writes that depend //! on the blobs they check, so a sweep never removes bytes between a check and the link after it. +use std::time::Duration; + use tokio::sync::{Mutex, MutexGuard}; use super::blobs::blob_key; @@ -16,6 +19,10 @@ use crate::state::AppState; static LOCK: Mutex<()> = Mutex::const_new(()); +/// How long a blob link is kept while no manifest names it. A push links each blob as it arrives +/// and PUTs its manifest after the last one, so a sweep in that gap must not drop the bytes. +pub const GRACE: Duration = Duration::from_secs(60 * 60); + /// Takes the registry's garbage collection lock. pub async fn lock() -> MutexGuard<'static, ()> { LOCK.lock().await @@ -25,7 +32,8 @@ pub async fn lock() -> MutexGuard<'static, ()> { /// digests that no package links any more. Returns how many digests were dropped. pub async fn sweep(state: &AppState, package_id: &str) -> forge_core::Result { let _guard = lock().await; - let dropped = state.store.oci_sweep_unreferenced(package_id).await?; + let linked_before = forge_core::time::stamp_ago(GRACE); + let dropped = state.store.oci_sweep_unreferenced(package_id, &linked_before).await?; for digest in &dropped { if state.store.oci_digest_linked(digest).await? { continue;