Merge branch '6347-log-timestamps'
[arvados.git] / sdk / go / dispatch / dispatch.go
1 // Package dispatch is a helper library for building Arvados container
2 // dispatchers.
3 package dispatch
4
5 import (
6         "context"
7         "fmt"
8         "log"
9         "sync"
10         "time"
11
12         "git.curoverse.com/arvados.git/sdk/go/arvados"
13         "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
14 )
15
16 const (
17         Queued    = arvados.ContainerStateQueued
18         Locked    = arvados.ContainerStateLocked
19         Running   = arvados.ContainerStateRunning
20         Complete  = arvados.ContainerStateComplete
21         Cancelled = arvados.ContainerStateCancelled
22 )
23
24 // Dispatcher struct
25 type Dispatcher struct {
26         Arv *arvadosclient.ArvadosClient
27
28         // Queue polling frequency
29         PollPeriod time.Duration
30
31         // Time to wait between successive attempts to run the same container
32         MinRetryPeriod time.Duration
33
34         // Func that implements the container lifecycle. Must be set
35         // to a non-nil DispatchFunc before calling Run().
36         RunContainer DispatchFunc
37
38         auth     arvados.APIClientAuthorization
39         mtx      sync.Mutex
40         trackers map[string]*runTracker
41         throttle throttle
42 }
43
44 // A DispatchFunc executes a container (if the container record is
45 // Locked) or resume monitoring an already-running container, and wait
46 // until that container exits.
47 //
48 // While the container runs, the DispatchFunc should listen for
49 // updated container records on the provided channel. When the channel
50 // closes, the DispatchFunc should stop the container if it's still
51 // running, and return.
52 //
53 // The DispatchFunc should not return until the container is finished.
54 type DispatchFunc func(*Dispatcher, arvados.Container, <-chan arvados.Container)
55
56 // Run watches the API server's queue for containers that are either
57 // ready to run and available to lock, or are already locked by this
58 // dispatcher's token. When a new one appears, Run calls RunContainer
59 // in a new goroutine.
60 func (d *Dispatcher) Run(ctx context.Context) error {
61         err := d.Arv.Call("GET", "api_client_authorizations", "", "current", nil, &d.auth)
62         if err != nil {
63                 return fmt.Errorf("error getting my token UUID: %v", err)
64         }
65
66         d.throttle.hold = d.MinRetryPeriod
67
68         poll := time.NewTicker(d.PollPeriod)
69         defer poll.Stop()
70
71         for {
72                 tracked := d.trackedUUIDs()
73                 d.checkForUpdates([][]interface{}{
74                         {"uuid", "in", tracked}})
75                 d.checkForUpdates([][]interface{}{
76                         {"locked_by_uuid", "=", d.auth.UUID},
77                         {"uuid", "not in", tracked}})
78                 d.checkForUpdates([][]interface{}{
79                         {"state", "=", Queued},
80                         {"priority", ">", "0"},
81                         {"uuid", "not in", tracked}})
82                 select {
83                 case <-poll.C:
84                         continue
85                 case <-ctx.Done():
86                         return ctx.Err()
87                 }
88         }
89 }
90
91 func (d *Dispatcher) trackedUUIDs() []string {
92         d.mtx.Lock()
93         defer d.mtx.Unlock()
94         if len(d.trackers) == 0 {
95                 // API bug: ["uuid", "not in", []] does not work as
96                 // expected, but this does:
97                 return []string{"this-uuid-does-not-exist"}
98         }
99         uuids := make([]string, 0, len(d.trackers))
100         for x := range d.trackers {
101                 uuids = append(uuids, x)
102         }
103         return uuids
104 }
105
106 // Start a runner in a new goroutine, and send the initial container
107 // record to its updates channel.
108 func (d *Dispatcher) start(c arvados.Container) *runTracker {
109         tracker := &runTracker{updates: make(chan arvados.Container, 1)}
110         tracker.updates <- c
111         go func() {
112                 d.RunContainer(d, c, tracker.updates)
113
114                 d.mtx.Lock()
115                 delete(d.trackers, c.UUID)
116                 d.mtx.Unlock()
117         }()
118         return tracker
119 }
120
121 func (d *Dispatcher) checkForUpdates(filters [][]interface{}) {
122         params := arvadosclient.Dict{
123                 "filters": filters,
124                 "order":   []string{"priority desc"}}
125
126         var list arvados.ContainerList
127         for offset, more := 0, true; more; offset += len(list.Items) {
128                 params["offset"] = offset
129                 err := d.Arv.List("containers", params, &list)
130                 if err != nil {
131                         log.Printf("Error getting list of containers: %q", err)
132                         return
133                 }
134                 more = len(list.Items) > 0 && list.ItemsAvailable > len(list.Items)+offset
135                 d.checkListForUpdates(list.Items)
136         }
137 }
138
139 func (d *Dispatcher) checkListForUpdates(containers []arvados.Container) {
140         d.mtx.Lock()
141         defer d.mtx.Unlock()
142         if d.trackers == nil {
143                 d.trackers = make(map[string]*runTracker)
144         }
145
146         for _, c := range containers {
147                 tracker, alreadyTracking := d.trackers[c.UUID]
148                 if c.LockedByUUID != "" && c.LockedByUUID != d.auth.UUID {
149                         log.Printf("debug: ignoring %s locked by %s", c.UUID, c.LockedByUUID)
150                 } else if alreadyTracking {
151                         switch c.State {
152                         case Queued:
153                                 tracker.close()
154                         case Locked, Running:
155                                 tracker.update(c)
156                         case Cancelled, Complete:
157                                 tracker.close()
158                         }
159                 } else {
160                         switch c.State {
161                         case Queued:
162                                 if !d.throttle.Check(c.UUID) {
163                                         break
164                                 }
165                                 err := d.lock(c.UUID)
166                                 if err != nil {
167                                         log.Printf("debug: error locking container %s: %s", c.UUID, err)
168                                         break
169                                 }
170                                 c.State = Locked
171                                 d.trackers[c.UUID] = d.start(c)
172                         case Locked, Running:
173                                 if !d.throttle.Check(c.UUID) {
174                                         break
175                                 }
176                                 d.trackers[c.UUID] = d.start(c)
177                         case Cancelled, Complete:
178                                 // no-op (we already stopped monitoring)
179                         }
180                 }
181         }
182 }
183
184 // UpdateState makes an API call to change the state of a container.
185 func (d *Dispatcher) UpdateState(uuid string, state arvados.ContainerState) error {
186         err := d.Arv.Update("containers", uuid,
187                 arvadosclient.Dict{
188                         "container": arvadosclient.Dict{"state": state},
189                 }, nil)
190         if err != nil {
191                 log.Printf("Error updating container %s to state %q: %s", uuid, state, err)
192         }
193         return err
194 }
195
196 // Lock makes the lock API call which updates the state of a container to Locked.
197 func (d *Dispatcher) lock(uuid string) error {
198         return d.Arv.Call("POST", "containers", uuid, "lock", nil, nil)
199 }
200
201 // Unlock makes the unlock API call which updates the state of a container to Queued.
202 func (d *Dispatcher) Unlock(uuid string) error {
203         return d.Arv.Call("POST", "containers", uuid, "unlock", nil, nil)
204 }
205
206 // TrackContainer starts a tracker for given uuid if one is not already existing, despite its state.
207 func (d *Dispatcher) TrackContainer(uuid string) {
208         d.mtx.Lock()
209         defer d.mtx.Unlock()
210
211         if d.trackers == nil {
212                 d.trackers = make(map[string]*runTracker)
213         }
214
215         _, alreadyTracking := d.trackers[uuid]
216         if alreadyTracking {
217                 return
218         }
219
220         var cntr arvados.Container
221         err := d.Arv.Call("GET", "containers", uuid, "", nil, &cntr)
222         if err != nil {
223                 log.Printf("Error getting container %s: %s", uuid, err)
224                 return
225         }
226
227         d.trackers[uuid] = d.start(cntr)
228 }
229
230 type runTracker struct {
231         closing bool
232         updates chan arvados.Container
233 }
234
235 func (tracker *runTracker) close() {
236         if !tracker.closing {
237                 close(tracker.updates)
238         }
239         tracker.closing = true
240 }
241
242 func (tracker *runTracker) update(c arvados.Container) {
243         if tracker.closing {
244                 return
245         }
246         select {
247         case <-tracker.updates:
248                 log.Printf("debug: runner is handling updates slowly, discarded previous update for %s", c.UUID)
249         default:
250         }
251         tracker.updates <- c
252 }