forked from coreos/fleet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reconcile.go
335 lines (288 loc) · 8.26 KB
/
reconcile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// Copyright 2014 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package agent
import (
"fmt"
"sort"
"time"
"github.com/coreos/fleet/job"
"github.com/coreos/fleet/log"
"github.com/coreos/fleet/machine"
"github.com/coreos/fleet/pkg"
"github.com/coreos/fleet/registry"
)
const (
// time between triggering reconciliation routine
reconcileInterval = 5 * time.Second
)
func NewReconciler(reg registry.Registry, rStream pkg.EventStream) *AgentReconciler {
return &AgentReconciler{
reg: reg,
rStream: rStream,
tManager: newTaskManager(),
}
}
type AgentReconciler struct {
reg registry.Registry
rStream pkg.EventStream
tManager *taskManager
}
// Run periodically attempts to reconcile the provided Agent until the stop
// channel is closed. Run will also reconcile in reaction to events on the
// AgentReconciler's rStream.
func (ar *AgentReconciler) Run(a *Agent, stop chan bool) {
reconcile := func() {
start := time.Now()
ar.Reconcile(a)
elapsed := time.Now().Sub(start)
msg := fmt.Sprintf("AgentReconciler completed reconciliation in %s", elapsed)
if elapsed > reconcileInterval {
log.Warning(msg)
} else {
log.Debug(msg)
}
}
reconciler := pkg.NewPeriodicReconciler(reconcileInterval, reconcile, ar.rStream)
reconciler.Run(stop)
}
// Reconcile drives the local Agent's state towards the desired state
// stored in the Registry.
func (ar *AgentReconciler) Reconcile(a *Agent) {
dAgentState, err := desiredAgentState(a, ar.reg)
if err != nil {
log.Errorf("Unable to determine agent's desired state: %v", err)
return
}
cAgentState, err := a.units()
if err != nil {
log.Errorf("Unable to determine agent's current state: %v", err)
return
}
tasks := ar.calculateTasksForUnits(dAgentState, cAgentState)
ar.launchTasks(tasks, a)
}
// Purge attempts to unload all Units that have been loaded locally
func (ar *AgentReconciler) Purge(a *Agent) {
for {
cAgentState, err := a.units()
if err != nil {
log.Errorf("Unable to determine agent's current state: %v", err)
return
}
if len(cAgentState) == 0 {
return
}
var tasks []task
for name, _ := range cAgentState {
tasks = append(tasks, task{
typ: taskTypeUnloadUnit,
reason: taskReasonPurgingAgent,
unit: &job.Unit{
Name: name,
},
})
}
ar.launchTasks(tasks, a)
time.Sleep(time.Second)
}
}
// desiredAgentState builds an *AgentState object that represents what the
// provided Agent should currently be doing.
func desiredAgentState(a *Agent, reg registry.Registry) (*AgentState, error) {
units, err := reg.Units()
if err != nil {
log.Errorf("Failed fetching Units from Registry: %v", err)
return nil, err
}
sUnits, err := reg.Schedule()
if err != nil {
log.Errorf("Failed fetching schedule from Registry: %v", err)
return nil, err
}
ms := a.Machine.State()
as := AgentState{
MState: &ms,
Units: make(map[string]*job.Unit),
}
sUnitMap := make(map[string]*job.ScheduledUnit)
for _, sUnit := range sUnits {
sUnit := sUnit
sUnitMap[sUnit.Name] = &sUnit
}
for _, u := range units {
u := u
md := u.RequiredTargetMetadata()
if u.IsGlobal() && !machine.HasMetadata(&ms, md) {
log.Debugf("Agent unable to run global unit %s: missing required metadata", u.Name)
continue
}
if !u.IsGlobal() {
sUnit, ok := sUnitMap[u.Name]
if !ok || sUnit.TargetMachineID == "" || sUnit.TargetMachineID != ms.ID {
continue
}
}
as.Units[u.Name] = &u
}
return &as, nil
}
// calculateTasksForUnits compares the desired and current state of an Agent.
// The generated tasks represent what, in order, should be done to make the
// desired state match the current state.
func (ar *AgentReconciler) calculateTasksForUnits(dState *AgentState, cState unitStates) []task {
jobs := pkg.NewUnsafeSet()
for cName := range cState {
jobs.Add(cName)
}
for dName := range dState.Units {
jobs.Add(dName)
}
sorted := sort.StringSlice(jobs.Values())
sorted.Sort()
var tasks []task
for _, name := range sorted {
tasks = append(tasks, ar.calculateTasksForUnit(dState, cState, name)...)
}
if len(tasks) == 0 {
return nil
}
reloadTask := task{typ: taskTypeReloadUnitFiles, reason: taskReasonAlwaysReloadUnitFiles}
tasks = append(tasks, reloadTask)
sort.Sort(sortableTasks(tasks))
// reload unnecessary if no UnloadUnit/LoadUnit tasks
if tasks[0].typ == taskTypeReloadUnitFiles {
tasks = tasks[1:]
}
return tasks
}
func (ar *AgentReconciler) calculateTasksForUnit(dState *AgentState, cState unitStates, jName string) (tasks []task) {
var dJob *job.Unit
var dJHash string
if dState != nil {
dJob = dState.Units[jName]
if dJob != nil {
dJHash = dJob.Unit.Hash().String()
}
}
var cJState *job.JobState
var cJHash string
if us, ok := cState[jName]; ok {
cJState = &us.state
cJHash = us.hash
}
if dJob == nil && cJState == nil {
log.Errorf("Desired state and current state of Job(%s) nil, not sure what to do", jName)
return nil
}
u := &job.Unit{
Name: jName,
}
if dJob == nil || dJob.TargetState == job.JobStateInactive {
if cJState == nil {
return nil
}
tasks = append(tasks, task{
typ: taskTypeUnloadUnit,
reason: taskReasonLoadedButNotScheduled,
unit: u,
})
return
}
u.Unit = dJob.Unit
if cJState == nil {
tasks = append(tasks, task{
typ: taskTypeLoadUnit,
reason: taskReasonScheduledButUnloaded,
unit: u,
})
// as an optimization, queue the unit for launching immediately after loading
if dJob.TargetState == job.JobStateLaunched {
tasks = append(tasks, task{
typ: taskTypeStartUnit,
reason: taskReasonLoadedDesiredStateLaunched,
unit: u,
})
}
return
}
if cJHash != dJHash {
log.Debugf("Desired hash %q differs to current hash %s of Job(%s) - unloading", dJHash, cJHash, jName)
// queue the correct unit for loading immediately after unloading the old one
tasks = append(tasks,
task{
typ: taskTypeUnloadUnit,
reason: taskReasonLoadedButHashDiffers,
unit: u,
},
task{
typ: taskTypeLoadUnit,
reason: taskReasonScheduledButUnloaded,
unit: u,
},
)
// as an optimization, queue the unit for launching immediately after loading
if dJob.TargetState == job.JobStateLaunched {
tasks = append(tasks, task{
typ: taskTypeStartUnit,
reason: taskReasonLoadedDesiredStateLaunched,
unit: u,
})
}
return
}
if *cJState == dJob.TargetState {
log.Debugf("Desired state %q matches current state of Job(%s), nothing to do", *cJState, jName)
return nil
}
if *cJState == job.JobStateInactive {
tasks = append(tasks, task{
typ: taskTypeLoadUnit,
reason: taskReasonScheduledButUnloaded,
unit: u,
})
}
if (*cJState == job.JobStateInactive || *cJState == job.JobStateLoaded) && dJob.TargetState == job.JobStateLaunched {
tasks = append(tasks, task{
typ: taskTypeStartUnit,
reason: taskReasonLoadedDesiredStateLaunched,
unit: u,
})
}
if *cJState == job.JobStateLaunched && dJob.TargetState == job.JobStateLoaded {
tasks = append(tasks, task{
typ: taskTypeStopUnit,
reason: taskReasonLaunchedDesiredStateLoaded,
unit: u,
})
}
if len(tasks) == 0 {
log.Errorf("Unable to determine how to reconcile Job(%s): desiredState=%#v currentState=%#v", jName, dJob, cJState)
}
return
}
func (ar *AgentReconciler) launchTasks(tasks []task, a *Agent) {
log.Debugf("AgentReconciler attempting tasks %s", tasks)
results := ar.tManager.Do(tasks, a)
for _, res := range results {
unitName := "N/A"
if res.task.unit != nil {
unitName = res.task.unit.Name
}
if res.err == nil {
log.Infof("AgentReconciler completed task: type=%s job=%s reason=%q", res.task.typ, unitName, res.task.reason)
} else {
log.Infof("AgentReconciler task failed: type=%s job=%s reason=%q err=%v", res.task.typ, unitName, res.task.reason, res.err)
}
}
}