-
Notifications
You must be signed in to change notification settings - Fork 0
Add installable toolchains with persisted state #51
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
16 changes: 16 additions & 0 deletions
16
backend/migrations/20260718000001_installed_toolchain_images.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,16 @@ | ||
| -- Toolchains a user has installed (pulled + warmed) from the UI, so the set | ||
| -- to prewarm is no longer env-only (RUNNER_PREPULL_IMAGES). The runner Docker | ||
| -- daemon is a single deployment-global resource, so this table is global (no | ||
| -- workspace_id) — an install is actioned/audited under the calling workspace's | ||
| -- content.write, but the pulled image is shared. `toolchain_key` is the catalog | ||
| -- alias (e.g. 'rust'); `image` is its resolved -latest reference. `error` holds | ||
| -- a static category on a failed pull, never raw daemon text. | ||
| CREATE TABLE installed_toolchain_images ( | ||
| toolchain_key TEXT PRIMARY KEY, | ||
| image TEXT NOT NULL, | ||
| status TEXT NOT NULL DEFAULT 'pending' | ||
| CHECK (status IN ('pending', 'installed', 'failed')), | ||
| error TEXT, | ||
| created_at TIMESTAMPTZ NOT NULL DEFAULT now(), | ||
| updated_at TIMESTAMPTZ NOT NULL DEFAULT now() | ||
| ); | ||
14 changes: 14 additions & 0 deletions
14
backend/migrations/20260718000002_installed_toolchain_images_status_index.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 @@ | ||
| -- Partial index matching `list_active_images` (db/toolchain_images.rs), which | ||
| -- runs on every hosted-runner Docker reconnect (30-60 s during an outage) to | ||
| -- build the prewarm set. A partial index over just the active rows keeps that | ||
| -- lookup index-only regardless of table size. | ||
| -- | ||
| -- Note: today the table is bounded to the toolchain catalog (toolchain_key is | ||
| -- the primary key and only catalog keys are ever inserted, so ~10 rows max), so | ||
| -- the planner may still seq-scan it — the index is defensive/forward-looking, | ||
| -- not a hot-path necessity at current cardinality. Added as its own migration | ||
| -- because migrations are immutable once applied (editing the create-table | ||
| -- migration would fail sqlx's startup checksum check). | ||
| CREATE INDEX IF NOT EXISTS installed_toolchain_images_active_idx | ||
| ON installed_toolchain_images (status) | ||
| WHERE status IN ('installed', 'pending'); |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| //! Access to `installed_toolchain_images` — the persisted set of toolchain | ||
| //! images a user has installed from the UI. The set is global (one runner | ||
| //! Docker daemon per deployment); `list_active_images` feeds the provisioner's | ||
| //! prewarm union so installed toolchains re-warm on every reconnect. Audit | ||
| //! entries for install/uninstall are written by the handler (they carry the | ||
| //! calling workspace + actor); this module only owns the row lifecycle. | ||
|
|
||
| use chrono::{DateTime, Utc}; | ||
| use sqlx::PgPool; | ||
|
|
||
| /// One installed-toolchain row. | ||
| #[derive(Debug, sqlx::FromRow)] | ||
| pub struct InstalledToolchainRow { | ||
| pub toolchain_key: String, | ||
| #[allow(dead_code)] | ||
| pub image: String, | ||
| pub status: String, | ||
| pub error: Option<String>, | ||
| #[allow(dead_code)] | ||
| pub created_at: DateTime<Utc>, | ||
| #[allow(dead_code)] | ||
| pub updated_at: DateTime<Utc>, | ||
| } | ||
|
|
||
| /// Every install row (any status) — for the catalog read. | ||
| pub async fn list(pool: &PgPool) -> sqlx::Result<Vec<InstalledToolchainRow>> { | ||
| sqlx::query_as::<_, InstalledToolchainRow>( | ||
| "SELECT toolchain_key, image, status, error, created_at, updated_at \ | ||
| FROM installed_toolchain_images", | ||
| ) | ||
| .fetch_all(pool) | ||
| .await | ||
| } | ||
|
|
||
| /// Images to prewarm: installed, plus pending (a pull already in flight — warm | ||
| /// it too so a reconnect during install doesn't drop it). | ||
| pub async fn list_active_images(pool: &PgPool) -> sqlx::Result<Vec<String>> { | ||
| let rows: Vec<(String,)> = sqlx::query_as( | ||
| "SELECT image FROM installed_toolchain_images \ | ||
| WHERE status IN ('installed', 'pending')", | ||
| ) | ||
| .fetch_all(pool) | ||
| .await?; | ||
| Ok(rows.into_iter().map(|(image,)| image).collect()) | ||
| } | ||
|
|
||
| /// Mark a toolchain as install-pending (idempotent: a re-install or retry | ||
| /// resets an existing row to pending and clears any prior error). | ||
| pub async fn upsert_pending(pool: &PgPool, key: &str, image: &str) -> sqlx::Result<()> { | ||
| sqlx::query( | ||
| "INSERT INTO installed_toolchain_images (toolchain_key, image, status) \ | ||
| VALUES ($1, $2, 'pending') \ | ||
| ON CONFLICT (toolchain_key) DO UPDATE \ | ||
| SET image = EXCLUDED.image, status = 'pending', error = NULL, updated_at = now()", | ||
| ) | ||
| .bind(key) | ||
| .bind(image) | ||
| .execute(pool) | ||
| .await?; | ||
| Ok(()) | ||
| } | ||
|
|
||
| /// Flip a row to installed after a successful pull. Guards on the row still | ||
| /// existing (a race with uninstall drops the update harmlessly). | ||
| pub async fn mark_installed(pool: &PgPool, key: &str) -> sqlx::Result<()> { | ||
| sqlx::query( | ||
| "UPDATE installed_toolchain_images \ | ||
| SET status = 'installed', error = NULL, updated_at = now() \ | ||
| WHERE toolchain_key = $1", | ||
| ) | ||
| .bind(key) | ||
| .execute(pool) | ||
| .await?; | ||
| Ok(()) | ||
| } | ||
|
|
||
| /// Flip a row to failed with a static error category. | ||
| pub async fn mark_failed(pool: &PgPool, key: &str, error: &str) -> sqlx::Result<()> { | ||
| sqlx::query( | ||
| "UPDATE installed_toolchain_images \ | ||
| SET status = 'failed', error = $2, updated_at = now() \ | ||
| WHERE toolchain_key = $1", | ||
| ) | ||
| .bind(key) | ||
| .bind(error) | ||
| .execute(pool) | ||
| .await?; | ||
| Ok(()) | ||
| } | ||
|
|
||
| /// Delete a row (uninstall); returns the removed image so the caller can rmi it. | ||
| pub async fn delete(pool: &PgPool, key: &str) -> sqlx::Result<Option<String>> { | ||
| let row: Option<(String,)> = sqlx::query_as( | ||
| "DELETE FROM installed_toolchain_images WHERE toolchain_key = $1 RETURNING image", | ||
| ) | ||
| .bind(key) | ||
| .fetch_optional(pool) | ||
| .await?; | ||
| Ok(row.map(|(image,)| image)) | ||
| } |
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.
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.
🛑 Performance Regression: Missing index on
statuscolumn causes N+1 query on every reconnect.The
list_active_imagesfunction indb/toolchain_images.rsfilters bystatus IN ('installed', 'pending')without an index. This query executes on every Docker daemon reconnect (every 30-60s during outages) and could block the reconnect loop on large datasets. Add index before the table goes to production.