-
Notifications
You must be signed in to change notification settings - Fork 414
/
hatchery.go
499 lines (431 loc) · 14.5 KB
/
hatchery.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
package hatchery
import (
"context"
"fmt"
"os"
"os/exec"
"os/signal"
"strconv"
"strings"
"syscall"
"time"
cache "github.com/patrickmn/go-cache"
"go.opencensus.io/stats"
"go.opencensus.io/tag"
"go.opencensus.io/trace"
"github.com/ovh/cds/engine/api/observability"
"github.com/ovh/cds/sdk"
"github.com/ovh/cds/sdk/log"
"github.com/ovh/cds/sdk/tracingutils"
)
var (
// Client is a CDS Client
Client sdk.HTTPClient
defaultMaxProvisioning = 10
models []sdk.Model
// Opencensus tags
TagHatchery tag.Key
TagHatcheryName tag.Key
)
func init() {
TagHatchery, _ = tag.NewKey("hatchery")
TagHatcheryName, _ = tag.NewKey("hatchery_name")
}
// WithTags returns a context with opencenstus tags
func WithTags(ctx context.Context, h Interface) context.Context {
ctx, _ = tag.New(ctx,
tag.Upsert(TagHatchery, h.ServiceName()),
tag.Upsert(TagHatcheryName, h.Service().Name),
)
return ctx
}
// Create creates hatchery
func Create(h Interface) error {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
// Gracefully shutdown connections
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
defer func() {
signal.Stop(c)
cancel()
}()
go func() {
select {
case <-c:
defer cancel()
return
case <-ctx.Done():
return
}
}()
// Init call hatchery.Register()
if err := h.Init(); err != nil {
return fmt.Errorf("Create> Init error: %v", err)
}
// Call WorkerModel Enabled first
var errwm error
models, errwm = h.WorkerModelsEnabled()
if errwm != nil {
log.Error("error on h.WorkerModelsEnabled() (init call): %v", errwm)
}
tickerProvision := time.NewTicker(time.Duration(h.Configuration().Provision.Frequency) * time.Second)
tickerRegister := time.NewTicker(time.Duration(h.Configuration().Provision.RegisterFrequency) * time.Second)
tickerGetModels := time.NewTicker(10 * time.Second)
defer func() {
tickerProvision.Stop()
tickerRegister.Stop()
tickerGetModels.Stop()
}()
pbjobs := make(chan sdk.PipelineBuildJob, 2)
wjobs := make(chan sdk.WorkflowNodeJobRun, h.Configuration().Provision.MaxConcurrentProvisioning)
errs := make(chan error, 1)
// Create a cache with a default expiration time of 3 second, and which
// purges expired items every minute
spawnIDs := cache.New(10*time.Second, 60*time.Second)
// hatchery is now fully Initialized
h.SetInitialized()
sdk.GoRoutine("queuePolling",
func() {
if err := h.CDSClient().QueuePolling(ctx, wjobs, pbjobs, errs, 20*time.Second, h.Configuration().Provision.GraceTimeQueued, h.ModelType(), h.Hatchery().RatioService, nil); err != nil {
log.Error("Queues polling stopped: %v", err)
cancel()
}
},
PanicDump(h),
)
// run the starters pool
workersStartChan, workerStartResultChan := startWorkerStarters(ctx, h)
hostname, errh := os.Hostname()
if errh != nil {
return fmt.Errorf("Create> Cannot retrieve hostname: %s", errh)
}
// read the result channel in another goroutine to let the main goroutine start new workers
sdk.GoRoutine("checkStarterResult",
func() {
for startWorkerRes := range workerStartResultChan {
if startWorkerRes.err != nil {
errs <- startWorkerRes.err
}
if startWorkerRes.temptToSpawn {
found := false
for _, hID := range startWorkerRes.request.spawnAttempts {
if hID == h.ID() {
found = true
break
}
}
if !found {
ctxHatcheryCount, cancelHatcheryCount := context.WithTimeout(ctx, 10*time.Second)
if hCount, err := h.CDSClient().HatcheryCount(ctxHatcheryCount, startWorkerRes.request.workflowNodeRunID); err == nil {
if int64(len(startWorkerRes.request.spawnAttempts)) < hCount {
ctxtJobIncAttemps, cancelJobIncAttemps := context.WithTimeout(ctx, 10*time.Second)
if _, errQ := h.CDSClient().QueueJobIncAttempts(ctxtJobIncAttemps, startWorkerRes.request.id); errQ != nil {
log.Warning("Hatchery> Create> cannot inc spawn attempts %v", errQ)
}
cancelJobIncAttemps()
}
} else {
log.Warning("Hatchery> Create> cannot get hatchery count: %v", err)
}
cancelHatcheryCount()
}
}
}
},
PanicDump(h),
)
// read the errs channel in another goroutine too
sdk.GoRoutine("checkErrs",
func() {
for err := range errs {
log.Error("%v", err)
}
},
PanicDump(h),
)
// the main goroutine
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-tickerGetModels.C:
var errwm error
models, errwm = h.WorkerModelsEnabled()
if errwm != nil {
log.Error("error on h.WorkerModelsEnabled(): %v", errwm)
}
case j := <-pbjobs:
t0 := time.Now()
if j.ID == 0 {
continue
}
//Check spawnsID
if _, exist := spawnIDs.Get(strconv.FormatInt(j.ID, 10)); exist {
log.Debug("pipeline build job %d already spawned in previous routine", j.ID)
continue
}
//Before doing anything, push in cache
spawnIDs.SetDefault(strconv.FormatInt(j.ID, 10), j.ID)
//Check bookedBy current hatchery
if j.BookedBy.ID != 0 && j.BookedBy.ID != h.ID() {
continue
}
//Check gracetime
if j.QueuedSeconds < int64(h.Configuration().Provision.GraceTimeQueued) {
log.Debug("pipeline build job %d is too fresh, queued since %d seconds, let existing waiting worker check it", j.ID, j.QueuedSeconds)
continue
}
//Check if hatchery if able to start a new worker
if !checkCapacities(h) {
log.Info("hatchery %s is not able to provision new worker", h.Service().Name)
continue
}
//Ask to start
workerRequest := workerStarterRequest{
id: j.ID,
isWorkflowJob: false,
execGroups: j.ExecGroups,
requirements: j.Job.Action.Requirements,
hostname: hostname,
timestamp: time.Now().Unix(),
}
// Check at least one worker model can match
var chosenModel *sdk.Model
for i := range models {
if canRunJob(h, workerRequest, models[i]) {
chosenModel = &models[i]
break
}
}
if chosenModel == nil {
//do something
continue
}
workerRequest.model = *chosenModel
log.Info("hatchery> Request a worker for pipeline build job %d (%.3f seconds elapsed)", j.ID, time.Since(t0).Seconds())
workersStartChan <- workerRequest
case j := <-wjobs:
t0 := time.Now()
if j.ID == 0 {
continue
}
var traceEnded *struct{}
currentCtx, currentCancel := context.WithTimeout(ctx, 10*time.Minute)
currentCtx = WithTags(currentCtx, h)
if val, has := j.Header.Get(tracingutils.SampledHeader); has && val == "1" {
currentCtx, _ = observability.New(currentCtx, h.ServiceName(), "hatchery.JobReceive", trace.AlwaysSample(), trace.SpanKindServer)
r, _ := j.Header.Get(sdk.WorkflowRunHeader)
w, _ := j.Header.Get(sdk.WorkflowHeader)
p, _ := j.Header.Get(sdk.ProjectKeyHeader)
observability.Current(currentCtx,
observability.Tag(observability.TagWorkflow, w),
observability.Tag(observability.TagWorkflowRun, r),
observability.Tag(observability.TagProjectKey, p),
observability.Tag(observability.TagWorkflowNodeJobRun, j.ID),
)
if _, ok := j.Header["SSE"]; ok {
log.Debug("hatchery> received job from SSE")
observability.Current(currentCtx,
observability.Tag("from", "sse"),
)
}
}
endTrace := func(reason string) {
if reason != "" {
observability.Current(currentCtx,
observability.Tag("reason", reason),
)
}
observability.End(currentCtx, nil, nil) // nolint
var T struct{}
traceEnded = &T
currentCancel()
}
go func() {
<-currentCtx.Done()
if traceEnded == nil {
endTrace(currentCtx.Err().Error())
}
}()
stats.Record(currentCtx, h.Stats().Jobs.M(1))
if _, ok := j.Header["SSE"]; ok {
stats.Record(currentCtx, h.Stats().JobsSSE.M(1))
}
//Check if the jobs is concerned by a pending worker creation
if _, exist := spawnIDs.Get(strconv.FormatInt(j.ID, 10)); exist {
log.Debug("job %d already spawned in previous routine", j.ID)
endTrace("already spawned")
continue
}
//Before doing anything, push in cache
spawnIDs.SetDefault(strconv.FormatInt(j.ID, 10), j.ID)
//Check bookedBy current hatchery
if j.BookedBy.ID != 0 && j.BookedBy.ID != h.ID() {
log.Debug("hatchery> job %d is booked by someone else (%d / %d)", j.ID, j.BookedBy.ID, h.ID())
endTrace("booked by someone else")
continue
}
//Check if hatchery if able to start a new worker
if !checkCapacities(h) {
log.Info("hatchery %s is not able to provision new worker", h.Service().Name)
endTrace("no capacities")
continue
}
workerRequest := workerStarterRequest{
ctx: currentCtx,
cancel: endTrace,
id: j.ID,
isWorkflowJob: true,
execGroups: j.ExecGroups,
requirements: j.Job.Action.Requirements,
hostname: hostname,
timestamp: time.Now().Unix(),
spawnAttempts: j.SpawnAttempts,
workflowNodeRunID: j.WorkflowNodeRunID,
}
// Check at least one worker model can match
var chosenModel *sdk.Model
for i := range models {
if canRunJob(h, workerRequest, models[i]) {
chosenModel = &models[i]
break
}
}
// No model has been found, let's send a failing result
if chosenModel == nil {
workerStartResultChan <- workerStarterResult{
request: workerRequest,
isRun: false,
temptToSpawn: true,
}
log.Debug("hatchery> no model")
endTrace("no model")
continue
}
//We got a model, let's start a worker
workerRequest.model = *chosenModel
//Ask to start
log.Debug("hatchery> Request a worker for job %d (%.3f seconds elapsed)", j.ID, time.Since(t0).Seconds())
workersStartChan <- workerRequest
case <-tickerProvision.C:
provisioning(h, models)
case <-tickerRegister.C:
if err := workerRegister(h, workersStartChan); err != nil {
log.Warning("Error on workerRegister: %s", err)
}
}
}
}
// MemoryRegisterContainer is the RAM used for spawning
// a docker container for register a worker model. 128 Mo
const MemoryRegisterContainer int64 = 128
// CheckRequirement checks binary requirement in path
func CheckRequirement(r sdk.Requirement) (bool, error) {
switch r.Type {
case sdk.BinaryRequirement:
if _, err := exec.LookPath(r.Value); err != nil {
// Return nil because the error contains 'Exit status X', that's what we wanted
return false, nil
}
return true, nil
default:
return false, nil
}
}
func canRunJob(h Interface, j workerStarterRequest, model sdk.Model) bool {
if model.Type != h.ModelType() {
log.Debug("canRunJob> model %s type:%s current hatchery modelType: %s", model.Name, model.Type, h.ModelType())
return false
}
// If the model needs registration, don't spawn for now
if h.NeedRegistration(&model) {
log.Debug("canRunJob> model %s needs registration", model.Name)
return false
}
// if current hatchery is in same group than worker model -> do not avoid spawn, even if worker model is in error
if model.NbSpawnErr > 5 && *h.Service().GroupID != model.ID {
log.Warning("canRunJob> Too many errors on spawn with model %s, please check this worker model", model.Name)
return false
}
if len(j.execGroups) > 0 {
checkGroup := false
for _, g := range j.execGroups {
if g.ID == model.GroupID {
checkGroup = true
break
}
}
if !checkGroup {
log.Debug("canRunJob> job %d - model %s attached to group %d can't run this job", j.id, model.Name, model.GroupID)
return false
}
}
var containsModelRequirement, containsHostnameRequirement bool
for _, r := range j.requirements {
switch r.Type {
case sdk.ModelRequirement:
containsModelRequirement = true
case sdk.HostnameRequirement:
containsHostnameRequirement = true
}
}
// Common check
for _, r := range j.requirements {
// If requirement is a Model requirement, it's easy. It's either can or can't run
// r.Value could be: theModelName --port=8888:9999, so we take strings.Split(r.Value, " ")[0] to compare
// only modelName
if r.Type == sdk.ModelRequirement && strings.Split(r.Value, " ")[0] != model.Name {
log.Debug("canRunJob> %d - job %d - model requirement r.Value(%s) != model.Name(%s)", j.timestamp, j.id, strings.Split(r.Value, " ")[0], model.Name)
return false
}
// If requirement is an hostname requirement, it's for a specific worker
if r.Type == sdk.HostnameRequirement && r.Value != j.hostname {
log.Debug("canRunJob> %d - job %d - hostname requirement r.Value(%s) != hostname(%s)", j.timestamp, j.id, r.Value, j.hostname)
return false
}
// service and memory requirements are only supported by docker model
if model.Type != sdk.Docker && (r.Type == sdk.ServiceRequirement || r.Type == sdk.MemoryRequirement) {
log.Debug("canRunJob> %d - job %d - job with service requirement or memory requirement: only for model docker. current model:%s", j.timestamp, j.id, model.Type)
return false
}
// Skip network access requirement as we can't check it
if r.Type == sdk.NetworkAccessRequirement || r.Type == sdk.PluginRequirement || r.Type == sdk.ServiceRequirement || r.Type == sdk.MemoryRequirement {
log.Debug("canRunJob> %d - job %d - job with service requirement or memory requirement: only for model docker. current model:%s", j.timestamp, j.id, model.Type)
continue
}
if r.Type == sdk.OSArchRequirement && model.RegisteredOS != "" && model.RegisteredArch != "" && r.Value != (model.RegisteredOS+"/"+model.RegisteredArch) {
log.Debug("canRunJob> %d - job %d - job with OSArch requirement: cannot spawn on this OSArch. current model: %s/%s", j.timestamp, j.id, model.RegisteredOS, model.RegisteredArch)
return false
}
if !containsModelRequirement && !containsHostnameRequirement {
if r.Type == sdk.BinaryRequirement {
found := false
// Check binary requirement against worker model capabilities
for _, c := range model.RegisteredCapabilities {
if r.Value == c.Value || r.Value == c.Name {
found = true
break
}
}
if !found {
log.Debug("canRunJob> %d - job %d - model(%s) does not have binary %s(%s) for this job.", j.timestamp, j.id, model.Name, r.Name, r.Value)
return false
}
}
}
}
return h.CanSpawn(&model, j.id, j.requirements)
}
func logTime(h Interface, name string, then time.Time) {
d := time.Since(then)
if d > time.Duration(h.Configuration().LogOptions.SpawnOptions.ThresholdCritical)*time.Second {
log.Error("%s took %s to execute", name, d)
return
}
if d > time.Duration(h.Configuration().LogOptions.SpawnOptions.ThresholdWarning)*time.Second {
log.Warning("%s took %s to execute", name, d)
return
}
log.Debug("%s took %s to execute", name, d)
}