20259: Add documentation for banner and tooltip features
[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         "regexp"
10         "sort"
11         "strconv"
12
13         "git.arvados.org/arvados.git/sdk/go/arvados"
14 )
15
16 var ErrInstanceTypesNotConfigured = errors.New("site configuration does not list any instance types")
17
18 var discountConfiguredRAMPercent = 5
19
20 // ConstraintsNotSatisfiableError includes a list of available instance types
21 // to be reported back to the user.
22 type ConstraintsNotSatisfiableError struct {
23         error
24         AvailableTypes []arvados.InstanceType
25 }
26
27 var pdhRegexp = regexp.MustCompile(`^[0-9a-f]{32}\+(\d+)$`)
28
29 // estimateDockerImageSize estimates how much disk space will be used
30 // by a Docker image, given the PDH of a collection containing a
31 // Docker image that was created by "arv-keepdocker".  Returns
32 // estimated number of bytes of disk space that should be reserved.
33 func estimateDockerImageSize(collectionPDH string) int64 {
34         m := pdhRegexp.FindStringSubmatch(collectionPDH)
35         if m == nil {
36                 return 0
37         }
38         n, err := strconv.ParseInt(m[1], 10, 64)
39         if err != nil || n < 122 {
40                 return 0
41         }
42         // To avoid having to fetch the collection, take advantage of
43         // the fact that the manifest storing a container image
44         // uploaded by arv-keepdocker has a predictable format, which
45         // allows us to estimate the size of the image based on just
46         // the size of the manifest.
47         //
48         // Use the following heuristic:
49         // - Start with the length of the manifest (n)
50         // - Subtract 80 characters for the filename and file segment
51         // - Divide by 42 to get the number of block identifiers ('hash\+size\ ' is 32+1+8+1)
52         // - Assume each block is full, multiply by 64 MiB
53         return ((n - 80) / 42) * (64 * 1024 * 1024)
54 }
55
56 // EstimateScratchSpace estimates how much available disk space (in
57 // bytes) is needed to run the container by summing the capacity
58 // requested by 'tmp' mounts plus disk space required to load the
59 // Docker image plus arv-mount block cache.
60 func EstimateScratchSpace(ctr *arvados.Container) (needScratch int64) {
61         for _, m := range ctr.Mounts {
62                 if m.Kind == "tmp" {
63                         needScratch += m.Capacity
64                 }
65         }
66
67         // Account for disk space usage by Docker, assumes the following behavior:
68         // - Layer tarballs are buffered to disk during "docker load".
69         // - Individual layer tarballs are extracted from buffered
70         // copy to the filesystem
71         dockerImageSize := estimateDockerImageSize(ctr.ContainerImage)
72
73         // The buffer is only needed during image load, so make sure
74         // the baseline scratch space at least covers dockerImageSize,
75         // and assume it will be released to the job afterwards.
76         if needScratch < dockerImageSize {
77                 needScratch = dockerImageSize
78         }
79
80         // Now reserve space for the extracted image on disk.
81         needScratch += dockerImageSize
82
83         // Now reserve space the arv-mount disk cache
84         needScratch += ctr.RuntimeConstraints.KeepCacheDisk
85
86         return
87 }
88
89 // compareVersion returns true if vs1 < vs2, otherwise false
90 func versionLess(vs1 string, vs2 string) (bool, error) {
91         v1, err := strconv.ParseFloat(vs1, 64)
92         if err != nil {
93                 return false, err
94         }
95         v2, err := strconv.ParseFloat(vs2, 64)
96         if err != nil {
97                 return false, err
98         }
99         return v1 < v2, nil
100 }
101
102 // ChooseInstanceType returns the cheapest available
103 // arvados.InstanceType big enough to run ctr.
104 func ChooseInstanceType(cc *arvados.Cluster, ctr *arvados.Container) (best arvados.InstanceType, err error) {
105         if len(cc.InstanceTypes) == 0 {
106                 err = ErrInstanceTypesNotConfigured
107                 return
108         }
109
110         needScratch := EstimateScratchSpace(ctr)
111
112         needVCPUs := ctr.RuntimeConstraints.VCPUs
113
114         needRAM := ctr.RuntimeConstraints.RAM + ctr.RuntimeConstraints.KeepCacheRAM
115         needRAM += int64(cc.Containers.ReserveExtraRAM)
116         if cc.Containers.LocalKeepBlobBuffersPerVCPU > 0 {
117                 // + 200 MiB for keepstore process + 10% for GOGC=10
118                 needRAM += 220 << 20
119                 // + 64 MiB for each blob buffer + 10% for GOGC=10
120                 needRAM += int64(cc.Containers.LocalKeepBlobBuffersPerVCPU * needVCPUs * (1 << 26) * 11 / 10)
121         }
122         needRAM = (needRAM * 100) / int64(100-discountConfiguredRAMPercent)
123
124         ok := false
125         for _, it := range cc.InstanceTypes {
126                 driverInsuff, driverErr := versionLess(it.CUDA.DriverVersion, ctr.RuntimeConstraints.CUDA.DriverVersion)
127                 capabilityInsuff, capabilityErr := versionLess(it.CUDA.HardwareCapability, ctr.RuntimeConstraints.CUDA.HardwareCapability)
128
129                 switch {
130                 // reasons to reject a node
131                 case ok && it.Price > best.Price: // already selected a node, and this one is more expensive
132                 case int64(it.Scratch) < needScratch: // insufficient scratch
133                 case int64(it.RAM) < needRAM: // insufficient RAM
134                 case it.VCPUs < needVCPUs: // insufficient VCPUs
135                 case it.Preemptible != ctr.SchedulingParameters.Preemptible: // wrong preemptable setting
136                 case it.Price == best.Price && (it.RAM < best.RAM || it.VCPUs < best.VCPUs): // same price, worse specs
137                 case it.CUDA.DeviceCount < ctr.RuntimeConstraints.CUDA.DeviceCount: // insufficient CUDA devices
138                 case ctr.RuntimeConstraints.CUDA.DeviceCount > 0 && (driverInsuff || driverErr != nil): // insufficient driver version
139                 case ctr.RuntimeConstraints.CUDA.DeviceCount > 0 && (capabilityInsuff || capabilityErr != nil): // insufficient hardware capability
140                         // Don't select this node
141                 default:
142                         // Didn't reject the node, so select it
143                         // Lower price || (same price && better specs)
144                         best = it
145                         ok = true
146                 }
147         }
148         if !ok {
149                 availableTypes := make([]arvados.InstanceType, 0, len(cc.InstanceTypes))
150                 for _, t := range cc.InstanceTypes {
151                         availableTypes = append(availableTypes, t)
152                 }
153                 sort.Slice(availableTypes, func(a, b int) bool {
154                         return availableTypes[a].Price < availableTypes[b].Price
155                 })
156                 err = ConstraintsNotSatisfiableError{
157                         errors.New("constraints not satisfiable by any configured instance type"),
158                         availableTypes,
159                 }
160                 return
161         }
162         return
163 }