- Notifications
You must be signed in to change notification settings - Fork1.1k
feat: add prebuilds reconciliation duration metric#20535
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
ssncferreira merged 4 commits intomainfromssncferreira/feat-prebuilds-reconciliation-duration-metricOct 29, 2025
+125 −31
Merged
Changes fromall commits
Commits
Show all changes
4 commits Select commitHold shift + click to select a range
19fa823 feat: add prebuilds reconciliation duration metric
ssncferreira9e9e56d Merge remote-tracking branch 'origin/main' into ssncferreira/feat-pre…
ssncferreiraddd9490 chore: update tests with better time precision
ssncferreira448bd32 Merge remote-tracking branch 'origin/main' into ssncferreira/feat-pre…
ssncferreiraFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
7 changes: 6 additions & 1 deletioncoderd/prebuilds/api.go
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
6 changes: 5 additions & 1 deletioncoderd/prebuilds/noop.go
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
6 changes: 3 additions & 3 deletionsenterprise/coderd/prebuilds/metricscollector_test.go
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
36 changes: 30 additions & 6 deletionsenterprise/coderd/prebuilds/reconcile.go
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
101 changes: 81 additions & 20 deletionsenterprise/coderd/prebuilds/reconcile_test.go
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 |
|---|---|---|
| @@ -72,7 +72,8 @@ func TestNoReconciliationActionsIfNoPresets(t *testing.T) { | ||
| require.Equal(t, templateVersion, gotTemplateVersion) | ||
| // when we trigger the reconciliation loop for all templates | ||
| _, err = controller.ReconcileAll(ctx) | ||
| require.NoError(t, err) | ||
| // then no reconciliation actions are taken | ||
| // because without presets, there are no prebuilds | ||
| @@ -126,7 +127,8 @@ func TestNoReconciliationActionsIfNoPrebuilds(t *testing.T) { | ||
| require.NotEmpty(t, presetParameters) | ||
| // when we trigger the reconciliation loop for all templates | ||
| _, err = controller.ReconcileAll(ctx) | ||
| require.NoError(t, err) | ||
| // then no reconciliation actions are taken | ||
| // because without prebuilds, there is nothing to reconcile | ||
| @@ -428,7 +430,8 @@ func (tc testCase) run(t *testing.T) { | ||
| // Run the reconciliation multiple times to ensure idempotency | ||
| // 8 was arbitrary, but large enough to reasonably trust the result | ||
| for i := 1; i <= 8; i++ { | ||
| _, err := controller.ReconcileAll(ctx) | ||
| require.NoErrorf(t, err, "failed on iteration %d", i) | ||
| if tc.shouldCreateNewPrebuild != nil { | ||
| newPrebuildCount := 0 | ||
| @@ -542,7 +545,8 @@ func TestMultiplePresetsPerTemplateVersion(t *testing.T) { | ||
| // Run the reconciliation multiple times to ensure idempotency | ||
| // 8 was arbitrary, but large enough to reasonably trust the result | ||
| for i := 1; i <= 8; i++ { | ||
| _, err := controller.ReconcileAll(ctx) | ||
| require.NoErrorf(t, err, "failed on iteration %d", i) | ||
| newPrebuildCount := 0 | ||
| workspaces, err := db.GetWorkspacesByTemplateID(ctx, template.ID) | ||
| @@ -668,7 +672,7 @@ func TestPrebuildScheduling(t *testing.T) { | ||
| DesiredInstances: 5, | ||
| }) | ||
| _,err := controller.ReconcileAll(ctx) | ||
| require.NoError(t, err) | ||
| // get workspace builds | ||
| @@ -751,7 +755,8 @@ func TestInvalidPreset(t *testing.T) { | ||
| // Run the reconciliation multiple times to ensure idempotency | ||
| // 8 was arbitrary, but large enough to reasonably trust the result | ||
| for i := 1; i <= 8; i++ { | ||
| _, err := controller.ReconcileAll(ctx) | ||
| require.NoErrorf(t, err, "failed on iteration %d", i) | ||
| workspaces, err := db.GetWorkspacesByTemplateID(ctx, template.ID) | ||
| require.NoError(t, err) | ||
| @@ -817,7 +822,8 @@ func TestDeletionOfPrebuiltWorkspaceWithInvalidPreset(t *testing.T) { | ||
| }) | ||
| // Old prebuilt workspace should be deleted. | ||
| _, err = controller.ReconcileAll(ctx) | ||
| require.NoError(t, err) | ||
| builds, err := db.GetWorkspaceBuildsByWorkspaceID(ctx, database.GetWorkspaceBuildsByWorkspaceIDParams{ | ||
| WorkspaceID: prebuiltWorkspace.ID, | ||
| @@ -916,12 +922,15 @@ func TestSkippingHardLimitedPresets(t *testing.T) { | ||
| // Trigger reconciliation to attempt creating a new prebuild. | ||
| // The outcome depends on whether the hard limit has been reached. | ||
| _, err = controller.ReconcileAll(ctx) | ||
| require.NoError(t, err) | ||
| // These two additional calls to ReconcileAll should not trigger any notifications. | ||
| // A notification is only sent once. | ||
| _, err = controller.ReconcileAll(ctx) | ||
| require.NoError(t, err) | ||
| _, err = controller.ReconcileAll(ctx) | ||
| require.NoError(t, err) | ||
| // Verify the final state after reconciliation. | ||
| workspaces, err = db.GetWorkspacesByTemplateID(ctx, template.ID) | ||
| @@ -1093,12 +1102,15 @@ func TestHardLimitedPresetShouldNotBlockDeletion(t *testing.T) { | ||
| // Trigger reconciliation to attempt creating a new prebuild. | ||
| // The outcome depends on whether the hard limit has been reached. | ||
| _, err = controller.ReconcileAll(ctx) | ||
| require.NoError(t, err) | ||
| // These two additional calls to ReconcileAll should not trigger any notifications. | ||
| // A notification is only sent once. | ||
| _, err = controller.ReconcileAll(ctx) | ||
| require.NoError(t, err) | ||
| _, err = controller.ReconcileAll(ctx) | ||
| require.NoError(t, err) | ||
| // Verify the final state after reconciliation. | ||
| // When hard limit is reached, no new workspace should be created. | ||
| @@ -1141,7 +1153,8 @@ func TestHardLimitedPresetShouldNotBlockDeletion(t *testing.T) { | ||
| } | ||
| // Trigger reconciliation to make sure that successful, but outdated prebuilt workspace will be deleted. | ||
| _, err = controller.ReconcileAll(ctx) | ||
| require.NoError(t, err) | ||
| workspaces, err = db.GetWorkspacesByTemplateID(ctx, template.ID) | ||
| require.NoError(t, err) | ||
| @@ -1740,7 +1753,8 @@ func TestExpiredPrebuildsMultipleActions(t *testing.T) { | ||
| } | ||
| // Trigger reconciliation to process expired prebuilds and enforce desired state. | ||
| _, err = controller.ReconcileAll(ctx) | ||
| require.NoError(t, err) | ||
| // Sort non-expired workspaces by CreatedAt in ascending order (oldest first) | ||
| sort.Slice(nonExpiredWorkspaces, func(i, j int) bool { | ||
| @@ -2145,7 +2159,8 @@ func TestCancelPendingPrebuilds(t *testing.T) { | ||
| require.NoError(t, err) | ||
| // When: the reconciliation loop is triggered | ||
| _, err = reconciler.ReconcileAll(ctx) | ||
| require.NoError(t, err) | ||
| if tt.shouldCancel { | ||
| // Then: the pending prebuild job from non-active version should be canceled | ||
| @@ -2347,7 +2362,8 @@ func TestCancelPendingPrebuilds(t *testing.T) { | ||
| templateBVersion3Pending := setupPrebuilds(t, db, owner.OrganizationID, templateBID, templateBVersion3ID, templateBVersion3PresetID, 1, true) | ||
| // When: the reconciliation loop is executed | ||
| _, err := reconciler.ReconcileAll(ctx) | ||
| require.NoError(t, err) | ||
| // Then: template A version 1 running workspaces should not be canceled | ||
| checkIfJobCanceledAndDeleted(t, clock, ctx, db, false, templateAVersion1Running) | ||
| @@ -2369,6 +2385,51 @@ func TestCancelPendingPrebuilds(t *testing.T) { | ||
| }) | ||
| } | ||
| func TestReconciliationStats(t *testing.T) { | ||
| t.Parallel() | ||
| // Setup | ||
| clock := quartz.NewReal() | ||
| db, ps := dbtestutil.NewDB(t) | ||
| client, _, _ := coderdtest.NewWithAPI(t, &coderdtest.Options{ | ||
| Database: db, | ||
| Pubsub: ps, | ||
| Clock: clock, | ||
| }) | ||
| fakeEnqueuer := newFakeEnqueuer() | ||
| registry := prometheus.NewRegistry() | ||
| cache := files.New(registry, &coderdtest.FakeAuthorizer{}) | ||
| logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: false}).Leveled(slog.LevelDebug) | ||
| reconciler := prebuilds.NewStoreReconciler(db, ps, cache, codersdk.PrebuildsConfig{}, logger, clock, registry, fakeEnqueuer, newNoopUsageCheckerPtr()) | ||
| owner := coderdtest.CreateFirstUser(t, client) | ||
| ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitShort) | ||
| defer cancel() | ||
| // Create a template version with a preset | ||
| dbfake.TemplateVersion(t, db).Seed(database.TemplateVersion{ | ||
| OrganizationID: owner.OrganizationID, | ||
| CreatedBy: owner.UserID, | ||
| }).Preset(database.TemplateVersionPreset{ | ||
| DesiredInstances: sql.NullInt32{ | ||
| Int32: 1, | ||
| Valid: true, | ||
| }, | ||
| }).Do() | ||
| // Verify that ReconcileAll tracks and returns elapsed time | ||
| start := time.Now() | ||
| stats, err := reconciler.ReconcileAll(ctx) | ||
| actualElapsed := time.Since(start) | ||
| require.NoError(t, err) | ||
| require.Greater(t, stats.Elapsed, time.Duration(0)) | ||
ssncferreira marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
| // Verify stats.Elapsed matches actual execution time | ||
| require.InDelta(t, actualElapsed.Milliseconds(), stats.Elapsed.Milliseconds(), 100) | ||
| // Verify reconciliation loop is not unexpectedly slow | ||
| require.Less(t, stats.Elapsed, 5*time.Second) | ||
| } | ||
| func newNoopEnqueuer() *notifications.NoopEnqueuer { | ||
| return notifications.NewNoopEnqueuer() | ||
| } | ||
| @@ -2863,7 +2924,7 @@ func TestReconciliationRespectsPauseSetting(t *testing.T) { | ||
| _ = setupTestDBPreset(t, db, templateVersionID, 2, "test") | ||
| // Initially, reconciliation should create prebuilds | ||
| _,err := reconciler.ReconcileAll(ctx) | ||
| require.NoError(t, err) | ||
| // Verify that prebuilds were created | ||
| @@ -2890,7 +2951,7 @@ func TestReconciliationRespectsPauseSetting(t *testing.T) { | ||
| require.Len(t, workspaces, 0, "prebuilds should be deleted") | ||
| // Run reconciliation again - it should be paused and not recreate prebuilds | ||
| _,err = reconciler.ReconcileAll(ctx) | ||
| require.NoError(t, err) | ||
| // Verify that no new prebuilds were created because reconciliation is paused | ||
| @@ -2903,7 +2964,7 @@ func TestReconciliationRespectsPauseSetting(t *testing.T) { | ||
| require.NoError(t, err) | ||
| // Run reconciliation again - it should now recreate the prebuilds | ||
| _,err = reconciler.ReconcileAll(ctx) | ||
| require.NoError(t, err) | ||
| // Verify that prebuilds were recreated | ||
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
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.