Skip to content

Commit eeb0407

Browse files
refactor: temporary commit - tests are passing
1 parent fe60b56 commit eeb0407

File tree

8 files changed

+420
-312
lines changed

8 files changed

+420
-312
lines changed

coderd/prebuilds/api.go

+5-12
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package prebuilds
33
import (
44
"context"
55

6-
"github.com/google/uuid"
7-
86
"github.com/coder/coder/v2/coderd/database"
97
)
108

@@ -17,14 +15,9 @@ type ReconciliationOrchestrator interface {
1715

1816
type Reconciler interface {
1917
// SnapshotState MUST be called inside a repeatable-read tx.
20-
SnapshotState(ctx context.Context, store database.Store) (*ReconciliationState, error)
21-
// DetermineActions MUST be called inside a repeatable-read tx.
22-
DetermineActions(ctx context.Context, state PresetState) (*ReconciliationActions, error)
23-
// Reconcile MUST be called inside a repeatable-read tx.
24-
Reconcile(ctx context.Context, state PresetState, actions ReconciliationActions) error
25-
}
26-
27-
type Claimer interface {
28-
Claim(ctx context.Context, store database.Store, userID uuid.UUID, name string, presetID uuid.UUID) (*uuid.UUID, error)
29-
Initiator() uuid.UUID
18+
SnapshotState(ctx context.Context, store database.Store) (*GlobalSnapshot, error)
19+
// CalculateActions MUST be called inside a repeatable-read tx.
20+
CalculateActions(ctx context.Context, state PresetSnapshot) (*ReconciliationActions, error)
21+
// ReconcilePreset MUST be called inside a repeatable-read tx.
22+
ReconcilePreset(ctx context.Context, snapshot PresetSnapshot) error
3023
}

coderd/prebuilds/noop.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ func NewNoopReconciler() *NoopReconciler {
1414

1515
func (NoopReconciler) RunLoop(context.Context) {}
1616
func (NoopReconciler) Stop(context.Context, error) {}
17-
func (NoopReconciler) SnapshotState(context.Context, database.Store) (*ReconciliationState, error) {
18-
return &ReconciliationState{}, nil
17+
func (NoopReconciler) SnapshotState(context.Context, database.Store) (*GlobalSnapshot, error) {
18+
return &GlobalSnapshot{}, nil
1919
}
2020

21-
func (NoopReconciler) DetermineActions(context.Context, PresetState) (*ReconciliationActions, error) {
21+
func (NoopReconciler) CalculateActions(context.Context, PresetSnapshot) (*ReconciliationActions, error) {
2222
return &ReconciliationActions{}, nil
2323
}
2424

25-
func (NoopReconciler) Reconcile(context.Context, PresetState, ReconciliationActions) error {
25+
func (NoopReconciler) ReconcilePreset(context.Context, PresetSnapshot) error {
2626
return nil
2727
}
2828

coderd/prebuilds/reconcile.go

+56-20
Original file line numberDiff line numberDiff line change
@@ -10,43 +10,79 @@ import (
1010
"github.com/coder/coder/v2/coderd/util/slice"
1111
)
1212

13-
// ReconciliationState represents a full point-in-time snapshot of state relating to prebuilds across all templates.
14-
type ReconciliationState struct {
13+
// ActionType represents the type of action needed to reconcile prebuilds.
14+
type ActionType int
15+
16+
const (
17+
// ActionTypeCreate indicates that new prebuilds should be created.
18+
ActionTypeCreate ActionType = iota
19+
20+
// ActionTypeDelete indicates that existing prebuilds should be deleted.
21+
ActionTypeDelete
22+
23+
// ActionTypeBackoff indicates that prebuild creation should be delayed.
24+
ActionTypeBackoff
25+
)
26+
27+
// GlobalSnapshot represents a full point-in-time snapshot of state relating to prebuilds across all templates.
28+
type GlobalSnapshot struct {
1529
Presets []database.GetTemplatePresetsWithPrebuildsRow
1630
RunningPrebuilds []database.GetRunningPrebuiltWorkspacesRow
1731
PrebuildsInProgress []database.CountInProgressPrebuildsRow
1832
Backoffs []database.GetPresetsBackoffRow
1933
}
2034

21-
// PresetState is a subset of ReconciliationState but specifically for a single preset.
22-
type PresetState struct {
35+
// PresetSnapshot is a filtered view of GlobalSnapshot focused on a single preset.
36+
// It contains the raw data needed to calculate the current state of a preset's prebuilds,
37+
// including running prebuilds, in-progress builds, and backoff information.
38+
type PresetSnapshot struct {
2339
Preset database.GetTemplatePresetsWithPrebuildsRow
2440
Running []database.GetRunningPrebuiltWorkspacesRow
2541
InProgress []database.CountInProgressPrebuildsRow
2642
Backoff *database.GetPresetsBackoffRow
2743
}
2844

29-
// ReconciliationActions represents the set of actions which must be taken to achieve the desired state for prebuilds.
45+
// ReconciliationState represents the processed state of a preset's prebuilds,
46+
// calculated from a PresetSnapshot. While PresetSnapshot contains raw data,
47+
// ReconciliationState contains derived metrics that are directly used to
48+
// determine what actions are needed (create, delete, or backoff).
49+
// For example, it calculates how many prebuilds are eligible, how many are
50+
// extraneous, and how many are in various transition states.
51+
type ReconciliationState struct {
52+
Actual int32 // Number of currently running prebuilds
53+
Desired int32 // Number of prebuilds desired as defined in the preset
54+
Eligible int32 // Number of prebuilds that are ready to be claimed
55+
Extraneous int32 // Number of extra running prebuilds beyond the desired count
56+
57+
// Counts of prebuilds in various transition states
58+
Starting int32
59+
Stopping int32
60+
Deleting int32
61+
}
62+
63+
// ReconciliationActions represents a single action needed to reconcile the current state with the desired state.
64+
// Exactly one field will be set based on the ActionType.
3065
type ReconciliationActions struct {
31-
Actual int32 // Running prebuilds for active version.
32-
Desired int32 // Active template version's desired instances as defined in preset.
33-
Eligible int32 // Prebuilds which can be claimed.
34-
Outdated int32 // Prebuilds which no longer match the active template version.
35-
Extraneous int32 // Extra running prebuilds for active version (somehow).
36-
Starting, Stopping, Deleting int32 // Prebuilds currently being provisioned up or down.
37-
Failed int32 // Number of prebuilds which have failed in the past CODER_WORKSPACE_PREBUILDS_RECONCILIATION_BACKOFF_LOOKBACK_PERIOD.
38-
Create int32 // The number of prebuilds required to be created to reconcile required state.
39-
DeleteIDs []uuid.UUID // IDs of running prebuilds required to be deleted to reconcile required state.
40-
BackoffUntil time.Time // The time to wait until before trying to provision a new prebuild.
66+
// ActionType determines which field is set and what action should be taken
67+
ActionType ActionType
68+
69+
// Create is set when ActionType is ActionTypeCreate and indicates the number of prebuilds to create
70+
Create int32
71+
72+
// DeleteIDs is set when ActionType is ActionTypeDelete and contains the IDs of prebuilds to delete
73+
DeleteIDs []uuid.UUID
74+
75+
// BackoffUntil is set when ActionType is ActionTypeBackoff and indicates when to retry creating prebuilds
76+
BackoffUntil time.Time
4177
}
4278

43-
func NewReconciliationState(presets []database.GetTemplatePresetsWithPrebuildsRow, runningPrebuilds []database.GetRunningPrebuiltWorkspacesRow,
79+
func NewGlobalSnapshot(presets []database.GetTemplatePresetsWithPrebuildsRow, runningPrebuilds []database.GetRunningPrebuiltWorkspacesRow,
4480
prebuildsInProgress []database.CountInProgressPrebuildsRow, backoffs []database.GetPresetsBackoffRow,
45-
) ReconciliationState {
46-
return ReconciliationState{Presets: presets, RunningPrebuilds: runningPrebuilds, PrebuildsInProgress: prebuildsInProgress, Backoffs: backoffs}
81+
) GlobalSnapshot {
82+
return GlobalSnapshot{Presets: presets, RunningPrebuilds: runningPrebuilds, PrebuildsInProgress: prebuildsInProgress, Backoffs: backoffs}
4783
}
4884

49-
func (s ReconciliationState) FilterByPreset(presetID uuid.UUID) (*PresetState, error) {
85+
func (s GlobalSnapshot) FilterByPreset(presetID uuid.UUID) (*PresetSnapshot, error) {
5086
preset, found := slice.Find(s.Presets, func(preset database.GetTemplatePresetsWithPrebuildsRow) bool {
5187
return preset.ID == presetID
5288
})
@@ -79,7 +115,7 @@ func (s ReconciliationState) FilterByPreset(presetID uuid.UUID) (*PresetState, e
79115
backoff = &backoffs[0]
80116
}
81117

82-
return &PresetState{
118+
return &PresetSnapshot{
83119
Preset: preset,
84120
Running: running,
85121
InProgress: inProgress,

0 commit comments

Comments
 (0)