-
Notifications
You must be signed in to change notification settings - Fork 914
fix(coderd): mark sub agent deletion via boolean instead of delete #18411
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mafredri
wants to merge
6
commits into
main
Choose a base branch
from
mafredri/fix-subagent-deletion
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+355
−38
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7d73585
fix(coderd): mark sub agent deletion via boolean instead of delete
mafredri 106e8ca
fix prebuilds view
mafredri 1c84433
fix order
mafredri 774337d
add log
mafredri 609f407
add comment to a few more places
mafredri bc370a8
troubleshooting "url"
mafredri File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
96 changes: 96 additions & 0 deletions
96
coderd/database/migrations/000338_use_deleted_boolean_for_subagents.down.sql
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
-- Restore prebuilds, previously modified in 000323_workspace_latest_builds_optimization.up.sql. | ||
DROP VIEW workspace_prebuilds; | ||
|
||
CREATE VIEW workspace_prebuilds AS | ||
WITH all_prebuilds AS ( | ||
SELECT w.id, | ||
w.name, | ||
w.template_id, | ||
w.created_at | ||
FROM workspaces w | ||
WHERE (w.owner_id = 'c42fdf75-3097-471c-8c33-fb52454d81c0'::uuid) | ||
), workspaces_with_latest_presets AS ( | ||
SELECT DISTINCT ON (workspace_builds.workspace_id) workspace_builds.workspace_id, | ||
workspace_builds.template_version_preset_id | ||
FROM workspace_builds | ||
WHERE (workspace_builds.template_version_preset_id IS NOT NULL) | ||
ORDER BY workspace_builds.workspace_id, workspace_builds.build_number DESC | ||
), workspaces_with_agents_status AS ( | ||
SELECT w.id AS workspace_id, | ||
bool_and((wa.lifecycle_state = 'ready'::workspace_agent_lifecycle_state)) AS ready | ||
FROM (((workspaces w | ||
JOIN workspace_latest_builds wlb ON ((wlb.workspace_id = w.id))) | ||
JOIN workspace_resources wr ON ((wr.job_id = wlb.job_id))) | ||
JOIN workspace_agents wa ON ((wa.resource_id = wr.id))) | ||
WHERE (w.owner_id = 'c42fdf75-3097-471c-8c33-fb52454d81c0'::uuid) | ||
GROUP BY w.id | ||
), current_presets AS ( | ||
SELECT w.id AS prebuild_id, | ||
wlp.template_version_preset_id | ||
FROM (workspaces w | ||
JOIN workspaces_with_latest_presets wlp ON ((wlp.workspace_id = w.id))) | ||
WHERE (w.owner_id = 'c42fdf75-3097-471c-8c33-fb52454d81c0'::uuid) | ||
) | ||
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 | ||
FROM ((all_prebuilds p | ||
LEFT JOIN workspaces_with_agents_status a ON ((a.workspace_id = p.id))) | ||
JOIN current_presets cp ON ((cp.prebuild_id = p.id))); | ||
|
||
-- Restore trigger without deleted check. | ||
DROP TRIGGER IF EXISTS workspace_agent_name_unique_trigger ON workspace_agents; | ||
DROP FUNCTION IF EXISTS check_workspace_agent_name_unique(); | ||
|
||
CREATE OR REPLACE FUNCTION check_workspace_agent_name_unique() | ||
RETURNS TRIGGER AS $$ | ||
DECLARE | ||
workspace_build_id uuid; | ||
agents_with_name int; | ||
BEGIN | ||
-- Find the workspace build the workspace agent is being inserted into. | ||
SELECT workspace_builds.id INTO workspace_build_id | ||
FROM workspace_resources | ||
JOIN workspace_builds ON workspace_builds.job_id = workspace_resources.job_id | ||
WHERE workspace_resources.id = NEW.resource_id; | ||
|
||
-- If the agent doesn't have a workspace build, we'll allow the insert. | ||
IF workspace_build_id IS NULL THEN | ||
RETURN NEW; | ||
END IF; | ||
|
||
-- Count how many agents in this workspace build already have the given agent name. | ||
SELECT COUNT(*) INTO agents_with_name | ||
FROM workspace_agents | ||
JOIN workspace_resources ON workspace_resources.id = workspace_agents.resource_id | ||
JOIN workspace_builds ON workspace_builds.job_id = workspace_resources.job_id | ||
WHERE workspace_builds.id = workspace_build_id | ||
AND workspace_agents.name = NEW.name | ||
AND workspace_agents.id != NEW.id; | ||
|
||
-- If there's already an agent with this name, raise an error | ||
IF agents_with_name > 0 THEN | ||
RAISE EXCEPTION 'workspace agent name "%" already exists in this workspace build', NEW.name | ||
USING ERRCODE = 'unique_violation'; | ||
END IF; | ||
|
||
RETURN NEW; | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
|
||
CREATE TRIGGER workspace_agent_name_unique_trigger | ||
BEFORE INSERT OR UPDATE OF name, resource_id ON workspace_agents | ||
FOR EACH ROW | ||
EXECUTE FUNCTION check_workspace_agent_name_unique(); | ||
|
||
COMMENT ON TRIGGER workspace_agent_name_unique_trigger ON workspace_agents IS | ||
'Use a trigger instead of a unique constraint because existing data may violate | ||
the uniqueness requirement. A trigger allows us to enforce uniqueness going | ||
forward without requiring a migration to clean up historical data.'; | ||
|
||
|
||
ALTER TABLE workspace_agents | ||
DROP COLUMN deleted; |
99 changes: 99 additions & 0 deletions
99
coderd/database/migrations/000338_use_deleted_boolean_for_subagents.up.sql
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
ALTER TABLE workspace_agents | ||
ADD COLUMN deleted BOOLEAN NOT NULL DEFAULT FALSE; | ||
|
||
COMMENT ON COLUMN workspace_agents.deleted IS 'Indicates whether or not the agent has been deleted. This is currently only applicable to sub agents.'; | ||
|
||
-- Recreate the trigger with deleted check. | ||
DROP TRIGGER IF EXISTS workspace_agent_name_unique_trigger ON workspace_agents; | ||
DROP FUNCTION IF EXISTS check_workspace_agent_name_unique(); | ||
|
||
CREATE OR REPLACE FUNCTION check_workspace_agent_name_unique() | ||
RETURNS TRIGGER AS $$ | ||
DECLARE | ||
workspace_build_id uuid; | ||
agents_with_name int; | ||
BEGIN | ||
-- Find the workspace build the workspace agent is being inserted into. | ||
SELECT workspace_builds.id INTO workspace_build_id | ||
FROM workspace_resources | ||
JOIN workspace_builds ON workspace_builds.job_id = workspace_resources.job_id | ||
WHERE workspace_resources.id = NEW.resource_id; | ||
|
||
-- If the agent doesn't have a workspace build, we'll allow the insert. | ||
IF workspace_build_id IS NULL THEN | ||
RETURN NEW; | ||
END IF; | ||
|
||
-- Count how many agents in this workspace build already have the given agent name. | ||
SELECT COUNT(*) INTO agents_with_name | ||
FROM workspace_agents | ||
JOIN workspace_resources ON workspace_resources.id = workspace_agents.resource_id | ||
JOIN workspace_builds ON workspace_builds.job_id = workspace_resources.job_id | ||
WHERE workspace_builds.id = workspace_build_id | ||
AND workspace_agents.name = NEW.name | ||
AND workspace_agents.id != NEW.id | ||
AND workspace_agents.deleted = FALSE; -- Ensure we only count non-deleted agents. | ||
|
||
-- If there's already an agent with this name, raise an error | ||
IF agents_with_name > 0 THEN | ||
RAISE EXCEPTION 'workspace agent name "%" already exists in this workspace build', NEW.name | ||
USING ERRCODE = 'unique_violation'; | ||
END IF; | ||
|
||
RETURN NEW; | ||
END; | ||
$$ LANGUAGE plpgsql; | ||
|
||
CREATE TRIGGER workspace_agent_name_unique_trigger | ||
BEFORE INSERT OR UPDATE OF name, resource_id ON workspace_agents | ||
FOR EACH ROW | ||
EXECUTE FUNCTION check_workspace_agent_name_unique(); | ||
|
||
COMMENT ON TRIGGER workspace_agent_name_unique_trigger ON workspace_agents IS | ||
'Use a trigger instead of a unique constraint because existing data may violate | ||
the uniqueness requirement. A trigger allows us to enforce uniqueness going | ||
forward without requiring a migration to clean up historical data.'; | ||
|
||
-- Handle agent deletion in prebuilds, previously modified in 000323_workspace_latest_builds_optimization.up.sql. | ||
DROP VIEW workspace_prebuilds; | ||
|
||
CREATE VIEW workspace_prebuilds AS | ||
WITH all_prebuilds AS ( | ||
SELECT w.id, | ||
w.name, | ||
w.template_id, | ||
w.created_at | ||
FROM workspaces w | ||
WHERE (w.owner_id = 'c42fdf75-3097-471c-8c33-fb52454d81c0'::uuid) | ||
), workspaces_with_latest_presets AS ( | ||
SELECT DISTINCT ON (workspace_builds.workspace_id) workspace_builds.workspace_id, | ||
workspace_builds.template_version_preset_id | ||
FROM workspace_builds | ||
WHERE (workspace_builds.template_version_preset_id IS NOT NULL) | ||
ORDER BY workspace_builds.workspace_id, workspace_builds.build_number DESC | ||
), workspaces_with_agents_status AS ( | ||
SELECT w.id AS workspace_id, | ||
bool_and((wa.lifecycle_state = 'ready'::workspace_agent_lifecycle_state)) AS ready | ||
FROM (((workspaces w | ||
JOIN workspace_latest_builds wlb ON ((wlb.workspace_id = w.id))) | ||
JOIN workspace_resources wr ON ((wr.job_id = wlb.job_id))) | ||
-- ADD: deleted check for sub agents. | ||
JOIN workspace_agents wa ON ((wa.resource_id = wr.id AND wa.deleted = FALSE))) | ||
WHERE (w.owner_id = 'c42fdf75-3097-471c-8c33-fb52454d81c0'::uuid) | ||
GROUP BY w.id | ||
), current_presets AS ( | ||
SELECT w.id AS prebuild_id, | ||
wlp.template_version_preset_id | ||
FROM (workspaces w | ||
JOIN workspaces_with_latest_presets wlp ON ((wlp.workspace_id = w.id))) | ||
WHERE (w.owner_id = 'c42fdf75-3097-471c-8c33-fb52454d81c0'::uuid) | ||
) | ||
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 | ||
FROM ((all_prebuilds p | ||
LEFT JOIN workspaces_with_agents_status a ON ((a.workspace_id = p.id))) | ||
JOIN current_presets cp ON ((cp.prebuild_id = p.id))); |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Review: This may be a bit unusual, but it already helped me catch an issue with the
workspace_prebuilds
VIEW.Ideally this potential cause would somehow be propagated to the user in case of test failure, but not sure how that could be done. I'll add a log entry to help with discovery.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this a lot! I do agree that it could get confusing to troubleshoot, but the log message in combination with
dbtestutil.DumpOnFailure
should help. You could also potentially set some relevant fields of the sub-agent so that its purpose is clear.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about this? 😆
bc370a8
(#18411)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SATISFACTORY