forked from coreos/fleet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reconcile.go
518 lines (434 loc) · 13.4 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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
package agent
import (
"fmt"
"time"
log "github.com/coreos/fleet/Godeps/_workspace/src/github.com/golang/glog"
"github.com/coreos/fleet/job"
"github.com/coreos/fleet/machine"
"github.com/coreos/fleet/pkg"
"github.com/coreos/fleet/registry"
"github.com/coreos/fleet/sign"
)
const (
// time between triggering reconciliation routine
reconcileInterval = 5 * time.Second
taskTypeLoadJob = "LoadJob"
taskTypeUnloadJob = "UnloadJob"
taskTypeStartJob = "StartJob"
taskTypeStopJob = "StopJob"
taskTypeUnscheduleJob = "UnscheduleJob"
taskTypeSubmitBid = "SubmitBid"
taskReasonScheduledButNotRunnable = "job scheduled locally but unable to run"
taskReasonScheduledButUnloaded = "job scheduled here but not loaded"
taskReasonLoadedButNotScheduled = "job loaded but not scheduled here"
taskReasonLoadedDesiredStateLaunched = "job currently loaded but desired state is launched"
taskReasonLaunchedDesiredStateLoaded = "job currently launched but desired state is loaded"
taskReasonPurgingAgent = "purging agent"
taskReasonAbleToResolveOffer = "offer unresolved and able to run job"
)
type task struct {
Type string
Job *job.Job
Reason string
}
func (t *task) String() string {
var jName string
if t.Job != nil {
jName = t.Job.Name
}
return fmt.Sprintf("{Type: %s, Job: %s, Reason: %q}", t.Type, jName, t.Reason)
}
type offer struct {
Bids pkg.Set
Job *job.Job
}
type offerCache map[string]*offer
func (oc *offerCache) add(name string, bids pkg.Set, j *job.Job) {
(*oc)[name] = &offer{
Bids: bids,
Job: j,
}
}
func NewReconciler(reg registry.Registry, verifier *sign.SignatureVerifier) *AgentReconciler {
return &AgentReconciler{reg, verifier, make(chan struct{})}
}
type AgentReconciler struct {
reg registry.Registry
verifier *sign.SignatureVerifier
rTrigger chan struct{}
}
// Run periodically attempts to reconcile the provided Agent until the stop
// channel is closed. Run will also reconcile in reaction to calls to Trigger.
// While a reconciliation is being attempted, calls to Trigger are ignored.
func (ar *AgentReconciler) Run(a *Agent, stop chan bool) {
ticker := time.Tick(reconcileInterval)
reconcile := func() {
done := make(chan struct{})
defer close(done)
// While the reconciliation is running, flush the trigger channel in the background
go func() {
for {
select {
case <-done:
return
default:
select {
case <-ar.rTrigger:
case <-done:
return
}
}
}
}()
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.V(1).Info(msg)
}
}
for {
select {
case <-stop:
log.V(1).Info("AgentReconciler exiting due to stop signal")
return
case <-ticker:
reconcile()
case <-ar.rTrigger:
reconcile()
}
}
}
// Trigger causes Reconcile to run if the Agent is running but is
// not currently reconciling.
func (ar *AgentReconciler) Trigger() {
ar.rTrigger <- struct{}{}
}
// Reconcile drives the local Agent's state towards the desired state
// stored in the Registry. Reconcile also attempts to bid for any
// outstanding job offers that the local Agent can run.
func (ar *AgentReconciler) Reconcile(a *Agent) {
ms := a.Machine.State()
jobs, err := ar.reg.Jobs()
if err != nil {
log.Errorf("Failed fetching Jobs from Registry: %v", err)
return
}
dAgentState, err := ar.desiredAgentState(jobs, ms.ID)
if err != nil {
log.Errorf("Unable to determine agent's desired state: %v", err)
return
}
cAgentState, err := currentAgentState(a)
if err != nil {
log.Errorf("Unable to determine agent's current state: %v", err)
return
}
for t := range ar.calculateTasksForJobs(&ms, dAgentState, cAgentState) {
err := ar.doTask(a, t)
if err != nil {
log.Errorf("Failed resolving task, halting reconciliation: task=%s err=%q", t, err)
return
}
}
oCache, err := ar.currentOffers(jobs)
if err != nil {
log.Errorf("Unable to determine current state of offers: %v", err)
return
}
for t := range ar.calculateTasksForOffers(oCache, dAgentState, &ms) {
err := ar.doTask(a, t)
if err != nil {
log.Errorf("Failed resolving task, halting reconciliation: task=%s err=%q", t, err)
return
}
}
}
// Purge attempts to unload all Jobs that have been loaded locally
func (ar *AgentReconciler) Purge(a *Agent) {
cAgentState, err := currentAgentState(a)
if err != nil {
log.Errorf("Unable to determine agent's current state: %v", err)
return
}
for _, cJob := range cAgentState.jobs {
t := task{
Type: taskTypeUnloadJob,
Job: cJob,
Reason: taskReasonPurgingAgent,
}
err := ar.doTask(a, &t)
if err != nil {
log.Errorf("Failed resolving task: task=%s err=%q", t, err)
}
}
}
// doTask takes action on an Agent based on the contents of a *task
func (ar *AgentReconciler) doTask(a *Agent, t *task) (err error) {
switch t.Type {
case taskTypeLoadJob:
err = a.loadJob(t.Job)
case taskTypeUnloadJob:
a.unloadJob(t.Job.Name)
case taskTypeStartJob:
a.startJob(t.Job.Name)
case taskTypeStopJob:
a.stopJob(t.Job.Name)
case taskTypeSubmitBid:
ar.submitBid(t.Job.Name, a.Machine.State().ID)
case taskTypeUnscheduleJob:
err = ar.unscheduleJob(t.Job.Name, a.Machine.State().ID)
default:
err = fmt.Errorf("unrecognized task type %q", t.Type)
}
if err == nil {
log.Infof("AgentReconciler completed task: %s", t)
}
return
}
func (ar *AgentReconciler) submitBid(jName, machID string) {
ar.reg.SubmitJobBid(jName, machID)
}
func (ar *AgentReconciler) unscheduleJob(jName, machID string) error {
return ar.reg.ClearJobTarget(jName, machID)
}
// desiredAgentState builds an *agentState object that represents what an
// Agent identified by the provided machine ID should currently be doing.
func (ar *AgentReconciler) desiredAgentState(jobs []job.Job, machID string) (*agentState, error) {
as := agentState{jobs: make(map[string]*job.Job)}
for _, j := range jobs {
j := j
if j.TargetMachineID == "" || j.TargetMachineID != machID {
continue
}
as.jobs[j.Name] = &j
}
return &as, nil
}
// currentAgentState builds an *agentState object that represents what an
// Agent is currently doing.
func currentAgentState(a *Agent) (*agentState, error) {
jobs, err := a.jobs()
if err != nil {
return nil, err
}
as := agentState{jobs: jobs}
return &as, nil
}
// calculateTasksForJobs compares the desired and current state of an Agent.
// The generateed tasks represent what should be done to make the desired
// state match the current state.
func (ar *AgentReconciler) calculateTasksForJobs(ms *machine.MachineState, dState, cState *agentState) <-chan *task {
taskchan := make(chan *task)
go func() {
jobs := pkg.NewUnsafeSet()
for cName := range cState.jobs {
jobs.Add(cName)
}
for dName := range dState.jobs {
jobs.Add(dName)
}
for _, name := range jobs.Values() {
ar.calculateTasksForJob(ms, dState, cState, name, taskchan)
}
close(taskchan)
}()
return taskchan
}
func (ar *AgentReconciler) calculateTasksForJob(ms *machine.MachineState, dState, cState *agentState, jName string, taskchan chan *task) {
var dJob, cJob *job.Job
if dState != nil {
dJob = dState.jobs[jName]
}
if cState != nil {
cJob = cState.jobs[jName]
}
if dJob == nil && cJob == nil {
log.Errorf("Desired state and current state of Job(%s) nil, not sure what to do", jName)
return
}
if dJob == nil || dJob.TargetState == job.JobStateInactive {
taskchan <- &task{
Type: taskTypeUnloadJob,
Job: cJob,
Reason: taskReasonLoadedButNotScheduled,
}
delete(cState.jobs, jName)
return
}
if able, reason := ar.ableToRun(cState, ms, dJob); !able {
log.Errorf("Unable to run locally-scheduled Job(%s): %s", jName, reason)
taskchan <- &task{
Type: taskTypeUnscheduleJob,
Job: dJob,
Reason: taskReasonScheduledButNotRunnable,
}
delete(dState.jobs, jName)
taskchan <- &task{
Type: taskTypeUnloadJob,
Job: dJob,
Reason: taskReasonScheduledButNotRunnable,
}
delete(cState.jobs, jName)
return
}
if cJob == nil {
taskchan <- &task{
Type: taskTypeLoadJob,
Job: dJob,
Reason: taskReasonScheduledButUnloaded,
}
return
}
if cJob.State == nil {
log.Errorf("Current state of Job(%s) unknown, unable to reconcile", jName)
return
}
if dJob.State == nil {
log.Errorf("Desired state of Job(%s) unknown, unable to reconcile", jName)
return
}
if *cJob.State == dJob.TargetState {
log.V(1).Infof("Desired state %q matches current state of Job(%s), nothing to do", *cJob.State, jName)
return
}
if *cJob.State == job.JobStateInactive {
taskchan <- &task{
Type: taskTypeLoadJob,
Job: dJob,
Reason: taskReasonScheduledButUnloaded,
}
}
if (*cJob.State == job.JobStateInactive || *cJob.State == job.JobStateLoaded) && dJob.TargetState == job.JobStateLaunched {
taskchan <- &task{
Type: taskTypeStartJob,
Job: cJob,
Reason: taskReasonLoadedDesiredStateLaunched,
}
return
}
if *cJob.State == job.JobStateLaunched && dJob.TargetState == job.JobStateLoaded {
taskchan <- &task{
Type: taskTypeStopJob,
Job: cJob,
Reason: taskReasonLaunchedDesiredStateLoaded,
}
return
}
log.Errorf("Unable to determine how to reconcile Job(%s): desiredState=%#v currentState=%#V", jName, dJob, cJob)
}
func (ar *AgentReconciler) currentOffers(jobs []job.Job) (*offerCache, error) {
jMap := make(map[string]*job.Job)
for _, j := range jobs {
j := j
jMap[j.Name] = &j
}
uOffers, err := ar.reg.UnresolvedJobOffers()
if err != nil {
return nil, fmt.Errorf("failed fetching JobOffers from Registry: %v", err)
}
oCache := &offerCache{}
for _, offer := range uOffers {
bids, err := ar.reg.Bids(offer.Job.Name)
if err != nil {
return nil, err
}
oCache.add(offer.Job.Name, bids, jMap[offer.Job.Name])
}
return oCache, nil
}
// calculateTasksForOffers compares the unresolved job offers and desired state
// of an Agent. The generated tasks represent which offers upon which the Agent
// should bid.
func (ar *AgentReconciler) calculateTasksForOffers(oCache *offerCache, dState *agentState, ms *machine.MachineState) <-chan *task {
taskchan := make(chan *task)
go func() {
for oName, cache := range *oCache {
if cache.Job == nil {
log.Errorf("Unable to determine what to do about JobOffer(%s), Job is nil", oName)
continue
}
ar.calculateTasksForOffer(dState, ms, cache.Job, cache.Bids, taskchan)
}
close(taskchan)
}()
return taskchan
}
func (ar *AgentReconciler) calculateTasksForOffer(dState *agentState, ms *machine.MachineState, j *job.Job, bids pkg.Set, taskchan chan *task) {
if bids.Contains(ms.ID) {
log.V(1).Infof("Bid already submitted for unresolved JobOffer(%s)", j.Name)
return
}
if able, reason := ar.ableToRun(dState, ms, j); !able {
log.V(1).Infof("Not bidding on Job(%s): %s", j.Name, reason)
return
}
taskchan <- &task{
Type: taskTypeSubmitBid,
Job: j,
Reason: taskReasonAbleToResolveOffer,
}
}
// ableToRun determines if the Agent can run the provided Job based on
// the Agent's desired state. A boolean indicating whether this is the
// case or not is returned. The following criteria is used:
// - Agent must meet the Job's machine target requirement (if any)
// - Job must pass signature verification
// - Agent must have all of the Job's required metadata (if any)
// - Agent must have all required Peers of the Job scheduled locally (if any)
// - Job must not conflict with any other Jobs scheduled to the agent
func (ar *AgentReconciler) ableToRun(as *agentState, ms *machine.MachineState, j *job.Job) (bool, string) {
log.V(1).Infof("Attempting to determine if able to run Job(%s)", j.Name)
if tgt, ok := j.RequiredTarget(); ok && !ms.MatchID(tgt) {
return false, fmt.Sprintf("Agent ID %q does not match required %q", ms.ID, tgt)
}
if !ar.verifyJobSignature(j) {
return false, "unable to verify signature"
}
log.V(1).Infof("Job(%s) has requirements: %s", j.Name, j.Requirements())
metadata := j.RequiredTargetMetadata()
if len(metadata) == 0 {
log.V(1).Infof("Job(%s) has no required machine metadata", j.Name)
} else {
log.V(1).Infof("Job(%s) requires machine metadata: %v", j.Name, metadata)
if !machine.HasMetadata(ms, metadata) {
return false, "local Machine metadata insufficient"
}
}
peers := j.Peers()
if len(peers) == 0 {
log.V(1).Infof("Job(%s) has no required peers", j.Name)
} else {
log.V(1).Infof("Job(%s) requires peers: %v", j.Name, peers)
for _, peer := range peers {
if !as.jobScheduled(peer) {
return false, fmt.Sprintf("required peer Job(%s) is not scheduled locally", peer)
}
}
}
if cExists, cJobName := as.hasConflict(j.Name, j.Conflicts()); cExists {
return false, fmt.Sprintf("found conflict with locally-scheduled Job(%s)", cJobName)
}
log.V(1).Infof("Determined local Agent is able to run Job(%s)", j.Name)
return true, ""
}
// verifyJobSignature attempts to verify the integrity of the given Job by checking the
// signature against a SignatureSet stored in the Registry
func (ar *AgentReconciler) verifyJobSignature(j *job.Job) bool {
if ar.verifier == nil {
return true
}
ss, _ := ar.reg.JobSignatureSet(j.Name)
ok, err := ar.verifier.VerifyJob(j, ss)
if err != nil {
log.V(1).Infof("Error verifying signature of Job(%s): %v", j.Name, err)
return false
} else if !ok {
log.V(1).Infof("Job(%s) does not match signature", j.Name)
return false
}
return true
}