1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
13 "git.arvados.org/arvados.git/sdk/go/arvados"
16 var ErrInstanceTypesNotConfigured = errors.New("site configuration does not list any instance types")
18 var discountConfiguredRAMPercent = 5
20 // ConstraintsNotSatisfiableError includes a list of available instance types
21 // to be reported back to the user.
22 type ConstraintsNotSatisfiableError struct {
24 AvailableTypes []arvados.InstanceType
27 var pdhRegexp = regexp.MustCompile(`^[0-9a-f]{32}\+(\d+)$`)
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)
38 n, err := strconv.ParseInt(m[1], 10, 64)
39 if err != nil || n < 122 {
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.
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)
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
60 func EstimateScratchSpace(ctr *arvados.Container) (needScratch int64) {
61 for _, m := range ctr.Mounts {
63 needScratch += m.Capacity
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)
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
80 // Now reserve space for the extracted image on disk.
81 needScratch += dockerImageSize
86 // ChooseInstanceType returns the cheapest available
87 // arvados.InstanceType big enough to run ctr.
88 func ChooseInstanceType(cc *arvados.Cluster, ctr *arvados.Container) (best arvados.InstanceType, err error) {
89 if len(cc.InstanceTypes) == 0 {
90 err = ErrInstanceTypesNotConfigured
94 needScratch := EstimateScratchSpace(ctr)
96 needVCPUs := ctr.RuntimeConstraints.VCPUs
98 needRAM := ctr.RuntimeConstraints.RAM + ctr.RuntimeConstraints.KeepCacheRAM
99 needRAM += int64(cc.Containers.ReserveExtraRAM)
100 needRAM = (needRAM * 100) / int64(100-discountConfiguredRAMPercent)
103 for _, it := range cc.InstanceTypes {
105 case ok && it.Price > best.Price:
106 case int64(it.Scratch) < needScratch:
107 case int64(it.RAM) < needRAM:
108 case it.VCPUs < needVCPUs:
109 case it.Preemptible != ctr.SchedulingParameters.Preemptible:
110 case it.Price == best.Price && (it.RAM < best.RAM || it.VCPUs < best.VCPUs):
111 // Equal price, but worse specs
113 // Lower price || (same price && better specs)
119 availableTypes := make([]arvados.InstanceType, 0, len(cc.InstanceTypes))
120 for _, t := range cc.InstanceTypes {
121 availableTypes = append(availableTypes, t)
123 sort.Slice(availableTypes, func(a, b int) bool {
124 return availableTypes[a].Price < availableTypes[b].Price
126 err = ConstraintsNotSatisfiableError{
127 errors.New("constraints not satisfiable by any configured instance type"),