mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
chore: optimize GetPrebuiltWorkspaces query (#18717)
* Adds GetRunningPrebuiltWorkspacesOptimized query * Runs both original and updated query side-by-side and logs diffs
This commit is contained in:
@@ -2596,6 +2596,14 @@ func (q *querier) GetRunningPrebuiltWorkspaces(ctx context.Context) ([]database.
|
||||
return q.db.GetRunningPrebuiltWorkspaces(ctx)
|
||||
}
|
||||
|
||||
func (q *querier) GetRunningPrebuiltWorkspacesOptimized(ctx context.Context) ([]database.GetRunningPrebuiltWorkspacesOptimizedRow, error) {
|
||||
// This query returns only prebuilt workspaces, but we decided to require permissions for all workspaces.
|
||||
if err := q.authorizeContext(ctx, policy.ActionRead, rbac.ResourceWorkspace.All()); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return q.db.GetRunningPrebuiltWorkspacesOptimized(ctx)
|
||||
}
|
||||
|
||||
func (q *querier) GetRuntimeConfig(ctx context.Context, key string) (string, error) {
|
||||
if err := q.authorizeContext(ctx, policy.ActionRead, rbac.ResourceSystem); err != nil {
|
||||
return "", err
|
||||
|
||||
@@ -178,7 +178,8 @@ func TestDBAuthzRecursive(t *testing.T) {
|
||||
if method.Name == "InTx" ||
|
||||
method.Name == "Ping" ||
|
||||
method.Name == "Wrappers" ||
|
||||
method.Name == "PGLocks" {
|
||||
method.Name == "PGLocks" ||
|
||||
method.Name == "GetRunningPrebuiltWorkspacesOptimized" {
|
||||
continue
|
||||
}
|
||||
// easy to know which method failed.
|
||||
|
||||
@@ -41,6 +41,8 @@ var skipMethods = map[string]string{
|
||||
"Wrappers": "Not relevant",
|
||||
"AcquireLock": "Not relevant",
|
||||
"TryAcquireLock": "Not relevant",
|
||||
// This method will be removed once we know this works correctly.
|
||||
"GetRunningPrebuiltWorkspacesOptimized": "Not relevant",
|
||||
}
|
||||
|
||||
// TestMethodTestSuite runs MethodTestSuite.
|
||||
|
||||
@@ -359,6 +359,14 @@ func Workspace(t testing.TB, db database.Store, orig database.WorkspaceTable) da
|
||||
NextStartAt: orig.NextStartAt,
|
||||
})
|
||||
require.NoError(t, err, "insert workspace")
|
||||
if orig.Deleted {
|
||||
err = db.UpdateWorkspaceDeletedByID(genCtx, database.UpdateWorkspaceDeletedByIDParams{
|
||||
ID: workspace.ID,
|
||||
Deleted: true,
|
||||
})
|
||||
require.NoError(t, err, "set workspace as deleted")
|
||||
workspace.Deleted = true
|
||||
}
|
||||
return workspace
|
||||
}
|
||||
|
||||
|
||||
@@ -1335,6 +1335,13 @@ func (m queryMetricsStore) GetRunningPrebuiltWorkspaces(ctx context.Context) ([]
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
func (m queryMetricsStore) GetRunningPrebuiltWorkspacesOptimized(ctx context.Context) ([]database.GetRunningPrebuiltWorkspacesOptimizedRow, error) {
|
||||
start := time.Now()
|
||||
r0, r1 := m.s.GetRunningPrebuiltWorkspacesOptimized(ctx)
|
||||
m.queryLatencies.WithLabelValues("GetRunningPrebuiltWorkspacesOptimized").Observe(time.Since(start).Seconds())
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
func (m queryMetricsStore) GetRuntimeConfig(ctx context.Context, key string) (string, error) {
|
||||
start := time.Now()
|
||||
r0, r1 := m.s.GetRuntimeConfig(ctx, key)
|
||||
|
||||
@@ -2778,6 +2778,21 @@ func (mr *MockStoreMockRecorder) GetRunningPrebuiltWorkspaces(ctx any) *gomock.C
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetRunningPrebuiltWorkspaces", reflect.TypeOf((*MockStore)(nil).GetRunningPrebuiltWorkspaces), ctx)
|
||||
}
|
||||
|
||||
// GetRunningPrebuiltWorkspacesOptimized mocks base method.
|
||||
func (m *MockStore) GetRunningPrebuiltWorkspacesOptimized(ctx context.Context) ([]database.GetRunningPrebuiltWorkspacesOptimizedRow, error) {
|
||||
m.ctrl.T.Helper()
|
||||
ret := m.ctrl.Call(m, "GetRunningPrebuiltWorkspacesOptimized", ctx)
|
||||
ret0, _ := ret[0].([]database.GetRunningPrebuiltWorkspacesOptimizedRow)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
|
||||
// GetRunningPrebuiltWorkspacesOptimized indicates an expected call of GetRunningPrebuiltWorkspacesOptimized.
|
||||
func (mr *MockStoreMockRecorder) GetRunningPrebuiltWorkspacesOptimized(ctx any) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetRunningPrebuiltWorkspacesOptimized", reflect.TypeOf((*MockStore)(nil).GetRunningPrebuiltWorkspacesOptimized), ctx)
|
||||
}
|
||||
|
||||
// GetRuntimeConfig mocks base method.
|
||||
func (m *MockStore) GetRuntimeConfig(ctx context.Context, key string) (string, error) {
|
||||
m.ctrl.T.Helper()
|
||||
|
||||
@@ -298,6 +298,7 @@ type sqlcQuerier interface {
|
||||
GetReplicaByID(ctx context.Context, id uuid.UUID) (Replica, error)
|
||||
GetReplicasUpdatedAfter(ctx context.Context, updatedAt time.Time) ([]Replica, error)
|
||||
GetRunningPrebuiltWorkspaces(ctx context.Context) ([]GetRunningPrebuiltWorkspacesRow, error)
|
||||
GetRunningPrebuiltWorkspacesOptimized(ctx context.Context) ([]GetRunningPrebuiltWorkspacesOptimizedRow, error)
|
||||
GetRuntimeConfig(ctx context.Context, key string) (string, error)
|
||||
GetTailnetAgents(ctx context.Context, id uuid.UUID) ([]TailnetAgent, error)
|
||||
GetTailnetClientsForAgent(ctx context.Context, agentID uuid.UUID) ([]TailnetClient, error)
|
||||
|
||||
@@ -5020,3 +5020,102 @@ func requireUsersMatch(t testing.TB, expected []database.User, found []database.
|
||||
t.Helper()
|
||||
require.ElementsMatch(t, expected, database.ConvertUserRows(found), msg)
|
||||
}
|
||||
|
||||
// TestGetRunningPrebuiltWorkspaces ensures the correct behavior of the
|
||||
// GetRunningPrebuiltWorkspaces query.
|
||||
func TestGetRunningPrebuiltWorkspaces(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
if !dbtestutil.WillUsePostgres() {
|
||||
t.Skip("Test requires PostgreSQL for complex queries")
|
||||
}
|
||||
|
||||
ctx := testutil.Context(t, testutil.WaitLong)
|
||||
db, _ := dbtestutil.NewDB(t)
|
||||
now := dbtime.Now()
|
||||
|
||||
// Given: a prebuilt workspace with a successful start build and a stop build.
|
||||
org := dbgen.Organization(t, db, database.Organization{})
|
||||
user := dbgen.User(t, db, database.User{})
|
||||
template := dbgen.Template(t, db, database.Template{
|
||||
CreatedBy: user.ID,
|
||||
OrganizationID: org.ID,
|
||||
})
|
||||
templateVersion := dbgen.TemplateVersion(t, db, database.TemplateVersion{
|
||||
TemplateID: uuid.NullUUID{UUID: template.ID, Valid: true},
|
||||
OrganizationID: org.ID,
|
||||
CreatedBy: user.ID,
|
||||
})
|
||||
preset := dbgen.Preset(t, db, database.InsertPresetParams{
|
||||
TemplateVersionID: templateVersion.ID,
|
||||
DesiredInstances: sql.NullInt32{Int32: 1, Valid: true},
|
||||
})
|
||||
|
||||
setupFixture := func(t *testing.T, db database.Store, name string, deleted bool, transition database.WorkspaceTransition, jobStatus database.ProvisionerJobStatus) database.WorkspaceTable {
|
||||
t.Helper()
|
||||
ws := dbgen.Workspace(t, db, database.WorkspaceTable{
|
||||
OwnerID: database.PrebuildsSystemUserID,
|
||||
TemplateID: template.ID,
|
||||
Name: name,
|
||||
Deleted: deleted,
|
||||
})
|
||||
var canceledAt sql.NullTime
|
||||
var jobError sql.NullString
|
||||
switch jobStatus {
|
||||
case database.ProvisionerJobStatusFailed:
|
||||
jobError = sql.NullString{String: assert.AnError.Error(), Valid: true}
|
||||
case database.ProvisionerJobStatusCanceled:
|
||||
canceledAt = sql.NullTime{Time: now, Valid: true}
|
||||
}
|
||||
pj := dbgen.ProvisionerJob(t, db, nil, database.ProvisionerJob{
|
||||
OrganizationID: org.ID,
|
||||
InitiatorID: database.PrebuildsSystemUserID,
|
||||
Provisioner: database.ProvisionerTypeEcho,
|
||||
Type: database.ProvisionerJobTypeWorkspaceBuild,
|
||||
StartedAt: sql.NullTime{Time: now.Add(-time.Minute), Valid: true},
|
||||
CanceledAt: canceledAt,
|
||||
CompletedAt: sql.NullTime{Time: now, Valid: true},
|
||||
Error: jobError,
|
||||
})
|
||||
wb := dbgen.WorkspaceBuild(t, db, database.WorkspaceBuild{
|
||||
WorkspaceID: ws.ID,
|
||||
TemplateVersionID: templateVersion.ID,
|
||||
TemplateVersionPresetID: uuid.NullUUID{UUID: preset.ID, Valid: true},
|
||||
JobID: pj.ID,
|
||||
BuildNumber: 1,
|
||||
Transition: transition,
|
||||
InitiatorID: database.PrebuildsSystemUserID,
|
||||
Reason: database.BuildReasonInitiator,
|
||||
})
|
||||
// Ensure things are set up as expectd
|
||||
require.Equal(t, transition, wb.Transition)
|
||||
require.Equal(t, int32(1), wb.BuildNumber)
|
||||
require.Equal(t, jobStatus, pj.JobStatus)
|
||||
require.Equal(t, deleted, ws.Deleted)
|
||||
|
||||
return ws
|
||||
}
|
||||
|
||||
// Given: a number of prebuild workspaces with different states exist.
|
||||
runningPrebuild := setupFixture(t, db, "running-prebuild", false, database.WorkspaceTransitionStart, database.ProvisionerJobStatusSucceeded)
|
||||
_ = setupFixture(t, db, "stopped-prebuild", false, database.WorkspaceTransitionStop, database.ProvisionerJobStatusSucceeded)
|
||||
_ = setupFixture(t, db, "failed-prebuild", false, database.WorkspaceTransitionStart, database.ProvisionerJobStatusFailed)
|
||||
_ = setupFixture(t, db, "canceled-prebuild", false, database.WorkspaceTransitionStart, database.ProvisionerJobStatusCanceled)
|
||||
_ = setupFixture(t, db, "deleted-prebuild", true, database.WorkspaceTransitionStart, database.ProvisionerJobStatusSucceeded)
|
||||
|
||||
// Given: a regular workspace also exists.
|
||||
_ = dbfake.WorkspaceBuild(t, db, database.WorkspaceTable{
|
||||
OwnerID: user.ID,
|
||||
TemplateID: template.ID,
|
||||
Name: "test-running-regular-workspace",
|
||||
Deleted: false,
|
||||
})
|
||||
|
||||
// When: we query for running prebuild workspaces
|
||||
runningPrebuilds, err := db.GetRunningPrebuiltWorkspaces(ctx)
|
||||
require.NoError(t, err)
|
||||
|
||||
// Then: only the running prebuild workspace should be returned.
|
||||
require.Len(t, runningPrebuilds, 1, "expected only one running prebuilt workspace")
|
||||
require.Equal(t, runningPrebuild.ID, runningPrebuilds[0].ID, "expected the running prebuilt workspace to be returned")
|
||||
}
|
||||
|
||||
@@ -6843,6 +6843,7 @@ FROM workspace_prebuilds p
|
||||
INNER JOIN workspace_latest_builds b ON b.workspace_id = p.id
|
||||
WHERE (b.transition = 'start'::workspace_transition
|
||||
AND b.job_status = 'succeeded'::provisioner_job_status)
|
||||
ORDER BY p.id
|
||||
`
|
||||
|
||||
type GetRunningPrebuiltWorkspacesRow struct {
|
||||
@@ -6886,6 +6887,106 @@ func (q *sqlQuerier) GetRunningPrebuiltWorkspaces(ctx context.Context) ([]GetRun
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const getRunningPrebuiltWorkspacesOptimized = `-- name: GetRunningPrebuiltWorkspacesOptimized :many
|
||||
WITH latest_prebuilds AS (
|
||||
-- All workspaces that match the following criteria:
|
||||
-- 1. Owned by prebuilds user
|
||||
-- 2. Not deleted
|
||||
-- 3. Latest build is a 'start' transition
|
||||
-- 4. Latest build was successful
|
||||
SELECT
|
||||
workspaces.id,
|
||||
workspaces.name,
|
||||
workspaces.template_id,
|
||||
workspace_latest_builds.template_version_id,
|
||||
workspace_latest_builds.job_id,
|
||||
workspaces.created_at
|
||||
FROM workspace_latest_builds
|
||||
JOIN workspaces ON workspaces.id = workspace_latest_builds.workspace_id
|
||||
WHERE workspace_latest_builds.transition = 'start'::workspace_transition
|
||||
AND workspace_latest_builds.job_status = 'succeeded'::provisioner_job_status
|
||||
AND workspaces.owner_id = 'c42fdf75-3097-471c-8c33-fb52454d81c0'::UUID
|
||||
AND NOT workspaces.deleted
|
||||
),
|
||||
workspace_latest_presets AS (
|
||||
-- For each of the above workspaces, the preset_id of the most recent
|
||||
-- successful start transition.
|
||||
SELECT DISTINCT ON (latest_prebuilds.id)
|
||||
latest_prebuilds.id AS workspace_id,
|
||||
workspace_builds.template_version_preset_id AS current_preset_id
|
||||
FROM latest_prebuilds
|
||||
JOIN workspace_builds ON workspace_builds.workspace_id = latest_prebuilds.id
|
||||
WHERE workspace_builds.transition = 'start'::workspace_transition
|
||||
AND workspace_builds.template_version_preset_id IS NOT NULL
|
||||
ORDER BY latest_prebuilds.id, workspace_builds.build_number DESC
|
||||
),
|
||||
ready_agents AS (
|
||||
-- For each of the above workspaces, check if all agents are ready.
|
||||
SELECT
|
||||
latest_prebuilds.job_id,
|
||||
BOOL_AND(workspace_agents.lifecycle_state = 'ready'::workspace_agent_lifecycle_state)::boolean AS ready
|
||||
FROM latest_prebuilds
|
||||
JOIN workspace_resources ON workspace_resources.job_id = latest_prebuilds.job_id
|
||||
JOIN workspace_agents ON workspace_agents.resource_id = workspace_resources.id
|
||||
WHERE workspace_agents.deleted = false
|
||||
AND workspace_agents.parent_id IS NULL
|
||||
GROUP BY latest_prebuilds.job_id
|
||||
)
|
||||
SELECT
|
||||
latest_prebuilds.id,
|
||||
latest_prebuilds.name,
|
||||
latest_prebuilds.template_id,
|
||||
latest_prebuilds.template_version_id,
|
||||
workspace_latest_presets.current_preset_id,
|
||||
COALESCE(ready_agents.ready, false)::boolean AS ready,
|
||||
latest_prebuilds.created_at
|
||||
FROM latest_prebuilds
|
||||
LEFT JOIN ready_agents ON ready_agents.job_id = latest_prebuilds.job_id
|
||||
LEFT JOIN workspace_latest_presets ON workspace_latest_presets.workspace_id = latest_prebuilds.id
|
||||
ORDER BY latest_prebuilds.id
|
||||
`
|
||||
|
||||
type GetRunningPrebuiltWorkspacesOptimizedRow struct {
|
||||
ID uuid.UUID `db:"id" json:"id"`
|
||||
Name string `db:"name" json:"name"`
|
||||
TemplateID uuid.UUID `db:"template_id" json:"template_id"`
|
||||
TemplateVersionID uuid.UUID `db:"template_version_id" json:"template_version_id"`
|
||||
CurrentPresetID uuid.NullUUID `db:"current_preset_id" json:"current_preset_id"`
|
||||
Ready bool `db:"ready" json:"ready"`
|
||||
CreatedAt time.Time `db:"created_at" json:"created_at"`
|
||||
}
|
||||
|
||||
func (q *sqlQuerier) GetRunningPrebuiltWorkspacesOptimized(ctx context.Context) ([]GetRunningPrebuiltWorkspacesOptimizedRow, error) {
|
||||
rows, err := q.db.QueryContext(ctx, getRunningPrebuiltWorkspacesOptimized)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []GetRunningPrebuiltWorkspacesOptimizedRow
|
||||
for rows.Next() {
|
||||
var i GetRunningPrebuiltWorkspacesOptimizedRow
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.Name,
|
||||
&i.TemplateID,
|
||||
&i.TemplateVersionID,
|
||||
&i.CurrentPresetID,
|
||||
&i.Ready,
|
||||
&i.CreatedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const getTemplatePresetsWithPrebuilds = `-- name: GetTemplatePresetsWithPrebuilds :many
|
||||
SELECT
|
||||
t.id AS template_id,
|
||||
|
||||
@@ -48,6 +48,64 @@ WHERE tvp.desired_instances IS NOT NULL -- Consider only presets that have a pre
|
||||
-- AND NOT t.deleted -- We don't exclude deleted templates because there's no constraint in the DB preventing a soft deletion on a template while workspaces are running.
|
||||
AND (t.id = sqlc.narg('template_id')::uuid OR sqlc.narg('template_id') IS NULL);
|
||||
|
||||
-- name: GetRunningPrebuiltWorkspacesOptimized :many
|
||||
WITH latest_prebuilds AS (
|
||||
-- All workspaces that match the following criteria:
|
||||
-- 1. Owned by prebuilds user
|
||||
-- 2. Not deleted
|
||||
-- 3. Latest build is a 'start' transition
|
||||
-- 4. Latest build was successful
|
||||
SELECT
|
||||
workspaces.id,
|
||||
workspaces.name,
|
||||
workspaces.template_id,
|
||||
workspace_latest_builds.template_version_id,
|
||||
workspace_latest_builds.job_id,
|
||||
workspaces.created_at
|
||||
FROM workspace_latest_builds
|
||||
JOIN workspaces ON workspaces.id = workspace_latest_builds.workspace_id
|
||||
WHERE workspace_latest_builds.transition = 'start'::workspace_transition
|
||||
AND workspace_latest_builds.job_status = 'succeeded'::provisioner_job_status
|
||||
AND workspaces.owner_id = 'c42fdf75-3097-471c-8c33-fb52454d81c0'::UUID
|
||||
AND NOT workspaces.deleted
|
||||
),
|
||||
workspace_latest_presets AS (
|
||||
-- For each of the above workspaces, the preset_id of the most recent
|
||||
-- successful start transition.
|
||||
SELECT DISTINCT ON (latest_prebuilds.id)
|
||||
latest_prebuilds.id AS workspace_id,
|
||||
workspace_builds.template_version_preset_id AS current_preset_id
|
||||
FROM latest_prebuilds
|
||||
JOIN workspace_builds ON workspace_builds.workspace_id = latest_prebuilds.id
|
||||
WHERE workspace_builds.transition = 'start'::workspace_transition
|
||||
AND workspace_builds.template_version_preset_id IS NOT NULL
|
||||
ORDER BY latest_prebuilds.id, workspace_builds.build_number DESC
|
||||
),
|
||||
ready_agents AS (
|
||||
-- For each of the above workspaces, check if all agents are ready.
|
||||
SELECT
|
||||
latest_prebuilds.job_id,
|
||||
BOOL_AND(workspace_agents.lifecycle_state = 'ready'::workspace_agent_lifecycle_state)::boolean AS ready
|
||||
FROM latest_prebuilds
|
||||
JOIN workspace_resources ON workspace_resources.job_id = latest_prebuilds.job_id
|
||||
JOIN workspace_agents ON workspace_agents.resource_id = workspace_resources.id
|
||||
WHERE workspace_agents.deleted = false
|
||||
AND workspace_agents.parent_id IS NULL
|
||||
GROUP BY latest_prebuilds.job_id
|
||||
)
|
||||
SELECT
|
||||
latest_prebuilds.id,
|
||||
latest_prebuilds.name,
|
||||
latest_prebuilds.template_id,
|
||||
latest_prebuilds.template_version_id,
|
||||
workspace_latest_presets.current_preset_id,
|
||||
COALESCE(ready_agents.ready, false)::boolean AS ready,
|
||||
latest_prebuilds.created_at
|
||||
FROM latest_prebuilds
|
||||
LEFT JOIN ready_agents ON ready_agents.job_id = latest_prebuilds.job_id
|
||||
LEFT JOIN workspace_latest_presets ON workspace_latest_presets.workspace_id = latest_prebuilds.id
|
||||
ORDER BY latest_prebuilds.id;
|
||||
|
||||
-- name: GetRunningPrebuiltWorkspaces :many
|
||||
SELECT
|
||||
p.id,
|
||||
@@ -60,7 +118,8 @@ SELECT
|
||||
FROM workspace_prebuilds p
|
||||
INNER JOIN workspace_latest_builds b ON b.workspace_id = p.id
|
||||
WHERE (b.transition = 'start'::workspace_transition
|
||||
AND b.job_status = 'succeeded'::provisioner_job_status);
|
||||
AND b.job_status = 'succeeded'::provisioner_job_status)
|
||||
ORDER BY p.id;
|
||||
|
||||
-- name: CountInProgressPrebuilds :many
|
||||
-- CountInProgressPrebuilds returns the number of in-progress prebuilds, grouped by preset ID and transition.
|
||||
|
||||
Reference in New Issue
Block a user