Skip to content

Commit 762e2dd

Browse files
authored
Merge pull request #14076 from Turbo87/cache-tags-backfill
Backfill `cache-tags` metadata onto existing S3 objects
2 parents 33caffc + f1b7d04 commit 762e2dd

13 files changed

Lines changed: 625 additions & 1 deletion

File tree

Cargo.lock

Lines changed: 112 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ aws-credential-types = { version = "=1.2.14", features = ["hardcoded-credentials
6969
# `rustls-webpki` v0.101.x. Using only `default-https-client` avoids this by
7070
# using the modern rustls 0.23 + hyper 1.x stack instead.
7171
aws-sdk-cloudfront = { version = "=1.123.0", default-features = false, features = ["default-https-client", "rt-tokio"] }
72+
aws-sdk-s3 = { version = "=1.137.0", default-features = false, features = ["default-https-client", "rt-tokio"] }
7273
aws-sdk-sqs = { version = "=1.102.0", default-features = false, features = ["default-https-client", "rt-tokio"] }
7374
axum = { version = "=0.8.9", features = ["macros", "matched-path"] }
7475
axum-extra = { version = "=0.12.6", features = ["erased-json", "middleware", "query", "typed-header"] }
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use crate::schema::cache_tags_backfills;
2+
3+
/// Struct used to `INSERT` a completed cache-tags backfill record into the
4+
/// `cache_tags_backfills` table.
5+
///
6+
/// A row records that every S3 object for the crate has been re-copied with
7+
/// `cache-tags` metadata. `crate_id` is `None` only if the crate was deleted
8+
/// after the backfill completed but before the record was written.
9+
#[derive(Debug, diesel::Insertable, bon::Builder)]
10+
#[diesel(table_name = cache_tags_backfills, check_for_backend(diesel::pg::Pg))]
11+
pub struct NewCacheTagsBackfillRow<'a> {
12+
pub crate_id: Option<i32>,
13+
pub crate_name: &'a str,
14+
}

crates/crates_io_database/src/models/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
pub use self::action::{NewVersionOwnerAction, VersionAction, VersionOwnerAction};
2+
pub use self::cache_tags_backfill::NewCacheTagsBackfillRow;
23
pub use self::category::{Category, CrateCategory, NewCategory};
34
pub use self::cloudfront_invalidation_queue::{
45
CloudFrontDistribution, CloudFrontInvalidationQueueItem,
@@ -24,6 +25,7 @@ pub use self::version::{NewVersion, TopVersions, Version};
2425
pub mod helpers;
2526

2627
mod action;
28+
mod cache_tags_backfill;
2729
pub mod category;
2830
mod cloudfront_invalidation_queue;
2931
pub mod crate_owner_invitation;

crates/crates_io_database/src/schema.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,23 @@ diesel::table! {
130130
}
131131
}
132132

133+
diesel::table! {
134+
use diesel::sql_types::*;
135+
use diesel_full_text_search::Tsvector;
136+
137+
/// Records which crates have had their S3 objects backfilled with `cache-tags` metadata. A row exists only for crates whose backfill completed.
138+
cache_tags_backfills (id) {
139+
/// Timestamp when the backfill for this crate completed.
140+
completed_at -> Timestamptz,
141+
/// The backfilled crate, or `NULL` if the crate was deleted after the backfill completed.
142+
crate_id -> Nullable<Int4>,
143+
/// The crate name at backfill time.
144+
crate_name -> Text,
145+
/// Unique identifier for each backfill record.
146+
id -> Int8,
147+
}
148+
}
149+
133150
diesel::table! {
134151
use diesel::sql_types::*;
135152
use diesel_full_text_search::Tsvector;
@@ -1277,6 +1294,7 @@ diesel::table! {
12771294
}
12781295

12791296
diesel::joinable!(api_tokens -> users (user_id));
1297+
diesel::joinable!(cache_tags_backfills -> crates (crate_id));
12801298
diesel::joinable!(crate_downloads -> crates (crate_id));
12811299
diesel::joinable!(crate_owner_invitations -> crates (crate_id));
12821300
diesel::joinable!(crate_owners -> crates (crate_id));
@@ -1313,6 +1331,7 @@ diesel::joinable!(versions_published_by -> versions (version_id));
13131331
diesel::allow_tables_to_appear_in_same_query!(
13141332
api_tokens,
13151333
background_jobs,
1334+
cache_tags_backfills,
13161335
categories,
13171336
cloudfront_invalidation_queue,
13181337
crate_downloads,

crates/crates_io_database_dump/src/dump-db.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ last_retry = "private"
4040
created_at = "private"
4141
priority = "private"
4242

43+
[cache_tags_backfills.columns]
44+
id = "private"
45+
crate_id = "private"
46+
crate_name = "private"
47+
completed_at = "private"
48+
4349
[categories.columns]
4450
id = "public"
4551
category = "public"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP TABLE cache_tags_backfills;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
CREATE TABLE IF NOT EXISTS cache_tags_backfills (
2+
id BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
3+
crate_id INTEGER REFERENCES crates(id) ON DELETE SET NULL,
4+
crate_name TEXT NOT NULL,
5+
completed_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
6+
);
7+
8+
COMMENT ON TABLE cache_tags_backfills IS
9+
'Records which crates have had their S3 objects backfilled with `cache-tags` metadata. A row exists only for crates whose backfill completed.';
10+
COMMENT ON COLUMN cache_tags_backfills.id IS
11+
'Unique identifier for each backfill record.';
12+
COMMENT ON COLUMN cache_tags_backfills.crate_id IS
13+
'The backfilled crate, or `NULL` if the crate was deleted after the backfill completed.';
14+
COMMENT ON COLUMN cache_tags_backfills.crate_name IS
15+
'The crate name at backfill time.';
16+
COMMENT ON COLUMN cache_tags_backfills.completed_at IS
17+
'Timestamp when the backfill for this crate completed.';
18+
19+
-- safety-assured:start
20+
-- Suppresses the "ADD INDEX without CONCURRENTLY" diesel-guard check.
21+
-- The table is brand new and empty, so the unique index builds instantly with
22+
-- no meaningful SHARE lock contention.
23+
CREATE UNIQUE INDEX idx_cache_tags_backfills_crate_id ON cache_tags_backfills (crate_id);
24+
-- safety-assured:end

0 commit comments

Comments
 (0)