@@ -10,43 +10,79 @@ import (
10
10
"github.com/coder/coder/v2/coderd/util/slice"
11
11
)
12
12
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 {
15
29
Presets []database.GetTemplatePresetsWithPrebuildsRow
16
30
RunningPrebuilds []database.GetRunningPrebuiltWorkspacesRow
17
31
PrebuildsInProgress []database.CountInProgressPrebuildsRow
18
32
Backoffs []database.GetPresetsBackoffRow
19
33
}
20
34
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 {
23
39
Preset database.GetTemplatePresetsWithPrebuildsRow
24
40
Running []database.GetRunningPrebuiltWorkspacesRow
25
41
InProgress []database.CountInProgressPrebuildsRow
26
42
Backoff * database.GetPresetsBackoffRow
27
43
}
28
44
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.
30
65
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
41
77
}
42
78
43
- func NewReconciliationState (presets []database.GetTemplatePresetsWithPrebuildsRow , runningPrebuilds []database.GetRunningPrebuiltWorkspacesRow ,
79
+ func NewGlobalSnapshot (presets []database.GetTemplatePresetsWithPrebuildsRow , runningPrebuilds []database.GetRunningPrebuiltWorkspacesRow ,
44
80
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 }
47
83
}
48
84
49
- func (s ReconciliationState ) FilterByPreset (presetID uuid.UUID ) (* PresetState , error ) {
85
+ func (s GlobalSnapshot ) FilterByPreset (presetID uuid.UUID ) (* PresetSnapshot , error ) {
50
86
preset , found := slice .Find (s .Presets , func (preset database.GetTemplatePresetsWithPrebuildsRow ) bool {
51
87
return preset .ID == presetID
52
88
})
@@ -79,7 +115,7 @@ func (s ReconciliationState) FilterByPreset(presetID uuid.UUID) (*PresetState, e
79
115
backoff = & backoffs [0 ]
80
116
}
81
117
82
- return & PresetState {
118
+ return & PresetSnapshot {
83
119
Preset : preset ,
84
120
Running : running ,
85
121
InProgress : inProgress ,
0 commit comments