Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f3c9e06

Browse files
fix: formatting
1 parent db65b8b commit f3c9e06

File tree

5 files changed

+122
-118
lines changed

5 files changed

+122
-118
lines changed

coderd/database/migrations/000313_prebuilds.up.sql

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,44 +17,44 @@ ORDER BY wb.workspace_id, wb.build_number DESC;
1717
-- (including lifecycle_state which indicates is agent ready or not) and corresponding preset_id for prebuild
1818
CREATE VIEW workspace_prebuilds AS
1919
WITH
20-
-- All workspaces owned by the "prebuilds" user.
21-
all_prebuilds AS (
20+
-- All workspaces owned by the "prebuilds" user.
21+
all_prebuilds AS (
2222
SELECT w.id, w.name, w.template_id, w.created_at
2323
FROM workspaces w
2424
WHERE w.owner_id = 'c42fdf75-3097-471c-8c33-fb52454d81c0' -- The system user responsible for prebuilds.
2525
),
26-
-- We can't rely on the template_version_preset_id in the workspace_builds table because this value is only set on the
27-
-- initial workspace creation. Subsequent stop/start transitions will not have a value for template_version_preset_id,
28-
-- and therefore we can't rely on (say) the latest build's chosen template_version_preset_id.
29-
--
30-
-- See https://github.com/coder/internal/issues/398
31-
workspaces_with_latest_presets AS (
32-
SELECT DISTINCT ON (workspace_id) workspace_id, template_version_preset_id
33-
FROM workspace_builds
34-
WHERE template_version_preset_id IS NOT NULL
35-
ORDER BY workspace_id, build_number DESC
36-
),
26+
-- We can't rely on the template_version_preset_id in the workspace_builds table because this value is only set on the
27+
-- initial workspace creation. Subsequent stop/start transitions will not have a value for template_version_preset_id,
28+
-- and therefore we can't rely on (say) the latest build's chosen template_version_preset_id.
29+
--
30+
-- See https://github.com/coder/internal/issues/398
31+
workspaces_with_latest_presets AS (
32+
SELECT DISTINCT ON (workspace_id) workspace_id, template_version_preset_id
33+
FROM workspace_builds
34+
WHERE template_version_preset_id IS NOT NULL
35+
ORDER BY workspace_id, build_number DESC
36+
),
3737
-- workspaces_with_agents_status contains workspaces owned by the "prebuilds" user,
3838
-- along with the readiness status of their agents.
3939
-- A workspace is marked as 'ready' only if ALL of its agents are ready.
4040
workspaces_with_agents_status AS (
4141
SELECT w.id AS workspace_id,
42-
BOOL_AND(wa.lifecycle_state = 'ready'::workspace_agent_lifecycle_state) AS ready
42+
BOOL_AND(wa.lifecycle_state = 'ready'::workspace_agent_lifecycle_state) AS ready
4343
FROM workspaces w
4444
INNER JOIN workspace_latest_builds wlb ON wlb.workspace_id = w.id
4545
INNER JOIN workspace_resources wr ON wr.job_id = wlb.job_id
4646
INNER JOIN workspace_agents wa ON wa.resource_id = wr.id
4747
WHERE w.owner_id = 'c42fdf75-3097-471c-8c33-fb52454d81c0' -- The system user responsible for prebuilds.
4848
GROUP BY w.id
4949
),
50-
current_presets AS (SELECT w.id AS prebuild_id, wlp.template_version_preset_id
51-
FROM workspaces w
52-
INNER JOIN workspaces_with_latest_presets wlp ON wlp.workspace_id = w.id
53-
WHERE w.owner_id = 'c42fdf75-3097-471c-8c33-fb52454d81c0') -- The system user responsible for prebuilds.
50+
current_presets AS (SELECT w.id AS prebuild_id, wlp.template_version_preset_id
51+
FROM workspaces w
52+
INNER JOIN workspaces_with_latest_presets wlp ON wlp.workspace_id = w.id
53+
WHERE w.owner_id = 'c42fdf75-3097-471c-8c33-fb52454d81c0') -- The system user responsible for prebuilds.
5454
SELECT p.id, p.name, p.template_id, p.created_at, COALESCE(a.ready, false) AS ready, cp.template_version_preset_id AS current_preset_id
5555
FROM all_prebuilds p
56-
LEFT JOIN workspaces_with_agents_status a ON a.workspace_id = p.id
57-
INNER JOIN current_presets cp ON cp.prebuild_id = p.id;
56+
LEFT JOIN workspaces_with_agents_status a ON a.workspace_id = p.id
57+
INNER JOIN current_presets cp ON cp.prebuild_id = p.id;
5858

5959
CREATE VIEW workspace_prebuild_builds AS
6060
SELECT id, workspace_id, template_version_id, transition, job_id, template_version_preset_id, build_number
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
ALTER TABLE template_version_presets
2-
ADD COLUMN desired_instances INT NULL,
3-
ADD COLUMN invalidate_after_secs INT NULL DEFAULT 0;
2+
ADD COLUMN desired_instances INT NULL,
3+
ADD COLUMN invalidate_after_secs INT NULL DEFAULT 0;
44

55
-- We should not be able to have presets with the same name for a particular template version.
66
CREATE UNIQUE INDEX idx_unique_preset_name ON template_version_presets (name, template_version_id);

coderd/database/queries.sql.go

Lines changed: 50 additions & 48 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: 49 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,23 @@ SELECT
1515
t.deleted,
1616
t.deprecated != '' AS deprecated
1717
FROM templates t
18-
INNER JOIN template_versions tv ON tv.template_id = t.id
19-
INNER JOIN template_version_presets tvp ON tvp.template_version_id = tv.id
20-
INNER JOIN organizations o ON o.id = t.organization_id
18+
INNER JOIN template_versions tv ON tv.template_id = t.id
19+
INNER JOIN template_version_presets tvp ON tvp.template_version_id = tv.id
20+
INNER JOIN organizations o ON o.id = t.organization_id
2121
WHERE tvp.desired_instances IS NOT NULL -- Consider only presets that have a prebuild configuration.
22-
AND (t.id = sqlc.narg('template_id')::uuid OR sqlc.narg('template_id') IS NULL);
22+
AND (t.id = sqlc.narg('template_id')::uuid OR sqlc.narg('template_id') IS NULL);
2323

2424
-- name: GetRunningPrebuiltWorkspaces :many
25-
SELECT p.id,
26-
p.name,
27-
p.template_id,
28-
b.template_version_id,
29-
p.current_preset_id AS current_preset_id,
30-
p.ready,
31-
p.created_at
25+
SELECT
26+
p.id,
27+
p.name,
28+
p.template_id,
29+
b.template_version_id,
30+
p.current_preset_id AS current_preset_id,
31+
p.ready,
32+
p.created_at
3233
FROM workspace_prebuilds p
33-
INNER JOIN workspace_latest_builds b ON b.workspace_id = p.id
34+
INNER JOIN workspace_latest_builds b ON b.workspace_id = p.id
3435
WHERE (b.transition = 'start'::workspace_transition
3536
AND b.job_status = 'succeeded'::provisioner_job_status);
3637

@@ -39,14 +40,14 @@ WHERE (b.transition = 'start'::workspace_transition
3940
-- Prebuild considered in-progress if it's in the "starting", "stopping", or "deleting" state.
4041
SELECT t.id AS template_id, wpb.template_version_id, wpb.transition, COUNT(wpb.transition)::int AS count
4142
FROM workspace_latest_builds wlb
42-
INNER JOIN workspace_prebuild_builds wpb ON wpb.id = wlb.id
43-
-- We only need these counts for active template versions.
44-
-- It doesn't influence whether we create or delete prebuilds
45-
-- for inactive template versions. This is because we never create
46-
-- prebuilds for inactive template versions, we always delete
47-
-- running prebuilds for inactive template versions, and we ignore
48-
-- prebuilds that are still building.
49-
INNER JOIN templates t ON t.active_version_id = wlb.template_version_id
43+
INNER JOIN workspace_prebuild_builds wpb ON wpb.id = wlb.id
44+
-- We only need these counts for active template versions.
45+
-- It doesn't influence whether we create or delete prebuilds
46+
-- for inactive template versions. This is because we never create
47+
-- prebuilds for inactive template versions, we always delete
48+
-- running prebuilds for inactive template versions, and we ignore
49+
-- prebuilds that are still building.
50+
INNER JOIN templates t ON t.active_version_id = wlb.template_version_id
5051
WHERE wlb.job_status IN ('pending'::provisioner_job_status, 'running'::provisioner_job_status)
5152
GROUP BY t.id, wpb.template_version_id, wpb.transition;
5253

@@ -69,37 +70,38 @@ WITH filtered_builds AS (
6970
-- Only select builds which are for prebuild creations
7071
SELECT wlb.template_version_id, wlb.created_at, tvp.id AS preset_id, wlb.job_status, tvp.desired_instances
7172
FROM template_version_presets tvp
72-
INNER JOIN workspace_latest_builds wlb ON wlb.template_version_preset_id = tvp.id
73-
INNER JOIN workspaces w ON wlb.workspace_id = w.id
74-
INNER JOIN template_versions tv ON wlb.template_version_id = tv.id
75-
INNER JOIN templates t ON tv.template_id = t.id AND t.active_version_id = tv.id
73+
INNER JOIN workspace_latest_builds wlb ON wlb.template_version_preset_id = tvp.id
74+
INNER JOIN workspaces w ON wlb.workspace_id = w.id
75+
INNER JOIN template_versions tv ON wlb.template_version_id = tv.id
76+
INNER JOIN templates t ON tv.template_id = t.id AND t.active_version_id = tv.id
7677
WHERE tvp.desired_instances IS NOT NULL -- Consider only presets that have a prebuild configuration.
77-
AND wlb.transition = 'start'::workspace_transition
78-
AND w.owner_id = 'c42fdf75-3097-471c-8c33-fb52454d81c0'
78+
AND wlb.transition = 'start'::workspace_transition
79+
AND w.owner_id = 'c42fdf75-3097-471c-8c33-fb52454d81c0'
7980
),
8081
time_sorted_builds AS (
81-
-- Group builds by preset, then sort each group by created_at.
82+
-- Group builds by preset, then sort each group by created_at.
8283
SELECT fb.template_version_id, fb.created_at, fb.preset_id, fb.job_status, fb.desired_instances,
83-
ROW_NUMBER() OVER (PARTITION BY fb.preset_id ORDER BY fb.created_at DESC) as rn
84+
ROW_NUMBER() OVER (PARTITION BY fb.preset_id ORDER BY fb.created_at DESC) as rn
8485
FROM filtered_builds fb
8586
),
8687
failed_count AS (
87-
-- Count failed builds per preset in the given period
88+
-- Count failed builds per preset in the given period
8889
SELECT preset_id, COUNT(*) AS num_failed
8990
FROM filtered_builds
9091
WHERE job_status = 'failed'::provisioner_job_status
9192
AND created_at >= @lookback::timestamptz
9293
GROUP BY preset_id
9394
)
94-
SELECT tsb.template_version_id,
95-
tsb.preset_id,
96-
COALESCE(fc.num_failed, 0)::int AS num_failed,
97-
MAX(tsb.created_at)::timestamptz AS last_build_at
95+
SELECT
96+
tsb.template_version_id,
97+
tsb.preset_id,
98+
COALESCE(fc.num_failed, 0)::int AS num_failed,
99+
MAX(tsb.created_at)::timestamptz AS last_build_at
98100
FROM time_sorted_builds tsb
99-
LEFT JOIN failed_count fc ON fc.preset_id = tsb.preset_id
101+
LEFT JOIN failed_count fc ON fc.preset_id = tsb.preset_id
100102
WHERE tsb.rn <= tsb.desired_instances -- Fetch the last N builds, where N is the number of desired instances; if any fail, we backoff
101-
AND tsb.job_status = 'failed'::provisioner_job_status
102-
AND created_at >= @lookback::timestamptz
103+
AND tsb.job_status = 'failed'::provisioner_job_status
104+
AND created_at >= @lookback::timestamptz
103105
GROUP BY tsb.template_version_id, tsb.preset_id, fc.num_failed;
104106

105107
-- name: ClaimPrebuiltWorkspace :one
@@ -114,24 +116,24 @@ WHERE w.id IN (
114116
INNER JOIN templates t ON p.template_id = t.id
115117
WHERE (b.transition = 'start'::workspace_transition
116118
AND b.job_status IN ('succeeded'::provisioner_job_status))
117-
-- The prebuilds system should never try to claim a prebuild for an inactive template version.
118-
-- Nevertheless, this filter is here as a defensive measure:
119-
AND b.template_version_id = t.active_version_id
120-
AND p.current_preset_id = @preset_id::uuid
121-
AND p.ready
119+
-- The prebuilds system should never try to claim a prebuild for an inactive template version.
120+
-- Nevertheless, this filter is here as a defensive measure:
121+
AND b.template_version_id = t.active_version_id
122+
AND p.current_preset_id = @preset_id::uuid
123+
AND p.ready
122124
LIMIT 1 FOR UPDATE OF p SKIP LOCKED -- Ensure that a concurrent request will not select the same prebuild.
123125
)
124126
RETURNING w.id, w.name;
125127

126128
-- name: GetPrebuildMetrics :many
127129
SELECT
128-
t.name as template_name,
129-
tvp.name as preset_name,
130+
t.name as template_name,
131+
tvp.name as preset_name,
130132
o.name as organization_name,
131-
COUNT(*) as created_count,
132-
COUNT(*) FILTER (WHERE pj.job_status = 'failed'::provisioner_job_status) as failed_count,
133-
COUNT(*) FILTER (
134-
WHERE w.owner_id != 'c42fdf75-3097-471c-8c33-fb52454d81c0'::uuid -- The system user responsible for prebuilds.
133+
COUNT(*) as created_count,
134+
COUNT(*) FILTER (WHERE pj.job_status = 'failed'::provisioner_job_status) as failed_count,
135+
COUNT(*) FILTER (
136+
WHERE w.owner_id != 'c42fdf75-3097-471c-8c33-fb52454d81c0'::uuid -- The system user responsible for prebuilds.
135137
) as claimed_count
136138
FROM workspaces w
137139
INNER JOIN workspace_prebuild_builds wpb ON wpb.workspace_id = w.id

coderd/database/queries/presets.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,5 @@ WHERE
5252
-- name: GetPresetByID :one
5353
SELECT tvp.*, tv.template_id, tv.organization_id FROM
5454
template_version_presets tvp
55-
INNER JOIN template_versions tv ON tvp.template_version_id = tv.id
55+
INNER JOIN template_versions tv ON tvp.template_version_id = tv.id
5656
WHERE tvp.id = @preset_id;

0 commit comments

Comments
 (0)