Merge branch 'master' into 13822-nm-delayed-daemon
[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         "errors"
9         "log"
10         "os/exec"
11         "sort"
12         "strings"
13         "time"
14
15         "git.curoverse.com/arvados.git/sdk/go/arvados"
16 )
17
18 var (
19         ErrInstanceTypesNotConfigured = errors.New("site configuration does not list any instance types")
20         discountConfiguredRAMPercent  = 5
21 )
22
23 // ConstraintsNotSatisfiableError includes a list of available instance types
24 // to be reported back to the user.
25 type ConstraintsNotSatisfiableError struct {
26         error
27         AvailableTypes []arvados.InstanceType
28 }
29
30 // ChooseInstanceType returns the cheapest available
31 // arvados.InstanceType big enough to run ctr.
32 func ChooseInstanceType(cc *arvados.Cluster, ctr *arvados.Container) (best arvados.InstanceType, err error) {
33         if len(cc.InstanceTypes) == 0 {
34                 err = ErrInstanceTypesNotConfigured
35                 return
36         }
37
38         needScratch := int64(0)
39         for _, m := range ctr.Mounts {
40                 if m.Kind == "tmp" {
41                         needScratch += m.Capacity
42                 }
43         }
44
45         needVCPUs := ctr.RuntimeConstraints.VCPUs
46
47         needRAM := ctr.RuntimeConstraints.RAM + ctr.RuntimeConstraints.KeepCacheRAM
48         needRAM = (needRAM * 100) / int64(100-discountConfiguredRAMPercent)
49
50         ok := false
51         for _, it := range cc.InstanceTypes {
52                 switch {
53                 case ok && it.Price > best.Price:
54                 case int64(it.Scratch) < needScratch:
55                 case int64(it.RAM) < needRAM:
56                 case it.VCPUs < needVCPUs:
57                 case it.Preemptible != ctr.SchedulingParameters.Preemptible:
58                 case it.Price == best.Price && (it.RAM < best.RAM || it.VCPUs < best.VCPUs):
59                         // Equal price, but worse specs
60                 default:
61                         // Lower price || (same price && better specs)
62                         best = it
63                         ok = true
64                 }
65         }
66         if !ok {
67                 availableTypes := make([]arvados.InstanceType, 0, len(cc.InstanceTypes))
68                 for _, t := range cc.InstanceTypes {
69                         availableTypes = append(availableTypes, t)
70                 }
71                 sort.Slice(availableTypes, func(a, b int) bool {
72                         return availableTypes[a].Price < availableTypes[b].Price
73                 })
74                 err = ConstraintsNotSatisfiableError{
75                         errors.New("constraints not satisfiable by any configured instance type"),
76                         availableTypes,
77                 }
78                 return
79         }
80         return
81 }
82
83 // SlurmNodeTypeFeatureKludge ensures SLURM accepts every instance
84 // type name as a valid feature name, even if no instances of that
85 // type have appeared yet.
86 //
87 // It takes advantage of some SLURM peculiarities:
88 //
89 // (1) A feature is valid after it has been offered by a node, even if
90 // it is no longer offered by any node. So, to make a feature name
91 // valid, we can add it to a dummy node ("compute0"), then remove it.
92 //
93 // (2) To test whether a set of feature names are valid without
94 // actually submitting a job, we can call srun --test-only with the
95 // desired features.
96 //
97 // SlurmNodeTypeFeatureKludge does a test-and-fix operation
98 // immediately, and then periodically, in case slurm restarts and
99 // forgets the list of valid features. It never returns (unless there
100 // are no node types configured, in which case it returns
101 // immediately), so it should generally be invoked with "go".
102 func SlurmNodeTypeFeatureKludge(cc *arvados.Cluster) {
103         if len(cc.InstanceTypes) == 0 {
104                 return
105         }
106         var features []string
107         for _, it := range cc.InstanceTypes {
108                 features = append(features, "instancetype="+it.Name)
109         }
110         for {
111                 slurmKludge(features)
112                 time.Sleep(2 * time.Second)
113         }
114 }
115
116 const slurmDummyNode = "compute0"
117
118 func slurmKludge(features []string) {
119         allFeatures := strings.Join(features, ",")
120
121         cmd := exec.Command("sinfo", "--nodes="+slurmDummyNode, "--format=%f", "--noheader")
122         out, err := cmd.CombinedOutput()
123         if err != nil {
124                 log.Printf("running %q %q: %s (output was %q)", cmd.Path, cmd.Args, err, out)
125                 return
126         }
127         if string(out) == allFeatures+"\n" {
128                 // Already configured correctly, nothing to do.
129                 return
130         }
131
132         log.Printf("configuring node %q with all node type features", slurmDummyNode)
133         cmd = exec.Command("scontrol", "update", "NodeName="+slurmDummyNode, "Features="+allFeatures)
134         log.Printf("running: %q %q", cmd.Path, cmd.Args)
135         out, err = cmd.CombinedOutput()
136         if err != nil {
137                 log.Printf("error: scontrol: %s (output was %q)", err, out)
138         }
139 }