The registry sweep keeps blob links made within an hour
Browse files at this commit806ecd800169edc0786f8271d53e02fdf6c0798eParent: 913ae53- fix
- the registry sweep keeps blob links made within an hour
3 changed files +70 -10
- crates/forge-core/src/store/oci.rs +21 -6
- crates/forge-core/tests/store_basic.rs +37 -0
- crates/forge-web/src/oci/gc.rs +12 -4
crates/forge-core/src/store/oci.rsmodified+21 -6
| @@ -30,9 +30,16 @@ impl Store { | ||
| 30 | 30 | /// package already has changes nothing. |
| 31 | 31 | pub async fn oci_link_blob(&self, package_id: &str, digest: &str, size: u64) -> Result<()> { |
| 32 | 32 | let conn = self.inner.conn.lock().await; |
| 33 | + // A link made again starts its grace period over: the sweep keeps a recent link (see | |
| 34 | + // `oci_sweep_unreferenced`), and the push that made it is still on its way to a manifest. | |
| 33 | 35 | conn.execute( |
| 34 | - "INSERT OR IGNORE INTO oci_blobs (package_id, digest, size, created_at) \ | |
| 35 | - VALUES (?1, ?2, ?3, ?4)", | |
| 36 | + "DELETE FROM oci_blobs WHERE package_id = ?1 AND digest = ?2", | |
| 37 | + (package_id.to_string(), digest.to_string()), | |
| 38 | + ) | |
| 39 | + .await | |
| 40 | + .map_err(db_err)?; | |
| 41 | + conn.execute( | |
| 42 | + "INSERT INTO oci_blobs (package_id, digest, size, created_at) VALUES (?1, ?2, ?3, ?4)", | |
| 36 | 43 | ( |
| 37 | 44 | package_id.to_string(), |
| 38 | 45 | digest.to_string(), |
| @@ -289,17 +296,25 @@ impl Store { | ||
| 289 | 296 | } |
| 290 | 297 | |
| 291 | 298 | /// The sweep of garbage collection for one package (mark and sweep). Drops every blob link of |
| 292 | - /// `package_id` that no manifest of the package refers to, and returns the digests dropped. | |
| 299 | + /// `package_id` that no manifest of the package refers to and that was made before | |
| 300 | + /// `linked_before`, and returns the digests dropped. A newer link is a push whose manifest is | |
| 301 | + /// not yet PUT, so it is kept. An open upload session names no digest until it finishes, and | |
| 302 | + /// finishing links the blob with a fresh stamp, so the grace period covers sessions too. | |
| 293 | 303 | /// The caller deletes the bytes of each digest that no package links any more. |
| 294 | - pub async fn oci_sweep_unreferenced(&self, package_id: &str) -> Result<Vec<String>> { | |
| 304 | + pub async fn oci_sweep_unreferenced( | |
| 305 | + &self, | |
| 306 | + package_id: &str, | |
| 307 | + linked_before: &str, | |
| 308 | + ) -> Result<Vec<String>> { | |
| 295 | 309 | let unreferenced = { |
| 296 | 310 | let conn = self.read_conn().await; |
| 297 | 311 | fetch_all( |
| 298 | 312 | &conn, |
| 299 | - "SELECT digest FROM oci_blobs WHERE package_id = ?1 AND digest NOT IN \ | |
| 313 | + "SELECT digest FROM oci_blobs WHERE package_id = ?1 AND created_at < ?2 \ | |
| 314 | + AND digest NOT IN \ | |
| 300 | 315 | (SELECT r.digest FROM oci_manifest_refs r JOIN oci_manifests m \ |
| 301 | 316 | ON m.id = r.manifest_id WHERE m.package_id = ?1)", |
| 302 | - (package_id.to_string(),), | |
| 317 | + (package_id.to_string(), linked_before.to_string()), | |
| 303 | 318 | |row| col_text(row, 0), |
| 304 | 319 | ) |
| 305 | 320 | .await? |
crates/forge-core/tests/store_basic.rsmodified+37 -0
| @@ -567,3 +567,40 @@ async fn set_username_display_changes_only_capitalization() { | ||
| 567 | 567 | let still = store.user_by_id(&user.id).await.unwrap().expect("found"); |
| 568 | 568 | assert_eq!(still.username, "Tahinli"); |
| 569 | 569 | } |
| 570 | + | |
| 571 | +#[tokio::test] | |
| 572 | +async fn oci_sweep_keeps_a_blob_linked_within_the_grace_period() { | |
| 573 | + let dir = tempfile::tempdir().expect("temp dir"); | |
| 574 | + let store = open_store(&db_path(&dir)).await; | |
| 575 | + let user = store | |
| 576 | + .create_user("tahinli", None, "h", false) | |
| 577 | + .await | |
| 578 | + .expect("user"); | |
| 579 | + let package = store | |
| 580 | + .ensure_package(&user.id, "oci", "app", "app") | |
| 581 | + .await | |
| 582 | + .expect("package"); | |
| 583 | + let digest = format!("sha256:{}", "a".repeat(64)); | |
| 584 | + store | |
| 585 | + .oci_link_blob(&package.id, &digest, 10) | |
| 586 | + .await | |
| 587 | + .expect("link"); | |
| 588 | + | |
| 589 | + // A link made within the grace period belongs to a push whose manifest is not yet PUT. | |
| 590 | + let grace_cutoff = forge_core::time::stamp_ago(Duration::from_secs(3600)); | |
| 591 | + let kept = store | |
| 592 | + .oci_sweep_unreferenced(&package.id, &grace_cutoff) | |
| 593 | + .await | |
| 594 | + .expect("sweep"); | |
| 595 | + assert!(kept.is_empty(), "a recent link is kept"); | |
| 596 | + assert!(store.oci_digest_linked(&digest).await.expect("linked")); | |
| 597 | + | |
| 598 | + // Once the link is older than the cutoff, the unreferenced digest is dropped. | |
| 599 | + let later_cutoff = stamp_after(Duration::from_secs(3600)); | |
| 600 | + let dropped = store | |
| 601 | + .oci_sweep_unreferenced(&package.id, &later_cutoff) | |
| 602 | + .await | |
| 603 | + .expect("sweep"); | |
| 604 | + assert_eq!(dropped, vec![digest.clone()]); | |
| 605 | + assert!(!store.oci_digest_linked(&digest).await.expect("linked")); | |
| 606 | +} |
crates/forge-web/src/oci/gc.rsmodified+12 -4
| @@ -2,13 +2,16 @@ | ||
| 2 | 2 | //! tag is deleted, never on a timer. |
| 3 | 3 | //! |
| 4 | 4 | //! Mark: the digests a package's manifests refer to. Sweep: the blob links of that package that no |
| 5 | -//! manifest refers to are dropped. Then each dropped digest whose bytes no package links any more | |
| 6 | -//! is deleted from the blob store. A blob pushed but not yet named by a manifest is swept too, so a | |
| 7 | -//! push that races a delete fails at its manifest with `MANIFEST_BLOB_UNKNOWN` and can be retried. | |
| 5 | +//! manifest refers to are dropped, unless the link is newer than [`GRACE`] (a push still on its way | |
| 6 | +//! to its manifest). Then each dropped digest whose bytes no package links any more is deleted from | |
| 7 | +//! the blob store. A push that outlives the grace fails at its manifest with `MANIFEST_BLOB_UNKNOWN` | |
| 8 | +//! and can be retried. | |
| 8 | 9 | //! |
| 9 | 10 | //! One process-wide lock serializes the sweep with the links and the manifest writes that depend |
| 10 | 11 | //! on the blobs they check, so a sweep never removes bytes between a check and the link after it. |
| 11 | 12 | |
| 13 | +use std::time::Duration; | |
| 14 | + | |
| 12 | 15 | use tokio::sync::{Mutex, MutexGuard}; |
| 13 | 16 | |
| 14 | 17 | use super::blobs::blob_key; |
| @@ -16,6 +19,10 @@ use crate::state::AppState; | ||
| 16 | 19 | |
| 17 | 20 | static LOCK: Mutex<()> = Mutex::const_new(()); |
| 18 | 21 | |
| 22 | +/// How long a blob link is kept while no manifest names it. A push links each blob as it arrives | |
| 23 | +/// and PUTs its manifest after the last one, so a sweep in that gap must not drop the bytes. | |
| 24 | +pub const GRACE: Duration = Duration::from_secs(60 * 60); | |
| 25 | + | |
| 19 | 26 | /// Takes the registry's garbage collection lock. |
| 20 | 27 | pub async fn lock() -> MutexGuard<'static, ()> { |
| 21 | 28 | LOCK.lock().await |
| @@ -25,7 +32,8 @@ pub async fn lock() -> MutexGuard<'static, ()> { | ||
| 25 | 32 | /// digests that no package links any more. Returns how many digests were dropped. |
| 26 | 33 | pub async fn sweep(state: &AppState, package_id: &str) -> forge_core::Result<usize> { |
| 27 | 34 | let _guard = lock().await; |
| 28 | - let dropped = state.store.oci_sweep_unreferenced(package_id).await?; | |
| 35 | + let linked_before = forge_core::time::stamp_ago(GRACE); | |
| 36 | + let dropped = state.store.oci_sweep_unreferenced(package_id, &linked_before).await?; | |
| 29 | 37 | for digest in &dropped { |
| 30 | 38 | if state.store.oci_digest_linked(digest).await? { |
| 31 | 39 | continue; |