mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
chore: replace original GetPrebuiltWorkspaces with optimized version (#18832)
Fixes https://github.com/coder/internal/issues/715 Follow-up from https://github.com/coder/coder/pull/18717 Now that we've determined the updated query is safe, remove the duplication.
This commit is contained in:
@@ -2654,14 +2654,6 @@ 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,8 +178,7 @@ func TestDBAuthzRecursive(t *testing.T) {
|
||||
if method.Name == "InTx" ||
|
||||
method.Name == "Ping" ||
|
||||
method.Name == "Wrappers" ||
|
||||
method.Name == "PGLocks" ||
|
||||
method.Name == "GetRunningPrebuiltWorkspacesOptimized" {
|
||||
method.Name == "PGLocks" {
|
||||
continue
|
||||
}
|
||||
// easy to know which method failed.
|
||||
|
||||
@@ -41,8 +41,6 @@ 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.
|
||||
|
||||
@@ -1356,13 +1356,6 @@ 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)
|
||||
|
||||
@@ -2852,21 +2852,6 @@ 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()
|
||||
|
||||
@@ -301,7 +301,6 @@ 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)
|
||||
|
||||
@@ -7361,63 +7361,6 @@ func (q *sqlQuerier) GetPresetsBackoff(ctx context.Context, lookback time.Time)
|
||||
}
|
||||
|
||||
const getRunningPrebuiltWorkspaces = `-- name: GetRunningPrebuiltWorkspaces :many
|
||||
SELECT
|
||||
p.id,
|
||||
p.name,
|
||||
p.template_id,
|
||||
b.template_version_id,
|
||||
p.current_preset_id AS current_preset_id,
|
||||
p.ready,
|
||||
p.created_at
|
||||
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 {
|
||||
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) GetRunningPrebuiltWorkspaces(ctx context.Context) ([]GetRunningPrebuiltWorkspacesRow, error) {
|
||||
rows, err := q.db.QueryContext(ctx, getRunningPrebuiltWorkspaces)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []GetRunningPrebuiltWorkspacesRow
|
||||
for rows.Next() {
|
||||
var i GetRunningPrebuiltWorkspacesRow
|
||||
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 getRunningPrebuiltWorkspacesOptimized = `-- name: GetRunningPrebuiltWorkspacesOptimized :many
|
||||
WITH latest_prebuilds AS (
|
||||
-- All workspaces that match the following criteria:
|
||||
-- 1. Owned by prebuilds user
|
||||
@@ -7476,7 +7419,7 @@ LEFT JOIN workspace_latest_presets ON workspace_latest_presets.workspace_id = la
|
||||
ORDER BY latest_prebuilds.id
|
||||
`
|
||||
|
||||
type GetRunningPrebuiltWorkspacesOptimizedRow struct {
|
||||
type GetRunningPrebuiltWorkspacesRow struct {
|
||||
ID uuid.UUID `db:"id" json:"id"`
|
||||
Name string `db:"name" json:"name"`
|
||||
TemplateID uuid.UUID `db:"template_id" json:"template_id"`
|
||||
@@ -7486,15 +7429,15 @@ type GetRunningPrebuiltWorkspacesOptimizedRow struct {
|
||||
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)
|
||||
func (q *sqlQuerier) GetRunningPrebuiltWorkspaces(ctx context.Context) ([]GetRunningPrebuiltWorkspacesRow, error) {
|
||||
rows, err := q.db.QueryContext(ctx, getRunningPrebuiltWorkspaces)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []GetRunningPrebuiltWorkspacesOptimizedRow
|
||||
var items []GetRunningPrebuiltWorkspacesRow
|
||||
for rows.Next() {
|
||||
var i GetRunningPrebuiltWorkspacesOptimizedRow
|
||||
var i GetRunningPrebuiltWorkspacesRow
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.Name,
|
||||
|
||||
@@ -48,7 +48,7 @@ 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
|
||||
-- name: GetRunningPrebuiltWorkspaces :many
|
||||
WITH latest_prebuilds AS (
|
||||
-- All workspaces that match the following criteria:
|
||||
-- 1. Owned by prebuilds user
|
||||
@@ -106,21 +106,6 @@ 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,
|
||||
p.name,
|
||||
p.template_id,
|
||||
b.template_version_id,
|
||||
p.current_preset_id AS current_preset_id,
|
||||
p.ready,
|
||||
p.created_at
|
||||
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;
|
||||
|
||||
-- name: CountInProgressPrebuilds :many
|
||||
-- CountInProgressPrebuilds returns the number of in-progress prebuilds, grouped by preset ID and transition.
|
||||
-- Prebuild considered in-progress if it's in the "starting", "stopping", or "deleting" state.
|
||||
|
||||
Reference in New Issue
Block a user