This repository has been archived by the owner on Feb 17, 2018. It is now read-only.
forked from coreos/fleet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.go
274 lines (229 loc) · 6.82 KB
/
engine.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
/*
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 engine
import (
"fmt"
"time"
"github.com/coreos/fleet/log"
"github.com/coreos/fleet/machine"
"github.com/coreos/fleet/pkg"
"github.com/coreos/fleet/registry"
)
const (
// name of lease that must be held by the lead engine in a cluster
engineLeaseName = "engine-leader"
// version at which the current engine code operates
engineVersion = 1
)
type Engine struct {
rec *Reconciler
registry registry.Registry
cRegistry registry.ClusterRegistry
lRegistry registry.LeaseRegistry
rStream pkg.EventStream
machine machine.Machine
lease registry.Lease
trigger chan struct{}
}
func New(reg *registry.EtcdRegistry, rStream pkg.EventStream, mach machine.Machine) *Engine {
rec := NewReconciler()
return &Engine{
rec: rec,
registry: reg,
cRegistry: reg,
lRegistry: reg,
rStream: rStream,
machine: mach,
trigger: make(chan struct{}),
}
}
func (e *Engine) Run(ival time.Duration, stop chan bool) {
leaseTTL := ival * 5
machID := e.machine.State().ID
reconcile := func() {
if !ensureEngineVersionMatch(e.cRegistry, engineVersion) {
return
}
var l registry.Lease
if isLeader(e.lease, machID) {
l = renewLeadership(e.lease, leaseTTL)
} else {
l = acquireLeadership(e.lRegistry, machID, engineVersion, leaseTTL)
}
// log all leadership changes
if l != nil && e.lease == nil && l.MachineID() != machID {
log.Infof("Engine leader is %s", l.MachineID())
} else if l != nil && e.lease != nil && l.MachineID() != e.lease.MachineID() {
log.Infof("Engine leadership changed from %s to %s", e.lease.MachineID(), l.MachineID())
}
e.lease = l
if !isLeader(e.lease, machID) {
return
}
// abort is closed when reconciliation must stop prematurely, either
// by a local timeout or the fleet server shutting down
abort := make(chan struct{})
// monitor is used to shut down the following goroutine
monitor := make(chan struct{})
go func() {
select {
case <-monitor:
return
case <-time.After(leaseTTL):
close(abort)
case <-stop:
close(abort)
}
}()
start := time.Now()
e.rec.Reconcile(e, abort)
close(monitor)
elapsed := time.Now().Sub(start)
msg := fmt.Sprintf("Engine completed reconciliation in %s", elapsed)
if elapsed > ival {
log.Warning(msg)
} else {
log.V(1).Info(msg)
}
}
rec := pkg.NewPeriodicReconciler(ival, reconcile, e.rStream)
rec.Run(stop)
}
func (e *Engine) Purge() {
// only purge the lease if we are the leader
if !isLeader(e.lease, e.machine.State().ID) {
return
}
err := e.lease.Release()
if err != nil {
log.Errorf("Failed to release lease: %v", err)
}
}
func isLeader(l registry.Lease, machID string) bool {
if l == nil {
return false
}
if l.MachineID() != machID {
return false
}
return true
}
func ensureEngineVersionMatch(cReg registry.ClusterRegistry, expect int) bool {
v, err := cReg.EngineVersion()
if err != nil {
log.Errorf("Unable to determine cluster engine version")
return false
}
if v < expect {
err = cReg.UpdateEngineVersion(v, expect)
if err != nil {
log.Errorf("Failed updating cluster engine version from %d to %d: %v", v, expect, err)
return false
}
log.Infof("Updated cluster engine version from %d to %d", v, expect)
} else if v > expect {
log.V(1).Infof("Cluster engine version higher than local engine version (%d > %d), unable to participate", v, expect)
return false
}
return true
}
func acquireLeadership(lReg registry.LeaseRegistry, machID string, ver int, ttl time.Duration) registry.Lease {
existing, err := lReg.GetLease(engineLeaseName)
if err != nil {
log.Errorf("Unable to determine current lessee: %v", err)
return nil
}
var l registry.Lease
if existing == nil {
l, err = lReg.AcquireLease(engineLeaseName, machID, ver, ttl)
if err != nil {
log.Errorf("Engine leadership acquisition failed: %v", err)
return nil
} else if l == nil {
log.V(1).Infof("Unable to acquire engine leadership")
return nil
}
log.Infof("Engine leadership acquired")
return l
}
if existing.Version() >= ver {
log.V(1).Infof("Lease already held by Machine(%s) operating at acceptable version %d", existing.MachineID(), existing.Version())
return existing
}
rem := existing.TimeRemaining()
l, err = lReg.StealLease(engineLeaseName, machID, ver, ttl+rem, existing.Index())
if err != nil {
log.Errorf("Engine leadership steal failed: %v", err)
return nil
} else if l == nil {
log.V(1).Infof("Unable to steal engine leadership")
return nil
}
log.Infof("Stole engine leadership from Machine(%s)", existing.MachineID())
if rem > 0 {
log.Infof("Waiting %v for previous lease to expire before continuing reconciliation", rem)
<-time.After(rem)
}
return l
}
func renewLeadership(l registry.Lease, ttl time.Duration) registry.Lease {
err := l.Renew(ttl)
if err != nil {
log.Errorf("Engine leadership lost, renewal failed: %v", err)
return nil
}
log.V(1).Infof("Engine leadership renewed")
return l
}
func (e *Engine) Trigger() {
e.trigger <- struct{}{}
}
func (e *Engine) clusterState() (*clusterState, error) {
units, err := e.registry.Units()
if err != nil {
log.Errorf("Failed fetching Units from Registry: %v", err)
return nil, err
}
sUnits, err := e.registry.Schedule()
if err != nil {
log.Errorf("Failed fetching schedule from Registry: %v", err)
return nil, err
}
machines, err := e.registry.Machines()
if err != nil {
log.Errorf("Failed fetching Machines from Registry: %v", err)
return nil, err
}
return newClusterState(units, sUnits, machines), nil
}
func (e *Engine) unscheduleUnit(name, machID string) (err error) {
err = e.registry.UnscheduleUnit(name, machID)
if err != nil {
log.Errorf("Failed unscheduling Unit(%s) from Machine(%s): %v", name, machID, err)
} else {
log.Infof("Unscheduled Job(%s) from Machine(%s)", name, machID)
}
return
}
// attemptScheduleUnit tries to persist a scheduling decision in the
// Registry, returning true on success. If any communication with the
// Registry fails, false is returned.
func (e *Engine) attemptScheduleUnit(name, machID string) bool {
err := e.registry.ScheduleUnit(name, machID)
if err != nil {
log.Errorf("Failed scheduling Unit(%s) to Machine(%s): %v", name, machID, err)
return false
}
log.Infof("Scheduled Unit(%s) to Machine(%s)", name, machID)
return true
}