Fix 2.4.2 upgrade notes formatting refs #19330
[arvados.git] / lib / dispatchcloud / container / queue.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package container
6
7 import (
8         "errors"
9         "io"
10         "sync"
11         "time"
12
13         "git.arvados.org/arvados.git/sdk/go/arvados"
14         "github.com/prometheus/client_golang/prometheus"
15         "github.com/sirupsen/logrus"
16 )
17
18 type typeChooser func(*arvados.Container) (arvados.InstanceType, error)
19
20 // An APIClient performs Arvados API requests. It is typically an
21 // *arvados.Client.
22 type APIClient interface {
23         RequestAndDecode(dst interface{}, method, path string, body io.Reader, params interface{}) error
24 }
25
26 // A QueueEnt is an entry in the queue, consisting of a container
27 // record and the instance type that should be used to run it.
28 type QueueEnt struct {
29         // The container to run. Only the UUID, State, Priority,
30         // RuntimeConstraints, Mounts, and ContainerImage fields are
31         // populated.
32         Container    arvados.Container    `json:"container"`
33         InstanceType arvados.InstanceType `json:"instance_type"`
34         FirstSeenAt  time.Time            `json:"first_seen_at"`
35 }
36
37 // String implements fmt.Stringer by returning the queued container's
38 // UUID.
39 func (c *QueueEnt) String() string {
40         return c.Container.UUID
41 }
42
43 // A Queue is an interface to an Arvados cluster's container
44 // database. It presents only the containers that are eligible to be
45 // run by, are already being run by, or have recently been run by the
46 // present dispatcher.
47 //
48 // The Entries, Get, and Forget methods do not block: they return
49 // immediately, using cached data.
50 //
51 // The updating methods (Cancel, Lock, Unlock, Update) do block: they
52 // return only after the operation has completed.
53 //
54 // A Queue's Update method should be called periodically to keep the
55 // cache up to date.
56 type Queue struct {
57         logger     logrus.FieldLogger
58         chooseType typeChooser
59         client     APIClient
60
61         auth    *arvados.APIClientAuthorization
62         current map[string]QueueEnt
63         updated time.Time
64         mtx     sync.Mutex
65
66         // Methods that modify the Queue (like Lock) add the affected
67         // container UUIDs to dontupdate. When applying a batch of
68         // updates received from the network, anything appearing in
69         // dontupdate is skipped, in case the received update has
70         // already been superseded by the locally initiated change.
71         // When no network update is in progress, this protection is
72         // not needed, and dontupdate is nil.
73         dontupdate map[string]struct{}
74
75         // active notification subscribers (see Subscribe)
76         subscribers map[<-chan struct{}]chan struct{}
77 }
78
79 // NewQueue returns a new Queue. When a new container appears in the
80 // Arvados cluster's queue during Update, chooseType will be called to
81 // assign an appropriate arvados.InstanceType for the queue entry.
82 func NewQueue(logger logrus.FieldLogger, reg *prometheus.Registry, chooseType typeChooser, client APIClient) *Queue {
83         cq := &Queue{
84                 logger:      logger,
85                 chooseType:  chooseType,
86                 client:      client,
87                 current:     map[string]QueueEnt{},
88                 subscribers: map[<-chan struct{}]chan struct{}{},
89         }
90         if reg != nil {
91                 go cq.runMetrics(reg)
92         }
93         return cq
94 }
95
96 // Subscribe returns a channel that becomes ready to receive when an
97 // entry in the Queue is updated.
98 //
99 //      ch := q.Subscribe()
100 //      defer q.Unsubscribe(ch)
101 //      for range ch {
102 //              // ...
103 //      }
104 func (cq *Queue) Subscribe() <-chan struct{} {
105         cq.mtx.Lock()
106         defer cq.mtx.Unlock()
107         ch := make(chan struct{}, 1)
108         cq.subscribers[ch] = ch
109         return ch
110 }
111
112 // Unsubscribe stops sending updates to the given channel. See
113 // Subscribe.
114 func (cq *Queue) Unsubscribe(ch <-chan struct{}) {
115         cq.mtx.Lock()
116         defer cq.mtx.Unlock()
117         delete(cq.subscribers, ch)
118 }
119
120 // Caller must have lock.
121 func (cq *Queue) notify() {
122         for _, ch := range cq.subscribers {
123                 select {
124                 case ch <- struct{}{}:
125                 default:
126                 }
127         }
128 }
129
130 // Forget drops the specified container from the cache. It should be
131 // called on finalized containers to avoid leaking memory over
132 // time. It is a no-op if the indicated container is not in a
133 // finalized state.
134 func (cq *Queue) Forget(uuid string) {
135         cq.mtx.Lock()
136         defer cq.mtx.Unlock()
137         ctr := cq.current[uuid].Container
138         if ctr.State == arvados.ContainerStateComplete || ctr.State == arvados.ContainerStateCancelled || (ctr.State == arvados.ContainerStateQueued && ctr.Priority == 0) {
139                 cq.delEnt(uuid, ctr.State)
140         }
141 }
142
143 // Get returns the (partial) Container record for the specified
144 // container. Like a map lookup, its second return value is false if
145 // the specified container is not in the Queue.
146 func (cq *Queue) Get(uuid string) (arvados.Container, bool) {
147         cq.mtx.Lock()
148         defer cq.mtx.Unlock()
149         ctr, ok := cq.current[uuid]
150         if !ok {
151                 return arvados.Container{}, false
152         }
153         return ctr.Container, true
154 }
155
156 // Entries returns all cache entries, keyed by container UUID.
157 //
158 // The returned threshold indicates the maximum age of any cached data
159 // returned in the map. This makes it possible for a scheduler to
160 // determine correctly the outcome of a remote process that updates
161 // container state. It must first wait for the remote process to exit,
162 // then wait for the Queue to start and finish its next Update --
163 // i.e., it must wait until threshold > timeProcessExited.
164 func (cq *Queue) Entries() (entries map[string]QueueEnt, threshold time.Time) {
165         cq.mtx.Lock()
166         defer cq.mtx.Unlock()
167         entries = make(map[string]QueueEnt, len(cq.current))
168         for uuid, ctr := range cq.current {
169                 entries[uuid] = ctr
170         }
171         threshold = cq.updated
172         return
173 }
174
175 // Update refreshes the cache from the Arvados API. It adds newly
176 // queued containers, and updates the state of previously queued
177 // containers.
178 func (cq *Queue) Update() error {
179         cq.mtx.Lock()
180         cq.dontupdate = map[string]struct{}{}
181         updateStarted := time.Now()
182         cq.mtx.Unlock()
183
184         next, err := cq.poll()
185         if err != nil {
186                 return err
187         }
188
189         cq.mtx.Lock()
190         defer cq.mtx.Unlock()
191         for uuid, ctr := range next {
192                 if _, dontupdate := cq.dontupdate[uuid]; dontupdate {
193                         // Don't clobber a local update that happened
194                         // after we started polling.
195                         continue
196                 }
197                 if cur, ok := cq.current[uuid]; !ok {
198                         cq.addEnt(uuid, *ctr)
199                 } else {
200                         cur.Container = *ctr
201                         cq.current[uuid] = cur
202                 }
203         }
204         for uuid, ent := range cq.current {
205                 if _, dontupdate := cq.dontupdate[uuid]; dontupdate {
206                         // Don't expunge an entry that was
207                         // added/updated locally after we started
208                         // polling.
209                         continue
210                 } else if _, stillpresent := next[uuid]; !stillpresent {
211                         // Expunge an entry that no longer appears in
212                         // the poll response (evidently it's
213                         // cancelled, completed, deleted, or taken by
214                         // a different dispatcher).
215                         cq.delEnt(uuid, ent.Container.State)
216                 }
217         }
218         cq.dontupdate = nil
219         cq.updated = updateStarted
220         cq.notify()
221         return nil
222 }
223
224 // Caller must have lock.
225 func (cq *Queue) delEnt(uuid string, state arvados.ContainerState) {
226         cq.logger.WithFields(logrus.Fields{
227                 "ContainerUUID": uuid,
228                 "State":         state,
229         }).Info("dropping container from queue")
230         delete(cq.current, uuid)
231 }
232
233 // Caller must have lock.
234 func (cq *Queue) addEnt(uuid string, ctr arvados.Container) {
235         it, err := cq.chooseType(&ctr)
236         if err != nil && (ctr.State == arvados.ContainerStateQueued || ctr.State == arvados.ContainerStateLocked) {
237                 // We assume here that any chooseType error is a hard
238                 // error: it wouldn't help to try again, or to leave
239                 // it for a different dispatcher process to attempt.
240                 errorString := err.Error()
241                 logger := cq.logger.WithField("ContainerUUID", ctr.UUID)
242                 logger.WithError(err).Warn("cancel container with no suitable instance type")
243                 go func() {
244                         if ctr.State == arvados.ContainerStateQueued {
245                                 // Can't set runtime error without
246                                 // locking first.
247                                 err := cq.Lock(ctr.UUID)
248                                 if err != nil {
249                                         logger.WithError(err).Warn("lock failed")
250                                         return
251                                         // ...and try again on the
252                                         // next Update, if the problem
253                                         // still exists.
254                                 }
255                         }
256                         var err error
257                         defer func() {
258                                 if err == nil {
259                                         return
260                                 }
261                                 // On failure, check current container
262                                 // state, and don't log the error if
263                                 // the failure came from losing a
264                                 // race.
265                                 var latest arvados.Container
266                                 cq.client.RequestAndDecode(&latest, "GET", "arvados/v1/containers/"+ctr.UUID, nil, map[string][]string{"select": {"state"}})
267                                 if latest.State == arvados.ContainerStateCancelled {
268                                         return
269                                 }
270                                 logger.WithError(err).Warn("error while trying to cancel unsatisfiable container")
271                         }()
272                         err = cq.setRuntimeError(ctr.UUID, errorString)
273                         if err != nil {
274                                 return
275                         }
276                         err = cq.Cancel(ctr.UUID)
277                         if err != nil {
278                                 return
279                         }
280                 }()
281                 return
282         }
283         cq.logger.WithFields(logrus.Fields{
284                 "ContainerUUID": ctr.UUID,
285                 "State":         ctr.State,
286                 "Priority":      ctr.Priority,
287                 "InstanceType":  it.Name,
288         }).Info("adding container to queue")
289         cq.current[uuid] = QueueEnt{Container: ctr, InstanceType: it, FirstSeenAt: time.Now()}
290 }
291
292 // Lock acquires the dispatch lock for the given container.
293 func (cq *Queue) Lock(uuid string) error {
294         return cq.apiUpdate(uuid, "lock")
295 }
296
297 // Unlock releases the dispatch lock for the given container.
298 func (cq *Queue) Unlock(uuid string) error {
299         return cq.apiUpdate(uuid, "unlock")
300 }
301
302 // setRuntimeError sets runtime_status["error"] to the given value.
303 // Container should already have state==Locked or Running.
304 func (cq *Queue) setRuntimeError(uuid, errorString string) error {
305         return cq.client.RequestAndDecode(nil, "PUT", "arvados/v1/containers/"+uuid, nil, map[string]map[string]map[string]interface{}{
306                 "container": {
307                         "runtime_status": {
308                                 "error": errorString,
309                         },
310                 },
311         })
312 }
313
314 // Cancel cancels the given container.
315 func (cq *Queue) Cancel(uuid string) error {
316         var resp arvados.Container
317         err := cq.client.RequestAndDecode(&resp, "PUT", "arvados/v1/containers/"+uuid, nil, map[string]map[string]interface{}{
318                 "container": {"state": arvados.ContainerStateCancelled},
319         })
320         if err != nil {
321                 return err
322         }
323         cq.updateWithResp(uuid, resp)
324         return nil
325 }
326
327 func (cq *Queue) apiUpdate(uuid, action string) error {
328         var resp arvados.Container
329         err := cq.client.RequestAndDecode(&resp, "POST", "arvados/v1/containers/"+uuid+"/"+action, nil, nil)
330         if err != nil {
331                 return err
332         }
333         cq.updateWithResp(uuid, resp)
334         return nil
335 }
336
337 // Update the local queue with the response received from a
338 // state-changing API request (lock/unlock/cancel).
339 func (cq *Queue) updateWithResp(uuid string, resp arvados.Container) {
340         cq.mtx.Lock()
341         defer cq.mtx.Unlock()
342         if cq.dontupdate != nil {
343                 cq.dontupdate[uuid] = struct{}{}
344         }
345         ent, ok := cq.current[uuid]
346         if !ok {
347                 // Container is not in queue (e.g., it was not added
348                 // because there is no suitable instance type, and
349                 // we're just locking/updating it in order to set an
350                 // error message). No need to add it, and we don't
351                 // necessarily have enough information to add it here
352                 // anyway because lock/unlock responses don't include
353                 // runtime_constraints.
354                 return
355         }
356         ent.Container.State, ent.Container.Priority, ent.Container.LockedByUUID = resp.State, resp.Priority, resp.LockedByUUID
357         cq.current[uuid] = ent
358         cq.notify()
359 }
360
361 func (cq *Queue) poll() (map[string]*arvados.Container, error) {
362         cq.mtx.Lock()
363         size := len(cq.current)
364         auth := cq.auth
365         cq.mtx.Unlock()
366
367         if auth == nil {
368                 auth = &arvados.APIClientAuthorization{}
369                 err := cq.client.RequestAndDecode(auth, "GET", "arvados/v1/api_client_authorizations/current", nil, nil)
370                 if err != nil {
371                         return nil, err
372                 }
373                 cq.mtx.Lock()
374                 cq.auth = auth
375                 cq.mtx.Unlock()
376         }
377
378         next := make(map[string]*arvados.Container, size)
379         apply := func(updates []arvados.Container) {
380                 for _, upd := range updates {
381                         if next[upd.UUID] == nil {
382                                 next[upd.UUID] = &arvados.Container{}
383                         }
384                         *next[upd.UUID] = upd
385                 }
386         }
387         selectParam := []string{"uuid", "state", "priority", "runtime_constraints", "container_image", "mounts", "scheduling_parameters", "created_at"}
388         limitParam := 1000
389
390         mine, err := cq.fetchAll(arvados.ResourceListParams{
391                 Select:  selectParam,
392                 Order:   "uuid",
393                 Limit:   &limitParam,
394                 Count:   "none",
395                 Filters: []arvados.Filter{{"locked_by_uuid", "=", auth.UUID}},
396         })
397         if err != nil {
398                 return nil, err
399         }
400         apply(mine)
401
402         avail, err := cq.fetchAll(arvados.ResourceListParams{
403                 Select:  selectParam,
404                 Order:   "uuid",
405                 Limit:   &limitParam,
406                 Count:   "none",
407                 Filters: []arvados.Filter{{"state", "=", arvados.ContainerStateQueued}, {"priority", ">", "0"}},
408         })
409         if err != nil {
410                 return nil, err
411         }
412         apply(avail)
413
414         missing := map[string]bool{}
415         cq.mtx.Lock()
416         for uuid, ent := range cq.current {
417                 if next[uuid] == nil &&
418                         ent.Container.State != arvados.ContainerStateCancelled &&
419                         ent.Container.State != arvados.ContainerStateComplete {
420                         missing[uuid] = true
421                 }
422         }
423         cq.mtx.Unlock()
424
425         for len(missing) > 0 {
426                 var batch []string
427                 for uuid := range missing {
428                         batch = append(batch, uuid)
429                         if len(batch) == 20 {
430                                 break
431                         }
432                 }
433                 filters := []arvados.Filter{{"uuid", "in", batch}}
434                 ended, err := cq.fetchAll(arvados.ResourceListParams{
435                         Select:  selectParam,
436                         Order:   "uuid",
437                         Count:   "none",
438                         Filters: filters,
439                 })
440                 if err != nil {
441                         return nil, err
442                 }
443                 apply(ended)
444                 if len(ended) == 0 {
445                         // This is the only case where we can conclude
446                         // a container has been deleted from the
447                         // database. A short (but non-zero) page, on
448                         // the other hand, can be caused by a response
449                         // size limit.
450                         for _, uuid := range batch {
451                                 cq.logger.WithField("ContainerUUID", uuid).Warn("container not found by controller (deleted?)")
452                                 delete(missing, uuid)
453                                 cq.mtx.Lock()
454                                 cq.delEnt(uuid, cq.current[uuid].Container.State)
455                                 cq.mtx.Unlock()
456                         }
457                         continue
458                 }
459                 for _, ctr := range ended {
460                         if _, ok := missing[ctr.UUID]; !ok {
461                                 msg := "BUG? server response did not match requested filters, erroring out rather than risk deadlock"
462                                 cq.logger.WithFields(logrus.Fields{
463                                         "ContainerUUID": ctr.UUID,
464                                         "Filters":       filters,
465                                 }).Error(msg)
466                                 return nil, errors.New(msg)
467                         }
468                         delete(missing, ctr.UUID)
469                 }
470         }
471         return next, nil
472 }
473
474 func (cq *Queue) fetchAll(initialParams arvados.ResourceListParams) ([]arvados.Container, error) {
475         var results []arvados.Container
476         params := initialParams
477         params.Offset = 0
478         for {
479                 // This list variable must be a new one declared
480                 // inside the loop: otherwise, items in the API
481                 // response would get deep-merged into the items
482                 // loaded in previous iterations.
483                 var list arvados.ContainerList
484
485                 err := cq.client.RequestAndDecode(&list, "GET", "arvados/v1/containers", nil, params)
486                 if err != nil {
487                         return nil, err
488                 }
489                 if len(list.Items) == 0 {
490                         break
491                 }
492
493                 results = append(results, list.Items...)
494                 if len(params.Order) == 1 && params.Order == "uuid" {
495                         params.Filters = append(initialParams.Filters, arvados.Filter{"uuid", ">", list.Items[len(list.Items)-1].UUID})
496                 } else {
497                         params.Offset += len(list.Items)
498                 }
499         }
500         return results, nil
501 }
502
503 func (cq *Queue) runMetrics(reg *prometheus.Registry) {
504         mEntries := prometheus.NewGaugeVec(prometheus.GaugeOpts{
505                 Namespace: "arvados",
506                 Subsystem: "dispatchcloud",
507                 Name:      "queue_entries",
508                 Help:      "Number of active container entries in the controller database.",
509         }, []string{"state", "instance_type"})
510         reg.MustRegister(mEntries)
511
512         type entKey struct {
513                 state arvados.ContainerState
514                 inst  string
515         }
516         count := map[entKey]int{}
517
518         ch := cq.Subscribe()
519         defer cq.Unsubscribe(ch)
520         for range ch {
521                 for k := range count {
522                         count[k] = 0
523                 }
524                 ents, _ := cq.Entries()
525                 for _, ent := range ents {
526                         count[entKey{ent.Container.State, ent.InstanceType.Name}]++
527                 }
528                 for k, v := range count {
529                         mEntries.WithLabelValues(string(k.state), k.inst).Set(float64(v))
530                 }
531         }
532 }