-
Notifications
You must be signed in to change notification settings - Fork 705
Add Git index commit batching #12593
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
LawnGnome
wants to merge
6
commits into
rust-lang:main
Choose a base branch
from
LawnGnome:batch-git-index-commits
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8ced0d0
WIP.
LawnGnome 393c809
Merge remote-tracking branch 'upstream/main' into batch-git-index-com…
LawnGnome 6fc7ac9
Use pubtime correctly.
LawnGnome 80065d6
Back out the `SyncToGitIndex::enqueue_crate` API.
LawnGnome c2e722a
Don't bother exporting `NewGitIndexSyncQueueItem`.
LawnGnome 252b183
Add doc.
LawnGnome File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
crates/crates_io_database/src/models/git_index_sync_queue.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| use crate::schema::git_index_sync_queue; | ||
| use diesel::{prelude::*, sql_types::Integer}; | ||
| use diesel_async::{AsyncPgConnection, RunQueryDsl}; | ||
|
|
||
| #[derive(Debug, HasQuery, QueryableByName)] | ||
| #[diesel(table_name = git_index_sync_queue)] | ||
| pub struct GitIndexSyncQueueItem { | ||
| pub crate_name: String, | ||
| pub created_at: chrono::DateTime<chrono::Utc>, | ||
| } | ||
|
|
||
| #[derive(Debug, Insertable)] | ||
| #[diesel(table_name = git_index_sync_queue, check_for_backend(diesel::pg::Pg))] | ||
| struct NewGitIndexSyncQueueItem<'a> { | ||
| pub crate_name: &'a str, | ||
| } | ||
|
|
||
| impl GitIndexSyncQueueItem { | ||
| /// Queue a crate to be synced. | ||
| /// | ||
| /// If the crate is already in the queue, then nothing happens, successfully. | ||
| pub async fn queue(conn: &mut AsyncPgConnection, crate_name: &str) -> QueryResult<()> { | ||
| diesel::insert_into(git_index_sync_queue::table) | ||
| .values(NewGitIndexSyncQueueItem { crate_name }) | ||
| // It's possible the crate has already been enqueued, in which case we won't change | ||
| // anything, since we want to keep the original creation time. | ||
| .on_conflict_do_nothing() | ||
| .execute(conn) | ||
| .await?; | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| /// Fetch the oldest crates from the queue, deleting them as we go. | ||
| /// | ||
| /// It's likely that you'll want to use this in a transaction so this can rolled back if | ||
| /// downstream processing fails. | ||
| pub async fn fetch_batch( | ||
| conn: &mut AsyncPgConnection, | ||
| limit: i32, | ||
| ) -> QueryResult<Vec<GitIndexSyncQueueItem>> { | ||
| diesel::sql_query(include_str!("git_index_sync_queue_fetch_batch.sql")) | ||
| .bind::<Integer, _>(limit) | ||
| .load(conn) | ||
| .await | ||
| } | ||
| } |
18 changes: 18 additions & 0 deletions
18
crates/crates_io_database/src/models/git_index_sync_queue_fetch_batch.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| WITH | ||
| batch AS ( | ||
| SELECT | ||
| crate_name | ||
| FROM | ||
| git_index_sync_queue | ||
| ORDER BY | ||
| created_at ASC | ||
| FOR UPDATE | ||
| LIMIT | ||
| $1 | ||
| ) | ||
| DELETE FROM git_index_sync_queue USING batch | ||
| WHERE | ||
| git_index_sync_queue.crate_name = batch.crate_name | ||
| RETURNING | ||
| git_index_sync_queue.crate_name, | ||
| git_index_sync_queue.created_at; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
migrations/2025-11-07-202524_create_git_index_sync_queue/down.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| -- Drop the Git index sync queue table. | ||
| DROP TABLE IF EXISTS git_index_sync_queue; |
14 changes: 14 additions & 0 deletions
14
migrations/2025-11-07-202524_create_git_index_sync_queue/up.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| -- Create a table to batch pending syncs to the Git crate index. | ||
| CREATE TABLE git_index_sync_queue ( | ||
| crate_name TEXT PRIMARY KEY NOT NULL, | ||
| created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP | ||
| ); | ||
|
|
||
| COMMENT ON TABLE git_index_sync_queue IS 'Queue for crates that need to be synced to the Git index'; | ||
|
|
||
| COMMENT ON COLUMN git_index_sync_queue.crate_name IS 'The name of the crate to be synced'; | ||
|
|
||
| COMMENT ON COLUMN git_index_sync_queue.created_at IS 'Timestamp when the sync was queued'; | ||
|
|
||
| -- Index for efficient batch processing (oldest first). | ||
| CREATE INDEX idx_git_index_sync_queue_created_at ON git_index_sync_queue (created_at); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a note:
using a postgres table as a queue like this has one problem:
if our handler code fails, the entries are still removed from the queue.
Also, multiple workers arent supported.
If these things are an issue, you could do it the same way we do it in docs.rs:
SELECT [...] LIMIT 100 FOR UPDATE SKIP LOCKEDDELETEthe entriesThrough this,