14495: Add dispatchcloud.EstimateScratchSpace()
[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         "regexp"
12         "sort"
13         "strconv"
14         "strings"
15         "time"
16
17         "git.curoverse.com/arvados.git/sdk/go/arvados"
18 )
19
20 var (
21         ErrInstanceTypesNotConfigured = errors.New("site configuration does not list any instance types")
22         discountConfiguredRAMPercent  = 5
23 )
24
25 // ConstraintsNotSatisfiableError includes a list of available instance types
26 // to be reported back to the user.
27 type ConstraintsNotSatisfiableError struct {
28         error
29         AvailableTypes []arvados.InstanceType
30 }
31
32 var pdhRegexp = regexp.MustCompile(`^[0-9a-f]{32}\+(\d+)$`)
33
34 // estimateDockerImageSize estimates how much disk space will be used
35 // by a Docker image, given the PDH of a collection containing a
36 // Docker image that was created by "arv-keepdocker".  Returns
37 // estimated number of bytes of disk space that should be reserved.
38 func estimateDockerImageSize(collectionPDH string) int64 {
39         m := pdhRegexp.FindStringSubmatch(collectionPDH)
40         if m == nil {
41                 return 0
42         }
43         n, err := strconv.ParseInt(m[1], 10, 64)
44         if err != nil || n < 122 {
45                 return 0
46         }
47         // To avoid having to fetch the collection, take advantage of
48         // the fact that the manifest storing a container image
49         // uploaded by arv-keepdocker has a predictable format, which
50         // allows us to estimate the size of the image based on just
51         // the size of the manifest.
52         //
53         // Use the following heuristic:
54         // - Start with the length of the mainfest (n)
55         // - Subtract 80 characters for the filename and file segment
56         // - Divide by 42 to get the number of block identifiers ('hash\+size\ ' is 32+1+8+1)
57         // - Assume each block is full, multiply by 64 MiB
58         return ((n - 80) / 42) * (64 * 1024 * 1024)
59 }
60
61 // EstimateScratchSpace estimates how much available disk space (in
62 // bytes) is needed to run the container by summing the capacity
63 // requested by 'tmp' mounts plus disk space required to load the
64 // Docker image.
65 func EstimateScratchSpace(ctr *arvados.Container) (needScratch int64) {
66         for _, m := range ctr.Mounts {
67                 if m.Kind == "tmp" {
68                         needScratch += m.Capacity
69                 }
70         }
71
72         // Account for disk space usage by Docker, assumes the following behavior:
73         // - Layer tarballs are buffered to disk during "docker load".
74         // - Individual layer tarballs are extracted from buffered
75         // copy to the filesystem
76         dockerImageSize := estimateDockerImageSize(ctr.ContainerImage)
77
78         // The buffer is only needed during image load, so make sure
79         // the baseline scratch space at least covers dockerImageSize,
80         // and assume it will be released to the job afterwards.
81         if needScratch < dockerImageSize {
82                 needScratch = dockerImageSize
83         }
84
85         // Now reserve space for the extracted image on disk.
86         needScratch += dockerImageSize
87
88         return
89 }
90
91 // ChooseInstanceType returns the cheapest available
92 // arvados.InstanceType big enough to run ctr.
93 func ChooseInstanceType(cc *arvados.Cluster, ctr *arvados.Container) (best arvados.InstanceType, err error) {
94         if len(cc.InstanceTypes) == 0 {
95                 err = ErrInstanceTypesNotConfigured
96                 return
97         }
98
99         needScratch := EstimateScratchSpace(ctr)
100
101         needVCPUs := ctr.RuntimeConstraints.VCPUs
102
103         needRAM := ctr.RuntimeConstraints.RAM + ctr.RuntimeConstraints.KeepCacheRAM
104         needRAM = (needRAM * 100) / int64(100-discountConfiguredRAMPercent)
105
106         ok := false
107         for _, it := range cc.InstanceTypes {
108                 switch {
109                 case ok && it.Price > best.Price:
110                 case int64(it.Scratch) < needScratch:
111                 case int64(it.RAM) < needRAM:
112                 case it.VCPUs < needVCPUs:
113                 case it.Preemptible != ctr.SchedulingParameters.Preemptible:
114                 case it.Price == best.Price && (it.RAM < best.RAM || it.VCPUs < best.VCPUs):
115                         // Equal price, but worse specs
116                 default:
117                         // Lower price || (same price && better specs)
118                         best = it
119                         ok = true
120                 }
121         }
122         if !ok {
123                 availableTypes := make([]arvados.InstanceType, 0, len(cc.InstanceTypes))
124                 for _, t := range cc.InstanceTypes {
125                         availableTypes = append(availableTypes, t)
126                 }
127                 sort.Slice(availableTypes, func(a, b int) bool {
128                         return availableTypes[a].Price < availableTypes[b].Price
129                 })
130                 err = ConstraintsNotSatisfiableError{
131                         errors.New("constraints not satisfiable by any configured instance type"),
132                         availableTypes,
133                 }
134                 return
135         }
136         return
137 }
138
139 // SlurmNodeTypeFeatureKludge ensures SLURM accepts every instance
140 // type name as a valid feature name, even if no instances of that
141 // type have appeared yet.
142 //
143 // It takes advantage of some SLURM peculiarities:
144 //
145 // (1) A feature is valid after it has been offered by a node, even if
146 // it is no longer offered by any node. So, to make a feature name
147 // valid, we can add it to a dummy node ("compute0"), then remove it.
148 //
149 // (2) To test whether a set of feature names are valid without
150 // actually submitting a job, we can call srun --test-only with the
151 // desired features.
152 //
153 // SlurmNodeTypeFeatureKludge does a test-and-fix operation
154 // immediately, and then periodically, in case slurm restarts and
155 // forgets the list of valid features. It never returns (unless there
156 // are no node types configured, in which case it returns
157 // immediately), so it should generally be invoked with "go".
158 func SlurmNodeTypeFeatureKludge(cc *arvados.Cluster) {
159         if len(cc.InstanceTypes) == 0 {
160                 return
161         }
162         var features []string
163         for _, it := range cc.InstanceTypes {
164                 features = append(features, "instancetype="+it.Name)
165         }
166         for {
167                 slurmKludge(features)
168                 time.Sleep(2 * time.Second)
169         }
170 }
171
172 const slurmDummyNode = "compute0"
173
174 func slurmKludge(features []string) {
175         allFeatures := strings.Join(features, ",")
176
177         cmd := exec.Command("sinfo", "--nodes="+slurmDummyNode, "--format=%f", "--noheader")
178         out, err := cmd.CombinedOutput()
179         if err != nil {
180                 log.Printf("running %q %q: %s (output was %q)", cmd.Path, cmd.Args, err, out)
181                 return
182         }
183         if string(out) == allFeatures+"\n" {
184                 // Already configured correctly, nothing to do.
185                 return
186         }
187
188         log.Printf("configuring node %q with all node type features", slurmDummyNode)
189         cmd = exec.Command("scontrol", "update", "NodeName="+slurmDummyNode, "Features="+allFeatures)
190         log.Printf("running: %q %q", cmd.Path, cmd.Args)
191         out, err = cmd.CombinedOutput()
192         if err != nil {
193                 log.Printf("error: scontrol: %s (output was %q)", err, out)
194         }
195 }