-
Notifications
You must be signed in to change notification settings - Fork 956
fix(coderd/prometheusmetrics)!: filter deleted wsbuilds to reduce db load #19197
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -247,6 +247,32 @@ func TestWorkspaceLatestBuildTotals(t *testing.T) { | |
codersdk.ProvisionerJobSucceeded: 3, | ||
codersdk.ProvisionerJobRunning: 1, | ||
}, | ||
}, { | ||
Name: "MultipleWithDeleted", | ||
Database: func() database.Store { | ||
db, _ := dbtestutil.NewDB(t) | ||
u := dbgen.User(t, db, database.User{}) | ||
org := dbgen.Organization(t, db, database.Organization{}) | ||
insertCanceled(t, db, u, org) | ||
insertFailed(t, db, u, org) | ||
insertSuccess(t, db, u, org) | ||
insertRunning(t, db, u, org) | ||
|
||
// Verify that deleted workspaces/builds are NOT counted in metrics. | ||
n, err := cryptorand.Intn(5) | ||
require.NoError(t, err) | ||
for range 1 + n { | ||
insertDeleted(t, db, u, org) | ||
} | ||
return db | ||
}, | ||
Total: 4, // Only non-deleted workspaces should be counted | ||
Status: map[codersdk.ProvisionerJobStatus]int{ | ||
codersdk.ProvisionerJobCanceled: 1, | ||
codersdk.ProvisionerJobFailed: 1, | ||
codersdk.ProvisionerJobSucceeded: 1, | ||
codersdk.ProvisionerJobRunning: 1, | ||
}, | ||
}} { | ||
t.Run(tc.Name, func(t *testing.T) { | ||
t.Parallel() | ||
|
@@ -323,6 +349,33 @@ func TestWorkspaceLatestBuildStatuses(t *testing.T) { | |
codersdk.ProvisionerJobSucceeded: 3, | ||
codersdk.ProvisionerJobRunning: 1, | ||
}, | ||
}, { | ||
Name: "MultipleWithDeleted", | ||
Database: func() database.Store { | ||
db, _ := dbtestutil.NewDB(t) | ||
u := dbgen.User(t, db, database.User{}) | ||
org := dbgen.Organization(t, db, database.Organization{}) | ||
insertTemplates(t, db, u, org) | ||
insertCanceled(t, db, u, org) | ||
insertFailed(t, db, u, org) | ||
insertSuccess(t, db, u, org) | ||
insertRunning(t, db, u, org) | ||
|
||
// Verify that deleted workspaces/builds are NOT counted in metrics. | ||
n, err := cryptorand.Intn(5) | ||
require.NoError(t, err) | ||
for range 1 + n { | ||
insertDeleted(t, db, u, org) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Review: This fails in the old implementation, but I thought it best to formalize the assumption. Considering the old implementation did not test for this, it seems like validation for the new approach. |
||
return db | ||
}, | ||
ExpectedWorkspaces: 4, // Only non-deleted workspaces should be counted | ||
ExpectedStatuses: map[codersdk.ProvisionerJobStatus]int{ | ||
codersdk.ProvisionerJobCanceled: 1, | ||
codersdk.ProvisionerJobFailed: 1, | ||
codersdk.ProvisionerJobSucceeded: 1, | ||
codersdk.ProvisionerJobRunning: 1, | ||
}, | ||
}} { | ||
t.Run(tc.Name, func(t *testing.T) { | ||
t.Parallel() | ||
|
@@ -907,3 +960,24 @@ func insertSuccess(t *testing.T, db database.Store, u database.User, org databas | |
}) | ||
require.NoError(t, err) | ||
} | ||
|
||
func insertDeleted(t *testing.T, db database.Store, u database.User, org database.Organization) { | ||
job := insertRunning(t, db, u, org) | ||
err := db.UpdateProvisionerJobWithCompleteByID(context.Background(), database.UpdateProvisionerJobWithCompleteByIDParams{ | ||
ID: job.ID, | ||
CompletedAt: sql.NullTime{ | ||
Time: dbtime.Now(), | ||
Valid: true, | ||
}, | ||
}) | ||
require.NoError(t, err) | ||
|
||
build, err := db.GetWorkspaceBuildByJobID(context.Background(), job.ID) | ||
require.NoError(t, err) | ||
|
||
err = db.UpdateWorkspaceDeletedByID(context.Background(), database.UpdateWorkspaceDeletedByIDParams{ | ||
ID: build.WorkspaceID, | ||
Deleted: true, | ||
}) | ||
require.NoError(t, err) | ||
} |
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.
Less queries! ❤️