ac394e114962ddf05d2e71e94cc4bb1ff46c4780
[arvados.git] / services / crunch-dispatch-slurm / crunch-dispatch-slurm.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 // Dispatcher service for Crunch that submits containers to the slurm queue.
6 package dispatchslurm
7
8 import (
9         "context"
10         "crypto/hmac"
11         "crypto/sha256"
12         "fmt"
13         "log"
14         "math"
15         "net/http"
16         "os"
17         "regexp"
18         "strings"
19         "time"
20
21         "git.arvados.org/arvados.git/lib/cmd"
22         "git.arvados.org/arvados.git/lib/dispatchcloud"
23         "git.arvados.org/arvados.git/lib/service"
24         "git.arvados.org/arvados.git/sdk/go/arvados"
25         "git.arvados.org/arvados.git/sdk/go/arvadosclient"
26         "git.arvados.org/arvados.git/sdk/go/ctxlog"
27         "git.arvados.org/arvados.git/sdk/go/dispatch"
28         "github.com/coreos/go-systemd/daemon"
29         "github.com/prometheus/client_golang/prometheus"
30         "github.com/sirupsen/logrus"
31 )
32
33 var Command cmd.Handler = service.Command(arvados.ServiceNameDispatchSLURM, newHandler)
34
35 func newHandler(ctx context.Context, cluster *arvados.Cluster, _ string, _ *prometheus.Registry) service.Handler {
36         logger := ctxlog.FromContext(ctx)
37         disp := &Dispatcher{logger: logger, cluster: cluster}
38         if err := disp.configure(); err != nil {
39                 return service.ErrorHandler(ctx, cluster, err)
40         }
41         disp.setup()
42         go func() {
43                 disp.err = disp.run()
44                 close(disp.done)
45         }()
46         return disp
47 }
48
49 type logger interface {
50         dispatch.Logger
51         Fatalf(string, ...interface{})
52 }
53
54 const initialNiceValue int64 = 10000
55
56 type Dispatcher struct {
57         *dispatch.Dispatcher
58         logger  logrus.FieldLogger
59         cluster *arvados.Cluster
60         sqCheck *SqueueChecker
61         slurm   Slurm
62
63         done chan struct{}
64         err  error
65
66         Client arvados.Client
67 }
68
69 func (disp *Dispatcher) CheckHealth() error {
70         return disp.err
71 }
72
73 func (disp *Dispatcher) Done() <-chan struct{} {
74         return disp.done
75 }
76
77 func (disp *Dispatcher) ServeHTTP(w http.ResponseWriter, r *http.Request) {
78         http.NotFound(w, r)
79 }
80
81 // configure() loads config files. Some tests skip this (see
82 // StubbedSuite).
83 func (disp *Dispatcher) configure() error {
84         if disp.logger == nil {
85                 disp.logger = logrus.StandardLogger()
86         }
87         disp.logger = disp.logger.WithField("ClusterID", disp.cluster.ClusterID)
88         disp.logger.Printf("crunch-dispatch-slurm %s started", cmd.Version.String())
89
90         disp.Client.APIHost = disp.cluster.Services.Controller.ExternalURL.Host
91         disp.Client.AuthToken = disp.cluster.SystemRootToken
92         disp.Client.Insecure = disp.cluster.TLS.Insecure
93
94         if disp.Client.APIHost != "" || disp.Client.AuthToken != "" {
95                 // Copy real configs into env vars so [a]
96                 // MakeArvadosClient() uses them, and [b] they get
97                 // propagated to crunch-run via SLURM.
98                 os.Setenv("ARVADOS_API_HOST", disp.Client.APIHost)
99                 os.Setenv("ARVADOS_API_TOKEN", disp.Client.AuthToken)
100                 os.Setenv("ARVADOS_API_HOST_INSECURE", "")
101                 if disp.Client.Insecure {
102                         os.Setenv("ARVADOS_API_HOST_INSECURE", "1")
103                 }
104                 for k, v := range disp.cluster.Containers.SLURM.SbatchEnvironmentVariables {
105                         os.Setenv(k, v)
106                 }
107         } else {
108                 disp.logger.Warnf("Client credentials missing from config, so falling back on environment variables (deprecated).")
109         }
110         return nil
111 }
112
113 // setup() initializes private fields after configure().
114 func (disp *Dispatcher) setup() {
115         disp.done = make(chan struct{})
116         arv, err := arvadosclient.MakeArvadosClient()
117         if err != nil {
118                 disp.logger.Fatalf("Error making Arvados client: %v", err)
119         }
120         arv.Retries = 25
121
122         disp.slurm = NewSlurmCLI()
123         disp.sqCheck = &SqueueChecker{
124                 Logger:         disp.logger,
125                 Period:         time.Duration(disp.cluster.Containers.CloudVMs.PollInterval),
126                 PrioritySpread: disp.cluster.Containers.SLURM.PrioritySpread,
127                 Slurm:          disp.slurm,
128         }
129         disp.Dispatcher = &dispatch.Dispatcher{
130                 Arv:            arv,
131                 Logger:         disp.logger,
132                 BatchSize:      disp.cluster.API.MaxItemsPerResponse,
133                 RunContainer:   disp.runContainer,
134                 PollPeriod:     time.Duration(disp.cluster.Containers.CloudVMs.PollInterval),
135                 MinRetryPeriod: time.Duration(disp.cluster.Containers.MinRetryPeriod),
136         }
137 }
138
139 func (disp *Dispatcher) run() error {
140         defer disp.sqCheck.Stop()
141
142         if disp.cluster != nil && len(disp.cluster.InstanceTypes) > 0 {
143                 go SlurmNodeTypeFeatureKludge(disp.cluster)
144         }
145
146         if _, err := daemon.SdNotify(false, "READY=1"); err != nil {
147                 log.Printf("Error notifying init daemon: %v", err)
148         }
149         go disp.checkSqueueForOrphans()
150         return disp.Dispatcher.Run(context.Background())
151 }
152
153 var containerUuidPattern = regexp.MustCompile(`^[a-z0-9]{5}-dz642-[a-z0-9]{15}$`)
154
155 // Check the next squeue report, and invoke TrackContainer for all the
156 // containers in the report. This gives us a chance to cancel slurm
157 // jobs started by a previous dispatch process that never released
158 // their slurm allocations even though their container states are
159 // Cancelled or Complete. See https://dev.arvados.org/issues/10979
160 func (disp *Dispatcher) checkSqueueForOrphans() {
161         for _, uuid := range disp.sqCheck.All() {
162                 if !containerUuidPattern.MatchString(uuid) || !strings.HasPrefix(uuid, disp.cluster.ClusterID) {
163                         continue
164                 }
165                 err := disp.TrackContainer(uuid)
166                 if err != nil {
167                         log.Printf("checkSqueueForOrphans: TrackContainer(%s): %s", uuid, err)
168                 }
169         }
170 }
171
172 func (disp *Dispatcher) slurmConstraintArgs(container arvados.Container) []string {
173         mem := int64(math.Ceil(float64(container.RuntimeConstraints.RAM+
174                 container.RuntimeConstraints.KeepCacheRAM+
175                 int64(disp.cluster.Containers.ReserveExtraRAM)) / float64(1048576)))
176
177         disk := dispatchcloud.EstimateScratchSpace(&container)
178         disk = int64(math.Ceil(float64(disk) / float64(1048576)))
179         return []string{
180                 fmt.Sprintf("--mem=%d", mem),
181                 fmt.Sprintf("--cpus-per-task=%d", container.RuntimeConstraints.VCPUs),
182                 fmt.Sprintf("--tmp=%d", disk),
183         }
184 }
185
186 func (disp *Dispatcher) sbatchArgs(container arvados.Container) ([]string, error) {
187         var args []string
188         args = append(args, disp.cluster.Containers.SLURM.SbatchArgumentsList...)
189         args = append(args, "--job-name="+container.UUID, fmt.Sprintf("--nice=%d", initialNiceValue), "--no-requeue")
190
191         if disp.cluster == nil {
192                 // no instance types configured
193                 args = append(args, disp.slurmConstraintArgs(container)...)
194         } else if it, err := dispatchcloud.ChooseInstanceType(disp.cluster, &container); err == dispatchcloud.ErrInstanceTypesNotConfigured {
195                 // ditto
196                 args = append(args, disp.slurmConstraintArgs(container)...)
197         } else if err != nil {
198                 return nil, err
199         } else {
200                 // use instancetype constraint instead of slurm mem/cpu/tmp specs
201                 args = append(args, "--constraint=instancetype="+it.Name)
202         }
203
204         if len(container.SchedulingParameters.Partitions) > 0 {
205                 args = append(args, "--partition="+strings.Join(container.SchedulingParameters.Partitions, ","))
206         }
207
208         return args, nil
209 }
210
211 func (disp *Dispatcher) submit(container arvados.Container, crunchRunCommand []string) error {
212         // append() here avoids modifying crunchRunCommand's
213         // underlying array, which is shared with other goroutines.
214         crArgs := append([]string(nil), crunchRunCommand...)
215         crArgs = append(crArgs, "--runtime-engine="+disp.cluster.Containers.RuntimeEngine)
216         crArgs = append(crArgs, container.UUID)
217
218         h := hmac.New(sha256.New, []byte(disp.cluster.SystemRootToken))
219         fmt.Fprint(h, container.UUID)
220         authsecret := fmt.Sprintf("%x", h.Sum(nil))
221
222         crScript := strings.NewReader(execScript(crArgs, map[string]string{"GatewayAuthSecret": authsecret}))
223
224         sbArgs, err := disp.sbatchArgs(container)
225         if err != nil {
226                 return err
227         }
228         log.Printf("running sbatch %+q", sbArgs)
229         return disp.slurm.Batch(crScript, sbArgs)
230 }
231
232 // Submit a container to the slurm queue (or resume monitoring if it's
233 // already in the queue).  Cancel the slurm job if the container's
234 // priority changes to zero or its state indicates it's no longer
235 // running.
236 func (disp *Dispatcher) runContainer(_ *dispatch.Dispatcher, ctr arvados.Container, status <-chan arvados.Container) error {
237         ctx, cancel := context.WithCancel(context.Background())
238         defer cancel()
239
240         if ctr.State == dispatch.Locked && !disp.sqCheck.HasUUID(ctr.UUID) {
241                 log.Printf("Submitting container %s to slurm", ctr.UUID)
242                 cmd := []string{disp.cluster.Containers.CrunchRunCommand}
243                 cmd = append(cmd, disp.cluster.Containers.CrunchRunArgumentsList...)
244                 err := disp.submit(ctr, cmd)
245                 if err != nil {
246                         return err
247                 }
248         }
249
250         log.Printf("Start monitoring container %v in state %q", ctr.UUID, ctr.State)
251         defer log.Printf("Done monitoring container %s", ctr.UUID)
252
253         // If the container disappears from the slurm queue, there is
254         // no point in waiting for further dispatch updates: just
255         // clean up and return.
256         go func(uuid string) {
257                 for ctx.Err() == nil && disp.sqCheck.HasUUID(uuid) {
258                 }
259                 cancel()
260         }(ctr.UUID)
261
262         for {
263                 select {
264                 case <-ctx.Done():
265                         // Disappeared from squeue
266                         if err := disp.Arv.Get("containers", ctr.UUID, nil, &ctr); err != nil {
267                                 log.Printf("error getting final container state for %s: %s", ctr.UUID, err)
268                         }
269                         switch ctr.State {
270                         case dispatch.Running:
271                                 disp.UpdateState(ctr.UUID, dispatch.Cancelled)
272                         case dispatch.Locked:
273                                 disp.Unlock(ctr.UUID)
274                         }
275                         return nil
276                 case updated, ok := <-status:
277                         if !ok {
278                                 log.Printf("container %s is done: cancel slurm job", ctr.UUID)
279                                 disp.scancel(ctr)
280                         } else if updated.Priority == 0 {
281                                 log.Printf("container %s has state %q, priority %d: cancel slurm job", ctr.UUID, updated.State, updated.Priority)
282                                 disp.scancel(ctr)
283                         } else {
284                                 p := int64(updated.Priority)
285                                 if p <= 1000 {
286                                         // API is providing
287                                         // user-assigned priority. If
288                                         // ctrs have equal priority,
289                                         // run the older one first.
290                                         p = int64(p)<<50 - (updated.CreatedAt.UnixNano() >> 14)
291                                 }
292                                 disp.sqCheck.SetPriority(ctr.UUID, p)
293                         }
294                 }
295         }
296 }
297 func (disp *Dispatcher) scancel(ctr arvados.Container) {
298         err := disp.slurm.Cancel(ctr.UUID)
299         if err != nil {
300                 log.Printf("scancel: %s", err)
301                 time.Sleep(time.Second)
302         } else if disp.sqCheck.HasUUID(ctr.UUID) {
303                 log.Printf("container %s is still in squeue after scancel", ctr.UUID)
304                 time.Sleep(time.Second)
305         }
306 }