Skip to content

Commit c227bb8

Browse files
committed
Control loop now handles reconciliation of multiple prebuilds-configured template versions
Correctly calculates extraneous prebuilds and returns offending prebuild IDs Signed-off-by: Danny Kopping <danny@coder.com>
1 parent a59a03d commit c227bb8

File tree

7 files changed

+130
-109
lines changed

7 files changed

+130
-109
lines changed

coderd/database/dbauthz/dbauthz.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2221,10 +2221,10 @@ func (q *querier) GetTemplateParameterInsights(ctx context.Context, arg database
22212221
return q.db.GetTemplateParameterInsights(ctx, arg)
22222222
}
22232223

2224-
func (q *querier) GetTemplatePrebuildState(ctx context.Context, templateID uuid.UUID) (database.GetTemplatePrebuildStateRow, error) {
2224+
func (q *querier) GetTemplatePrebuildState(ctx context.Context, templateID uuid.UUID) ([]database.GetTemplatePrebuildStateRow, error) {
22252225
// TODO: authz
22262226
if err := q.authorizeContext(ctx, policy.ActionRead, rbac.ResourceTemplate); err != nil {
2227-
return database.GetTemplatePrebuildStateRow{}, err
2227+
return nil, err
22282228
}
22292229
return q.db.GetTemplatePrebuildState(ctx, templateID)
22302230
}

coderd/database/dbmem/dbmem.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5464,7 +5464,7 @@ func (q *FakeQuerier) GetTemplateParameterInsights(ctx context.Context, arg data
54645464
return rows, nil
54655465
}
54665466

5467-
func (q *FakeQuerier) GetTemplatePrebuildState(ctx context.Context, templateID uuid.UUID) (database.GetTemplatePrebuildStateRow, error) {
5467+
func (q *FakeQuerier) GetTemplatePrebuildState(ctx context.Context, templateID uuid.UUID) ([]database.GetTemplatePrebuildStateRow, error) {
54685468
panic("not implemented")
54695469
}
54705470

coderd/database/dbmetrics/querymetrics.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/querier.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries.sql.go

Lines changed: 58 additions & 38 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/prebuilds.sql

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
-- name: GetTemplatePrebuildState :one
1+
-- name: GetTemplatePrebuildState :many
22
WITH
33
-- All prebuilds currently running
44
running_prebuilds AS (SELECT p.template_id,
@@ -8,6 +8,7 @@ WITH
88
FROM workspace_prebuilds p
99
INNER JOIN workspace_latest_build b ON b.workspace_id = p.id
1010
INNER JOIN provisioner_jobs pj ON b.job_id = pj.id
11+
INNER JOIN templates t ON p.template_id = t.id
1112
WHERE (b.transition = 'start'::workspace_transition
1213
-- if a deletion job fails, the workspace will still be running
1314
OR pj.job_status IN ('failed'::provisioner_job_status, 'canceled'::provisioner_job_status,
@@ -28,37 +29,36 @@ WITH
2829
GROUP BY t.id, tv.id, tvpp.id),
2930
-- Jobs relating to prebuilds current in-flight
3031
prebuilds_in_progress AS (SELECT wpb.template_version_id, wpb.transition, COUNT(wpb.transition) AS count
31-
FROM workspace_prebuild_builds wpb
32-
INNER JOIN workspace_latest_build wlb ON wpb.workspace_id = wlb.workspace_id
32+
FROM workspace_latest_build wlb
3333
INNER JOIN provisioner_jobs pj ON wlb.job_id = pj.id
34+
INNER JOIN workspace_prebuild_builds wpb ON wpb.id = wlb.id
3435
WHERE pj.job_status NOT IN
3536
('succeeded'::provisioner_job_status, 'canceled'::provisioner_job_status,
3637
'failed'::provisioner_job_status)
3738
GROUP BY wpb.template_version_id, wpb.transition)
3839
SELECT t.template_id,
3940
t.template_version_id,
40-
t.using_active_version AS is_active,
41-
p.ids AS running_prebuild_ids,
42-
CAST(COALESCE(
43-
MAX(CASE WHEN t.using_active_version THEN p.count ELSE 0 END),
44-
0) AS INT) AS actual, -- running prebuilds for active version
41+
t.using_active_version AS is_active,
42+
CAST(COALESCE(MAX(CASE WHEN p.template_version_id = t.template_version_id THEN p.ids END),
43+
'') AS TEXT) AS running_prebuild_ids,
44+
CAST(COALESCE(MAX(CASE WHEN t.using_active_version THEN p.count ELSE 0 END),
45+
0) AS INT) AS actual, -- running prebuilds for active version
4546
CAST(COALESCE(MAX(CASE WHEN t.using_active_version THEN t.desired_instances ELSE 0 END),
46-
0) AS INT) AS desired, -- we only care about the active version's desired instances
47-
CAST(COALESCE(MAX(CASE WHEN t.using_active_version THEN 0 ELSE p.count END),
48-
0) AS INT) AS extraneous, -- running prebuilds for inactive version
47+
0) AS INT) AS desired, -- we only care about the active version's desired instances
4948
CAST(COALESCE(MAX(CASE
50-
WHEN pip.transition = 'start'::workspace_transition THEN pip.count
51-
ELSE 0 END), 0) AS INT) AS starting,
52-
CAST(COALESCE(MAX(CASE
53-
WHEN pip.transition = 'stop'::workspace_transition THEN pip.count
54-
ELSE 0 END),
55-
0) AS INT) AS stopping, -- not strictly needed, since prebuilds should never be left if a "stopped" state, but useful to know
56-
CAST(COALESCE(MAX(CASE
57-
WHEN pip.transition = 'delete'::workspace_transition THEN pip.count
58-
ELSE 0 END), 0) AS INT) AS deleting,
59-
t.deleted AS template_deleted,
60-
t.deprecated AS template_deprecated
49+
WHEN p.template_version_id = t.template_version_id AND t.using_active_version = false
50+
THEN p.count END),
51+
0) AS INT) AS extraneous, -- running prebuilds for inactive version
52+
CAST(COALESCE(MAX(CASE WHEN pip.transition = 'start'::workspace_transition THEN pip.count ELSE 0 END),
53+
0) AS INT) AS starting,
54+
CAST(COALESCE(MAX(CASE WHEN pip.transition = 'stop'::workspace_transition THEN pip.count ELSE 0 END),
55+
0) AS INT) AS stopping, -- not strictly needed, since prebuilds should never be left if a "stopped" state, but useful to know
56+
CAST(COALESCE(MAX(CASE WHEN pip.transition = 'delete'::workspace_transition THEN pip.count ELSE 0 END),
57+
0) AS INT) AS deleting,
58+
t.deleted AS template_deleted,
59+
t.deprecated AS template_deprecated
6160
FROM templates_with_prebuilds t
62-
LEFT JOIN running_prebuilds p ON p.template_id = t.template_id
61+
LEFT JOIN running_prebuilds p ON p.template_version_id = t.template_version_id
6362
LEFT JOIN prebuilds_in_progress pip ON pip.template_version_id = t.template_version_id
64-
GROUP BY t.using_active_version, t.template_id, t.template_version_id, p.count, p.ids, t.deleted, t.deprecated;
63+
GROUP BY t.using_active_version, t.template_id, t.template_version_id, p.count, p.ids,
64+
p.template_version_id, t.deleted, t.deprecated;

0 commit comments

Comments
 (0)