1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
10 "git.curoverse.com/arvados.git/lib/dispatchcloud/container"
11 "git.curoverse.com/arvados.git/sdk/go/arvados"
12 "github.com/sirupsen/logrus"
15 func (sch *Scheduler) runQueue() {
16 unsorted, _ := sch.queue.Entries()
17 sorted := make([]container.QueueEnt, 0, len(unsorted))
18 for _, ent := range unsorted {
19 sorted = append(sorted, ent)
21 sort.Slice(sorted, func(i, j int) bool {
22 return sorted[i].Container.Priority > sorted[j].Container.Priority
25 running := sch.pool.Running()
26 unalloc := sch.pool.Unallocated()
28 sch.logger.WithFields(logrus.Fields{
29 "Containers": len(sorted),
30 "Processes": len(running),
33 dontstart := map[arvados.InstanceType]bool{}
34 var overquota []container.QueueEnt // entries that are unmappable because of worker pool quota
37 for i, ctr := range sorted {
38 ctr, it := ctr.Container, ctr.InstanceType
39 logger := sch.logger.WithFields(logrus.Fields{
40 "ContainerUUID": ctr.UUID,
41 "InstanceType": it.Name,
43 if _, running := running[ctr.UUID]; running || ctr.Priority < 1 {
47 case arvados.ContainerStateQueued:
48 if unalloc[it] < 1 && sch.pool.AtQuota() {
49 logger.Debug("not locking: AtQuota and no unalloc workers")
50 overquota = sorted[i:]
53 sch.bgLock(logger, ctr.UUID)
55 case arvados.ContainerStateLocked:
58 } else if sch.pool.AtQuota() {
59 logger.Debug("not starting: AtQuota and no unalloc workers")
60 overquota = sorted[i:]
63 logger.Info("creating new instance")
64 if !sch.pool.Create(it) {
65 // (Note pool.Create works
66 // asynchronously and logs its
67 // own failures, so we don't
68 // need to log this as a
71 sch.queue.Unlock(ctr.UUID)
72 // Don't let lower-priority
73 // containers starve this one
74 // by using keeping idle
75 // workers alive on different
76 // instance types. TODO:
77 // avoid getting starved here
78 // if instances of a specific
80 overquota = sorted[i:]
86 // We already tried & failed to start
87 // a higher-priority container on the
88 // same instance type. Don't let this
89 // one sneak in ahead of it.
90 } else if sch.pool.StartContainer(it, ctr) {
98 if len(overquota) > 0 {
99 // Unlock any containers that are unmappable while
101 for _, ctr := range overquota {
103 if ctr.State == arvados.ContainerStateLocked {
104 logger := sch.logger.WithField("ContainerUUID", ctr.UUID)
105 logger.Debug("unlock because pool capacity is used by higher priority containers")
106 err := sch.queue.Unlock(ctr.UUID)
108 logger.WithError(err).Warn("error unlocking")
112 // Shut down idle workers that didn't get any
113 // containers mapped onto them before we hit quota.
114 for it, n := range unalloc {
118 sch.pool.Shutdown(it)
123 // Start an API call to lock the given container, and return
124 // immediately while waiting for the response in a new goroutine. Do
125 // nothing if a lock request is already in progress for this
127 func (sch *Scheduler) bgLock(logger logrus.FieldLogger, uuid string) {
128 logger.Debug("locking")
130 defer sch.mtx.Unlock()
131 if sch.locking[uuid] {
132 logger.Debug("locking in progress, doing nothing")
135 if ctr, ok := sch.queue.Get(uuid); !ok || ctr.State != arvados.ContainerStateQueued {
136 // This happens if the container has been cancelled or
137 // locked since runQueue called sch.queue.Entries(),
138 // possibly by a bgLock() call from a previous
139 // runQueue iteration. In any case, we will respond
140 // appropriately on the next runQueue iteration, which
141 // will have already been triggered by the queue
143 logger.WithField("State", ctr.State).Debug("container no longer queued by the time we decided to lock it, doing nothing")
146 sch.locking[uuid] = true
150 defer sch.mtx.Unlock()
151 delete(sch.locking, uuid)
153 err := sch.queue.Lock(uuid)
155 logger.WithError(err).Warn("error locking container")
158 logger.Debug("lock succeeded")
159 ctr, ok := sch.queue.Get(uuid)
161 logger.Error("(BUG?) container disappeared from queue after Lock succeeded")
162 } else if ctr.State != arvados.ContainerStateLocked {
163 logger.Warnf("(race?) container has state=%q after Lock succeeded", ctr.State)