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