1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
5 // Package dispatch is a helper library for building Arvados container
16 "git.curoverse.com/arvados.git/sdk/go/arvados"
17 "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
21 Queued = arvados.ContainerStateQueued
22 Locked = arvados.ContainerStateLocked
23 Running = arvados.ContainerStateRunning
24 Complete = arvados.ContainerStateComplete
25 Cancelled = arvados.ContainerStateCancelled
29 type Dispatcher struct {
30 Arv *arvadosclient.ArvadosClient
32 // Batch size for container queries
35 // Queue polling frequency
36 PollPeriod time.Duration
38 // Time to wait between successive attempts to run the same container
39 MinRetryPeriod time.Duration
41 // Func that implements the container lifecycle. Must be set
42 // to a non-nil DispatchFunc before calling Run().
43 RunContainer DispatchFunc
45 auth arvados.APIClientAuthorization
47 trackers map[string]*runTracker
51 // A DispatchFunc executes a container (if the container record is
52 // Locked) or resume monitoring an already-running container, and wait
53 // until that container exits.
55 // While the container runs, the DispatchFunc should listen for
56 // updated container records on the provided channel. When the channel
57 // closes, the DispatchFunc should stop the container if it's still
58 // running, and return.
60 // The DispatchFunc should not return until the container is finished.
61 type DispatchFunc func(*Dispatcher, arvados.Container, <-chan arvados.Container)
63 // Run watches the API server's queue for containers that are either
64 // ready to run and available to lock, or are already locked by this
65 // dispatcher's token. When a new one appears, Run calls RunContainer
66 // in a new goroutine.
67 func (d *Dispatcher) Run(ctx context.Context) error {
68 err := d.Arv.Call("GET", "api_client_authorizations", "", "current", nil, &d.auth)
70 return fmt.Errorf("error getting my token UUID: %v", err)
73 d.throttle.hold = d.MinRetryPeriod
75 poll := time.NewTicker(d.PollPeriod)
90 todo := make(map[string]*runTracker)
92 // Make a copy of trackers
93 for uuid, tracker := range d.trackers {
98 // Containers I currently own (Locked/Running)
99 querySuccess := d.checkForUpdates([][]interface{}{
100 {"locked_by_uuid", "=", d.auth.UUID}}, todo)
102 // Containers I should try to dispatch
103 querySuccess = d.checkForUpdates([][]interface{}{
104 {"state", "=", Queued},
105 {"priority", ">", "0"}}, todo) && querySuccess
108 // There was an error in one of the previous queries,
109 // we probably didn't get updates for all the
110 // containers we should have. Don't check them
111 // individually because it may be expensive.
115 // Containers I know about but didn't fall into the
116 // above two categories (probably Complete/Cancelled)
118 for uuid := range todo {
119 missed = append(missed, uuid)
122 for len(missed) > 0 {
124 if len(missed) > 20 {
131 querySuccess = d.checkForUpdates([][]interface{}{
132 {"uuid", "in", batch}}, todo) && querySuccess
136 // There was an error in one of the previous queries, we probably
137 // didn't see all the containers we should have, so don't shut down
138 // the missed containers.
142 // Containers that I know about that didn't show up in any
143 // query should be let go.
144 for uuid, tracker := range todo {
145 log.Printf("Container %q not returned by any query, stopping tracking.", uuid)
152 // Start a runner in a new goroutine, and send the initial container
153 // record to its updates channel.
154 func (d *Dispatcher) start(c arvados.Container) *runTracker {
155 tracker := &runTracker{updates: make(chan arvados.Container, 1)}
158 d.RunContainer(d, c, tracker.updates)
159 // RunContainer blocks for the lifetime of the container. When
160 // it returns, the tracker should delete itself.
162 delete(d.trackers, c.UUID)
168 func (d *Dispatcher) checkForUpdates(filters [][]interface{}, todo map[string]*runTracker) bool {
169 var countList arvados.ContainerList
170 params := arvadosclient.Dict{
174 "order": []string{"priority desc"}}
175 err := d.Arv.List("containers", params, &countList)
177 log.Printf("Error getting count of containers: %q", err)
180 itemsAvailable := countList.ItemsAvailable
181 params = arvadosclient.Dict{
184 "limit": d.BatchSize,
185 "order": []string{"priority desc"}}
188 params["offset"] = offset
189 var list arvados.ContainerList
190 err := d.Arv.List("containers", params, &list)
192 log.Printf("Error getting list of containers: %q", err)
195 d.checkListForUpdates(list.Items, todo)
196 offset += len(list.Items)
197 if len(list.Items) == 0 || itemsAvailable <= offset {
203 func (d *Dispatcher) checkListForUpdates(containers []arvados.Container, todo map[string]*runTracker) {
206 if d.trackers == nil {
207 d.trackers = make(map[string]*runTracker)
210 for _, c := range containers {
211 tracker, alreadyTracking := d.trackers[c.UUID]
214 if c.LockedByUUID != "" && c.LockedByUUID != d.auth.UUID {
215 log.Printf("debug: ignoring %s locked by %s", c.UUID, c.LockedByUUID)
216 } else if alreadyTracking {
220 case Locked, Running:
222 case Cancelled, Complete:
228 if !d.throttle.Check(c.UUID) {
231 err := d.lock(c.UUID)
233 log.Printf("debug: error locking container %s: %s", c.UUID, err)
237 d.trackers[c.UUID] = d.start(c)
238 case Locked, Running:
239 if !d.throttle.Check(c.UUID) {
242 d.trackers[c.UUID] = d.start(c)
243 case Cancelled, Complete:
244 // no-op (we already stopped monitoring)
250 // UpdateState makes an API call to change the state of a container.
251 func (d *Dispatcher) UpdateState(uuid string, state arvados.ContainerState) error {
252 err := d.Arv.Update("containers", uuid,
254 "container": arvadosclient.Dict{"state": state},
257 log.Printf("Error updating container %s to state %q: %s", uuid, state, err)
262 // Lock makes the lock API call which updates the state of a container to Locked.
263 func (d *Dispatcher) lock(uuid string) error {
264 return d.Arv.Call("POST", "containers", uuid, "lock", nil, nil)
267 // Unlock makes the unlock API call which updates the state of a container to Queued.
268 func (d *Dispatcher) Unlock(uuid string) error {
269 return d.Arv.Call("POST", "containers", uuid, "unlock", nil, nil)
272 // TrackContainer ensures a tracker is running for the given UUID,
273 // regardless of the current state of the container (except: if the
274 // container is locked by a different dispatcher, a tracker will not
275 // be started). If the container is not in Locked or Running state,
276 // the new tracker will close down immediately.
278 // This allows the dispatcher to put its own RunContainer func into a
279 // cleanup phase (for example, to kill local processes created by a
280 // prevous dispatch process that are still running even though the
281 // container state is final) without the risk of having multiple
282 // goroutines monitoring the same UUID.
283 func (d *Dispatcher) TrackContainer(uuid string) error {
284 var cntr arvados.Container
285 err := d.Arv.Call("GET", "containers", uuid, "", nil, &cntr)
289 if cntr.LockedByUUID != "" && cntr.LockedByUUID != d.auth.UUID {
295 if _, alreadyTracking := d.trackers[uuid]; alreadyTracking {
298 if d.trackers == nil {
299 d.trackers = make(map[string]*runTracker)
301 d.trackers[uuid] = d.start(cntr)
303 case Queued, Cancelled, Complete:
304 d.trackers[uuid].close()
309 type runTracker struct {
311 updates chan arvados.Container
314 func (tracker *runTracker) close() {
315 if !tracker.closing {
316 close(tracker.updates)
318 tracker.closing = true
321 func (tracker *runTracker) update(c arvados.Container) {
326 case <-tracker.updates:
327 log.Printf("debug: runner is handling updates slowly, discarded previous update for %s", c.UUID)