- Notifications
You must be signed in to change notification settings - Fork948
feat: prioritize human-initiated workspace builds over prebuilds in queue#18882
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
blink-so wants to merge1 commit intomainChoose a base branch fromfeat/priority-queue-human-over-prebuilds
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.
Uh oh!
There was an error while loading.Please reload this page.
Open
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
5 changes: 5 additions & 0 deletionscoderd/database/migrations/000247_provisioner_job_priority.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,5 @@ | ||
-- Remove the priority-based index | ||
DROP INDEX IF EXISTS idx_provisioner_jobs_priority_created_at; | ||
-- Remove the priority column | ||
ALTER TABLE provisioner_jobs DROP COLUMN IF EXISTS priority; |
14 changes: 14 additions & 0 deletionscoderd/database/migrations/000247_provisioner_job_priority.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 @@ | ||
-- Add priority column to provisioner_jobs table to support prioritizing human-initiated jobs over prebuilds | ||
ALTER TABLE provisioner_jobs ADD COLUMN priority integer NOT NULL DEFAULT 0; | ||
-- Create index for efficient priority-based ordering | ||
CREATE INDEX idx_provisioner_jobs_priority_created_at ON provisioner_jobs (organization_id, started_at, priority DESC, created_at ASC) WHERE started_at IS NULL; | ||
-- Update existing jobs to set priority based on whether they are prebuilds | ||
-- Priority 1 = human-initiated jobs, Priority 0 = prebuilds | ||
UPDATE provisioner_jobs | ||
SET priority = CASE | ||
WHEN initiator_id = 'c42fdf75-3097-471c-8c33-fb52454d81c0' THEN 0 -- PrebuildsSystemUserID | ||
ELSE 1 -- Human-initiated | ||
END | ||
WHERE started_at IS NULL; -- Only update pending jobs |
1 change: 1 addition & 0 deletionscoderd/database/queries/provisionerjobs.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
1 change: 1 addition & 0 deletionscoderd/provisionerjobs.go
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
100 changes: 100 additions & 0 deletionscoderd/wsbuilder/priority_test.go
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 @@ | ||
package wsbuilder_test | ||
import ( | ||
"database/sql" | ||
"encoding/json" | ||
"testing" | ||
"time" | ||
"github.com/google/uuid" | ||
"github.com/stretchr/testify/require" | ||
"github.com/sqlc-dev/pqtype" | ||
"github.com/coder/coder/v2/coderd/coderdtest" | ||
"github.com/coder/coder/v2/coderd/database" | ||
"github.com/coder/coder/v2/coderd/database/dbtestutil" | ||
"github.com/coder/coder/v2/testutil" | ||
) | ||
func TestPriorityQueue(t *testing.T) { | ||
t.Parallel() | ||
db, ps := dbtestutil.NewDB(t) | ||
client := coderdtest.New(t, &coderdtest.Options{ | ||
IncludeProvisionerDaemon: true, | ||
Database: db, | ||
Pubsub: ps, | ||
}) | ||
owner := coderdtest.CreateFirstUser(t, client) | ||
// Create a template | ||
version := coderdtest.CreateTemplateVersion(t, client, owner.OrganizationID, nil) | ||
coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID) | ||
ctx := testutil.Context(t, testutil.WaitMedium) | ||
// Test priority setting by directly creating provisioner jobs | ||
// Create a human-initiated job | ||
humanJob, err := db.InsertProvisionerJob(ctx, database.InsertProvisionerJobParams{ | ||
ID: uuid.New(), | ||
CreatedAt: time.Now(), | ||
UpdatedAt: time.Now(), | ||
InitiatorID: owner.UserID, | ||
OrganizationID: owner.OrganizationID, | ||
Provisioner: database.ProvisionerTypeEcho, | ||
Type: database.ProvisionerJobTypeWorkspaceBuild, | ||
StorageMethod: database.ProvisionerStorageMethodFile, | ||
FileID: uuid.New(), | ||
Input: json.RawMessage(`{}`), | ||
Tags: database.StringMap{}, | ||
TraceMetadata: pqtype.NullRawMessage{}, | ||
Priority: 1, // Human-initiated should have priority 1 | ||
}) | ||
require.NoError(t, err) | ||
// Create a prebuild job | ||
prebuildJob, err := db.InsertProvisionerJob(ctx, database.InsertProvisionerJobParams{ | ||
ID: uuid.New(), | ||
CreatedAt: time.Now().Add(time.Millisecond), // Slightly later | ||
UpdatedAt: time.Now().Add(time.Millisecond), | ||
InitiatorID: database.PrebuildsSystemUserID, | ||
OrganizationID: owner.OrganizationID, | ||
Provisioner: database.ProvisionerTypeEcho, | ||
Type: database.ProvisionerJobTypeWorkspaceBuild, | ||
StorageMethod: database.ProvisionerStorageMethodFile, | ||
FileID: uuid.New(), | ||
Input: json.RawMessage(`{}`), | ||
Tags: database.StringMap{}, | ||
TraceMetadata: pqtype.NullRawMessage{}, | ||
Priority: 0, // Prebuild should have priority 0 | ||
}) | ||
require.NoError(t, err) | ||
// Verify that human job has higher priority than prebuild job | ||
require.Equal(t, int32(1), humanJob.Priority, "Human-initiated job should have priority 1") | ||
require.Equal(t, int32(0), prebuildJob.Priority, "Prebuild job should have priority 0") | ||
// Test job acquisition order - human jobs should be acquired first | ||
// Even though the prebuild job was created later, the human job should be acquired first due to higher priority | ||
acquiredJob1, err := db.AcquireProvisionerJob(ctx, database.AcquireProvisionerJobParams{ | ||
OrganizationID: owner.OrganizationID, | ||
StartedAt: sql.NullTime{Time: time.Now(), Valid: true}, | ||
WorkerID: uuid.NullUUID{UUID: uuid.New(), Valid: true}, | ||
Types: []database.ProvisionerType{database.ProvisionerTypeEcho}, | ||
ProvisionerTags: json.RawMessage(`{}`), | ||
}) | ||
require.NoError(t, err) | ||
require.Equal(t, int32(1), acquiredJob1.Priority, "First acquired job should be human-initiated due to higher priority") | ||
require.Equal(t, humanJob.ID, acquiredJob1.ID, "First acquired job should be the human job") | ||
acquiredJob2, err := db.AcquireProvisionerJob(ctx, database.AcquireProvisionerJobParams{ | ||
OrganizationID: owner.OrganizationID, | ||
StartedAt: sql.NullTime{Time: time.Now(), Valid: true}, | ||
WorkerID: uuid.NullUUID{UUID: uuid.New(), Valid: true}, | ||
Types: []database.ProvisionerType{database.ProvisionerTypeEcho}, | ||
ProvisionerTags: json.RawMessage(`{}`), | ||
}) | ||
require.NoError(t, err) | ||
require.Equal(t, int32(0), acquiredJob2.Priority, "Second acquired job should be prebuild") | ||
require.Equal(t, prebuildJob.ID, acquiredJob2.ID, "Second acquired job should be the prebuild job") | ||
} |
7 changes: 7 additions & 0 deletionscoderd/wsbuilder/wsbuilder.go
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
1 change: 1 addition & 0 deletionscodersdk/provisionerdaemons.go
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.
Uh oh!
There was an error while loading.Please reload this page.
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.