forked from coreos/fleet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
job.go
345 lines (282 loc) · 8.42 KB
/
job.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
package registry
import (
"errors"
"path"
"strings"
etcdErr "github.com/coreos/fleet/third_party/github.com/coreos/etcd/error"
"github.com/coreos/fleet/third_party/github.com/coreos/go-etcd/etcd"
log "github.com/coreos/fleet/third_party/github.com/golang/glog"
"github.com/coreos/fleet/event"
"github.com/coreos/fleet/job"
"github.com/coreos/fleet/unit"
)
const (
jobPrefix = "job"
)
// GetAllJobs lists all Jobs known by the Registry
func (r *EtcdRegistry) GetAllJobs() ([]job.Job, error) {
var jobs []job.Job
key := path.Join(r.keyPrefix, jobPrefix)
resp, err := r.etcd.Get(key, true, true)
if err != nil {
if isKeyNotFound(err) {
err = nil
}
return jobs, err
}
for _, dir := range resp.Node.Nodes {
for _, node := range dir.Nodes {
if !strings.HasSuffix(node.Key, "object") {
continue
}
j := r.getJobFromJSON(node.Value)
if j == nil {
continue
}
jobs = append(jobs, *j)
}
}
return jobs, nil
}
// GetJobTarget looks up where the given job is scheduled. If the job has
// been scheduled, the ID the target machine is returned. Otherwise, an
// empty string is returned.
func (r *EtcdRegistry) GetJobTarget(jobName string) (string, error) {
// Figure out to which Machine this Job is scheduled
key := r.jobTargetAgentPath(jobName)
resp, err := r.etcd.Get(key, false, true)
if err != nil {
return "", err
}
return resp.Node.Value, nil
}
func (r *EtcdRegistry) ClearJobTarget(jobName, machID string) error {
key := r.jobTargetAgentPath(jobName)
_, err := r.etcd.CompareAndDelete(key, machID, 0)
if isKeyNotFound(err) {
err = nil
}
return err
}
// GetJob looks for a Job of the given name in the Registry. It returns a fully
// hydrated Job on success, or nil on any kind of failure.
func (r *EtcdRegistry) GetJob(jobName string) (j *job.Job, err error) {
key := path.Join(r.keyPrefix, jobPrefix, jobName, "object")
resp, err := r.etcd.Get(key, false, true)
if err != nil {
if isKeyNotFound(err) {
err = nil
}
return
}
j = r.getJobFromJSON(resp.Node.Value)
if j == nil {
log.V(1).Infof("Error unmarshaling Job(%s): %v", jobName, err)
}
return
}
func (r *EtcdRegistry) getJobFromJSON(val string) *job.Job {
var jm jobModel
if err := unmarshal(val, &jm); err != nil {
return nil
}
return r.getJobFromModel(jm)
}
func (r *EtcdRegistry) getJobFromModel(jm jobModel) *job.Job {
var err error
var unit *unit.Unit
// New-style Jobs should have a populated UnitHash, and the contents of the Unit are stored separately in the Registry
if !jm.UnitHash.Empty() {
unit = r.getUnitByHash(jm.UnitHash)
if unit == nil {
log.Warningf("No Unit found in Registry for Job(%s)", jm.Name)
return nil
}
if unit.Hash() != jm.UnitHash {
log.Errorf("Unit Hash %s does not match expected %s for Job(%s)!", unit.Hash(), jm.UnitHash, jm.Name)
return nil
}
log.V(2).Infof("Got Unit for Job(%s) from registry", jm.Name)
} else {
// Old-style Jobs had "Payloads" instead of Units, also stored separately in the Registry
log.V(2).Infof("Legacy Job(%s) has no PayloadHash - looking for associated Payload", jm.Name)
unit, err = r.getUnitFromLegacyPayload(jm.Name)
if err != nil {
log.Errorf("Error retrieving legacy payload for Job(%s)", jm.Name)
return nil
} else if unit == nil {
log.Warningf("No Payload found in Registry for Job(%s)", jm.Name)
return nil
}
log.Infof("Migrating legacy Payload(%s)", jm.Name)
if err := r.storeOrGetUnit(*unit); err != nil {
log.Warningf("Unable to migrate legacy Payload: %v", err)
}
}
j := job.NewJob(jm.Name, *unit)
j.UnitState = r.getUnitState(jm.Name)
j.State = r.determineJobState(jm.Name)
return j
}
// jobModel is used for serializing and deserializing Jobs stored in the Registry
type jobModel struct {
Name string
UnitHash unit.Hash
}
// DestroyJob removes a Job object from the repository, along with any legacy
// associated Payload and SignatureSet. It does not yet remove underlying
// Units from the repository.
func (r *EtcdRegistry) DestroyJob(jobName string) error {
key := path.Join(r.keyPrefix, jobPrefix, jobName)
r.etcd.Delete(key, true)
// TODO(jonboulle): add unit reference counting and actually destroying Units
r.destroyLegacyPayload(jobName)
r.destroySignatureSetOfJob(jobName)
// TODO(jonboulle): handle errors
return nil
}
// destroyLegacyPayload removes an old-style Payload from the registry
func (r *EtcdRegistry) destroyLegacyPayload(payloadName string) {
key := path.Join(r.keyPrefix, payloadPrefix, payloadName)
r.etcd.Delete(key, false)
}
// CreateJob attempts to store a Job and its associated Unit in the registry
func (r *EtcdRegistry) CreateJob(j *job.Job) (err error) {
if err := r.storeOrGetUnit(j.Unit); err != nil {
return err
}
key := path.Join(r.keyPrefix, jobPrefix, j.Name, "object")
jm := jobModel{
Name: j.Name,
UnitHash: j.UnitHash,
}
json, err := marshal(jm)
if err != nil {
return
}
_, err = r.etcd.Create(key, json, 0)
if err != nil && err.(*etcd.EtcdError).ErrorCode == etcdErr.EcodeNodeExist {
err = errors.New("job already exists")
}
return
}
func (r *EtcdRegistry) GetJobTargetState(jobName string) (*job.JobState, error) {
key := r.jobTargetStatePath(jobName)
resp, err := r.etcd.Get(key, false, false)
if err != nil {
if err.(*etcd.EtcdError).ErrorCode != etcdErr.EcodeNodeExist {
log.Errorf("Unable to determine target-state of Job(%s): %v", jobName, err)
}
return nil, err
}
return job.ParseJobState(resp.Node.Value), nil
}
func (r *EtcdRegistry) SetJobTargetState(jobName string, state job.JobState) error {
key := r.jobTargetStatePath(jobName)
_, err := r.etcd.Set(key, string(state), 0)
return err
}
func (es *EventStream) filterJobTargetStateChanges(resp *etcd.Response) *event.Event {
if resp.Action != "set" {
return nil
}
dir, baseName := path.Split(resp.Node.Key)
if baseName != "target-state" {
return nil
}
dir = strings.TrimSuffix(dir, "/")
jobName := path.Base(dir)
ts := job.ParseJobState(resp.Node.Value)
if ts == nil {
return nil
}
cs := es.registry.determineJobState(jobName)
if *cs == *ts {
return nil
}
var cType string
switch *cs {
case job.JobStateInactive:
cType = "CommandLoadJob"
case job.JobStateLoaded:
if *ts == job.JobStateInactive {
cType = "CommandUnloadJob"
} else if *ts == job.JobStateLaunched {
cType = "CommandStartJob"
}
case job.JobStateLaunched:
if *ts == job.JobStateLoaded {
cType = "CommandStopJob"
} else if *ts == job.JobStateInactive {
cType = "CommandUnloadJob"
}
}
if cType == "" {
return nil
}
agent, _ := es.registry.GetJobTarget(jobName)
return &event.Event{cType, jobName, agent}
}
func (r *EtcdRegistry) ScheduleJob(jobName string, machID string) error {
key := r.jobTargetAgentPath(jobName)
_, err := r.etcd.Create(key, machID, 0)
return err
}
func (r *EtcdRegistry) LockJob(jobName, context string) *TimedResourceMutex {
return r.lockResource("job", jobName, context)
}
func filterEventJobScheduled(resp *etcd.Response) *event.Event {
if resp.Action != "create" {
return nil
}
dir, baseName := path.Split(resp.Node.Key)
if baseName != "target" {
return nil
}
dir = strings.TrimSuffix(dir, "/")
dir, jobName := path.Split(dir)
dir = strings.TrimSuffix(dir, "/")
dir, prefixName := path.Split(dir)
if prefixName != jobPrefix {
return nil
}
return &event.Event{"EventJobScheduled", jobName, resp.Node.Value}
}
func filterEventJobUnscheduled(resp *etcd.Response) *event.Event {
if resp.Action != "delete" && resp.Action != "compareAndDelete" {
return nil
}
dir, baseName := path.Split(resp.Node.Key)
if baseName != "target" {
return nil
}
dir = strings.TrimSuffix(dir, "/")
dir, jobName := path.Split(dir)
dir = strings.TrimSuffix(dir, "/")
dir, prefixName := path.Split(dir)
if prefixName != jobPrefix {
return nil
}
if resp.PrevNode == nil {
return nil
}
return &event.Event{"EventJobUnscheduled", jobName, resp.PrevNode.Value}
}
func filterEventJobDestroyed(resp *etcd.Response) *event.Event {
if resp.Action != "delete" {
return nil
}
dir, jobName := path.Split(resp.Node.Key)
dir = strings.TrimSuffix(dir, "/")
dir, prefixName := path.Split(dir)
if prefixName != jobPrefix {
return nil
}
return &event.Event{"EventJobDestroyed", jobName, nil}
}
func (r *EtcdRegistry) jobTargetAgentPath(jobName string) string {
return path.Join(r.keyPrefix, jobPrefix, jobName, "target")
}
func (r *EtcdRegistry) jobTargetStatePath(jobName string) string {
return path.Join(r.keyPrefix, jobPrefix, jobName, "target-state")
}