Merge branch 'master' into 11850-singlecontainer-max-requirements
[arvados.git] / lib / dispatchcloud / node_size.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package dispatchcloud
6
7 import (
8         "bytes"
9         "errors"
10         "log"
11         "os/exec"
12         "strings"
13         "time"
14
15         "git.curoverse.com/arvados.git/sdk/go/arvados"
16 )
17
18 var (
19         ErrConstraintsNotSatisfiable  = errors.New("constraints not satisfiable by any configured instance type")
20         ErrInstanceTypesNotConfigured = errors.New("site configuration does not list any instance types")
21         discountConfiguredRAMPercent  = 5
22 )
23
24 // ChooseInstanceType returns the cheapest available
25 // arvados.InstanceType big enough to run ctr.
26 func ChooseInstanceType(cc *arvados.Cluster, ctr *arvados.Container) (best arvados.InstanceType, err error) {
27         if len(cc.InstanceTypes) == 0 {
28                 err = ErrInstanceTypesNotConfigured
29                 return
30         }
31
32         needScratch := int64(0)
33         for _, m := range ctr.Mounts {
34                 if m.Kind == "tmp" {
35                         needScratch += m.Capacity
36                 }
37         }
38
39         needVCPUs := ctr.RuntimeConstraints.VCPUs
40
41         needRAM := ctr.RuntimeConstraints.RAM + ctr.RuntimeConstraints.KeepCacheRAM
42         needRAM = (needRAM * 100) / int64(100-discountConfiguredRAMPercent)
43
44         err = ErrConstraintsNotSatisfiable
45         for _, it := range cc.InstanceTypes {
46                 switch {
47                 case err == nil && it.Price > best.Price:
48                 case it.Scratch < needScratch:
49                 case it.RAM < needRAM:
50                 case it.VCPUs < needVCPUs:
51                 case it.Price == best.Price && (it.RAM < best.RAM || it.VCPUs < best.VCPUs):
52                         // Equal price, but worse specs
53                 default:
54                         // Lower price || (same price && better specs)
55                         best = it
56                         err = nil
57                 }
58         }
59         return
60 }
61
62 // SlurmNodeTypeFeatureKludge ensures SLURM accepts every instance
63 // type name as a valid feature name, even if no instances of that
64 // type have appeared yet.
65 //
66 // It takes advantage of some SLURM peculiarities:
67 //
68 // (1) A feature is valid after it has been offered by a node, even if
69 // it is no longer offered by any node. So, to make a feature name
70 // valid, we can add it to a dummy node ("compute0"), then remove it.
71 //
72 // (2) when srun is given an invalid --gres argument and an invalid
73 // --constraint argument, the error message mentions "Invalid feature
74 // specification". So, to test whether a feature name is valid without
75 // actually submitting a job, we can call srun with the feature name
76 // and an invalid --gres argument.
77 //
78 // SlurmNodeTypeFeatureKludge does a test-and-fix operation
79 // immediately, and then periodically, in case slurm restarts and
80 // forgets the list of valid features. It never returns (unless there
81 // are no node types configured, in which case it returns
82 // immediately), so it should generally be invoked with "go".
83 func SlurmNodeTypeFeatureKludge(cc *arvados.Cluster) {
84         if len(cc.InstanceTypes) == 0 {
85                 return
86         }
87         var features []string
88         for _, it := range cc.InstanceTypes {
89                 features = append(features, "instancetype="+it.Name)
90         }
91         for {
92                 slurmKludge(features)
93                 time.Sleep(time.Minute)
94         }
95 }
96
97 var (
98         slurmDummyNode     = "compute0"
99         slurmErrBadFeature = "Invalid feature"
100         slurmErrBadGres    = "Invalid generic resource"
101 )
102
103 func slurmKludge(features []string) {
104         cmd := exec.Command("srun", "--gres=invalid-gres-specification", "--constraint="+strings.Join(features, "&"), "true")
105         out, err := cmd.CombinedOutput()
106         switch {
107         case err == nil:
108                 log.Printf("warning: guaranteed-to-fail srun command did not fail: %q %q", cmd.Path, cmd.Args)
109                 log.Printf("output was: %q", out)
110
111         case bytes.Contains(out, []byte(slurmErrBadFeature)):
112                 log.Printf("temporarily configuring node %q with all node type features", slurmDummyNode)
113                 for _, nodeFeatures := range []string{strings.Join(features, ","), ""} {
114                         cmd = exec.Command("scontrol", "update", "NodeName="+slurmDummyNode, "Features="+nodeFeatures)
115                         log.Printf("running: %q %q", cmd.Path, cmd.Args)
116                         out, err := cmd.CombinedOutput()
117                         if err != nil {
118                                 log.Printf("error: scontrol: %s (output was %q)", err, out)
119                         }
120                 }
121
122         case bytes.Contains(out, []byte(slurmErrBadGres)):
123                 // Evidently our node-type feature names are all valid.
124
125         default:
126                 log.Printf("warning: expected srun error %q or %q, but output was %q", slurmErrBadFeature, slurmErrBadGres, out)
127         }
128 }