Skip to content

Commit 41a9778

Browse files
committed
Improving control loop resilience, fixing calculations
Signed-off-by: Danny Kopping <danny@coder.com>
1 parent cffd634 commit 41a9778

File tree

8 files changed

+247
-97
lines changed

8 files changed

+247
-97
lines changed

cli/server.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -944,7 +944,7 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd.
944944
}
945945

946946
// TODO: implement experiment and configs
947-
prebuildsCtrl := prebuilds.NewController(logger.Named("prebuilds.controller"), options.Database)
947+
prebuildsCtrl := prebuilds.NewController(options.Database, options.Pubsub, logger.Named("prebuilds.controller"))
948948
go prebuildsCtrl.Loop(ctx)
949949
defer prebuildsCtrl.Stop()
950950

coderd/database/dbauthz/dbauthz.go

+2-2
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 nil, err
2227+
return database.GetTemplatePrebuildStateRow{}, err
22282228
}
22292229
return q.db.GetTemplatePrebuildState(ctx, templateID)
22302230
}

coderd/database/dbmem/dbmem.go

+1-1
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

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/querier.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries.sql.go

+41-47
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/queries/prebuilds.sql

+23-17
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
-- name: GetTemplatePrebuildState :many
1+
-- name: GetTemplatePrebuildState :one
22
WITH
33
-- All prebuilds currently running
4-
running_prebuilds AS (SELECT p.*, b.template_version_id
4+
running_prebuilds AS (SELECT p.template_id, b.template_version_id, COUNT(*) AS count, STRING_AGG(p.id::text, ',') AS ids
55
FROM workspace_prebuilds p
66
INNER JOIN workspace_latest_build b ON b.workspace_id = p.id
7-
WHERE b.transition = 'start'::workspace_transition),
7+
WHERE b.transition = 'start'::workspace_transition
8+
GROUP BY p.template_id, b.template_version_id),
89
-- All templates which have been configured for prebuilds (any version)
910
templates_with_prebuilds AS (SELECT t.id AS template_id,
1011
tv.id AS template_version_id,
@@ -18,26 +19,31 @@ WITH
1819
INNER JOIN template_version_preset_prebuilds tvpp ON tvpp.preset_id = tvp.id
1920
WHERE t.id = @template_id::uuid
2021
GROUP BY t.id, tv.id, tvpp.id),
21-
prebuilds_in_progress AS (SELECT wpb.template_version_id, pj.id AS job_id, pj.type, pj.job_status, wpb.transition
22+
prebuilds_in_progress AS (SELECT wpb.template_version_id, wpb.transition, COUNT(wpb.transition) AS count
2223
FROM workspace_prebuild_builds wpb
2324
INNER JOIN workspace_latest_build wlb ON wpb.workspace_id = wlb.workspace_id
2425
INNER JOIN provisioner_jobs pj ON wlb.job_id = pj.id
2526
WHERE pj.job_status NOT IN
2627
('succeeded'::provisioner_job_status, 'canceled'::provisioner_job_status,
27-
'failed'::provisioner_job_status))
28+
'failed'::provisioner_job_status)
29+
GROUP BY wpb.template_version_id, wpb.transition)
2830
SELECT t.template_id,
29-
CAST(COUNT(p.id) AS INT) AS actual, -- running prebuilds for active version
30-
CAST(MAX(CASE WHEN t.using_active_version THEN t.desired_instances ELSE 0 END) AS int) AS desired, -- we only care about the active version's desired instances
31-
CAST(SUM(CASE WHEN t.using_active_version THEN 0 ELSE 1 END) AS INT) AS extraneous, -- running prebuilds for inactive version
32-
CAST(SUM(CASE
33-
WHEN pip.transition = 'start'::workspace_transition THEN 1
34-
ELSE 0 END) AS INT) AS starting,
35-
CAST(SUM(CASE
36-
WHEN pip.transition = 'delete'::workspace_transition THEN 1
37-
ELSE 0 END) AS INT) AS deleting,
38-
t.deleted AS template_deleted,
39-
t.deprecated AS template_deprecated
31+
p.ids AS running_prebuild_ids,
32+
CAST(SUM(CASE WHEN t.using_active_version THEN p.count ELSE 0 END) AS INT) AS actual, -- running prebuilds for active version
33+
CAST(MAX(CASE WHEN t.using_active_version THEN t.desired_instances ELSE 0 END) AS int) AS desired, -- we only care about the active version's desired instances
34+
CAST(SUM(CASE WHEN t.using_active_version THEN 0 ELSE p.count END) AS INT) AS extraneous, -- running prebuilds for inactive version
35+
CAST(MAX(CASE
36+
WHEN pip.transition = 'start'::workspace_transition THEN pip.count
37+
ELSE 0 END) AS INT) AS starting,
38+
CAST(MAX(CASE
39+
WHEN pip.transition = 'stop'::workspace_transition THEN pip.count
40+
ELSE 0 END) AS INT) AS stopping, -- not strictly needed, since prebuilds should never be left if a "stopped" state, but useful to know
41+
CAST(MAX(CASE
42+
WHEN pip.transition = 'delete'::workspace_transition THEN pip.count
43+
ELSE 0 END) AS INT) AS deleting,
44+
t.deleted AS template_deleted,
45+
t.deprecated AS template_deprecated
4046
FROM templates_with_prebuilds t
4147
LEFT JOIN running_prebuilds p ON p.template_version_id = t.template_version_id
4248
LEFT JOIN prebuilds_in_progress pip ON pip.template_version_id = t.template_version_id
43-
GROUP BY t.template_id, p.id, t.deleted, t.deprecated, pip.template_version_id;
49+
GROUP BY t.template_id, p.count, p.ids, t.deleted, t.deprecated;

0 commit comments

Comments
 (0)