mirror of
https://github.com/coder/coder.git
synced 2025-07-06 15:41:45 +00:00
Fixed bug in state query relating to multiple template versions & workspaces in partially-deleted statuses
Signed-off-by: Danny Kopping <danny@coder.com>
This commit is contained in:
@ -5398,10 +5398,17 @@ func (q *sqlQuerier) GetParameterSchemasByJobID(ctx context.Context, jobID uuid.
|
||||
const getTemplatePrebuildState = `-- name: GetTemplatePrebuildState :one
|
||||
WITH
|
||||
-- All prebuilds currently running
|
||||
running_prebuilds AS (SELECT p.template_id, b.template_version_id, COUNT(*) AS count, STRING_AGG(p.id::text, ',') AS ids
|
||||
running_prebuilds AS (SELECT p.template_id,
|
||||
b.template_version_id,
|
||||
COUNT(*) AS count,
|
||||
STRING_AGG(p.id::text, ',') AS ids
|
||||
FROM workspace_prebuilds p
|
||||
INNER JOIN workspace_latest_build b ON b.workspace_id = p.id
|
||||
WHERE b.transition = 'start'::workspace_transition
|
||||
INNER JOIN provisioner_jobs pj ON b.job_id = pj.id
|
||||
WHERE (b.transition = 'start'::workspace_transition
|
||||
-- if a deletion job fails, the workspace will still be running
|
||||
OR pj.job_status IN ('failed'::provisioner_job_status, 'canceled'::provisioner_job_status,
|
||||
'unknown'::provisioner_job_status))
|
||||
GROUP BY p.template_id, b.template_version_id),
|
||||
-- All templates which have been configured for prebuilds (any version)
|
||||
templates_with_prebuilds AS (SELECT t.id AS template_id,
|
||||
@ -5416,6 +5423,7 @@ WITH
|
||||
INNER JOIN template_version_preset_prebuilds tvpp ON tvpp.preset_id = tvp.id
|
||||
WHERE t.id = $1::uuid
|
||||
GROUP BY t.id, tv.id, tvpp.id),
|
||||
-- Jobs relating to prebuilds current in-flight
|
||||
prebuilds_in_progress AS (SELECT wpb.template_version_id, wpb.transition, COUNT(wpb.transition) AS count
|
||||
FROM workspace_prebuild_builds wpb
|
||||
INNER JOIN workspace_latest_build wlb ON wpb.workspace_id = wlb.workspace_id
|
||||
@ -5425,29 +5433,38 @@ WITH
|
||||
'failed'::provisioner_job_status)
|
||||
GROUP BY wpb.template_version_id, wpb.transition)
|
||||
SELECT t.template_id,
|
||||
p.ids AS running_prebuild_ids,
|
||||
CAST(SUM(CASE WHEN t.using_active_version THEN p.count ELSE 0 END) AS INT) AS actual, -- running prebuilds for active version
|
||||
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
|
||||
CAST(SUM(CASE WHEN t.using_active_version THEN 0 ELSE p.count END) AS INT) AS extraneous, -- running prebuilds for inactive version
|
||||
CAST(MAX(CASE
|
||||
WHEN pip.transition = 'start'::workspace_transition THEN pip.count
|
||||
ELSE 0 END) AS INT) AS starting,
|
||||
CAST(MAX(CASE
|
||||
WHEN pip.transition = 'stop'::workspace_transition THEN pip.count
|
||||
ELSE 0 END) AS INT) AS stopping, -- not strictly needed, since prebuilds should never be left if a "stopped" state, but useful to know
|
||||
CAST(MAX(CASE
|
||||
WHEN pip.transition = 'delete'::workspace_transition THEN pip.count
|
||||
ELSE 0 END) AS INT) AS deleting,
|
||||
t.deleted AS template_deleted,
|
||||
t.deprecated AS template_deprecated
|
||||
t.template_version_id,
|
||||
t.using_active_version AS is_active,
|
||||
p.ids AS running_prebuild_ids,
|
||||
CAST(COALESCE(
|
||||
MAX(CASE WHEN t.using_active_version THEN p.count ELSE 0 END),
|
||||
0) AS INT) AS actual, -- running prebuilds for active version
|
||||
CAST(COALESCE(MAX(CASE WHEN t.using_active_version THEN t.desired_instances ELSE 0 END),
|
||||
0) AS INT) AS desired, -- we only care about the active version's desired instances
|
||||
CAST(COALESCE(MAX(CASE WHEN t.using_active_version THEN 0 ELSE p.count END),
|
||||
0) AS INT) AS extraneous, -- running prebuilds for inactive version
|
||||
CAST(COALESCE(MAX(CASE
|
||||
WHEN pip.transition = 'start'::workspace_transition THEN pip.count
|
||||
ELSE 0 END), 0) AS INT) AS starting,
|
||||
CAST(COALESCE(MAX(CASE
|
||||
WHEN pip.transition = 'stop'::workspace_transition THEN pip.count
|
||||
ELSE 0 END),
|
||||
0) AS INT) AS stopping, -- not strictly needed, since prebuilds should never be left if a "stopped" state, but useful to know
|
||||
CAST(COALESCE(MAX(CASE
|
||||
WHEN pip.transition = 'delete'::workspace_transition THEN pip.count
|
||||
ELSE 0 END), 0) AS INT) AS deleting,
|
||||
t.deleted AS template_deleted,
|
||||
t.deprecated AS template_deprecated
|
||||
FROM templates_with_prebuilds t
|
||||
LEFT JOIN running_prebuilds p ON p.template_version_id = t.template_version_id
|
||||
LEFT JOIN running_prebuilds p ON p.template_id = t.template_id
|
||||
LEFT JOIN prebuilds_in_progress pip ON pip.template_version_id = t.template_version_id
|
||||
GROUP BY t.template_id, p.count, p.ids, t.deleted, t.deprecated
|
||||
GROUP BY t.using_active_version, t.template_id, t.template_version_id, p.count, p.ids, t.deleted, t.deprecated
|
||||
`
|
||||
|
||||
type GetTemplatePrebuildStateRow struct {
|
||||
TemplateID uuid.UUID `db:"template_id" json:"template_id"`
|
||||
TemplateVersionID uuid.UUID `db:"template_version_id" json:"template_version_id"`
|
||||
IsActive bool `db:"is_active" json:"is_active"`
|
||||
RunningPrebuildIds []byte `db:"running_prebuild_ids" json:"running_prebuild_ids"`
|
||||
Actual int32 `db:"actual" json:"actual"`
|
||||
Desired int32 `db:"desired" json:"desired"`
|
||||
@ -5464,6 +5481,8 @@ func (q *sqlQuerier) GetTemplatePrebuildState(ctx context.Context, templateID uu
|
||||
var i GetTemplatePrebuildStateRow
|
||||
err := row.Scan(
|
||||
&i.TemplateID,
|
||||
&i.TemplateVersionID,
|
||||
&i.IsActive,
|
||||
&i.RunningPrebuildIds,
|
||||
&i.Actual,
|
||||
&i.Desired,
|
||||
|
@ -1,10 +1,17 @@
|
||||
-- name: GetTemplatePrebuildState :one
|
||||
WITH
|
||||
-- All prebuilds currently running
|
||||
running_prebuilds AS (SELECT p.template_id, b.template_version_id, COUNT(*) AS count, STRING_AGG(p.id::text, ',') AS ids
|
||||
running_prebuilds AS (SELECT p.template_id,
|
||||
b.template_version_id,
|
||||
COUNT(*) AS count,
|
||||
STRING_AGG(p.id::text, ',') AS ids
|
||||
FROM workspace_prebuilds p
|
||||
INNER JOIN workspace_latest_build b ON b.workspace_id = p.id
|
||||
WHERE b.transition = 'start'::workspace_transition
|
||||
INNER JOIN provisioner_jobs pj ON b.job_id = pj.id
|
||||
WHERE (b.transition = 'start'::workspace_transition
|
||||
-- if a deletion job fails, the workspace will still be running
|
||||
OR pj.job_status IN ('failed'::provisioner_job_status, 'canceled'::provisioner_job_status,
|
||||
'unknown'::provisioner_job_status))
|
||||
GROUP BY p.template_id, b.template_version_id),
|
||||
-- All templates which have been configured for prebuilds (any version)
|
||||
templates_with_prebuilds AS (SELECT t.id AS template_id,
|
||||
@ -19,6 +26,7 @@ WITH
|
||||
INNER JOIN template_version_preset_prebuilds tvpp ON tvpp.preset_id = tvp.id
|
||||
WHERE t.id = @template_id::uuid
|
||||
GROUP BY t.id, tv.id, tvpp.id),
|
||||
-- Jobs relating to prebuilds current in-flight
|
||||
prebuilds_in_progress AS (SELECT wpb.template_version_id, wpb.transition, COUNT(wpb.transition) AS count
|
||||
FROM workspace_prebuild_builds wpb
|
||||
INNER JOIN workspace_latest_build wlb ON wpb.workspace_id = wlb.workspace_id
|
||||
@ -28,22 +36,29 @@ WITH
|
||||
'failed'::provisioner_job_status)
|
||||
GROUP BY wpb.template_version_id, wpb.transition)
|
||||
SELECT t.template_id,
|
||||
p.ids AS running_prebuild_ids,
|
||||
CAST(SUM(CASE WHEN t.using_active_version THEN p.count ELSE 0 END) AS INT) AS actual, -- running prebuilds for active version
|
||||
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
|
||||
CAST(SUM(CASE WHEN t.using_active_version THEN 0 ELSE p.count END) AS INT) AS extraneous, -- running prebuilds for inactive version
|
||||
CAST(MAX(CASE
|
||||
WHEN pip.transition = 'start'::workspace_transition THEN pip.count
|
||||
ELSE 0 END) AS INT) AS starting,
|
||||
CAST(MAX(CASE
|
||||
WHEN pip.transition = 'stop'::workspace_transition THEN pip.count
|
||||
ELSE 0 END) AS INT) AS stopping, -- not strictly needed, since prebuilds should never be left if a "stopped" state, but useful to know
|
||||
CAST(MAX(CASE
|
||||
WHEN pip.transition = 'delete'::workspace_transition THEN pip.count
|
||||
ELSE 0 END) AS INT) AS deleting,
|
||||
t.deleted AS template_deleted,
|
||||
t.deprecated AS template_deprecated
|
||||
t.template_version_id,
|
||||
t.using_active_version AS is_active,
|
||||
p.ids AS running_prebuild_ids,
|
||||
CAST(COALESCE(
|
||||
MAX(CASE WHEN t.using_active_version THEN p.count ELSE 0 END),
|
||||
0) AS INT) AS actual, -- running prebuilds for active version
|
||||
CAST(COALESCE(MAX(CASE WHEN t.using_active_version THEN t.desired_instances ELSE 0 END),
|
||||
0) AS INT) AS desired, -- we only care about the active version's desired instances
|
||||
CAST(COALESCE(MAX(CASE WHEN t.using_active_version THEN 0 ELSE p.count END),
|
||||
0) AS INT) AS extraneous, -- running prebuilds for inactive version
|
||||
CAST(COALESCE(MAX(CASE
|
||||
WHEN pip.transition = 'start'::workspace_transition THEN pip.count
|
||||
ELSE 0 END), 0) AS INT) AS starting,
|
||||
CAST(COALESCE(MAX(CASE
|
||||
WHEN pip.transition = 'stop'::workspace_transition THEN pip.count
|
||||
ELSE 0 END),
|
||||
0) AS INT) AS stopping, -- not strictly needed, since prebuilds should never be left if a "stopped" state, but useful to know
|
||||
CAST(COALESCE(MAX(CASE
|
||||
WHEN pip.transition = 'delete'::workspace_transition THEN pip.count
|
||||
ELSE 0 END), 0) AS INT) AS deleting,
|
||||
t.deleted AS template_deleted,
|
||||
t.deprecated AS template_deprecated
|
||||
FROM templates_with_prebuilds t
|
||||
LEFT JOIN running_prebuilds p ON p.template_version_id = t.template_version_id
|
||||
LEFT JOIN running_prebuilds p ON p.template_id = t.template_id
|
||||
LEFT JOIN prebuilds_in_progress pip ON pip.template_version_id = t.template_version_id
|
||||
GROUP BY t.template_id, p.count, p.ids, t.deleted, t.deprecated;
|
||||
GROUP BY t.using_active_version, t.template_id, t.template_version_id, p.count, p.ids, t.deleted, t.deprecated;
|
||||
|
Reference in New Issue
Block a user