1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
7 // Dispatcher service for Crunch that submits containers to the slurm queue.
22 "git.curoverse.com/arvados.git/sdk/go/arvados"
23 "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
24 "git.curoverse.com/arvados.git/sdk/go/config"
25 "git.curoverse.com/arvados.git/sdk/go/dispatch"
26 "github.com/coreos/go-systemd/daemon"
29 // Config used by crunch-dispatch-slurm
33 SbatchArguments []string
34 PollPeriod arvados.Duration
36 // crunch-run command to invoke. The container UUID will be
37 // appended. If nil, []string{"crunch-run"} will be used.
39 // Example: []string{"crunch-run", "--cgroup-parent-subsystem=memory"}
40 CrunchRunCommand []string
42 // Minimum time between two attempts to run the same container
43 MinRetryPeriod arvados.Duration
55 sqCheck = &SqueueChecker{}
58 const defaultConfigPath = "/etc/arvados/crunch-dispatch-slurm/crunch-dispatch-slurm.yml"
61 flags := flag.NewFlagSet("crunch-dispatch-slurm", flag.ExitOnError)
62 flags.Usage = func() { usage(flags) }
64 configPath := flags.String(
67 "`path` to JSON or YAML configuration file")
68 dumpConfig := flag.Bool(
71 "write current configuration to stdout and exit")
73 // Parse args; omit the first arg which is the command name
74 flags.Parse(os.Args[1:])
76 err := readConfig(&theConfig, *configPath)
81 if theConfig.CrunchRunCommand == nil {
82 theConfig.CrunchRunCommand = []string{"crunch-run"}
85 if theConfig.PollPeriod == 0 {
86 theConfig.PollPeriod = arvados.Duration(10 * time.Second)
89 if theConfig.Client.APIHost != "" || theConfig.Client.AuthToken != "" {
90 // Copy real configs into env vars so [a]
91 // MakeArvadosClient() uses them, and [b] they get
92 // propagated to crunch-run via SLURM.
93 os.Setenv("ARVADOS_API_HOST", theConfig.Client.APIHost)
94 os.Setenv("ARVADOS_API_TOKEN", theConfig.Client.AuthToken)
95 os.Setenv("ARVADOS_API_HOST_INSECURE", "")
96 if theConfig.Client.Insecure {
97 os.Setenv("ARVADOS_API_HOST_INSECURE", "1")
99 os.Setenv("ARVADOS_KEEP_SERVICES", strings.Join(theConfig.Client.KeepServiceURIs, " "))
100 os.Setenv("ARVADOS_EXTERNAL_CLIENT", "")
102 log.Printf("warning: Client credentials missing from config, so falling back on environment variables (deprecated).")
106 log.Fatal(config.DumpAndExit(theConfig))
109 arv, err := arvadosclient.MakeArvadosClient()
111 log.Printf("Error making Arvados client: %v", err)
116 sqCheck = &SqueueChecker{Period: time.Duration(theConfig.PollPeriod)}
119 dispatcher := &dispatch.Dispatcher{
122 PollPeriod: time.Duration(theConfig.PollPeriod),
123 MinRetryPeriod: time.Duration(theConfig.MinRetryPeriod),
126 if _, err := daemon.SdNotify(false, "READY=1"); err != nil {
127 log.Printf("Error notifying init daemon: %v", err)
130 go checkSqueueForOrphans(dispatcher, sqCheck)
132 return dispatcher.Run(context.Background())
135 var containerUuidPattern = regexp.MustCompile(`^[a-z0-9]{5}-dz642-[a-z0-9]{15}$`)
137 // Check the next squeue report, and invoke TrackContainer for all the
138 // containers in the report. This gives us a chance to cancel slurm
139 // jobs started by a previous dispatch process that never released
140 // their slurm allocations even though their container states are
141 // Cancelled or Complete. See https://dev.arvados.org/issues/10979
142 func checkSqueueForOrphans(dispatcher *dispatch.Dispatcher, sqCheck *SqueueChecker) {
143 for _, uuid := range sqCheck.All() {
144 if !containerUuidPattern.MatchString(uuid) {
147 err := dispatcher.TrackContainer(uuid)
149 log.Printf("checkSqueueForOrphans: TrackContainer(%s): %s", uuid, err)
154 func niceness(priority int) int {
161 return (1000 - priority) * 1000
165 func sbatchFunc(container arvados.Container) *exec.Cmd {
166 mem := int64(math.Ceil(float64(container.RuntimeConstraints.RAM+container.RuntimeConstraints.KeepCacheRAM) / float64(1048576)))
169 for _, m := range container.Mounts {
174 disk = int64(math.Ceil(float64(disk) / float64(1048576)))
176 var sbatchArgs []string
177 sbatchArgs = append(sbatchArgs, theConfig.SbatchArguments...)
178 sbatchArgs = append(sbatchArgs, fmt.Sprintf("--job-name=%s", container.UUID))
179 sbatchArgs = append(sbatchArgs, fmt.Sprintf("--mem=%d", mem))
180 sbatchArgs = append(sbatchArgs, fmt.Sprintf("--cpus-per-task=%d", container.RuntimeConstraints.VCPUs))
181 sbatchArgs = append(sbatchArgs, fmt.Sprintf("--tmp=%d", disk))
182 sbatchArgs = append(sbatchArgs, fmt.Sprintf("--nice=%d", niceness(container.Priority)))
183 if len(container.SchedulingParameters.Partitions) > 0 {
184 sbatchArgs = append(sbatchArgs, fmt.Sprintf("--partition=%s", strings.Join(container.SchedulingParameters.Partitions, ",")))
187 return exec.Command("sbatch", sbatchArgs...)
191 func scancelFunc(container arvados.Container) *exec.Cmd {
192 return exec.Command("scancel", "--name="+container.UUID)
196 func scontrolFunc(container arvados.Container) *exec.Cmd {
197 return exec.Command("scontrol", "update", "JobName="+container.UUID, fmt.Sprintf("--nice=%d", niceness(container.Priority)))
200 // Wrap these so that they can be overridden by tests
201 var sbatchCmd = sbatchFunc
202 var scancelCmd = scancelFunc
203 var scontrolCmd = scontrolFunc
205 // Submit job to slurm using sbatch.
206 func submit(dispatcher *dispatch.Dispatcher, container arvados.Container, crunchRunCommand []string) error {
207 cmd := sbatchCmd(container)
209 // Send a tiny script on stdin to execute the crunch-run
210 // command (slurm requires this to be a #! script)
211 cmd.Stdin = strings.NewReader(execScript(append(crunchRunCommand, container.UUID)))
213 var stdout, stderr bytes.Buffer
217 // Mutex between squeue sync and running sbatch or scancel.
219 defer sqCheck.L.Unlock()
221 log.Printf("exec sbatch %+q", cmd.Args)
226 log.Printf("sbatch succeeded: %q", strings.TrimSpace(stdout.String()))
229 case *exec.ExitError:
230 dispatcher.Unlock(container.UUID)
231 return fmt.Errorf("sbatch %+q failed: %v (stderr: %q)", cmd.Args, err, stderr.Bytes())
234 dispatcher.Unlock(container.UUID)
235 return fmt.Errorf("exec failed: %v", err)
239 // Submit a container to the slurm queue (or resume monitoring if it's
240 // already in the queue). Cancel the slurm job if the container's
241 // priority changes to zero or its state indicates it's no longer
243 func run(disp *dispatch.Dispatcher, ctr arvados.Container, status <-chan arvados.Container) {
244 ctx, cancel := context.WithCancel(context.Background())
247 if ctr.State == dispatch.Locked && !sqCheck.HasUUID(ctr.UUID) {
248 log.Printf("Submitting container %s to slurm", ctr.UUID)
249 if err := submit(disp, ctr, theConfig.CrunchRunCommand); err != nil {
250 text := fmt.Sprintf("Error submitting container %s to slurm: %s", ctr.UUID, err)
253 lr := arvadosclient.Dict{"log": arvadosclient.Dict{
254 "object_uuid": ctr.UUID,
255 "event_type": "dispatch",
256 "properties": map[string]string{"text": text}}}
257 disp.Arv.Create("logs", lr, nil)
259 disp.Unlock(ctr.UUID)
264 log.Printf("Start monitoring container %v in state %q", ctr.UUID, ctr.State)
265 defer log.Printf("Done monitoring container %s", ctr.UUID)
267 // If the container disappears from the slurm queue, there is
268 // no point in waiting for further dispatch updates: just
269 // clean up and return.
270 go func(uuid string) {
271 for ctx.Err() == nil && sqCheck.HasUUID(uuid) {
279 // Disappeared from squeue
280 if err := disp.Arv.Get("containers", ctr.UUID, nil, &ctr); err != nil {
281 log.Printf("Error getting final container state for %s: %s", ctr.UUID, err)
284 case dispatch.Running:
285 disp.UpdateState(ctr.UUID, dispatch.Cancelled)
286 case dispatch.Locked:
287 disp.Unlock(ctr.UUID)
290 case updated, ok := <-status:
292 log.Printf("Dispatcher says container %s is done: cancel slurm job", ctr.UUID)
294 } else if updated.Priority == 0 {
295 log.Printf("Container %s has state %q, priority %d: cancel slurm job", ctr.UUID, updated.State, updated.Priority)
297 } else if niceness(updated.Priority) != sqCheck.GetNiceness(ctr.UUID) && sqCheck.GetNiceness(ctr.UUID) != -1 {
298 // dynamically adjust priority
299 scontrolUpdate(updated)
305 func scancel(ctr arvados.Container) {
307 cmd := scancelCmd(ctr)
308 msg, err := cmd.CombinedOutput()
312 log.Printf("%q %q: %s %q", cmd.Path, cmd.Args, err, msg)
313 time.Sleep(time.Second)
314 } else if sqCheck.HasUUID(ctr.UUID) {
315 log.Printf("container %s is still in squeue after scancel", ctr.UUID)
316 time.Sleep(time.Second)
320 func scontrolUpdate(ctr arvados.Container) {
322 cmd := scontrolCmd(ctr)
323 msg, err := cmd.CombinedOutput()
327 log.Printf("%q %q: %s %q", cmd.Path, cmd.Args, err, msg)
328 time.Sleep(time.Second)
329 } else if sqCheck.HasUUID(ctr.UUID) {
330 log.Printf("Container %s priority is now %v, niceness is now %v",
331 ctr.UUID, ctr.Priority, sqCheck.GetNiceness(ctr.UUID))
335 func readConfig(dst interface{}, path string) error {
336 err := config.LoadFile(dst, path)
337 if err != nil && os.IsNotExist(err) && path == defaultConfigPath {
338 log.Printf("Config not specified. Continue with default configuration.")