Files
coder/coderd/database/queries/presets.sql
Yevhenii Shcherbina 53e8e9c7cd fix: reduce cost of prebuild failure (#17697)
Relates to https://github.com/coder/coder/issues/17432

### Part 1:

Notes:
- `GetPresetsAtFailureLimit` SQL query is added, which is similar to
`GetPresetsBackoff`, they use same CTEs: `filtered_builds`,
`time_sorted_builds`, but they are still different.

- Query is executed on every loop iteration. We can consider marking
specific preset as permanently failed as an optimization to avoid
executing query on every loop iteration. But I decided don't do it for
now.

- By default `FailureHardLimit` is set to 3.

- `FailureHardLimit` is configurable. Setting it to zero - means that
hard limit is disabled.

### Part 2

Notes:
- `PrebuildFailureLimitReached` notification is added.
- Notification is sent to template admins.
- Notification is sent only the first time, when hard limit is reached.
But it will `log.Warn` on every loop iteration.
- I introduced this enum:
```sql
CREATE TYPE prebuild_status AS ENUM (
  'normal',           -- Prebuilds are working as expected; this is the default, healthy state.
  'hard_limited',     -- Prebuilds have failed repeatedly and hit the configured hard failure limit; won't be retried anymore.
  'validation_failed' -- Prebuilds failed due to a non-retryable validation error (e.g. template misconfiguration); won't be retried.
);
```
`validation_failed` not used in this PR, but I think it will be used in
next one, so I wanted to save us an extra migration.

- Notification looks like this:
<img width="472" alt="image"
src="https://github.com/user-attachments/assets/e10efea0-1790-4e7f-a65c-f94c40fced27"
/>

### Latest notification views:
<img width="463" alt="image"
src="https://github.com/user-attachments/assets/11310c58-68d1-4075-a497-f76d854633fe"
/>
<img width="725" alt="image"
src="https://github.com/user-attachments/assets/6bbfe21a-91ac-47c3-a9d1-21807bb0c53a"
/>
2025-05-21 15:16:38 -04:00

72 lines
1.7 KiB
SQL

-- name: InsertPreset :one
INSERT INTO template_version_presets (
id,
template_version_id,
name,
created_at,
desired_instances,
invalidate_after_secs
)
VALUES (
@id,
@template_version_id,
@name,
@created_at,
@desired_instances,
@invalidate_after_secs
) RETURNING *;
-- name: InsertPresetParameters :many
INSERT INTO
template_version_preset_parameters (template_version_preset_id, name, value)
SELECT
@template_version_preset_id,
unnest(@names :: TEXT[]),
unnest(@values :: TEXT[])
RETURNING *;
-- name: UpdatePresetPrebuildStatus :exec
UPDATE template_version_presets
SET prebuild_status = @status
WHERE id = @preset_id;
-- name: GetPresetsByTemplateVersionID :many
SELECT
*
FROM
template_version_presets
WHERE
template_version_id = @template_version_id;
-- name: GetPresetByWorkspaceBuildID :one
SELECT
template_version_presets.*
FROM
template_version_presets
INNER JOIN workspace_builds ON workspace_builds.template_version_preset_id = template_version_presets.id
WHERE
workspace_builds.id = @workspace_build_id;
-- name: GetPresetParametersByTemplateVersionID :many
SELECT
template_version_preset_parameters.*
FROM
template_version_preset_parameters
INNER JOIN template_version_presets ON template_version_preset_parameters.template_version_preset_id = template_version_presets.id
WHERE
template_version_presets.template_version_id = @template_version_id;
-- name: GetPresetParametersByPresetID :many
SELECT
tvpp.*
FROM
template_version_preset_parameters tvpp
WHERE
tvpp.template_version_preset_id = @preset_id;
-- name: GetPresetByID :one
SELECT tvp.*, tv.template_id, tv.organization_id FROM
template_version_presets tvp
INNER JOIN template_versions tv ON tvp.template_version_id = tv.id
WHERE tvp.id = @preset_id;