forge
Explore
Log in

Don't you dare go Hollow

The registry sweep keeps blob links made within an hour

Browse files at this commit

806ecd800169edc0786f8271d53e02fdf6c0798eAhmet Kaan GümüşParent: 913ae53
  • fix
    • the registry sweep keeps blob links made within an hour
Show full message
🐛 fix: the registry sweep keeps blob links made within an hour
crates/forge-core/src/store/oci.rsmodified+21 -6
@@ -30,9 +30,16 @@ impl Store {
3030 /// package already has changes nothing.
3131 pub async fn oci_link_blob(&self, package_id: &str, digest: &str, size: u64) -> Result<()> {
3232 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.
3335 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)",
3643 (
3744 package_id.to_string(),
3845 digest.to_string(),
@@ -289,17 +296,25 @@ impl Store {
289296 }
290297
291298 /// 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.
293303 /// 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>> {
295309 let unreferenced = {
296310 let conn = self.read_conn().await;
297311 fetch_all(
298312 &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 \
300315 (SELECT r.digest FROM oci_manifest_refs r JOIN oci_manifests m \
301316 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()),
303318 |row| col_text(row, 0),
304319 )
305320 .await?
crates/forge-core/tests/store_basic.rsmodified+37 -0
@@ -567,3 +567,40 @@ async fn set_username_display_changes_only_capitalization() {
567567 let still = store.user_by_id(&user.id).await.unwrap().expect("found");
568568 assert_eq!(still.username, "Tahinli");
569569 }
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 @@
22 //! tag is deleted, never on a timer.
33 //!
44 //! 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.
89 //!
910 //! One process-wide lock serializes the sweep with the links and the manifest writes that depend
1011 //! on the blobs they check, so a sweep never removes bytes between a check and the link after it.
1112
13+use std::time::Duration;
14+
1215 use tokio::sync::{Mutex, MutexGuard};
1316
1417 use super::blobs::blob_key;
@@ -16,6 +19,10 @@ use crate::state::AppState;
1619
1720 static LOCK: Mutex<()> = Mutex::const_new(());
1821
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+
1926 /// Takes the registry's garbage collection lock.
2027 pub async fn lock() -> MutexGuard<'static, ()> {
2128 LOCK.lock().await
@@ -25,7 +32,8 @@ pub async fn lock() -> MutexGuard<'static, ()> {
2532 /// digests that no package links any more. Returns how many digests were dropped.
2633 pub async fn sweep(state: &AppState, package_id: &str) -> forge_core::Result<usize> {
2734 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?;
2937 for digest in &dropped {
3038 if state.store.oci_digest_linked(digest).await? {
3139 continue;