You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
@@ -43,17 +43,22 @@ FROM workspace_latest_builds wlb
WHERE pj.job_status IN ('pending'::provisioner_job_status, 'running'::provisioner_job_status)
GROUP BY t.id, wpb.template_version_id, wpb.transition;
-- name: GetPresetsBackoff :many
-- GetPresetsBackoff groups workspace builds by template version ID.
-- For each group, the query checks up to N of the most recent jobs that occurred within the
-- lookback period, where N equals the number of desired instances for the corresponding preset.
-- For each group, the query checks the last N jobs, where N equals the number of desired instances for the corresponding preset.
-- If at least one of the last N jobs has failed, we should backoff on the corresponding template version ID.
-- Query returns a list of template version IDs for which we should backoff.
-- Only active template versions with configured presets are considered.
--
-- NOTE:
-- We only consider jobs that occurred within the lookback period; any failures that happened before this period are ignored.
-- We also return the number of failed workspace builds, which is used downstream to determine the backoff duration.
-- name: GetPresetsBackoff :many
-- We back off on the template version ID if at least one of the N latest workspace builds has failed.
-- However, we also return the number of failed workspace builds that occurred during the lookback period.
--
-- In other words:
-- - To **decide whether to back off**, we look at the N most recent builds (regardless of when they happened).
-- - To **calculate the number of failed builds**, we consider all builds within the defined lookback period.
--
-- The number of failed builds is used downstream to determine the backoff duration.
WITH filtered_builds AS (
-- Only select builds which are for prebuild creations
SELECT wlb.*, tvp.id AS preset_id, pj.job_status, tvp.desired_instances
Expand All
@@ -63,23 +68,31 @@ WITH filtered_builds AS (
INNER JOIN template_versions tv ON wlb.template_version_id = tv.id
INNER JOIN templates t ON tv.template_id = t.id AND t.active_version_id = tv.id
WHERE tvp.desired_instances IS NOT NULL -- Consider only presets that have a prebuild configuration.
AND wlb.transition = 'start'::workspace_transition
AND wlb.transition = 'start'::workspace_transition
),
time_sorted_builds AS (
-- Group builds by template version, then sort each group by created_at.
SELECT fb.*,
ROW_NUMBER() OVER (PARTITION BY fb.template_version_preset_id ORDER BY fb.created_at DESC) as rn
FROM filtered_builds fb
),
failed_count AS (
-- Count failed builds per template version/preset in the given period
SELECT preset_id, COUNT(*) AS num_failed
FROM filtered_builds
WHERE job_status = 'failed'::provisioner_job_status
AND created_at >= @lookback::timestamptz
GROUP BY preset_id
)
SELECT tsb.template_version_id,
tsb.preset_id,
COUNT(*)::int AS num_failed, -- Count failed builds per template version/preset in the given period
COALESCE(fc.num_failed, 0)::intAS num_failed,
MAX(tsb.created_at::timestamptz) AS last_build_at
FROM time_sorted_builds tsb
LEFT JOIN failed_count fc ON fc.preset_id = tsb.preset_id
WHERE tsb.rn <= tsb.desired_instances -- Fetch the last N builds, where N is the number of desired instances; if any fail, we backoff
AND tsb.job_status = 'failed'::provisioner_job_status
AND created_at >= @lookback::timestamptz
GROUP BY tsb.template_version_id, tsb.preset_id;
GROUP BY tsb.template_version_id, tsb.preset_id, fc.num_failed;
-- name: ClaimPrebuild :one
UPDATE workspaces w
Expand Down
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.