-
Notifications
You must be signed in to change notification settings - Fork 18
/
worker.go
364 lines (290 loc) · 9.57 KB
/
worker.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
package worker
import (
"context"
"errors"
"fmt"
"github.com/cirruslabs/cirrus-ci-agent/api"
"github.com/cirruslabs/cirrus-cli/internal/executor/instance/abstract"
"github.com/cirruslabs/cirrus-cli/internal/executor/instance/persistentworker"
"github.com/cirruslabs/cirrus-cli/internal/executor/instance/persistentworker/isolation/tart"
"github.com/cirruslabs/cirrus-cli/internal/executor/instance/persistentworker/isolation/vetu"
"github.com/cirruslabs/cirrus-cli/internal/version"
"github.com/cirruslabs/cirrus-cli/internal/worker/security"
upstreampkg "github.com/cirruslabs/cirrus-cli/internal/worker/upstream"
"github.com/cirruslabs/echelon"
"github.com/cirruslabs/echelon/renderers"
"github.com/puzpuzpuz/xsync/v3"
"github.com/sirupsen/logrus"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"math"
"os"
"runtime"
"time"
)
var (
ErrInitializationFailed = errors.New("worker initialization failed")
ErrShutdown = errors.New("worker is shutting down")
tracer = otel.Tracer("worker")
meter = otel.Meter("worker")
)
type Worker struct {
upstreams []*upstreampkg.Upstream
security *security.Security
userSpecifiedLabels map[string]string
userSpecifiedResources map[string]float64
tasks *xsync.MapOf[int64, *Task]
taskCompletions chan int64
imagesCounter metric.Int64Counter
tasksCounter metric.Int64Counter
standbyHitCounter metric.Int64Counter
logger logrus.FieldLogger
standbyConfig *StandbyConfig
standbyInstance abstract.Instance
}
func New(opts ...Option) (*Worker, error) {
worker := &Worker{
upstreams: []*upstreampkg.Upstream{},
security: security.NoSecurity(),
userSpecifiedLabels: make(map[string]string),
tasks: xsync.NewMapOf[int64, *Task](),
taskCompletions: make(chan int64),
logger: logrus.New(),
}
// Apply options
for _, opt := range opts {
opt(worker)
}
// Sanity check
if len(worker.upstreams) == 0 {
return nil, fmt.Errorf("%w: no upstreams were specified", ErrInitializationFailed)
}
// Image-related metrics
imagesCounter, err := meter.Int64Counter("org.cirruslabs.persistent_worker.images.total")
if err != nil {
return nil, fmt.Errorf("%w: failed to initialize images counter: %v",
ErrInitializationFailed, err)
}
worker.imagesCounter = imagesCounter
return worker, nil
}
func (worker *Worker) info(workerName string) *api.WorkerInfo {
hostname, err := os.Hostname()
if err != nil {
hostname = ""
}
const (
ReservedLabelName = "name"
ReservedLabelVersion = "version"
ReservedLabelHostname = "hostname"
ReservedLabelOS = "os"
ReservedLabelArchitecture = "arch"
)
// Create base labels
labels := map[string]string{
ReservedLabelName: workerName,
ReservedLabelVersion: version.FullVersion,
ReservedLabelHostname: hostname,
ReservedLabelOS: runtime.GOOS,
ReservedLabelArchitecture: runtime.GOARCH,
}
// Merge with the user specified labels
for key, value := range worker.userSpecifiedLabels {
labels[key] = value
}
return &api.WorkerInfo{
Labels: labels,
ResourcesTotal: worker.userSpecifiedResources,
}
}
func (worker *Worker) Run(ctx context.Context) error {
// Task-related metrics
_, err := meter.Int64ObservableGauge("org.cirruslabs.persistent_worker.tasks.running_count",
metric.WithDescription("Number of tasks running on the Persistent Worker."),
metric.WithInt64Callback(func(ctx context.Context, observer metric.Int64Observer) error {
observer.Observe(int64(worker.tasks.Size()))
return nil
}),
)
if err != nil {
return err
}
worker.tasksCounter, err = meter.Int64Counter("org.cirruslabs.persistent_worker.tasks.count")
if err != nil {
return err
}
// Resource-related metrics
_, err = meter.Float64ObservableGauge("org.cirruslabs.persistent_worker.resources.unused_count",
metric.WithDescription("Amount of resources available for use on the Persistent Worker."),
metric.WithFloat64Callback(func(ctx context.Context, observer metric.Float64Observer) error {
for key, value := range worker.resourcesNotInUse() {
observer.Observe(value, metric.WithAttributes(attribute.String("name", key)))
}
return nil
}),
)
if err != nil {
return err
}
_, err = meter.Float64ObservableGauge("org.cirruslabs.persistent_worker.resources.used_count",
metric.WithDescription("Amount of resources used on the Persistent Worker."),
metric.WithFloat64Callback(func(ctx context.Context, observer metric.Float64Observer) error {
for key, value := range worker.resourcesInUse() {
observer.Observe(value, metric.WithAttributes(attribute.String("name", key)))
}
return nil
}),
)
if err != nil {
return err
}
// standby-related metrics
worker.standbyHitCounter, err = meter.Int64Counter("org.cirruslabs.persistent_worker.standby.hit")
if err != nil {
return err
}
// https://github.com/cirruslabs/cirrus-cli/issues/571
if tart.Installed() {
if err := tart.Cleanup(); err != nil {
worker.logger.Warnf("failed to cleanup Tart VMs: %v", err)
}
}
if vetu.Installed() {
if err := vetu.Cleanup(); err != nil {
worker.logger.Warnf("failed to cleanup Vetu VMs: %v", err)
}
}
// A sub-context to cancel out all Run() side-effects when it finishes
subCtx, cancel := context.WithCancel(ctx)
defer cancel()
for {
// Create and start a standby instance if configured and not created yet
worker.tryCreateStandby(ctx)
for _, upstream := range worker.upstreams {
if err := worker.pollSingleUpstream(subCtx, upstream); err != nil {
if errors.Is(err, ErrShutdown) {
return nil
}
worker.logger.Errorf("failed to poll upstream %s: %v", upstream.Name(), err)
}
}
select {
case <-subCtx.Done():
return nil
case <-time.After(time.Duration(worker.pollIntervalSeconds()) * time.Second):
// continue the loop
}
}
}
func (worker *Worker) tryCreateStandby(ctx context.Context) {
// Do nothing if no standby instance is configured
if worker.standbyConfig == nil {
return
}
// Do nothing if the standby instance is already instantiated
if worker.standbyInstance != nil {
return
}
// Do nothing if there are tasks that are running to simplify the resource management
if !worker.canFitResources(worker.standbyConfig.Resources) {
return
}
worker.logger.Debugf("creating a new standby instance with isolation %s", worker.standbyConfig.Isolation)
standbyInstance, err := persistentworker.New(worker.standbyConfig.Isolation, worker.security, worker.logger)
if err != nil {
worker.logger.Errorf("failed to create a standby instance: %v", err)
return
}
logger := echelon.NewLogger(echelon.TraceLevel, renderers.NewSimpleRenderer(os.Stdout, nil))
worker.logger.Debugf("warming-up the standby instance")
if err := standbyInstance.(abstract.WarmableInstance).Warmup(ctx, "standby", nil, logger); err != nil {
worker.logger.Errorf("failed to warm-up a standby instance: %v", err)
if err := standbyInstance.Close(ctx); err != nil {
worker.logger.Errorf("failed to terminate the standby instance after an: "+
"unsuccessful warm-up: %v", err)
}
return
}
worker.logger.Debugf("standby instance had successfully warmed-up")
worker.standbyInstance = standbyInstance
}
func (worker *Worker) pollSingleUpstream(ctx context.Context, upstream *upstreampkg.Upstream) error {
if err := upstream.Register(ctx, worker.info(upstream.WorkerName())); err != nil {
worker.logger.Errorf("failed to register worker with the upstream %s: %v",
upstream.Name(), err)
return nil
}
// De-register completed tasks
worker.registerTaskCompletions()
request := &api.PollRequest{
WorkerInfo: worker.info(upstream.WorkerName()),
RunningTasks: worker.runningTasks(upstream),
ResourcesInUse: worker.resourcesInUse(),
}
response, err := upstream.Poll(ctx, request)
if err != nil {
return err
}
for _, taskToStop := range response.TasksToStop {
worker.stopTask(taskToStop)
}
for _, taskToStart := range response.TasksToStart {
worker.startTask(ctx, upstream, taskToStart)
}
if response.Shutdown {
worker.logger.Infof("received shutdown signal from the upstream %s, terminating...",
upstream.Name())
return ErrShutdown
}
return nil
}
func (worker *Worker) Pause(ctx context.Context, wait bool) error {
// A sub-context to cancel this function's side effects when it finishes
subCtx, cancel := context.WithCancel(ctx)
defer cancel()
for _, upstream := range worker.upstreams {
if err := upstream.SetDisabled(subCtx, true); err != nil {
return err
}
}
if !wait {
return nil
}
for _, upstream := range worker.upstreams {
for {
response, err := upstream.QueryRunningTasks(ctx, &api.QueryRunningTasksRequest{})
if err != nil {
return fmt.Errorf("upstream %s failed while waiting: %w", upstream.Name(), err)
} else if len(response.RunningTasks) == 0 {
// done waiting for the current upstream
break
}
select {
case <-ctx.Done():
return nil
case <-time.After(time.Duration(upstream.PollIntervalSeconds()) * time.Second):
// continue waiting for the current upstream
}
}
}
return nil
}
func (worker *Worker) Resume(ctx context.Context) (err error) {
// A sub-context to cancel this function's side effects when it finishes
subCtx, cancel := context.WithCancel(ctx)
defer cancel()
for _, slot := range worker.upstreams {
err = slot.SetDisabled(subCtx, false)
}
return
}
func (worker *Worker) pollIntervalSeconds() uint32 {
result := uint32(math.MaxUint32)
for _, upstream := range worker.upstreams {
if upstream.PollIntervalSeconds() < result {
result = upstream.PollIntervalSeconds()
}
}
return result
}