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