Skip to content

fix: fix workspace status filter returning more statuses that requested #7732

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

Merged
merged 14 commits into from
Jun 5, 2023

Conversation

Emyrk
Copy link
Member

@Emyrk Emyrk commented May 31, 2023

Fixes the postgres condition to not make failed workspaces show up with the deleted status filter.
Also fixed the dbfake to match the postgres implementation. It was acting a lot differently.

See #7685 (comment)
Fixes: #7685

Testing

Made a unit test to test this logic. Doing it on a real DB is slow, so going to skip in CI 😢. Took a bit of work to get the test setup.

Comment on lines +30 to +35
var genCtx = dbauthz.As(context.Background(), rbac.Subject{
ID: "owner",
Roles: rbac.Roles(must(rbac.RoleNames{rbac.RoleOwner()}.Expand())),
Groups: []string{},
Scope: rbac.ExpandableScope(rbac.ScopeAll),
})
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is so dbgen can work on dbauthz wrapped databases for unit testing.

Comment on lines 160 to 165
latest_build.transition = 'delete'::workspace_transition
latest_build.transition = 'delete'::workspace_transition AND
-- If the error field is null, the status is 'failed'
latest_build.error IS NULL

WHEN @status = 'deleting' THEN
latest_build.completed_at IS NOT NULL AND
latest_build.completed_at IS NULL AND
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the only fix needed for prod. All other fixes were just for the dbfake

@Emyrk Emyrk changed the title chore: Workspace status filter returning more statuses fix: Workspace status filter returning more statuses May 31, 2023
@Emyrk Emyrk requested a review from Kira-Pilot May 31, 2023 16:50
@ammario ammario changed the title fix: Workspace status filter returning more statuses fix: workspace status filter returning more statuses May 31, 2023

WHEN @status = 'deleting' THEN
latest_build.completed_at IS NOT NULL AND
latest_build.completed_at IS NULL AND
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so, it's really difficult to read this query and convince yourself that these WHERE clauses are

a) non-overlapping
b) match the golang implementation of the status computation

What do you think about the strategy of introducing a stored procedure that computes the status? Then the filter logic in this query becomes a trivial WHERE clause, and we can use the DB-computed status in prod, rather than computing the status in the API layer.

Annoyingly, we'd still have to keep the golang implementation around for the dbfake, but they could both be written in an imperative style and so it will be much easier to verify the algorithm matches.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh it was a headache. Are we using stored procedures anywhere else, or would this be the first case? I'm open to anything that makes this easier.

Ideally though, it would be in the next PR so this bug is fixed in main. The major contribution from this PR is the test imo.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have triggers for deletion of tokens stored in the DB. This would be the first computed column, though.

Fine to follow up in another PR.

}, database.ProvisionerJob{
StartedAt: sql.NullTime{Time: time.Now().Add(time.Second * -2), Valid: true},
CompletedAt: sql.NullTime{Time: time.Now(), Valid: true},
}, database.WorkspaceTransitionDelete)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these tests are great and all, but they are not combinatorially exhaustive on null vs non-null for all fields. This is another reason I think a stored procedure for the status is better.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is definitely not exhaustive. The annoying thing too is when we prepulate this much data (which isn't even that much), the postgres test gets so slow. So I skipped it in CI, meaning this bug could easily pop up again silently 😢.

I was debating about putting in a batch insert SQL query outside our typical db interface and see if it is faster. If we go the stored procedure route, might be worth investigating how to make the postgres test run in a reasonable amount of time.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if the slowness is related to how we have to create the job, then acquire it, then fail/complete it. Acquiring in particular might be slow because of the locking we do.

In testing we could have some queries that directly insert the job in the state we want it. Not exposed on the main Store interface, but available if you type assert to a test type.

@Emyrk Emyrk changed the title fix: workspace status filter returning more statuses fix: fix workspace status filter returning more statuses that requested Jun 1, 2023
@Kira-Pilot
Copy link
Member

@Emyrk I tried this out locally and failed workspaces do not show up with the deleted status filter - awesome.
I am a bit confused about the status behavior in general, however. If I have a deleted workspace and I filter by status:deleted, nothing shows up. This is confusing to me given the ticket description. Am I missing something?
Screenshot 2023-06-01 at 11 20 03 AM
Screenshot 2023-06-01 at 11 20 23 AM

@Emyrk
Copy link
Member Author

Emyrk commented Jun 5, 2023

@Kira-Pilot could it be "deleting"?
Can you send the "all workspaces" response?

@Emyrk
Copy link
Member Author

Emyrk commented Jun 5, 2023

@Emyrk I tried this out locally and failed workspaces do not show up with the deleted status filter - awesome. I am a bit confused about the status behavior in general, however. If I have a deleted workspace and I filter by status:deleted, nothing shows up. This is confusing to me given the ticket description. Am I missing something? Screenshot 2023-06-01 at 11 20 03 AM Screenshot 2023-06-01 at 11 20 23 AM

Ah, so that is a different bug actually.

deleted is a special case handled in the sql here:

-- Optionally include deleted workspaces
workspaces.deleted = @deleted

What I fixed was the list of statuses. So if you search status:deleted nothing shows up as we do not show deleted workspaces to users right now.

So that is expected behavior.

@Emyrk Emyrk merged commit fa8f50a into main Jun 5, 2023
@Emyrk Emyrk deleted the stevenmasley/workspace_search_failed branch June 5, 2023 23:12
@github-actions github-actions bot locked and limited conversation to collaborators Jun 5, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BE Filtering: When filtering for "deleted", the API is returning workspaces with status "failed"
3 participants