1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
12 "git.arvados.org/arvados.git/lib/dispatchcloud/container"
13 "git.arvados.org/arvados.git/sdk/go/arvados"
14 "github.com/sirupsen/logrus"
17 // Queue is a test stub for container.Queue. The caller specifies the
18 // initial queue state.
20 // Containers represent the API server database contents.
21 Containers []arvados.Container
23 // ChooseType will be called for each entry in Containers. It
25 ChooseType func(*arvados.Container) (arvados.InstanceType, error)
27 Logger logrus.FieldLogger
29 entries map[string]container.QueueEnt
31 subscribers map[<-chan struct{}]chan struct{}
32 stateChanges []QueueStateChange
37 type QueueStateChange struct {
39 From arvados.ContainerState
40 To arvados.ContainerState
43 // All calls to Lock/Unlock/Cancel to date.
44 func (q *Queue) StateChanges() []QueueStateChange {
50 // Entries returns the containers that were queued when Update was
52 func (q *Queue) Entries() (map[string]container.QueueEnt, time.Time) {
56 r := map[string]container.QueueEnt{}
57 for uuid, ent := range q.entries {
63 // Get returns the container from the cached queue, i.e., as it was
64 // when Update was last called -- just like a container.Queue does. If
65 // the state has been changed (via Lock, Unlock, or Cancel) since the
66 // last Update, the updated state is returned.
67 func (q *Queue) Get(uuid string) (arvados.Container, bool) {
70 ent, ok := q.entries[uuid]
71 return ent.Container, ok
74 func (q *Queue) Forget(uuid string) {
77 delete(q.entries, uuid)
80 func (q *Queue) Lock(uuid string) error {
83 return q.changeState(uuid, arvados.ContainerStateQueued, arvados.ContainerStateLocked)
86 func (q *Queue) Unlock(uuid string) error {
89 return q.changeState(uuid, arvados.ContainerStateLocked, arvados.ContainerStateQueued)
92 func (q *Queue) Cancel(uuid string) error {
95 return q.changeState(uuid, q.entries[uuid].Container.State, arvados.ContainerStateCancelled)
98 func (q *Queue) Subscribe() <-chan struct{} {
101 if q.subscribers == nil {
102 q.subscribers = map[<-chan struct{}]chan struct{}{}
104 ch := make(chan struct{}, 1)
105 q.subscribers[ch] = ch
109 func (q *Queue) Unsubscribe(ch <-chan struct{}) {
112 delete(q.subscribers, ch)
115 // caller must have lock.
116 func (q *Queue) notify() {
117 for _, ch := range q.subscribers {
119 case ch <- struct{}{}:
125 // caller must have lock.
126 func (q *Queue) changeState(uuid string, from, to arvados.ContainerState) error {
127 ent := q.entries[uuid]
128 q.stateChanges = append(q.stateChanges, QueueStateChange{uuid, from, to})
129 if ent.Container.State != from {
130 return fmt.Errorf("changeState failed: state=%q", ent.Container.State)
132 ent.Container.State = to
133 q.entries[uuid] = ent
134 for i, ctr := range q.Containers {
135 if ctr.UUID == uuid {
136 q.Containers[i].State = to
144 // Update rebuilds the current entries from the Containers slice.
145 func (q *Queue) Update() error {
148 updTime := time.Now()
149 upd := map[string]container.QueueEnt{}
150 for _, ctr := range q.Containers {
151 _, exists := q.entries[ctr.UUID]
152 if !exists && (ctr.State == arvados.ContainerStateComplete || ctr.State == arvados.ContainerStateCancelled) {
155 if ent, ok := upd[ctr.UUID]; ok {
159 it, _ := q.ChooseType(&ctr)
160 upd[ctr.UUID] = container.QueueEnt{
163 FirstSeenAt: time.Now(),
173 // Notify adds/updates an entry in the Containers slice. This
174 // simulates the effect of an API update from someone other than the
175 // dispatcher -- e.g., crunch-run updating state to "Complete" when a
178 // The resulting changes are not exposed through Get() or Entries()
179 // until the next call to Update().
181 // Return value is true unless the update is rejected (invalid state
183 func (q *Queue) Notify(upd arvados.Container) bool {
186 for i, ctr := range q.Containers {
187 if ctr.UUID == upd.UUID {
188 if allowContainerUpdate[ctr.State][upd.State] {
189 q.Containers[i] = upd
193 q.Logger.WithField("ContainerUUID", ctr.UUID).Infof("test.Queue rejected update from %s to %s", ctr.State, upd.State)
198 q.Containers = append(q.Containers, upd)
202 var allowContainerUpdate = map[arvados.ContainerState]map[arvados.ContainerState]bool{
203 arvados.ContainerStateQueued: {
204 arvados.ContainerStateQueued: true,
205 arvados.ContainerStateLocked: true,
206 arvados.ContainerStateCancelled: true,
208 arvados.ContainerStateLocked: {
209 arvados.ContainerStateQueued: true,
210 arvados.ContainerStateLocked: true,
211 arvados.ContainerStateRunning: true,
212 arvados.ContainerStateCancelled: true,
214 arvados.ContainerStateRunning: {
215 arvados.ContainerStateRunning: true,
216 arvados.ContainerStateCancelled: true,
217 arvados.ContainerStateComplete: true,