-
Notifications
You must be signed in to change notification settings - Fork 1k
chore: optimize GetPrebuiltWorkspaces query #18717
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
Changes from 1 commit
2c48c95
68a4304
c95b6d0
6b4d6dc
53c3ba5
b237251
5dc69d9
5158046
3e69a1b
2e760c7
a0f4f36
30c86cc
1d6b4c0
6a1c635
e43b5ba
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
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 |
---|---|---|
|
@@ -48,7 +48,7 @@ WHERE tvp.desired_instances IS NOT NULL -- Consider only presets that have a pre | |
-- AND NOT t.deleted -- We don't exclude deleted templates because there's no constraint in the DB preventing a soft deletion on a template while workspaces are running. | ||
AND (t.id = sqlc.narg('template_id')::uuid OR sqlc.narg('template_id') IS NULL); | ||
|
||
-- name: GetRunningPrebuiltWorkspaces :many | ||
-- name: GetRunningPrebuiltWorkspacesOptimized :many | ||
WITH latest_prebuilds AS ( | ||
johnstcn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
SELECT | ||
workspaces.id, | ||
|
@@ -96,6 +96,23 @@ SELECT | |
FROM latest_prebuilds | ||
LEFT JOIN ready_agents ON ready_agents.job_id = latest_prebuilds.job_id | ||
LEFT JOIN workspace_latest_presets ON workspace_latest_presets.workspace_id = latest_prebuilds.id | ||
ORDER BY latest_prebuilds.id | ||
; | ||
|
||
-- name: GetRunningPrebuiltWorkspaces :many | ||
SELECT | ||
p.id, | ||
p.name, | ||
p.template_id, | ||
b.template_version_id, | ||
p.current_preset_id AS current_preset_id, | ||
p.ready, | ||
p.created_at | ||
FROM workspace_prebuilds p | ||
INNER JOIN workspace_latest_builds b ON b.workspace_id = p.id | ||
WHERE (b.transition = 'start'::workspace_transition | ||
AND b.job_status = 'succeeded'::provisioner_job_status) | ||
ORDER BY p.id; | ||
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: adding stable ordering for diffing |
||
; | ||
johnstcn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
-- name: CountInProgressPrebuilds :many | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,8 @@ import ( | |
"sync/atomic" | ||
"time" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
|
||
"github.com/hashicorp/go-multierror" | ||
"github.com/prometheus/client_golang/prometheus" | ||
johnstcn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
@@ -398,11 +400,21 @@ func (c *StoreReconciler) SnapshotState(ctx context.Context, store database.Stor | |
return xerrors.Errorf("failed to get preset prebuild schedules: %w", err) | ||
} | ||
|
||
// Get results from both original and optimized queries for comparison | ||
allRunningPrebuilds, err := db.GetRunningPrebuiltWorkspaces(ctx) | ||
if err != nil { | ||
return xerrors.Errorf("failed to get running prebuilds: %w", err) | ||
} | ||
|
||
// Compare with optimized query to ensure behavioral correctness | ||
optimized, err := db.GetRunningPrebuiltWorkspacesOptimized(ctx) | ||
if err != nil { | ||
// Log the error but continue with original results | ||
c.logger.Error(ctx, "optimized GetRunningPrebuiltWorkspacesOptimized failed", slog.Error(err)) | ||
} else { | ||
CompareGetRunningPrebuiltWorkspacesResults(ctx, c.logger, allRunningPrebuilds, optimized) | ||
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. This is a fun way to test it out 😄 |
||
} | ||
|
||
allPrebuildsInProgress, err := db.CountInProgressPrebuilds(ctx) | ||
if err != nil { | ||
return xerrors.Errorf("failed to get prebuilds in progress: %w", err) | ||
|
@@ -922,3 +934,29 @@ func SetPrebuildsReconciliationPaused(ctx context.Context, db database.Store, pa | |
} | ||
return db.UpsertPrebuildsSettings(ctx, string(settingsJSON)) | ||
} | ||
|
||
// CompareGetRunningPrebuiltWorkspacesResults compares the original and optimized | ||
// query results and logs any differences found. This function can be easily | ||
// removed once we're confident the optimized query works correctly. | ||
johnstcn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
func CompareGetRunningPrebuiltWorkspacesResults( | ||
ctx context.Context, | ||
logger slog.Logger, | ||
original []database.GetRunningPrebuiltWorkspacesRow, | ||
optimized []database.GetRunningPrebuiltWorkspacesOptimizedRow, | ||
) { | ||
// Convert optimized results to the same type as original for comparison | ||
var optimizedConverted []database.GetRunningPrebuiltWorkspacesRow | ||
if original != nil { | ||
optimizedConverted := make([]database.GetRunningPrebuiltWorkspacesRow, len(optimized)) | ||
for i, row := range optimized { | ||
optimizedConverted[i] = database.GetRunningPrebuiltWorkspacesRow(row) | ||
} | ||
} | ||
|
||
// Compare the results and log an error if they differ. | ||
// NOTE: explicitly not sorting here as both query results are ordered by ID. | ||
if diff := cmp.Diff(original, optimizedConverted); diff != "" { | ||
logger.Error(ctx, "results differ for GetRunningPrebuiltWorkspacesOptimized", | ||
slog.F("diff", diff)) | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.